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