]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/usbip/stub_dev.c
Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[karo-tx-linux.git] / drivers / staging / usbip / stub_dev.c
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/slab.h>
21 #include <linux/kthread.h>
22
23 #include "usbip_common.h"
24 #include "stub.h"
25
26
27
28 static int stub_probe(struct usb_interface *interface,
29                                 const struct usb_device_id *id);
30 static void stub_disconnect(struct usb_interface *interface);
31
32
33 /*
34  * Define device IDs here if you want to explicitly limit exportable devices.
35  * In the most cases, wild card matching will be ok because driver binding can
36  * be changed dynamically by a userland program.
37  */
38 static struct usb_device_id stub_table[] = {
39 #if 0
40         /* just an example */
41         { USB_DEVICE(0x05ac, 0x0301) },   /* Mac 1 button mouse */
42         { USB_DEVICE(0x0430, 0x0009) },   /* Plat Home Keyboard */
43         { USB_DEVICE(0x059b, 0x0001) },   /* Iomega USB Zip 100 */
44         { USB_DEVICE(0x04b3, 0x4427) },   /* IBM USB CD-ROM */
45         { USB_DEVICE(0x05a9, 0xa511) },   /* LifeView USB cam */
46         { USB_DEVICE(0x55aa, 0x0201) },   /* Imation card reader */
47         { USB_DEVICE(0x046d, 0x0870) },   /* Qcam Express(QV-30) */
48         { USB_DEVICE(0x04bb, 0x0101) },   /* IO-DATA HD 120GB */
49         { USB_DEVICE(0x04bb, 0x0904) },   /* IO-DATA USB-ET/TX */
50         { USB_DEVICE(0x04bb, 0x0201) },   /* IO-DATA USB-ET/TX */
51         { USB_DEVICE(0x08bb, 0x2702) },   /* ONKYO USB Speaker */
52         { USB_DEVICE(0x046d, 0x08b2) },   /* Logicool Qcam 4000 Pro */
53 #endif
54         /* magic for wild card */
55         { .driver_info = 1 },
56         { 0, }                                     /* Terminating entry */
57 };
58 MODULE_DEVICE_TABLE(usb, stub_table);
59
60 struct usb_driver stub_driver = {
61         .name           = "usbip",
62         .probe          = stub_probe,
63         .disconnect     = stub_disconnect,
64         .id_table       = stub_table,
65 };
66
67
68 /*-------------------------------------------------------------------------*/
69
70 /* Define sysfs entries for a usbip-bound device */
71
72
73 /*
74  * usbip_status shows status of usbip as long as this driver is bound to the
75  * target device.
76  */
77 static ssize_t show_status(struct device *dev, struct device_attribute *attr,
78                            char *buf)
79 {
80         struct stub_device *sdev = dev_get_drvdata(dev);
81         int status;
82
83         if (!sdev) {
84                 dev_err(dev, "sdev is null\n");
85                 return -ENODEV;
86         }
87
88         spin_lock(&sdev->ud.lock);
89         status = sdev->ud.status;
90         spin_unlock(&sdev->ud.lock);
91
92         return snprintf(buf, PAGE_SIZE, "%d\n", status);
93 }
94 static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
95
96 /*
97  * usbip_sockfd gets a socket descriptor of an established TCP connection that
98  * is used to transfer usbip requests by kernel threads. -1 is a magic number
99  * by which usbip connection is finished.
100  */
101 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
102                             const char *buf, size_t count)
103 {
104         struct stub_device *sdev = dev_get_drvdata(dev);
105         int sockfd = 0;
106         struct socket *socket;
107
108         if (!sdev) {
109                 dev_err(dev, "sdev is null\n");
110                 return -ENODEV;
111         }
112
113         sscanf(buf, "%d", &sockfd);
114
115         if (sockfd != -1) {
116                 dev_info(dev, "stub up\n");
117
118                 spin_lock(&sdev->ud.lock);
119
120                 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
121                         dev_err(dev, "not ready\n");
122                         spin_unlock(&sdev->ud.lock);
123                         return -EINVAL;
124                 }
125
126                 socket = sockfd_to_socket(sockfd);
127                 if (!socket) {
128                         spin_unlock(&sdev->ud.lock);
129                         return -EINVAL;
130                 }
131
132 #if 0
133                 setnodelay(socket);
134                 setkeepalive(socket);
135                 setreuse(socket);
136 #endif
137
138                 sdev->ud.tcp_socket = socket;
139
140                 spin_unlock(&sdev->ud.lock);
141
142                 sdev->ud.tcp_rx = kthread_run(stub_rx_loop, &sdev->ud, "stub_rx");
143                 sdev->ud.tcp_tx = kthread_run(stub_tx_loop, &sdev->ud, "stub_tx");
144
145                 spin_lock(&sdev->ud.lock);
146                 sdev->ud.status = SDEV_ST_USED;
147                 spin_unlock(&sdev->ud.lock);
148
149         } else {
150                 dev_info(dev, "stub down\n");
151
152                 spin_lock(&sdev->ud.lock);
153                 if (sdev->ud.status != SDEV_ST_USED) {
154                         spin_unlock(&sdev->ud.lock);
155                         return -EINVAL;
156                 }
157                 spin_unlock(&sdev->ud.lock);
158
159                 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
160         }
161
162         return count;
163 }
164 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
165
166 static int stub_add_files(struct device *dev)
167 {
168         int err = 0;
169
170         err = device_create_file(dev, &dev_attr_usbip_status);
171         if (err)
172                 goto err_status;
173
174         err = device_create_file(dev, &dev_attr_usbip_sockfd);
175         if (err)
176                 goto err_sockfd;
177
178         err = device_create_file(dev, &dev_attr_usbip_debug);
179         if (err)
180                 goto err_debug;
181
182         return 0;
183
184 err_debug:
185         device_remove_file(dev, &dev_attr_usbip_sockfd);
186
187 err_sockfd:
188         device_remove_file(dev, &dev_attr_usbip_status);
189
190 err_status:
191         return err;
192 }
193
194 static void stub_remove_files(struct device *dev)
195 {
196         device_remove_file(dev, &dev_attr_usbip_status);
197         device_remove_file(dev, &dev_attr_usbip_sockfd);
198         device_remove_file(dev, &dev_attr_usbip_debug);
199 }
200
201
202
203 /*-------------------------------------------------------------------------*/
204
205 /* Event handler functions called by an event handler thread */
206
207 static void stub_shutdown_connection(struct usbip_device *ud)
208 {
209         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
210
211         /*
212          * When removing an exported device, kernel panic sometimes occurred
213          * and then EIP was sk_wait_data of stub_rx thread. Is this because
214          * sk_wait_data returned though stub_rx thread was already finished by
215          * step 1?
216          */
217         if (ud->tcp_socket) {
218                 usbip_udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
219                 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
220         }
221
222         /* 1. stop threads */
223         if (ud->tcp_rx && !task_is_dead(ud->tcp_rx))
224                 kthread_stop(ud->tcp_rx);
225         if (ud->tcp_tx && !task_is_dead(ud->tcp_tx))
226                 kthread_stop(ud->tcp_tx);
227
228         /* 2. close the socket */
229         /*
230          * tcp_socket is freed after threads are killed.
231          * So usbip_xmit do not touch NULL socket.
232          */
233         if (ud->tcp_socket) {
234                 sock_release(ud->tcp_socket);
235                 ud->tcp_socket = NULL;
236         }
237
238         /* 3. free used data */
239         stub_device_cleanup_urbs(sdev);
240
241         /* 4. free stub_unlink */
242         {
243                 unsigned long flags;
244                 struct stub_unlink *unlink, *tmp;
245
246                 spin_lock_irqsave(&sdev->priv_lock, flags);
247
248                 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
249                         list_del(&unlink->list);
250                         kfree(unlink);
251                 }
252
253                 list_for_each_entry_safe(unlink, tmp,
254                                                  &sdev->unlink_free, list) {
255                         list_del(&unlink->list);
256                         kfree(unlink);
257                 }
258
259                 spin_unlock_irqrestore(&sdev->priv_lock, flags);
260         }
261 }
262
263 static void stub_device_reset(struct usbip_device *ud)
264 {
265         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
266         struct usb_device *udev = sdev->udev;
267         int ret;
268
269         usbip_udbg("device reset");
270
271         ret = usb_lock_device_for_reset(udev, sdev->interface);
272         if (ret < 0) {
273                 dev_err(&udev->dev, "lock for reset\n");
274
275                 spin_lock(&ud->lock);
276                 ud->status = SDEV_ST_ERROR;
277                 spin_unlock(&ud->lock);
278
279                 return;
280         }
281
282         /* try to reset the device */
283         ret = usb_reset_device(udev);
284
285         usb_unlock_device(udev);
286
287         spin_lock(&ud->lock);
288         if (ret) {
289                 dev_err(&udev->dev, "device reset\n");
290                 ud->status = SDEV_ST_ERROR;
291
292         } else {
293                 dev_info(&udev->dev, "device reset\n");
294                 ud->status = SDEV_ST_AVAILABLE;
295
296         }
297         spin_unlock(&ud->lock);
298
299         return;
300 }
301
302 static void stub_device_unusable(struct usbip_device *ud)
303 {
304         spin_lock(&ud->lock);
305         ud->status = SDEV_ST_ERROR;
306         spin_unlock(&ud->lock);
307 }
308
309
310 /*-------------------------------------------------------------------------*/
311
312 /**
313  * stub_device_alloc - allocate a new stub_device struct
314  * @interface: usb_interface of a new device
315  *
316  * Allocates and initializes a new stub_device struct.
317  */
318 static struct stub_device *stub_device_alloc(struct usb_device *udev,
319                                              struct usb_interface *interface)
320 {
321         struct stub_device *sdev;
322         int busnum = interface_to_busnum(interface);
323         int devnum = interface_to_devnum(interface);
324
325         dev_dbg(&interface->dev, "allocating stub device");
326
327         /* yes, it's a new device */
328         sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
329         if (!sdev) {
330                 dev_err(&interface->dev, "no memory for stub_device\n");
331                 return NULL;
332         }
333
334         sdev->interface = usb_get_intf(interface);
335         sdev->udev = usb_get_dev(udev);
336
337         /*
338          * devid is defined with devnum when this driver is first allocated.
339          * devnum may change later if a device is reset. However, devid never
340          * changes during a usbip connection.
341          */
342         sdev->devid     = (busnum << 16) | devnum;
343
344         sdev->ud.side = USBIP_STUB;
345         sdev->ud.status = SDEV_ST_AVAILABLE;
346         /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
347         spin_lock_init(&sdev->ud.lock);
348         sdev->ud.tcp_socket = NULL;
349
350         INIT_LIST_HEAD(&sdev->priv_init);
351         INIT_LIST_HEAD(&sdev->priv_tx);
352         INIT_LIST_HEAD(&sdev->priv_free);
353         INIT_LIST_HEAD(&sdev->unlink_free);
354         INIT_LIST_HEAD(&sdev->unlink_tx);
355         /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
356         spin_lock_init(&sdev->priv_lock);
357
358         init_waitqueue_head(&sdev->tx_waitq);
359
360         sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
361         sdev->ud.eh_ops.reset    = stub_device_reset;
362         sdev->ud.eh_ops.unusable = stub_device_unusable;
363
364         usbip_start_eh(&sdev->ud);
365
366         usbip_udbg("register new interface\n");
367         return sdev;
368 }
369
370 static int stub_device_free(struct stub_device *sdev)
371 {
372         if (!sdev)
373                 return -EINVAL;
374
375         kfree(sdev);
376         usbip_udbg("kfree udev ok\n");
377
378         return 0;
379 }
380
381
382 /*-------------------------------------------------------------------------*/
383
384 /*
385  * If a usb device has multiple active interfaces, this driver is bound to all
386  * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
387  * active interface). Currently, a userland program must ensure that it
388  * looks at the usbip's sysfs entries of only the first active interface.
389  *
390  * TODO: use "struct usb_device_driver" to bind a usb device.
391  * However, it seems it is not fully supported in mainline kernel yet
392  * (2.6.19.2).
393  */
394 static int stub_probe(struct usb_interface *interface,
395                       const struct usb_device_id *id)
396 {
397         struct usb_device *udev = interface_to_usbdev(interface);
398         struct stub_device *sdev = NULL;
399         const char *udev_busid = dev_name(interface->dev.parent);
400         int err = 0;
401         struct bus_id_priv *busid_priv;
402
403         dev_dbg(&interface->dev, "Enter\n");
404
405         /* check we should claim or not by busid_table */
406         busid_priv = get_busid_priv(udev_busid);
407         if (!busid_priv  || (busid_priv->status == STUB_BUSID_REMOV) ||
408                              (busid_priv->status == STUB_BUSID_OTHER)) {
409                 dev_info(&interface->dev,
410                          "this device %s is not in match_busid table. skip!\n",
411                          udev_busid);
412
413                 /*
414                  * Return value should be ENODEV or ENOXIO to continue trying
415                  * other matched drivers by the driver core.
416                  * See driver_probe_device() in driver/base/dd.c
417                  */
418                 return -ENODEV;
419         }
420
421         if (udev->descriptor.bDeviceClass ==  USB_CLASS_HUB) {
422                 usbip_udbg("this device %s is a usb hub device. skip!\n",
423                                                                 udev_busid);
424                 return -ENODEV;
425         }
426
427         if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
428                 usbip_udbg("this device %s is attached on vhci_hcd. skip!\n",
429                                                                 udev_busid);
430                 return -ENODEV;
431         }
432
433
434         if (busid_priv->status == STUB_BUSID_ALLOC) {
435                 sdev = busid_priv->sdev;
436                 if (!sdev)
437                         return -ENODEV;
438
439                 busid_priv->interf_count++;
440                 dev_info(&interface->dev,
441                  "USB/IP Stub: register a new interface "
442                  "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
443                  interface->cur_altsetting->desc.bInterfaceNumber);
444
445                 /* set private data to usb_interface */
446                 usb_set_intfdata(interface, sdev);
447
448                 err = stub_add_files(&interface->dev);
449                 if (err) {
450                         dev_err(&interface->dev, "create sysfs files for %s\n",
451                                 udev_busid);
452                         usb_set_intfdata(interface, NULL);
453                         busid_priv->interf_count--;
454
455                         return err;
456                 }
457
458                 usb_get_intf(interface);
459                 return 0;
460         }
461
462         /* ok. this is my device. */
463         sdev = stub_device_alloc(udev, interface);
464         if (!sdev)
465                 return -ENOMEM;
466
467         dev_info(&interface->dev, "USB/IP Stub: register a new device "
468                  "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
469                  interface->cur_altsetting->desc.bInterfaceNumber);
470
471         busid_priv->interf_count = 0;
472         busid_priv->shutdown_busid = 0;
473
474         /* set private data to usb_interface */
475         usb_set_intfdata(interface, sdev);
476         busid_priv->interf_count++;
477
478         busid_priv->sdev = sdev;
479
480         err = stub_add_files(&interface->dev);
481         if (err) {
482                 dev_err(&interface->dev, "create sysfs files for %s\n",
483                         udev_busid);
484                 usb_set_intfdata(interface, NULL);
485                 usb_put_intf(interface);
486
487                 busid_priv->interf_count = 0;
488
489                 busid_priv->sdev = NULL;
490                 stub_device_free(sdev);
491                 return err;
492         }
493         busid_priv->status = STUB_BUSID_ALLOC;
494
495         return 0;
496 }
497
498 static void shutdown_busid(struct bus_id_priv *busid_priv)
499 {
500         if (busid_priv->sdev && !busid_priv->shutdown_busid) {
501                 busid_priv->shutdown_busid = 1;
502                 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
503
504                 /* 2. wait for the stop of the event handler */
505                 usbip_stop_eh(&busid_priv->sdev->ud);
506         }
507
508 }
509
510
511 /*
512  * called in usb_disconnect() or usb_deregister()
513  * but only if actconfig(active configuration) exists
514  */
515 static void stub_disconnect(struct usb_interface *interface)
516 {
517         struct stub_device *sdev;
518         const char *udev_busid = dev_name(interface->dev.parent);
519         struct bus_id_priv *busid_priv;
520
521         busid_priv = get_busid_priv(udev_busid);
522
523         usbip_udbg("Enter\n");
524
525         if (!busid_priv) {
526                 BUG();
527                 return;
528         }
529
530         sdev = usb_get_intfdata(interface);
531
532         /* get stub_device */
533         if (!sdev) {
534                 err(" could not get device from inteface data");
535                 /* BUG(); */
536                 return;
537         }
538
539         usb_set_intfdata(interface, NULL);
540
541         /*
542          * NOTE:
543          * rx/tx threads are invoked for each usb_device.
544          */
545         stub_remove_files(&interface->dev);
546
547         /*If usb reset called from event handler*/
548         if (busid_priv->sdev->ud.eh == current) {
549                 busid_priv->interf_count--;
550                 return;
551         }
552
553         if (busid_priv->interf_count > 1) {
554                 busid_priv->interf_count--;
555                 shutdown_busid(busid_priv);
556                 usb_put_intf(interface);
557                 return;
558         }
559
560         busid_priv->interf_count = 0;
561
562
563         /* 1. shutdown the current connection */
564         shutdown_busid(busid_priv);
565
566         usb_put_dev(sdev->udev);
567         usb_put_intf(interface);
568
569         /* 3. free sdev */
570         busid_priv->sdev = NULL;
571         stub_device_free(sdev);
572
573         if (busid_priv->status == STUB_BUSID_ALLOC) {
574                 busid_priv->status = STUB_BUSID_ADDED;
575         } else {
576                 busid_priv->status = STUB_BUSID_OTHER;
577                 del_match_busid((char *)udev_busid);
578         }
579         usbip_udbg("bye\n");
580 }