blob: f7ccd921100d660e1e844e4df39ef2d8a9fe08e5 [file] [log] [blame]
Vamsi Krishna5b944712012-06-29 18:36:23 -07001/*
Pavankumar Kondeti4fde7d42012-12-28 17:15:41 +05302 * Copyright (c) 2012-2013, Linux Foundation. All rights reserved.
Vamsi Krishna5b944712012-06-29 18:36:23 -07003 *
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
Hemant Kumar54aeb822013-01-16 18:55:14 -080036enum bus_id {
37 BUS_HSIC,
38 BUS_USB,
39 BUS_UNDEF,
40};
41
42#define BUSNAME_LEN 20
43
44static enum bus_id str_to_busid(const char *name)
45{
46 if (!strncasecmp("msm_hsic_host", name, BUSNAME_LEN))
47 return BUS_HSIC;
48 if (!strncasecmp("msm_ehci_host.0", name, BUSNAME_LEN))
49 return BUS_USB;
50
51 return BUS_UNDEF;
52}
53
Vamsi Krishna5b944712012-06-29 18:36:23 -070054struct data_pkt {
55 int n_read;
56 char *buf;
57 size_t len;
58 struct list_head list;
59 void *ctxt;
60};
61
62#define FILE_OPENED BIT(0)
63#define USB_DEV_CONNECTED BIT(1)
64#define NO_RX_REQS 10
Hemant Kumar54aeb822013-01-16 18:55:14 -080065#define NO_BRIDGE_INSTANCES 4
66#define EFS_HSIC_BRIDGE_INDEX 2
67#define EFS_USB_BRIDGE_INDEX 3
Vamsi Krishna5b944712012-06-29 18:36:23 -070068#define MAX_DATA_PKT_SIZE 16384
Hemant Kumarea4b0b02012-09-16 20:28:56 -070069#define PENDING_URB_TIMEOUT 10
Vamsi Krishna5b944712012-06-29 18:36:23 -070070
71struct ks_bridge {
72 char *name;
73 spinlock_t lock;
74 struct workqueue_struct *wq;
75 struct work_struct to_mdm_work;
76 struct work_struct start_rx_work;
77 struct list_head to_mdm_list;
78 struct list_head to_ks_list;
79 wait_queue_head_t ks_wait_q;
Hemant Kumar54aeb822013-01-16 18:55:14 -080080 struct miscdevice fs_dev;
Hemant Kumarea4b0b02012-09-16 20:28:56 -070081 wait_queue_head_t pending_urb_wait;
82 atomic_t tx_pending_cnt;
83 atomic_t rx_pending_cnt;
Vamsi Krishna5b944712012-06-29 18:36:23 -070084
85 /* usb specific */
86 struct usb_device *udev;
87 struct usb_interface *ifc;
88 __u8 in_epAddr;
89 __u8 out_epAddr;
90 unsigned int in_pipe;
91 unsigned int out_pipe;
92 struct usb_anchor submitted;
93
94 unsigned long flags;
Vamsi Krishna5b944712012-06-29 18:36:23 -070095
96#define DBG_MSG_LEN 40
97#define DBG_MAX_MSG 500
98 unsigned int dbg_idx;
99 rwlock_t dbg_lock;
100 char (dbgbuf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
101};
102struct ks_bridge *__ksb[NO_BRIDGE_INSTANCES];
103
104/* by default debugging is enabled */
105static unsigned int enable_dbg = 1;
106module_param(enable_dbg, uint, S_IRUGO | S_IWUSR);
107
108static void
109dbg_log_event(struct ks_bridge *ksb, char *event, int d1, int d2)
110{
111 unsigned long flags;
112 unsigned long long t;
113 unsigned long nanosec;
114
115 if (!enable_dbg)
116 return;
117
118 write_lock_irqsave(&ksb->dbg_lock, flags);
119 t = cpu_clock(smp_processor_id());
120 nanosec = do_div(t, 1000000000)/1000;
121 scnprintf(ksb->dbgbuf[ksb->dbg_idx], DBG_MSG_LEN, "%5lu.%06lu:%s:%x:%x",
122 (unsigned long)t, nanosec, event, d1, d2);
123
124 ksb->dbg_idx++;
125 ksb->dbg_idx = ksb->dbg_idx % DBG_MAX_MSG;
126 write_unlock_irqrestore(&ksb->dbg_lock, flags);
127}
128
129static
130struct data_pkt *ksb_alloc_data_pkt(size_t count, gfp_t flags, void *ctxt)
131{
132 struct data_pkt *pkt;
133
134 pkt = kzalloc(sizeof(struct data_pkt), flags);
135 if (!pkt) {
136 pr_err("failed to allocate data packet\n");
137 return ERR_PTR(-ENOMEM);
138 }
139
140 pkt->buf = kmalloc(count, flags);
141 if (!pkt->buf) {
142 pr_err("failed to allocate data buffer\n");
143 kfree(pkt);
144 return ERR_PTR(-ENOMEM);
145 }
146
147 pkt->len = count;
148 INIT_LIST_HEAD(&pkt->list);
149 pkt->ctxt = ctxt;
150
151 return pkt;
152}
153
154static void ksb_free_data_pkt(struct data_pkt *pkt)
155{
156 kfree(pkt->buf);
157 kfree(pkt);
158}
159
160
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530161static void
162submit_one_urb(struct ks_bridge *ksb, gfp_t flags, struct data_pkt *pkt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700163static ssize_t ksb_fs_read(struct file *fp, char __user *buf,
164 size_t count, loff_t *pos)
165{
166 int ret;
167 unsigned long flags;
168 struct ks_bridge *ksb = fp->private_data;
169 struct data_pkt *pkt;
170 size_t space, copied;
171
172read_start:
173 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
174 return -ENODEV;
175
176 spin_lock_irqsave(&ksb->lock, flags);
177 if (list_empty(&ksb->to_ks_list)) {
178 spin_unlock_irqrestore(&ksb->lock, flags);
179 ret = wait_event_interruptible(ksb->ks_wait_q,
180 !list_empty(&ksb->to_ks_list) ||
181 !test_bit(USB_DEV_CONNECTED, &ksb->flags));
182 if (ret < 0)
183 return ret;
184
185 goto read_start;
186 }
187
188 space = count;
189 copied = 0;
190 while (!list_empty(&ksb->to_ks_list) && space) {
191 size_t len;
192
193 pkt = list_first_entry(&ksb->to_ks_list, struct data_pkt, list);
Hemant Kumard45f96c2012-09-24 12:32:32 -0700194 len = min_t(size_t, space, pkt->len - pkt->n_read);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700195 spin_unlock_irqrestore(&ksb->lock, flags);
196
Hemant Kumard45f96c2012-09-24 12:32:32 -0700197 ret = copy_to_user(buf + copied, pkt->buf + pkt->n_read, len);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700198 if (ret) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800199 dev_err(ksb->fs_dev.this_device,
200 "copy_to_user failed err:%d\n", ret);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700201 ksb_free_data_pkt(pkt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700202 return ret;
203 }
204
Hemant Kumard45f96c2012-09-24 12:32:32 -0700205 pkt->n_read += len;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700206 space -= len;
207 copied += len;
208
209 spin_lock_irqsave(&ksb->lock, flags);
210 if (pkt->n_read == pkt->len) {
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530211 /*
212 * re-init the packet and queue it
213 * for more data.
214 */
Vamsi Krishna5b944712012-06-29 18:36:23 -0700215 list_del_init(&pkt->list);
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530216 pkt->n_read = 0;
217 pkt->len = MAX_DATA_PKT_SIZE;
218 spin_unlock_irqrestore(&ksb->lock, flags);
219 submit_one_urb(ksb, GFP_KERNEL, pkt);
220 spin_lock_irqsave(&ksb->lock, flags);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700221 }
222 }
223 spin_unlock_irqrestore(&ksb->lock, flags);
224
225 dbg_log_event(ksb, "KS_READ", copied, 0);
226
Hemant Kumar54aeb822013-01-16 18:55:14 -0800227 dev_dbg(ksb->fs_dev.this_device, "count:%d space:%d copied:%d", count,
228 space, copied);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700229
230 return copied;
231}
232
233static void ksb_tx_cb(struct urb *urb)
234{
235 struct data_pkt *pkt = urb->context;
236 struct ks_bridge *ksb = pkt->ctxt;
237
238 dbg_log_event(ksb, "C TX_URB", urb->status, 0);
Hemant Kumar54aeb822013-01-16 18:55:14 -0800239 dev_dbg(&ksb->udev->dev, "status:%d", urb->status);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700240
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700241 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
Vamsi Krishna5b944712012-06-29 18:36:23 -0700242 usb_autopm_put_interface_async(ksb->ifc);
243
244 if (urb->status < 0)
Hemant Kumar54aeb822013-01-16 18:55:14 -0800245 pr_err_ratelimited("%s: urb failed with err:%d",
246 ksb->fs_dev.name, urb->status);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700247
248 ksb_free_data_pkt(pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700249
250 atomic_dec(&ksb->tx_pending_cnt);
251 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700252}
253
254static void ksb_tomdm_work(struct work_struct *w)
255{
256 struct ks_bridge *ksb = container_of(w, struct ks_bridge, to_mdm_work);
257 struct data_pkt *pkt;
258 unsigned long flags;
259 struct urb *urb;
260 int ret;
261
262 spin_lock_irqsave(&ksb->lock, flags);
263 while (!list_empty(&ksb->to_mdm_list)
264 && test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
265 pkt = list_first_entry(&ksb->to_mdm_list,
266 struct data_pkt, list);
267 list_del_init(&pkt->list);
268 spin_unlock_irqrestore(&ksb->lock, flags);
269
270 urb = usb_alloc_urb(0, GFP_KERNEL);
271 if (!urb) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800272 pr_err_ratelimited("%s: unable to allocate urb",
273 ksb->fs_dev.name);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700274 ksb_free_data_pkt(pkt);
275 return;
276 }
277
278 ret = usb_autopm_get_interface(ksb->ifc);
279 if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800280 pr_err_ratelimited("%s: autopm_get failed:%d",
281 ksb->fs_dev.name, ret);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700282 usb_free_urb(urb);
283 ksb_free_data_pkt(pkt);
284 return;
285 }
286 usb_fill_bulk_urb(urb, ksb->udev, ksb->out_pipe,
287 pkt->buf, pkt->len, ksb_tx_cb, pkt);
288 usb_anchor_urb(urb, &ksb->submitted);
289
290 dbg_log_event(ksb, "S TX_URB", pkt->len, 0);
291
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700292 atomic_inc(&ksb->tx_pending_cnt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700293 ret = usb_submit_urb(urb, GFP_KERNEL);
294 if (ret) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800295 dev_err(&ksb->udev->dev, "out urb submission failed");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700296 usb_unanchor_urb(urb);
297 usb_free_urb(urb);
298 ksb_free_data_pkt(pkt);
299 usb_autopm_put_interface(ksb->ifc);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700300 atomic_dec(&ksb->tx_pending_cnt);
301 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700302 return;
303 }
304
Hemant Kumar9b230b52012-09-19 12:40:11 -0700305 usb_free_urb(urb);
306
Vamsi Krishna5b944712012-06-29 18:36:23 -0700307 spin_lock_irqsave(&ksb->lock, flags);
308 }
309 spin_unlock_irqrestore(&ksb->lock, flags);
310}
311
312static ssize_t ksb_fs_write(struct file *fp, const char __user *buf,
313 size_t count, loff_t *pos)
314{
315 int ret;
316 struct data_pkt *pkt;
317 unsigned long flags;
318 struct ks_bridge *ksb = fp->private_data;
319
Pavankumar Kondeti4fde7d42012-12-28 17:15:41 +0530320
321 if (count > MAX_DATA_PKT_SIZE)
322 count = MAX_DATA_PKT_SIZE;
323
Vamsi Krishna5b944712012-06-29 18:36:23 -0700324 pkt = ksb_alloc_data_pkt(count, GFP_KERNEL, ksb);
325 if (IS_ERR(pkt)) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800326 dev_err(ksb->fs_dev.this_device,
327 "unable to allocate data packet");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700328 return PTR_ERR(pkt);
329 }
330
331 ret = copy_from_user(pkt->buf, buf, count);
332 if (ret) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800333 dev_err(ksb->fs_dev.this_device,
334 "copy_from_user failed: err:%d", ret);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700335 ksb_free_data_pkt(pkt);
336 return ret;
337 }
338
339 spin_lock_irqsave(&ksb->lock, flags);
340 list_add_tail(&pkt->list, &ksb->to_mdm_list);
341 spin_unlock_irqrestore(&ksb->lock, flags);
342
343 queue_work(ksb->wq, &ksb->to_mdm_work);
344
345 return count;
346}
347
Vamsi Krishna5b944712012-06-29 18:36:23 -0700348static int ksb_fs_open(struct inode *ip, struct file *fp)
349{
Hemant Kumar54aeb822013-01-16 18:55:14 -0800350 struct miscdevice *mdev = fp->private_data;
351 struct ks_bridge *ksb = container_of(mdev, struct ks_bridge, fs_dev);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700352
Hemant Kumar54aeb822013-01-16 18:55:14 -0800353 if (IS_ERR(ksb)) {
354 pr_err("ksb device not found");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700355 return -ENODEV;
356 }
357
Hemant Kumar54aeb822013-01-16 18:55:14 -0800358 dev_dbg(ksb->fs_dev.this_device, ":%s", ksb->fs_dev.name);
359 dbg_log_event(ksb, "FS-OPEN", 0, 0);
360
Vamsi Krishna5b944712012-06-29 18:36:23 -0700361 fp->private_data = ksb;
362 set_bit(FILE_OPENED, &ksb->flags);
363
364 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
365 queue_work(ksb->wq, &ksb->start_rx_work);
366
367 return 0;
368}
369
370static int ksb_fs_release(struct inode *ip, struct file *fp)
371{
372 struct ks_bridge *ksb = fp->private_data;
373
Hemant Kumar54aeb822013-01-16 18:55:14 -0800374 dev_dbg(ksb->fs_dev.this_device, ":%s", ksb->fs_dev.name);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700375 dbg_log_event(ksb, "FS-RELEASE", 0, 0);
376
377 clear_bit(FILE_OPENED, &ksb->flags);
378 fp->private_data = NULL;
379
380 return 0;
381}
382
383static const struct file_operations ksb_fops = {
384 .owner = THIS_MODULE,
385 .read = ksb_fs_read,
386 .write = ksb_fs_write,
387 .open = ksb_fs_open,
388 .release = ksb_fs_release,
389};
390
Hemant Kumar54aeb822013-01-16 18:55:14 -0800391static struct miscdevice ksb_fboot_dev[] = {
392 {
393 .minor = MISC_DYNAMIC_MINOR,
394 .name = "ks_hsic_bridge",
395 .fops = &ksb_fops,
396 },
397 {
398 .minor = MISC_DYNAMIC_MINOR,
399 .name = "ks_usb_bridge",
400 .fops = &ksb_fops,
401 },
Vamsi Krishna5b944712012-06-29 18:36:23 -0700402};
403
404static const struct file_operations efs_fops = {
405 .owner = THIS_MODULE,
406 .read = ksb_fs_read,
407 .write = ksb_fs_write,
Hemant Kumar54aeb822013-01-16 18:55:14 -0800408 .open = ksb_fs_open,
Vamsi Krishna5b944712012-06-29 18:36:23 -0700409 .release = ksb_fs_release,
410};
411
Hemant Kumar54aeb822013-01-16 18:55:14 -0800412static struct miscdevice ksb_efs_hsic_dev = {
Vamsi Krishna5b944712012-06-29 18:36:23 -0700413 .minor = MISC_DYNAMIC_MINOR,
Hemant Kumar54aeb822013-01-16 18:55:14 -0800414 .name = "efs_hsic_bridge",
Vamsi Krishna5b944712012-06-29 18:36:23 -0700415 .fops = &efs_fops,
416};
417
Hemant Kumar54aeb822013-01-16 18:55:14 -0800418static struct miscdevice ksb_efs_usb_dev = {
419 .minor = MISC_DYNAMIC_MINOR,
420 .name = "efs_usb_bridge",
421 .fops = &efs_fops,
422};
Vamsi Krishna5b944712012-06-29 18:36:23 -0700423static const struct usb_device_id ksb_usb_ids[] = {
424 { USB_DEVICE(0x5c6, 0x9008),
425 .driver_info = (unsigned long)&ksb_fboot_dev, },
426 { USB_DEVICE(0x5c6, 0x9048),
Hemant Kumar54aeb822013-01-16 18:55:14 -0800427 .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
Vamsi Krishna5b944712012-06-29 18:36:23 -0700428 { USB_DEVICE(0x5c6, 0x904C),
Hemant Kumar54aeb822013-01-16 18:55:14 -0800429 .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
430 { USB_DEVICE(0x5c6, 0x9079),
431 .driver_info = (unsigned long)&ksb_efs_usb_dev, },
Vamsi Krishna5b944712012-06-29 18:36:23 -0700432
433 {} /* terminating entry */
434};
435MODULE_DEVICE_TABLE(usb, ksb_usb_ids);
436
437static void ksb_rx_cb(struct urb *urb);
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530438static void
439submit_one_urb(struct ks_bridge *ksb, gfp_t flags, struct data_pkt *pkt)
Vamsi Krishna5b944712012-06-29 18:36:23 -0700440{
Vamsi Krishna5b944712012-06-29 18:36:23 -0700441 struct urb *urb;
442 int ret;
443
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530444 urb = usb_alloc_urb(0, flags);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700445 if (!urb) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800446 dev_err(&ksb->udev->dev, "unable to allocate urb");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700447 ksb_free_data_pkt(pkt);
448 return;
449 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700450
451 usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
452 pkt->buf, pkt->len,
453 ksb_rx_cb, pkt);
454 usb_anchor_urb(urb, &ksb->submitted);
455
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700456 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
Vamsi Krishna5b944712012-06-29 18:36:23 -0700457 usb_unanchor_urb(urb);
458 usb_free_urb(urb);
459 ksb_free_data_pkt(pkt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700460 return;
461 }
462
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700463 atomic_inc(&ksb->rx_pending_cnt);
464 ret = usb_submit_urb(urb, GFP_ATOMIC);
465 if (ret) {
466 dev_err(&ksb->udev->dev, "in urb submission failed");
467 usb_unanchor_urb(urb);
468 usb_free_urb(urb);
469 ksb_free_data_pkt(pkt);
470 atomic_dec(&ksb->rx_pending_cnt);
471 wake_up(&ksb->pending_urb_wait);
472 return;
473 }
474
475 dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
476
Vamsi Krishna5b944712012-06-29 18:36:23 -0700477 usb_free_urb(urb);
478}
479static void ksb_rx_cb(struct urb *urb)
480{
481 struct data_pkt *pkt = urb->context;
482 struct ks_bridge *ksb = pkt->ctxt;
483
484 dbg_log_event(ksb, "C RX_URB", urb->status, urb->actual_length);
485
Hemant Kumar54aeb822013-01-16 18:55:14 -0800486 dev_dbg(&ksb->udev->dev, "status:%d actual:%d", urb->status,
487 urb->actual_length);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700488
Hemant Kumara6194c12012-08-31 18:38:32 -0700489 /*non zero len of data received while unlinking urb*/
490 if (urb->status == -ENOENT && urb->actual_length > 0)
491 goto add_to_list;
492
Vamsi Krishna5b944712012-06-29 18:36:23 -0700493 if (urb->status < 0) {
Hemant Kumara6194c12012-08-31 18:38:32 -0700494 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT
495 && urb->status != -EPROTO)
Hemant Kumar54aeb822013-01-16 18:55:14 -0800496 pr_err_ratelimited("%s: urb failed with err:%d",
497 ksb->fs_dev.name, urb->status);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700498 ksb_free_data_pkt(pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700499 goto done;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700500 }
501
502 if (urb->actual_length == 0) {
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530503 submit_one_urb(ksb, GFP_ATOMIC, pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700504 goto done;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700505 }
506
Hemant Kumara6194c12012-08-31 18:38:32 -0700507add_to_list:
Vamsi Krishna5b944712012-06-29 18:36:23 -0700508 spin_lock(&ksb->lock);
509 pkt->len = urb->actual_length;
510 list_add_tail(&pkt->list, &ksb->to_ks_list);
511 spin_unlock(&ksb->lock);
512
513 /* wake up read thread */
514 wake_up(&ksb->ks_wait_q);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700515done:
516 atomic_dec(&ksb->rx_pending_cnt);
517 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700518}
519
520static void ksb_start_rx_work(struct work_struct *w)
521{
522 struct ks_bridge *ksb =
523 container_of(w, struct ks_bridge, start_rx_work);
524 struct data_pkt *pkt;
525 struct urb *urb;
526 int i = 0;
527 int ret;
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530528 bool put = true;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700529
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530530 ret = usb_autopm_get_interface(ksb->ifc);
531 if (ret < 0) {
532 if (ret != -EAGAIN && ret != -EACCES) {
533 pr_err_ratelimited("autopm_get failed:%d", ret);
534 return;
535 }
536 put = false;
537 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700538 for (i = 0; i < NO_RX_REQS; i++) {
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700539
540 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
541 return;
542
Vamsi Krishna5b944712012-06-29 18:36:23 -0700543 pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_KERNEL, ksb);
544 if (IS_ERR(pkt)) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800545 dev_err(&ksb->udev->dev, "unable to allocate data pkt");
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530546 break;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700547 }
548
549 urb = usb_alloc_urb(0, GFP_KERNEL);
550 if (!urb) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800551 dev_err(&ksb->udev->dev, "unable to allocate urb");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700552 ksb_free_data_pkt(pkt);
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530553 break;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700554 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700555
556 usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
557 pkt->buf, pkt->len,
558 ksb_rx_cb, pkt);
559 usb_anchor_urb(urb, &ksb->submitted);
560
561 dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
562
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700563 atomic_inc(&ksb->rx_pending_cnt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700564 ret = usb_submit_urb(urb, GFP_KERNEL);
565 if (ret) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800566 dev_err(&ksb->udev->dev, "in urb submission failed");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700567 usb_unanchor_urb(urb);
568 usb_free_urb(urb);
569 ksb_free_data_pkt(pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700570 atomic_dec(&ksb->rx_pending_cnt);
571 wake_up(&ksb->pending_urb_wait);
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530572 break;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700573 }
574
Vamsi Krishna5b944712012-06-29 18:36:23 -0700575 usb_free_urb(urb);
576 }
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530577 if (put)
578 usb_autopm_put_interface_async(ksb->ifc);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700579}
580
581static int
582ksb_usb_probe(struct usb_interface *ifc, const struct usb_device_id *id)
583{
584 __u8 ifc_num;
585 struct usb_host_interface *ifc_desc;
586 struct usb_endpoint_descriptor *ep_desc;
587 int i;
588 struct ks_bridge *ksb;
Hemant Kumar54aeb822013-01-16 18:55:14 -0800589 struct miscdevice *mdev, *fbdev;
590 struct usb_device *udev;
591 unsigned int bus_id;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700592
593 ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
594
Hemant Kumar54aeb822013-01-16 18:55:14 -0800595 udev = interface_to_usbdev(ifc);
596 fbdev = mdev = (struct miscdevice *)id->driver_info;
597
598 bus_id = str_to_busid(udev->bus->bus_name);
599 if (bus_id == BUS_UNDEF) {
600 dev_err(&udev->dev, "unknown usb bus %s, probe failed\n",
601 udev->bus->bus_name);
602 return -ENODEV;
603 }
604
Vamsi Krishna5b944712012-06-29 18:36:23 -0700605 switch (id->idProduct) {
606 case 0x9008:
607 if (ifc_num != 0)
608 return -ENODEV;
Hemant Kumar54aeb822013-01-16 18:55:14 -0800609 ksb = __ksb[bus_id];
610 mdev = &fbdev[bus_id];
Vamsi Krishna5b944712012-06-29 18:36:23 -0700611 break;
612 case 0x9048:
613 case 0x904C:
614 if (ifc_num != 2)
615 return -ENODEV;
Hemant Kumar54aeb822013-01-16 18:55:14 -0800616 ksb = __ksb[EFS_HSIC_BRIDGE_INDEX];
617 break;
618 case 0x9079:
619 if (ifc_num != 2)
620 return -ENODEV;
621 ksb = __ksb[EFS_USB_BRIDGE_INDEX];
Vamsi Krishna5b944712012-06-29 18:36:23 -0700622 break;
623 default:
624 return -ENODEV;
625 }
626
627 if (!ksb) {
628 pr_err("ksb is not initialized");
629 return -ENODEV;
630 }
631
632 ksb->udev = usb_get_dev(interface_to_usbdev(ifc));
633 ksb->ifc = ifc;
634 ifc_desc = ifc->cur_altsetting;
635
636 for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
637 ep_desc = &ifc_desc->endpoint[i].desc;
638
639 if (!ksb->in_epAddr && usb_endpoint_is_bulk_in(ep_desc))
640 ksb->in_epAddr = ep_desc->bEndpointAddress;
641
642 if (!ksb->out_epAddr && usb_endpoint_is_bulk_out(ep_desc))
643 ksb->out_epAddr = ep_desc->bEndpointAddress;
644 }
645
646 if (!(ksb->in_epAddr && ksb->out_epAddr)) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800647 dev_err(&udev->dev,
648 "could not find bulk in and bulk out endpoints");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700649 usb_put_dev(ksb->udev);
650 ksb->ifc = NULL;
651 return -ENODEV;
652 }
653
654 ksb->in_pipe = usb_rcvbulkpipe(ksb->udev, ksb->in_epAddr);
655 ksb->out_pipe = usb_sndbulkpipe(ksb->udev, ksb->out_epAddr);
656
657 usb_set_intfdata(ifc, ksb);
658 set_bit(USB_DEV_CONNECTED, &ksb->flags);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700659 atomic_set(&ksb->tx_pending_cnt, 0);
660 atomic_set(&ksb->rx_pending_cnt, 0);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700661
662 dbg_log_event(ksb, "PID-ATT", id->idProduct, 0);
663
Hemant Kumar54aeb822013-01-16 18:55:14 -0800664 ksb->fs_dev = *mdev;
665 misc_register(&ksb->fs_dev);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700666
Hemant Kumarf292a322012-09-07 19:00:21 -0700667 ifc->needs_remote_wakeup = 1;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700668 usb_enable_autosuspend(ksb->udev);
669
Hemant Kumar54aeb822013-01-16 18:55:14 -0800670 dev_dbg(&udev->dev, "usb dev connected");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700671
672 return 0;
673}
674
675static int ksb_usb_suspend(struct usb_interface *ifc, pm_message_t message)
676{
677 struct ks_bridge *ksb = usb_get_intfdata(ifc);
678
679 dbg_log_event(ksb, "SUSPEND", 0, 0);
680
Vamsi Krishna5b944712012-06-29 18:36:23 -0700681 usb_kill_anchored_urbs(&ksb->submitted);
682
683 return 0;
684}
685
686static int ksb_usb_resume(struct usb_interface *ifc)
687{
688 struct ks_bridge *ksb = usb_get_intfdata(ifc);
689
690 dbg_log_event(ksb, "RESUME", 0, 0);
691
692 if (test_bit(FILE_OPENED, &ksb->flags))
693 queue_work(ksb->wq, &ksb->start_rx_work);
694
695 return 0;
696}
697
698static void ksb_usb_disconnect(struct usb_interface *ifc)
699{
700 struct ks_bridge *ksb = usb_get_intfdata(ifc);
701 unsigned long flags;
702 struct data_pkt *pkt;
703
704 dbg_log_event(ksb, "PID-DETACH", 0, 0);
705
706 clear_bit(USB_DEV_CONNECTED, &ksb->flags);
707 wake_up(&ksb->ks_wait_q);
708 cancel_work_sync(&ksb->to_mdm_work);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700709 cancel_work_sync(&ksb->start_rx_work);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700710
Hemant Kumar54aeb822013-01-16 18:55:14 -0800711 misc_deregister(&ksb->fs_dev);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700712
Hemant Kumar38980ec42012-08-24 19:42:31 -0700713 usb_kill_anchored_urbs(&ksb->submitted);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700714
715 wait_event_interruptible_timeout(
716 ksb->pending_urb_wait,
717 !atomic_read(&ksb->tx_pending_cnt) &&
718 !atomic_read(&ksb->rx_pending_cnt),
719 msecs_to_jiffies(PENDING_URB_TIMEOUT));
720
Vamsi Krishna5b944712012-06-29 18:36:23 -0700721 spin_lock_irqsave(&ksb->lock, flags);
722 while (!list_empty(&ksb->to_ks_list)) {
723 pkt = list_first_entry(&ksb->to_ks_list,
724 struct data_pkt, list);
725 list_del_init(&pkt->list);
726 ksb_free_data_pkt(pkt);
727 }
728 while (!list_empty(&ksb->to_mdm_list)) {
729 pkt = list_first_entry(&ksb->to_mdm_list,
730 struct data_pkt, list);
731 list_del_init(&pkt->list);
732 ksb_free_data_pkt(pkt);
733 }
734 spin_unlock_irqrestore(&ksb->lock, flags);
735
Hemant Kumarf292a322012-09-07 19:00:21 -0700736 ifc->needs_remote_wakeup = 0;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700737 usb_put_dev(ksb->udev);
738 ksb->ifc = NULL;
739 usb_set_intfdata(ifc, NULL);
740
741 return;
742}
743
744static struct usb_driver ksb_usb_driver = {
745 .name = "ks_bridge",
746 .probe = ksb_usb_probe,
747 .disconnect = ksb_usb_disconnect,
748 .suspend = ksb_usb_suspend,
749 .resume = ksb_usb_resume,
750 .id_table = ksb_usb_ids,
751 .supports_autosuspend = 1,
752};
753
754static ssize_t ksb_debug_show(struct seq_file *s, void *unused)
755{
756 unsigned long flags;
757 struct ks_bridge *ksb = s->private;
758 int i;
759
760 read_lock_irqsave(&ksb->dbg_lock, flags);
761 for (i = 0; i < DBG_MAX_MSG; i++) {
762 if (i == (ksb->dbg_idx - 1))
763 seq_printf(s, "-->%s\n", ksb->dbgbuf[i]);
764 else
765 seq_printf(s, "%s\n", ksb->dbgbuf[i]);
766 }
767 read_unlock_irqrestore(&ksb->dbg_lock, flags);
768
769 return 0;
770}
771
772static int ksb_debug_open(struct inode *ip, struct file *fp)
773{
774 return single_open(fp, ksb_debug_show, ip->i_private);
775
776 return 0;
777}
778
779static const struct file_operations dbg_fops = {
780 .open = ksb_debug_open,
781 .read = seq_read,
782 .llseek = seq_lseek,
783 .release = single_release,
784};
785static struct dentry *dbg_dir;
786static int __init ksb_init(void)
787{
788 struct ks_bridge *ksb;
789 int num_instances = 0;
790 int ret = 0;
791 int i;
792
793 dbg_dir = debugfs_create_dir("ks_bridge", NULL);
794 if (IS_ERR(dbg_dir))
795 pr_err("unable to create debug dir");
796
797 for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
798 ksb = kzalloc(sizeof(struct ks_bridge), GFP_KERNEL);
799 if (!ksb) {
800 pr_err("unable to allocat mem for ks_bridge");
Hemant Kumar38980ec42012-08-24 19:42:31 -0700801 ret = -ENOMEM;
802 goto dev_free;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700803 }
804 __ksb[i] = ksb;
805
806 ksb->name = kasprintf(GFP_KERNEL, "ks_bridge:%i", i + 1);
807 if (!ksb->name) {
808 pr_info("unable to allocate name");
809 kfree(ksb);
810 ret = -ENOMEM;
811 goto dev_free;
812 }
813
814 spin_lock_init(&ksb->lock);
815 INIT_LIST_HEAD(&ksb->to_mdm_list);
816 INIT_LIST_HEAD(&ksb->to_ks_list);
817 init_waitqueue_head(&ksb->ks_wait_q);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700818 init_waitqueue_head(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700819 ksb->wq = create_singlethread_workqueue(ksb->name);
820 if (!ksb->wq) {
821 pr_err("unable to allocate workqueue");
822 kfree(ksb->name);
823 kfree(ksb);
824 ret = -ENOMEM;
825 goto dev_free;
826 }
827
828 INIT_WORK(&ksb->to_mdm_work, ksb_tomdm_work);
829 INIT_WORK(&ksb->start_rx_work, ksb_start_rx_work);
830 init_usb_anchor(&ksb->submitted);
831
832 ksb->dbg_idx = 0;
833 ksb->dbg_lock = __RW_LOCK_UNLOCKED(lck);
834
835 if (!IS_ERR(dbg_dir))
836 debugfs_create_file(ksb->name, S_IRUGO, dbg_dir,
837 ksb, &dbg_fops);
838
839 num_instances++;
840 }
841
842 ret = usb_register(&ksb_usb_driver);
843 if (ret) {
844 pr_err("unable to register ks bridge driver");
845 goto dev_free;
846 }
847
848 pr_info("init done");
849
850 return 0;
851
852dev_free:
853 if (!IS_ERR(dbg_dir))
854 debugfs_remove_recursive(dbg_dir);
855
856 for (i = 0; i < num_instances; i++) {
857 ksb = __ksb[i];
858
859 destroy_workqueue(ksb->wq);
860 kfree(ksb->name);
861 kfree(ksb);
862 }
863
864 return ret;
865
866}
867
868static void __exit ksb_exit(void)
869{
870 struct ks_bridge *ksb;
871 int i;
872
873 if (!IS_ERR(dbg_dir))
874 debugfs_remove_recursive(dbg_dir);
875
876 usb_deregister(&ksb_usb_driver);
877
878 for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
879 ksb = __ksb[i];
880
881 destroy_workqueue(ksb->wq);
882 kfree(ksb->name);
883 kfree(ksb);
884 }
885}
886
887module_init(ksb_init);
888module_exit(ksb_exit);
889
890MODULE_DESCRIPTION(DRIVER_DESC);
891MODULE_VERSION(DRIVER_VERSION);
892MODULE_LICENSE("GPL v2");