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