staging: usbip: bugfix prevent driver unbind
[linux-2.6/libata-dev.git] / drivers / staging / usbip / stub_dev.c
blob8cbea42b69bc75a87a61a60652c80e389eefa14b
1 /*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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.
20 #include <linux/device.h>
21 #include <linux/kthread.h>
23 #include "usbip_common.h"
24 #include "stub.h"
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);
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.
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 */
57 MODULE_DEVICE_TABLE(usb, stub_table);
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,
69 * usbip_status shows status of usbip as long as this driver is bound to the
70 * target device.
72 static ssize_t show_status(struct device *dev, struct device_attribute *attr,
73 char *buf)
75 struct stub_device *sdev = dev_get_drvdata(dev);
76 int status;
78 if (!sdev) {
79 dev_err(dev, "sdev is null\n");
80 return -ENODEV;
83 spin_lock(&sdev->ud.lock);
84 status = sdev->ud.status;
85 spin_unlock(&sdev->ud.lock);
87 return snprintf(buf, PAGE_SIZE, "%d\n", status);
89 static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
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.
96 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
97 const char *buf, size_t count)
99 struct stub_device *sdev = dev_get_drvdata(dev);
100 int sockfd = 0;
101 struct socket *socket;
103 if (!sdev) {
104 dev_err(dev, "sdev is null\n");
105 return -ENODEV;
108 sscanf(buf, "%d", &sockfd);
110 if (sockfd != -1) {
111 dev_info(dev, "stub up\n");
113 spin_lock(&sdev->ud.lock);
115 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
116 dev_err(dev, "not ready\n");
117 spin_unlock(&sdev->ud.lock);
118 return -EINVAL;
121 socket = sockfd_to_socket(sockfd);
122 if (!socket) {
123 spin_unlock(&sdev->ud.lock);
124 return -EINVAL;
126 #if 0
127 setnodelay(socket);
128 setkeepalive(socket);
129 setreuse(socket);
130 #endif
131 sdev->ud.tcp_socket = socket;
133 spin_unlock(&sdev->ud.lock);
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");
138 spin_lock(&sdev->ud.lock);
139 sdev->ud.status = SDEV_ST_USED;
140 spin_unlock(&sdev->ud.lock);
142 } else {
143 dev_info(dev, "stub down\n");
145 spin_lock(&sdev->ud.lock);
146 if (sdev->ud.status != SDEV_ST_USED) {
147 spin_unlock(&sdev->ud.lock);
148 return -EINVAL;
150 spin_unlock(&sdev->ud.lock);
152 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
155 return count;
157 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
159 static int stub_add_files(struct device *dev)
161 int err = 0;
163 err = device_create_file(dev, &dev_attr_usbip_status);
164 if (err)
165 goto err_status;
167 err = device_create_file(dev, &dev_attr_usbip_sockfd);
168 if (err)
169 goto err_sockfd;
171 err = device_create_file(dev, &dev_attr_usbip_debug);
172 if (err)
173 goto err_debug;
175 return 0;
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;
185 static void stub_remove_files(struct device *dev)
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);
192 static void stub_shutdown_connection(struct usbip_device *ud)
194 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
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?
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);
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);
214 /* 2. close the socket */
216 * tcp_socket is freed after threads are killed.
217 * So usbip_xmit do not touch NULL socket.
219 if (ud->tcp_socket) {
220 sock_release(ud->tcp_socket);
221 ud->tcp_socket = NULL;
224 /* 3. free used data */
225 stub_device_cleanup_urbs(sdev);
227 /* 4. free stub_unlink */
229 unsigned long flags;
230 struct stub_unlink *unlink, *tmp;
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);
237 list_for_each_entry_safe(unlink, tmp,
238 &sdev->unlink_free, list) {
239 list_del(&unlink->list);
240 kfree(unlink);
242 spin_unlock_irqrestore(&sdev->priv_lock, flags);
246 static void stub_device_reset(struct usbip_device *ud)
248 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
249 struct usb_device *udev = sdev->udev;
250 int ret;
252 dev_dbg(&udev->dev, "device reset");
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;
263 /* try to reset the device */
264 ret = usb_reset_device(udev);
266 usb_unlock_device(udev);
268 spin_lock(&ud->lock);
269 if (ret) {
270 dev_err(&udev->dev, "device reset\n");
271 ud->status = SDEV_ST_ERROR;
273 } else {
274 dev_info(&udev->dev, "device reset\n");
275 ud->status = SDEV_ST_AVAILABLE;
278 spin_unlock(&ud->lock);
280 return;
283 static void stub_device_unusable(struct usbip_device *ud)
285 spin_lock(&ud->lock);
286 ud->status = SDEV_ST_ERROR;
287 spin_unlock(&ud->lock);
291 * stub_device_alloc - allocate a new stub_device struct
292 * @interface: usb_interface of a new device
294 * Allocates and initializes a new stub_device struct.
296 static struct stub_device *stub_device_alloc(struct usb_device *udev,
297 struct usb_interface *interface)
299 struct stub_device *sdev;
300 int busnum = interface_to_busnum(interface);
301 int devnum = interface_to_devnum(interface);
303 dev_dbg(&interface->dev, "allocating stub device");
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;
312 sdev->interface = usb_get_intf(interface);
313 sdev->udev = usb_get_dev(udev);
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.
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;
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);
335 init_waitqueue_head(&sdev->tx_waitq);
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;
341 usbip_start_eh(&sdev->ud);
343 dev_dbg(&interface->dev, "register new interface\n");
345 return sdev;
348 static int stub_device_free(struct stub_device *sdev)
350 if (!sdev)
351 return -EINVAL;
353 kfree(sdev);
354 pr_debug("kfree udev ok\n");
356 return 0;
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.
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).
369 static int stub_probe(struct usb_interface *interface,
370 const struct usb_device_id *id)
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;
378 dev_dbg(&interface->dev, "Enter\n");
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);
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
392 return -ENODEV;
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;
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;
407 if (busid_priv->status == STUB_BUSID_ALLOC) {
408 sdev = busid_priv->sdev;
409 if (!sdev)
410 return -ENODEV;
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);
418 /* set private data to usb_interface */
419 usb_set_intfdata(interface, sdev);
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--;
428 return err;
431 usb_get_intf(interface);
432 return 0;
435 /* ok. this is my device. */
436 sdev = stub_device_alloc(udev, interface);
437 if (!sdev)
438 return -ENOMEM;
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);
444 busid_priv->interf_count = 0;
445 busid_priv->shutdown_busid = 0;
447 /* set private data to usb_interface */
448 usb_set_intfdata(interface, sdev);
449 busid_priv->interf_count++;
451 busid_priv->sdev = sdev;
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);
459 busid_priv->interf_count = 0;
461 busid_priv->sdev = NULL;
462 stub_device_free(sdev);
463 return err;
465 busid_priv->status = STUB_BUSID_ALLOC;
467 return 0;
470 static void shutdown_busid(struct bus_id_priv *busid_priv)
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);
476 /* 2. wait for the stop of the event handler */
477 usbip_stop_eh(&busid_priv->sdev->ud);
482 * called in usb_disconnect() or usb_deregister()
483 * but only if actconfig(active configuration) exists
485 static void stub_disconnect(struct usb_interface *interface)
487 struct stub_device *sdev;
488 const char *udev_busid = dev_name(interface->dev.parent);
489 struct bus_id_priv *busid_priv;
491 dev_dbg(&interface->dev, "Enter\n");
493 busid_priv = get_busid_priv(udev_busid);
494 if (!busid_priv) {
495 BUG();
496 return;
499 sdev = usb_get_intfdata(interface);
501 /* get stub_device */
502 if (!sdev) {
503 dev_err(&interface->dev, "could not get device");
504 /* BUG(); */
505 return;
508 usb_set_intfdata(interface, NULL);
511 * NOTE:
512 * rx/tx threads are invoked for each usb_device.
514 stub_remove_files(&interface->dev);
516 /*If usb reset called from event handler*/
517 if (busid_priv->sdev->ud.eh == current) {
518 busid_priv->interf_count--;
519 return;
522 if (busid_priv->interf_count > 1) {
523 busid_priv->interf_count--;
524 shutdown_busid(busid_priv);
525 usb_put_intf(interface);
526 return;
529 busid_priv->interf_count = 0;
531 /* 1. shutdown the current connection */
532 shutdown_busid(busid_priv);
534 usb_put_dev(sdev->udev);
535 usb_put_intf(interface);
537 /* 3. free sdev */
538 busid_priv->sdev = NULL;
539 stub_device_free(sdev);
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);
550 * Presence of pre_reset and post_reset prevents the driver from being unbound
551 * when the device is being reset
554 int stub_pre_reset(struct usb_interface *interface)
556 dev_dbg(&interface->dev, "pre_reset\n");
557 return 0;
560 int stub_post_reset(struct usb_interface *interface)
562 dev_dbg(&interface->dev, "post_reset\n");
563 return 0;