Linux-libre 4.14.82-gnu
[librecmc/linux-libre.git] / drivers / usb / 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 #include <linux/string.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23
24 #include "usbip_common.h"
25 #include "stub.h"
26
27 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
28 #define DRIVER_DESC "USB/IP Host Driver"
29
30 struct kmem_cache *stub_priv_cache;
31
32 /*
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
38 static struct bus_id_priv busid_table[MAX_BUSID];
39 static spinlock_t busid_table_lock;
40
41 static void init_busid_table(void)
42 {
43         int i;
44
45         /*
46          * This also sets the bus_table[i].status to
47          * STUB_BUSID_OTHER, which is 0.
48          */
49         memset(busid_table, 0, sizeof(busid_table));
50
51         spin_lock_init(&busid_table_lock);
52
53         for (i = 0; i < MAX_BUSID; i++)
54                 spin_lock_init(&busid_table[i].busid_lock);
55 }
56
57 /*
58  * Find the index of the busid by name.
59  * Must be called with busid_table_lock held.
60  */
61 static int get_busid_idx(const char *busid)
62 {
63         int i;
64         int idx = -1;
65
66         for (i = 0; i < MAX_BUSID; i++) {
67                 spin_lock(&busid_table[i].busid_lock);
68                 if (busid_table[i].name[0])
69                         if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
70                                 idx = i;
71                                 spin_unlock(&busid_table[i].busid_lock);
72                                 break;
73                         }
74                 spin_unlock(&busid_table[i].busid_lock);
75         }
76         return idx;
77 }
78
79 /* Returns holding busid_lock. Should call put_busid_priv() to unlock */
80 struct bus_id_priv *get_busid_priv(const char *busid)
81 {
82         int idx;
83         struct bus_id_priv *bid = NULL;
84
85         spin_lock(&busid_table_lock);
86         idx = get_busid_idx(busid);
87         if (idx >= 0) {
88                 bid = &(busid_table[idx]);
89                 /* get busid_lock before returning */
90                 spin_lock(&bid->busid_lock);
91         }
92         spin_unlock(&busid_table_lock);
93
94         return bid;
95 }
96
97 void put_busid_priv(struct bus_id_priv *bid)
98 {
99         if (bid)
100                 spin_unlock(&bid->busid_lock);
101 }
102
103 static int add_match_busid(char *busid)
104 {
105         int i;
106         int ret = -1;
107
108         spin_lock(&busid_table_lock);
109         /* already registered? */
110         if (get_busid_idx(busid) >= 0) {
111                 ret = 0;
112                 goto out;
113         }
114
115         for (i = 0; i < MAX_BUSID; i++) {
116                 spin_lock(&busid_table[i].busid_lock);
117                 if (!busid_table[i].name[0]) {
118                         strlcpy(busid_table[i].name, busid, BUSID_SIZE);
119                         if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
120                             (busid_table[i].status != STUB_BUSID_REMOV))
121                                 busid_table[i].status = STUB_BUSID_ADDED;
122                         ret = 0;
123                         spin_unlock(&busid_table[i].busid_lock);
124                         break;
125                 }
126                 spin_unlock(&busid_table[i].busid_lock);
127         }
128
129 out:
130         spin_unlock(&busid_table_lock);
131
132         return ret;
133 }
134
135 int del_match_busid(char *busid)
136 {
137         int idx;
138         int ret = -1;
139
140         spin_lock(&busid_table_lock);
141         idx = get_busid_idx(busid);
142         if (idx < 0)
143                 goto out;
144
145         /* found */
146         ret = 0;
147
148         spin_lock(&busid_table[idx].busid_lock);
149
150         if (busid_table[idx].status == STUB_BUSID_OTHER)
151                 memset(busid_table[idx].name, 0, BUSID_SIZE);
152
153         if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
154             (busid_table[idx].status != STUB_BUSID_ADDED))
155                 busid_table[idx].status = STUB_BUSID_REMOV;
156
157         spin_unlock(&busid_table[idx].busid_lock);
158 out:
159         spin_unlock(&busid_table_lock);
160
161         return ret;
162 }
163
164 static ssize_t match_busid_show(struct device_driver *drv, char *buf)
165 {
166         int i;
167         char *out = buf;
168
169         spin_lock(&busid_table_lock);
170         for (i = 0; i < MAX_BUSID; i++) {
171                 spin_lock(&busid_table[i].busid_lock);
172                 if (busid_table[i].name[0])
173                         out += sprintf(out, "%s ", busid_table[i].name);
174                 spin_unlock(&busid_table[i].busid_lock);
175         }
176         spin_unlock(&busid_table_lock);
177         out += sprintf(out, "\n");
178
179         return out - buf;
180 }
181
182 static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
183                                  size_t count)
184 {
185         int len;
186         char busid[BUSID_SIZE];
187
188         if (count < 5)
189                 return -EINVAL;
190
191         /* busid needs to include \0 termination */
192         len = strlcpy(busid, buf + 4, BUSID_SIZE);
193         if (sizeof(busid) <= len)
194                 return -EINVAL;
195
196         if (!strncmp(buf, "add ", 4)) {
197                 if (add_match_busid(busid) < 0)
198                         return -ENOMEM;
199
200                 pr_debug("add busid %s\n", busid);
201                 return count;
202         }
203
204         if (!strncmp(buf, "del ", 4)) {
205                 if (del_match_busid(busid) < 0)
206                         return -ENODEV;
207
208                 pr_debug("del busid %s\n", busid);
209                 return count;
210         }
211
212         return -EINVAL;
213 }
214 static DRIVER_ATTR_RW(match_busid);
215
216 static int do_rebind(char *busid, struct bus_id_priv *busid_priv)
217 {
218         int ret;
219
220         /* device_attach() callers should hold parent lock for USB */
221         if (busid_priv->udev->dev.parent)
222                 device_lock(busid_priv->udev->dev.parent);
223         ret = device_attach(&busid_priv->udev->dev);
224         if (busid_priv->udev->dev.parent)
225                 device_unlock(busid_priv->udev->dev.parent);
226         if (ret < 0) {
227                 dev_err(&busid_priv->udev->dev, "rebind failed\n");
228                 return ret;
229         }
230         return 0;
231 }
232
233 static void stub_device_rebind(void)
234 {
235 #if IS_MODULE(CONFIG_USBIP_HOST)
236         struct bus_id_priv *busid_priv;
237         int i;
238
239         /* update status to STUB_BUSID_OTHER so probe ignores the device */
240         spin_lock(&busid_table_lock);
241         for (i = 0; i < MAX_BUSID; i++) {
242                 if (busid_table[i].name[0] &&
243                     busid_table[i].shutdown_busid) {
244                         busid_priv = &(busid_table[i]);
245                         busid_priv->status = STUB_BUSID_OTHER;
246                 }
247         }
248         spin_unlock(&busid_table_lock);
249
250         /* now run rebind - no need to hold locks. driver files are removed */
251         for (i = 0; i < MAX_BUSID; i++) {
252                 if (busid_table[i].name[0] &&
253                     busid_table[i].shutdown_busid) {
254                         busid_priv = &(busid_table[i]);
255                         do_rebind(busid_table[i].name, busid_priv);
256                 }
257         }
258 #endif
259 }
260
261 static ssize_t rebind_store(struct device_driver *dev, const char *buf,
262                                  size_t count)
263 {
264         int ret;
265         int len;
266         struct bus_id_priv *bid;
267
268         /* buf length should be less that BUSID_SIZE */
269         len = strnlen(buf, BUSID_SIZE);
270
271         if (!(len < BUSID_SIZE))
272                 return -EINVAL;
273
274         bid = get_busid_priv(buf);
275         if (!bid)
276                 return -ENODEV;
277
278         /* mark the device for deletion so probe ignores it during rescan */
279         bid->status = STUB_BUSID_OTHER;
280         /* release the busid lock */
281         put_busid_priv(bid);
282
283         ret = do_rebind((char *) buf, bid);
284         if (ret < 0)
285                 return ret;
286
287         /* delete device from busid_table */
288         del_match_busid((char *) buf);
289
290         return count;
291 }
292
293 static DRIVER_ATTR_WO(rebind);
294
295 static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
296 {
297         struct stub_priv *priv, *tmp;
298
299         list_for_each_entry_safe(priv, tmp, listhead, list) {
300                 list_del(&priv->list);
301                 return priv;
302         }
303
304         return NULL;
305 }
306
307 static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
308 {
309         unsigned long flags;
310         struct stub_priv *priv;
311
312         spin_lock_irqsave(&sdev->priv_lock, flags);
313
314         priv = stub_priv_pop_from_listhead(&sdev->priv_init);
315         if (priv)
316                 goto done;
317
318         priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
319         if (priv)
320                 goto done;
321
322         priv = stub_priv_pop_from_listhead(&sdev->priv_free);
323
324 done:
325         spin_unlock_irqrestore(&sdev->priv_lock, flags);
326
327         return priv;
328 }
329
330 void stub_device_cleanup_urbs(struct stub_device *sdev)
331 {
332         struct stub_priv *priv;
333         struct urb *urb;
334
335         dev_dbg(&sdev->udev->dev, "Stub device cleaning up urbs\n");
336
337         while ((priv = stub_priv_pop(sdev))) {
338                 urb = priv->urb;
339                 dev_dbg(&sdev->udev->dev, "free urb seqnum %lu\n",
340                         priv->seqnum);
341                 usb_kill_urb(urb);
342
343                 kmem_cache_free(stub_priv_cache, priv);
344
345                 kfree(urb->transfer_buffer);
346                 urb->transfer_buffer = NULL;
347
348                 kfree(urb->setup_packet);
349                 urb->setup_packet = NULL;
350
351                 usb_free_urb(urb);
352         }
353 }
354
355 static int __init usbip_host_init(void)
356 {
357         int ret;
358
359         init_busid_table();
360
361         stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
362         if (!stub_priv_cache) {
363                 pr_err("kmem_cache_create failed\n");
364                 return -ENOMEM;
365         }
366
367         ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
368         if (ret) {
369                 pr_err("usb_register failed %d\n", ret);
370                 goto err_usb_register;
371         }
372
373         ret = driver_create_file(&stub_driver.drvwrap.driver,
374                                  &driver_attr_match_busid);
375         if (ret) {
376                 pr_err("driver_create_file failed\n");
377                 goto err_create_file;
378         }
379
380         ret = driver_create_file(&stub_driver.drvwrap.driver,
381                                  &driver_attr_rebind);
382         if (ret) {
383                 pr_err("driver_create_file failed\n");
384                 goto err_create_file;
385         }
386
387         return ret;
388
389 err_create_file:
390         usb_deregister_device_driver(&stub_driver);
391 err_usb_register:
392         kmem_cache_destroy(stub_priv_cache);
393         return ret;
394 }
395
396 static void __exit usbip_host_exit(void)
397 {
398         driver_remove_file(&stub_driver.drvwrap.driver,
399                            &driver_attr_match_busid);
400
401         driver_remove_file(&stub_driver.drvwrap.driver,
402                            &driver_attr_rebind);
403
404         /*
405          * deregister() calls stub_disconnect() for all devices. Device
406          * specific data is cleared in stub_disconnect().
407          */
408         usb_deregister_device_driver(&stub_driver);
409
410         /* initiate scan to attach devices */
411         stub_device_rebind();
412
413         kmem_cache_destroy(stub_priv_cache);
414 }
415
416 module_init(usbip_host_init);
417 module_exit(usbip_host_exit);
418
419 MODULE_AUTHOR(DRIVER_AUTHOR);
420 MODULE_DESCRIPTION(DRIVER_DESC);
421 MODULE_LICENSE("GPL");