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/file.h>
22 #include <linux/kthread.h>
23 #include <linux/module.h>
24
25 #include "usbip_common.h"
26 #include "stub.h"
27
28 /*
29 * usbip_status shows the status of usbip-host as long as this driver is bound
30 * to the target device.
31 */
usbip_status_show(struct device * dev,struct device_attribute * attr,char * buf)32 static ssize_t usbip_status_show(struct device *dev,
33 struct device_attribute *attr, char *buf)
34 {
35 struct stub_device *sdev = dev_get_drvdata(dev);
36 int status;
37
38 if (!sdev) {
39 dev_err(dev, "sdev is null\n");
40 return -ENODEV;
41 }
42
43 spin_lock_irq(&sdev->ud.lock);
44 status = sdev->ud.status;
45 spin_unlock_irq(&sdev->ud.lock);
46
47 return snprintf(buf, PAGE_SIZE, "%d\n", status);
48 }
49 static DEVICE_ATTR_RO(usbip_status);
50
51 /*
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.
55 */
store_sockfd(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)56 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
57 const char *buf, size_t count)
58 {
59 struct stub_device *sdev = dev_get_drvdata(dev);
60 int sockfd = 0;
61 struct socket *socket;
62 int rv;
63 struct task_struct *tcp_rx = NULL;
64 struct task_struct *tcp_tx = NULL;
65
66 if (!sdev) {
67 dev_err(dev, "sdev is null\n");
68 return -ENODEV;
69 }
70
71 rv = sscanf(buf, "%d", &sockfd);
72 if (rv != 1)
73 return -EINVAL;
74
75 if (sockfd != -1) {
76 int err;
77
78 dev_info(dev, "stub up\n");
79
80 spin_lock_irq(&sdev->ud.lock);
81
82 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
83 dev_err(dev, "not ready\n");
84 goto err;
85 }
86
87 socket = sockfd_lookup(sockfd, &err);
88 if (!socket) {
89 dev_err(dev, "failed to lookup sock");
90 goto err;
91 }
92
93 if (socket->type != SOCK_STREAM) {
94 dev_err(dev, "Expecting SOCK_STREAM - found %d",
95 socket->type);
96 goto sock_err;
97 }
98
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)) {
103 sockfd_put(socket);
104 return -EINVAL;
105 }
106 tcp_tx = kthread_create(stub_tx_loop, &sdev->ud, "stub_tx");
107 if (IS_ERR(tcp_tx)) {
108 kthread_stop(tcp_rx);
109 sockfd_put(socket);
110 return -EINVAL;
111 }
112
113 /* get task structs now */
114 get_task_struct(tcp_rx);
115 get_task_struct(tcp_tx);
116
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);
125
126 wake_up_process(sdev->ud.tcp_rx);
127 wake_up_process(sdev->ud.tcp_tx);
128
129 } else {
130 dev_info(dev, "stub down\n");
131
132 spin_lock_irq(&sdev->ud.lock);
133 if (sdev->ud.status != SDEV_ST_USED)
134 goto err;
135
136 spin_unlock_irq(&sdev->ud.lock);
137
138 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
139 }
140
141 return count;
142
143 sock_err:
144 sockfd_put(socket);
145 err:
146 spin_unlock_irq(&sdev->ud.lock);
147 return -EINVAL;
148 }
149 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
150
stub_add_files(struct device * dev)151 static int stub_add_files(struct device *dev)
152 {
153 int err = 0;
154
155 err = device_create_file(dev, &dev_attr_usbip_status);
156 if (err)
157 goto err_status;
158
159 err = device_create_file(dev, &dev_attr_usbip_sockfd);
160 if (err)
161 goto err_sockfd;
162
163 err = device_create_file(dev, &dev_attr_usbip_debug);
164 if (err)
165 goto err_debug;
166
167 return 0;
168
169 err_debug:
170 device_remove_file(dev, &dev_attr_usbip_sockfd);
171 err_sockfd:
172 device_remove_file(dev, &dev_attr_usbip_status);
173 err_status:
174 return err;
175 }
176
stub_remove_files(struct device * dev)177 static void stub_remove_files(struct device *dev)
178 {
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);
182 }
183
stub_shutdown_connection(struct usbip_device * ud)184 static void stub_shutdown_connection(struct usbip_device *ud)
185 {
186 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
187
188 /*
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
192 * step 1?
193 */
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);
197 }
198
199 /* 1. stop threads */
200 if (ud->tcp_rx) {
201 kthread_stop_put(ud->tcp_rx);
202 ud->tcp_rx = NULL;
203 }
204 if (ud->tcp_tx) {
205 kthread_stop_put(ud->tcp_tx);
206 ud->tcp_tx = NULL;
207 }
208
209 /*
210 * 2. close the socket
211 *
212 * tcp_socket is freed after threads are killed so that usbip_xmit does
213 * not touch NULL socket.
214 */
215 if (ud->tcp_socket) {
216 sockfd_put(ud->tcp_socket);
217 ud->tcp_socket = NULL;
218 ud->sockfd = -1;
219 }
220
221 /* 3. free used data */
222 stub_device_cleanup_urbs(sdev);
223
224 /* 4. free stub_unlink */
225 {
226 unsigned long flags;
227 struct stub_unlink *unlink, *tmp;
228
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);
232 kfree(unlink);
233 }
234 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
235 list) {
236 list_del(&unlink->list);
237 kfree(unlink);
238 }
239 spin_unlock_irqrestore(&sdev->priv_lock, flags);
240 }
241 }
242
stub_device_reset(struct usbip_device * ud)243 static void stub_device_reset(struct usbip_device *ud)
244 {
245 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
246 struct usb_device *udev = sdev->udev;
247 int ret;
248
249 dev_dbg(&udev->dev, "device reset");
250
251 ret = usb_lock_device_for_reset(udev, sdev->interface);
252 if (ret < 0) {
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);
257 return;
258 }
259
260 /* try to reset the device */
261 ret = usb_reset_device(udev);
262 usb_unlock_device(udev);
263
264 spin_lock_irq(&ud->lock);
265 if (ret) {
266 dev_err(&udev->dev, "device reset\n");
267 ud->status = SDEV_ST_ERROR;
268 } else {
269 dev_info(&udev->dev, "device reset\n");
270 ud->status = SDEV_ST_AVAILABLE;
271 }
272 spin_unlock_irq(&ud->lock);
273 }
274
stub_device_unusable(struct usbip_device * ud)275 static void stub_device_unusable(struct usbip_device *ud)
276 {
277 spin_lock_irq(&ud->lock);
278 ud->status = SDEV_ST_ERROR;
279 spin_unlock_irq(&ud->lock);
280 }
281
282 /**
283 * stub_device_alloc - allocate a new stub_device struct
284 * @interface: usb_interface of a new device
285 *
286 * Allocates and initializes a new stub_device struct.
287 */
stub_device_alloc(struct usb_device * udev)288 static struct stub_device *stub_device_alloc(struct usb_device *udev)
289 {
290 struct stub_device *sdev;
291 int busnum = udev->bus->busnum;
292 int devnum = udev->devnum;
293
294 dev_dbg(&udev->dev, "allocating stub device");
295
296 /* yes, it's a new device */
297 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
298 if (!sdev)
299 return NULL;
300
301 sdev->udev = usb_get_dev(udev);
302
303 /*
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.
307 */
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;
314
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);
321
322 init_waitqueue_head(&sdev->tx_waitq);
323
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;
327
328 usbip_start_eh(&sdev->ud);
329
330 dev_dbg(&udev->dev, "register new device\n");
331
332 return sdev;
333 }
334
stub_device_free(struct stub_device * sdev)335 static void stub_device_free(struct stub_device *sdev)
336 {
337 kfree(sdev);
338 }
339
stub_probe(struct usb_device * udev)340 static int stub_probe(struct usb_device *udev)
341 {
342 struct stub_device *sdev = NULL;
343 const char *udev_busid = dev_name(&udev->dev);
344 struct bus_id_priv *busid_priv;
345 int rc = 0;
346
347 dev_dbg(&udev->dev, "Enter probe\n");
348
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)) {
353 dev_info(&udev->dev,
354 "%s is not in match_busid table... skip!\n",
355 udev_busid);
356
357 /*
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
361 */
362 rc = -ENODEV;
363 goto call_put_busid_priv;
364 }
365
366 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
367 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
368 udev_busid);
369 rc = -ENODEV;
370 goto call_put_busid_priv;
371 }
372
373 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
374 dev_dbg(&udev->dev,
375 "%s is attached on vhci_hcd... skip!\n",
376 udev_busid);
377
378 rc = -ENODEV;
379 goto call_put_busid_priv;
380 }
381
382 /* ok, this is my device */
383 sdev = stub_device_alloc(udev);
384 if (!sdev) {
385 rc = -ENOMEM;
386 goto call_put_busid_priv;
387 }
388
389 dev_info(&udev->dev,
390 "usbip-host: register new device (bus %u dev %u)\n",
391 udev->bus->busnum, udev->devnum);
392
393 busid_priv->shutdown_busid = 0;
394
395 /* set private data to usb_device */
396 dev_set_drvdata(&udev->dev, sdev);
397 busid_priv->sdev = sdev;
398 busid_priv->udev = udev;
399
400 /*
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.
404 */
405 rc = usb_hub_claim_port(udev->parent, udev->portnum,
406 (struct usb_dev_state *) udev);
407 if (rc) {
408 dev_dbg(&udev->dev, "unable to claim port\n");
409 goto err_port;
410 }
411
412 rc = stub_add_files(&udev->dev);
413 if (rc) {
414 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
415 goto err_files;
416 }
417 busid_priv->status = STUB_BUSID_ALLOC;
418
419 rc = 0;
420 goto call_put_busid_priv;
421
422 err_files:
423 usb_hub_release_port(udev->parent, udev->portnum,
424 (struct usb_dev_state *) udev);
425 err_port:
426 dev_set_drvdata(&udev->dev, NULL);
427 usb_put_dev(udev);
428 kthread_stop_put(sdev->ud.eh);
429
430 busid_priv->sdev = NULL;
431 stub_device_free(sdev);
432
433 call_put_busid_priv:
434 put_busid_priv(busid_priv);
435 return rc;
436 }
437
shutdown_busid(struct bus_id_priv * busid_priv)438 static void shutdown_busid(struct bus_id_priv *busid_priv)
439 {
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);
443
444 /* wait for the stop of the event handler */
445 usbip_stop_eh(&busid_priv->sdev->ud);
446 }
447 }
448
449 /*
450 * called in usb_disconnect() or usb_deregister()
451 * but only if actconfig(active configuration) exists
452 */
stub_disconnect(struct usb_device * udev)453 static void stub_disconnect(struct usb_device *udev)
454 {
455 struct stub_device *sdev;
456 const char *udev_busid = dev_name(&udev->dev);
457 struct bus_id_priv *busid_priv;
458 int rc;
459
460 dev_dbg(&udev->dev, "Enter disconnect\n");
461
462 busid_priv = get_busid_priv(udev_busid);
463 if (!busid_priv) {
464 BUG();
465 return;
466 }
467
468 sdev = dev_get_drvdata(&udev->dev);
469
470 /* get stub_device */
471 if (!sdev) {
472 dev_err(&udev->dev, "could not get device");
473 goto call_put_busid_priv;
474 }
475
476 dev_set_drvdata(&udev->dev, NULL);
477
478 /*
479 * NOTE: rx/tx threads are invoked for each usb_device.
480 */
481 stub_remove_files(&udev->dev);
482
483 /* release port */
484 rc = usb_hub_release_port(udev->parent, udev->portnum,
485 (struct usb_dev_state *) udev);
486 if (rc) {
487 dev_dbg(&udev->dev, "unable to release port\n");
488 goto call_put_busid_priv;
489 }
490
491 /* If usb reset is called from event handler */
492 if (busid_priv->sdev->ud.eh == current)
493 goto call_put_busid_priv;
494
495 /* shutdown the current connection */
496 shutdown_busid(busid_priv);
497
498 usb_put_dev(sdev->udev);
499
500 /* free sdev */
501 busid_priv->sdev = NULL;
502 stub_device_free(sdev);
503
504 if (busid_priv->status == STUB_BUSID_ALLOC)
505 busid_priv->status = STUB_BUSID_ADDED;
506
507 call_put_busid_priv:
508 put_busid_priv(busid_priv);
509 }
510
511 #ifdef CONFIG_PM
512
513 /* These functions need usb_port_suspend and usb_port_resume,
514 * which reside in drivers/usb/core/usb.h. Skip for now. */
515
stub_suspend(struct usb_device * udev,pm_message_t message)516 static int stub_suspend(struct usb_device *udev, pm_message_t message)
517 {
518 dev_dbg(&udev->dev, "stub_suspend\n");
519
520 return 0;
521 }
522
stub_resume(struct usb_device * udev,pm_message_t message)523 static int stub_resume(struct usb_device *udev, pm_message_t message)
524 {
525 dev_dbg(&udev->dev, "stub_resume\n");
526
527 return 0;
528 }
529
530 #endif /* CONFIG_PM */
531
532 struct usb_device_driver stub_driver = {
533 .name = "usbip-host",
534 .probe = stub_probe,
535 .disconnect = stub_disconnect,
536 #ifdef CONFIG_PM
537 .suspend = stub_suspend,
538 .resume = stub_resume,
539 #endif
540 .supports_autosuspend = 0,
541 };
542