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
;
65 dev_err(dev
, "sdev is null\n");
69 rv
= sscanf(buf
, "%d", &sockfd
);
76 dev_info(dev
, "stub up\n");
78 spin_lock_irq(&sdev
->ud
.lock
);
80 if (sdev
->ud
.status
!= SDEV_ST_AVAILABLE
) {
81 dev_err(dev
, "not ready\n");
85 socket
= sockfd_lookup(sockfd
, &err
);
89 sdev
->ud
.tcp_socket
= socket
;
90 sdev
->ud
.sockfd
= sockfd
;
92 spin_unlock_irq(&sdev
->ud
.lock
);
94 sdev
->ud
.tcp_rx
= kthread_get_run(stub_rx_loop
, &sdev
->ud
,
96 sdev
->ud
.tcp_tx
= kthread_get_run(stub_tx_loop
, &sdev
->ud
,
99 spin_lock_irq(&sdev
->ud
.lock
);
100 sdev
->ud
.status
= SDEV_ST_USED
;
101 spin_unlock_irq(&sdev
->ud
.lock
);
104 dev_info(dev
, "stub down\n");
106 spin_lock_irq(&sdev
->ud
.lock
);
107 if (sdev
->ud
.status
!= SDEV_ST_USED
)
110 spin_unlock_irq(&sdev
->ud
.lock
);
112 usbip_event_add(&sdev
->ud
, SDEV_EVENT_DOWN
);
118 spin_unlock_irq(&sdev
->ud
.lock
);
121 static DEVICE_ATTR(usbip_sockfd
, S_IWUSR
, NULL
, store_sockfd
);
123 static int stub_add_files(struct device
*dev
)
127 err
= device_create_file(dev
, &dev_attr_usbip_status
);
131 err
= device_create_file(dev
, &dev_attr_usbip_sockfd
);
135 err
= device_create_file(dev
, &dev_attr_usbip_debug
);
142 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
144 device_remove_file(dev
, &dev_attr_usbip_status
);
149 static void stub_remove_files(struct device
*dev
)
151 device_remove_file(dev
, &dev_attr_usbip_status
);
152 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
153 device_remove_file(dev
, &dev_attr_usbip_debug
);
156 static void stub_shutdown_connection(struct usbip_device
*ud
)
158 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
161 * When removing an exported device, kernel panic sometimes occurred
162 * and then EIP was sk_wait_data of stub_rx thread. Is this because
163 * sk_wait_data returned though stub_rx thread was already finished by
166 if (ud
->tcp_socket
) {
167 dev_dbg(&sdev
->udev
->dev
, "shutdown sockfd\n");
168 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
171 /* 1. stop threads */
173 kthread_stop_put(ud
->tcp_rx
);
177 kthread_stop_put(ud
->tcp_tx
);
182 * 2. close the socket
184 * tcp_socket is freed after threads are killed so that usbip_xmit does
185 * not touch NULL socket.
187 if (ud
->tcp_socket
) {
188 sockfd_put(ud
->tcp_socket
);
189 ud
->tcp_socket
= NULL
;
193 /* 3. free used data */
194 stub_device_cleanup_urbs(sdev
);
196 /* 4. free stub_unlink */
199 struct stub_unlink
*unlink
, *tmp
;
201 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
202 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
203 list_del(&unlink
->list
);
206 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
,
208 list_del(&unlink
->list
);
211 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
215 static void stub_device_reset(struct usbip_device
*ud
)
217 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
218 struct usb_device
*udev
= sdev
->udev
;
221 dev_dbg(&udev
->dev
, "device reset");
223 ret
= usb_lock_device_for_reset(udev
, NULL
);
225 dev_err(&udev
->dev
, "lock for reset\n");
226 spin_lock_irq(&ud
->lock
);
227 ud
->status
= SDEV_ST_ERROR
;
228 spin_unlock_irq(&ud
->lock
);
232 /* try to reset the device */
233 ret
= usb_reset_device(udev
);
234 usb_unlock_device(udev
);
236 spin_lock_irq(&ud
->lock
);
238 dev_err(&udev
->dev
, "device reset\n");
239 ud
->status
= SDEV_ST_ERROR
;
241 dev_info(&udev
->dev
, "device reset\n");
242 ud
->status
= SDEV_ST_AVAILABLE
;
244 spin_unlock_irq(&ud
->lock
);
247 static void stub_device_unusable(struct usbip_device
*ud
)
249 spin_lock_irq(&ud
->lock
);
250 ud
->status
= SDEV_ST_ERROR
;
251 spin_unlock_irq(&ud
->lock
);
255 * stub_device_alloc - allocate a new stub_device struct
256 * @udev: usb_device of a new device
258 * Allocates and initializes a new stub_device struct.
260 static struct stub_device
*stub_device_alloc(struct usb_device
*udev
)
262 struct stub_device
*sdev
;
263 int busnum
= udev
->bus
->busnum
;
264 int devnum
= udev
->devnum
;
266 dev_dbg(&udev
->dev
, "allocating stub device");
268 /* yes, it's a new device */
269 sdev
= kzalloc(sizeof(struct stub_device
), GFP_KERNEL
);
273 sdev
->udev
= usb_get_dev(udev
);
276 * devid is defined with devnum when this driver is first allocated.
277 * devnum may change later if a device is reset. However, devid never
278 * changes during a usbip connection.
280 sdev
->devid
= (busnum
<< 16) | devnum
;
281 sdev
->ud
.side
= USBIP_STUB
;
282 sdev
->ud
.status
= SDEV_ST_AVAILABLE
;
283 spin_lock_init(&sdev
->ud
.lock
);
284 sdev
->ud
.tcp_socket
= NULL
;
285 sdev
->ud
.sockfd
= -1;
287 INIT_LIST_HEAD(&sdev
->priv_init
);
288 INIT_LIST_HEAD(&sdev
->priv_tx
);
289 INIT_LIST_HEAD(&sdev
->priv_free
);
290 INIT_LIST_HEAD(&sdev
->unlink_free
);
291 INIT_LIST_HEAD(&sdev
->unlink_tx
);
292 spin_lock_init(&sdev
->priv_lock
);
294 init_waitqueue_head(&sdev
->tx_waitq
);
296 sdev
->ud
.eh_ops
.shutdown
= stub_shutdown_connection
;
297 sdev
->ud
.eh_ops
.reset
= stub_device_reset
;
298 sdev
->ud
.eh_ops
.unusable
= stub_device_unusable
;
300 usbip_start_eh(&sdev
->ud
);
302 dev_dbg(&udev
->dev
, "register new device\n");
307 static void stub_device_free(struct stub_device
*sdev
)
312 static int stub_probe(struct usb_device
*udev
)
314 struct stub_device
*sdev
= NULL
;
315 const char *udev_busid
= dev_name(&udev
->dev
);
316 struct bus_id_priv
*busid_priv
;
319 dev_dbg(&udev
->dev
, "Enter probe\n");
321 /* check we should claim or not by busid_table */
322 busid_priv
= get_busid_priv(udev_busid
);
323 if (!busid_priv
|| (busid_priv
->status
== STUB_BUSID_REMOV
) ||
324 (busid_priv
->status
== STUB_BUSID_OTHER
)) {
326 "%s is not in match_busid table... skip!\n",
330 * Return value should be ENODEV or ENOXIO to continue trying
331 * other matched drivers by the driver core.
332 * See driver_probe_device() in driver/base/dd.c
335 goto call_put_busid_priv
;
338 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
) {
339 dev_dbg(&udev
->dev
, "%s is a usb hub device... skip!\n",
342 goto call_put_busid_priv
;
345 if (!strcmp(udev
->bus
->bus_name
, "vhci_hcd")) {
347 "%s is attached on vhci_hcd... skip!\n",
351 goto call_put_busid_priv
;
354 /* ok, this is my device */
355 sdev
= stub_device_alloc(udev
);
358 goto call_put_busid_priv
;
362 "usbip-host: register new device (bus %u dev %u)\n",
363 udev
->bus
->busnum
, udev
->devnum
);
365 busid_priv
->shutdown_busid
= 0;
367 /* set private data to usb_device */
368 dev_set_drvdata(&udev
->dev
, sdev
);
369 busid_priv
->sdev
= sdev
;
370 busid_priv
->udev
= udev
;
373 * Claim this hub port.
374 * It doesn't matter what value we pass as owner
375 * (struct dev_state) as long as it is unique.
377 rc
= usb_hub_claim_port(udev
->parent
, udev
->portnum
,
378 (struct usb_dev_state
*) udev
);
380 dev_dbg(&udev
->dev
, "unable to claim port\n");
384 rc
= stub_add_files(&udev
->dev
);
386 dev_err(&udev
->dev
, "stub_add_files for %s\n", udev_busid
);
389 busid_priv
->status
= STUB_BUSID_ALLOC
;
392 goto call_put_busid_priv
;
395 usb_hub_release_port(udev
->parent
, udev
->portnum
,
396 (struct usb_dev_state
*) udev
);
398 dev_set_drvdata(&udev
->dev
, NULL
);
401 busid_priv
->sdev
= NULL
;
402 stub_device_free(sdev
);
405 put_busid_priv(busid_priv
);
409 static void shutdown_busid(struct bus_id_priv
*busid_priv
)
411 if (busid_priv
->sdev
&& !busid_priv
->shutdown_busid
) {
412 busid_priv
->shutdown_busid
= 1;
413 usbip_event_add(&busid_priv
->sdev
->ud
, SDEV_EVENT_REMOVED
);
415 /* wait for the stop of the event handler */
416 usbip_stop_eh(&busid_priv
->sdev
->ud
);
421 * called in usb_disconnect() or usb_deregister()
422 * but only if actconfig(active configuration) exists
424 static void stub_disconnect(struct usb_device
*udev
)
426 struct stub_device
*sdev
;
427 const char *udev_busid
= dev_name(&udev
->dev
);
428 struct bus_id_priv
*busid_priv
;
431 dev_dbg(&udev
->dev
, "Enter disconnect\n");
433 busid_priv
= get_busid_priv(udev_busid
);
439 sdev
= dev_get_drvdata(&udev
->dev
);
441 /* get stub_device */
443 dev_err(&udev
->dev
, "could not get device");
444 goto call_put_busid_priv
;
447 dev_set_drvdata(&udev
->dev
, NULL
);
450 * NOTE: rx/tx threads are invoked for each usb_device.
452 stub_remove_files(&udev
->dev
);
455 rc
= usb_hub_release_port(udev
->parent
, udev
->portnum
,
456 (struct usb_dev_state
*) udev
);
458 dev_dbg(&udev
->dev
, "unable to release port\n");
459 goto call_put_busid_priv
;
462 /* If usb reset is called from event handler */
463 if (usbip_in_eh(current
))
464 goto call_put_busid_priv
;
466 /* shutdown the current connection */
467 shutdown_busid(busid_priv
);
469 usb_put_dev(sdev
->udev
);
472 busid_priv
->sdev
= NULL
;
473 stub_device_free(sdev
);
475 if (busid_priv
->status
== STUB_BUSID_ALLOC
)
476 busid_priv
->status
= STUB_BUSID_ADDED
;
479 put_busid_priv(busid_priv
);
484 /* These functions need usb_port_suspend and usb_port_resume,
485 * which reside in drivers/usb/core/usb.h. Skip for now. */
487 static int stub_suspend(struct usb_device
*udev
, pm_message_t message
)
489 dev_dbg(&udev
->dev
, "stub_suspend\n");
494 static int stub_resume(struct usb_device
*udev
, pm_message_t message
)
496 dev_dbg(&udev
->dev
, "stub_resume\n");
501 #endif /* CONFIG_PM */
503 struct usb_device_driver stub_driver
= {
504 .name
= "usbip-host",
506 .disconnect
= stub_disconnect
,
508 .suspend
= stub_suspend
,
509 .resume
= stub_resume
,
511 .supports_autosuspend
= 0,