blob: 11d388c36e07cbd4599f4127ed2a35b28b6213ca [file] [log] [blame]
Hemant Kumar14401d52011-11-03 16:40:32 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/module.h>
18#include <linux/kref.h>
19#include <linux/debugfs.h>
20#include <linux/platform_device.h>
21#include <linux/uaccess.h>
22#include <linux/ratelimit.h>
23#include <linux/usb/ch9.h>
24#include <linux/usb/cdc.h>
25#include <linux/termios.h>
26#include <asm/unaligned.h>
27#include <mach/usb_bridge.h>
28
29static const char *ctrl_bridge_names[] = {
30 "dun_ctrl_hsic0",
31 "rmnet_ctrl_hsic0"
32};
33
34/* polling interval for Interrupt ep */
35#define HS_INTERVAL 7
36#define FS_LS_INTERVAL 3
37
38#define ACM_CTRL_DTR (1 << 0)
39#define DEFAULT_READ_URB_LENGTH 4096
40
41struct ctrl_bridge {
42
43 struct usb_device *udev;
44 struct usb_interface *intf;
45
46 unsigned int int_pipe;
47 struct urb *inturb;
48 void *intbuf;
49
50 struct urb *readurb;
51 void *readbuf;
52
53 struct usb_anchor tx_submitted;
54 struct usb_ctrlrequest *in_ctlreq;
55
56 struct bridge *brdg;
57 struct platform_device *pdev;
58
59 /* input control lines (DSR, CTS, CD, RI) */
60 unsigned int cbits_tohost;
61
62 /* output control lines (DTR, RTS) */
63 unsigned int cbits_tomdm;
64
65 /* counters */
66 unsigned int snd_encap_cmd;
67 unsigned int get_encap_res;
68 unsigned int resp_avail;
69 unsigned int set_ctrl_line_sts;
70 unsigned int notify_ser_state;
71
72};
73
74static struct ctrl_bridge *__dev[MAX_BRIDGE_DEVICES];
75
76/* counter used for indexing ctrl bridge devices */
77static int ch_id;
78
79unsigned int ctrl_bridge_get_cbits_tohost(unsigned int id)
80{
81 struct ctrl_bridge *dev;
82
83 if (id >= MAX_BRIDGE_DEVICES)
84 return -EINVAL;
85
86 dev = __dev[id];
87 if (!dev)
88 return -ENODEV;
89
90 return dev->cbits_tohost;
91}
92EXPORT_SYMBOL(ctrl_bridge_get_cbits_tohost);
93
94int ctrl_bridge_set_cbits(unsigned int id, unsigned int cbits)
95{
96 struct ctrl_bridge *dev;
97 struct bridge *brdg;
98 int retval;
99
100 if (id >= MAX_BRIDGE_DEVICES)
101 return -EINVAL;
102
103 dev = __dev[id];
104 if (!dev)
105 return -ENODEV;
106
107 pr_debug("%s: dev[id] =%u cbits : %u\n", __func__, id, cbits);
108
109 brdg = dev->brdg;
110 if (!brdg)
111 return -ENODEV;
112
113 dev->cbits_tomdm = cbits;
114
115 retval = ctrl_bridge_write(id, NULL, 0);
116
117 /* if DTR is high, update latest modem info to host */
118 if (brdg && (cbits & ACM_CTRL_DTR) && brdg->ops.send_cbits)
119 brdg->ops.send_cbits(brdg->ctx, dev->cbits_tohost);
120
121 return retval;
122}
123EXPORT_SYMBOL(ctrl_bridge_set_cbits);
124
125static void resp_avail_cb(struct urb *urb)
126{
127 struct ctrl_bridge *dev = urb->context;
128 struct usb_device *udev;
129 int status = 0;
130 int resubmit_urb = 1;
131 struct bridge *brdg = dev->brdg;
132
133 udev = interface_to_usbdev(dev->intf);
134 switch (urb->status) {
135 case 0:
136 /*success*/
137 dev->get_encap_res++;
138 if (brdg && brdg->ops.send_pkt)
139 brdg->ops.send_pkt(brdg->ctx, urb->transfer_buffer,
140 urb->actual_length);
141 break;
142
143 /*do not resubmit*/
144 case -ESHUTDOWN:
145 case -ENOENT:
146 case -ECONNRESET:
147 /* unplug */
148 case -EPROTO:
149 /*babble error*/
150 resubmit_urb = 0;
151 /*resubmit*/
152 case -EOVERFLOW:
153 default:
154 dev_dbg(&udev->dev, "%s: non zero urb status = %d\n",
155 __func__, urb->status);
156 }
157
158 if (resubmit_urb) {
159 /*re- submit int urb to check response available*/
160 status = usb_submit_urb(dev->inturb, GFP_ATOMIC);
161 if (status)
162 dev_err(&udev->dev,
163 "%s: Error re-submitting Int URB %d\n",
164 __func__, status);
165 }
166}
167
168static void notification_available_cb(struct urb *urb)
169{
170 int status;
171 struct usb_cdc_notification *ctrl;
172 struct usb_device *udev;
173 struct ctrl_bridge *dev = urb->context;
174 struct bridge *brdg = dev->brdg;
175 unsigned int ctrl_bits;
176 unsigned char *data;
177
178 udev = interface_to_usbdev(dev->intf);
179
180 switch (urb->status) {
181 case 0:
182 /*success*/
183 break;
184 case -ESHUTDOWN:
185 case -ENOENT:
186 case -ECONNRESET:
187 case -EPROTO:
188 /* unplug */
189 return;
190 case -EPIPE:
191 dev_err(&udev->dev, "%s: stall on int endpoint\n", __func__);
192 /* TBD : halt to be cleared in work */
193 case -EOVERFLOW:
194 default:
195 pr_debug_ratelimited("%s: non zero urb status = %d\n",
196 __func__, urb->status);
197 goto resubmit_int_urb;
198 }
199
200 ctrl = (struct usb_cdc_notification *)urb->transfer_buffer;
201 data = (unsigned char *)(ctrl + 1);
202
203 switch (ctrl->bNotificationType) {
204 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
205 dev->resp_avail++;
206 usb_fill_control_urb(dev->readurb, udev,
207 usb_rcvctrlpipe(udev, 0),
208 (unsigned char *)dev->in_ctlreq,
209 dev->readbuf,
210 DEFAULT_READ_URB_LENGTH,
211 resp_avail_cb, dev);
212
213 status = usb_submit_urb(dev->readurb, GFP_ATOMIC);
214 if (status) {
215 dev_err(&udev->dev,
216 "%s: Error submitting Read URB %d\n",
217 __func__, status);
218 goto resubmit_int_urb;
219 }
220 return;
221 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
222 dev_dbg(&udev->dev, "%s network\n", ctrl->wValue ?
223 "connected to" : "disconnected from");
224 break;
225 case USB_CDC_NOTIFY_SERIAL_STATE:
226 dev->notify_ser_state++;
227 ctrl_bits = get_unaligned_le16(data);
228 dev_dbg(&udev->dev, "serial state: %d\n", ctrl_bits);
229 dev->cbits_tohost = ctrl_bits;
230 if (brdg && brdg->ops.send_cbits)
231 brdg->ops.send_cbits(brdg->ctx, ctrl_bits);
232 break;
233 default:
234 dev_err(&udev->dev, "%s: unknown notification %d received:"
235 "index %d len %d data0 %d data1 %d",
236 __func__, ctrl->bNotificationType, ctrl->wIndex,
237 ctrl->wLength, data[0], data[1]);
238 }
239
240resubmit_int_urb:
241 status = usb_submit_urb(urb, GFP_ATOMIC);
242 if (status)
243 dev_err(&udev->dev, "%s: Error re-submitting Int URB %d\n",
244 __func__, status);
245}
246
247int ctrl_bridge_start_read(struct ctrl_bridge *dev)
248{
249 int retval = 0;
250 struct usb_device *udev;
251
252 udev = interface_to_usbdev(dev->intf);
253
254 retval = usb_autopm_get_interface_async(dev->intf);
255 if (retval < 0) {
256 dev_err(&udev->dev, "%s resumption fail\n", __func__);
257 goto done_nopm;
258 }
259
260 retval = usb_submit_urb(dev->inturb, GFP_KERNEL);
261 if (retval < 0)
262 dev_err(&udev->dev, "%s intr submit %d\n", __func__, retval);
263
264 usb_autopm_put_interface_async(dev->intf);
265done_nopm:
266 return retval;
267}
268
269static int ctrl_bridge_stop_read(struct ctrl_bridge *dev)
270{
271 if (dev->readurb) {
272 dev_dbg(&dev->udev->dev, "killing rcv urb\n");
273 usb_unlink_urb(dev->readurb);
274 }
275
276 if (dev->inturb) {
277 dev_dbg(&dev->udev->dev, "killing int urb\n");
278 usb_unlink_urb(dev->inturb);
279 }
280
281 return 0;
282}
283
284int ctrl_bridge_open(struct bridge *brdg)
285{
286 struct ctrl_bridge *dev;
287
288 if (!brdg) {
289 err("bridge is null\n");
290 return -EINVAL;
291 }
292
293 if (brdg->ch_id >= MAX_BRIDGE_DEVICES)
294 return -EINVAL;
295
296 dev = __dev[brdg->ch_id];
297 if (!dev) {
298 err("dev is null\n");
299 return -ENODEV;
300 }
301
302 dev->brdg = brdg;
303 dev->snd_encap_cmd = 0;
304 dev->get_encap_res = 0;
305 dev->resp_avail = 0;
306 dev->set_ctrl_line_sts = 0;
307 dev->notify_ser_state = 0;
308
309 return ctrl_bridge_start_read(dev);
310}
311EXPORT_SYMBOL(ctrl_bridge_open);
312
313void ctrl_bridge_close(unsigned int id)
314{
315 struct ctrl_bridge *dev;
316
317 if (id >= MAX_BRIDGE_DEVICES)
318 return;
319
320 dev = __dev[id];
Jack Phama7c92672011-11-29 16:38:21 -0800321 if (!dev || !dev->brdg)
Hemant Kumar14401d52011-11-03 16:40:32 -0700322 return;
323
324 dev_dbg(&dev->udev->dev, "%s:\n", __func__);
325
326 ctrl_bridge_set_cbits(dev->brdg->ch_id, 0);
327 usb_unlink_anchored_urbs(&dev->tx_submitted);
328 ctrl_bridge_stop_read(dev);
329
330 dev->brdg = NULL;
331}
332EXPORT_SYMBOL(ctrl_bridge_close);
333
334static void ctrl_write_callback(struct urb *urb)
335{
336
337 if (urb->status) {
338 pr_debug("Write status/size %d/%d\n",
339 urb->status, urb->actual_length);
340 }
341
342 kfree(urb->transfer_buffer);
343 kfree(urb->setup_packet);
344 usb_free_urb(urb);
345}
346
347int ctrl_bridge_write(unsigned int id, char *data, size_t size)
348{
349 int result;
350 struct urb *writeurb;
351 struct usb_ctrlrequest *out_ctlreq;
352 struct usb_device *udev;
353 struct ctrl_bridge *dev;
354
355 if (id >= MAX_BRIDGE_DEVICES) {
356 result = -EINVAL;
357 goto free_data;
358 }
359
360 dev = __dev[id];
361
362 if (!dev) {
363 result = -ENODEV;
364 goto free_data;
365 }
366
367 udev = interface_to_usbdev(dev->intf);
368
369 dev_dbg(&udev->dev, "%s:[id]:%u: write (%d bytes)\n",
370 __func__, id, size);
371
372 writeurb = usb_alloc_urb(0, GFP_ATOMIC);
373 if (!writeurb) {
374 dev_err(&udev->dev, "%s: error allocating read urb\n",
375 __func__);
376 result = -ENOMEM;
377 goto free_data;
378 }
379
380 out_ctlreq = kmalloc(sizeof(*out_ctlreq), GFP_ATOMIC);
381 if (!out_ctlreq) {
382 dev_err(&udev->dev,
383 "%s: error allocating setup packet buffer\n",
384 __func__);
385 result = -ENOMEM;
386 goto free_urb;
387 }
388
389 /* CDC Send Encapsulated Request packet */
390 out_ctlreq->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
391 USB_RECIP_INTERFACE);
392 if (!data && !size) {
393 out_ctlreq->bRequest = USB_CDC_REQ_SET_CONTROL_LINE_STATE;
394 out_ctlreq->wValue = dev->cbits_tomdm;
395 dev->set_ctrl_line_sts++;
396 } else {
397 out_ctlreq->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
398 out_ctlreq->wValue = 0;
399 dev->snd_encap_cmd++;
400 }
401 out_ctlreq->wIndex =
402 dev->intf->cur_altsetting->desc.bInterfaceNumber;
403 out_ctlreq->wLength = cpu_to_le16(size);
404
405 usb_fill_control_urb(writeurb, udev,
406 usb_sndctrlpipe(udev, 0),
407 (unsigned char *)out_ctlreq,
408 (void *)data, size,
409 ctrl_write_callback, NULL);
410
411 result = usb_autopm_get_interface_async(dev->intf);
412 if (result < 0) {
413 dev_err(&udev->dev, "%s: unable to resume interface: %d\n",
414 __func__, result);
415
416 /*
417 * Revisit: if (result == -EPERM)
418 * bridge_suspend(dev->intf, PMSG_SUSPEND);
419 */
420
421 goto free_ctrlreq;
422 }
423
424 usb_anchor_urb(writeurb, &dev->tx_submitted);
425 result = usb_submit_urb(writeurb, GFP_ATOMIC);
426 if (result < 0) {
427 dev_err(&udev->dev, "%s: submit URB error %d\n",
428 __func__, result);
429 usb_autopm_put_interface_async(dev->intf);
430 goto unanchor_urb;
431 }
432
433 return size;
434
435unanchor_urb:
436 usb_unanchor_urb(writeurb);
437free_ctrlreq:
438 kfree(out_ctlreq);
439free_urb:
440 usb_free_urb(writeurb);
441free_data:
442 kfree(data);
443
444 return result;
445}
446EXPORT_SYMBOL(ctrl_bridge_write);
447
448int ctrl_bridge_suspend(unsigned int id)
449{
450 struct ctrl_bridge *dev;
451
452 if (id >= MAX_BRIDGE_DEVICES)
453 return -EINVAL;
454
455 dev = __dev[id];
456 if (!dev)
457 return -ENODEV;
458
459 usb_kill_anchored_urbs(&dev->tx_submitted);
460
461 return ctrl_bridge_stop_read(dev);
462}
463
464int ctrl_bridge_resume(unsigned int id)
465{
466 struct ctrl_bridge *dev;
467
468 if (id >= MAX_BRIDGE_DEVICES)
469 return -EINVAL;
470
471 dev = __dev[id];
472 if (!dev)
473 return -ENODEV;
474
475 return ctrl_bridge_start_read(dev);
476}
477
478#if defined(CONFIG_DEBUG_FS)
479#define DEBUG_BUF_SIZE 1024
480static ssize_t ctrl_bridge_read_stats(struct file *file, char __user *ubuf,
481 size_t count, loff_t *ppos)
482{
483 struct ctrl_bridge *dev;
484 char *buf;
485 int ret;
486 int i;
487 int temp = 0;
488
489 buf = kzalloc(sizeof(char) * DEBUG_BUF_SIZE, GFP_KERNEL);
490 if (!buf)
491 return -ENOMEM;
492
493 for (i = 0; i < ch_id; i++) {
494 dev = __dev[i];
495 if (!dev)
496 continue;
497
498 temp += scnprintf(buf + temp, DEBUG_BUF_SIZE - temp,
499 "\nName#%s dev %p\n"
500 "snd encap cmd cnt: %u\n"
501 "get encap res cnt: %u\n"
502 "res available cnt: %u\n"
503 "set ctrlline sts cnt: %u\n"
504 "notify ser state cnt: %u\n"
505 "cbits_tomdm: %d\n"
506 "cbits_tohost: %d\n",
507 dev->pdev->name, dev,
508 dev->snd_encap_cmd,
509 dev->get_encap_res,
510 dev->resp_avail,
511 dev->set_ctrl_line_sts,
512 dev->notify_ser_state,
513 dev->cbits_tomdm,
514 dev->cbits_tohost);
515
516 }
517
518 ret = simple_read_from_buffer(ubuf, count, ppos, buf, temp);
519
520 kfree(buf);
521
522 return ret;
523}
524
525static ssize_t ctrl_bridge_reset_stats(struct file *file,
526 const char __user *buf, size_t count, loff_t *ppos)
527{
528 struct ctrl_bridge *dev;
529 int i;
530
531 for (i = 0; i < ch_id; i++) {
532 dev = __dev[i];
533 if (!dev)
534 continue;
535
536 dev->snd_encap_cmd = 0;
537 dev->get_encap_res = 0;
538 dev->resp_avail = 0;
539 dev->set_ctrl_line_sts = 0;
540 dev->notify_ser_state = 0;
541 }
542 return count;
543}
544
545const struct file_operations ctrl_stats_ops = {
546 .read = ctrl_bridge_read_stats,
547 .write = ctrl_bridge_reset_stats,
548};
549
550struct dentry *ctrl_dent;
551struct dentry *ctrl_dfile;
552static void ctrl_bridge_debugfs_init(void)
553{
554 ctrl_dent = debugfs_create_dir("ctrl_hsic_bridge", 0);
555 if (IS_ERR(ctrl_dent))
556 return;
557
558 ctrl_dfile =
559 debugfs_create_file("status", 0644, ctrl_dent, 0,
560 &ctrl_stats_ops);
561 if (!ctrl_dfile || IS_ERR(ctrl_dfile))
562 debugfs_remove(ctrl_dent);
563}
564
565static void ctrl_bridge_debugfs_exit(void)
566{
567 debugfs_remove(ctrl_dfile);
568 debugfs_remove(ctrl_dent);
569}
570
571#else
572static void ctrl_bridge_debugfs_init(void) { }
573static void ctrl_bridge_debugfs_exit(void) { }
574#endif
575
576int
577ctrl_bridge_probe(struct usb_interface *ifc, struct usb_host_endpoint *int_in,
578 int id)
579{
580 struct ctrl_bridge *dev;
581 struct usb_device *udev;
582 struct usb_endpoint_descriptor *ep;
583 u16 wMaxPacketSize;
584 int retval = 0;
585 int interval;
586
587 udev = interface_to_usbdev(ifc);
588
589 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
590 if (!dev) {
591 dev_err(&udev->dev, "%s: unable to allocate dev\n",
592 __func__);
593 return -ENOMEM;
594 }
595 dev->pdev = platform_device_alloc(ctrl_bridge_names[id], id);
596 if (!dev->pdev) {
597 dev_err(&dev->udev->dev,
598 "%s: unable to allocate platform device\n", __func__);
599 retval = -ENOMEM;
600 goto nomem;
601 }
602
603 dev->udev = udev;
604 dev->int_pipe = usb_rcvintpipe(udev,
605 int_in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
606 dev->intf = ifc;
607
608 init_usb_anchor(&dev->tx_submitted);
609
610 /*use max pkt size from ep desc*/
611 ep = &dev->intf->cur_altsetting->endpoint[0].desc;
612
613 dev->inturb = usb_alloc_urb(0, GFP_KERNEL);
614 if (!dev->inturb) {
615 dev_err(&udev->dev, "%s: error allocating int urb\n", __func__);
616 retval = -ENOMEM;
617 goto pdev_del;
618 }
619
620 wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
621
622 dev->intbuf = kmalloc(wMaxPacketSize, GFP_KERNEL);
623 if (!dev->intbuf) {
624 dev_err(&udev->dev, "%s: error allocating int buffer\n",
625 __func__);
626 retval = -ENOMEM;
627 goto free_inturb;
628 }
629
630 interval =
631 (udev->speed == USB_SPEED_HIGH) ? HS_INTERVAL : FS_LS_INTERVAL;
632
633 usb_fill_int_urb(dev->inturb, udev, dev->int_pipe,
634 dev->intbuf, wMaxPacketSize,
635 notification_available_cb, dev, interval);
636
637 dev->readurb = usb_alloc_urb(0, GFP_KERNEL);
638 if (!dev->readurb) {
639 dev_err(&udev->dev, "%s: error allocating read urb\n",
640 __func__);
641 retval = -ENOMEM;
642 goto free_intbuf;
643 }
644
645 dev->readbuf = kmalloc(DEFAULT_READ_URB_LENGTH, GFP_KERNEL);
646 if (!dev->readbuf) {
647 dev_err(&udev->dev, "%s: error allocating read buffer\n",
648 __func__);
649 retval = -ENOMEM;
650 goto free_rurb;
651 }
652
653 dev->in_ctlreq = kmalloc(sizeof(*dev->in_ctlreq), GFP_KERNEL);
654 if (!dev->in_ctlreq) {
655 dev_err(&udev->dev,
656 "%s:error allocating setup packet buffer\n",
657 __func__);
658 retval = -ENOMEM;
659 goto free_rbuf;
660 }
661
662 dev->in_ctlreq->bRequestType =
663 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
664 dev->in_ctlreq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
665 dev->in_ctlreq->wValue = 0;
666 dev->in_ctlreq->wIndex =
667 dev->intf->cur_altsetting->desc.bInterfaceNumber;
668 dev->in_ctlreq->wLength = cpu_to_le16(DEFAULT_READ_URB_LENGTH);
669
670 __dev[id] = dev;
671
672 platform_device_add(dev->pdev);
673
674 ch_id++;
675
676 return retval;
677
678free_rbuf:
679 kfree(dev->readbuf);
680free_rurb:
681 usb_free_urb(dev->readurb);
682free_intbuf:
683 kfree(dev->intbuf);
684free_inturb:
685 usb_free_urb(dev->inturb);
686pdev_del:
687 platform_device_del(dev->pdev);
688nomem:
689 kfree(dev);
690
691 return retval;
692}
693
694void ctrl_bridge_disconnect(unsigned int id)
695{
696 struct ctrl_bridge *dev = __dev[id];
697
698 dev_dbg(&dev->udev->dev, "%s:\n", __func__);
699
Jack Pham452c6922011-12-14 18:15:26 -0800700 platform_device_del(dev->pdev);
701
Hemant Kumar14401d52011-11-03 16:40:32 -0700702 kfree(dev->in_ctlreq);
703 kfree(dev->readbuf);
704 kfree(dev->intbuf);
705
706 usb_free_urb(dev->readurb);
707 usb_free_urb(dev->inturb);
708
Hemant Kumar14401d52011-11-03 16:40:32 -0700709 __dev[id] = NULL;
710 ch_id--;
711
712 kfree(dev);
713}
714
715static int __init ctrl_bridge_init(void)
716{
717 ctrl_bridge_debugfs_init();
718
719 return 0;
720}
721module_init(ctrl_bridge_init);
722
723static void __exit ctrl_bridge_exit(void)
724{
725 ctrl_bridge_debugfs_exit();
726}
727module_exit(ctrl_bridge_exit);
728
729MODULE_DESCRIPTION("Qualcomm modem control bridge driver");
730MODULE_LICENSE("GPL v2");