]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - drivers/usb/usbip/stub_dev.c
Merge remote-tracking branch 'asoc/topic/pcm512x' into asoc-next
[mirror_ubuntu-focal-kernel.git] / drivers / usb / usbip / stub_dev.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4 */
5
6 #include <linux/device.h>
7 #include <linux/file.h>
8 #include <linux/kthread.h>
9 #include <linux/module.h>
10
11 #include "usbip_common.h"
12 #include "stub.h"
13
14 /*
15 * usbip_status shows the status of usbip-host as long as this driver is bound
16 * to the target device.
17 */
18 static ssize_t usbip_status_show(struct device *dev,
19 struct device_attribute *attr, char *buf)
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
29 spin_lock_irq(&sdev->ud.lock);
30 status = sdev->ud.status;
31 spin_unlock_irq(&sdev->ud.lock);
32
33 return snprintf(buf, PAGE_SIZE, "%d\n", status);
34 }
35 static DEVICE_ATTR_RO(usbip_status);
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;
48 int rv;
49
50 if (!sdev) {
51 dev_err(dev, "sdev is null\n");
52 return -ENODEV;
53 }
54
55 rv = sscanf(buf, "%d", &sockfd);
56 if (rv != 1)
57 return -EINVAL;
58
59 if (sockfd != -1) {
60 int err;
61
62 dev_info(dev, "stub up\n");
63
64 spin_lock_irq(&sdev->ud.lock);
65
66 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
67 dev_err(dev, "not ready\n");
68 goto err;
69 }
70
71 socket = sockfd_lookup(sockfd, &err);
72 if (!socket)
73 goto err;
74
75 sdev->ud.tcp_socket = socket;
76
77 spin_unlock_irq(&sdev->ud.lock);
78
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");
83
84 spin_lock_irq(&sdev->ud.lock);
85 sdev->ud.status = SDEV_ST_USED;
86 spin_unlock_irq(&sdev->ud.lock);
87
88 } else {
89 dev_info(dev, "stub down\n");
90
91 spin_lock_irq(&sdev->ud.lock);
92 if (sdev->ud.status != SDEV_ST_USED)
93 goto err;
94
95 spin_unlock_irq(&sdev->ud.lock);
96
97 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
98 }
99
100 return count;
101
102 err:
103 spin_unlock_irq(&sdev->ud.lock);
104 return -EINVAL;
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);
128 err_sockfd:
129 device_remove_file(dev, &dev_attr_usbip_status);
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
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) {
152 dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
153 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
154 }
155
156 /* 1. stop threads */
157 if (ud->tcp_rx) {
158 kthread_stop_put(ud->tcp_rx);
159 ud->tcp_rx = NULL;
160 }
161 if (ud->tcp_tx) {
162 kthread_stop_put(ud->tcp_tx);
163 ud->tcp_tx = NULL;
164 }
165
166 /*
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.
171 */
172 if (ud->tcp_socket) {
173 sockfd_put(ud->tcp_socket);
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);
186 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
187 list_del(&unlink->list);
188 kfree(unlink);
189 }
190 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
191 list) {
192 list_del(&unlink->list);
193 kfree(unlink);
194 }
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);
202 struct usb_device *udev = sdev->udev;
203 int ret;
204
205 dev_dbg(&udev->dev, "device reset");
206
207 ret = usb_lock_device_for_reset(udev, NULL);
208 if (ret < 0) {
209 dev_err(&udev->dev, "lock for reset\n");
210 spin_lock_irq(&ud->lock);
211 ud->status = SDEV_ST_ERROR;
212 spin_unlock_irq(&ud->lock);
213 return;
214 }
215
216 /* try to reset the device */
217 ret = usb_reset_device(udev);
218 usb_unlock_device(udev);
219
220 spin_lock_irq(&ud->lock);
221 if (ret) {
222 dev_err(&udev->dev, "device reset\n");
223 ud->status = SDEV_ST_ERROR;
224 } else {
225 dev_info(&udev->dev, "device reset\n");
226 ud->status = SDEV_ST_AVAILABLE;
227 }
228 spin_unlock_irq(&ud->lock);
229 }
230
231 static void stub_device_unusable(struct usbip_device *ud)
232 {
233 spin_lock_irq(&ud->lock);
234 ud->status = SDEV_ST_ERROR;
235 spin_unlock_irq(&ud->lock);
236 }
237
238 /**
239 * stub_device_alloc - allocate a new stub_device struct
240 * @udev: usb_device of a new device
241 *
242 * Allocates and initializes a new stub_device struct.
243 */
244 static struct stub_device *stub_device_alloc(struct usb_device *udev)
245 {
246 struct stub_device *sdev;
247 int busnum = udev->bus->busnum;
248 int devnum = udev->devnum;
249
250 dev_dbg(&udev->dev, "allocating stub device");
251
252 /* yes, it's a new device */
253 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
254 if (!sdev)
255 return NULL;
256
257 sdev->udev = usb_get_dev(udev);
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 */
264 sdev->devid = (busnum << 16) | devnum;
265 sdev->ud.side = USBIP_STUB;
266 sdev->ud.status = SDEV_ST_AVAILABLE;
267 spin_lock_init(&sdev->ud.lock);
268 sdev->ud.tcp_socket = NULL;
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);
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
285 dev_dbg(&udev->dev, "register new device\n");
286
287 return sdev;
288 }
289
290 static void stub_device_free(struct stub_device *sdev)
291 {
292 kfree(sdev);
293 }
294
295 static int stub_probe(struct usb_device *udev)
296 {
297 struct stub_device *sdev = NULL;
298 const char *udev_busid = dev_name(&udev->dev);
299 struct bus_id_priv *busid_priv;
300 int rc;
301
302 dev_dbg(&udev->dev, "Enter\n");
303
304 /* check we should claim or not by busid_table */
305 busid_priv = get_busid_priv(udev_busid);
306 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
307 (busid_priv->status == STUB_BUSID_OTHER)) {
308 dev_info(&udev->dev,
309 "%s is not in match_busid table... skip!\n",
310 udev_busid);
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
320 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
321 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
322 udev_busid);
323 return -ENODEV;
324 }
325
326 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
327 dev_dbg(&udev->dev,
328 "%s is attached on vhci_hcd... skip!\n",
329 udev_busid);
330
331 return -ENODEV;
332 }
333
334 /* ok, this is my device */
335 sdev = stub_device_alloc(udev);
336 if (!sdev)
337 return -ENOMEM;
338
339 dev_info(&udev->dev,
340 "usbip-host: register new device (bus %u dev %u)\n",
341 udev->bus->busnum, udev->devnum);
342
343 busid_priv->shutdown_busid = 0;
344
345 /* set private data to usb_device */
346 dev_set_drvdata(&udev->dev, sdev);
347 busid_priv->sdev = sdev;
348 busid_priv->udev = udev;
349
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,
356 (struct usb_dev_state *) udev);
357 if (rc) {
358 dev_dbg(&udev->dev, "unable to claim port\n");
359 goto err_port;
360 }
361
362 rc = stub_add_files(&udev->dev);
363 if (rc) {
364 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
365 goto err_files;
366 }
367 busid_priv->status = STUB_BUSID_ALLOC;
368
369 return 0;
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);
376
377 busid_priv->sdev = NULL;
378 stub_device_free(sdev);
379 return rc;
380 }
381
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
388 /* wait for the stop of the event handler */
389 usbip_stop_eh(&busid_priv->sdev->ud);
390 }
391 }
392
393 /*
394 * called in usb_disconnect() or usb_deregister()
395 * but only if actconfig(active configuration) exists
396 */
397 static void stub_disconnect(struct usb_device *udev)
398 {
399 struct stub_device *sdev;
400 const char *udev_busid = dev_name(&udev->dev);
401 struct bus_id_priv *busid_priv;
402 int rc;
403
404 dev_dbg(&udev->dev, "Enter\n");
405
406 busid_priv = get_busid_priv(udev_busid);
407 if (!busid_priv) {
408 BUG();
409 return;
410 }
411
412 sdev = dev_get_drvdata(&udev->dev);
413
414 /* get stub_device */
415 if (!sdev) {
416 dev_err(&udev->dev, "could not get device");
417 return;
418 }
419
420 dev_set_drvdata(&udev->dev, NULL);
421
422 /*
423 * NOTE: rx/tx threads are invoked for each usb_device.
424 */
425 stub_remove_files(&udev->dev);
426
427 /* release port */
428 rc = usb_hub_release_port(udev->parent, udev->portnum,
429 (struct usb_dev_state *) udev);
430 if (rc) {
431 dev_dbg(&udev->dev, "unable to release port\n");
432 return;
433 }
434
435 /* If usb reset is called from event handler */
436 if (usbip_in_eh(current))
437 return;
438
439 /* shutdown the current connection */
440 shutdown_busid(busid_priv);
441
442 usb_put_dev(sdev->udev);
443
444 /* free sdev */
445 busid_priv->sdev = NULL;
446 stub_device_free(sdev);
447
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 }
454 }
455
456 #ifdef CONFIG_PM
457
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)
462 {
463 dev_dbg(&udev->dev, "stub_suspend\n");
464
465 return 0;
466 }
467
468 static int stub_resume(struct usb_device *udev, pm_message_t message)
469 {
470 dev_dbg(&udev->dev, "stub_resume\n");
471
472 return 0;
473 }
474
475 #endif /* CONFIG_PM */
476
477 struct usb_device_driver stub_driver = {
478 .name = "usbip-host",
479 .probe = stub_probe,
480 .disconnect = stub_disconnect,
481 #ifdef CONFIG_PM
482 .suspend = stub_suspend,
483 .resume = stub_resume,
484 #endif
485 .supports_autosuspend = 0,
486 };