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,
20 #include <linux/device.h>
21 #include <linux/file.h>
22 #include <linux/kthread.h>
23 #include <linux/module.h>
25 #include "usbip_common.h"
29 * usbip_status shows the status of usbip-host as long as this driver is bound
30 * to the target device.
32 static ssize_t usbip_status_show(struct device *dev,
33 struct device_attribute *attr, char *buf)
35 struct stub_device *sdev = dev_get_drvdata(dev);
39 dev_err(dev, "sdev is null\n");
43 spin_lock_irq(&sdev->ud.lock);
44 status = sdev->ud.status;
45 spin_unlock_irq(&sdev->ud.lock);
47 return snprintf(buf, PAGE_SIZE, "%d\n", status);
49 static DEVICE_ATTR_RO(usbip_status);
52 * usbip_sockfd gets a socket descriptor of an established TCP connection that
53 * is used to transfer usbip requests by kernel threads. -1 is a magic number
54 * by which usbip connection is finished.
56 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
57 const char *buf, size_t count)
59 struct stub_device *sdev = dev_get_drvdata(dev);
61 struct socket *socket;
63 struct task_struct *tcp_rx = NULL;
64 struct task_struct *tcp_tx = NULL;
67 dev_err(dev, "sdev is null\n");
71 rv = sscanf(buf, "%d", &sockfd);
78 dev_info(dev, "stub up\n");
80 spin_lock_irq(&sdev->ud.lock);
82 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
83 dev_err(dev, "not ready\n");
87 socket = sockfd_lookup(sockfd, &err);
89 dev_err(dev, "failed to lookup sock");
93 if (socket->type != SOCK_STREAM) {
94 dev_err(dev, "Expecting SOCK_STREAM - found %d",
99 /* unlock and create threads and get tasks */
100 spin_unlock_irq(&sdev->ud.lock);
101 tcp_rx = kthread_create(stub_rx_loop, &sdev->ud, "stub_rx");
102 if (IS_ERR(tcp_rx)) {
106 tcp_tx = kthread_create(stub_tx_loop, &sdev->ud, "stub_tx");
107 if (IS_ERR(tcp_tx)) {
108 kthread_stop(tcp_rx);
113 /* get task structs now */
114 get_task_struct(tcp_rx);
115 get_task_struct(tcp_tx);
117 /* lock and update sdev->ud state */
118 spin_lock_irq(&sdev->ud.lock);
119 sdev->ud.tcp_socket = socket;
120 sdev->ud.sockfd = sockfd;
121 sdev->ud.tcp_rx = tcp_rx;
122 sdev->ud.tcp_tx = tcp_tx;
123 sdev->ud.status = SDEV_ST_USED;
124 spin_unlock_irq(&sdev->ud.lock);
126 wake_up_process(sdev->ud.tcp_rx);
127 wake_up_process(sdev->ud.tcp_tx);
130 dev_info(dev, "stub down\n");
132 spin_lock_irq(&sdev->ud.lock);
133 if (sdev->ud.status != SDEV_ST_USED)
136 spin_unlock_irq(&sdev->ud.lock);
138 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
146 spin_unlock_irq(&sdev->ud.lock);
149 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
151 static int stub_add_files(struct device *dev)
155 err = device_create_file(dev, &dev_attr_usbip_status);
159 err = device_create_file(dev, &dev_attr_usbip_sockfd);
163 err = device_create_file(dev, &dev_attr_usbip_debug);
170 device_remove_file(dev, &dev_attr_usbip_sockfd);
172 device_remove_file(dev, &dev_attr_usbip_status);
177 static void stub_remove_files(struct device *dev)
179 device_remove_file(dev, &dev_attr_usbip_status);
180 device_remove_file(dev, &dev_attr_usbip_sockfd);
181 device_remove_file(dev, &dev_attr_usbip_debug);
184 static void stub_shutdown_connection(struct usbip_device *ud)
186 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
189 * When removing an exported device, kernel panic sometimes occurred
190 * and then EIP was sk_wait_data of stub_rx thread. Is this because
191 * sk_wait_data returned though stub_rx thread was already finished by
194 if (ud->tcp_socket) {
195 dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
196 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
199 /* 1. stop threads */
201 kthread_stop_put(ud->tcp_rx);
205 kthread_stop_put(ud->tcp_tx);
210 * 2. close the socket
212 * tcp_socket is freed after threads are killed so that usbip_xmit does
213 * not touch NULL socket.
215 if (ud->tcp_socket) {
216 sockfd_put(ud->tcp_socket);
217 ud->tcp_socket = NULL;
221 /* 3. free used data */
222 stub_device_cleanup_urbs(sdev);
224 /* 4. free stub_unlink */
227 struct stub_unlink *unlink, *tmp;
229 spin_lock_irqsave(&sdev->priv_lock, flags);
230 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
231 list_del(&unlink->list);
234 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
236 list_del(&unlink->list);
239 spin_unlock_irqrestore(&sdev->priv_lock, flags);
243 static void stub_device_reset(struct usbip_device *ud)
245 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
246 struct usb_device *udev = sdev->udev;
249 dev_dbg(&udev->dev, "device reset");
251 ret = usb_lock_device_for_reset(udev, sdev->interface);
253 dev_err(&udev->dev, "lock for reset\n");
254 spin_lock_irq(&ud->lock);
255 ud->status = SDEV_ST_ERROR;
256 spin_unlock_irq(&ud->lock);
260 /* try to reset the device */
261 ret = usb_reset_device(udev);
262 usb_unlock_device(udev);
264 spin_lock_irq(&ud->lock);
266 dev_err(&udev->dev, "device reset\n");
267 ud->status = SDEV_ST_ERROR;
269 dev_info(&udev->dev, "device reset\n");
270 ud->status = SDEV_ST_AVAILABLE;
272 spin_unlock_irq(&ud->lock);
275 static void stub_device_unusable(struct usbip_device *ud)
277 spin_lock_irq(&ud->lock);
278 ud->status = SDEV_ST_ERROR;
279 spin_unlock_irq(&ud->lock);
283 * stub_device_alloc - allocate a new stub_device struct
284 * @interface: usb_interface of a new device
286 * Allocates and initializes a new stub_device struct.
288 static struct stub_device *stub_device_alloc(struct usb_device *udev)
290 struct stub_device *sdev;
291 int busnum = udev->bus->busnum;
292 int devnum = udev->devnum;
294 dev_dbg(&udev->dev, "allocating stub device");
296 /* yes, it's a new device */
297 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
301 sdev->udev = usb_get_dev(udev);
304 * devid is defined with devnum when this driver is first allocated.
305 * devnum may change later if a device is reset. However, devid never
306 * changes during a usbip connection.
308 sdev->devid = (busnum << 16) | devnum;
309 sdev->ud.side = USBIP_STUB;
310 sdev->ud.status = SDEV_ST_AVAILABLE;
311 spin_lock_init(&sdev->ud.lock);
312 sdev->ud.tcp_socket = NULL;
313 sdev->ud.sockfd = -1;
315 INIT_LIST_HEAD(&sdev->priv_init);
316 INIT_LIST_HEAD(&sdev->priv_tx);
317 INIT_LIST_HEAD(&sdev->priv_free);
318 INIT_LIST_HEAD(&sdev->unlink_free);
319 INIT_LIST_HEAD(&sdev->unlink_tx);
320 spin_lock_init(&sdev->priv_lock);
322 init_waitqueue_head(&sdev->tx_waitq);
324 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
325 sdev->ud.eh_ops.reset = stub_device_reset;
326 sdev->ud.eh_ops.unusable = stub_device_unusable;
328 usbip_start_eh(&sdev->ud);
330 dev_dbg(&udev->dev, "register new device\n");
335 static void stub_device_free(struct stub_device *sdev)
340 static int stub_probe(struct usb_device *udev)
342 struct stub_device *sdev = NULL;
343 const char *udev_busid = dev_name(&udev->dev);
344 struct bus_id_priv *busid_priv;
347 dev_dbg(&udev->dev, "Enter probe\n");
349 /* check we should claim or not by busid_table */
350 busid_priv = get_busid_priv(udev_busid);
351 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
352 (busid_priv->status == STUB_BUSID_OTHER)) {
354 "%s is not in match_busid table... skip!\n",
358 * Return value should be ENODEV or ENOXIO to continue trying
359 * other matched drivers by the driver core.
360 * See driver_probe_device() in driver/base/dd.c
363 goto call_put_busid_priv;
366 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
367 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
370 goto call_put_busid_priv;
373 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
375 "%s is attached on vhci_hcd... skip!\n",
379 goto call_put_busid_priv;
382 /* ok, this is my device */
383 sdev = stub_device_alloc(udev);
386 goto call_put_busid_priv;
390 "usbip-host: register new device (bus %u dev %u)\n",
391 udev->bus->busnum, udev->devnum);
393 busid_priv->shutdown_busid = 0;
395 /* set private data to usb_device */
396 dev_set_drvdata(&udev->dev, sdev);
397 busid_priv->sdev = sdev;
398 busid_priv->udev = udev;
401 * Claim this hub port.
402 * It doesn't matter what value we pass as owner
403 * (struct dev_state) as long as it is unique.
405 rc = usb_hub_claim_port(udev->parent, udev->portnum,
406 (struct usb_dev_state *) udev);
408 dev_dbg(&udev->dev, "unable to claim port\n");
412 rc = stub_add_files(&udev->dev);
414 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
417 busid_priv->status = STUB_BUSID_ALLOC;
420 goto call_put_busid_priv;
423 usb_hub_release_port(udev->parent, udev->portnum,
424 (struct usb_dev_state *) udev);
426 dev_set_drvdata(&udev->dev, NULL);
428 kthread_stop_put(sdev->ud.eh);
430 busid_priv->sdev = NULL;
431 stub_device_free(sdev);
434 put_busid_priv(busid_priv);
438 static void shutdown_busid(struct bus_id_priv *busid_priv)
440 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
441 busid_priv->shutdown_busid = 1;
442 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
444 /* wait for the stop of the event handler */
445 usbip_stop_eh(&busid_priv->sdev->ud);
450 * called in usb_disconnect() or usb_deregister()
451 * but only if actconfig(active configuration) exists
453 static void stub_disconnect(struct usb_device *udev)
455 struct stub_device *sdev;
456 const char *udev_busid = dev_name(&udev->dev);
457 struct bus_id_priv *busid_priv;
460 dev_dbg(&udev->dev, "Enter disconnect\n");
462 busid_priv = get_busid_priv(udev_busid);
468 sdev = dev_get_drvdata(&udev->dev);
470 /* get stub_device */
472 dev_err(&udev->dev, "could not get device");
473 goto call_put_busid_priv;
476 dev_set_drvdata(&udev->dev, NULL);
479 * NOTE: rx/tx threads are invoked for each usb_device.
481 stub_remove_files(&udev->dev);
484 rc = usb_hub_release_port(udev->parent, udev->portnum,
485 (struct usb_dev_state *) udev);
487 dev_dbg(&udev->dev, "unable to release port\n");
488 goto call_put_busid_priv;
491 /* If usb reset is called from event handler */
492 if (busid_priv->sdev->ud.eh == current)
493 goto call_put_busid_priv;
495 /* shutdown the current connection */
496 shutdown_busid(busid_priv);
498 usb_put_dev(sdev->udev);
501 busid_priv->sdev = NULL;
502 stub_device_free(sdev);
504 if (busid_priv->status == STUB_BUSID_ALLOC)
505 busid_priv->status = STUB_BUSID_ADDED;
508 put_busid_priv(busid_priv);
513 /* These functions need usb_port_suspend and usb_port_resume,
514 * which reside in drivers/usb/core/usb.h. Skip for now. */
516 static int stub_suspend(struct usb_device *udev, pm_message_t message)
518 dev_dbg(&udev->dev, "stub_suspend\n");
523 static int stub_resume(struct usb_device *udev, pm_message_t message)
525 dev_dbg(&udev->dev, "stub_resume\n");
530 #endif /* CONFIG_PM */
532 struct usb_device_driver stub_driver = {
533 .name = "usbip-host",
535 .disconnect = stub_disconnect,
537 .suspend = stub_suspend,
538 .resume = stub_resume,
540 .supports_autosuspend = 0,