blob: 00398a694273570e83d34246aa8638a70a3101f6 [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/string.h>
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060021
22#include "usbip_common.h"
23#include "stub.h"
24
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060025#define DRIVER_AUTHOR "Takahiro Hirofuchi"
matt mooney64e62422011-05-11 22:33:44 -070026#define DRIVER_DESC "USB/IP Host Driver"
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060027
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060028struct kmem_cache *stub_priv_cache;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060029/*
30 * busid_tables defines matching busids that usbip can grab. A user can change
31 * dynamically what device is locally used and what device is exported to a
32 * remote host.
33 */
34#define MAX_BUSID 16
Endre Kollaraa5873e2010-07-27 12:39:30 +020035static struct bus_id_priv busid_table[MAX_BUSID];
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060036static spinlock_t busid_table_lock;
37
matt mooneyefad25e2011-05-19 21:36:57 -070038static void init_busid_table(void)
39{
40 int i;
41
42 for (i = 0; i < MAX_BUSID; i++) {
43 memset(busid_table[i].name, 0, BUSID_SIZE);
44 busid_table[i].status = STUB_BUSID_OTHER;
45 busid_table[i].interf_count = 0;
46 busid_table[i].sdev = NULL;
47 busid_table[i].shutdown_busid = 0;
48 }
49
50 spin_lock_init(&busid_table_lock);
51}
52
matt mooney41e02f02011-05-19 21:36:58 -070053/*
54 * Find the index of the busid by name.
55 * Must be called with busid_table_lock held.
56 */
57static int get_busid_idx(const char *busid)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060058{
59 int i;
matt mooney41e02f02011-05-19 21:36:58 -070060 int idx = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060061
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060062 for (i = 0; i < MAX_BUSID; i++)
Endre Kollaraa5873e2010-07-27 12:39:30 +020063 if (busid_table[i].name[0])
64 if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
matt mooney41e02f02011-05-19 21:36:58 -070065 idx = i;
66 break;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060067 }
matt mooney41e02f02011-05-19 21:36:58 -070068 return idx;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060069}
70
Endre Kollaraa5873e2010-07-27 12:39:30 +020071struct bus_id_priv *get_busid_priv(const char *busid)
72{
matt mooney41e02f02011-05-19 21:36:58 -070073 int idx;
74 struct bus_id_priv *bid = NULL;
Endre Kollaraa5873e2010-07-27 12:39:30 +020075
76 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -070077 idx = get_busid_idx(busid);
78 if (idx >= 0)
79 bid = &(busid_table[idx]);
Endre Kollaraa5873e2010-07-27 12:39:30 +020080 spin_unlock(&busid_table_lock);
81
matt mooney41e02f02011-05-19 21:36:58 -070082 return bid;
Endre Kollaraa5873e2010-07-27 12:39:30 +020083}
84
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060085static int add_match_busid(char *busid)
86{
87 int i;
matt mooney41e02f02011-05-19 21:36:58 -070088 int ret = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060089
90 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -070091 /* already registered? */
92 if (get_busid_idx(busid) >= 0) {
93 ret = 0;
94 goto out;
95 }
96
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060097 for (i = 0; i < MAX_BUSID; i++)
Endre Kollaraa5873e2010-07-27 12:39:30 +020098 if (!busid_table[i].name[0]) {
99 strncpy(busid_table[i].name, busid, BUSID_SIZE);
100 if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
101 (busid_table[i].status != STUB_BUSID_REMOV))
102 busid_table[i].status = STUB_BUSID_ADDED;
matt mooney41e02f02011-05-19 21:36:58 -0700103 ret = 0;
104 break;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600105 }
matt mooney41e02f02011-05-19 21:36:58 -0700106
107out:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600108 spin_unlock(&busid_table_lock);
109
matt mooney41e02f02011-05-19 21:36:58 -0700110 return ret;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600111}
112
Endre Kollaraa5873e2010-07-27 12:39:30 +0200113int del_match_busid(char *busid)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600114{
matt mooney41e02f02011-05-19 21:36:58 -0700115 int idx;
116 int ret = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600117
118 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -0700119 idx = get_busid_idx(busid);
120 if (idx < 0)
121 goto out;
122
123 /* found */
124 ret = 0;
125
126 if (busid_table[idx].status == STUB_BUSID_OTHER)
127 memset(busid_table[idx].name, 0, BUSID_SIZE);
128
129 if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
130 (busid_table[idx].status != STUB_BUSID_ADDED))
131 busid_table[idx].status = STUB_BUSID_REMOV;
132
133out:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600134 spin_unlock(&busid_table_lock);
135
matt mooney41e02f02011-05-19 21:36:58 -0700136 return ret;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600137}
matt mooney6c033b42011-05-06 03:47:42 -0700138
matt mooneyefad25e2011-05-19 21:36:57 -0700139static ssize_t show_match_busid(struct device_driver *drv, char *buf)
Endre Kollaraa5873e2010-07-27 12:39:30 +0200140{
141 int i;
matt mooneyefad25e2011-05-19 21:36:57 -0700142 char *out = buf;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200143
matt mooneyefad25e2011-05-19 21:36:57 -0700144 spin_lock(&busid_table_lock);
145 for (i = 0; i < MAX_BUSID; i++)
146 if (busid_table[i].name[0])
147 out += sprintf(out, "%s ", busid_table[i].name);
148 spin_unlock(&busid_table_lock);
matt mooneyefad25e2011-05-19 21:36:57 -0700149 out += sprintf(out, "\n");
matt mooney41e02f02011-05-19 21:36:58 -0700150
matt mooneyefad25e2011-05-19 21:36:57 -0700151 return out - buf;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200152}
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600153
154static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
matt mooney6c033b42011-05-06 03:47:42 -0700155 size_t count)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600156{
157 int len;
Kay Sieverse9133972008-10-30 01:59:32 +0100158 char busid[BUSID_SIZE];
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600159
160 if (count < 5)
161 return -EINVAL;
162
163 /* strnlen() does not include \0 */
Kay Sieverse9133972008-10-30 01:59:32 +0100164 len = strnlen(buf + 4, BUSID_SIZE);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600165
166 /* busid needs to include \0 termination */
Kay Sieverse9133972008-10-30 01:59:32 +0100167 if (!(len < BUSID_SIZE))
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600168 return -EINVAL;
169
Kay Sieverse9133972008-10-30 01:59:32 +0100170 strncpy(busid, buf + 4, BUSID_SIZE);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600171
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600172 if (!strncmp(buf, "add ", 4)) {
matt mooney41e02f02011-05-19 21:36:58 -0700173 if (add_match_busid(busid) < 0) {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600174 return -ENOMEM;
matt mooney41e02f02011-05-19 21:36:58 -0700175 } else {
matt mooney1a4b6f62011-05-19 16:47:32 -0700176 pr_debug("add busid %s\n", busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600177 return count;
178 }
179 } else if (!strncmp(buf, "del ", 4)) {
matt mooney41e02f02011-05-19 21:36:58 -0700180 if (del_match_busid(busid) < 0) {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600181 return -ENODEV;
matt mooney41e02f02011-05-19 21:36:58 -0700182 } else {
matt mooney1a4b6f62011-05-19 16:47:32 -0700183 pr_debug("del busid %s\n", busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600184 return count;
185 }
matt mooney41e02f02011-05-19 21:36:58 -0700186 } else {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600187 return -EINVAL;
matt mooney41e02f02011-05-19 21:36:58 -0700188 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600189}
matt mooney41e02f02011-05-19 21:36:58 -0700190static DRIVER_ATTR(match_busid, S_IRUSR | S_IWUSR, show_match_busid,
matt mooney6c033b42011-05-06 03:47:42 -0700191 store_match_busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600192
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600193static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
194{
195 struct stub_priv *priv, *tmp;
196
197 list_for_each_entry_safe(priv, tmp, listhead, list) {
198 list_del(&priv->list);
199 return priv;
200 }
201
202 return NULL;
203}
204
205static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
206{
207 unsigned long flags;
208 struct stub_priv *priv;
209
210 spin_lock_irqsave(&sdev->priv_lock, flags);
211
212 priv = stub_priv_pop_from_listhead(&sdev->priv_init);
matt mooney41e02f02011-05-19 21:36:58 -0700213 if (priv)
214 goto done;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600215
216 priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
matt mooney41e02f02011-05-19 21:36:58 -0700217 if (priv)
218 goto done;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600219
220 priv = stub_priv_pop_from_listhead(&sdev->priv_free);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600221
matt mooney41e02f02011-05-19 21:36:58 -0700222done:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600223 spin_unlock_irqrestore(&sdev->priv_lock, flags);
matt mooney41e02f02011-05-19 21:36:58 -0700224
225 return priv;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600226}
227
228void stub_device_cleanup_urbs(struct stub_device *sdev)
229{
230 struct stub_priv *priv;
matt mooney41e02f02011-05-19 21:36:58 -0700231 struct urb *urb;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600232
matt mooney1a4b6f62011-05-19 16:47:32 -0700233 dev_dbg(&sdev->udev->dev, "free sdev %p\n", sdev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600234
235 while ((priv = stub_priv_pop(sdev))) {
matt mooney41e02f02011-05-19 21:36:58 -0700236 urb = priv->urb;
matt mooney1a4b6f62011-05-19 16:47:32 -0700237 dev_dbg(&sdev->udev->dev, "free urb %p\n", urb);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600238 usb_kill_urb(urb);
239
240 kmem_cache_free(stub_priv_cache, priv);
241
Ilia Mirkin06dde502011-03-13 00:29:12 -0500242 kfree(urb->transfer_buffer);
Ilia Mirkin06dde502011-03-13 00:29:12 -0500243 kfree(urb->setup_packet);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600244 usb_free_urb(urb);
245 }
246}
247
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600248static int __init usb_stub_init(void)
249{
250 int ret;
251
252 stub_priv_cache = kmem_cache_create("stub_priv",
253 sizeof(struct stub_priv), 0,
254 SLAB_HWCACHE_ALIGN, NULL);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600255 if (!stub_priv_cache) {
matt mooney41e02f02011-05-19 21:36:58 -0700256 pr_err("kmem_cache_create failed\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600257 return -ENOMEM;
258 }
259
260 ret = usb_register(&stub_driver);
matt mooney41e02f02011-05-19 21:36:58 -0700261 if (ret < 0) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700262 pr_err("usb_register failed %d\n", ret);
matt mooney41e02f02011-05-19 21:36:58 -0700263 goto err_usb_register;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600264 }
265
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600266 ret = driver_create_file(&stub_driver.drvwrap.driver,
267 &driver_attr_match_busid);
matt mooney41e02f02011-05-19 21:36:58 -0700268 if (ret < 0) {
269 pr_err("driver_create_file failed\n");
270 goto err_create_file;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600271 }
272
matt mooney41e02f02011-05-19 21:36:58 -0700273 init_busid_table();
274 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600275 return ret;
matt mooney41e02f02011-05-19 21:36:58 -0700276
277err_create_file:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600278 usb_deregister(&stub_driver);
matt mooney41e02f02011-05-19 21:36:58 -0700279err_usb_register:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600280 kmem_cache_destroy(stub_priv_cache);
281 return ret;
282}
283
284static void __exit usb_stub_exit(void)
285{
286 driver_remove_file(&stub_driver.drvwrap.driver,
287 &driver_attr_match_busid);
288
289 /*
290 * deregister() calls stub_disconnect() for all devices. Device
291 * specific data is cleared in stub_disconnect().
292 */
293 usb_deregister(&stub_driver);
294
295 kmem_cache_destroy(stub_priv_cache);
296}
297
298module_init(usb_stub_init);
299module_exit(usb_stub_exit);
300
301MODULE_AUTHOR(DRIVER_AUTHOR);
302MODULE_DESCRIPTION(DRIVER_DESC);
303MODULE_LICENSE("GPL");
matt mooney6973c6f2011-05-11 01:54:14 -0700304MODULE_VERSION(USBIP_VERSION);