blob: e07166534c62d7fa2226fdcd81c2086c2345a233 [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
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
matt mooney7aaacb42011-05-11 22:33:43 -070021#include <linux/string.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040022#include <linux/module.h>
Valentina Maneaa46034c2014-03-08 14:53:33 +020023#include <linux/device.h>
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060024
25#include "usbip_common.h"
26#include "stub.h"
27
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060028#define DRIVER_AUTHOR "Takahiro Hirofuchi"
matt mooney64e62422011-05-11 22:33:44 -070029#define DRIVER_DESC "USB/IP Host Driver"
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060030
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060031struct kmem_cache *stub_priv_cache;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060032/*
33 * busid_tables defines matching busids that usbip can grab. A user can change
34 * dynamically what device is locally used and what device is exported to a
35 * remote host.
36 */
37#define MAX_BUSID 16
Endre Kollaraa5873e2010-07-27 12:39:30 +020038static struct bus_id_priv busid_table[MAX_BUSID];
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060039static spinlock_t busid_table_lock;
40
matt mooneyefad25e2011-05-19 21:36:57 -070041static void init_busid_table(void)
42{
Kurt Kanzenbach521e1e72013-04-04 16:03:10 +020043 /*
44 * This also sets the bus_table[i].status to
45 * STUB_BUSID_OTHER, which is 0.
46 */
matt mooney0a186be2011-05-27 01:49:24 -070047 memset(busid_table, 0, sizeof(busid_table));
matt mooneyefad25e2011-05-19 21:36:57 -070048
49 spin_lock_init(&busid_table_lock);
50}
51
matt mooney41e02f02011-05-19 21:36:58 -070052/*
53 * Find the index of the busid by name.
54 * Must be called with busid_table_lock held.
55 */
56static int get_busid_idx(const char *busid)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060057{
58 int i;
matt mooney41e02f02011-05-19 21:36:58 -070059 int idx = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060060
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060061 for (i = 0; i < MAX_BUSID; i++)
Endre Kollaraa5873e2010-07-27 12:39:30 +020062 if (busid_table[i].name[0])
63 if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
matt mooney41e02f02011-05-19 21:36:58 -070064 idx = i;
65 break;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060066 }
matt mooney41e02f02011-05-19 21:36:58 -070067 return idx;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060068}
69
Endre Kollaraa5873e2010-07-27 12:39:30 +020070struct bus_id_priv *get_busid_priv(const char *busid)
71{
matt mooney41e02f02011-05-19 21:36:58 -070072 int idx;
73 struct bus_id_priv *bid = NULL;
Endre Kollaraa5873e2010-07-27 12:39:30 +020074
75 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -070076 idx = get_busid_idx(busid);
77 if (idx >= 0)
78 bid = &(busid_table[idx]);
Endre Kollaraa5873e2010-07-27 12:39:30 +020079 spin_unlock(&busid_table_lock);
80
matt mooney41e02f02011-05-19 21:36:58 -070081 return bid;
Endre Kollaraa5873e2010-07-27 12:39:30 +020082}
83
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060084static int add_match_busid(char *busid)
85{
86 int i;
matt mooney41e02f02011-05-19 21:36:58 -070087 int ret = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060088
89 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -070090 /* already registered? */
91 if (get_busid_idx(busid) >= 0) {
92 ret = 0;
93 goto out;
94 }
95
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060096 for (i = 0; i < MAX_BUSID; i++)
Endre Kollaraa5873e2010-07-27 12:39:30 +020097 if (!busid_table[i].name[0]) {
Rickard Strandqvist96955ed2014-07-26 16:43:20 +020098 strlcpy(busid_table[i].name, busid, BUSID_SIZE);
Endre Kollaraa5873e2010-07-27 12:39:30 +020099 if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
100 (busid_table[i].status != STUB_BUSID_REMOV))
101 busid_table[i].status = STUB_BUSID_ADDED;
matt mooney41e02f02011-05-19 21:36:58 -0700102 ret = 0;
103 break;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600104 }
matt mooney41e02f02011-05-19 21:36:58 -0700105
106out:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600107 spin_unlock(&busid_table_lock);
108
matt mooney41e02f02011-05-19 21:36:58 -0700109 return ret;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600110}
111
Endre Kollaraa5873e2010-07-27 12:39:30 +0200112int del_match_busid(char *busid)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600113{
matt mooney41e02f02011-05-19 21:36:58 -0700114 int idx;
115 int ret = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600116
117 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -0700118 idx = get_busid_idx(busid);
119 if (idx < 0)
120 goto out;
121
122 /* found */
123 ret = 0;
124
125 if (busid_table[idx].status == STUB_BUSID_OTHER)
126 memset(busid_table[idx].name, 0, BUSID_SIZE);
127
128 if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
129 (busid_table[idx].status != STUB_BUSID_ADDED))
130 busid_table[idx].status = STUB_BUSID_REMOV;
131
132out:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600133 spin_unlock(&busid_table_lock);
134
matt mooney41e02f02011-05-19 21:36:58 -0700135 return ret;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600136}
matt mooney6c033b42011-05-06 03:47:42 -0700137
Greg Kroah-Hartmancc3d53d2017-06-09 11:03:14 +0200138static ssize_t match_busid_show(struct device_driver *drv, char *buf)
Endre Kollaraa5873e2010-07-27 12:39:30 +0200139{
140 int i;
matt mooneyefad25e2011-05-19 21:36:57 -0700141 char *out = buf;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200142
matt mooneyefad25e2011-05-19 21:36:57 -0700143 spin_lock(&busid_table_lock);
144 for (i = 0; i < MAX_BUSID; i++)
145 if (busid_table[i].name[0])
146 out += sprintf(out, "%s ", busid_table[i].name);
147 spin_unlock(&busid_table_lock);
matt mooneyefad25e2011-05-19 21:36:57 -0700148 out += sprintf(out, "\n");
matt mooney41e02f02011-05-19 21:36:58 -0700149
matt mooneyefad25e2011-05-19 21:36:57 -0700150 return out - buf;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200151}
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600152
Greg Kroah-Hartmancc3d53d2017-06-09 11:03:14 +0200153static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
matt mooney6c033b42011-05-06 03:47:42 -0700154 size_t count)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600155{
156 int len;
Kay Sieverse9133972008-10-30 01:59:32 +0100157 char busid[BUSID_SIZE];
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600158
159 if (count < 5)
160 return -EINVAL;
161
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600162 /* busid needs to include \0 termination */
Rickard Strandqvist96955ed2014-07-26 16:43:20 +0200163 len = strlcpy(busid, buf + 4, BUSID_SIZE);
164 if (sizeof(busid) <= len)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600165 return -EINVAL;
166
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600167 if (!strncmp(buf, "add ", 4)) {
Kurt Kanzenbach2183b772013-04-04 16:03:09 +0200168 if (add_match_busid(busid) < 0)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600169 return -ENOMEM;
Kurt Kanzenbach2183b772013-04-04 16:03:09 +0200170
171 pr_debug("add busid %s\n", busid);
172 return count;
matt mooney41e02f02011-05-19 21:36:58 -0700173 }
Kurt Kanzenbach2183b772013-04-04 16:03:09 +0200174
175 if (!strncmp(buf, "del ", 4)) {
176 if (del_match_busid(busid) < 0)
177 return -ENODEV;
178
179 pr_debug("del busid %s\n", busid);
180 return count;
181 }
182
183 return -EINVAL;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600184}
Greg Kroah-Hartmancc3d53d2017-06-09 11:03:14 +0200185static DRIVER_ATTR_RW(match_busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600186
Valentina Maneaa46034c2014-03-08 14:53:33 +0200187static ssize_t rebind_store(struct device_driver *dev, const char *buf,
188 size_t count)
189{
190 int ret;
191 int len;
192 struct bus_id_priv *bid;
193
194 /* buf length should be less that BUSID_SIZE */
195 len = strnlen(buf, BUSID_SIZE);
196
197 if (!(len < BUSID_SIZE))
198 return -EINVAL;
199
200 bid = get_busid_priv(buf);
201 if (!bid)
202 return -ENODEV;
203
204 ret = device_attach(&bid->udev->dev);
205 if (ret < 0) {
206 dev_err(&bid->udev->dev, "rebind failed\n");
207 return ret;
208 }
209
210 return count;
211}
212
213static DRIVER_ATTR_WO(rebind);
214
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600215static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
216{
217 struct stub_priv *priv, *tmp;
218
219 list_for_each_entry_safe(priv, tmp, listhead, list) {
220 list_del(&priv->list);
221 return priv;
222 }
223
224 return NULL;
225}
226
227static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
228{
229 unsigned long flags;
230 struct stub_priv *priv;
231
232 spin_lock_irqsave(&sdev->priv_lock, flags);
233
234 priv = stub_priv_pop_from_listhead(&sdev->priv_init);
matt mooney41e02f02011-05-19 21:36:58 -0700235 if (priv)
236 goto done;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600237
238 priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
matt mooney41e02f02011-05-19 21:36:58 -0700239 if (priv)
240 goto done;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600241
242 priv = stub_priv_pop_from_listhead(&sdev->priv_free);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600243
matt mooney41e02f02011-05-19 21:36:58 -0700244done:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600245 spin_unlock_irqrestore(&sdev->priv_lock, flags);
matt mooney41e02f02011-05-19 21:36:58 -0700246
247 return priv;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600248}
249
250void stub_device_cleanup_urbs(struct stub_device *sdev)
251{
252 struct stub_priv *priv;
matt mooney41e02f02011-05-19 21:36:58 -0700253 struct urb *urb;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600254
matt mooney1a4b6f62011-05-19 16:47:32 -0700255 dev_dbg(&sdev->udev->dev, "free sdev %p\n", sdev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600256
257 while ((priv = stub_priv_pop(sdev))) {
matt mooney41e02f02011-05-19 21:36:58 -0700258 urb = priv->urb;
matt mooney1a4b6f62011-05-19 16:47:32 -0700259 dev_dbg(&sdev->udev->dev, "free urb %p\n", urb);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600260 usb_kill_urb(urb);
261
262 kmem_cache_free(stub_priv_cache, priv);
263
Ilia Mirkin06dde502011-03-13 00:29:12 -0500264 kfree(urb->transfer_buffer);
Michael Grzeschikb3b51412017-05-22 13:02:44 +0200265 urb->transfer_buffer = NULL;
266
Ilia Mirkin06dde502011-03-13 00:29:12 -0500267 kfree(urb->setup_packet);
Michael Grzeschikb3b51412017-05-22 13:02:44 +0200268 urb->setup_packet = NULL;
269
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600270 usb_free_urb(urb);
271 }
272}
273
matt mooney27ed5da2011-05-19 21:36:59 -0700274static int __init usbip_host_init(void)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600275{
276 int ret;
277
Bart Westgeest737912e2012-01-25 13:46:32 -0500278 init_busid_table();
matt mooney7d4de892011-05-19 21:37:00 -0700279
Bart Westgeest737912e2012-01-25 13:46:32 -0500280 stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600281 if (!stub_priv_cache) {
matt mooney41e02f02011-05-19 21:36:58 -0700282 pr_err("kmem_cache_create failed\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600283 return -ENOMEM;
284 }
285
Valentina Maneab7945b72014-01-23 23:12:29 +0200286 ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
navin patidar1583aeb2013-09-10 10:44:07 +0530287 if (ret) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700288 pr_err("usb_register failed %d\n", ret);
matt mooney41e02f02011-05-19 21:36:58 -0700289 goto err_usb_register;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600290 }
291
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600292 ret = driver_create_file(&stub_driver.drvwrap.driver,
293 &driver_attr_match_busid);
navin patidar1583aeb2013-09-10 10:44:07 +0530294 if (ret) {
matt mooney41e02f02011-05-19 21:36:58 -0700295 pr_err("driver_create_file failed\n");
296 goto err_create_file;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600297 }
298
Valentina Maneaa46034c2014-03-08 14:53:33 +0200299 ret = driver_create_file(&stub_driver.drvwrap.driver,
300 &driver_attr_rebind);
301 if (ret) {
302 pr_err("driver_create_file failed\n");
303 goto err_create_file;
304 }
305
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600306 return ret;
matt mooney41e02f02011-05-19 21:36:58 -0700307
308err_create_file:
Valentina Maneab7945b72014-01-23 23:12:29 +0200309 usb_deregister_device_driver(&stub_driver);
matt mooney41e02f02011-05-19 21:36:58 -0700310err_usb_register:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600311 kmem_cache_destroy(stub_priv_cache);
312 return ret;
313}
314
matt mooney27ed5da2011-05-19 21:36:59 -0700315static void __exit usbip_host_exit(void)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600316{
317 driver_remove_file(&stub_driver.drvwrap.driver,
318 &driver_attr_match_busid);
319
Valentina Maneaa46034c2014-03-08 14:53:33 +0200320 driver_remove_file(&stub_driver.drvwrap.driver,
321 &driver_attr_rebind);
322
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600323 /*
324 * deregister() calls stub_disconnect() for all devices. Device
325 * specific data is cleared in stub_disconnect().
326 */
Valentina Maneab7945b72014-01-23 23:12:29 +0200327 usb_deregister_device_driver(&stub_driver);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600328
329 kmem_cache_destroy(stub_priv_cache);
330}
331
matt mooney27ed5da2011-05-19 21:36:59 -0700332module_init(usbip_host_init);
333module_exit(usbip_host_exit);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600334
335MODULE_AUTHOR(DRIVER_AUTHOR);
336MODULE_DESCRIPTION(DRIVER_DESC);
337MODULE_LICENSE("GPL");