blob: c0a20b05584f189e8ab8fce5a7a175157db68544 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * u_smd.c - utilities for USB gadget serial over smd
3 *
4 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
5 *
6 * This code also borrows from drivers/usb/gadget/u_serial.c, which is
7 * Copyright (C) 2000 - 2003 Al Borchers (alborchers@steinerpoint.com)
8 * Copyright (C) 2008 David Brownell
9 * Copyright (C) 2008 by Nokia Corporation
10 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
11 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 and
15 * only version 2 as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 */
22#include <linux/kernel.h>
23#include <linux/interrupt.h>
24#include <linux/device.h>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/termios.h>
28#include <mach/msm_smd.h>
29#include <linux/debugfs.h>
30
31#include "u_serial.h"
32
33#define SMD_RX_QUEUE_SIZE 8
34#define SMD_RX_BUF_SIZE 2048
35
36#define SMD_TX_QUEUE_SIZE 8
37#define SMD_TX_BUF_SIZE 2048
38
39static struct workqueue_struct *gsmd_wq;
40
41#define SMD_N_PORTS 2
42#define CH_OPENED 0
43struct smd_port_info {
44 struct smd_channel *ch;
45 char *name;
46 unsigned long flags;
47 wait_queue_head_t wait;
48};
49
50struct smd_port_info smd_pi[SMD_N_PORTS] = {
51 {
52 .name = "DS",
53 },
54 {
55 .name = "UNUSED",
56 },
57};
58
59struct gsmd_port {
60 unsigned port_num;
61 spinlock_t port_lock;
62
63 unsigned n_read;
64 struct list_head read_pool;
65 struct list_head read_queue;
66 struct work_struct push;
67
68 struct list_head write_pool;
69 struct work_struct pull;
70
71 struct gserial *port_usb;
72
73 struct smd_port_info *pi;
74 struct work_struct connect_work;
75
76 /* At present, smd does not notify
77 * control bit change info from modem
78 */
79 struct work_struct update_modem_ctrl_sig;
80
81#define SMD_ACM_CTRL_DTR 0x01
82#define SMD_ACM_CTRL_RTS 0x02
83 unsigned cbits_to_modem;
84
85#define SMD_ACM_CTRL_DCD 0x01
86#define SMD_ACM_CTRL_DSR 0x02
87#define SMD_ACM_CTRL_BRK 0x04
88#define SMD_ACM_CTRL_RI 0x08
89 unsigned cbits_to_laptop;
90
91 /* pkt counters */
92 unsigned long nbytes_tomodem;
93 unsigned long nbytes_tolaptop;
94};
95
96static struct smd_portmaster {
97 struct mutex lock;
98 struct gsmd_port *port;
99} smd_ports[SMD_N_PORTS];
100static unsigned n_smd_ports;
101
102static void gsmd_free_req(struct usb_ep *ep, struct usb_request *req)
103{
104 kfree(req->buf);
105 usb_ep_free_request(ep, req);
106}
107
108static void gsmd_free_requests(struct usb_ep *ep, struct list_head *head)
109{
110 struct usb_request *req;
111
112 while (!list_empty(head)) {
113 req = list_entry(head->next, struct usb_request, list);
114 list_del(&req->list);
115 gsmd_free_req(ep, req);
116 }
117}
118
119static struct usb_request *
120gsmd_alloc_req(struct usb_ep *ep, unsigned len, gfp_t flags)
121{
122 struct usb_request *req;
123
124 req = usb_ep_alloc_request(ep, flags);
125 if (!req) {
126 pr_err("%s: usb alloc request failed\n", __func__);
127 return 0;
128 }
129
130 req->length = len;
131 req->buf = kmalloc(len, flags);
132 if (!req->buf) {
133 pr_err("%s: request buf allocation failed\n", __func__);
134 usb_ep_free_request(ep, req);
135 return 0;
136 }
137
138 return req;
139}
140
141static int gsmd_alloc_requests(struct usb_ep *ep, struct list_head *head,
142 int num, int size,
143 void (*cb)(struct usb_ep *ep, struct usb_request *))
144{
145 int i;
146 struct usb_request *req;
147
148 pr_debug("%s: ep:%p head:%p num:%d size:%d cb:%p", __func__,
149 ep, head, num, size, cb);
150
151 for (i = 0; i < num; i++) {
152 req = gsmd_alloc_req(ep, size, GFP_ATOMIC);
153 if (!req) {
154 pr_debug("%s: req allocated:%d\n", __func__, i);
155 return list_empty(head) ? -ENOMEM : 0;
156 }
157 req->complete = cb;
158 list_add(&req->list, head);
159 }
160
161 return 0;
162}
163
164static void gsmd_start_rx(struct gsmd_port *port)
165{
166 struct list_head *pool;
167 struct usb_ep *out;
168 int ret;
169
170 if (!port) {
171 pr_err("%s: port is null\n", __func__);
172 return;
173 }
174
175 spin_lock_irq(&port->port_lock);
176
177 if (!port->port_usb) {
178 pr_debug("%s: USB disconnected\n", __func__);
179 goto start_rx_end;
180 }
181
182 pool = &port->read_pool;
183 out = port->port_usb->out;
184
185 while (!list_empty(pool)) {
186 struct usb_request *req;
187
188 req = list_entry(pool->next, struct usb_request, list);
189 list_del(&req->list);
190 req->length = SMD_RX_BUF_SIZE;
191
192 spin_unlock_irq(&port->port_lock);
193 ret = usb_ep_queue(out, req, GFP_KERNEL);
194 spin_lock_irq(&port->port_lock);
195 if (ret) {
196 pr_err("%s: usb ep out queue failed"
197 "port:%p, port#%d\n",
198 __func__, port, port->port_num);
199 list_add_tail(&req->list, pool);
200 break;
201 }
202 }
203start_rx_end:
204 spin_unlock_irq(&port->port_lock);
205}
206
207static void gsmd_rx_push(struct work_struct *w)
208{
209 struct gsmd_port *port = container_of(w, struct gsmd_port, push);
210 struct list_head *q;
211
212 pr_debug("%s: port:%p port#%d", __func__, port, port->port_num);
213
214 spin_lock_irq(&port->port_lock);
215
216 q = &port->read_queue;
217 while (!list_empty(q)) {
218 struct usb_request *req;
219 int avail;
220 struct smd_port_info *pi = port->pi;
221
222 req = list_first_entry(q, struct usb_request, list);
223
224 switch (req->status) {
225 case -ESHUTDOWN:
226 pr_debug("%s: req status shutdown portno#%d port:%p\n",
227 __func__, port->port_num, port);
228 goto rx_push_end;
229 default:
230 pr_warning("%s: port:%p port#%d"
231 " Unexpected Rx Status:%d\n", __func__,
232 port, port->port_num, req->status);
233 case 0:
234 /* normal completion */
235 break;
236 }
237
238 avail = smd_write_avail(pi->ch);
239 if (!avail)
240 goto rx_push_end;
241
242 if (req->actual) {
243 char *packet = req->buf;
244 unsigned size = req->actual;
245 unsigned n;
246 unsigned count;
247
248 n = port->n_read;
249 if (n) {
250 packet += n;
251 size -= n;
252 }
253
254 count = smd_write(pi->ch, packet, size);
255 if (count < 0) {
256 pr_err("%s: smd write failed err:%d\n",
257 __func__, count);
258 goto rx_push_end;
259 }
260
261 if (count != size) {
262 port->n_read += count;
263 goto rx_push_end;
264 }
265
266 port->nbytes_tomodem += count;
267 }
268
269 port->n_read = 0;
270 list_move(&req->list, &port->read_pool);
271 }
272
273rx_push_end:
274 spin_unlock_irq(&port->port_lock);
275
276 gsmd_start_rx(port);
277}
278
279static void gsmd_read_pending(struct gsmd_port *port)
280{
281 int avail;
282
283 if (!port || !port->pi->ch)
284 return;
285
286 /* passing null buffer discards the data */
287 while ((avail = smd_read_avail(port->pi->ch)))
288 smd_read(port->pi->ch, 0, avail);
289
290 return;
291}
292
293static void gsmd_tx_pull(struct work_struct *w)
294{
295 struct gsmd_port *port = container_of(w, struct gsmd_port, pull);
296 struct list_head *pool = &port->write_pool;
297
298 pr_debug("%s: port:%p port#%d pool:%p\n", __func__,
299 port, port->port_num, pool);
300
301 if (!port->port_usb) {
302 pr_debug("%s: usb is disconnected\n", __func__);
303 gsmd_read_pending(port);
304 return;
305 }
306
307 spin_lock_irq(&port->port_lock);
308 while (!list_empty(pool)) {
309 struct usb_request *req;
310 struct usb_ep *in = port->port_usb->in;
311 struct smd_port_info *pi = port->pi;
312 int avail;
313 int ret;
314
315 avail = smd_read_avail(pi->ch);
316 if (!avail)
317 break;
318
319 avail = avail > SMD_TX_BUF_SIZE ? SMD_TX_BUF_SIZE : avail;
320
321 req = list_entry(pool->next, struct usb_request, list);
322 list_del(&req->list);
323 req->length = smd_read(pi->ch, req->buf, avail);
324
325 spin_unlock_irq(&port->port_lock);
326 ret = usb_ep_queue(in, req, GFP_KERNEL);
327 spin_lock_irq(&port->port_lock);
328 if (ret) {
329 pr_err("%s: usb ep out queue failed"
330 "port:%p, port#%d err:%d\n",
331 __func__, port, port->port_num, ret);
332 /* could be usb disconnected */
333 if (!port->port_usb)
334 gsmd_free_req(in, req);
335 else
336 list_add(&req->list, pool);
337 goto tx_pull_end;
338 }
339
340 port->nbytes_tolaptop += req->length;
341 }
342
343tx_pull_end:
344 /* TBD: Check how code behaves on USB bus suspend */
345 if (port->port_usb && smd_read_avail(port->pi->ch) && !list_empty(pool))
346 queue_work(gsmd_wq, &port->pull);
347
348 spin_unlock_irq(&port->port_lock);
349
350 return;
351}
352
353static void gsmd_read_complete(struct usb_ep *ep, struct usb_request *req)
354{
355 struct gsmd_port *port = ep->driver_data;
356 unsigned long flags;
357
358 pr_debug("%s: ep:%p port:%p\n", __func__, ep, port);
359
360 if (!port) {
361 pr_err("%s: port is null\n", __func__);
362 return;
363 }
364
365 spin_lock_irqsave(&port->port_lock, flags);
366 list_add_tail(&req->list, &port->read_queue);
367 queue_work(gsmd_wq, &port->push);
368 spin_unlock_irqrestore(&port->port_lock, flags);
369
370 return;
371}
372
373static void gsmd_write_complete(struct usb_ep *ep, struct usb_request *req)
374{
375 struct gsmd_port *port = ep->driver_data;
376 unsigned long flags;
377
378 pr_debug("%s: ep:%p port:%p\n", __func__, ep, port);
379
380 if (!port) {
381 pr_err("%s: port is null\n", __func__);
382 return;
383 }
384
385 spin_lock_irqsave(&port->port_lock, flags);
386 list_add(&req->list, &port->write_pool);
387
388 switch (req->status) {
389 default:
390 pr_warning("%s: port:%p port#%d unexpected %s status %d\n",
391 __func__, port, port->port_num,
392 ep->name, req->status);
393 /* FALL THROUGH */
394 case 0:
395 queue_work(gsmd_wq, &port->pull);
396 break;
397
398 case -ESHUTDOWN:
399 /* disconnect */
400 pr_debug("%s: %s shutdown\n", __func__, ep->name);
401 gsmd_free_req(ep, req);
402 break;
403 }
404
405 spin_unlock_irqrestore(&port->port_lock, flags);
406
407 return;
408}
409
410static void gsmd_start_io(struct gsmd_port *port)
411{
412 int ret = -ENODEV;
413 unsigned long flags;
414
415 pr_debug("%s: port: %p\n", __func__, port);
416
417 spin_lock_irqsave(&port->port_lock, flags);
418
419 if (!port->port_usb)
420 goto start_io_out;
421
422 ret = gsmd_alloc_requests(port->port_usb->out,
423 &port->read_pool,
424 SMD_RX_QUEUE_SIZE, SMD_RX_BUF_SIZE,
425 gsmd_read_complete);
426 if (ret) {
427 pr_err("%s: unable to allocate out requests\n",
428 __func__);
429 goto start_io_out;
430 }
431
432 ret = gsmd_alloc_requests(port->port_usb->in,
433 &port->write_pool,
434 SMD_TX_QUEUE_SIZE, SMD_TX_BUF_SIZE,
435 gsmd_write_complete);
436 if (ret) {
437 gsmd_free_requests(port->port_usb->out, &port->read_pool);
438 pr_err("%s: unable to allocate IN requests\n",
439 __func__);
440 goto start_io_out;
441 }
442
443start_io_out:
444 spin_unlock_irqrestore(&port->port_lock, flags);
445
446 if (ret)
447 return;
448
449 gsmd_start_rx(port);
450}
451
452static unsigned int convert_uart_sigs_to_acm(unsigned uart_sig)
453{
454 unsigned int acm_sig = 0;
455
456 /* should this needs to be in calling functions ??? */
457 uart_sig &= (TIOCM_RI | TIOCM_CD | TIOCM_DSR);
458
459 if (uart_sig & TIOCM_RI)
460 acm_sig |= SMD_ACM_CTRL_RI;
461 if (uart_sig & TIOCM_CD)
462 acm_sig |= SMD_ACM_CTRL_DCD;
463 if (uart_sig & TIOCM_DSR)
464 acm_sig |= SMD_ACM_CTRL_DSR;
465
466 return acm_sig;
467}
468
469static unsigned int convert_acm_sigs_to_uart(unsigned acm_sig)
470{
471 unsigned int uart_sig = 0;
472
473 /* should this needs to be in calling functions ??? */
474 acm_sig &= (SMD_ACM_CTRL_DTR | SMD_ACM_CTRL_RTS);
475
476 if (acm_sig & SMD_ACM_CTRL_DTR)
477 uart_sig |= TIOCM_DTR;
478 if (acm_sig & SMD_ACM_CTRL_RTS)
479 uart_sig |= TIOCM_RTS;
480
481 return uart_sig;
482}
483
484static void gsmd_notify(void *priv, unsigned event)
485{
486 struct gsmd_port *port = priv;
487 struct smd_port_info *pi = port->pi;
488 int i;
489
490 switch (event) {
491 case SMD_EVENT_DATA:
492 pr_debug("%s: Event data\n", __func__);
493 if (smd_read_avail(pi->ch))
494 queue_work(gsmd_wq, &port->pull);
495 if (smd_write_avail(pi->ch))
496 queue_work(gsmd_wq, &port->push);
497 break;
498 case SMD_EVENT_OPEN:
499 pr_debug("%s: Event Open\n", __func__);
500 set_bit(CH_OPENED, &pi->flags);
501 wake_up(&pi->wait);
502 break;
503 case SMD_EVENT_CLOSE:
504 pr_debug("%s: Event Close\n", __func__);
505 clear_bit(CH_OPENED, &pi->flags);
506 break;
507 case SMD_EVENT_STATUS:
508 i = smd_tiocmget(port->pi->ch);
509 port->cbits_to_laptop = convert_uart_sigs_to_acm(i);
510 if (port->port_usb && port->port_usb->send_modem_ctrl_bits)
511 port->port_usb->send_modem_ctrl_bits(port->port_usb,
512 port->cbits_to_laptop);
513 break;
514 }
515}
516
517#define MAX_SMD_RETRY_CNT 20
518static void gsmd_connect_work(struct work_struct *w)
519{
520 struct gsmd_port *port;
521 struct smd_port_info *pi;
522 int ret;
523 int retry_cnt = 0;
524
525 port = container_of(w, struct gsmd_port, connect_work);
526 pi = port->pi;
527
528 pr_debug("%s: port:%p port#%d\n", __func__, port, port->port_num);
529
530 /* SMD driver comes online gets initialized and loads modem
531 * 10 seconds after boot up. If USB cable is connected at boot-up,
532 * this might result smd open failure. To work-around, retry
533 * opening multiple times.
534 */
535 do {
536 if (!port->port_usb)
537 return;
538
539 ret = smd_named_open_on_edge(pi->name, SMD_APPS_MODEM,
540 &pi->ch, port, gsmd_notify);
541 if (!ret)
542 break;
543
544 retry_cnt++;
545 msleep(1000);
546 } while (retry_cnt < MAX_SMD_RETRY_CNT);
547
548 if (ret) {
549 pr_err("%s: unable to open smd port:%s err:%d\n",
550 __func__, pi->name, ret);
551 return;
552 }
553
554 pr_debug("%s: SMD port open successful retrycnt:%d\n",
555 __func__, retry_cnt);
556
557 wait_event(pi->wait, test_bit(CH_OPENED, &pi->flags));
558
559 if (!port->port_usb)
560 return;
561
562 /* update usb control signals to modem */
563 if (port->cbits_to_modem)
564 smd_tiocmset(port->pi->ch,
565 port->cbits_to_modem,
566 ~port->cbits_to_modem);
567
568 gsmd_start_io(port);
569}
570
571static void gsmd_notify_modem(struct gserial *gser, u8 portno, int ctrl_bits)
572{
573 struct gsmd_port *port;
574 int temp;
575
576 if (portno >= n_smd_ports) {
577 pr_err("%s: invalid portno#%d\n", __func__, portno);
578 return;
579 }
580
581 if (!gser) {
582 pr_err("%s: gser is null\n", __func__);
583 return;
584 }
585
586 port = smd_ports[portno].port;
587
588 temp = convert_acm_sigs_to_uart(ctrl_bits);
589
590 if (temp == port->cbits_to_modem)
591 return;
592
593 port->cbits_to_modem = temp;
594
595 /* usb could send control signal before smd is ready */
596 if (!test_bit(CH_OPENED, &port->pi->flags))
597 return;
598
599 /* if DTR is high, update latest modem info to laptop */
600 if (port->cbits_to_modem & TIOCM_DTR) {
601 unsigned i;
602
603 i = smd_tiocmget(port->pi->ch);
604 port->cbits_to_laptop = convert_uart_sigs_to_acm(i);
605
606 if (gser->send_modem_ctrl_bits)
607 gser->send_modem_ctrl_bits(
608 port->port_usb,
609 port->cbits_to_laptop);
610 }
611
612 smd_tiocmset(port->pi->ch,
613 port->cbits_to_modem,
614 ~port->cbits_to_modem);
615}
616
617int gsmd_connect(struct gserial *gser, u8 portno)
618{
619 unsigned long flags;
620 int ret;
621 struct gsmd_port *port;
622
623 pr_debug("%s: gserial:%p portno:%u\n", __func__, gser, portno);
624
625 if (portno >= n_smd_ports) {
626 pr_err("%s: Invalid port no#%d", __func__, portno);
627 return -EINVAL;
628 }
629
630 if (!gser) {
631 pr_err("%s: gser is null\n", __func__);
632 return -EINVAL;
633 }
634
635 port = smd_ports[portno].port;
636
637 spin_lock_irqsave(&port->port_lock, flags);
638 port->port_usb = gser;
639 gser->notify_modem = gsmd_notify_modem;
640 port->nbytes_tomodem = 0;
641 port->nbytes_tolaptop = 0;
642 spin_unlock_irqrestore(&port->port_lock, flags);
643
644 ret = usb_ep_enable(gser->in, gser->in_desc);
645 if (ret) {
646 pr_err("%s: usb_ep_enable failed eptype:IN ep:%p",
647 __func__, gser->in);
648 port->port_usb = 0;
649 return ret;
650 }
651 gser->in->driver_data = port;
652
653 ret = usb_ep_enable(gser->out, gser->out_desc);
654 if (ret) {
655 pr_err("%s: usb_ep_enable failed eptype:OUT ep:%p",
656 __func__, gser->out);
657 port->port_usb = 0;
658 gser->in->driver_data = 0;
659 return ret;
660 }
661 gser->out->driver_data = port;
662
663 queue_work(gsmd_wq, &port->connect_work);
664
665 return 0;
666}
667
668void gsmd_disconnect(struct gserial *gser, u8 portno)
669{
670 unsigned long flags;
671 struct gsmd_port *port;
672
673 pr_debug("%s: gserial:%p portno:%u\n", __func__, gser, portno);
674
675 if (portno >= n_smd_ports) {
676 pr_err("%s: invalid portno#%d\n", __func__, portno);
677 return;
678 }
679
680 if (!gser) {
681 pr_err("%s: gser is null\n", __func__);
682 return;
683 }
684
685 port = smd_ports[portno].port;
686
687 spin_lock_irqsave(&port->port_lock, flags);
688 port->port_usb = 0;
689 spin_unlock_irqrestore(&port->port_lock, flags);
690
691 /* disable endpoints, aborting down any active I/O */
692 usb_ep_disable(gser->out);
693 usb_ep_disable(gser->in);
694
695 spin_lock_irqsave(&port->port_lock, flags);
696 gsmd_free_requests(gser->out, &port->read_pool);
697 gsmd_free_requests(gser->out, &port->read_queue);
698 gsmd_free_requests(gser->in, &port->write_pool);
699 port->n_read = 0;
700 spin_unlock_irqrestore(&port->port_lock, flags);
701
702 if (!test_bit(CH_OPENED, &port->pi->flags))
703 return;
704
705 /* lower the dtr */
706 port->cbits_to_modem = 0;
707 smd_tiocmset(port->pi->ch,
708 port->cbits_to_modem,
709 ~port->cbits_to_modem);
710
711 smd_close(port->pi->ch);
712 port->pi->flags = 0;
713}
714
715static void gsmd_port_free(int portno)
716{
717 struct gsmd_port *port = smd_ports[portno].port;
718
719 if (!port)
720 kfree(port);
721}
722
723static int gsmd_port_alloc(int portno, struct usb_cdc_line_coding *coding)
724{
725 struct gsmd_port *port;
726
727 port = kzalloc(sizeof(struct gsmd_port), GFP_KERNEL);
728 if (!port)
729 return -ENOMEM;
730
731 port->port_num = portno;
732 port->pi = &smd_pi[portno];
733
734 spin_lock_init(&port->port_lock);
735
736 INIT_LIST_HEAD(&port->read_pool);
737 INIT_LIST_HEAD(&port->read_queue);
738 INIT_WORK(&port->push, gsmd_rx_push);
739
740 INIT_LIST_HEAD(&port->write_pool);
741 INIT_WORK(&port->pull, gsmd_tx_pull);
742
743 INIT_WORK(&port->connect_work, gsmd_connect_work);
744 init_waitqueue_head(&port->pi->wait);
745
746 smd_ports[portno].port = port;
747
748 pr_debug("%s: port:%p portno:%d\n", __func__, port, portno);
749
750 return 0;
751}
752
753#if defined(CONFIG_DEBUG_FS)
754static ssize_t debug_smd_read_stats(struct file *file, char __user *ubuf,
755 size_t count, loff_t *ppos)
756{
757 struct gsmd_port *port;
758 char *buf;
759 unsigned long flags;
760 int temp = 0;
761 int i;
762 int ret;
763
764 buf = kzalloc(sizeof(char) * 512, GFP_KERNEL);
765 if (!buf)
766 return -ENOMEM;
767
768 for (i = 0; i < n_smd_ports; i++) {
769 port = smd_ports[i].port;
770 spin_lock_irqsave(&port->port_lock, flags);
771 temp += scnprintf(buf + temp, 512 - temp,
772 "###PORT:%d###\n"
773 "nbytes_tolaptop: %lu\n"
774 "nbytes_tomodem: %lu\n"
775 "cbits_to_modem: %u\n"
776 "cbits_to_laptop: %u\n"
777 "n_read: %u\n",
778 i, port->nbytes_tolaptop, port->nbytes_tomodem,
779 port->cbits_to_modem, port->cbits_to_laptop,
780 port->n_read);
781 spin_unlock_irqrestore(&port->port_lock, flags);
782 }
783
784 ret = simple_read_from_buffer(ubuf, count, ppos, buf, temp);
785
786 kfree(buf);
787
788 return ret;
789
790}
791
792static ssize_t debug_smd_reset_stats(struct file *file, const char __user *buf,
793 size_t count, loff_t *ppos)
794{
795 struct gsmd_port *port;
796 unsigned long flags;
797 int i;
798
799 for (i = 0; i < n_smd_ports; i++) {
800 port = smd_ports[i].port;
801
802 spin_lock_irqsave(&port->port_lock, flags);
803 port->nbytes_tolaptop = 0;
804 port->nbytes_tomodem = 0;
805 spin_unlock_irqrestore(&port->port_lock, flags);
806 }
807
808 return count;
809}
810
811static int debug_smd_open(struct inode *inode, struct file *file)
812{
813 return 0;
814}
815
816static const struct file_operations debug_gsmd_ops = {
817 .open = debug_smd_open,
818 .read = debug_smd_read_stats,
819 .write = debug_smd_reset_stats,
820};
821
822static void gsmd_debugfs_init(void)
823{
824 struct dentry *dent;
825
826 dent = debugfs_create_dir("usb_gsmd", 0);
827 if (IS_ERR(dent))
828 return;
829
830 debugfs_create_file("status", 0444, dent, 0, &debug_gsmd_ops);
831}
832#else
833static void gsmd_debugfs_init(void) {}
834#endif
835
836int gsmd_setup(struct usb_gadget *g, unsigned count)
837{
838 struct usb_cdc_line_coding coding;
839 int ret;
840 int i;
841
842 pr_debug("%s: g:%p count: %d\n", __func__, g, count);
843
844 if (!count || count > SMD_N_PORTS) {
845 pr_err("%s: Invalid num of ports count:%d gadget:%p\n",
846 __func__, count, g);
847 return -EINVAL;
848 }
849
850 coding.dwDTERate = cpu_to_le32(9600);
851 coding.bCharFormat = 8;
852 coding.bParityType = USB_CDC_NO_PARITY;
853 coding.bDataBits = USB_CDC_1_STOP_BITS;
854
855 gsmd_wq = create_singlethread_workqueue("k_gsmd");
856 if (!gsmd_wq) {
857 pr_err("%s: Unable to create workqueue gsmd_wq\n",
858 __func__);
859 return -ENOMEM;
860 }
861
862 for (i = 0; i < count; i++) {
863 mutex_init(&smd_ports[i].lock);
Manu Gautam4bd21422011-09-09 14:55:32 +0530864 n_smd_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865 ret = gsmd_port_alloc(i, &coding);
866 if (ret) {
Manu Gautam4bd21422011-09-09 14:55:32 +0530867 n_smd_ports--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700868 pr_err("%s: Unable to alloc port:%d\n", __func__, i);
869 goto free_smd_ports;
870 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 }
872
873 gsmd_debugfs_init();
874
875 return 0;
876free_smd_ports:
877 for (i = 0; i < n_smd_ports; i++)
878 gsmd_port_free(i);
879
880 destroy_workqueue(gsmd_wq);
881
882 return ret;
883}
884
885void gsmd_cleanup(struct usb_gadget *g, unsigned count)
886{
887 /* TBD */
888}