blob: a3df8ee82faff77d9c114ed605412ccfb0704ce6 [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 */
42static 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;
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;
76
Harvey Yangdcf147792013-01-22 13:31:30 +080077 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060078
Kurt Kanzenbach8c4e5832013-02-22 12:13:34 +010079 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");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060083
Harvey Yangdcf147792013-01-22 13:31:30 +080084 spin_lock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060085 sdev->ud.status = SDEV_ST_USED;
Harvey Yangdcf147792013-01-22 13:31:30 +080086 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060087
88 } else {
89 dev_info(dev, "stub down\n");
90
Harvey Yangdcf147792013-01-22 13:31:30 +080091 spin_lock_irq(&sdev->ud.lock);
Kurt Kanzenbach31398f62013-04-04 16:03:08 +020092 if (sdev->ud.status != SDEV_ST_USED)
93 goto err;
94
Harvey Yangdcf147792013-01-22 13:31:30 +080095 spin_unlock_irq(&sdev->ud.lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060096
97 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
98 }
99
100 return count;
Kurt Kanzenbach31398f62013-04-04 16:03:08 +0200101
102err:
103 spin_unlock_irq(&sdev->ud.lock);
Al Viro964ea962014-03-05 20:33:08 -0500104 return -EINVAL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600105}
106static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
107
108static 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
126err_debug:
127 device_remove_file(dev, &dev_attr_usbip_sockfd);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600128err_sockfd:
129 device_remove_file(dev, &dev_attr_usbip_status);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600130err_status:
131 return err;
132}
133
134static 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
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600141static 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) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700152 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
153 ud->tcp_socket);
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;
176 }
177
178 /* 3. free used data */
179 stub_device_cleanup_urbs(sdev);
180
181 /* 4. free stub_unlink */
182 {
183 unsigned long flags;
184 struct stub_unlink *unlink, *tmp;
185
186 spin_lock_irqsave(&sdev->priv_lock, flags);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600187 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
188 list_del(&unlink->list);
189 kfree(unlink);
190 }
matt mooney87352762011-05-19 21:36:56 -0700191 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
192 list) {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600193 list_del(&unlink->list);
194 kfree(unlink);
195 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600196 spin_unlock_irqrestore(&sdev->priv_lock, flags);
197 }
198}
199
200static void stub_device_reset(struct usbip_device *ud)
201{
202 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
Max Vozeler2d8f4592011-01-12 15:01:59 +0200203 struct usb_device *udev = sdev->udev;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600204 int ret;
205
matt mooney1a4b6f62011-05-19 16:47:32 -0700206 dev_dbg(&udev->dev, "device reset");
Max Vozeler2d8f4592011-01-12 15:01:59 +0200207
Alexander Popov8c7003a32016-04-28 13:07:22 +0300208 ret = usb_lock_device_for_reset(udev, NULL);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600209 if (ret < 0) {
210 dev_err(&udev->dev, "lock for reset\n");
Harvey Yangdcf147792013-01-22 13:31:30 +0800211 spin_lock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600212 ud->status = SDEV_ST_ERROR;
Harvey Yangdcf147792013-01-22 13:31:30 +0800213 spin_unlock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600214 return;
215 }
216
217 /* try to reset the device */
218 ret = usb_reset_device(udev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600219 usb_unlock_device(udev);
220
Harvey Yangdcf147792013-01-22 13:31:30 +0800221 spin_lock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600222 if (ret) {
223 dev_err(&udev->dev, "device reset\n");
224 ud->status = SDEV_ST_ERROR;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600225 } else {
226 dev_info(&udev->dev, "device reset\n");
227 ud->status = SDEV_ST_AVAILABLE;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600228 }
Harvey Yangdcf147792013-01-22 13:31:30 +0800229 spin_unlock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600230}
231
232static void stub_device_unusable(struct usbip_device *ud)
233{
Harvey Yangdcf147792013-01-22 13:31:30 +0800234 spin_lock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600235 ud->status = SDEV_ST_ERROR;
Harvey Yangdcf147792013-01-22 13:31:30 +0800236 spin_unlock_irq(&ud->lock);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600237}
238
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600239/**
240 * stub_device_alloc - allocate a new stub_device struct
Alexander Popov8c7003a32016-04-28 13:07:22 +0300241 * @udev: usb_device of a new device
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600242 *
243 * Allocates and initializes a new stub_device struct.
244 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200245static struct stub_device *stub_device_alloc(struct usb_device *udev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600246{
247 struct stub_device *sdev;
Valentina Maneab7945b72014-01-23 23:12:29 +0200248 int busnum = udev->bus->busnum;
249 int devnum = udev->devnum;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600250
Valentina Maneab7945b72014-01-23 23:12:29 +0200251 dev_dbg(&udev->dev, "allocating stub device");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600252
253 /* yes, it's a new device */
254 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800255 if (!sdev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600256 return NULL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600257
Max Vozeler2d8f4592011-01-12 15:01:59 +0200258 sdev->udev = usb_get_dev(udev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600259
260 /*
261 * devid is defined with devnum when this driver is first allocated.
262 * devnum may change later if a device is reset. However, devid never
263 * changes during a usbip connection.
264 */
matt mooneyb744a452011-05-06 03:47:41 -0700265 sdev->devid = (busnum << 16) | devnum;
266 sdev->ud.side = USBIP_STUB;
267 sdev->ud.status = SDEV_ST_AVAILABLE;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600268 spin_lock_init(&sdev->ud.lock);
matt mooneyb744a452011-05-06 03:47:41 -0700269 sdev->ud.tcp_socket = NULL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600270
271 INIT_LIST_HEAD(&sdev->priv_init);
272 INIT_LIST_HEAD(&sdev->priv_tx);
273 INIT_LIST_HEAD(&sdev->priv_free);
274 INIT_LIST_HEAD(&sdev->unlink_free);
275 INIT_LIST_HEAD(&sdev->unlink_tx);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600276 spin_lock_init(&sdev->priv_lock);
277
278 init_waitqueue_head(&sdev->tx_waitq);
279
280 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
281 sdev->ud.eh_ops.reset = stub_device_reset;
282 sdev->ud.eh_ops.unusable = stub_device_unusable;
283
284 usbip_start_eh(&sdev->ud);
285
Valentina Maneab7945b72014-01-23 23:12:29 +0200286 dev_dbg(&udev->dev, "register new device\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700287
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600288 return sdev;
289}
290
Kurt Kanzenbachb94b3a62013-04-04 16:03:11 +0200291static void stub_device_free(struct stub_device *sdev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600292{
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600293 kfree(sdev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600294}
295
Valentina Maneab7945b72014-01-23 23:12:29 +0200296static int stub_probe(struct usb_device *udev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600297{
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600298 struct stub_device *sdev = NULL;
Valentina Maneab7945b72014-01-23 23:12:29 +0200299 const char *udev_busid = dev_name(&udev->dev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200300 struct bus_id_priv *busid_priv;
Valentina Manea6080cd02014-03-08 14:53:34 +0200301 int rc;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600302
Valentina Maneab7945b72014-01-23 23:12:29 +0200303 dev_dbg(&udev->dev, "Enter\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600304
305 /* check we should claim or not by busid_table */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200306 busid_priv = get_busid_priv(udev_busid);
matt mooney87352762011-05-19 21:36:56 -0700307 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
matt mooneyb744a452011-05-06 03:47:41 -0700308 (busid_priv->status == STUB_BUSID_OTHER)) {
Valentina Maneab7945b72014-01-23 23:12:29 +0200309 dev_info(&udev->dev,
Elad Wexler6165cc52013-09-05 12:07:10 +0300310 "%s is not in match_busid table... skip!\n",
311 udev_busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600312
313 /*
314 * Return value should be ENODEV or ENOXIO to continue trying
315 * other matched drivers by the driver core.
316 * See driver_probe_device() in driver/base/dd.c
317 */
318 return -ENODEV;
319 }
320
matt mooney1a4b6f62011-05-19 16:47:32 -0700321 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
322 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
323 udev_busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600324 return -ENODEV;
325 }
326
327 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
Elad Wexler6165cc52013-09-05 12:07:10 +0300328 dev_dbg(&udev->dev,
329 "%s is attached on vhci_hcd... skip!\n",
330 udev_busid);
331
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600332 return -ENODEV;
333 }
334
matt mooneyd012c2a52011-05-19 21:37:03 -0700335 /* ok, this is my device */
Valentina Maneab7945b72014-01-23 23:12:29 +0200336 sdev = stub_device_alloc(udev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600337 if (!sdev)
338 return -ENOMEM;
339
Valentina Maneab7945b72014-01-23 23:12:29 +0200340 dev_info(&udev->dev,
341 "usbip-host: register new device (bus %u dev %u)\n",
342 udev->bus->busnum, udev->devnum);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600343
Endre Kollaraa5873e2010-07-27 12:39:30 +0200344 busid_priv->shutdown_busid = 0;
345
Valentina Maneab7945b72014-01-23 23:12:29 +0200346 /* set private data to usb_device */
347 dev_set_drvdata(&udev->dev, sdev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200348 busid_priv->sdev = sdev;
Valentina Maneaa46034c2014-03-08 14:53:33 +0200349 busid_priv->udev = udev;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600350
Valentina Manea6080cd02014-03-08 14:53:34 +0200351 /*
352 * Claim this hub port.
353 * It doesn't matter what value we pass as owner
354 * (struct dev_state) as long as it is unique.
355 */
356 rc = usb_hub_claim_port(udev->parent, udev->portnum,
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200357 (struct usb_dev_state *) udev);
Valentina Manea6080cd02014-03-08 14:53:34 +0200358 if (rc) {
359 dev_dbg(&udev->dev, "unable to claim port\n");
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300360 goto err_port;
Valentina Manea6080cd02014-03-08 14:53:34 +0200361 }
362
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300363 rc = stub_add_files(&udev->dev);
364 if (rc) {
Valentina Maneab7945b72014-01-23 23:12:29 +0200365 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300366 goto err_files;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600367 }
Endre Kollaraa5873e2010-07-27 12:39:30 +0200368 busid_priv->status = STUB_BUSID_ALLOC;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600369
370 return 0;
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300371err_files:
372 usb_hub_release_port(udev->parent, udev->portnum,
373 (struct usb_dev_state *) udev);
374err_port:
375 dev_set_drvdata(&udev->dev, NULL);
376 usb_put_dev(udev);
Alexey Khoroshilov3ff67442014-11-29 01:29:10 +0300377
378 busid_priv->sdev = NULL;
379 stub_device_free(sdev);
380 return rc;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600381}
382
Endre Kollaraa5873e2010-07-27 12:39:30 +0200383static void shutdown_busid(struct bus_id_priv *busid_priv)
384{
385 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
386 busid_priv->shutdown_busid = 1;
387 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
388
Kurt Kanzenbach7717880742013-04-04 16:03:05 +0200389 /* wait for the stop of the event handler */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200390 usbip_stop_eh(&busid_priv->sdev->ud);
391 }
Endre Kollaraa5873e2010-07-27 12:39:30 +0200392}
393
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600394/*
395 * called in usb_disconnect() or usb_deregister()
396 * but only if actconfig(active configuration) exists
397 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200398static void stub_disconnect(struct usb_device *udev)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600399{
Endre Kollaraa5873e2010-07-27 12:39:30 +0200400 struct stub_device *sdev;
Valentina Maneab7945b72014-01-23 23:12:29 +0200401 const char *udev_busid = dev_name(&udev->dev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200402 struct bus_id_priv *busid_priv;
Valentina Manea6080cd02014-03-08 14:53:34 +0200403 int rc;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200404
Valentina Maneab7945b72014-01-23 23:12:29 +0200405 dev_dbg(&udev->dev, "Enter\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700406
Endre Kollaraa5873e2010-07-27 12:39:30 +0200407 busid_priv = get_busid_priv(udev_busid);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200408 if (!busid_priv) {
409 BUG();
410 return;
411 }
412
Valentina Maneab7945b72014-01-23 23:12:29 +0200413 sdev = dev_get_drvdata(&udev->dev);
Endre Kollaraa5873e2010-07-27 12:39:30 +0200414
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600415 /* get stub_device */
416 if (!sdev) {
Valentina Maneab7945b72014-01-23 23:12:29 +0200417 dev_err(&udev->dev, "could not get device");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600418 return;
419 }
420
Valentina Maneab7945b72014-01-23 23:12:29 +0200421 dev_set_drvdata(&udev->dev, NULL);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600422
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600423 /*
Bart Westgeestc7f00892012-10-10 13:34:27 -0400424 * NOTE: rx/tx threads are invoked for each usb_device.
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600425 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200426 stub_remove_files(&udev->dev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600427
Valentina Manea6080cd02014-03-08 14:53:34 +0200428 /* release port */
429 rc = usb_hub_release_port(udev->parent, udev->portnum,
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200430 (struct usb_dev_state *) udev);
Valentina Manea6080cd02014-03-08 14:53:34 +0200431 if (rc) {
432 dev_dbg(&udev->dev, "unable to release port\n");
433 return;
434 }
435
Bart Westgeestc7f00892012-10-10 13:34:27 -0400436 /* If usb reset is called from event handler */
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900437 if (usbip_in_eh(current))
Endre Kollaraa5873e2010-07-27 12:39:30 +0200438 return;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200439
Bart Westgeestc7f00892012-10-10 13:34:27 -0400440 /* shutdown the current connection */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200441 shutdown_busid(busid_priv);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600442
Max Vozeler2d8f4592011-01-12 15:01:59 +0200443 usb_put_dev(sdev->udev);
Max Vozeler2d8f4592011-01-12 15:01:59 +0200444
Bart Westgeestc7f00892012-10-10 13:34:27 -0400445 /* free sdev */
Endre Kollaraa5873e2010-07-27 12:39:30 +0200446 busid_priv->sdev = NULL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600447 stub_device_free(sdev);
448
Endre Kollaraa5873e2010-07-27 12:39:30 +0200449 if (busid_priv->status == STUB_BUSID_ALLOC) {
450 busid_priv->status = STUB_BUSID_ADDED;
451 } else {
452 busid_priv->status = STUB_BUSID_OTHER;
453 del_match_busid((char *)udev_busid);
454 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600455}
matt mooneyd012c2a52011-05-19 21:37:03 -0700456
Valentina Maneab7945b72014-01-23 23:12:29 +0200457#ifdef CONFIG_PM
Akshay Joshi553a1a52011-08-17 15:58:31 -0400458
Valentina Maneab7945b72014-01-23 23:12:29 +0200459/* These functions need usb_port_suspend and usb_port_resume,
460 * which reside in drivers/usb/core/usb.h. Skip for now. */
461
462static int stub_suspend(struct usb_device *udev, pm_message_t message)
Arjan Mels1aee1992011-06-30 22:18:18 +0200463{
Valentina Maneab7945b72014-01-23 23:12:29 +0200464 dev_dbg(&udev->dev, "stub_suspend\n");
465
Arjan Mels1aee1992011-06-30 22:18:18 +0200466 return 0;
467}
468
Valentina Maneab7945b72014-01-23 23:12:29 +0200469static int stub_resume(struct usb_device *udev, pm_message_t message)
Arjan Mels1aee1992011-06-30 22:18:18 +0200470{
Valentina Maneab7945b72014-01-23 23:12:29 +0200471 dev_dbg(&udev->dev, "stub_resume\n");
472
Arjan Mels1aee1992011-06-30 22:18:18 +0200473 return 0;
474}
475
Valentina Maneab7945b72014-01-23 23:12:29 +0200476#endif /* CONFIG_PM */
477
478struct usb_device_driver stub_driver = {
matt mooneyd012c2a52011-05-19 21:37:03 -0700479 .name = "usbip-host",
480 .probe = stub_probe,
481 .disconnect = stub_disconnect,
Valentina Maneab7945b72014-01-23 23:12:29 +0200482#ifdef CONFIG_PM
483 .suspend = stub_suspend,
484 .resume = stub_resume,
485#endif
486 .supports_autosuspend = 0,
Akshay Joshi97c451c2011-08-17 15:58:32 -0400487};