blob: 705a9e530a19c378fd16d3db39033bfea7342dce [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>
Paul Gortmaker99c97852011-07-03 15:49:50 -040021#include <linux/module.h>
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060022
23#include "usbip_common.h"
24#include "stub.h"
25
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060026#define DRIVER_AUTHOR "Takahiro Hirofuchi"
matt mooney64e62422011-05-11 22:33:44 -070027#define DRIVER_DESC "USB/IP Host Driver"
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060028
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060029struct kmem_cache *stub_priv_cache;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060030/*
31 * busid_tables defines matching busids that usbip can grab. A user can change
32 * dynamically what device is locally used and what device is exported to a
33 * remote host.
34 */
35#define MAX_BUSID 16
Endre Kollaraa5873e2010-07-27 12:39:30 +020036static struct bus_id_priv busid_table[MAX_BUSID];
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060037static spinlock_t busid_table_lock;
38
matt mooneyefad25e2011-05-19 21:36:57 -070039static void init_busid_table(void)
40{
41 int i;
42
matt mooney0a186be2011-05-27 01:49:24 -070043 memset(busid_table, 0, sizeof(busid_table));
44 for (i = 0; i < MAX_BUSID; i++)
matt mooneyefad25e2011-05-19 21:36:57 -070045 busid_table[i].status = STUB_BUSID_OTHER;
matt mooneyefad25e2011-05-19 21:36:57 -070046
47 spin_lock_init(&busid_table_lock);
48}
49
matt mooney41e02f02011-05-19 21:36:58 -070050/*
51 * Find the index of the busid by name.
52 * Must be called with busid_table_lock held.
53 */
54static int get_busid_idx(const char *busid)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060055{
56 int i;
matt mooney41e02f02011-05-19 21:36:58 -070057 int idx = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060058
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060059 for (i = 0; i < MAX_BUSID; i++)
Endre Kollaraa5873e2010-07-27 12:39:30 +020060 if (busid_table[i].name[0])
61 if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
matt mooney41e02f02011-05-19 21:36:58 -070062 idx = i;
63 break;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060064 }
matt mooney41e02f02011-05-19 21:36:58 -070065 return idx;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060066}
67
Endre Kollaraa5873e2010-07-27 12:39:30 +020068struct bus_id_priv *get_busid_priv(const char *busid)
69{
matt mooney41e02f02011-05-19 21:36:58 -070070 int idx;
71 struct bus_id_priv *bid = NULL;
Endre Kollaraa5873e2010-07-27 12:39:30 +020072
73 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -070074 idx = get_busid_idx(busid);
75 if (idx >= 0)
76 bid = &(busid_table[idx]);
Endre Kollaraa5873e2010-07-27 12:39:30 +020077 spin_unlock(&busid_table_lock);
78
matt mooney41e02f02011-05-19 21:36:58 -070079 return bid;
Endre Kollaraa5873e2010-07-27 12:39:30 +020080}
81
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060082static int add_match_busid(char *busid)
83{
84 int i;
matt mooney41e02f02011-05-19 21:36:58 -070085 int ret = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060086
87 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -070088 /* already registered? */
89 if (get_busid_idx(busid) >= 0) {
90 ret = 0;
91 goto out;
92 }
93
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060094 for (i = 0; i < MAX_BUSID; i++)
Endre Kollaraa5873e2010-07-27 12:39:30 +020095 if (!busid_table[i].name[0]) {
96 strncpy(busid_table[i].name, busid, BUSID_SIZE);
97 if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
98 (busid_table[i].status != STUB_BUSID_REMOV))
99 busid_table[i].status = STUB_BUSID_ADDED;
matt mooney41e02f02011-05-19 21:36:58 -0700100 ret = 0;
101 break;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600102 }
matt mooney41e02f02011-05-19 21:36:58 -0700103
104out:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600105 spin_unlock(&busid_table_lock);
106
matt mooney41e02f02011-05-19 21:36:58 -0700107 return ret;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600108}
109
Endre Kollaraa5873e2010-07-27 12:39:30 +0200110int del_match_busid(char *busid)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600111{
matt mooney41e02f02011-05-19 21:36:58 -0700112 int idx;
113 int ret = -1;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600114
115 spin_lock(&busid_table_lock);
matt mooney41e02f02011-05-19 21:36:58 -0700116 idx = get_busid_idx(busid);
117 if (idx < 0)
118 goto out;
119
120 /* found */
121 ret = 0;
122
123 if (busid_table[idx].status == STUB_BUSID_OTHER)
124 memset(busid_table[idx].name, 0, BUSID_SIZE);
125
126 if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
127 (busid_table[idx].status != STUB_BUSID_ADDED))
128 busid_table[idx].status = STUB_BUSID_REMOV;
129
130out:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600131 spin_unlock(&busid_table_lock);
132
matt mooney41e02f02011-05-19 21:36:58 -0700133 return ret;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600134}
matt mooney6c033b42011-05-06 03:47:42 -0700135
matt mooneyefad25e2011-05-19 21:36:57 -0700136static ssize_t show_match_busid(struct device_driver *drv, char *buf)
Endre Kollaraa5873e2010-07-27 12:39:30 +0200137{
138 int i;
matt mooneyefad25e2011-05-19 21:36:57 -0700139 char *out = buf;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200140
matt mooneyefad25e2011-05-19 21:36:57 -0700141 spin_lock(&busid_table_lock);
142 for (i = 0; i < MAX_BUSID; i++)
143 if (busid_table[i].name[0])
144 out += sprintf(out, "%s ", busid_table[i].name);
145 spin_unlock(&busid_table_lock);
matt mooneyefad25e2011-05-19 21:36:57 -0700146 out += sprintf(out, "\n");
matt mooney41e02f02011-05-19 21:36:58 -0700147
matt mooneyefad25e2011-05-19 21:36:57 -0700148 return out - buf;
Endre Kollaraa5873e2010-07-27 12:39:30 +0200149}
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600150
151static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
matt mooney6c033b42011-05-06 03:47:42 -0700152 size_t count)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600153{
154 int len;
Kay Sieverse9133972008-10-30 01:59:32 +0100155 char busid[BUSID_SIZE];
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600156
157 if (count < 5)
158 return -EINVAL;
159
160 /* strnlen() does not include \0 */
Kay Sieverse9133972008-10-30 01:59:32 +0100161 len = strnlen(buf + 4, BUSID_SIZE);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600162
163 /* busid needs to include \0 termination */
Kay Sieverse9133972008-10-30 01:59:32 +0100164 if (!(len < BUSID_SIZE))
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600165 return -EINVAL;
166
Kay Sieverse9133972008-10-30 01:59:32 +0100167 strncpy(busid, buf + 4, BUSID_SIZE);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600168
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600169 if (!strncmp(buf, "add ", 4)) {
matt mooney41e02f02011-05-19 21:36:58 -0700170 if (add_match_busid(busid) < 0) {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600171 return -ENOMEM;
matt mooney41e02f02011-05-19 21:36:58 -0700172 } else {
matt mooney1a4b6f62011-05-19 16:47:32 -0700173 pr_debug("add busid %s\n", busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600174 return count;
175 }
176 } else if (!strncmp(buf, "del ", 4)) {
matt mooney41e02f02011-05-19 21:36:58 -0700177 if (del_match_busid(busid) < 0) {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600178 return -ENODEV;
matt mooney41e02f02011-05-19 21:36:58 -0700179 } else {
matt mooney1a4b6f62011-05-19 16:47:32 -0700180 pr_debug("del busid %s\n", busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600181 return count;
182 }
matt mooney41e02f02011-05-19 21:36:58 -0700183 } else {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600184 return -EINVAL;
matt mooney41e02f02011-05-19 21:36:58 -0700185 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600186}
matt mooney41e02f02011-05-19 21:36:58 -0700187static DRIVER_ATTR(match_busid, S_IRUSR | S_IWUSR, show_match_busid,
matt mooney6c033b42011-05-06 03:47:42 -0700188 store_match_busid);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600189
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600190static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
191{
192 struct stub_priv *priv, *tmp;
193
194 list_for_each_entry_safe(priv, tmp, listhead, list) {
195 list_del(&priv->list);
196 return priv;
197 }
198
199 return NULL;
200}
201
202static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
203{
204 unsigned long flags;
205 struct stub_priv *priv;
206
207 spin_lock_irqsave(&sdev->priv_lock, flags);
208
209 priv = stub_priv_pop_from_listhead(&sdev->priv_init);
matt mooney41e02f02011-05-19 21:36:58 -0700210 if (priv)
211 goto done;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600212
213 priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
matt mooney41e02f02011-05-19 21:36:58 -0700214 if (priv)
215 goto done;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600216
217 priv = stub_priv_pop_from_listhead(&sdev->priv_free);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600218
matt mooney41e02f02011-05-19 21:36:58 -0700219done:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600220 spin_unlock_irqrestore(&sdev->priv_lock, flags);
matt mooney41e02f02011-05-19 21:36:58 -0700221
222 return priv;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600223}
224
225void stub_device_cleanup_urbs(struct stub_device *sdev)
226{
227 struct stub_priv *priv;
matt mooney41e02f02011-05-19 21:36:58 -0700228 struct urb *urb;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600229
matt mooney1a4b6f62011-05-19 16:47:32 -0700230 dev_dbg(&sdev->udev->dev, "free sdev %p\n", sdev);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600231
232 while ((priv = stub_priv_pop(sdev))) {
matt mooney41e02f02011-05-19 21:36:58 -0700233 urb = priv->urb;
matt mooney1a4b6f62011-05-19 16:47:32 -0700234 dev_dbg(&sdev->udev->dev, "free urb %p\n", urb);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600235 usb_kill_urb(urb);
236
237 kmem_cache_free(stub_priv_cache, priv);
238
Ilia Mirkin06dde502011-03-13 00:29:12 -0500239 kfree(urb->transfer_buffer);
Ilia Mirkin06dde502011-03-13 00:29:12 -0500240 kfree(urb->setup_packet);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600241 usb_free_urb(urb);
242 }
243}
244
matt mooney27ed5da2011-05-19 21:36:59 -0700245static int __init usbip_host_init(void)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600246{
247 int ret;
248
Bart Westgeest737912e2012-01-25 13:46:32 -0500249 init_busid_table();
matt mooney7d4de892011-05-19 21:37:00 -0700250
Bart Westgeest737912e2012-01-25 13:46:32 -0500251 stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600252 if (!stub_priv_cache) {
matt mooney41e02f02011-05-19 21:36:58 -0700253 pr_err("kmem_cache_create failed\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600254 return -ENOMEM;
255 }
256
257 ret = usb_register(&stub_driver);
matt mooney41e02f02011-05-19 21:36:58 -0700258 if (ret < 0) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700259 pr_err("usb_register failed %d\n", ret);
matt mooney41e02f02011-05-19 21:36:58 -0700260 goto err_usb_register;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600261 }
262
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600263 ret = driver_create_file(&stub_driver.drvwrap.driver,
264 &driver_attr_match_busid);
matt mooney41e02f02011-05-19 21:36:58 -0700265 if (ret < 0) {
266 pr_err("driver_create_file failed\n");
267 goto err_create_file;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600268 }
269
matt mooney41e02f02011-05-19 21:36:58 -0700270 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600271 return ret;
matt mooney41e02f02011-05-19 21:36:58 -0700272
273err_create_file:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600274 usb_deregister(&stub_driver);
matt mooney41e02f02011-05-19 21:36:58 -0700275err_usb_register:
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600276 kmem_cache_destroy(stub_priv_cache);
277 return ret;
278}
279
matt mooney27ed5da2011-05-19 21:36:59 -0700280static void __exit usbip_host_exit(void)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600281{
282 driver_remove_file(&stub_driver.drvwrap.driver,
283 &driver_attr_match_busid);
284
285 /*
286 * deregister() calls stub_disconnect() for all devices. Device
287 * specific data is cleared in stub_disconnect().
288 */
289 usb_deregister(&stub_driver);
290
291 kmem_cache_destroy(stub_priv_cache);
292}
293
matt mooney27ed5da2011-05-19 21:36:59 -0700294module_init(usbip_host_init);
295module_exit(usbip_host_exit);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600296
297MODULE_AUTHOR(DRIVER_AUTHOR);
298MODULE_DESCRIPTION(DRIVER_DESC);
299MODULE_LICENSE("GPL");
matt mooney6973c6f2011-05-11 01:54:14 -0700300MODULE_VERSION(USBIP_VERSION);