blob: c0d6ff1baa721754d42d1cae3b076685dcd86fe2 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -06002/*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -06004 */
5
matt mooney7aaacb42011-05-11 22:33:43 -07006#include <linux/device.h>
Bernard Blackham3d0a2a22012-10-22 06:45:00 +11007#include <linux/file.h>
Arnd Bergmann9720b4b2011-03-02 00:13:05 +01008#include <linux/kthread.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -04009#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060011#include "usbip_common.h"
12#include "stub.h"
13
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060014/*
matt mooneyd012c2a52011-05-19 21:37:03 -070015 * usbip_status shows the status of usbip-host as long as this driver is bound
16 * to the target device.
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060017 */
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070018static ssize_t usbip_status_show(struct device *dev,
19 struct device_attribute *attr, char *buf)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060020{
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
Harvey Yangdcf147792013-01-22 13:31:30 +080029 spin_lock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060030 status = sdev->ud.status;
Harvey Yangdcf147792013-01-22 13:31:30 +080031 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060032
33 return snprintf(buf, PAGE_SIZE, "%d\n", status);
34}
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070035static DEVICE_ATTR_RO(usbip_status);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060036
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 */
Greg Kroah-Hartmanca359102018-01-23 11:24:07 +010042static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *attr,
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060043 const char *buf, size_t count)
44{
45 struct stub_device *sdev = dev_get_drvdata(dev);
46 int sockfd = 0;
47 struct socket *socket;
Elena Oatf8cfc022014-02-27 12:26:52 +020048 int rv;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060049
50 if (!sdev) {
51 dev_err(dev, "sdev is null\n");
52 return -ENODEV;
53 }
54
Elena Oatf8cfc022014-02-27 12:26:52 +020055 rv = sscanf(buf, "%d", &sockfd);
56 if (rv != 1)
57 return -EINVAL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060058
59 if (sockfd != -1) {
Al Viro964ea962014-03-05 20:33:08 -050060 int err;
Pawel Lebioda3eed8c02014-05-14 19:20:27 +020061
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060062 dev_info(dev, "stub up\n");
63
Harvey Yangdcf147792013-01-22 13:31:30 +080064 spin_lock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060065
66 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
67 dev_err(dev, "not ready\n");
Kurt Kanzenbach31398f62013-04-04 16:03:08 +020068 goto err;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060069 }
70
Al Viro964ea962014-03-05 20:33:08 -050071 socket = sockfd_lookup(sockfd, &err);
Kurt Kanzenbach31398f62013-04-04 16:03:08 +020072 if (!socket)
73 goto err;
74
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060075 sdev->ud.tcp_socket = socket;
Shuah Khan009f41a2018-01-26 11:56:50 -070076 sdev->ud.sockfd = sockfd;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060077
Harvey Yangdcf147792013-01-22 13:31:30 +080078 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060079
Kurt Kanzenbach8c4e5832013-02-22 12:13:34 +010080 sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
81 "stub_rx");
82 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
83 "stub_tx");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060084
Harvey Yangdcf147792013-01-22 13:31:30 +080085 spin_lock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060086 sdev->ud.status = SDEV_ST_USED;
Harvey Yangdcf147792013-01-22 13:31:30 +080087 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060088
89 } else {
90 dev_info(dev, "stub down\n");
91
Harvey Yangdcf147792013-01-22 13:31:30 +080092 spin_lock_irq(&sdev->ud.lock);
Kurt Kanzenbach31398f62013-04-04 16:03:08 +020093 if (sdev->ud.status != SDEV_ST_USED)
94 goto err;
95
Harvey Yangdcf147792013-01-22 13:31:30 +080096 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060097
98 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
99 }
100
101 return count;
Kurt Kanzenbach31398f62013-04-04 16:03:08 +0200102
103err:
104 spin_unlock_irq(&sdev->ud.lock);
Al Viro964ea962014-03-05 20:33:08 -0500105 return -EINVAL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600106}
Greg Kroah-Hartmanca359102018-01-23 11:24:07 +0100107static DEVICE_ATTR_WO(usbip_sockfd);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600108
109static int stub_add_files(struct device *dev)
110{
111 int err = 0;
112
113 err = device_create_file(dev, &dev_attr_usbip_status);
114 if (err)
115 goto err_status;
116
117 err = device_create_file(dev, &dev_attr_usbip_sockfd);
118 if (err)
119 goto err_sockfd;
120
121 err = device_create_file(dev, &dev_attr_usbip_debug);
122 if (err)
123 goto err_debug;
124
125 return 0;
126
127err_debug:
128 device_remove_file(dev, &dev_attr_usbip_sockfd);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600129err_sockfd:
130 device_remove_file(dev, &dev_attr_usbip_status);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600131err_status:
132 return err;
133}
134
135static void stub_remove_files(struct device *dev)
136{
137 device_remove_file(dev, &dev_attr_usbip_status);
138 device_remove_file(dev, &dev_attr_usbip_sockfd);
139 device_remove_file(dev, &dev_attr_usbip_debug);
140}
141
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600142static void stub_shutdown_connection(struct usbip_device *ud)
143{
144 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
145
146 /*
147 * When removing an exported device, kernel panic sometimes occurred
148 * and then EIP was sk_wait_data of stub_rx thread. Is this because
149 * sk_wait_data returned though stub_rx thread was already finished by
150 * step 1?
151 */
152 if (ud->tcp_socket) {
Shuah Khan90120d12017-12-15 10:50:09 -0700153 dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600154 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
155 }
156
157 /* 1. stop threads */
navinb7caecb2012-09-19 17:07:27 +0530158 if (ud->tcp_rx) {
Oleg Nesterovba46ce32012-03-13 19:07:18 +0100159 kthread_stop_put(ud->tcp_rx);
navinb7caecb2012-09-19 17:07:27 +0530160 ud->tcp_rx = NULL;
161 }
162 if (ud->tcp_tx) {
Oleg Nesterovba46ce32012-03-13 19:07:18 +0100163 kthread_stop_put(ud->tcp_tx);
navinb7caecb2012-09-19 17:07:27 +0530164 ud->tcp_tx = NULL;
165 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600166
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600167 /*
matt mooney87352762011-05-19 21:36:56 -0700168 * 2. close the socket
169 *
170 * tcp_socket is freed after threads are killed so that usbip_xmit does
171 * not touch NULL socket.
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600172 */
173 if (ud->tcp_socket) {
Al Viro964ea962014-03-05 20:33:08 -0500174 sockfd_put(ud->tcp_socket);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600175 ud->tcp_socket = NULL;
Shuah Khan009f41a2018-01-26 11:56:50 -0700176 ud->sockfd = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600177 }
178
179 /* 3. free used data */
180 stub_device_cleanup_urbs(sdev);
181
182 /* 4. free stub_unlink */
183 {
184 unsigned long flags;
185 struct stub_unlink *unlink, *tmp;
186
187 spin_lock_irqsave(&sdev->priv_lock, flags);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600188 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
189 list_del(&unlink->list);
190 kfree(unlink);
191 }
matt mooney87352762011-05-19 21:36:56 -0700192 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
193 list) {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600194 list_del(&unlink->list);
195 kfree(unlink);
196 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600197 spin_unlock_irqrestore(&sdev->priv_lock, flags);
198 }
199}
200
201static void stub_device_reset(struct usbip_device *ud)
202{
203 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
Max Vozeler2d8f4592011-01-12 15:01:59 +0200204 struct usb_device *udev = sdev->udev;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600205 int ret;
206
matt mooney1a4b6f62011-05-19 16:47:32 -0700207 dev_dbg(&udev->dev, "device reset");
Max Vozeler2d8f4592011-01-12 15:01:59 +0200208
Alexander Popov8c7003a32016-04-28 13:07:22 +0300209 ret = usb_lock_device_for_reset(udev, NULL);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600210 if (ret < 0) {
211 dev_err(&udev->dev, "lock for reset\n");
Harvey Yangdcf147792013-01-22 13:31:30 +0800212 spin_lock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600213 ud->status = SDEV_ST_ERROR;
Harvey Yangdcf147792013-01-22 13:31:30 +0800214 spin_unlock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600215 return;
216 }
217
218 /* try to reset the device */
219 ret = usb_reset_device(udev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600220 usb_unlock_device(udev);
221
Harvey Yangdcf147792013-01-22 13:31:30 +0800222 spin_lock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600223 if (ret) {
224 dev_err(&udev->dev, "device reset\n");
225 ud->status = SDEV_ST_ERROR;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600226 } else {
227 dev_info(&udev->dev, "device reset\n");
228 ud->status = SDEV_ST_AVAILABLE;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600229 }
Harvey Yangdcf147792013-01-22 13:31:30 +0800230 spin_unlock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600231}
232
233static void stub_device_unusable(struct usbip_device *ud)
234{
Harvey Yangdcf147792013-01-22 13:31:30 +0800235 spin_lock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600236 ud->status = SDEV_ST_ERROR;
Harvey Yangdcf147792013-01-22 13:31:30 +0800237 spin_unlock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600238}
239
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600240/**
241 * stub_device_alloc - allocate a new stub_device struct
Alexander Popov8c7003a32016-04-28 13:07:22 +0300242 * @udev: usb_device of a new device
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600243 *
244 * Allocates and initializes a new stub_device struct.
245 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200246static struct stub_device *stub_device_alloc(struct usb_device *udev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600247{
248 struct stub_device *sdev;
Valentina Maneab7945b72014-01-23 23:12:29 +0200249 int busnum = udev->bus->busnum;
250 int devnum = udev->devnum;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600251
Valentina Maneab7945b72014-01-23 23:12:29 +0200252 dev_dbg(&udev->dev, "allocating stub device");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600253
254 /* yes, it's a new device */
255 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800256 if (!sdev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600257 return NULL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600258
Max Vozeler2d8f4592011-01-12 15:01:59 +0200259 sdev->udev = usb_get_dev(udev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600260
261 /*
262 * devid is defined with devnum when this driver is first allocated.
263 * devnum may change later if a device is reset. However, devid never
264 * changes during a usbip connection.
265 */
matt mooneyb744a452011-05-06 03:47:41 -0700266 sdev->devid = (busnum << 16) | devnum;
267 sdev->ud.side = USBIP_STUB;
268 sdev->ud.status = SDEV_ST_AVAILABLE;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600269 spin_lock_init(&sdev->ud.lock);
matt mooneyb744a452011-05-06 03:47:41 -0700270 sdev->ud.tcp_socket = NULL;
Shuah Khan009f41a2018-01-26 11:56:50 -0700271 sdev->ud.sockfd = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600272
273 INIT_LIST_HEAD(&sdev->priv_init);
274 INIT_LIST_HEAD(&sdev->priv_tx);
275 INIT_LIST_HEAD(&sdev->priv_free);
276 INIT_LIST_HEAD(&sdev->unlink_free);
277 INIT_LIST_HEAD(&sdev->unlink_tx);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600278 spin_lock_init(&sdev->priv_lock);
279
280 init_waitqueue_head(&sdev->tx_waitq);
281
282 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
283 sdev->ud.eh_ops.reset = stub_device_reset;
284 sdev->ud.eh_ops.unusable = stub_device_unusable;
285
286 usbip_start_eh(&sdev->ud);
287
Valentina Maneab7945b72014-01-23 23:12:29 +0200288 dev_dbg(&udev->dev, "register new device\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700289
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600290 return sdev;
291}
292
Kurt Kanzenbachb94b3a62013-04-04 16:03:11 +0200293static void stub_device_free(struct stub_device *sdev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600294{
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600295 kfree(sdev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600296}
297
Valentina Maneab7945b72014-01-23 23:12:29 +0200298static int stub_probe(struct usb_device *udev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600299{
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600300 struct stub_device *sdev = NULL;
Valentina Maneab7945b72014-01-23 23:12:29 +0200301 const char *udev_busid = dev_name(&udev->dev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200302 struct bus_id_priv *busid_priv;
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600303 int rc = 0;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600304
Shuah Khan28b68ac2018-04-11 18:13:30 -0600305 dev_dbg(&udev->dev, "Enter probe\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600306
307 /* check we should claim or not by busid_table */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200308 busid_priv = get_busid_priv(udev_busid);
matt mooney87352762011-05-19 21:36:56 -0700309 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
matt mooneyb744a452011-05-06 03:47:41 -0700310 (busid_priv->status == STUB_BUSID_OTHER)) {
Valentina Maneab7945b72014-01-23 23:12:29 +0200311 dev_info(&udev->dev,
Elad Wexler6165cc52013-09-05 12:07:10 +0300312 "%s is not in match_busid table... skip!\n",
313 udev_busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600314
315 /*
316 * Return value should be ENODEV or ENOXIO to continue trying
317 * other matched drivers by the driver core.
318 * See driver_probe_device() in driver/base/dd.c
319 */
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600320 rc = -ENODEV;
321 goto call_put_busid_priv;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600322 }
323
matt mooney1a4b6f62011-05-19 16:47:32 -0700324 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
325 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
326 udev_busid);
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600327 rc = -ENODEV;
328 goto call_put_busid_priv;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600329 }
330
331 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
Elad Wexler6165cc52013-09-05 12:07:10 +0300332 dev_dbg(&udev->dev,
333 "%s is attached on vhci_hcd... skip!\n",
334 udev_busid);
335
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600336 rc = -ENODEV;
337 goto call_put_busid_priv;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600338 }
339
matt mooneyd012c2a52011-05-19 21:37:03 -0700340 /* ok, this is my device */
Valentina Maneab7945b72014-01-23 23:12:29 +0200341 sdev = stub_device_alloc(udev);
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600342 if (!sdev) {
343 rc = -ENOMEM;
344 goto call_put_busid_priv;
345 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600346
Valentina Maneab7945b72014-01-23 23:12:29 +0200347 dev_info(&udev->dev,
348 "usbip-host: register new device (bus %u dev %u)\n",
349 udev->bus->busnum, udev->devnum);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600350
Endre Kollaraa5873e2010-07-27 12:39:30 +0200351 busid_priv->shutdown_busid = 0;
352
Valentina Maneab7945b72014-01-23 23:12:29 +0200353 /* set private data to usb_device */
354 dev_set_drvdata(&udev->dev, sdev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200355 busid_priv->sdev = sdev;
Valentina Maneaa46034c2014-03-08 14:53:33 +0200356 busid_priv->udev = udev;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600357
Valentina Manea6080cd02014-03-08 14:53:34 +0200358 /*
359 * Claim this hub port.
360 * It doesn't matter what value we pass as owner
361 * (struct dev_state) as long as it is unique.
362 */
363 rc = usb_hub_claim_port(udev->parent, udev->portnum,
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200364 (struct usb_dev_state *) udev);
Valentina Manea6080cd02014-03-08 14:53:34 +0200365 if (rc) {
366 dev_dbg(&udev->dev, "unable to claim port\n");
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300367 goto err_port;
Valentina Manea6080cd02014-03-08 14:53:34 +0200368 }
369
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300370 rc = stub_add_files(&udev->dev);
371 if (rc) {
Valentina Maneab7945b72014-01-23 23:12:29 +0200372 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300373 goto err_files;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600374 }
Endre Kollaraa5873e2010-07-27 12:39:30 +0200375 busid_priv->status = STUB_BUSID_ALLOC;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600376
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600377 rc = 0;
378 goto call_put_busid_priv;
379
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300380err_files:
381 usb_hub_release_port(udev->parent, udev->portnum,
382 (struct usb_dev_state *) udev);
383err_port:
384 dev_set_drvdata(&udev->dev, NULL);
385 usb_put_dev(udev);
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300386
387 busid_priv->sdev = NULL;
388 stub_device_free(sdev);
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600389
390call_put_busid_priv:
391 put_busid_priv(busid_priv);
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300392 return rc;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600393}
394
Endre Kollaraa5873e2010-07-27 12:39:30 +0200395static void shutdown_busid(struct bus_id_priv *busid_priv)
396{
397 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
398 busid_priv->shutdown_busid = 1;
399 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
400
Kurt Kanzenbach7717880742013-04-04 16:03:05 +0200401 /* wait for the stop of the event handler */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200402 usbip_stop_eh(&busid_priv->sdev->ud);
403 }
Endre Kollaraa5873e2010-07-27 12:39:30 +0200404}
405
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600406/*
407 * called in usb_disconnect() or usb_deregister()
408 * but only if actconfig(active configuration) exists
409 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200410static void stub_disconnect(struct usb_device *udev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600411{
Endre Kollaraa5873e2010-07-27 12:39:30 +0200412 struct stub_device *sdev;
Valentina Maneab7945b72014-01-23 23:12:29 +0200413 const char *udev_busid = dev_name(&udev->dev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200414 struct bus_id_priv *busid_priv;
Valentina Manea6080cd02014-03-08 14:53:34 +0200415 int rc;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200416
Shuah Khan28b68ac2018-04-11 18:13:30 -0600417 dev_dbg(&udev->dev, "Enter disconnect\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700418
Endre Kollaraa5873e2010-07-27 12:39:30 +0200419 busid_priv = get_busid_priv(udev_busid);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200420 if (!busid_priv) {
421 BUG();
422 return;
423 }
424
Valentina Maneab7945b72014-01-23 23:12:29 +0200425 sdev = dev_get_drvdata(&udev->dev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200426
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600427 /* get stub_device */
428 if (!sdev) {
Valentina Maneab7945b72014-01-23 23:12:29 +0200429 dev_err(&udev->dev, "could not get device");
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600430 goto call_put_busid_priv;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600431 }
432
Valentina Maneab7945b72014-01-23 23:12:29 +0200433 dev_set_drvdata(&udev->dev, NULL);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600434
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600435 /*
Bart Westgeestc7f00892012-10-10 13:34:27 -0400436 * NOTE: rx/tx threads are invoked for each usb_device.
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600437 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200438 stub_remove_files(&udev->dev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600439
Valentina Manea6080cd02014-03-08 14:53:34 +0200440 /* release port */
441 rc = usb_hub_release_port(udev->parent, udev->portnum,
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200442 (struct usb_dev_state *) udev);
Valentina Manea6080cd02014-03-08 14:53:34 +0200443 if (rc) {
444 dev_dbg(&udev->dev, "unable to release port\n");
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600445 goto call_put_busid_priv;
Valentina Manea6080cd02014-03-08 14:53:34 +0200446 }
447
Bart Westgeestc7f00892012-10-10 13:34:27 -0400448 /* If usb reset is called from event handler */
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900449 if (usbip_in_eh(current))
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600450 goto call_put_busid_priv;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200451
Bart Westgeestc7f00892012-10-10 13:34:27 -0400452 /* shutdown the current connection */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200453 shutdown_busid(busid_priv);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600454
Max Vozeler2d8f4592011-01-12 15:01:59 +0200455 usb_put_dev(sdev->udev);
Max Vozeler2d8f4592011-01-12 15:01:59 +0200456
Bart Westgeestc7f00892012-10-10 13:34:27 -0400457 /* free sdev */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200458 busid_priv->sdev = NULL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600459 stub_device_free(sdev);
460
Shuah Khan (Samsung OSG)7510df32018-04-30 16:17:20 -0600461 if (busid_priv->status == STUB_BUSID_ALLOC)
Endre Kollaraa5873e2010-07-27 12:39:30 +0200462 busid_priv->status = STUB_BUSID_ADDED;
Shuah Khan (Samsung OSG)220765572018-05-14 20:49:58 -0600463
464call_put_busid_priv:
465 put_busid_priv(busid_priv);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600466}
matt mooneyd012c2a52011-05-19 21:37:03 -0700467
Valentina Maneab7945b72014-01-23 23:12:29 +0200468#ifdef CONFIG_PM
Akshay Joshi553a1a52011-08-17 15:58:31 -0400469
Valentina Maneab7945b72014-01-23 23:12:29 +0200470/* These functions need usb_port_suspend and usb_port_resume,
471 * which reside in drivers/usb/core/usb.h. Skip for now. */
472
473static int stub_suspend(struct usb_device *udev, pm_message_t message)
Arjan Mels1aee1992011-06-30 22:18:18 +0200474{
Valentina Maneab7945b72014-01-23 23:12:29 +0200475 dev_dbg(&udev->dev, "stub_suspend\n");
476
Arjan Mels1aee1992011-06-30 22:18:18 +0200477 return 0;
478}
479
Valentina Maneab7945b72014-01-23 23:12:29 +0200480static int stub_resume(struct usb_device *udev, pm_message_t message)
Arjan Mels1aee1992011-06-30 22:18:18 +0200481{
Valentina Maneab7945b72014-01-23 23:12:29 +0200482 dev_dbg(&udev->dev, "stub_resume\n");
483
Arjan Mels1aee1992011-06-30 22:18:18 +0200484 return 0;
485}
486
Valentina Maneab7945b72014-01-23 23:12:29 +0200487#endif /* CONFIG_PM */
488
489struct usb_device_driver stub_driver = {
matt mooneyd012c2a52011-05-19 21:37:03 -0700490 .name = "usbip-host",
491 .probe = stub_probe,
492 .disconnect = stub_disconnect,
Valentina Maneab7945b72014-01-23 23:12:29 +0200493#ifdef CONFIG_PM
494 .suspend = stub_suspend,
495 .resume = stub_resume,
496#endif
497 .supports_autosuspend = 0,
Akshay Joshi97c451c2011-08-17 15:58:32 -0400498};