OSDN Git Service

cnss2: Add support for genoa sdio
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / usb / misc / ks_bridge.c
1 /*
2  * Copyright (c) 2012-2014, 2017-2019, Linux Foundation. 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/cdev.h>
30 #include <linux/list.h>
31 #include <linux/wait.h>
32 #include <linux/poll.h>
33 #include <linux/kobject.h>
34
35 #define DRIVER_DESC     "USB host ks bridge driver"
36
37 enum bus_id {
38         BUS_HSIC,
39         BUS_USB,
40         BUS_UNDEF,
41 };
42
43 #define BUSNAME_LEN     20
44
45 static enum bus_id str_to_busid(const char *name)
46 {
47         if (!strncasecmp("msm_hsic_host", name, BUSNAME_LEN))
48                 return BUS_HSIC;
49         if (!strncasecmp("msm_ehci_host.0", name, BUSNAME_LEN))
50                 return BUS_USB;
51         if (!strncasecmp("xhci-hcd.0.auto", name, BUSNAME_LEN) ||
52             !strncasecmp("xhci-hcd.1.auto", name, BUSNAME_LEN))
53                 return BUS_USB;
54
55         return BUS_UNDEF;
56 }
57
58 struct data_pkt {
59         int                     n_read;
60         char                    *buf;
61         size_t                  len;
62         struct list_head        list;
63         void                    *ctxt;
64 };
65
66 #define FILE_OPENED             BIT(0)
67 #define USB_DEV_CONNECTED       BIT(1)
68 #define NO_RX_REQS              10
69 #define NO_BRIDGE_INSTANCES     4
70 #define EFS_HSIC_BRIDGE_INDEX   2
71 #define EFS_USB_BRIDGE_INDEX    3
72 #define MAX_DATA_PKT_SIZE       16384
73 #define PENDING_URB_TIMEOUT     10
74
75 struct ksb_dev_info {
76         const char *name;
77 };
78
79 struct ks_bridge {
80         char                    *name;
81         spinlock_t              lock;
82         struct workqueue_struct *wq;
83         struct work_struct      to_mdm_work;
84         struct work_struct      start_rx_work;
85         struct list_head        to_mdm_list;
86         struct list_head        to_ks_list;
87         wait_queue_head_t       ks_wait_q;
88         wait_queue_head_t       pending_urb_wait;
89         atomic_t                tx_pending_cnt;
90         atomic_t                rx_pending_cnt;
91
92         struct ksb_dev_info     id_info;
93
94         /* cdev interface */
95         dev_t                   cdev_start_no;
96         struct cdev             cdev;
97         struct class            *class;
98         struct device           *device;
99
100         /* usb specific */
101         struct usb_device       *udev;
102         struct usb_interface    *ifc;
103         __u8                    in_epAddr;
104         __u8                    out_epAddr;
105         unsigned int            in_pipe;
106         unsigned int            out_pipe;
107         struct usb_anchor       submitted;
108
109         unsigned long           flags;
110
111         /* to handle INT IN ep */
112         unsigned int            period;
113
114 #define DBG_MSG_LEN   40
115 #define DBG_MAX_MSG   500
116         unsigned int    dbg_idx;
117         rwlock_t        dbg_lock;
118
119         char     (dbgbuf[DBG_MAX_MSG])[DBG_MSG_LEN];   /* buffer */
120 };
121
122 struct ks_bridge *__ksb[NO_BRIDGE_INSTANCES];
123
124 /* by default debugging is enabled */
125 static unsigned int enable_dbg = 1;
126 module_param(enable_dbg, uint, S_IRUGO | S_IWUSR);
127
128 static void
129 dbg_log_event(struct ks_bridge *ksb, char *event, int d1, int d2)
130 {
131         unsigned long flags;
132         unsigned long long t;
133         unsigned long nanosec;
134
135         if (!enable_dbg)
136                 return;
137
138         write_lock_irqsave(&ksb->dbg_lock, flags);
139         t = cpu_clock(smp_processor_id());
140         nanosec = do_div(t, 1000000000)/1000;
141         scnprintf(ksb->dbgbuf[ksb->dbg_idx], DBG_MSG_LEN, "%5lu.%06lu:%s:%x:%x",
142                         (unsigned long)t, nanosec, event, d1, d2);
143
144         ksb->dbg_idx++;
145         ksb->dbg_idx = ksb->dbg_idx % DBG_MAX_MSG;
146         write_unlock_irqrestore(&ksb->dbg_lock, flags);
147 }
148
149 static
150 struct data_pkt *ksb_alloc_data_pkt(size_t count, gfp_t flags, void *ctxt)
151 {
152         struct data_pkt *pkt;
153
154         pkt = kzalloc(sizeof(struct data_pkt), flags);
155         if (!pkt)
156                 return ERR_PTR(-ENOMEM);
157
158         pkt->buf = kmalloc(count, flags);
159         if (!pkt->buf) {
160                 kfree(pkt);
161                 return ERR_PTR(-ENOMEM);
162         }
163
164         pkt->len = count;
165         INIT_LIST_HEAD(&pkt->list);
166         pkt->ctxt = ctxt;
167
168         return pkt;
169 }
170
171 static void ksb_free_data_pkt(struct data_pkt *pkt)
172 {
173         kfree(pkt->buf);
174         kfree(pkt);
175 }
176
177
178 static void
179 submit_one_urb(struct ks_bridge *ksb, gfp_t flags, struct data_pkt *pkt);
180 static ssize_t ksb_fs_read(struct file *fp, char __user *buf,
181                                 size_t count, loff_t *pos)
182 {
183         int ret;
184         unsigned long flags;
185         struct ks_bridge *ksb = fp->private_data;
186         struct data_pkt *pkt = NULL;
187         size_t space, copied;
188
189 read_start:
190         if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
191                 return -ENODEV;
192
193         spin_lock_irqsave(&ksb->lock, flags);
194         if (list_empty(&ksb->to_ks_list)) {
195                 spin_unlock_irqrestore(&ksb->lock, flags);
196                 ret = wait_event_interruptible(ksb->ks_wait_q,
197                                 !list_empty(&ksb->to_ks_list) ||
198                                 !test_bit(USB_DEV_CONNECTED, &ksb->flags));
199                 if (ret < 0)
200                         return ret;
201
202                 goto read_start;
203         }
204
205         space = count;
206         copied = 0;
207         while (!list_empty(&ksb->to_ks_list) && space &&
208                         test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
209                 size_t len;
210
211                 pkt = list_first_entry(&ksb->to_ks_list, struct data_pkt, list);
212                 list_del_init(&pkt->list);
213                 len = min_t(size_t, space, pkt->len - pkt->n_read);
214                 spin_unlock_irqrestore(&ksb->lock, flags);
215
216                 ret = copy_to_user(buf + copied, pkt->buf + pkt->n_read, len);
217                 if (ret) {
218                         dev_err(ksb->device,
219                                         "copy_to_user failed err:%d\n", ret);
220                         ksb_free_data_pkt(pkt);
221                         return -EFAULT;
222                 }
223
224                 pkt->n_read += len;
225                 space -= len;
226                 copied += len;
227
228                 if (pkt->n_read == pkt->len) {
229                         /*
230                          * re-init the packet and queue it
231                          * for more data.
232                          */
233                         pkt->n_read = 0;
234                         pkt->len = MAX_DATA_PKT_SIZE;
235                         submit_one_urb(ksb, GFP_KERNEL, pkt);
236                         pkt = NULL;
237                 }
238                 spin_lock_irqsave(&ksb->lock, flags);
239         }
240
241         /* put the partial packet back in the list */
242         if (!space && pkt && pkt->n_read != pkt->len) {
243                 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
244                         list_add(&pkt->list, &ksb->to_ks_list);
245                 else
246                         ksb_free_data_pkt(pkt);
247         }
248         spin_unlock_irqrestore(&ksb->lock, flags);
249
250         dbg_log_event(ksb, "KS_READ", copied, 0);
251
252         dev_dbg(ksb->device, "count:%zu space:%zu copied:%zu", count,
253                         space, copied);
254
255         return copied;
256 }
257
258 static void ksb_tx_cb(struct urb *urb)
259 {
260         struct data_pkt *pkt = urb->context;
261         struct ks_bridge *ksb = pkt->ctxt;
262
263         dbg_log_event(ksb, "C TX_URB", urb->status, 0);
264         dev_dbg(&ksb->udev->dev, "status:%d", urb->status);
265
266         if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
267                 usb_autopm_put_interface_async(ksb->ifc);
268
269         if (urb->status < 0)
270                 pr_err_ratelimited("%s: urb failed with err:%d",
271                                 ksb->id_info.name, urb->status);
272
273         ksb_free_data_pkt(pkt);
274
275         atomic_dec(&ksb->tx_pending_cnt);
276         wake_up(&ksb->pending_urb_wait);
277 }
278
279 static void ksb_tomdm_work(struct work_struct *w)
280 {
281         struct ks_bridge *ksb = container_of(w, struct ks_bridge, to_mdm_work);
282         struct data_pkt *pkt;
283         unsigned long flags;
284         struct urb *urb;
285         int ret;
286
287         spin_lock_irqsave(&ksb->lock, flags);
288         while (!list_empty(&ksb->to_mdm_list)
289                         && test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
290                 pkt = list_first_entry(&ksb->to_mdm_list,
291                                 struct data_pkt, list);
292                 list_del_init(&pkt->list);
293                 spin_unlock_irqrestore(&ksb->lock, flags);
294
295                 urb = usb_alloc_urb(0, GFP_KERNEL);
296                 if (!urb) {
297                         dbg_log_event(ksb, "TX_URB_MEM_FAIL", -ENOMEM, 0);
298                         pr_err_ratelimited("%s: unable to allocate urb",
299                                         ksb->id_info.name);
300                         ksb_free_data_pkt(pkt);
301                         return;
302                 }
303
304                 ret = usb_autopm_get_interface(ksb->ifc);
305                 if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
306                         dbg_log_event(ksb, "TX_URB_AUTOPM_FAIL", ret, 0);
307                         pr_err_ratelimited("%s: autopm_get failed:%d",
308                                         ksb->id_info.name, ret);
309                         usb_free_urb(urb);
310                         ksb_free_data_pkt(pkt);
311                         return;
312                 }
313                 usb_fill_bulk_urb(urb, ksb->udev, ksb->out_pipe,
314                                 pkt->buf, pkt->len, ksb_tx_cb, pkt);
315                 usb_anchor_urb(urb, &ksb->submitted);
316
317                 dbg_log_event(ksb, "S TX_URB", pkt->len, 0);
318
319                 atomic_inc(&ksb->tx_pending_cnt);
320                 ret = usb_submit_urb(urb, GFP_KERNEL);
321                 if (ret) {
322                         dev_err(&ksb->udev->dev, "out urb submission failed");
323                         usb_unanchor_urb(urb);
324                         usb_free_urb(urb);
325                         ksb_free_data_pkt(pkt);
326                         usb_autopm_put_interface(ksb->ifc);
327                         atomic_dec(&ksb->tx_pending_cnt);
328                         wake_up(&ksb->pending_urb_wait);
329                         return;
330                 }
331
332                 usb_free_urb(urb);
333
334                 spin_lock_irqsave(&ksb->lock, flags);
335         }
336         spin_unlock_irqrestore(&ksb->lock, flags);
337 }
338
339 static ssize_t ksb_fs_write(struct file *fp, const char __user *buf,
340                                  size_t count, loff_t *pos)
341 {
342         int                     ret;
343         struct data_pkt         *pkt;
344         unsigned long           flags;
345         struct ks_bridge        *ksb = fp->private_data;
346
347         if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
348                 return -ENODEV;
349
350         if (count > MAX_DATA_PKT_SIZE)
351                 count = MAX_DATA_PKT_SIZE;
352
353         pkt = ksb_alloc_data_pkt(count, GFP_KERNEL, ksb);
354         if (IS_ERR(pkt)) {
355                 dev_err(ksb->device,
356                                 "unable to allocate data packet");
357                 return PTR_ERR(pkt);
358         }
359
360         ret = copy_from_user(pkt->buf, buf, count);
361         if (ret) {
362                 dev_err(ksb->device,
363                                 "copy_from_user failed: err:%d", ret);
364                 ksb_free_data_pkt(pkt);
365                 return ret;
366         }
367
368         spin_lock_irqsave(&ksb->lock, flags);
369         list_add_tail(&pkt->list, &ksb->to_mdm_list);
370         spin_unlock_irqrestore(&ksb->lock, flags);
371
372         queue_work(ksb->wq, &ksb->to_mdm_work);
373
374         dbg_log_event(ksb, "KS_WRITE", count, 0);
375
376         return count;
377 }
378
379 static int ksb_fs_open(struct inode *ip, struct file *fp)
380 {
381         struct ks_bridge *ksb =
382                         container_of(ip->i_cdev, struct ks_bridge, cdev);
383
384         if (IS_ERR(ksb)) {
385                 pr_err("ksb device not found");
386                 return -ENODEV;
387         }
388
389         dev_dbg(ksb->device, ":%s", ksb->id_info.name);
390         dbg_log_event(ksb, "FS-OPEN", 0, 0);
391
392         fp->private_data = ksb;
393         set_bit(FILE_OPENED, &ksb->flags);
394
395         if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
396                 queue_work(ksb->wq, &ksb->start_rx_work);
397
398         return 0;
399 }
400
401 static unsigned int ksb_fs_poll(struct file *file, poll_table *wait)
402 {
403         struct ks_bridge        *ksb = file->private_data;
404         unsigned long           flags;
405         int                     ret = 0;
406
407         if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
408                 return POLLERR;
409
410         poll_wait(file, &ksb->ks_wait_q, wait);
411         if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
412                 return POLLERR;
413
414         spin_lock_irqsave(&ksb->lock, flags);
415         if (!list_empty(&ksb->to_ks_list))
416                 ret = POLLIN | POLLRDNORM;
417         spin_unlock_irqrestore(&ksb->lock, flags);
418
419         return ret;
420 }
421
422 static int ksb_fs_release(struct inode *ip, struct file *fp)
423 {
424         struct ks_bridge        *ksb = fp->private_data;
425
426         if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
427                 dev_dbg(ksb->device, ":%s", ksb->id_info.name);
428         dbg_log_event(ksb, "FS-RELEASE", 0, 0);
429
430         clear_bit(FILE_OPENED, &ksb->flags);
431         fp->private_data = NULL;
432
433         return 0;
434 }
435
436 static const struct file_operations ksb_fops = {
437         .owner = THIS_MODULE,
438         .read = ksb_fs_read,
439         .write = ksb_fs_write,
440         .open = ksb_fs_open,
441         .release = ksb_fs_release,
442         .poll = ksb_fs_poll,
443 };
444
445 static struct ksb_dev_info ksb_fboot_dev[] = {
446         {
447                 .name = "ks_hsic_bridge",
448         },
449         {
450                 .name = "ks_usb_bridge",
451         },
452 };
453
454 static struct ksb_dev_info ksb_efs_hsic_dev = {
455         .name = "efs_hsic_bridge",
456 };
457
458 static struct ksb_dev_info ksb_efs_usb_dev = {
459         .name = "efs_usb_bridge",
460 };
461 static const struct usb_device_id ksb_usb_ids[] = {
462         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x9008, 0),
463         .driver_info = (unsigned long)&ksb_fboot_dev, },
464         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x9025, 0), },
465         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x9091, 0), },
466         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x901D, 0), },
467         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x901F, 0), },
468         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x900E, 0), },
469         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x9900, 0), },
470         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x9901, 0), },
471         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x9902, 3),
472         .driver_info = (unsigned long)&ksb_fboot_dev, },
473         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x9903, 5),
474         .driver_info = (unsigned long)&ksb_fboot_dev, },
475         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x9048, 2),
476         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
477         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x904C, 2),
478         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
479         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x9075, 2),
480         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
481         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x9079, 2),
482         .driver_info = (unsigned long)&ksb_efs_usb_dev, },
483         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x908A, 2),
484         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
485         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x908E, 3),
486         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
487         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x909C, 2),
488         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
489         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x909D, 2),
490         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
491         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x909E, 3),
492         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
493         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x909F, 2),
494         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
495         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x90A0, 2),
496         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
497         { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x90A4, 3),
498         .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
499
500         {} /* terminating entry */
501 };
502 MODULE_DEVICE_TABLE(usb, ksb_usb_ids);
503
504 static void ksb_rx_cb(struct urb *urb);
505 static void
506 submit_one_urb(struct ks_bridge *ksb, gfp_t flags, struct data_pkt *pkt)
507 {
508         struct urb *urb;
509         int ret;
510
511         urb = usb_alloc_urb(0, flags);
512         if (!urb) {
513                 dev_err(&ksb->udev->dev, "unable to allocate urb");
514                 ksb_free_data_pkt(pkt);
515                 return;
516         }
517
518         if (ksb->period)
519                 usb_fill_int_urb(urb, ksb->udev, ksb->in_pipe,
520                                  pkt->buf, pkt->len,
521                                  ksb_rx_cb, pkt, ksb->period);
522         else
523                 usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
524                                 pkt->buf, pkt->len,
525                                 ksb_rx_cb, pkt);
526
527         usb_anchor_urb(urb, &ksb->submitted);
528
529         if (!test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
530                 usb_unanchor_urb(urb);
531                 usb_free_urb(urb);
532                 ksb_free_data_pkt(pkt);
533                 return;
534         }
535
536         atomic_inc(&ksb->rx_pending_cnt);
537         ret = usb_submit_urb(urb, flags);
538         if (ret) {
539                 dev_err(&ksb->udev->dev, "in urb submission failed");
540                 usb_unanchor_urb(urb);
541                 usb_free_urb(urb);
542                 ksb_free_data_pkt(pkt);
543                 atomic_dec(&ksb->rx_pending_cnt);
544                 wake_up(&ksb->pending_urb_wait);
545                 return;
546         }
547
548         dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
549
550         usb_free_urb(urb);
551 }
552 static void ksb_rx_cb(struct urb *urb)
553 {
554         struct data_pkt *pkt = urb->context;
555         struct ks_bridge *ksb = pkt->ctxt;
556         bool wakeup = true;
557
558         dbg_log_event(ksb, "C RX_URB", urb->status, urb->actual_length);
559
560         dev_dbg(&ksb->udev->dev, "status:%d actual:%d", urb->status,
561                         urb->actual_length);
562
563         /*non zero len of data received while unlinking urb*/
564         if (urb->status == -ENOENT && (urb->actual_length > 0)) {
565                 /*
566                  * If we wakeup the reader process now, it may
567                  * queue the URB before its reject flag gets
568                  * cleared.
569                  */
570                 wakeup = false;
571                 goto add_to_list;
572         }
573
574         if (urb->status < 0) {
575                 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT
576                                 && urb->status != -EPROTO)
577                         pr_err_ratelimited("%s: urb failed with err:%d",
578                                         ksb->id_info.name, urb->status);
579
580                 if (!urb->actual_length) {
581                         ksb_free_data_pkt(pkt);
582                         goto done;
583                 }
584         }
585
586         usb_mark_last_busy(ksb->udev);
587
588         if (urb->actual_length == 0) {
589                 submit_one_urb(ksb, GFP_ATOMIC, pkt);
590                 goto done;
591         }
592
593 add_to_list:
594         spin_lock(&ksb->lock);
595         pkt->len = urb->actual_length;
596         list_add_tail(&pkt->list, &ksb->to_ks_list);
597         spin_unlock(&ksb->lock);
598         /* wake up read thread */
599         if (wakeup)
600                 wake_up(&ksb->ks_wait_q);
601 done:
602         atomic_dec(&ksb->rx_pending_cnt);
603         wake_up(&ksb->pending_urb_wait);
604 }
605
606 static void ksb_start_rx_work(struct work_struct *w)
607 {
608         struct ks_bridge *ksb =
609                         container_of(w, struct ks_bridge, start_rx_work);
610         struct data_pkt *pkt;
611         struct urb *urb;
612         int i = 0;
613         int ret;
614         bool put = true;
615
616         ret = usb_autopm_get_interface(ksb->ifc);
617         if (ret < 0) {
618                 if (ret != -EAGAIN && ret != -EACCES) {
619                         pr_err_ratelimited("%s: autopm_get failed:%d",
620                                         ksb->id_info.name, ret);
621                         return;
622                 }
623                 put = false;
624         }
625         for (i = 0; i < NO_RX_REQS; i++) {
626
627                 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
628                         break;
629
630                 pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_KERNEL, ksb);
631                 if (IS_ERR(pkt)) {
632                         dev_err(&ksb->udev->dev, "unable to allocate data pkt");
633                         break;
634                 }
635
636                 urb = usb_alloc_urb(0, GFP_KERNEL);
637                 if (!urb) {
638                         dev_err(&ksb->udev->dev, "unable to allocate urb");
639                         ksb_free_data_pkt(pkt);
640                         break;
641                 }
642
643                 if (ksb->period)
644                         usb_fill_int_urb(urb, ksb->udev, ksb->in_pipe,
645                                         pkt->buf, pkt->len,
646                                         ksb_rx_cb, pkt, ksb->period);
647                 else
648                         usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
649                                         pkt->buf, pkt->len,
650                                         ksb_rx_cb, pkt);
651
652                 usb_anchor_urb(urb, &ksb->submitted);
653
654                 dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
655
656                 atomic_inc(&ksb->rx_pending_cnt);
657                 ret = usb_submit_urb(urb, GFP_KERNEL);
658                 if (ret) {
659                         dev_err(&ksb->udev->dev, "in urb submission failed");
660                         usb_unanchor_urb(urb);
661                         usb_free_urb(urb);
662                         ksb_free_data_pkt(pkt);
663                         atomic_dec(&ksb->rx_pending_cnt);
664                         wake_up(&ksb->pending_urb_wait);
665                         break;
666                 }
667
668                 usb_free_urb(urb);
669         }
670         if (put)
671                 usb_autopm_put_interface_async(ksb->ifc);
672 }
673
674 static void ks_bridge_notify_status(struct kobject *kobj,
675                                                 const struct usb_device_id *id)
676 {
677         char product_info[32];
678         char *envp[2] = { product_info, NULL };
679
680         snprintf(product_info, sizeof(product_info), "PRODUCT=%x/%x/%x",
681                         id->idVendor, id->idProduct, id->bDeviceProtocol);
682         kobject_uevent_env(kobj, KOBJ_ONLINE, envp);
683 }
684
685 static int
686 ksb_usb_probe(struct usb_interface *ifc, const struct usb_device_id *id)
687 {
688         __u8                            ifc_num, ifc_count, ksb_port_num;
689         struct usb_host_interface       *ifc_desc;
690         struct usb_endpoint_descriptor  *ep_desc;
691         int                             i;
692         struct ks_bridge                *ksb;
693         unsigned long                   flags;
694         struct data_pkt                 *pkt;
695         struct ksb_dev_info             *mdev, *fbdev;
696         struct usb_device               *udev;
697         unsigned int                    bus_id;
698         int                             ret;
699         bool                            free_mdev = false;
700
701         ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
702
703         udev = interface_to_usbdev(ifc);
704         ifc_count = udev->actconfig->desc.bNumInterfaces;
705         fbdev = mdev = (struct ksb_dev_info *)id->driver_info;
706
707         bus_id = str_to_busid(udev->bus->bus_name);
708         if (bus_id == BUS_UNDEF) {
709                 dev_err(&udev->dev, "unknown usb bus %s, probe failed\n",
710                                 udev->bus->bus_name);
711                 return -ENODEV;
712         }
713
714         switch (id->idProduct) {
715         case 0x900E:
716         case 0x9025:
717         case 0x9091:
718         case 0x901D:
719         case 0x901F:
720                 /* 1-1 mapping between ksb and udev port which starts with 1 */
721                 ksb_port_num = udev->portnum - 1;
722                 dev_dbg(&udev->dev, "ifc_count: %u, port_num:%u\n", ifc_count,
723                                 ksb_port_num);
724                 if (ifc_count > 1)
725                         return -ENODEV;
726                 if (ksb_port_num >= NO_BRIDGE_INSTANCES) {
727                         dev_err(&udev->dev, "port-num:%u invalid. Try first\n",
728                                         ksb_port_num);
729                         ksb_port_num = 0;
730                 }
731                 ksb = __ksb[ksb_port_num];
732                 if (ksb->ifc) {
733                         dev_err(&udev->dev, "port already in use\n");
734                         return -ENODEV;
735                 }
736                 mdev = kzalloc(sizeof(struct ksb_dev_info), GFP_KERNEL);
737                 if (!mdev)
738                         return -ENOMEM;
739                 free_mdev = true;
740                 mdev->name = ksb->name;
741                 break;
742         case 0x9008:
743         case 0x9902:
744         case 0x9903:
745                 ksb = __ksb[bus_id];
746                 mdev = &fbdev[bus_id];
747                 break;
748         case 0x9048:
749         case 0x904C:
750         case 0x9075:
751         case 0x908A:
752         case 0x908E:
753         case 0x90A0:
754         case 0x909C:
755         case 0x909D:
756         case 0x909E:
757         case 0x909F:
758         case 0x90A4:
759                 ksb = __ksb[EFS_HSIC_BRIDGE_INDEX];
760                 break;
761         case 0x9079:
762                 if (ifc_num != 2)
763                         return -ENODEV;
764                 ksb = __ksb[EFS_USB_BRIDGE_INDEX];
765                 break;
766         default:
767                 return -ENODEV;
768         }
769
770         if (!ksb) {
771                 pr_err("ksb is not initialized");
772                 return -ENODEV;
773         }
774
775         ksb->udev = usb_get_dev(interface_to_usbdev(ifc));
776         ksb->ifc = ifc;
777         ifc_desc = ifc->cur_altsetting;
778         ksb->id_info = *mdev;
779
780         for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
781                 ep_desc = &ifc_desc->endpoint[i].desc;
782
783                 if (!ksb->in_epAddr && (usb_endpoint_is_bulk_in(ep_desc))) {
784                         ksb->in_epAddr = ep_desc->bEndpointAddress;
785                         ksb->period = 0;
786                 }
787
788                 if (!ksb->in_epAddr && (usb_endpoint_is_int_in(ep_desc))) {
789                         ksb->in_epAddr = ep_desc->bEndpointAddress;
790                         ksb->period = ep_desc->bInterval;
791                 }
792
793                 if (!ksb->out_epAddr && usb_endpoint_is_bulk_out(ep_desc))
794                         ksb->out_epAddr = ep_desc->bEndpointAddress;
795         }
796
797         if (!(ksb->in_epAddr && ksb->out_epAddr)) {
798                 dev_err(&udev->dev,
799                         "could not find bulk in and bulk out endpoints");
800                 usb_put_dev(ksb->udev);
801                 ksb->ifc = NULL;
802                 if (free_mdev)
803                         kfree(mdev);
804                 return -ENODEV;
805         }
806
807         ksb->in_pipe = ksb->period ?
808                 usb_rcvintpipe(ksb->udev, ksb->in_epAddr) :
809                 usb_rcvbulkpipe(ksb->udev, ksb->in_epAddr);
810
811         ksb->out_pipe = usb_sndbulkpipe(ksb->udev, ksb->out_epAddr);
812
813         usb_set_intfdata(ifc, ksb);
814         set_bit(USB_DEV_CONNECTED, &ksb->flags);
815         atomic_set(&ksb->tx_pending_cnt, 0);
816         atomic_set(&ksb->rx_pending_cnt, 0);
817
818         dbg_log_event(ksb, "PID-ATT", id->idProduct, 0);
819
820         /*free up stale buffers if any from previous disconnect*/
821         spin_lock_irqsave(&ksb->lock, flags);
822         while (!list_empty(&ksb->to_ks_list)) {
823                 pkt = list_first_entry(&ksb->to_ks_list,
824                                 struct data_pkt, list);
825                 list_del_init(&pkt->list);
826                 ksb_free_data_pkt(pkt);
827         }
828         while (!list_empty(&ksb->to_mdm_list)) {
829                 pkt = list_first_entry(&ksb->to_mdm_list,
830                                 struct data_pkt, list);
831                 list_del_init(&pkt->list);
832                 ksb_free_data_pkt(pkt);
833         }
834         spin_unlock_irqrestore(&ksb->lock, flags);
835
836         ret = alloc_chrdev_region(&ksb->cdev_start_no, 0, 1, mdev->name);
837         if (ret < 0) {
838                 dbg_log_event(ksb, "chr reg failed", ret, 0);
839                 goto fail_chrdev_region;
840         }
841
842         ksb->class = class_create(THIS_MODULE, mdev->name);
843         if (IS_ERR(ksb->class)) {
844                 dbg_log_event(ksb, "clscr failed", PTR_ERR(ksb->class), 0);
845                 goto fail_class_create;
846         }
847
848         cdev_init(&ksb->cdev, &ksb_fops);
849         ksb->cdev.owner = THIS_MODULE;
850
851         ret = cdev_add(&ksb->cdev, ksb->cdev_start_no, 1);
852         if (ret < 0) {
853                 dbg_log_event(ksb, "cdev_add failed", ret, 0);
854                 goto fail_class_create;
855         }
856
857         ksb->device = device_create(ksb->class, &udev->dev, ksb->cdev_start_no,
858                                 NULL, mdev->name);
859         if (IS_ERR(ksb->device)) {
860                 dbg_log_event(ksb, "devcrfailed", PTR_ERR(ksb->device), 0);
861                 goto fail_device_create;
862         }
863
864         if (device_can_wakeup(&ksb->udev->dev))
865                 ifc->needs_remote_wakeup = 1;
866
867         if (free_mdev)
868                 kfree(mdev);
869
870         ks_bridge_notify_status(&ksb->device->kobj, id);
871         dev_dbg(&udev->dev, "usb dev connected");
872
873         return 0;
874
875 fail_device_create:
876         cdev_del(&ksb->cdev);
877 fail_class_create:
878         unregister_chrdev_region(ksb->cdev_start_no, 1);
879 fail_chrdev_region:
880         usb_set_intfdata(ifc, NULL);
881         clear_bit(USB_DEV_CONNECTED, &ksb->flags);
882
883         if (free_mdev)
884                 kfree(mdev);
885
886         return -ENODEV;
887
888 }
889
890 static int ksb_usb_suspend(struct usb_interface *ifc, pm_message_t message)
891 {
892         struct ks_bridge *ksb = usb_get_intfdata(ifc);
893         unsigned long flags;
894
895         dbg_log_event(ksb, "SUSPEND", 0, 0);
896
897         if (pm_runtime_autosuspend_expiration(&ksb->udev->dev)) {
898                 dbg_log_event(ksb, "SUSP ABORT-TimeCheck", 0, 0);
899                 return -EBUSY;
900         }
901
902         usb_kill_anchored_urbs(&ksb->submitted);
903
904         spin_lock_irqsave(&ksb->lock, flags);
905         if (!list_empty(&ksb->to_ks_list)) {
906                 spin_unlock_irqrestore(&ksb->lock, flags);
907                 dbg_log_event(ksb, "SUSPEND ABORT", 0, 0);
908                 /*
909                  * Now wakeup the reader process and queue
910                  * Rx URBs for more data.
911                  */
912                 wake_up(&ksb->ks_wait_q);
913                 queue_work(ksb->wq, &ksb->start_rx_work);
914                 return -EBUSY;
915         }
916         spin_unlock_irqrestore(&ksb->lock, flags);
917
918         return 0;
919 }
920
921 static int ksb_usb_resume(struct usb_interface *ifc)
922 {
923         struct ks_bridge *ksb = usb_get_intfdata(ifc);
924
925         dbg_log_event(ksb, "RESUME", 0, 0);
926
927         if (test_bit(FILE_OPENED, &ksb->flags))
928                 queue_work(ksb->wq, &ksb->start_rx_work);
929
930         return 0;
931 }
932
933 static void ksb_usb_disconnect(struct usb_interface *ifc)
934 {
935         struct ks_bridge *ksb = usb_get_intfdata(ifc);
936         unsigned long flags;
937         struct data_pkt *pkt;
938
939         dbg_log_event(ksb, "PID-DETACH", 0, 0);
940
941         clear_bit(USB_DEV_CONNECTED, &ksb->flags);
942         kobject_uevent(&ksb->device->kobj, KOBJ_OFFLINE);
943         wake_up(&ksb->ks_wait_q);
944         cancel_work_sync(&ksb->to_mdm_work);
945         cancel_work_sync(&ksb->start_rx_work);
946
947         device_destroy(ksb->class, ksb->cdev_start_no);
948         cdev_del(&ksb->cdev);
949         class_destroy(ksb->class);
950         unregister_chrdev_region(ksb->cdev_start_no, 1);
951
952         usb_kill_anchored_urbs(&ksb->submitted);
953
954         wait_event_interruptible_timeout(
955                                         ksb->pending_urb_wait,
956                                         !atomic_read(&ksb->tx_pending_cnt) &&
957                                         !atomic_read(&ksb->rx_pending_cnt),
958                                         msecs_to_jiffies(PENDING_URB_TIMEOUT));
959
960         spin_lock_irqsave(&ksb->lock, flags);
961         while (!list_empty(&ksb->to_ks_list)) {
962                 pkt = list_first_entry(&ksb->to_ks_list,
963                                 struct data_pkt, list);
964                 list_del_init(&pkt->list);
965                 ksb_free_data_pkt(pkt);
966         }
967         while (!list_empty(&ksb->to_mdm_list)) {
968                 pkt = list_first_entry(&ksb->to_mdm_list,
969                                 struct data_pkt, list);
970                 list_del_init(&pkt->list);
971                 ksb_free_data_pkt(pkt);
972         }
973         spin_unlock_irqrestore(&ksb->lock, flags);
974
975         ifc->needs_remote_wakeup = 0;
976         usb_put_dev(ksb->udev);
977         ksb->ifc = NULL;
978         usb_set_intfdata(ifc, NULL);
979 }
980
981 static struct usb_driver ksb_usb_driver = {
982         .name =         "ks_bridge",
983         .probe =        ksb_usb_probe,
984         .disconnect =   ksb_usb_disconnect,
985         .suspend =      ksb_usb_suspend,
986         .resume =       ksb_usb_resume,
987         .reset_resume = ksb_usb_resume,
988         .id_table =     ksb_usb_ids,
989         .supports_autosuspend = 1,
990 };
991
992 static int ksb_debug_show(struct seq_file *s, void *unused)
993 {
994         unsigned long           flags;
995         struct ks_bridge        *ksb = s->private;
996         int                     i;
997
998         read_lock_irqsave(&ksb->dbg_lock, flags);
999         for (i = 0; i < DBG_MAX_MSG; i++) {
1000                 if (i == (ksb->dbg_idx - 1))
1001                         seq_printf(s, "-->%s\n", ksb->dbgbuf[i]);
1002                 else
1003                         seq_printf(s, "%s\n", ksb->dbgbuf[i]);
1004         }
1005         read_unlock_irqrestore(&ksb->dbg_lock, flags);
1006
1007         return 0;
1008 }
1009
1010 static int ksb_debug_open(struct inode *ip, struct file *fp)
1011 {
1012         return single_open(fp, ksb_debug_show, ip->i_private);
1013
1014         return 0;
1015 }
1016
1017 static const struct file_operations dbg_fops = {
1018         .open = ksb_debug_open,
1019         .read = seq_read,
1020         .llseek = seq_lseek,
1021         .release = single_release,
1022 };
1023
1024 static struct dentry *dbg_dir;
1025
1026 static int __init ksb_init(void)
1027 {
1028         struct ks_bridge *ksb;
1029         int num_instances = 0;
1030         int ret = 0;
1031         int i;
1032
1033         dbg_dir = debugfs_create_dir("ks_bridge", NULL);
1034         if (IS_ERR(dbg_dir))
1035                 pr_err("unable to create debug dir");
1036
1037         for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
1038                 ksb = kzalloc(sizeof(struct ks_bridge), GFP_KERNEL);
1039                 if (!ksb) {
1040                         pr_err("unable to allocat mem for ks_bridge");
1041                         ret =  -ENOMEM;
1042                         goto dev_free;
1043                 }
1044                 __ksb[i] = ksb;
1045
1046                 ksb->name = kasprintf(GFP_KERNEL, "ks_usb_bridge.%i", i);
1047                 if (!ksb->name) {
1048                         pr_info("unable to allocate name");
1049                         kfree(ksb);
1050                         ret = -ENOMEM;
1051                         goto dev_free;
1052                 }
1053
1054                 spin_lock_init(&ksb->lock);
1055                 INIT_LIST_HEAD(&ksb->to_mdm_list);
1056                 INIT_LIST_HEAD(&ksb->to_ks_list);
1057                 init_waitqueue_head(&ksb->ks_wait_q);
1058                 init_waitqueue_head(&ksb->pending_urb_wait);
1059                 ksb->wq = create_singlethread_workqueue(ksb->name);
1060                 if (!ksb->wq) {
1061                         pr_err("unable to allocate workqueue");
1062                         kfree(ksb->name);
1063                         kfree(ksb);
1064                         ret = -ENOMEM;
1065                         goto dev_free;
1066                 }
1067
1068                 INIT_WORK(&ksb->to_mdm_work, ksb_tomdm_work);
1069                 INIT_WORK(&ksb->start_rx_work, ksb_start_rx_work);
1070                 init_usb_anchor(&ksb->submitted);
1071
1072                 ksb->dbg_idx = 0;
1073                 ksb->dbg_lock = __RW_LOCK_UNLOCKED(lck);
1074
1075                 if (!IS_ERR(dbg_dir))
1076                         debugfs_create_file(ksb->name, S_IRUGO, dbg_dir,
1077                                         ksb, &dbg_fops);
1078
1079                 num_instances++;
1080         }
1081
1082         ret = usb_register(&ksb_usb_driver);
1083         if (ret) {
1084                 pr_err("unable to register ks bridge driver");
1085                 goto dev_free;
1086         }
1087
1088         pr_info("init done");
1089
1090         return 0;
1091
1092 dev_free:
1093         if (!IS_ERR(dbg_dir))
1094                 debugfs_remove_recursive(dbg_dir);
1095
1096         for (i = 0; i < num_instances; i++) {
1097                 ksb = __ksb[i];
1098
1099                 destroy_workqueue(ksb->wq);
1100                 kfree(ksb->name);
1101                 kfree(ksb);
1102         }
1103
1104         return ret;
1105
1106 }
1107
1108 static void __exit ksb_exit(void)
1109 {
1110         struct ks_bridge *ksb;
1111         int i;
1112
1113         if (!IS_ERR(dbg_dir))
1114                 debugfs_remove_recursive(dbg_dir);
1115
1116         usb_deregister(&ksb_usb_driver);
1117
1118         for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
1119                 ksb = __ksb[i];
1120
1121                 destroy_workqueue(ksb->wq);
1122                 kfree(ksb->name);
1123                 kfree(ksb);
1124         }
1125 }
1126
1127 module_init(ksb_init);
1128 module_exit(ksb_exit);
1129
1130 MODULE_DESCRIPTION(DRIVER_DESC);
1131 MODULE_LICENSE("GPL v2");