blob: c653ce533430acfe39b193458fab74664ea3067c [file] [log] [blame]
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -06001/*
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
matt mooney7aaacb42011-05-11 22:33:43 -070020#include <linux/device.h>
Bernard Blackham3d0a2a22012-10-22 06:45:00 +110021#include <linux/file.h>
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010022#include <linux/kthread.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040023#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060025#include "usbip_common.h"
26#include "stub.h"
27
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060028/*
matt mooneyd012c2a52011-05-19 21:37:03 -070029 * usbip_status shows the status of usbip-host as long as this driver is bound
30 * to the target device.
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060031 */
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070032static ssize_t usbip_status_show(struct device *dev,
33 struct device_attribute *attr, char *buf)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060034{
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
Harvey Yangdcf147792013-01-22 13:31:30 +080043 spin_lock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060044 status = sdev->ud.status;
Harvey Yangdcf147792013-01-22 13:31:30 +080045 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060046
47 return snprintf(buf, PAGE_SIZE, "%d\n", status);
48}
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070049static DEVICE_ATTR_RO(usbip_status);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060050
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;
Elena Oatf8cfc022014-02-27 12:26:52 +020062 int rv;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060063
64 if (!sdev) {
65 dev_err(dev, "sdev is null\n");
66 return -ENODEV;
67 }
68
Elena Oatf8cfc022014-02-27 12:26:52 +020069 rv = sscanf(buf, "%d", &sockfd);
70 if (rv != 1)
71 return -EINVAL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060072
73 if (sockfd != -1) {
Al Viro964ea962014-03-05 20:33:08 -050074 int err;
Pawel Lebioda3eed8c02014-05-14 19:20:27 +020075
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060076 dev_info(dev, "stub up\n");
77
Harvey Yangdcf147792013-01-22 13:31:30 +080078 spin_lock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060079
80 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
81 dev_err(dev, "not ready\n");
Kurt Kanzenbach31398f62013-04-04 16:03:08 +020082 goto err;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060083 }
84
Al Viro964ea962014-03-05 20:33:08 -050085 socket = sockfd_lookup(sockfd, &err);
Kurt Kanzenbach31398f62013-04-04 16:03:08 +020086 if (!socket)
87 goto err;
88
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060089 sdev->ud.tcp_socket = socket;
90
Harvey Yangdcf147792013-01-22 13:31:30 +080091 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060092
Kurt Kanzenbach8c4e5832013-02-22 12:13:34 +010093 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");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060097
Harvey Yangdcf147792013-01-22 13:31:30 +080098 spin_lock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060099 sdev->ud.status = SDEV_ST_USED;
Harvey Yangdcf147792013-01-22 13:31:30 +0800100 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600101
102 } else {
103 dev_info(dev, "stub down\n");
104
Harvey Yangdcf147792013-01-22 13:31:30 +0800105 spin_lock_irq(&sdev->ud.lock);
Kurt Kanzenbach31398f62013-04-04 16:03:08 +0200106 if (sdev->ud.status != SDEV_ST_USED)
107 goto err;
108
Harvey Yangdcf147792013-01-22 13:31:30 +0800109 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600110
111 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
112 }
113
114 return count;
Kurt Kanzenbach31398f62013-04-04 16:03:08 +0200115
116err:
117 spin_unlock_irq(&sdev->ud.lock);
Al Viro964ea962014-03-05 20:33:08 -0500118 return -EINVAL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600119}
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);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600142err_sockfd:
143 device_remove_file(dev, &dev_attr_usbip_status);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600144err_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
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600155static 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) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700166 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
167 ud->tcp_socket);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600168 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
169 }
170
171 /* 1. stop threads */
navinb7caecb2012-09-19 17:07:27 +0530172 if (ud->tcp_rx) {
Oleg Nesterovba46ce32012-03-13 19:07:18 +0100173 kthread_stop_put(ud->tcp_rx);
navinb7caecb2012-09-19 17:07:27 +0530174 ud->tcp_rx = NULL;
175 }
176 if (ud->tcp_tx) {
Oleg Nesterovba46ce32012-03-13 19:07:18 +0100177 kthread_stop_put(ud->tcp_tx);
navinb7caecb2012-09-19 17:07:27 +0530178 ud->tcp_tx = NULL;
179 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600180
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600181 /*
matt mooney87352762011-05-19 21:36:56 -0700182 * 2. close the socket
183 *
184 * tcp_socket is freed after threads are killed so that usbip_xmit does
185 * not touch NULL socket.
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600186 */
187 if (ud->tcp_socket) {
Al Viro964ea962014-03-05 20:33:08 -0500188 sockfd_put(ud->tcp_socket);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600189 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);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600201 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
202 list_del(&unlink->list);
203 kfree(unlink);
204 }
matt mooney87352762011-05-19 21:36:56 -0700205 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
206 list) {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600207 list_del(&unlink->list);
208 kfree(unlink);
209 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600210 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);
Max Vozeler2d8f4592011-01-12 15:01:59 +0200217 struct usb_device *udev = sdev->udev;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600218 int ret;
219
matt mooney1a4b6f62011-05-19 16:47:32 -0700220 dev_dbg(&udev->dev, "device reset");
Max Vozeler2d8f4592011-01-12 15:01:59 +0200221
Alexander Popov8c7003a32016-04-28 13:07:22 +0300222 ret = usb_lock_device_for_reset(udev, NULL);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600223 if (ret < 0) {
224 dev_err(&udev->dev, "lock for reset\n");
Harvey Yangdcf147792013-01-22 13:31:30 +0800225 spin_lock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600226 ud->status = SDEV_ST_ERROR;
Harvey Yangdcf147792013-01-22 13:31:30 +0800227 spin_unlock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600228 return;
229 }
230
231 /* try to reset the device */
232 ret = usb_reset_device(udev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600233 usb_unlock_device(udev);
234
Harvey Yangdcf147792013-01-22 13:31:30 +0800235 spin_lock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600236 if (ret) {
237 dev_err(&udev->dev, "device reset\n");
238 ud->status = SDEV_ST_ERROR;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600239 } else {
240 dev_info(&udev->dev, "device reset\n");
241 ud->status = SDEV_ST_AVAILABLE;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600242 }
Harvey Yangdcf147792013-01-22 13:31:30 +0800243 spin_unlock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600244}
245
246static void stub_device_unusable(struct usbip_device *ud)
247{
Harvey Yangdcf147792013-01-22 13:31:30 +0800248 spin_lock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600249 ud->status = SDEV_ST_ERROR;
Harvey Yangdcf147792013-01-22 13:31:30 +0800250 spin_unlock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600251}
252
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600253/**
254 * stub_device_alloc - allocate a new stub_device struct
Alexander Popov8c7003a32016-04-28 13:07:22 +0300255 * @udev: usb_device of a new device
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600256 *
257 * Allocates and initializes a new stub_device struct.
258 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200259static struct stub_device *stub_device_alloc(struct usb_device *udev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600260{
261 struct stub_device *sdev;
Valentina Maneab7945b72014-01-23 23:12:29 +0200262 int busnum = udev->bus->busnum;
263 int devnum = udev->devnum;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600264
Valentina Maneab7945b72014-01-23 23:12:29 +0200265 dev_dbg(&udev->dev, "allocating stub device");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600266
267 /* yes, it's a new device */
268 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800269 if (!sdev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600270 return NULL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600271
Max Vozeler2d8f4592011-01-12 15:01:59 +0200272 sdev->udev = usb_get_dev(udev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600273
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 */
matt mooneyb744a452011-05-06 03:47:41 -0700279 sdev->devid = (busnum << 16) | devnum;
280 sdev->ud.side = USBIP_STUB;
281 sdev->ud.status = SDEV_ST_AVAILABLE;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600282 spin_lock_init(&sdev->ud.lock);
matt mooneyb744a452011-05-06 03:47:41 -0700283 sdev->ud.tcp_socket = NULL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600284
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);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600290 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
Valentina Maneab7945b72014-01-23 23:12:29 +0200300 dev_dbg(&udev->dev, "register new device\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700301
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600302 return sdev;
303}
304
Kurt Kanzenbachb94b3a62013-04-04 16:03:11 +0200305static void stub_device_free(struct stub_device *sdev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600306{
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600307 kfree(sdev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600308}
309
Valentina Maneab7945b72014-01-23 23:12:29 +0200310static int stub_probe(struct usb_device *udev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600311{
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600312 struct stub_device *sdev = NULL;
Valentina Maneab7945b72014-01-23 23:12:29 +0200313 const char *udev_busid = dev_name(&udev->dev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200314 struct bus_id_priv *busid_priv;
Valentina Manea6080cd02014-03-08 14:53:34 +0200315 int rc;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600316
Valentina Maneab7945b72014-01-23 23:12:29 +0200317 dev_dbg(&udev->dev, "Enter\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600318
319 /* check we should claim or not by busid_table */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200320 busid_priv = get_busid_priv(udev_busid);
matt mooney87352762011-05-19 21:36:56 -0700321 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
matt mooneyb744a452011-05-06 03:47:41 -0700322 (busid_priv->status == STUB_BUSID_OTHER)) {
Valentina Maneab7945b72014-01-23 23:12:29 +0200323 dev_info(&udev->dev,
Elad Wexler6165cc52013-09-05 12:07:10 +0300324 "%s is not in match_busid table... skip!\n",
325 udev_busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600326
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
matt mooney1a4b6f62011-05-19 16:47:32 -0700335 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
336 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
337 udev_busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600338 return -ENODEV;
339 }
340
341 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
Elad Wexler6165cc52013-09-05 12:07:10 +0300342 dev_dbg(&udev->dev,
343 "%s is attached on vhci_hcd... skip!\n",
344 udev_busid);
345
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600346 return -ENODEV;
347 }
348
matt mooneyd012c2a52011-05-19 21:37:03 -0700349 /* ok, this is my device */
Valentina Maneab7945b72014-01-23 23:12:29 +0200350 sdev = stub_device_alloc(udev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600351 if (!sdev)
352 return -ENOMEM;
353
Valentina Maneab7945b72014-01-23 23:12:29 +0200354 dev_info(&udev->dev,
355 "usbip-host: register new device (bus %u dev %u)\n",
356 udev->bus->busnum, udev->devnum);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600357
Endre Kollaraa5873e2010-07-27 12:39:30 +0200358 busid_priv->shutdown_busid = 0;
359
Valentina Maneab7945b72014-01-23 23:12:29 +0200360 /* set private data to usb_device */
361 dev_set_drvdata(&udev->dev, sdev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200362 busid_priv->sdev = sdev;
Valentina Maneaa46034c2014-03-08 14:53:33 +0200363 busid_priv->udev = udev;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600364
Valentina Manea6080cd02014-03-08 14:53:34 +0200365 /*
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,
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200371 (struct usb_dev_state *) udev);
Valentina Manea6080cd02014-03-08 14:53:34 +0200372 if (rc) {
373 dev_dbg(&udev->dev, "unable to claim port\n");
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300374 goto err_port;
Valentina Manea6080cd02014-03-08 14:53:34 +0200375 }
376
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300377 rc = stub_add_files(&udev->dev);
378 if (rc) {
Valentina Maneab7945b72014-01-23 23:12:29 +0200379 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300380 goto err_files;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600381 }
Endre Kollaraa5873e2010-07-27 12:39:30 +0200382 busid_priv->status = STUB_BUSID_ALLOC;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600383
384 return 0;
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300385err_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);
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300391
392 busid_priv->sdev = NULL;
393 stub_device_free(sdev);
394 return rc;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600395}
396
Endre Kollaraa5873e2010-07-27 12:39:30 +0200397static void shutdown_busid(struct bus_id_priv *busid_priv)
398{
399 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
400 busid_priv->shutdown_busid = 1;
401 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
402
Kurt Kanzenbach7717880742013-04-04 16:03:05 +0200403 /* wait for the stop of the event handler */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200404 usbip_stop_eh(&busid_priv->sdev->ud);
405 }
Endre Kollaraa5873e2010-07-27 12:39:30 +0200406}
407
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600408/*
409 * called in usb_disconnect() or usb_deregister()
410 * but only if actconfig(active configuration) exists
411 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200412static void stub_disconnect(struct usb_device *udev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600413{
Endre Kollaraa5873e2010-07-27 12:39:30 +0200414 struct stub_device *sdev;
Valentina Maneab7945b72014-01-23 23:12:29 +0200415 const char *udev_busid = dev_name(&udev->dev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200416 struct bus_id_priv *busid_priv;
Valentina Manea6080cd02014-03-08 14:53:34 +0200417 int rc;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200418
Valentina Maneab7945b72014-01-23 23:12:29 +0200419 dev_dbg(&udev->dev, "Enter\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700420
Endre Kollaraa5873e2010-07-27 12:39:30 +0200421 busid_priv = get_busid_priv(udev_busid);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200422 if (!busid_priv) {
423 BUG();
424 return;
425 }
426
Valentina Maneab7945b72014-01-23 23:12:29 +0200427 sdev = dev_get_drvdata(&udev->dev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200428
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600429 /* get stub_device */
430 if (!sdev) {
Valentina Maneab7945b72014-01-23 23:12:29 +0200431 dev_err(&udev->dev, "could not get device");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600432 return;
433 }
434
Valentina Maneab7945b72014-01-23 23:12:29 +0200435 dev_set_drvdata(&udev->dev, NULL);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600436
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600437 /*
Bart Westgeestc7f00892012-10-10 13:34:27 -0400438 * NOTE: rx/tx threads are invoked for each usb_device.
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600439 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200440 stub_remove_files(&udev->dev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600441
Valentina Manea6080cd02014-03-08 14:53:34 +0200442 /* release port */
443 rc = usb_hub_release_port(udev->parent, udev->portnum,
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200444 (struct usb_dev_state *) udev);
Valentina Manea6080cd02014-03-08 14:53:34 +0200445 if (rc) {
446 dev_dbg(&udev->dev, "unable to release port\n");
447 return;
448 }
449
Bart Westgeestc7f00892012-10-10 13:34:27 -0400450 /* If usb reset is called from event handler */
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900451 if (usbip_in_eh(current))
Endre Kollaraa5873e2010-07-27 12:39:30 +0200452 return;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200453
Bart Westgeestc7f00892012-10-10 13:34:27 -0400454 /* shutdown the current connection */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200455 shutdown_busid(busid_priv);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600456
Max Vozeler2d8f4592011-01-12 15:01:59 +0200457 usb_put_dev(sdev->udev);
Max Vozeler2d8f4592011-01-12 15:01:59 +0200458
Bart Westgeestc7f00892012-10-10 13:34:27 -0400459 /* free sdev */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200460 busid_priv->sdev = NULL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600461 stub_device_free(sdev);
462
Endre Kollaraa5873e2010-07-27 12:39:30 +0200463 if (busid_priv->status == STUB_BUSID_ALLOC) {
464 busid_priv->status = STUB_BUSID_ADDED;
465 } else {
466 busid_priv->status = STUB_BUSID_OTHER;
467 del_match_busid((char *)udev_busid);
468 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600469}
matt mooneyd012c2a52011-05-19 21:37:03 -0700470
Valentina Maneab7945b72014-01-23 23:12:29 +0200471#ifdef CONFIG_PM
Akshay Joshi553a1a52011-08-17 15:58:31 -0400472
Valentina Maneab7945b72014-01-23 23:12:29 +0200473/* These functions need usb_port_suspend and usb_port_resume,
474 * which reside in drivers/usb/core/usb.h. Skip for now. */
475
476static int stub_suspend(struct usb_device *udev, pm_message_t message)
Arjan Mels1aee1992011-06-30 22:18:18 +0200477{
Valentina Maneab7945b72014-01-23 23:12:29 +0200478 dev_dbg(&udev->dev, "stub_suspend\n");
479
Arjan Mels1aee1992011-06-30 22:18:18 +0200480 return 0;
481}
482
Valentina Maneab7945b72014-01-23 23:12:29 +0200483static int stub_resume(struct usb_device *udev, pm_message_t message)
Arjan Mels1aee1992011-06-30 22:18:18 +0200484{
Valentina Maneab7945b72014-01-23 23:12:29 +0200485 dev_dbg(&udev->dev, "stub_resume\n");
486
Arjan Mels1aee1992011-06-30 22:18:18 +0200487 return 0;
488}
489
Valentina Maneab7945b72014-01-23 23:12:29 +0200490#endif /* CONFIG_PM */
491
492struct usb_device_driver stub_driver = {
matt mooneyd012c2a52011-05-19 21:37:03 -0700493 .name = "usbip-host",
494 .probe = stub_probe,
495 .disconnect = stub_disconnect,
Valentina Maneab7945b72014-01-23 23:12:29 +0200496#ifdef CONFIG_PM
497 .suspend = stub_suspend,
498 .resume = stub_resume,
499#endif
500 .supports_autosuspend = 0,
Akshay Joshi97c451c2011-08-17 15:58:32 -0400501};