Commit | Line | Data |
---|---|---|
4d7b5c7f TH |
1 | /* |
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 | ||
5a0e3ad6 | 20 | #include <linux/slab.h> |
4d7b5c7f TH |
21 | |
22 | #include "usbip_common.h" | |
23 | #include "stub.h" | |
24 | ||
25 | /* Version Information */ | |
26 | #define DRIVER_VERSION "1.0" | |
27 | #define DRIVER_AUTHOR "Takahiro Hirofuchi" | |
28 | #define DRIVER_DESC "Stub Driver for USB/IP" | |
29 | ||
30 | /* stub_priv is allocated from stub_priv_cache */ | |
31 | struct kmem_cache *stub_priv_cache; | |
32 | ||
33 | /*-------------------------------------------------------------------------*/ | |
34 | ||
35 | /* Define sysfs entries for the usbip driver */ | |
36 | ||
37 | ||
38 | /* | |
39 | * busid_tables defines matching busids that usbip can grab. A user can change | |
40 | * dynamically what device is locally used and what device is exported to a | |
41 | * remote host. | |
42 | */ | |
43 | #define MAX_BUSID 16 | |
e9133972 KS |
44 | #define BUSID_SIZE 20 |
45 | static char busid_table[MAX_BUSID][BUSID_SIZE]; | |
4d7b5c7f TH |
46 | static spinlock_t busid_table_lock; |
47 | ||
48 | ||
e9133972 | 49 | int match_busid(const char *busid) |
4d7b5c7f TH |
50 | { |
51 | int i; | |
52 | ||
53 | spin_lock(&busid_table_lock); | |
54 | ||
55 | for (i = 0; i < MAX_BUSID; i++) | |
56 | if (busid_table[i][0]) | |
e9133972 | 57 | if (!strncmp(busid_table[i], busid, BUSID_SIZE)) { |
4d7b5c7f TH |
58 | /* already registerd */ |
59 | spin_unlock(&busid_table_lock); | |
60 | return 0; | |
61 | } | |
62 | ||
63 | spin_unlock(&busid_table_lock); | |
64 | ||
65 | return 1; | |
66 | } | |
67 | ||
68 | static ssize_t show_match_busid(struct device_driver *drv, char *buf) | |
69 | { | |
70 | int i; | |
71 | char *out = buf; | |
72 | ||
73 | spin_lock(&busid_table_lock); | |
74 | ||
75 | for (i = 0; i < MAX_BUSID; i++) | |
76 | if (busid_table[i][0]) | |
77 | out += sprintf(out, "%s ", busid_table[i]); | |
78 | ||
79 | spin_unlock(&busid_table_lock); | |
80 | ||
81 | out += sprintf(out, "\n"); | |
82 | ||
83 | return out - buf; | |
84 | } | |
85 | ||
86 | static int add_match_busid(char *busid) | |
87 | { | |
88 | int i; | |
89 | ||
90 | if (!match_busid(busid)) | |
91 | return 0; | |
92 | ||
93 | spin_lock(&busid_table_lock); | |
94 | ||
95 | for (i = 0; i < MAX_BUSID; i++) | |
96 | if (!busid_table[i][0]) { | |
e9133972 | 97 | strncpy(busid_table[i], busid, BUSID_SIZE); |
4d7b5c7f TH |
98 | spin_unlock(&busid_table_lock); |
99 | return 0; | |
100 | } | |
101 | ||
102 | spin_unlock(&busid_table_lock); | |
103 | ||
104 | return -1; | |
105 | } | |
106 | ||
107 | static int del_match_busid(char *busid) | |
108 | { | |
109 | int i; | |
110 | ||
111 | spin_lock(&busid_table_lock); | |
112 | ||
113 | for (i = 0; i < MAX_BUSID; i++) | |
e9133972 | 114 | if (!strncmp(busid_table[i], busid, BUSID_SIZE)) { |
4d7b5c7f | 115 | /* found */ |
e9133972 | 116 | memset(busid_table[i], 0, BUSID_SIZE); |
4d7b5c7f TH |
117 | spin_unlock(&busid_table_lock); |
118 | return 0; | |
119 | } | |
120 | ||
121 | spin_unlock(&busid_table_lock); | |
122 | ||
123 | return -1; | |
124 | } | |
125 | ||
126 | static ssize_t store_match_busid(struct device_driver *dev, const char *buf, | |
127 | size_t count) | |
128 | { | |
129 | int len; | |
e9133972 | 130 | char busid[BUSID_SIZE]; |
4d7b5c7f TH |
131 | |
132 | if (count < 5) | |
133 | return -EINVAL; | |
134 | ||
135 | /* strnlen() does not include \0 */ | |
e9133972 | 136 | len = strnlen(buf + 4, BUSID_SIZE); |
4d7b5c7f TH |
137 | |
138 | /* busid needs to include \0 termination */ | |
e9133972 | 139 | if (!(len < BUSID_SIZE)) |
4d7b5c7f TH |
140 | return -EINVAL; |
141 | ||
e9133972 | 142 | strncpy(busid, buf + 4, BUSID_SIZE); |
4d7b5c7f TH |
143 | |
144 | ||
145 | if (!strncmp(buf, "add ", 4)) { | |
146 | if (add_match_busid(busid) < 0) | |
147 | return -ENOMEM; | |
148 | else { | |
b8868e45 | 149 | usbip_udbg("add busid %s\n", busid); |
4d7b5c7f TH |
150 | return count; |
151 | } | |
152 | } else if (!strncmp(buf, "del ", 4)) { | |
153 | if (del_match_busid(busid) < 0) | |
154 | return -ENODEV; | |
155 | else { | |
b8868e45 | 156 | usbip_udbg("del busid %s\n", busid); |
4d7b5c7f TH |
157 | return count; |
158 | } | |
159 | } else | |
160 | return -EINVAL; | |
161 | } | |
162 | ||
163 | static DRIVER_ATTR(match_busid, S_IRUSR|S_IWUSR, show_match_busid, | |
164 | store_match_busid); | |
165 | ||
166 | ||
167 | ||
168 | /*-------------------------------------------------------------------------*/ | |
169 | ||
170 | /* Cleanup functions used to free private data */ | |
171 | ||
172 | static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead) | |
173 | { | |
174 | struct stub_priv *priv, *tmp; | |
175 | ||
176 | list_for_each_entry_safe(priv, tmp, listhead, list) { | |
177 | list_del(&priv->list); | |
178 | return priv; | |
179 | } | |
180 | ||
181 | return NULL; | |
182 | } | |
183 | ||
184 | static struct stub_priv *stub_priv_pop(struct stub_device *sdev) | |
185 | { | |
186 | unsigned long flags; | |
187 | struct stub_priv *priv; | |
188 | ||
189 | spin_lock_irqsave(&sdev->priv_lock, flags); | |
190 | ||
191 | priv = stub_priv_pop_from_listhead(&sdev->priv_init); | |
192 | if (priv) { | |
193 | spin_unlock_irqrestore(&sdev->priv_lock, flags); | |
194 | return priv; | |
195 | } | |
196 | ||
197 | priv = stub_priv_pop_from_listhead(&sdev->priv_tx); | |
198 | if (priv) { | |
199 | spin_unlock_irqrestore(&sdev->priv_lock, flags); | |
200 | return priv; | |
201 | } | |
202 | ||
203 | priv = stub_priv_pop_from_listhead(&sdev->priv_free); | |
204 | if (priv) { | |
205 | spin_unlock_irqrestore(&sdev->priv_lock, flags); | |
206 | return priv; | |
207 | } | |
208 | ||
209 | spin_unlock_irqrestore(&sdev->priv_lock, flags); | |
210 | return NULL; | |
211 | } | |
212 | ||
213 | void stub_device_cleanup_urbs(struct stub_device *sdev) | |
214 | { | |
215 | struct stub_priv *priv; | |
216 | ||
b8868e45 | 217 | usbip_udbg("free sdev %p\n", sdev); |
4d7b5c7f TH |
218 | |
219 | while ((priv = stub_priv_pop(sdev))) { | |
220 | struct urb *urb = priv->urb; | |
221 | ||
b8868e45 | 222 | usbip_udbg(" free urb %p\n", urb); |
4d7b5c7f TH |
223 | usb_kill_urb(urb); |
224 | ||
225 | kmem_cache_free(stub_priv_cache, priv); | |
226 | ||
227 | if (urb->transfer_buffer != NULL) | |
228 | kfree(urb->transfer_buffer); | |
229 | ||
230 | if (urb->setup_packet != NULL) | |
231 | kfree(urb->setup_packet); | |
232 | ||
233 | usb_free_urb(urb); | |
234 | } | |
235 | } | |
236 | ||
237 | ||
238 | /*-------------------------------------------------------------------------*/ | |
239 | ||
240 | static int __init usb_stub_init(void) | |
241 | { | |
242 | int ret; | |
243 | ||
244 | stub_priv_cache = kmem_cache_create("stub_priv", | |
245 | sizeof(struct stub_priv), 0, | |
246 | SLAB_HWCACHE_ALIGN, NULL); | |
247 | ||
248 | if (!stub_priv_cache) { | |
249 | printk(KERN_ERR KBUILD_MODNAME | |
250 | ": create stub_priv_cache error\n"); | |
251 | return -ENOMEM; | |
252 | } | |
253 | ||
254 | ret = usb_register(&stub_driver); | |
255 | if (ret) { | |
256 | printk(KERN_ERR KBUILD_MODNAME ": usb_register failed %d\n", | |
257 | ret); | |
258 | goto error_usb_register; | |
259 | } | |
260 | ||
261 | printk(KERN_INFO KBUILD_MODNAME ":" | |
262 | DRIVER_DESC ":" DRIVER_VERSION "\n"); | |
263 | ||
264 | memset(busid_table, 0, sizeof(busid_table)); | |
265 | spin_lock_init(&busid_table_lock); | |
266 | ||
267 | ret = driver_create_file(&stub_driver.drvwrap.driver, | |
268 | &driver_attr_match_busid); | |
269 | ||
270 | if (ret) { | |
271 | printk(KERN_ERR KBUILD_MODNAME ": create driver sysfs\n"); | |
272 | goto error_create_file; | |
273 | } | |
274 | ||
275 | return ret; | |
276 | error_create_file: | |
277 | usb_deregister(&stub_driver); | |
278 | error_usb_register: | |
279 | kmem_cache_destroy(stub_priv_cache); | |
280 | return ret; | |
281 | } | |
282 | ||
283 | static void __exit usb_stub_exit(void) | |
284 | { | |
285 | driver_remove_file(&stub_driver.drvwrap.driver, | |
286 | &driver_attr_match_busid); | |
287 | ||
288 | /* | |
289 | * deregister() calls stub_disconnect() for all devices. Device | |
290 | * specific data is cleared in stub_disconnect(). | |
291 | */ | |
292 | usb_deregister(&stub_driver); | |
293 | ||
294 | kmem_cache_destroy(stub_priv_cache); | |
295 | } | |
296 | ||
297 | module_init(usb_stub_init); | |
298 | module_exit(usb_stub_exit); | |
299 | ||
300 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
301 | MODULE_DESCRIPTION(DRIVER_DESC); | |
302 | MODULE_LICENSE("GPL"); |