blob: 584503a5dbef759e7b141c10e98044bc665d900b [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{
Hemant Kumarf13358e2012-01-19 12:07:20 -0800336 struct ctrl_bridge *dev = urb->context;
Hemant Kumar14401d52011-11-03 16:40:32 -0700337
338 if (urb->status) {
339 pr_debug("Write status/size %d/%d\n",
340 urb->status, urb->actual_length);
341 }
342
343 kfree(urb->transfer_buffer);
344 kfree(urb->setup_packet);
345 usb_free_urb(urb);
Hemant Kumarf13358e2012-01-19 12:07:20 -0800346 usb_autopm_put_interface_async(dev->intf);
Hemant Kumar14401d52011-11-03 16:40:32 -0700347}
348
349int ctrl_bridge_write(unsigned int id, char *data, size_t size)
350{
351 int result;
352 struct urb *writeurb;
353 struct usb_ctrlrequest *out_ctlreq;
354 struct usb_device *udev;
355 struct ctrl_bridge *dev;
356
357 if (id >= MAX_BRIDGE_DEVICES) {
358 result = -EINVAL;
359 goto free_data;
360 }
361
362 dev = __dev[id];
363
364 if (!dev) {
365 result = -ENODEV;
366 goto free_data;
367 }
368
369 udev = interface_to_usbdev(dev->intf);
370
371 dev_dbg(&udev->dev, "%s:[id]:%u: write (%d bytes)\n",
372 __func__, id, size);
373
374 writeurb = usb_alloc_urb(0, GFP_ATOMIC);
375 if (!writeurb) {
376 dev_err(&udev->dev, "%s: error allocating read urb\n",
377 __func__);
378 result = -ENOMEM;
379 goto free_data;
380 }
381
382 out_ctlreq = kmalloc(sizeof(*out_ctlreq), GFP_ATOMIC);
383 if (!out_ctlreq) {
384 dev_err(&udev->dev,
385 "%s: error allocating setup packet buffer\n",
386 __func__);
387 result = -ENOMEM;
388 goto free_urb;
389 }
390
391 /* CDC Send Encapsulated Request packet */
392 out_ctlreq->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
393 USB_RECIP_INTERFACE);
394 if (!data && !size) {
395 out_ctlreq->bRequest = USB_CDC_REQ_SET_CONTROL_LINE_STATE;
396 out_ctlreq->wValue = dev->cbits_tomdm;
397 dev->set_ctrl_line_sts++;
398 } else {
399 out_ctlreq->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
400 out_ctlreq->wValue = 0;
401 dev->snd_encap_cmd++;
402 }
403 out_ctlreq->wIndex =
404 dev->intf->cur_altsetting->desc.bInterfaceNumber;
405 out_ctlreq->wLength = cpu_to_le16(size);
406
407 usb_fill_control_urb(writeurb, udev,
408 usb_sndctrlpipe(udev, 0),
409 (unsigned char *)out_ctlreq,
410 (void *)data, size,
Hemant Kumarf13358e2012-01-19 12:07:20 -0800411 ctrl_write_callback, dev);
Hemant Kumar14401d52011-11-03 16:40:32 -0700412
413 result = usb_autopm_get_interface_async(dev->intf);
414 if (result < 0) {
415 dev_err(&udev->dev, "%s: unable to resume interface: %d\n",
416 __func__, result);
417
418 /*
419 * Revisit: if (result == -EPERM)
420 * bridge_suspend(dev->intf, PMSG_SUSPEND);
421 */
422
423 goto free_ctrlreq;
424 }
425
426 usb_anchor_urb(writeurb, &dev->tx_submitted);
427 result = usb_submit_urb(writeurb, GFP_ATOMIC);
428 if (result < 0) {
429 dev_err(&udev->dev, "%s: submit URB error %d\n",
430 __func__, result);
431 usb_autopm_put_interface_async(dev->intf);
432 goto unanchor_urb;
433 }
434
435 return size;
436
437unanchor_urb:
438 usb_unanchor_urb(writeurb);
439free_ctrlreq:
440 kfree(out_ctlreq);
441free_urb:
442 usb_free_urb(writeurb);
443free_data:
444 kfree(data);
445
446 return result;
447}
448EXPORT_SYMBOL(ctrl_bridge_write);
449
450int ctrl_bridge_suspend(unsigned int id)
451{
452 struct ctrl_bridge *dev;
453
454 if (id >= MAX_BRIDGE_DEVICES)
455 return -EINVAL;
456
457 dev = __dev[id];
458 if (!dev)
459 return -ENODEV;
460
461 usb_kill_anchored_urbs(&dev->tx_submitted);
462
463 return ctrl_bridge_stop_read(dev);
464}
465
466int ctrl_bridge_resume(unsigned int id)
467{
468 struct ctrl_bridge *dev;
469
470 if (id >= MAX_BRIDGE_DEVICES)
471 return -EINVAL;
472
473 dev = __dev[id];
474 if (!dev)
475 return -ENODEV;
476
477 return ctrl_bridge_start_read(dev);
478}
479
480#if defined(CONFIG_DEBUG_FS)
481#define DEBUG_BUF_SIZE 1024
482static ssize_t ctrl_bridge_read_stats(struct file *file, char __user *ubuf,
483 size_t count, loff_t *ppos)
484{
485 struct ctrl_bridge *dev;
486 char *buf;
487 int ret;
488 int i;
489 int temp = 0;
490
491 buf = kzalloc(sizeof(char) * DEBUG_BUF_SIZE, GFP_KERNEL);
492 if (!buf)
493 return -ENOMEM;
494
495 for (i = 0; i < ch_id; i++) {
496 dev = __dev[i];
497 if (!dev)
498 continue;
499
500 temp += scnprintf(buf + temp, DEBUG_BUF_SIZE - temp,
501 "\nName#%s dev %p\n"
502 "snd encap cmd cnt: %u\n"
503 "get encap res cnt: %u\n"
504 "res available cnt: %u\n"
505 "set ctrlline sts cnt: %u\n"
506 "notify ser state cnt: %u\n"
507 "cbits_tomdm: %d\n"
508 "cbits_tohost: %d\n",
509 dev->pdev->name, dev,
510 dev->snd_encap_cmd,
511 dev->get_encap_res,
512 dev->resp_avail,
513 dev->set_ctrl_line_sts,
514 dev->notify_ser_state,
515 dev->cbits_tomdm,
516 dev->cbits_tohost);
517
518 }
519
520 ret = simple_read_from_buffer(ubuf, count, ppos, buf, temp);
521
522 kfree(buf);
523
524 return ret;
525}
526
527static ssize_t ctrl_bridge_reset_stats(struct file *file,
528 const char __user *buf, size_t count, loff_t *ppos)
529{
530 struct ctrl_bridge *dev;
531 int i;
532
533 for (i = 0; i < ch_id; i++) {
534 dev = __dev[i];
535 if (!dev)
536 continue;
537
538 dev->snd_encap_cmd = 0;
539 dev->get_encap_res = 0;
540 dev->resp_avail = 0;
541 dev->set_ctrl_line_sts = 0;
542 dev->notify_ser_state = 0;
543 }
544 return count;
545}
546
547const struct file_operations ctrl_stats_ops = {
548 .read = ctrl_bridge_read_stats,
549 .write = ctrl_bridge_reset_stats,
550};
551
552struct dentry *ctrl_dent;
553struct dentry *ctrl_dfile;
554static void ctrl_bridge_debugfs_init(void)
555{
556 ctrl_dent = debugfs_create_dir("ctrl_hsic_bridge", 0);
557 if (IS_ERR(ctrl_dent))
558 return;
559
560 ctrl_dfile =
561 debugfs_create_file("status", 0644, ctrl_dent, 0,
562 &ctrl_stats_ops);
563 if (!ctrl_dfile || IS_ERR(ctrl_dfile))
564 debugfs_remove(ctrl_dent);
565}
566
567static void ctrl_bridge_debugfs_exit(void)
568{
569 debugfs_remove(ctrl_dfile);
570 debugfs_remove(ctrl_dent);
571}
572
573#else
574static void ctrl_bridge_debugfs_init(void) { }
575static void ctrl_bridge_debugfs_exit(void) { }
576#endif
577
578int
579ctrl_bridge_probe(struct usb_interface *ifc, struct usb_host_endpoint *int_in,
580 int id)
581{
582 struct ctrl_bridge *dev;
583 struct usb_device *udev;
584 struct usb_endpoint_descriptor *ep;
585 u16 wMaxPacketSize;
586 int retval = 0;
587 int interval;
588
589 udev = interface_to_usbdev(ifc);
590
591 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
592 if (!dev) {
593 dev_err(&udev->dev, "%s: unable to allocate dev\n",
594 __func__);
595 return -ENOMEM;
596 }
597 dev->pdev = platform_device_alloc(ctrl_bridge_names[id], id);
598 if (!dev->pdev) {
599 dev_err(&dev->udev->dev,
600 "%s: unable to allocate platform device\n", __func__);
601 retval = -ENOMEM;
602 goto nomem;
603 }
604
605 dev->udev = udev;
606 dev->int_pipe = usb_rcvintpipe(udev,
607 int_in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
608 dev->intf = ifc;
609
610 init_usb_anchor(&dev->tx_submitted);
611
612 /*use max pkt size from ep desc*/
613 ep = &dev->intf->cur_altsetting->endpoint[0].desc;
614
615 dev->inturb = usb_alloc_urb(0, GFP_KERNEL);
616 if (!dev->inturb) {
617 dev_err(&udev->dev, "%s: error allocating int urb\n", __func__);
618 retval = -ENOMEM;
619 goto pdev_del;
620 }
621
622 wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
623
624 dev->intbuf = kmalloc(wMaxPacketSize, GFP_KERNEL);
625 if (!dev->intbuf) {
626 dev_err(&udev->dev, "%s: error allocating int buffer\n",
627 __func__);
628 retval = -ENOMEM;
629 goto free_inturb;
630 }
631
632 interval =
633 (udev->speed == USB_SPEED_HIGH) ? HS_INTERVAL : FS_LS_INTERVAL;
634
635 usb_fill_int_urb(dev->inturb, udev, dev->int_pipe,
636 dev->intbuf, wMaxPacketSize,
637 notification_available_cb, dev, interval);
638
639 dev->readurb = usb_alloc_urb(0, GFP_KERNEL);
640 if (!dev->readurb) {
641 dev_err(&udev->dev, "%s: error allocating read urb\n",
642 __func__);
643 retval = -ENOMEM;
644 goto free_intbuf;
645 }
646
647 dev->readbuf = kmalloc(DEFAULT_READ_URB_LENGTH, GFP_KERNEL);
648 if (!dev->readbuf) {
649 dev_err(&udev->dev, "%s: error allocating read buffer\n",
650 __func__);
651 retval = -ENOMEM;
652 goto free_rurb;
653 }
654
655 dev->in_ctlreq = kmalloc(sizeof(*dev->in_ctlreq), GFP_KERNEL);
656 if (!dev->in_ctlreq) {
657 dev_err(&udev->dev,
658 "%s:error allocating setup packet buffer\n",
659 __func__);
660 retval = -ENOMEM;
661 goto free_rbuf;
662 }
663
664 dev->in_ctlreq->bRequestType =
665 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
666 dev->in_ctlreq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
667 dev->in_ctlreq->wValue = 0;
668 dev->in_ctlreq->wIndex =
669 dev->intf->cur_altsetting->desc.bInterfaceNumber;
670 dev->in_ctlreq->wLength = cpu_to_le16(DEFAULT_READ_URB_LENGTH);
671
672 __dev[id] = dev;
673
674 platform_device_add(dev->pdev);
675
676 ch_id++;
677
678 return retval;
679
680free_rbuf:
681 kfree(dev->readbuf);
682free_rurb:
683 usb_free_urb(dev->readurb);
684free_intbuf:
685 kfree(dev->intbuf);
686free_inturb:
687 usb_free_urb(dev->inturb);
688pdev_del:
689 platform_device_del(dev->pdev);
690nomem:
691 kfree(dev);
692
693 return retval;
694}
695
696void ctrl_bridge_disconnect(unsigned int id)
697{
698 struct ctrl_bridge *dev = __dev[id];
699
700 dev_dbg(&dev->udev->dev, "%s:\n", __func__);
701
Jack Pham452c6922011-12-14 18:15:26 -0800702 platform_device_del(dev->pdev);
703
Hemant Kumar14401d52011-11-03 16:40:32 -0700704 kfree(dev->in_ctlreq);
705 kfree(dev->readbuf);
706 kfree(dev->intbuf);
707
708 usb_free_urb(dev->readurb);
709 usb_free_urb(dev->inturb);
710
Hemant Kumar14401d52011-11-03 16:40:32 -0700711 __dev[id] = NULL;
712 ch_id--;
713
714 kfree(dev);
715}
716
717static int __init ctrl_bridge_init(void)
718{
719 ctrl_bridge_debugfs_init();
720
721 return 0;
722}
723module_init(ctrl_bridge_init);
724
725static void __exit ctrl_bridge_exit(void)
726{
727 ctrl_bridge_debugfs_exit();
728}
729module_exit(ctrl_bridge_exit);
730
731MODULE_DESCRIPTION("Qualcomm modem control bridge driver");
732MODULE_LICENSE("GPL v2");