xref: /linux/drivers/usb/usbip/stub_dev.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2003-2008 Takahiro Hirofuchi
4  *
5  * This is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18  * USA.
19  */
20 
21 #include <linux/device.h>
22 #include <linux/file.h>
23 #include <linux/kthread.h>
24 #include <linux/module.h>
25 
26 #include "usbip_common.h"
27 #include "stub.h"
28 
29 /*
30  * usbip_status shows the status of usbip-host as long as this driver is bound
31  * to the target device.
32  */
33 static ssize_t usbip_status_show(struct device *dev,
34 				 struct device_attribute *attr, char *buf)
35 {
36 	struct stub_device *sdev = dev_get_drvdata(dev);
37 	int status;
38 
39 	if (!sdev) {
40 		dev_err(dev, "sdev is null\n");
41 		return -ENODEV;
42 	}
43 
44 	spin_lock_irq(&sdev->ud.lock);
45 	status = sdev->ud.status;
46 	spin_unlock_irq(&sdev->ud.lock);
47 
48 	return snprintf(buf, PAGE_SIZE, "%d\n", status);
49 }
50 static DEVICE_ATTR_RO(usbip_status);
51 
52 /*
53  * usbip_sockfd gets a socket descriptor of an established TCP connection that
54  * is used to transfer usbip requests by kernel threads. -1 is a magic number
55  * by which usbip connection is finished.
56  */
57 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
58 			    const char *buf, size_t count)
59 {
60 	struct stub_device *sdev = dev_get_drvdata(dev);
61 	int sockfd = 0;
62 	struct socket *socket;
63 	int rv;
64 
65 	if (!sdev) {
66 		dev_err(dev, "sdev is null\n");
67 		return -ENODEV;
68 	}
69 
70 	rv = sscanf(buf, "%d", &sockfd);
71 	if (rv != 1)
72 		return -EINVAL;
73 
74 	if (sockfd != -1) {
75 		int err;
76 
77 		dev_info(dev, "stub up\n");
78 
79 		spin_lock_irq(&sdev->ud.lock);
80 
81 		if (sdev->ud.status != SDEV_ST_AVAILABLE) {
82 			dev_err(dev, "not ready\n");
83 			goto err;
84 		}
85 
86 		socket = sockfd_lookup(sockfd, &err);
87 		if (!socket)
88 			goto err;
89 
90 		sdev->ud.tcp_socket = socket;
91 
92 		spin_unlock_irq(&sdev->ud.lock);
93 
94 		sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
95 						  "stub_rx");
96 		sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
97 						  "stub_tx");
98 
99 		spin_lock_irq(&sdev->ud.lock);
100 		sdev->ud.status = SDEV_ST_USED;
101 		spin_unlock_irq(&sdev->ud.lock);
102 
103 	} else {
104 		dev_info(dev, "stub down\n");
105 
106 		spin_lock_irq(&sdev->ud.lock);
107 		if (sdev->ud.status != SDEV_ST_USED)
108 			goto err;
109 
110 		spin_unlock_irq(&sdev->ud.lock);
111 
112 		usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
113 	}
114 
115 	return count;
116 
117 err:
118 	spin_unlock_irq(&sdev->ud.lock);
119 	return -EINVAL;
120 }
121 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
122 
123 static int stub_add_files(struct device *dev)
124 {
125 	int err = 0;
126 
127 	err = device_create_file(dev, &dev_attr_usbip_status);
128 	if (err)
129 		goto err_status;
130 
131 	err = device_create_file(dev, &dev_attr_usbip_sockfd);
132 	if (err)
133 		goto err_sockfd;
134 
135 	err = device_create_file(dev, &dev_attr_usbip_debug);
136 	if (err)
137 		goto err_debug;
138 
139 	return 0;
140 
141 err_debug:
142 	device_remove_file(dev, &dev_attr_usbip_sockfd);
143 err_sockfd:
144 	device_remove_file(dev, &dev_attr_usbip_status);
145 err_status:
146 	return err;
147 }
148 
149 static void stub_remove_files(struct device *dev)
150 {
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);
154 }
155 
156 static void stub_shutdown_connection(struct usbip_device *ud)
157 {
158 	struct stub_device *sdev = container_of(ud, struct stub_device, ud);
159 
160 	/*
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
164 	 * step 1?
165 	 */
166 	if (ud->tcp_socket) {
167 		dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
168 			ud->tcp_socket);
169 		kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
170 	}
171 
172 	/* 1. stop threads */
173 	if (ud->tcp_rx) {
174 		kthread_stop_put(ud->tcp_rx);
175 		ud->tcp_rx = NULL;
176 	}
177 	if (ud->tcp_tx) {
178 		kthread_stop_put(ud->tcp_tx);
179 		ud->tcp_tx = NULL;
180 	}
181 
182 	/*
183 	 * 2. close the socket
184 	 *
185 	 * tcp_socket is freed after threads are killed so that usbip_xmit does
186 	 * not touch NULL socket.
187 	 */
188 	if (ud->tcp_socket) {
189 		sockfd_put(ud->tcp_socket);
190 		ud->tcp_socket = NULL;
191 	}
192 
193 	/* 3. free used data */
194 	stub_device_cleanup_urbs(sdev);
195 
196 	/* 4. free stub_unlink */
197 	{
198 		unsigned long flags;
199 		struct stub_unlink *unlink, *tmp;
200 
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);
204 			kfree(unlink);
205 		}
206 		list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
207 					 list) {
208 			list_del(&unlink->list);
209 			kfree(unlink);
210 		}
211 		spin_unlock_irqrestore(&sdev->priv_lock, flags);
212 	}
213 }
214 
215 static void stub_device_reset(struct usbip_device *ud)
216 {
217 	struct stub_device *sdev = container_of(ud, struct stub_device, ud);
218 	struct usb_device *udev = sdev->udev;
219 	int ret;
220 
221 	dev_dbg(&udev->dev, "device reset");
222 
223 	ret = usb_lock_device_for_reset(udev, NULL);
224 	if (ret < 0) {
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);
229 		return;
230 	}
231 
232 	/* try to reset the device */
233 	ret = usb_reset_device(udev);
234 	usb_unlock_device(udev);
235 
236 	spin_lock_irq(&ud->lock);
237 	if (ret) {
238 		dev_err(&udev->dev, "device reset\n");
239 		ud->status = SDEV_ST_ERROR;
240 	} else {
241 		dev_info(&udev->dev, "device reset\n");
242 		ud->status = SDEV_ST_AVAILABLE;
243 	}
244 	spin_unlock_irq(&ud->lock);
245 }
246 
247 static void stub_device_unusable(struct usbip_device *ud)
248 {
249 	spin_lock_irq(&ud->lock);
250 	ud->status = SDEV_ST_ERROR;
251 	spin_unlock_irq(&ud->lock);
252 }
253 
254 /**
255  * stub_device_alloc - allocate a new stub_device struct
256  * @udev: usb_device of a new device
257  *
258  * Allocates and initializes a new stub_device struct.
259  */
260 static struct stub_device *stub_device_alloc(struct usb_device *udev)
261 {
262 	struct stub_device *sdev;
263 	int busnum = udev->bus->busnum;
264 	int devnum = udev->devnum;
265 
266 	dev_dbg(&udev->dev, "allocating stub device");
267 
268 	/* yes, it's a new device */
269 	sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
270 	if (!sdev)
271 		return NULL;
272 
273 	sdev->udev = usb_get_dev(udev);
274 
275 	/*
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.
279 	 */
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 
286 	INIT_LIST_HEAD(&sdev->priv_init);
287 	INIT_LIST_HEAD(&sdev->priv_tx);
288 	INIT_LIST_HEAD(&sdev->priv_free);
289 	INIT_LIST_HEAD(&sdev->unlink_free);
290 	INIT_LIST_HEAD(&sdev->unlink_tx);
291 	spin_lock_init(&sdev->priv_lock);
292 
293 	init_waitqueue_head(&sdev->tx_waitq);
294 
295 	sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
296 	sdev->ud.eh_ops.reset    = stub_device_reset;
297 	sdev->ud.eh_ops.unusable = stub_device_unusable;
298 
299 	usbip_start_eh(&sdev->ud);
300 
301 	dev_dbg(&udev->dev, "register new device\n");
302 
303 	return sdev;
304 }
305 
306 static void stub_device_free(struct stub_device *sdev)
307 {
308 	kfree(sdev);
309 }
310 
311 static int stub_probe(struct usb_device *udev)
312 {
313 	struct stub_device *sdev = NULL;
314 	const char *udev_busid = dev_name(&udev->dev);
315 	struct bus_id_priv *busid_priv;
316 	int rc;
317 
318 	dev_dbg(&udev->dev, "Enter\n");
319 
320 	/* check we should claim or not by busid_table */
321 	busid_priv = get_busid_priv(udev_busid);
322 	if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
323 	    (busid_priv->status == STUB_BUSID_OTHER)) {
324 		dev_info(&udev->dev,
325 			"%s is not in match_busid table... skip!\n",
326 			udev_busid);
327 
328 		/*
329 		 * Return value should be ENODEV or ENOXIO to continue trying
330 		 * other matched drivers by the driver core.
331 		 * See driver_probe_device() in driver/base/dd.c
332 		 */
333 		return -ENODEV;
334 	}
335 
336 	if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
337 		dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
338 			 udev_busid);
339 		return -ENODEV;
340 	}
341 
342 	if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
343 		dev_dbg(&udev->dev,
344 			"%s is attached on vhci_hcd... skip!\n",
345 			udev_busid);
346 
347 		return -ENODEV;
348 	}
349 
350 	/* ok, this is my device */
351 	sdev = stub_device_alloc(udev);
352 	if (!sdev)
353 		return -ENOMEM;
354 
355 	dev_info(&udev->dev,
356 		"usbip-host: register new device (bus %u dev %u)\n",
357 		udev->bus->busnum, udev->devnum);
358 
359 	busid_priv->shutdown_busid = 0;
360 
361 	/* set private data to usb_device */
362 	dev_set_drvdata(&udev->dev, sdev);
363 	busid_priv->sdev = sdev;
364 	busid_priv->udev = udev;
365 
366 	/*
367 	 * Claim this hub port.
368 	 * It doesn't matter what value we pass as owner
369 	 * (struct dev_state) as long as it is unique.
370 	 */
371 	rc = usb_hub_claim_port(udev->parent, udev->portnum,
372 			(struct usb_dev_state *) udev);
373 	if (rc) {
374 		dev_dbg(&udev->dev, "unable to claim port\n");
375 		goto err_port;
376 	}
377 
378 	rc = stub_add_files(&udev->dev);
379 	if (rc) {
380 		dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
381 		goto err_files;
382 	}
383 	busid_priv->status = STUB_BUSID_ALLOC;
384 
385 	return 0;
386 err_files:
387 	usb_hub_release_port(udev->parent, udev->portnum,
388 			     (struct usb_dev_state *) udev);
389 err_port:
390 	dev_set_drvdata(&udev->dev, NULL);
391 	usb_put_dev(udev);
392 
393 	busid_priv->sdev = NULL;
394 	stub_device_free(sdev);
395 	return rc;
396 }
397 
398 static 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 
404 		/* wait for the stop of the event handler */
405 		usbip_stop_eh(&busid_priv->sdev->ud);
406 	}
407 }
408 
409 /*
410  * called in usb_disconnect() or usb_deregister()
411  * but only if actconfig(active configuration) exists
412  */
413 static void stub_disconnect(struct usb_device *udev)
414 {
415 	struct stub_device *sdev;
416 	const char *udev_busid = dev_name(&udev->dev);
417 	struct bus_id_priv *busid_priv;
418 	int rc;
419 
420 	dev_dbg(&udev->dev, "Enter\n");
421 
422 	busid_priv = get_busid_priv(udev_busid);
423 	if (!busid_priv) {
424 		BUG();
425 		return;
426 	}
427 
428 	sdev = dev_get_drvdata(&udev->dev);
429 
430 	/* get stub_device */
431 	if (!sdev) {
432 		dev_err(&udev->dev, "could not get device");
433 		return;
434 	}
435 
436 	dev_set_drvdata(&udev->dev, NULL);
437 
438 	/*
439 	 * NOTE: rx/tx threads are invoked for each usb_device.
440 	 */
441 	stub_remove_files(&udev->dev);
442 
443 	/* release port */
444 	rc = usb_hub_release_port(udev->parent, udev->portnum,
445 				  (struct usb_dev_state *) udev);
446 	if (rc) {
447 		dev_dbg(&udev->dev, "unable to release port\n");
448 		return;
449 	}
450 
451 	/* If usb reset is called from event handler */
452 	if (usbip_in_eh(current))
453 		return;
454 
455 	/* shutdown the current connection */
456 	shutdown_busid(busid_priv);
457 
458 	usb_put_dev(sdev->udev);
459 
460 	/* free sdev */
461 	busid_priv->sdev = NULL;
462 	stub_device_free(sdev);
463 
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 	}
470 }
471 
472 #ifdef CONFIG_PM
473 
474 /* These functions need usb_port_suspend and usb_port_resume,
475  * which reside in drivers/usb/core/usb.h. Skip for now. */
476 
477 static int stub_suspend(struct usb_device *udev, pm_message_t message)
478 {
479 	dev_dbg(&udev->dev, "stub_suspend\n");
480 
481 	return 0;
482 }
483 
484 static int stub_resume(struct usb_device *udev, pm_message_t message)
485 {
486 	dev_dbg(&udev->dev, "stub_resume\n");
487 
488 	return 0;
489 }
490 
491 #endif	/* CONFIG_PM */
492 
493 struct usb_device_driver stub_driver = {
494 	.name		= "usbip-host",
495 	.probe		= stub_probe,
496 	.disconnect	= stub_disconnect,
497 #ifdef CONFIG_PM
498 	.suspend	= stub_suspend,
499 	.resume		= stub_resume,
500 #endif
501 	.supports_autosuspend	=	0,
502 };
503