staging/usbip: convert to kthread
[pandora-kernel.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         kthread_stop(ud->tcp_rx);
224         kthread_stop(ud->tcp_tx);
225
226         /* 2. close the socket */
227         /*
228          * tcp_socket is freed after threads are killed.
229          * So usbip_xmit do not touch NULL socket.
230          */
231         if (ud->tcp_socket) {
232                 sock_release(ud->tcp_socket);
233                 ud->tcp_socket = NULL;
234         }
235
236         /* 3. free used data */
237         stub_device_cleanup_urbs(sdev);
238
239         /* 4. free stub_unlink */
240         {
241                 unsigned long flags;
242                 struct stub_unlink *unlink, *tmp;
243
244                 spin_lock_irqsave(&sdev->priv_lock, flags);
245
246                 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
247                         list_del(&unlink->list);
248                         kfree(unlink);
249                 }
250
251                 list_for_each_entry_safe(unlink, tmp,
252                                                  &sdev->unlink_free, list) {
253                         list_del(&unlink->list);
254                         kfree(unlink);
255                 }
256
257                 spin_unlock_irqrestore(&sdev->priv_lock, flags);
258         }
259 }
260
261 static void stub_device_reset(struct usbip_device *ud)
262 {
263         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
264         struct usb_device *udev = sdev->udev;
265         int ret;
266
267         usbip_udbg("device reset");
268
269         ret = usb_lock_device_for_reset(udev, sdev->interface);
270         if (ret < 0) {
271                 dev_err(&udev->dev, "lock for reset\n");
272
273                 spin_lock(&ud->lock);
274                 ud->status = SDEV_ST_ERROR;
275                 spin_unlock(&ud->lock);
276
277                 return;
278         }
279
280         /* try to reset the device */
281         ret = usb_reset_device(udev);
282
283         usb_unlock_device(udev);
284
285         spin_lock(&ud->lock);
286         if (ret) {
287                 dev_err(&udev->dev, "device reset\n");
288                 ud->status = SDEV_ST_ERROR;
289
290         } else {
291                 dev_info(&udev->dev, "device reset\n");
292                 ud->status = SDEV_ST_AVAILABLE;
293
294         }
295         spin_unlock(&ud->lock);
296
297         return;
298 }
299
300 static void stub_device_unusable(struct usbip_device *ud)
301 {
302         spin_lock(&ud->lock);
303         ud->status = SDEV_ST_ERROR;
304         spin_unlock(&ud->lock);
305 }
306
307
308 /*-------------------------------------------------------------------------*/
309
310 /**
311  * stub_device_alloc - allocate a new stub_device struct
312  * @interface: usb_interface of a new device
313  *
314  * Allocates and initializes a new stub_device struct.
315  */
316 static struct stub_device *stub_device_alloc(struct usb_device *udev,
317                                              struct usb_interface *interface)
318 {
319         struct stub_device *sdev;
320         int busnum = interface_to_busnum(interface);
321         int devnum = interface_to_devnum(interface);
322
323         dev_dbg(&interface->dev, "allocating stub device");
324
325         /* yes, it's a new device */
326         sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
327         if (!sdev) {
328                 dev_err(&interface->dev, "no memory for stub_device\n");
329                 return NULL;
330         }
331
332         sdev->interface = usb_get_intf(interface);
333         sdev->udev = usb_get_dev(udev);
334
335         /*
336          * devid is defined with devnum when this driver is first allocated.
337          * devnum may change later if a device is reset. However, devid never
338          * changes during a usbip connection.
339          */
340         sdev->devid     = (busnum << 16) | devnum;
341
342         sdev->ud.side = USBIP_STUB;
343         sdev->ud.status = SDEV_ST_AVAILABLE;
344         /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
345         spin_lock_init(&sdev->ud.lock);
346         sdev->ud.tcp_socket = NULL;
347
348         INIT_LIST_HEAD(&sdev->priv_init);
349         INIT_LIST_HEAD(&sdev->priv_tx);
350         INIT_LIST_HEAD(&sdev->priv_free);
351         INIT_LIST_HEAD(&sdev->unlink_free);
352         INIT_LIST_HEAD(&sdev->unlink_tx);
353         /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
354         spin_lock_init(&sdev->priv_lock);
355
356         init_waitqueue_head(&sdev->tx_waitq);
357
358         sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
359         sdev->ud.eh_ops.reset    = stub_device_reset;
360         sdev->ud.eh_ops.unusable = stub_device_unusable;
361
362         usbip_start_eh(&sdev->ud);
363
364         usbip_udbg("register new interface\n");
365         return sdev;
366 }
367
368 static int stub_device_free(struct stub_device *sdev)
369 {
370         if (!sdev)
371                 return -EINVAL;
372
373         kfree(sdev);
374         usbip_udbg("kfree udev ok\n");
375
376         return 0;
377 }
378
379
380 /*-------------------------------------------------------------------------*/
381
382 /*
383  * If a usb device has multiple active interfaces, this driver is bound to all
384  * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
385  * active interface). Currently, a userland program must ensure that it
386  * looks at the usbip's sysfs entries of only the first active interface.
387  *
388  * TODO: use "struct usb_device_driver" to bind a usb device.
389  * However, it seems it is not fully supported in mainline kernel yet
390  * (2.6.19.2).
391  */
392 static int stub_probe(struct usb_interface *interface,
393                       const struct usb_device_id *id)
394 {
395         struct usb_device *udev = interface_to_usbdev(interface);
396         struct stub_device *sdev = NULL;
397         const char *udev_busid = dev_name(interface->dev.parent);
398         int err = 0;
399         struct bus_id_priv *busid_priv;
400
401         dev_dbg(&interface->dev, "Enter\n");
402
403         /* check we should claim or not by busid_table */
404         busid_priv = get_busid_priv(udev_busid);
405         if (!busid_priv  || (busid_priv->status == STUB_BUSID_REMOV) ||
406                              (busid_priv->status == STUB_BUSID_OTHER)) {
407                 dev_info(&interface->dev,
408                          "this device %s is not in match_busid table. skip!\n",
409                          udev_busid);
410
411                 /*
412                  * Return value should be ENODEV or ENOXIO to continue trying
413                  * other matched drivers by the driver core.
414                  * See driver_probe_device() in driver/base/dd.c
415                  */
416                 return -ENODEV;
417         }
418
419         if (udev->descriptor.bDeviceClass ==  USB_CLASS_HUB) {
420                 usbip_udbg("this device %s is a usb hub device. skip!\n",
421                                                                 udev_busid);
422                 return -ENODEV;
423         }
424
425         if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
426                 usbip_udbg("this device %s is attached on vhci_hcd. skip!\n",
427                                                                 udev_busid);
428                 return -ENODEV;
429         }
430
431
432         if (busid_priv->status == STUB_BUSID_ALLOC) {
433                 sdev = busid_priv->sdev;
434                 if (!sdev)
435                         return -ENODEV;
436
437                 busid_priv->interf_count++;
438                 dev_info(&interface->dev,
439                  "USB/IP Stub: register a new interface "
440                  "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
441                  interface->cur_altsetting->desc.bInterfaceNumber);
442
443                 /* set private data to usb_interface */
444                 usb_set_intfdata(interface, sdev);
445
446                 err = stub_add_files(&interface->dev);
447                 if (err) {
448                         dev_err(&interface->dev, "create sysfs files for %s\n",
449                                 udev_busid);
450                         usb_set_intfdata(interface, NULL);
451                         busid_priv->interf_count--;
452
453                         return err;
454                 }
455
456                 usb_get_intf(interface);
457                 return 0;
458         }
459
460         /* ok. this is my device. */
461         sdev = stub_device_alloc(udev, interface);
462         if (!sdev)
463                 return -ENOMEM;
464
465         dev_info(&interface->dev, "USB/IP Stub: register a new device "
466                  "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
467                  interface->cur_altsetting->desc.bInterfaceNumber);
468
469         busid_priv->interf_count = 0;
470         busid_priv->shutdown_busid = 0;
471
472         /* set private data to usb_interface */
473         usb_set_intfdata(interface, sdev);
474         busid_priv->interf_count++;
475
476         busid_priv->sdev = sdev;
477
478         err = stub_add_files(&interface->dev);
479         if (err) {
480                 dev_err(&interface->dev, "create sysfs files for %s\n",
481                         udev_busid);
482                 usb_set_intfdata(interface, NULL);
483                 usb_put_intf(interface);
484
485                 busid_priv->interf_count = 0;
486
487                 busid_priv->sdev = NULL;
488                 stub_device_free(sdev);
489                 return err;
490         }
491         busid_priv->status = STUB_BUSID_ALLOC;
492
493         return 0;
494 }
495
496 static void shutdown_busid(struct bus_id_priv *busid_priv)
497 {
498         if (busid_priv->sdev && !busid_priv->shutdown_busid) {
499                 busid_priv->shutdown_busid = 1;
500                 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
501
502                 /* 2. wait for the stop of the event handler */
503                 usbip_stop_eh(&busid_priv->sdev->ud);
504         }
505
506 }
507
508
509 /*
510  * called in usb_disconnect() or usb_deregister()
511  * but only if actconfig(active configuration) exists
512  */
513 static void stub_disconnect(struct usb_interface *interface)
514 {
515         struct stub_device *sdev;
516         const char *udev_busid = dev_name(interface->dev.parent);
517         struct bus_id_priv *busid_priv;
518
519         busid_priv = get_busid_priv(udev_busid);
520
521         usbip_udbg("Enter\n");
522
523         if (!busid_priv) {
524                 BUG();
525                 return;
526         }
527
528         sdev = usb_get_intfdata(interface);
529
530         /* get stub_device */
531         if (!sdev) {
532                 err(" could not get device from inteface data");
533                 /* BUG(); */
534                 return;
535         }
536
537         usb_set_intfdata(interface, NULL);
538
539         /*
540          * NOTE:
541          * rx/tx threads are invoked for each usb_device.
542          */
543         stub_remove_files(&interface->dev);
544
545         /*If usb reset called from event handler*/
546         if (busid_priv->sdev->ud.eh == current) {
547                 busid_priv->interf_count--;
548                 return;
549         }
550
551         if (busid_priv->interf_count > 1) {
552                 busid_priv->interf_count--;
553                 shutdown_busid(busid_priv);
554                 usb_put_intf(interface);
555                 return;
556         }
557
558         busid_priv->interf_count = 0;
559
560
561         /* 1. shutdown the current connection */
562         shutdown_busid(busid_priv);
563
564         usb_put_dev(sdev->udev);
565         usb_put_intf(interface);
566
567         /* 3. free sdev */
568         busid_priv->sdev = NULL;
569         stub_device_free(sdev);
570
571         if (busid_priv->status == STUB_BUSID_ALLOC) {
572                 busid_priv->status = STUB_BUSID_ADDED;
573         } else {
574                 busid_priv->status = STUB_BUSID_OTHER;
575                 del_match_busid((char *)udev_busid);
576         }
577         usbip_udbg("bye\n");
578 }