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