blob: 10cbe59d1dc90033cd051f65a1f48d9b8d9fc18b [file] [log] [blame]
Vamsi Krishna5b944712012-06-29 18:36:23 -07001/*
2 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14/* add additional information to our printk's */
15#define pr_fmt(fmt) "%s: " fmt "\n", __func__
16
17#include <linux/kernel.h>
18#include <linux/errno.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/module.h>
22#include <linux/kref.h>
23#include <linux/platform_device.h>
24#include <linux/ratelimit.h>
25#include <linux/uaccess.h>
26#include <linux/usb.h>
27#include <linux/debugfs.h>
28#include <linux/seq_file.h>
29#include <linux/miscdevice.h>
30#include <linux/list.h>
31#include <linux/wait.h>
32
33#define DRIVER_DESC "USB host ks bridge driver"
34#define DRIVER_VERSION "1.0"
35
36struct data_pkt {
37 int n_read;
38 char *buf;
39 size_t len;
40 struct list_head list;
41 void *ctxt;
42};
43
44#define FILE_OPENED BIT(0)
45#define USB_DEV_CONNECTED BIT(1)
46#define NO_RX_REQS 10
47#define NO_BRIDGE_INSTANCES 2
48#define BOOT_BRIDGE_INDEX 0
49#define EFS_BRIDGE_INDEX 1
50#define MAX_DATA_PKT_SIZE 16384
51
52struct ks_bridge {
53 char *name;
54 spinlock_t lock;
55 struct workqueue_struct *wq;
56 struct work_struct to_mdm_work;
57 struct work_struct start_rx_work;
58 struct list_head to_mdm_list;
59 struct list_head to_ks_list;
60 wait_queue_head_t ks_wait_q;
61
62 /* usb specific */
63 struct usb_device *udev;
64 struct usb_interface *ifc;
65 __u8 in_epAddr;
66 __u8 out_epAddr;
67 unsigned int in_pipe;
68 unsigned int out_pipe;
69 struct usb_anchor submitted;
70
71 unsigned long flags;
72 unsigned int alloced_read_pkts;
73
74#define DBG_MSG_LEN 40
75#define DBG_MAX_MSG 500
76 unsigned int dbg_idx;
77 rwlock_t dbg_lock;
78 char (dbgbuf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
79};
80struct ks_bridge *__ksb[NO_BRIDGE_INSTANCES];
81
82/* by default debugging is enabled */
83static unsigned int enable_dbg = 1;
84module_param(enable_dbg, uint, S_IRUGO | S_IWUSR);
85
86static void
87dbg_log_event(struct ks_bridge *ksb, char *event, int d1, int d2)
88{
89 unsigned long flags;
90 unsigned long long t;
91 unsigned long nanosec;
92
93 if (!enable_dbg)
94 return;
95
96 write_lock_irqsave(&ksb->dbg_lock, flags);
97 t = cpu_clock(smp_processor_id());
98 nanosec = do_div(t, 1000000000)/1000;
99 scnprintf(ksb->dbgbuf[ksb->dbg_idx], DBG_MSG_LEN, "%5lu.%06lu:%s:%x:%x",
100 (unsigned long)t, nanosec, event, d1, d2);
101
102 ksb->dbg_idx++;
103 ksb->dbg_idx = ksb->dbg_idx % DBG_MAX_MSG;
104 write_unlock_irqrestore(&ksb->dbg_lock, flags);
105}
106
107static
108struct data_pkt *ksb_alloc_data_pkt(size_t count, gfp_t flags, void *ctxt)
109{
110 struct data_pkt *pkt;
111
112 pkt = kzalloc(sizeof(struct data_pkt), flags);
113 if (!pkt) {
114 pr_err("failed to allocate data packet\n");
115 return ERR_PTR(-ENOMEM);
116 }
117
118 pkt->buf = kmalloc(count, flags);
119 if (!pkt->buf) {
120 pr_err("failed to allocate data buffer\n");
121 kfree(pkt);
122 return ERR_PTR(-ENOMEM);
123 }
124
125 pkt->len = count;
126 INIT_LIST_HEAD(&pkt->list);
127 pkt->ctxt = ctxt;
128
129 return pkt;
130}
131
132static void ksb_free_data_pkt(struct data_pkt *pkt)
133{
134 kfree(pkt->buf);
135 kfree(pkt);
136}
137
138
139static ssize_t ksb_fs_read(struct file *fp, char __user *buf,
140 size_t count, loff_t *pos)
141{
142 int ret;
143 unsigned long flags;
144 struct ks_bridge *ksb = fp->private_data;
145 struct data_pkt *pkt;
146 size_t space, copied;
147
148read_start:
149 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
150 return -ENODEV;
151
152 spin_lock_irqsave(&ksb->lock, flags);
153 if (list_empty(&ksb->to_ks_list)) {
154 spin_unlock_irqrestore(&ksb->lock, flags);
155 ret = wait_event_interruptible(ksb->ks_wait_q,
156 !list_empty(&ksb->to_ks_list) ||
157 !test_bit(USB_DEV_CONNECTED, &ksb->flags));
158 if (ret < 0)
159 return ret;
160
161 goto read_start;
162 }
163
164 space = count;
165 copied = 0;
166 while (!list_empty(&ksb->to_ks_list) && space) {
167 size_t len;
168
169 pkt = list_first_entry(&ksb->to_ks_list, struct data_pkt, list);
170 len = min_t(size_t, space, pkt->len);
171 pkt->n_read += len;
172 spin_unlock_irqrestore(&ksb->lock, flags);
173
174 ret = copy_to_user(buf + copied, pkt->buf, len);
175 if (ret) {
176 pr_err("copy_to_user failed err:%d\n", ret);
177 ksb_free_data_pkt(pkt);
178 ksb->alloced_read_pkts--;
179 return ret;
180 }
181
182 space -= len;
183 copied += len;
184
185 spin_lock_irqsave(&ksb->lock, flags);
186 if (pkt->n_read == pkt->len) {
187 list_del_init(&pkt->list);
188 ksb_free_data_pkt(pkt);
189 ksb->alloced_read_pkts--;
190 }
191 }
192 spin_unlock_irqrestore(&ksb->lock, flags);
193
194 dbg_log_event(ksb, "KS_READ", copied, 0);
195
196 pr_debug("count:%d space:%d copied:%d", count, space, copied);
197
198 return copied;
199}
200
201static void ksb_tx_cb(struct urb *urb)
202{
203 struct data_pkt *pkt = urb->context;
204 struct ks_bridge *ksb = pkt->ctxt;
205
206 dbg_log_event(ksb, "C TX_URB", urb->status, 0);
207 pr_debug("status:%d", urb->status);
208
209 if (ksb->ifc)
210 usb_autopm_put_interface_async(ksb->ifc);
211
212 if (urb->status < 0)
213 pr_err_ratelimited("urb failed with err:%d", urb->status);
214
215 ksb_free_data_pkt(pkt);
216}
217
218static void ksb_tomdm_work(struct work_struct *w)
219{
220 struct ks_bridge *ksb = container_of(w, struct ks_bridge, to_mdm_work);
221 struct data_pkt *pkt;
222 unsigned long flags;
223 struct urb *urb;
224 int ret;
225
226 spin_lock_irqsave(&ksb->lock, flags);
227 while (!list_empty(&ksb->to_mdm_list)
228 && test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
229 pkt = list_first_entry(&ksb->to_mdm_list,
230 struct data_pkt, list);
231 list_del_init(&pkt->list);
232 spin_unlock_irqrestore(&ksb->lock, flags);
233
234 urb = usb_alloc_urb(0, GFP_KERNEL);
235 if (!urb) {
236 pr_err_ratelimited("unable to allocate urb");
237 ksb_free_data_pkt(pkt);
238 return;
239 }
240
241 ret = usb_autopm_get_interface(ksb->ifc);
242 if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
243 pr_err_ratelimited("autopm_get failed:%d", ret);
244 usb_free_urb(urb);
245 ksb_free_data_pkt(pkt);
246 return;
247 }
248 usb_fill_bulk_urb(urb, ksb->udev, ksb->out_pipe,
249 pkt->buf, pkt->len, ksb_tx_cb, pkt);
250 usb_anchor_urb(urb, &ksb->submitted);
251
252 dbg_log_event(ksb, "S TX_URB", pkt->len, 0);
253
254 ret = usb_submit_urb(urb, GFP_KERNEL);
255 if (ret) {
256 pr_err("out urb submission failed");
257 usb_unanchor_urb(urb);
258 usb_free_urb(urb);
259 ksb_free_data_pkt(pkt);
260 usb_autopm_put_interface(ksb->ifc);
261 return;
262 }
263
264 spin_lock_irqsave(&ksb->lock, flags);
265 }
266 spin_unlock_irqrestore(&ksb->lock, flags);
267}
268
269static ssize_t ksb_fs_write(struct file *fp, const char __user *buf,
270 size_t count, loff_t *pos)
271{
272 int ret;
273 struct data_pkt *pkt;
274 unsigned long flags;
275 struct ks_bridge *ksb = fp->private_data;
276
277 pkt = ksb_alloc_data_pkt(count, GFP_KERNEL, ksb);
278 if (IS_ERR(pkt)) {
279 pr_err("unable to allocate data packet");
280 return PTR_ERR(pkt);
281 }
282
283 ret = copy_from_user(pkt->buf, buf, count);
284 if (ret) {
285 pr_err("copy_from_user failed: err:%d", ret);
286 ksb_free_data_pkt(pkt);
287 return ret;
288 }
289
290 spin_lock_irqsave(&ksb->lock, flags);
291 list_add_tail(&pkt->list, &ksb->to_mdm_list);
292 spin_unlock_irqrestore(&ksb->lock, flags);
293
294 queue_work(ksb->wq, &ksb->to_mdm_work);
295
296 return count;
297}
298
299static int efs_fs_open(struct inode *ip, struct file *fp)
300{
301 struct ks_bridge *ksb = __ksb[EFS_BRIDGE_INDEX];
302
303 pr_debug(":%s", ksb->name);
304 dbg_log_event(ksb, "EFS-FS-OPEN", 0, 0);
305
306 if (!ksb) {
307 pr_err("ksb is being removed");
308 return -ENODEV;
309 }
310
311 fp->private_data = ksb;
312 set_bit(FILE_OPENED, &ksb->flags);
313
314 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
315 queue_work(ksb->wq, &ksb->start_rx_work);
316
317 return 0;
318}
319
320static int ksb_fs_open(struct inode *ip, struct file *fp)
321{
322 struct ks_bridge *ksb = __ksb[BOOT_BRIDGE_INDEX];
323
324 pr_debug(":%s", ksb->name);
325 dbg_log_event(ksb, "KS-FS-OPEN", 0, 0);
326
327 if (!ksb) {
328 pr_err("ksb is being removed");
329 return -ENODEV;
330 }
331
332 fp->private_data = ksb;
333 set_bit(FILE_OPENED, &ksb->flags);
334
335 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
336 queue_work(ksb->wq, &ksb->start_rx_work);
337
338 return 0;
339}
340
341static int ksb_fs_release(struct inode *ip, struct file *fp)
342{
343 struct ks_bridge *ksb = fp->private_data;
344
345 pr_debug(":%s", ksb->name);
346 dbg_log_event(ksb, "FS-RELEASE", 0, 0);
347
348 clear_bit(FILE_OPENED, &ksb->flags);
349 fp->private_data = NULL;
350
351 return 0;
352}
353
354static const struct file_operations ksb_fops = {
355 .owner = THIS_MODULE,
356 .read = ksb_fs_read,
357 .write = ksb_fs_write,
358 .open = ksb_fs_open,
359 .release = ksb_fs_release,
360};
361
362static struct miscdevice ksb_fboot_dev = {
363 .minor = MISC_DYNAMIC_MINOR,
364 .name = "ks_bridge",
365 .fops = &ksb_fops,
366};
367
368static const struct file_operations efs_fops = {
369 .owner = THIS_MODULE,
370 .read = ksb_fs_read,
371 .write = ksb_fs_write,
372 .open = efs_fs_open,
373 .release = ksb_fs_release,
374};
375
376static struct miscdevice ksb_efs_dev = {
377 .minor = MISC_DYNAMIC_MINOR,
378 .name = "efs_bridge",
379 .fops = &efs_fops,
380};
381
382static const struct usb_device_id ksb_usb_ids[] = {
383 { USB_DEVICE(0x5c6, 0x9008),
384 .driver_info = (unsigned long)&ksb_fboot_dev, },
385 { USB_DEVICE(0x5c6, 0x9048),
386 .driver_info = (unsigned long)&ksb_efs_dev, },
387 { USB_DEVICE(0x5c6, 0x904C),
388 .driver_info = (unsigned long)&ksb_efs_dev, },
389
390 {} /* terminating entry */
391};
392MODULE_DEVICE_TABLE(usb, ksb_usb_ids);
393
394static void ksb_rx_cb(struct urb *urb);
395static void submit_one_urb(struct ks_bridge *ksb)
396{
397 struct data_pkt *pkt;
398 struct urb *urb;
399 int ret;
400
401 pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_ATOMIC, ksb);
402 if (IS_ERR(pkt)) {
403 pr_err("unable to allocate data pkt");
404 return;
405 }
406
407 urb = usb_alloc_urb(0, GFP_ATOMIC);
408 if (!urb) {
409 pr_err("unable to allocate urb");
410 ksb_free_data_pkt(pkt);
411 return;
412 }
413 ksb->alloced_read_pkts++;
414
415 usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
416 pkt->buf, pkt->len,
417 ksb_rx_cb, pkt);
418 usb_anchor_urb(urb, &ksb->submitted);
419
420 dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
421
422 ret = usb_submit_urb(urb, GFP_ATOMIC);
423 if (ret) {
424 pr_err("in urb submission failed");
425 usb_unanchor_urb(urb);
426 usb_free_urb(urb);
427 ksb_free_data_pkt(pkt);
428 ksb->alloced_read_pkts--;
429 return;
430 }
431
432 usb_free_urb(urb);
433}
434static void ksb_rx_cb(struct urb *urb)
435{
436 struct data_pkt *pkt = urb->context;
437 struct ks_bridge *ksb = pkt->ctxt;
438
439 dbg_log_event(ksb, "C RX_URB", urb->status, urb->actual_length);
440
441 pr_debug("status:%d actual:%d", urb->status, urb->actual_length);
442
443 if (urb->status < 0) {
444 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
445 pr_err_ratelimited("urb failed with err:%d",
446 urb->status);
447 ksb_free_data_pkt(pkt);
448 ksb->alloced_read_pkts--;
449 return;
450 }
451
452 if (urb->actual_length == 0) {
453 ksb_free_data_pkt(pkt);
454 ksb->alloced_read_pkts--;
455 goto resubmit_urb;
456 }
457
458 spin_lock(&ksb->lock);
459 pkt->len = urb->actual_length;
460 list_add_tail(&pkt->list, &ksb->to_ks_list);
461 spin_unlock(&ksb->lock);
462
463 /* wake up read thread */
464 wake_up(&ksb->ks_wait_q);
465
466resubmit_urb:
467 submit_one_urb(ksb);
468
469}
470
471static void ksb_start_rx_work(struct work_struct *w)
472{
473 struct ks_bridge *ksb =
474 container_of(w, struct ks_bridge, start_rx_work);
475 struct data_pkt *pkt;
476 struct urb *urb;
477 int i = 0;
478 int ret;
479
480 for (i = 0; i < NO_RX_REQS; i++) {
481 pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_KERNEL, ksb);
482 if (IS_ERR(pkt)) {
483 pr_err("unable to allocate data pkt");
484 return;
485 }
486
487 urb = usb_alloc_urb(0, GFP_KERNEL);
488 if (!urb) {
489 pr_err("unable to allocate urb");
490 ksb_free_data_pkt(pkt);
491 return;
492 }
493
494 ret = usb_autopm_get_interface(ksb->ifc);
495 if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
496 pr_err_ratelimited("autopm_get failed:%d", ret);
497 usb_free_urb(urb);
498 ksb_free_data_pkt(pkt);
499 return;
500 }
501 ksb->alloced_read_pkts++;
502
503 usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
504 pkt->buf, pkt->len,
505 ksb_rx_cb, pkt);
506 usb_anchor_urb(urb, &ksb->submitted);
507
508 dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
509
510 ret = usb_submit_urb(urb, GFP_KERNEL);
511 if (ret) {
512 pr_err("in urb submission failed");
513 usb_unanchor_urb(urb);
514 usb_free_urb(urb);
515 ksb_free_data_pkt(pkt);
516 ksb->alloced_read_pkts--;
517 usb_autopm_put_interface(ksb->ifc);
518 return;
519 }
520
521 usb_autopm_put_interface_async(ksb->ifc);
522 usb_free_urb(urb);
523 }
524}
525
526static int
527ksb_usb_probe(struct usb_interface *ifc, const struct usb_device_id *id)
528{
529 __u8 ifc_num;
530 struct usb_host_interface *ifc_desc;
531 struct usb_endpoint_descriptor *ep_desc;
532 int i;
533 struct ks_bridge *ksb;
534 struct miscdevice *fs_dev;
535
536 ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
537
538 switch (id->idProduct) {
539 case 0x9008:
540 if (ifc_num != 0)
541 return -ENODEV;
542 ksb = __ksb[BOOT_BRIDGE_INDEX];
543 break;
544 case 0x9048:
545 case 0x904C:
546 if (ifc_num != 2)
547 return -ENODEV;
548 ksb = __ksb[EFS_BRIDGE_INDEX];
549 break;
550 default:
551 return -ENODEV;
552 }
553
554 if (!ksb) {
555 pr_err("ksb is not initialized");
556 return -ENODEV;
557 }
558
559 ksb->udev = usb_get_dev(interface_to_usbdev(ifc));
560 ksb->ifc = ifc;
561 ifc_desc = ifc->cur_altsetting;
562
563 for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
564 ep_desc = &ifc_desc->endpoint[i].desc;
565
566 if (!ksb->in_epAddr && usb_endpoint_is_bulk_in(ep_desc))
567 ksb->in_epAddr = ep_desc->bEndpointAddress;
568
569 if (!ksb->out_epAddr && usb_endpoint_is_bulk_out(ep_desc))
570 ksb->out_epAddr = ep_desc->bEndpointAddress;
571 }
572
573 if (!(ksb->in_epAddr && ksb->out_epAddr)) {
574 pr_err("could not find bulk in and bulk out endpoints");
575 usb_put_dev(ksb->udev);
576 ksb->ifc = NULL;
577 return -ENODEV;
578 }
579
580 ksb->in_pipe = usb_rcvbulkpipe(ksb->udev, ksb->in_epAddr);
581 ksb->out_pipe = usb_sndbulkpipe(ksb->udev, ksb->out_epAddr);
582
583 usb_set_intfdata(ifc, ksb);
584 set_bit(USB_DEV_CONNECTED, &ksb->flags);
585
586 dbg_log_event(ksb, "PID-ATT", id->idProduct, 0);
587
588 fs_dev = (struct miscdevice *)id->driver_info;
589 misc_register(fs_dev);
590
591 usb_enable_autosuspend(ksb->udev);
592
593 pr_debug("usb dev connected");
594
595 return 0;
596}
597
598static int ksb_usb_suspend(struct usb_interface *ifc, pm_message_t message)
599{
600 struct ks_bridge *ksb = usb_get_intfdata(ifc);
601
602 dbg_log_event(ksb, "SUSPEND", 0, 0);
603
604 pr_info("read cnt: %d", ksb->alloced_read_pkts);
605
606 usb_kill_anchored_urbs(&ksb->submitted);
607
608 return 0;
609}
610
611static int ksb_usb_resume(struct usb_interface *ifc)
612{
613 struct ks_bridge *ksb = usb_get_intfdata(ifc);
614
615 dbg_log_event(ksb, "RESUME", 0, 0);
616
617 if (test_bit(FILE_OPENED, &ksb->flags))
618 queue_work(ksb->wq, &ksb->start_rx_work);
619
620 return 0;
621}
622
623static void ksb_usb_disconnect(struct usb_interface *ifc)
624{
625 struct ks_bridge *ksb = usb_get_intfdata(ifc);
626 unsigned long flags;
627 struct data_pkt *pkt;
628
629 dbg_log_event(ksb, "PID-DETACH", 0, 0);
630
631 clear_bit(USB_DEV_CONNECTED, &ksb->flags);
632 wake_up(&ksb->ks_wait_q);
633 cancel_work_sync(&ksb->to_mdm_work);
634
635 usb_kill_anchored_urbs(&ksb->submitted);
636
637 spin_lock_irqsave(&ksb->lock, flags);
638 while (!list_empty(&ksb->to_ks_list)) {
639 pkt = list_first_entry(&ksb->to_ks_list,
640 struct data_pkt, list);
641 list_del_init(&pkt->list);
642 ksb_free_data_pkt(pkt);
643 }
644 while (!list_empty(&ksb->to_mdm_list)) {
645 pkt = list_first_entry(&ksb->to_mdm_list,
646 struct data_pkt, list);
647 list_del_init(&pkt->list);
648 ksb_free_data_pkt(pkt);
649 }
650 spin_unlock_irqrestore(&ksb->lock, flags);
651
652 usb_put_dev(ksb->udev);
653 ksb->ifc = NULL;
654 usb_set_intfdata(ifc, NULL);
655
656 return;
657}
658
659static struct usb_driver ksb_usb_driver = {
660 .name = "ks_bridge",
661 .probe = ksb_usb_probe,
662 .disconnect = ksb_usb_disconnect,
663 .suspend = ksb_usb_suspend,
664 .resume = ksb_usb_resume,
665 .id_table = ksb_usb_ids,
666 .supports_autosuspend = 1,
667};
668
669static ssize_t ksb_debug_show(struct seq_file *s, void *unused)
670{
671 unsigned long flags;
672 struct ks_bridge *ksb = s->private;
673 int i;
674
675 read_lock_irqsave(&ksb->dbg_lock, flags);
676 for (i = 0; i < DBG_MAX_MSG; i++) {
677 if (i == (ksb->dbg_idx - 1))
678 seq_printf(s, "-->%s\n", ksb->dbgbuf[i]);
679 else
680 seq_printf(s, "%s\n", ksb->dbgbuf[i]);
681 }
682 read_unlock_irqrestore(&ksb->dbg_lock, flags);
683
684 return 0;
685}
686
687static int ksb_debug_open(struct inode *ip, struct file *fp)
688{
689 return single_open(fp, ksb_debug_show, ip->i_private);
690
691 return 0;
692}
693
694static const struct file_operations dbg_fops = {
695 .open = ksb_debug_open,
696 .read = seq_read,
697 .llseek = seq_lseek,
698 .release = single_release,
699};
700static struct dentry *dbg_dir;
701static int __init ksb_init(void)
702{
703 struct ks_bridge *ksb;
704 int num_instances = 0;
705 int ret = 0;
706 int i;
707
708 dbg_dir = debugfs_create_dir("ks_bridge", NULL);
709 if (IS_ERR(dbg_dir))
710 pr_err("unable to create debug dir");
711
712 for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
713 ksb = kzalloc(sizeof(struct ks_bridge), GFP_KERNEL);
714 if (!ksb) {
715 pr_err("unable to allocat mem for ks_bridge");
716 return -ENOMEM;
717 }
718 __ksb[i] = ksb;
719
720 ksb->name = kasprintf(GFP_KERNEL, "ks_bridge:%i", i + 1);
721 if (!ksb->name) {
722 pr_info("unable to allocate name");
723 kfree(ksb);
724 ret = -ENOMEM;
725 goto dev_free;
726 }
727
728 spin_lock_init(&ksb->lock);
729 INIT_LIST_HEAD(&ksb->to_mdm_list);
730 INIT_LIST_HEAD(&ksb->to_ks_list);
731 init_waitqueue_head(&ksb->ks_wait_q);
732 ksb->wq = create_singlethread_workqueue(ksb->name);
733 if (!ksb->wq) {
734 pr_err("unable to allocate workqueue");
735 kfree(ksb->name);
736 kfree(ksb);
737 ret = -ENOMEM;
738 goto dev_free;
739 }
740
741 INIT_WORK(&ksb->to_mdm_work, ksb_tomdm_work);
742 INIT_WORK(&ksb->start_rx_work, ksb_start_rx_work);
743 init_usb_anchor(&ksb->submitted);
744
745 ksb->dbg_idx = 0;
746 ksb->dbg_lock = __RW_LOCK_UNLOCKED(lck);
747
748 if (!IS_ERR(dbg_dir))
749 debugfs_create_file(ksb->name, S_IRUGO, dbg_dir,
750 ksb, &dbg_fops);
751
752 num_instances++;
753 }
754
755 ret = usb_register(&ksb_usb_driver);
756 if (ret) {
757 pr_err("unable to register ks bridge driver");
758 goto dev_free;
759 }
760
761 pr_info("init done");
762
763 return 0;
764
765dev_free:
766 if (!IS_ERR(dbg_dir))
767 debugfs_remove_recursive(dbg_dir);
768
769 for (i = 0; i < num_instances; i++) {
770 ksb = __ksb[i];
771
772 destroy_workqueue(ksb->wq);
773 kfree(ksb->name);
774 kfree(ksb);
775 }
776
777 return ret;
778
779}
780
781static void __exit ksb_exit(void)
782{
783 struct ks_bridge *ksb;
784 int i;
785
786 if (!IS_ERR(dbg_dir))
787 debugfs_remove_recursive(dbg_dir);
788
789 usb_deregister(&ksb_usb_driver);
790
791 for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
792 ksb = __ksb[i];
793
794 destroy_workqueue(ksb->wq);
795 kfree(ksb->name);
796 kfree(ksb);
797 }
798}
799
800module_init(ksb_init);
801module_exit(ksb_exit);
802
803MODULE_DESCRIPTION(DRIVER_DESC);
804MODULE_VERSION(DRIVER_VERSION);
805MODULE_LICENSE("GPL v2");