]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/usbip/stub_dev.c
Merge branch 'kbuild' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[mirror_ubuntu-artful-kernel.git] / drivers / usb / usbip / stub_dev.c
CommitLineData
4d7b5c7f
TH
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
7aaacb43 20#include <linux/device.h>
3d0a2a22 21#include <linux/file.h>
9720b4bc 22#include <linux/kthread.h>
99c97852 23#include <linux/module.h>
5a0e3ad6 24
4d7b5c7f
TH
25#include "usbip_common.h"
26#include "stub.h"
27
4d7b5c7f 28/*
d012c2a5 29 * usbip_status shows the status of usbip-host as long as this driver is bound
30 * to the target device.
4d7b5c7f 31 */
b1f56aca
GKH
32static ssize_t usbip_status_show(struct device *dev,
33 struct device_attribute *attr, char *buf)
4d7b5c7f
TH
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
dcf14779 43 spin_lock_irq(&sdev->ud.lock);
4d7b5c7f 44 status = sdev->ud.status;
dcf14779 45 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f
TH
46
47 return snprintf(buf, PAGE_SIZE, "%d\n", status);
48}
b1f56aca 49static DEVICE_ATTR_RO(usbip_status);
4d7b5c7f
TH
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 */
56static 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;
f8cfc023 62 int rv;
4d7b5c7f
TH
63
64 if (!sdev) {
65 dev_err(dev, "sdev is null\n");
66 return -ENODEV;
67 }
68
f8cfc023
EO
69 rv = sscanf(buf, "%d", &sockfd);
70 if (rv != 1)
71 return -EINVAL;
4d7b5c7f
TH
72
73 if (sockfd != -1) {
964ea96e 74 int err;
3eed8c03 75
4d7b5c7f
TH
76 dev_info(dev, "stub up\n");
77
dcf14779 78 spin_lock_irq(&sdev->ud.lock);
4d7b5c7f
TH
79
80 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
81 dev_err(dev, "not ready\n");
31398f63 82 goto err;
4d7b5c7f
TH
83 }
84
964ea96e 85 socket = sockfd_lookup(sockfd, &err);
31398f63
KK
86 if (!socket)
87 goto err;
88
4d7b5c7f
TH
89 sdev->ud.tcp_socket = socket;
90
dcf14779 91 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f 92
8c4e5834
KK
93 sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
94 "stub_rx");
95 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
96 "stub_tx");
4d7b5c7f 97
dcf14779 98 spin_lock_irq(&sdev->ud.lock);
4d7b5c7f 99 sdev->ud.status = SDEV_ST_USED;
dcf14779 100 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f
TH
101
102 } else {
103 dev_info(dev, "stub down\n");
104
dcf14779 105 spin_lock_irq(&sdev->ud.lock);
31398f63
KK
106 if (sdev->ud.status != SDEV_ST_USED)
107 goto err;
108
dcf14779 109 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f
TH
110
111 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
112 }
113
114 return count;
31398f63
KK
115
116err:
117 spin_unlock_irq(&sdev->ud.lock);
964ea96e 118 return -EINVAL;
4d7b5c7f
TH
119}
120static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
121
122static int stub_add_files(struct device *dev)
123{
124 int err = 0;
125
126 err = device_create_file(dev, &dev_attr_usbip_status);
127 if (err)
128 goto err_status;
129
130 err = device_create_file(dev, &dev_attr_usbip_sockfd);
131 if (err)
132 goto err_sockfd;
133
134 err = device_create_file(dev, &dev_attr_usbip_debug);
135 if (err)
136 goto err_debug;
137
138 return 0;
139
140err_debug:
141 device_remove_file(dev, &dev_attr_usbip_sockfd);
4d7b5c7f
TH
142err_sockfd:
143 device_remove_file(dev, &dev_attr_usbip_status);
4d7b5c7f
TH
144err_status:
145 return err;
146}
147
148static void stub_remove_files(struct device *dev)
149{
150 device_remove_file(dev, &dev_attr_usbip_status);
151 device_remove_file(dev, &dev_attr_usbip_sockfd);
152 device_remove_file(dev, &dev_attr_usbip_debug);
153}
154
4d7b5c7f
TH
155static void stub_shutdown_connection(struct usbip_device *ud)
156{
157 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
158
159 /*
160 * When removing an exported device, kernel panic sometimes occurred
161 * and then EIP was sk_wait_data of stub_rx thread. Is this because
162 * sk_wait_data returned though stub_rx thread was already finished by
163 * step 1?
164 */
165 if (ud->tcp_socket) {
1a4b6f66 166 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
167 ud->tcp_socket);
4d7b5c7f
TH
168 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
169 }
170
171 /* 1. stop threads */
b7caecb8 172 if (ud->tcp_rx) {
ba46ce30 173 kthread_stop_put(ud->tcp_rx);
b7caecb8 174 ud->tcp_rx = NULL;
175 }
176 if (ud->tcp_tx) {
ba46ce30 177 kthread_stop_put(ud->tcp_tx);
b7caecb8 178 ud->tcp_tx = NULL;
179 }
4d7b5c7f 180
4d7b5c7f 181 /*
87352760 182 * 2. close the socket
183 *
184 * tcp_socket is freed after threads are killed so that usbip_xmit does
185 * not touch NULL socket.
4d7b5c7f
TH
186 */
187 if (ud->tcp_socket) {
964ea96e 188 sockfd_put(ud->tcp_socket);
4d7b5c7f
TH
189 ud->tcp_socket = NULL;
190 }
191
192 /* 3. free used data */
193 stub_device_cleanup_urbs(sdev);
194
195 /* 4. free stub_unlink */
196 {
197 unsigned long flags;
198 struct stub_unlink *unlink, *tmp;
199
200 spin_lock_irqsave(&sdev->priv_lock, flags);
4d7b5c7f
TH
201 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
202 list_del(&unlink->list);
203 kfree(unlink);
204 }
87352760 205 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
206 list) {
4d7b5c7f
TH
207 list_del(&unlink->list);
208 kfree(unlink);
209 }
4d7b5c7f
TH
210 spin_unlock_irqrestore(&sdev->priv_lock, flags);
211 }
212}
213
214static void stub_device_reset(struct usbip_device *ud)
215{
216 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
2d8f4595 217 struct usb_device *udev = sdev->udev;
4d7b5c7f
TH
218 int ret;
219
1a4b6f66 220 dev_dbg(&udev->dev, "device reset");
2d8f4595 221
4d7b5c7f
TH
222 ret = usb_lock_device_for_reset(udev, sdev->interface);
223 if (ret < 0) {
224 dev_err(&udev->dev, "lock for reset\n");
dcf14779 225 spin_lock_irq(&ud->lock);
4d7b5c7f 226 ud->status = SDEV_ST_ERROR;
dcf14779 227 spin_unlock_irq(&ud->lock);
4d7b5c7f
TH
228 return;
229 }
230
231 /* try to reset the device */
232 ret = usb_reset_device(udev);
4d7b5c7f
TH
233 usb_unlock_device(udev);
234
dcf14779 235 spin_lock_irq(&ud->lock);
4d7b5c7f
TH
236 if (ret) {
237 dev_err(&udev->dev, "device reset\n");
238 ud->status = SDEV_ST_ERROR;
4d7b5c7f
TH
239 } else {
240 dev_info(&udev->dev, "device reset\n");
241 ud->status = SDEV_ST_AVAILABLE;
4d7b5c7f 242 }
dcf14779 243 spin_unlock_irq(&ud->lock);
4d7b5c7f
TH
244}
245
246static void stub_device_unusable(struct usbip_device *ud)
247{
dcf14779 248 spin_lock_irq(&ud->lock);
4d7b5c7f 249 ud->status = SDEV_ST_ERROR;
dcf14779 250 spin_unlock_irq(&ud->lock);
4d7b5c7f
TH
251}
252
4d7b5c7f
TH
253/**
254 * stub_device_alloc - allocate a new stub_device struct
255 * @interface: usb_interface of a new device
256 *
257 * Allocates and initializes a new stub_device struct.
258 */
b7945b77 259static struct stub_device *stub_device_alloc(struct usb_device *udev)
4d7b5c7f
TH
260{
261 struct stub_device *sdev;
b7945b77
VM
262 int busnum = udev->bus->busnum;
263 int devnum = udev->devnum;
4d7b5c7f 264
b7945b77 265 dev_dbg(&udev->dev, "allocating stub device");
4d7b5c7f
TH
266
267 /* yes, it's a new device */
268 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
78110bb8 269 if (!sdev)
4d7b5c7f 270 return NULL;
4d7b5c7f 271
2d8f4595 272 sdev->udev = usb_get_dev(udev);
4d7b5c7f
TH
273
274 /*
275 * devid is defined with devnum when this driver is first allocated.
276 * devnum may change later if a device is reset. However, devid never
277 * changes during a usbip connection.
278 */
b744a45f 279 sdev->devid = (busnum << 16) | devnum;
280 sdev->ud.side = USBIP_STUB;
281 sdev->ud.status = SDEV_ST_AVAILABLE;
4d7b5c7f 282 spin_lock_init(&sdev->ud.lock);
b744a45f 283 sdev->ud.tcp_socket = NULL;
4d7b5c7f
TH
284
285 INIT_LIST_HEAD(&sdev->priv_init);
286 INIT_LIST_HEAD(&sdev->priv_tx);
287 INIT_LIST_HEAD(&sdev->priv_free);
288 INIT_LIST_HEAD(&sdev->unlink_free);
289 INIT_LIST_HEAD(&sdev->unlink_tx);
4d7b5c7f
TH
290 spin_lock_init(&sdev->priv_lock);
291
292 init_waitqueue_head(&sdev->tx_waitq);
293
294 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
295 sdev->ud.eh_ops.reset = stub_device_reset;
296 sdev->ud.eh_ops.unusable = stub_device_unusable;
297
298 usbip_start_eh(&sdev->ud);
299
b7945b77 300 dev_dbg(&udev->dev, "register new device\n");
1a4b6f66 301
4d7b5c7f
TH
302 return sdev;
303}
304
b94b3a62 305static void stub_device_free(struct stub_device *sdev)
4d7b5c7f 306{
4d7b5c7f 307 kfree(sdev);
4d7b5c7f
TH
308}
309
b7945b77 310static int stub_probe(struct usb_device *udev)
4d7b5c7f 311{
4d7b5c7f 312 struct stub_device *sdev = NULL;
b7945b77 313 const char *udev_busid = dev_name(&udev->dev);
aa5873e9 314 struct bus_id_priv *busid_priv;
6080cd0e 315 int rc;
4d7b5c7f 316
b7945b77 317 dev_dbg(&udev->dev, "Enter\n");
4d7b5c7f
TH
318
319 /* check we should claim or not by busid_table */
aa5873e9 320 busid_priv = get_busid_priv(udev_busid);
87352760 321 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
b744a45f 322 (busid_priv->status == STUB_BUSID_OTHER)) {
b7945b77 323 dev_info(&udev->dev,
6165cc51
EW
324 "%s is not in match_busid table... skip!\n",
325 udev_busid);
4d7b5c7f
TH
326
327 /*
328 * Return value should be ENODEV or ENOXIO to continue trying
329 * other matched drivers by the driver core.
330 * See driver_probe_device() in driver/base/dd.c
331 */
332 return -ENODEV;
333 }
334
1a4b6f66 335 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
336 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
337 udev_busid);
4d7b5c7f
TH
338 return -ENODEV;
339 }
340
341 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
6165cc51
EW
342 dev_dbg(&udev->dev,
343 "%s is attached on vhci_hcd... skip!\n",
344 udev_busid);
345
4d7b5c7f
TH
346 return -ENODEV;
347 }
348
d012c2a5 349 /* ok, this is my device */
b7945b77 350 sdev = stub_device_alloc(udev);
4d7b5c7f
TH
351 if (!sdev)
352 return -ENOMEM;
353
b7945b77
VM
354 dev_info(&udev->dev,
355 "usbip-host: register new device (bus %u dev %u)\n",
356 udev->bus->busnum, udev->devnum);
4d7b5c7f 357
aa5873e9
EK
358 busid_priv->shutdown_busid = 0;
359
b7945b77
VM
360 /* set private data to usb_device */
361 dev_set_drvdata(&udev->dev, sdev);
aa5873e9 362 busid_priv->sdev = sdev;
a46034ca 363 busid_priv->udev = udev;
4d7b5c7f 364
6080cd0e
VM
365 /*
366 * Claim this hub port.
367 * It doesn't matter what value we pass as owner
368 * (struct dev_state) as long as it is unique.
369 */
370 rc = usb_hub_claim_port(udev->parent, udev->portnum,
9b6f0c4b 371 (struct usb_dev_state *) udev);
6080cd0e
VM
372 if (rc) {
373 dev_dbg(&udev->dev, "unable to claim port\n");
3ff67445 374 goto err_port;
6080cd0e
VM
375 }
376
3ff67445
AK
377 rc = stub_add_files(&udev->dev);
378 if (rc) {
b7945b77 379 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
3ff67445 380 goto err_files;
4d7b5c7f 381 }
aa5873e9 382 busid_priv->status = STUB_BUSID_ALLOC;
4d7b5c7f
TH
383
384 return 0;
3ff67445
AK
385err_files:
386 usb_hub_release_port(udev->parent, udev->portnum,
387 (struct usb_dev_state *) udev);
388err_port:
389 dev_set_drvdata(&udev->dev, NULL);
390 usb_put_dev(udev);
391 kthread_stop_put(sdev->ud.eh);
392
393 busid_priv->sdev = NULL;
394 stub_device_free(sdev);
395 return rc;
4d7b5c7f
TH
396}
397
aa5873e9
EK
398static void shutdown_busid(struct bus_id_priv *busid_priv)
399{
400 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
401 busid_priv->shutdown_busid = 1;
402 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
403
77178807 404 /* wait for the stop of the event handler */
aa5873e9
EK
405 usbip_stop_eh(&busid_priv->sdev->ud);
406 }
aa5873e9
EK
407}
408
4d7b5c7f
TH
409/*
410 * called in usb_disconnect() or usb_deregister()
411 * but only if actconfig(active configuration) exists
412 */
b7945b77 413static void stub_disconnect(struct usb_device *udev)
4d7b5c7f 414{
aa5873e9 415 struct stub_device *sdev;
b7945b77 416 const char *udev_busid = dev_name(&udev->dev);
aa5873e9 417 struct bus_id_priv *busid_priv;
6080cd0e 418 int rc;
aa5873e9 419
b7945b77 420 dev_dbg(&udev->dev, "Enter\n");
4d7b5c7f 421
1a4b6f66 422 busid_priv = get_busid_priv(udev_busid);
aa5873e9
EK
423 if (!busid_priv) {
424 BUG();
425 return;
426 }
427
b7945b77 428 sdev = dev_get_drvdata(&udev->dev);
aa5873e9 429
4d7b5c7f
TH
430 /* get stub_device */
431 if (!sdev) {
b7945b77 432 dev_err(&udev->dev, "could not get device");
4d7b5c7f
TH
433 return;
434 }
435
b7945b77 436 dev_set_drvdata(&udev->dev, NULL);
4d7b5c7f 437
4d7b5c7f 438 /*
c7f00899 439 * NOTE: rx/tx threads are invoked for each usb_device.
4d7b5c7f 440 */
b7945b77 441 stub_remove_files(&udev->dev);
4d7b5c7f 442
6080cd0e
VM
443 /* release port */
444 rc = usb_hub_release_port(udev->parent, udev->portnum,
9b6f0c4b 445 (struct usb_dev_state *) udev);
6080cd0e
VM
446 if (rc) {
447 dev_dbg(&udev->dev, "unable to release port\n");
448 return;
449 }
450
c7f00899 451 /* If usb reset is called from event handler */
b7945b77 452 if (busid_priv->sdev->ud.eh == current)
aa5873e9 453 return;
4d7b5c7f 454
c7f00899 455 /* shutdown the current connection */
aa5873e9 456 shutdown_busid(busid_priv);
4d7b5c7f 457
2d8f4595 458 usb_put_dev(sdev->udev);
2d8f4595 459
c7f00899 460 /* free sdev */
aa5873e9 461 busid_priv->sdev = NULL;
4d7b5c7f
TH
462 stub_device_free(sdev);
463
aa5873e9
EK
464 if (busid_priv->status == STUB_BUSID_ALLOC) {
465 busid_priv->status = STUB_BUSID_ADDED;
466 } else {
467 busid_priv->status = STUB_BUSID_OTHER;
468 del_match_busid((char *)udev_busid);
469 }
4d7b5c7f 470}
d012c2a5 471
b7945b77 472#ifdef CONFIG_PM
553a1a50 473
b7945b77
VM
474/* These functions need usb_port_suspend and usb_port_resume,
475 * which reside in drivers/usb/core/usb.h. Skip for now. */
476
477static int stub_suspend(struct usb_device *udev, pm_message_t message)
1aee199c 478{
b7945b77
VM
479 dev_dbg(&udev->dev, "stub_suspend\n");
480
1aee199c
AM
481 return 0;
482}
483
b7945b77 484static int stub_resume(struct usb_device *udev, pm_message_t message)
1aee199c 485{
b7945b77
VM
486 dev_dbg(&udev->dev, "stub_resume\n");
487
1aee199c
AM
488 return 0;
489}
490
b7945b77
VM
491#endif /* CONFIG_PM */
492
493struct usb_device_driver stub_driver = {
d012c2a5 494 .name = "usbip-host",
495 .probe = stub_probe,
496 .disconnect = stub_disconnect,
b7945b77
VM
497#ifdef CONFIG_PM
498 .suspend = stub_suspend,
499 .resume = stub_resume,
500#endif
501 .supports_autosuspend = 0,
97c451ca 502};