blob: c31c8402a0c55ddd2f4b463ebabd28be6438f490 [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/string.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -04007#include <linux/module.h>
Valentina Maneaa46034c2014-03-08 14:53:33 +02008#include <linux/device.h>
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -06009
10#include "usbip_common.h"
11#include "stub.h"
12
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060013#define DRIVER_AUTHOR "Takahiro Hirofuchi"
matt mooney64e62422011-05-11 22:33:44 -070014#define DRIVER_DESC "USB/IP Host Driver"
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060015
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060016struct kmem_cache *stub_priv_cache;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060017/*
18 * busid_tables defines matching busids that usbip can grab. A user can change
19 * dynamically what device is locally used and what device is exported to a
20 * remote host.
21 */
22#define MAX_BUSID 16
Endre Kollaraa5873e2010-07-27 12:39:30 +020023static struct bus_id_priv busid_table[MAX_BUSID];
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060024static spinlock_t busid_table_lock;
25
matt mooneyefad25e2011-05-19 21:36:57 -070026static void init_busid_table(void)
27{
Kurt Kanzenbach521e1e72013-04-04 16:03:10 +020028 /*
29 * This also sets the bus_table[i].status to
30 * STUB_BUSID_OTHER, which is 0.
31 */
matt mooney0a186be2011-05-27 01:49:24 -070032 memset(busid_table, 0, sizeof(busid_table));
matt mooneyefad25e2011-05-19 21:36:57 -070033
34 spin_lock_init(&busid_table_lock);
35}
36
matt mooney41e02f02011-05-19 21:36:58 -070037/*
38 * Find the index of the busid by name.
39 * Must be called with busid_table_lock held.
40 */
41static int get_busid_idx(const char *busid)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060042{
43 int i;
matt mooney41e02f02011-05-19 21:36:58 -070044 int idx = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060045
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060046 for (i = 0; i < MAX_BUSID; i++)
Endre Kollaraa5873e2010-07-27 12:39:30 +020047 if (busid_table[i].name[0])
48 if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
matt mooney41e02f02011-05-19 21:36:58 -070049 idx = i;
50 break;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060051 }
matt mooney41e02f02011-05-19 21:36:58 -070052 return idx;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060053}
54
Endre Kollaraa5873e2010-07-27 12:39:30 +020055struct bus_id_priv *get_busid_priv(const char *busid)
56{
matt mooney41e02f02011-05-19 21:36:58 -070057 int idx;
58 struct bus_id_priv *bid = NULL;
Endre Kollaraa5873e2010-07-27 12:39:30 +020059
60 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -070061 idx = get_busid_idx(busid);
62 if (idx >= 0)
63 bid = &(busid_table[idx]);
Endre Kollaraa5873e2010-07-27 12:39:30 +020064 spin_unlock(&busid_table_lock);
65
matt mooney41e02f02011-05-19 21:36:58 -070066 return bid;
Endre Kollaraa5873e2010-07-27 12:39:30 +020067}
68
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060069static int add_match_busid(char *busid)
70{
71 int i;
matt mooney41e02f02011-05-19 21:36:58 -070072 int ret = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060073
74 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -070075 /* already registered? */
76 if (get_busid_idx(busid) >= 0) {
77 ret = 0;
78 goto out;
79 }
80
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060081 for (i = 0; i < MAX_BUSID; i++)
Endre Kollaraa5873e2010-07-27 12:39:30 +020082 if (!busid_table[i].name[0]) {
Rickard Strandqvist96955ed2014-07-26 16:43:20 +020083 strlcpy(busid_table[i].name, busid, BUSID_SIZE);
Endre Kollaraa5873e2010-07-27 12:39:30 +020084 if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
85 (busid_table[i].status != STUB_BUSID_REMOV))
86 busid_table[i].status = STUB_BUSID_ADDED;
matt mooney41e02f02011-05-19 21:36:58 -070087 ret = 0;
88 break;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060089 }
matt mooney41e02f02011-05-19 21:36:58 -070090
91out:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060092 spin_unlock(&busid_table_lock);
93
matt mooney41e02f02011-05-19 21:36:58 -070094 return ret;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060095}
96
Endre Kollaraa5873e2010-07-27 12:39:30 +020097int del_match_busid(char *busid)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060098{
matt mooney41e02f02011-05-19 21:36:58 -070099 int idx;
100 int ret = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600101
102 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -0700103 idx = get_busid_idx(busid);
104 if (idx < 0)
105 goto out;
106
107 /* found */
108 ret = 0;
109
110 if (busid_table[idx].status == STUB_BUSID_OTHER)
111 memset(busid_table[idx].name, 0, BUSID_SIZE);
112
113 if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
114 (busid_table[idx].status != STUB_BUSID_ADDED))
115 busid_table[idx].status = STUB_BUSID_REMOV;
116
117out:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600118 spin_unlock(&busid_table_lock);
119
matt mooney41e02f02011-05-19 21:36:58 -0700120 return ret;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600121}
matt mooney6c033b42011-05-06 03:47:42 -0700122
Greg Kroah-Hartmancc3d53d2017-06-09 11:03:14 +0200123static ssize_t match_busid_show(struct device_driver *drv, char *buf)
Endre Kollaraa5873e2010-07-27 12:39:30 +0200124{
125 int i;
matt mooneyefad25e2011-05-19 21:36:57 -0700126 char *out = buf;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200127
matt mooneyefad25e2011-05-19 21:36:57 -0700128 spin_lock(&busid_table_lock);
129 for (i = 0; i < MAX_BUSID; i++)
130 if (busid_table[i].name[0])
131 out += sprintf(out, "%s ", busid_table[i].name);
132 spin_unlock(&busid_table_lock);
matt mooneyefad25e2011-05-19 21:36:57 -0700133 out += sprintf(out, "\n");
matt mooney41e02f02011-05-19 21:36:58 -0700134
matt mooneyefad25e2011-05-19 21:36:57 -0700135 return out - buf;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200136}
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600137
Greg Kroah-Hartmancc3d53d2017-06-09 11:03:14 +0200138static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
matt mooney6c033b42011-05-06 03:47:42 -0700139 size_t count)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600140{
141 int len;
Kay Sieverse9133972008-10-30 01:59:32 +0100142 char busid[BUSID_SIZE];
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600143
144 if (count < 5)
145 return -EINVAL;
146
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600147 /* busid needs to include \0 termination */
Rickard Strandqvist96955ed2014-07-26 16:43:20 +0200148 len = strlcpy(busid, buf + 4, BUSID_SIZE);
149 if (sizeof(busid) <= len)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600150 return -EINVAL;
151
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600152 if (!strncmp(buf, "add ", 4)) {
Kurt Kanzenbach2183b772013-04-04 16:03:09 +0200153 if (add_match_busid(busid) < 0)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600154 return -ENOMEM;
Kurt Kanzenbach2183b772013-04-04 16:03:09 +0200155
156 pr_debug("add busid %s\n", busid);
157 return count;
matt mooney41e02f02011-05-19 21:36:58 -0700158 }
Kurt Kanzenbach2183b772013-04-04 16:03:09 +0200159
160 if (!strncmp(buf, "del ", 4)) {
161 if (del_match_busid(busid) < 0)
162 return -ENODEV;
163
164 pr_debug("del busid %s\n", busid);
165 return count;
166 }
167
168 return -EINVAL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600169}
Greg Kroah-Hartmancc3d53d2017-06-09 11:03:14 +0200170static DRIVER_ATTR_RW(match_busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600171
Valentina Maneaa46034c2014-03-08 14:53:33 +0200172static ssize_t rebind_store(struct device_driver *dev, const char *buf,
173 size_t count)
174{
175 int ret;
176 int len;
177 struct bus_id_priv *bid;
178
179 /* buf length should be less that BUSID_SIZE */
180 len = strnlen(buf, BUSID_SIZE);
181
182 if (!(len < BUSID_SIZE))
183 return -EINVAL;
184
185 bid = get_busid_priv(buf);
186 if (!bid)
187 return -ENODEV;
188
189 ret = device_attach(&bid->udev->dev);
190 if (ret < 0) {
191 dev_err(&bid->udev->dev, "rebind failed\n");
192 return ret;
193 }
194
195 return count;
196}
197
198static DRIVER_ATTR_WO(rebind);
199
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600200static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
201{
202 struct stub_priv *priv, *tmp;
203
204 list_for_each_entry_safe(priv, tmp, listhead, list) {
205 list_del(&priv->list);
206 return priv;
207 }
208
209 return NULL;
210}
211
212static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
213{
214 unsigned long flags;
215 struct stub_priv *priv;
216
217 spin_lock_irqsave(&sdev->priv_lock, flags);
218
219 priv = stub_priv_pop_from_listhead(&sdev->priv_init);
matt mooney41e02f02011-05-19 21:36:58 -0700220 if (priv)
221 goto done;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600222
223 priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
matt mooney41e02f02011-05-19 21:36:58 -0700224 if (priv)
225 goto done;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600226
227 priv = stub_priv_pop_from_listhead(&sdev->priv_free);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600228
matt mooney41e02f02011-05-19 21:36:58 -0700229done:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600230 spin_unlock_irqrestore(&sdev->priv_lock, flags);
matt mooney41e02f02011-05-19 21:36:58 -0700231
232 return priv;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600233}
234
235void stub_device_cleanup_urbs(struct stub_device *sdev)
236{
237 struct stub_priv *priv;
matt mooney41e02f02011-05-19 21:36:58 -0700238 struct urb *urb;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600239
Shuah Khan248a2202017-12-18 17:23:37 -0700240 dev_dbg(&sdev->udev->dev, "Stub device cleaning up urbs\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600241
242 while ((priv = stub_priv_pop(sdev))) {
matt mooney41e02f02011-05-19 21:36:58 -0700243 urb = priv->urb;
Shuah Khan248a2202017-12-18 17:23:37 -0700244 dev_dbg(&sdev->udev->dev, "free urb seqnum %lu\n",
245 priv->seqnum);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600246 usb_kill_urb(urb);
247
248 kmem_cache_free(stub_priv_cache, priv);
249
Ilia Mirkin06dde502011-03-13 00:29:12 -0500250 kfree(urb->transfer_buffer);
Michael Grzeschikb3b51412017-05-22 13:02:44 +0200251 urb->transfer_buffer = NULL;
252
Ilia Mirkin06dde502011-03-13 00:29:12 -0500253 kfree(urb->setup_packet);
Michael Grzeschikb3b51412017-05-22 13:02:44 +0200254 urb->setup_packet = NULL;
255
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600256 usb_free_urb(urb);
257 }
258}
259
matt mooney27ed5da2011-05-19 21:36:59 -0700260static int __init usbip_host_init(void)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600261{
262 int ret;
263
Bart Westgeest737912e2012-01-25 13:46:32 -0500264 init_busid_table();
matt mooney7d4de892011-05-19 21:37:00 -0700265
Bart Westgeest737912e2012-01-25 13:46:32 -0500266 stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600267 if (!stub_priv_cache) {
matt mooney41e02f02011-05-19 21:36:58 -0700268 pr_err("kmem_cache_create failed\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600269 return -ENOMEM;
270 }
271
Valentina Maneab7945b72014-01-23 23:12:29 +0200272 ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
navin patidar1583aeb2013-09-10 10:44:07 +0530273 if (ret) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700274 pr_err("usb_register failed %d\n", ret);
matt mooney41e02f02011-05-19 21:36:58 -0700275 goto err_usb_register;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600276 }
277
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600278 ret = driver_create_file(&stub_driver.drvwrap.driver,
279 &driver_attr_match_busid);
navin patidar1583aeb2013-09-10 10:44:07 +0530280 if (ret) {
matt mooney41e02f02011-05-19 21:36:58 -0700281 pr_err("driver_create_file failed\n");
282 goto err_create_file;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600283 }
284
Valentina Maneaa46034c2014-03-08 14:53:33 +0200285 ret = driver_create_file(&stub_driver.drvwrap.driver,
286 &driver_attr_rebind);
287 if (ret) {
288 pr_err("driver_create_file failed\n");
289 goto err_create_file;
290 }
291
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600292 return ret;
matt mooney41e02f02011-05-19 21:36:58 -0700293
294err_create_file:
Valentina Maneab7945b72014-01-23 23:12:29 +0200295 usb_deregister_device_driver(&stub_driver);
matt mooney41e02f02011-05-19 21:36:58 -0700296err_usb_register:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600297 kmem_cache_destroy(stub_priv_cache);
298 return ret;
299}
300
matt mooney27ed5da2011-05-19 21:36:59 -0700301static void __exit usbip_host_exit(void)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600302{
303 driver_remove_file(&stub_driver.drvwrap.driver,
304 &driver_attr_match_busid);
305
Valentina Maneaa46034c2014-03-08 14:53:33 +0200306 driver_remove_file(&stub_driver.drvwrap.driver,
307 &driver_attr_rebind);
308
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600309 /*
310 * deregister() calls stub_disconnect() for all devices. Device
311 * specific data is cleared in stub_disconnect().
312 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200313 usb_deregister_device_driver(&stub_driver);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600314
315 kmem_cache_destroy(stub_priv_cache);
316}
317
matt mooney27ed5da2011-05-19 21:36:59 -0700318module_init(usbip_host_init);
319module_exit(usbip_host_exit);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600320
321MODULE_AUTHOR(DRIVER_AUTHOR);
322MODULE_DESCRIPTION(DRIVER_DESC);
323MODULE_LICENSE("GPL");