1 /*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20 #include <linux/string.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/scatterlist.h>
24
25 #include "usbip_common.h"
26 #include "stub.h"
27
28 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
29 #define DRIVER_DESC "USB/IP Host Driver"
30
31 struct kmem_cache *stub_priv_cache;
32
33 /*
34 * busid_tables defines matching busids that usbip can grab. A user can change
35 * dynamically what device is locally used and what device is exported to a
36 * remote host.
37 */
38 #define MAX_BUSID 16
39 static struct bus_id_priv busid_table[MAX_BUSID];
40 static spinlock_t busid_table_lock;
41
init_busid_table(void)42 static void init_busid_table(void)
43 {
44 int i;
45
46 /*
47 * This also sets the bus_table[i].status to
48 * STUB_BUSID_OTHER, which is 0.
49 */
50 memset(busid_table, 0, sizeof(busid_table));
51
52 spin_lock_init(&busid_table_lock);
53
54 for (i = 0; i < MAX_BUSID; i++)
55 spin_lock_init(&busid_table[i].busid_lock);
56 }
57
58 /*
59 * Find the index of the busid by name.
60 * Must be called with busid_table_lock held.
61 */
get_busid_idx(const char * busid)62 static int get_busid_idx(const char *busid)
63 {
64 int i;
65 int idx = -1;
66
67 for (i = 0; i < MAX_BUSID; i++) {
68 spin_lock(&busid_table[i].busid_lock);
69 if (busid_table[i].name[0])
70 if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
71 idx = i;
72 spin_unlock(&busid_table[i].busid_lock);
73 break;
74 }
75 spin_unlock(&busid_table[i].busid_lock);
76 }
77 return idx;
78 }
79
80 /* Returns holding busid_lock. Should call put_busid_priv() to unlock */
get_busid_priv(const char * busid)81 struct bus_id_priv *get_busid_priv(const char *busid)
82 {
83 int idx;
84 struct bus_id_priv *bid = NULL;
85
86 spin_lock(&busid_table_lock);
87 idx = get_busid_idx(busid);
88 if (idx >= 0) {
89 bid = &(busid_table[idx]);
90 /* get busid_lock before returning */
91 spin_lock(&bid->busid_lock);
92 }
93 spin_unlock(&busid_table_lock);
94
95 return bid;
96 }
97
put_busid_priv(struct bus_id_priv * bid)98 void put_busid_priv(struct bus_id_priv *bid)
99 {
100 if (bid)
101 spin_unlock(&bid->busid_lock);
102 }
103
add_match_busid(char * busid)104 static int add_match_busid(char *busid)
105 {
106 int i;
107 int ret = -1;
108
109 spin_lock(&busid_table_lock);
110 /* already registered? */
111 if (get_busid_idx(busid) >= 0) {
112 ret = 0;
113 goto out;
114 }
115
116 for (i = 0; i < MAX_BUSID; i++) {
117 spin_lock(&busid_table[i].busid_lock);
118 if (!busid_table[i].name[0]) {
119 strlcpy(busid_table[i].name, busid, BUSID_SIZE);
120 if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
121 (busid_table[i].status != STUB_BUSID_REMOV))
122 busid_table[i].status = STUB_BUSID_ADDED;
123 ret = 0;
124 spin_unlock(&busid_table[i].busid_lock);
125 break;
126 }
127 spin_unlock(&busid_table[i].busid_lock);
128 }
129
130 out:
131 spin_unlock(&busid_table_lock);
132
133 return ret;
134 }
135
del_match_busid(char * busid)136 int del_match_busid(char *busid)
137 {
138 int idx;
139 int ret = -1;
140
141 spin_lock(&busid_table_lock);
142 idx = get_busid_idx(busid);
143 if (idx < 0)
144 goto out;
145
146 /* found */
147 ret = 0;
148
149 spin_lock(&busid_table[idx].busid_lock);
150
151 if (busid_table[idx].status == STUB_BUSID_OTHER)
152 memset(busid_table[idx].name, 0, BUSID_SIZE);
153
154 if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
155 (busid_table[idx].status != STUB_BUSID_ADDED))
156 busid_table[idx].status = STUB_BUSID_REMOV;
157
158 spin_unlock(&busid_table[idx].busid_lock);
159 out:
160 spin_unlock(&busid_table_lock);
161
162 return ret;
163 }
164
match_busid_show(struct device_driver * drv,char * buf)165 static ssize_t match_busid_show(struct device_driver *drv, char *buf)
166 {
167 int i;
168 char *out = buf;
169
170 spin_lock(&busid_table_lock);
171 for (i = 0; i < MAX_BUSID; i++) {
172 spin_lock(&busid_table[i].busid_lock);
173 if (busid_table[i].name[0])
174 out += sprintf(out, "%s ", busid_table[i].name);
175 spin_unlock(&busid_table[i].busid_lock);
176 }
177 spin_unlock(&busid_table_lock);
178 out += sprintf(out, "\n");
179
180 return out - buf;
181 }
182
match_busid_store(struct device_driver * dev,const char * buf,size_t count)183 static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
184 size_t count)
185 {
186 int len;
187 char busid[BUSID_SIZE];
188
189 if (count < 5)
190 return -EINVAL;
191
192 /* busid needs to include \0 termination */
193 len = strlcpy(busid, buf + 4, BUSID_SIZE);
194 if (sizeof(busid) <= len)
195 return -EINVAL;
196
197 if (!strncmp(buf, "add ", 4)) {
198 if (add_match_busid(busid) < 0)
199 return -ENOMEM;
200
201 pr_debug("add busid %s\n", busid);
202 return count;
203 }
204
205 if (!strncmp(buf, "del ", 4)) {
206 if (del_match_busid(busid) < 0)
207 return -ENODEV;
208
209 pr_debug("del busid %s\n", busid);
210 return count;
211 }
212
213 return -EINVAL;
214 }
215 static DRIVER_ATTR_RW(match_busid);
216
do_rebind(char * busid,struct bus_id_priv * busid_priv)217 static int do_rebind(char *busid, struct bus_id_priv *busid_priv)
218 {
219 int ret;
220
221 /* device_attach() callers should hold parent lock for USB */
222 if (busid_priv->udev->dev.parent)
223 device_lock(busid_priv->udev->dev.parent);
224 ret = device_attach(&busid_priv->udev->dev);
225 if (busid_priv->udev->dev.parent)
226 device_unlock(busid_priv->udev->dev.parent);
227 if (ret < 0) {
228 dev_err(&busid_priv->udev->dev, "rebind failed\n");
229 return ret;
230 }
231 return 0;
232 }
233
stub_device_rebind(void)234 static void stub_device_rebind(void)
235 {
236 #if IS_MODULE(CONFIG_USBIP_HOST)
237 struct bus_id_priv *busid_priv;
238 int i;
239
240 /* update status to STUB_BUSID_OTHER so probe ignores the device */
241 spin_lock(&busid_table_lock);
242 for (i = 0; i < MAX_BUSID; i++) {
243 if (busid_table[i].name[0] &&
244 busid_table[i].shutdown_busid) {
245 busid_priv = &(busid_table[i]);
246 busid_priv->status = STUB_BUSID_OTHER;
247 }
248 }
249 spin_unlock(&busid_table_lock);
250
251 /* now run rebind - no need to hold locks. driver files are removed */
252 for (i = 0; i < MAX_BUSID; i++) {
253 if (busid_table[i].name[0] &&
254 busid_table[i].shutdown_busid) {
255 busid_priv = &(busid_table[i]);
256 do_rebind(busid_table[i].name, busid_priv);
257 }
258 }
259 #endif
260 }
261
rebind_store(struct device_driver * dev,const char * buf,size_t count)262 static ssize_t rebind_store(struct device_driver *dev, const char *buf,
263 size_t count)
264 {
265 int ret;
266 int len;
267 struct bus_id_priv *bid;
268
269 /* buf length should be less that BUSID_SIZE */
270 len = strnlen(buf, BUSID_SIZE);
271
272 if (!(len < BUSID_SIZE))
273 return -EINVAL;
274
275 bid = get_busid_priv(buf);
276 if (!bid)
277 return -ENODEV;
278
279 /* mark the device for deletion so probe ignores it during rescan */
280 bid->status = STUB_BUSID_OTHER;
281 /* release the busid lock */
282 put_busid_priv(bid);
283
284 ret = do_rebind((char *) buf, bid);
285 if (ret < 0)
286 return ret;
287
288 /* delete device from busid_table */
289 del_match_busid((char *) buf);
290
291 return count;
292 }
293
294 static DRIVER_ATTR_WO(rebind);
295
stub_priv_pop_from_listhead(struct list_head * listhead)296 static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
297 {
298 struct stub_priv *priv, *tmp;
299
300 list_for_each_entry_safe(priv, tmp, listhead, list) {
301 list_del_init(&priv->list);
302 return priv;
303 }
304
305 return NULL;
306 }
307
stub_free_priv_and_urb(struct stub_priv * priv)308 void stub_free_priv_and_urb(struct stub_priv *priv)
309 {
310 struct urb *urb;
311 int i;
312
313 for (i = 0; i < priv->num_urbs; i++) {
314 urb = priv->urbs[i];
315
316 if (!urb)
317 return;
318
319 kfree(urb->setup_packet);
320 urb->setup_packet = NULL;
321
322
323 if (urb->transfer_buffer && !priv->sgl) {
324 kfree(urb->transfer_buffer);
325 urb->transfer_buffer = NULL;
326 }
327
328 if (urb->num_sgs) {
329 sgl_free(urb->sg);
330 urb->sg = NULL;
331 urb->num_sgs = 0;
332 }
333
334 usb_free_urb(urb);
335 }
336 if (!list_empty(&priv->list))
337 list_del(&priv->list);
338 if (priv->sgl)
339 sgl_free(priv->sgl);
340 kfree(priv->urbs);
341 kmem_cache_free(stub_priv_cache, priv);
342 }
343
stub_priv_pop(struct stub_device * sdev)344 static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
345 {
346 unsigned long flags;
347 struct stub_priv *priv;
348
349 spin_lock_irqsave(&sdev->priv_lock, flags);
350
351 priv = stub_priv_pop_from_listhead(&sdev->priv_init);
352 if (priv)
353 goto done;
354
355 priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
356 if (priv)
357 goto done;
358
359 priv = stub_priv_pop_from_listhead(&sdev->priv_free);
360
361 done:
362 spin_unlock_irqrestore(&sdev->priv_lock, flags);
363
364 return priv;
365 }
366
stub_device_cleanup_urbs(struct stub_device * sdev)367 void stub_device_cleanup_urbs(struct stub_device *sdev)
368 {
369 struct stub_priv *priv;
370 int i;
371
372 dev_dbg(&sdev->udev->dev, "Stub device cleaning up urbs\n");
373
374 while ((priv = stub_priv_pop(sdev))) {
375 for (i = 0; i < priv->num_urbs; i++)
376 usb_kill_urb(priv->urbs[i]);
377
378 stub_free_priv_and_urb(priv);
379 }
380 }
381
usbip_host_init(void)382 static int __init usbip_host_init(void)
383 {
384 int ret;
385
386 init_busid_table();
387
388 stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
389 if (!stub_priv_cache) {
390 pr_err("kmem_cache_create failed\n");
391 return -ENOMEM;
392 }
393
394 ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
395 if (ret) {
396 pr_err("usb_register failed %d\n", ret);
397 goto err_usb_register;
398 }
399
400 ret = driver_create_file(&stub_driver.drvwrap.driver,
401 &driver_attr_match_busid);
402 if (ret) {
403 pr_err("driver_create_file failed\n");
404 goto err_create_file;
405 }
406
407 ret = driver_create_file(&stub_driver.drvwrap.driver,
408 &driver_attr_rebind);
409 if (ret) {
410 pr_err("driver_create_file failed\n");
411 goto err_create_file;
412 }
413
414 return ret;
415
416 err_create_file:
417 usb_deregister_device_driver(&stub_driver);
418 err_usb_register:
419 kmem_cache_destroy(stub_priv_cache);
420 return ret;
421 }
422
usbip_host_exit(void)423 static void __exit usbip_host_exit(void)
424 {
425 driver_remove_file(&stub_driver.drvwrap.driver,
426 &driver_attr_match_busid);
427
428 driver_remove_file(&stub_driver.drvwrap.driver,
429 &driver_attr_rebind);
430
431 /*
432 * deregister() calls stub_disconnect() for all devices. Device
433 * specific data is cleared in stub_disconnect().
434 */
435 usb_deregister_device_driver(&stub_driver);
436
437 /* initiate scan to attach devices */
438 stub_device_rebind();
439
440 kmem_cache_destroy(stub_priv_cache);
441 }
442
443 module_init(usbip_host_init);
444 module_exit(usbip_host_exit);
445
446 MODULE_AUTHOR(DRIVER_AUTHOR);
447 MODULE_DESCRIPTION(DRIVER_DESC);
448 MODULE_LICENSE("GPL");
449