blob: 7102d815963fc15d1d506f92de9e60ebc939c6ba [file] [log] [blame]
Chiranjeevi Velempatia3cf1982012-05-15 18:15:26 +05301/* Copyright (c) 2012, 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/interrupt.h>
15#include <linux/device.h>
16#include <linux/delay.h>
17#include <linux/slab.h>
18#include <linux/termios.h>
19#include <linux/debugfs.h>
20#include <linux/smux.h>
21
22#include <mach/usb_gadget_xport.h>
23
24#define CH_OPENED 0
25#define CH_READY 1
26
27static unsigned int num_ctrl_ports;
28
29static const char *ghsuart_ctrl_names[] = {
30 "SMUX_RMNET_CTL_HSUART"
31};
32
33struct ghsuart_ctrl_port {
34 /* port */
35 unsigned port_num;
36 /* gadget */
37 enum gadget_type gtype;
38 spinlock_t port_lock;
39 void *port_usb;
40 /* work queue*/
41 struct workqueue_struct *wq;
42 struct work_struct connect_w;
43 struct work_struct disconnect_w;
44 /*ctrl pkt response cb*/
45 int (*send_cpkt_response)(void *g, void *buf, size_t len);
46 void *ctxt;
47 unsigned int ch_id;
48 /* flow control bits */
49 unsigned long flags;
50 int (*send_pkt)(void *, void *, size_t actual);
51 /* Channel status */
52 unsigned long channel_sts;
53 /* control bits */
54 unsigned cbits_tomodem;
55 /* counters */
56 unsigned long to_modem;
57 unsigned long to_host;
58 unsigned long drp_cpkt_cnt;
59};
60
61static struct {
62 struct ghsuart_ctrl_port *port;
63 struct platform_driver pdrv;
64} ghsuart_ctrl_ports[NUM_HSUART_PORTS];
65
66static int ghsuart_ctrl_receive(void *dev, void *buf, size_t actual);
67
68static void smux_control_event(void *priv, int event_type, const void *metadata)
69{
70 struct grmnet *gr = NULL;
71 struct ghsuart_ctrl_port *port = priv;
72 void *buf;
73 unsigned long flags;
74 size_t len;
75
76 switch (event_type) {
77 case SMUX_CONNECTED:
78 spin_lock_irqsave(&port->port_lock, flags);
79 if (!port->port_usb) {
80 spin_unlock_irqrestore(&port->port_lock, flags);
81 return;
82 }
83 spin_unlock_irqrestore(&port->port_lock, flags);
84 set_bit(CH_OPENED, &port->channel_sts);
85 if (port->gtype == USB_GADGET_RMNET) {
86 gr = port->port_usb;
87 if (gr && gr->connect)
88 gr->connect(gr);
89 }
90 break;
91 case SMUX_DISCONNECTED:
92 clear_bit(CH_OPENED, &port->channel_sts);
93 break;
94 case SMUX_READ_DONE:
95 len = ((struct smux_meta_read *)metadata)->len;
96 buf = ((struct smux_meta_read *)metadata)->buffer;
97 ghsuart_ctrl_receive(port, buf, len);
98 break;
99 case SMUX_READ_FAIL:
100 buf = ((struct smux_meta_read *)metadata)->buffer;
101 kfree(buf);
102 break;
103 case SMUX_WRITE_DONE:
104 case SMUX_WRITE_FAIL:
105 buf = ((struct smux_meta_write *)metadata)->buffer;
106 kfree(buf);
107 break;
108 case SMUX_LOW_WM_HIT:
109 case SMUX_HIGH_WM_HIT:
110 case SMUX_TIOCM_UPDATE:
111 break;
112 default:
113 pr_err("%s Event %d not supported\n", __func__, event_type);
114 };
115}
116
117static int rx_control_buffer(void *priv, void **pkt_priv, void **buffer,
118 int size)
119{
120 void *rx_buf;
121
122 rx_buf = kmalloc(size, GFP_KERNEL);
123 if (!rx_buf)
124 return -EAGAIN;
125 *buffer = rx_buf;
126 *pkt_priv = NULL;
127
128 return 0;
129}
130
131static int ghsuart_ctrl_receive(void *dev, void *buf, size_t actual)
132{
133 struct ghsuart_ctrl_port *port = dev;
134 int retval = 0;
135
136 pr_debug_ratelimited("%s: read complete bytes read: %d\n",
137 __func__, actual);
138
139 /* send it to USB here */
140 if (port && port->send_cpkt_response) {
141 retval = port->send_cpkt_response(port->port_usb, buf, actual);
142 port->to_host++;
143 }
144 kfree(buf);
145 return retval;
146}
147
148static int
149ghsuart_send_cpkt_tomodem(u8 portno, void *buf, size_t len)
150{
151 void *cbuf;
152 struct ghsuart_ctrl_port *port;
153 int ret;
154
155 if (portno >= num_ctrl_ports) {
156 pr_err("%s: Invalid portno#%d\n", __func__, portno);
157 return -ENODEV;
158 }
159
160 port = ghsuart_ctrl_ports[portno].port;
161 if (!port) {
162 pr_err("%s: port is null\n", __func__);
163 return -ENODEV;
164 }
165 /* drop cpkt if ch is not open */
166 if (!test_bit(CH_OPENED, &port->channel_sts)) {
167 port->drp_cpkt_cnt++;
168 return 0;
169 }
170 cbuf = kmalloc(len, GFP_ATOMIC);
171 if (!cbuf)
172 return -ENOMEM;
173
174 memcpy(cbuf, buf, len);
175
176 pr_debug("%s: ctrl_pkt:%d bytes\n", __func__, len);
177
178 ret = msm_smux_write(port->ch_id, port, (void *)cbuf, len);
179 if (ret < 0) {
180 pr_err_ratelimited("%s: write error:%d\n",
181 __func__, ret);
182 port->drp_cpkt_cnt++;
183 kfree(cbuf);
184 return ret;
185 }
186 port->to_modem++;
187
188 return 0;
189}
190
191static void
192ghsuart_send_cbits_tomodem(void *gptr, u8 portno, int cbits)
193{
194 struct ghsuart_ctrl_port *port;
195
196 if (portno >= num_ctrl_ports || !gptr) {
197 pr_err("%s: Invalid portno#%d\n", __func__, portno);
198 return;
199 }
200
201 port = ghsuart_ctrl_ports[portno].port;
202 if (!port) {
203 pr_err("%s: port is null\n", __func__);
204 return;
205 }
206
207 if (cbits == port->cbits_tomodem)
208 return;
209
210 port->cbits_tomodem = cbits;
211
212 if (!test_bit(CH_OPENED, &port->channel_sts))
213 return;
214
215 pr_debug("%s: ctrl_tomodem:%d\n", __func__, cbits);
216 /* Send the control bits to the Modem */
217 msm_smux_tiocm_set(port->ch_id, cbits, ~cbits);
218}
219
220static void ghsuart_ctrl_connect_w(struct work_struct *w)
221{
222 struct ghsuart_ctrl_port *port =
223 container_of(w, struct ghsuart_ctrl_port, connect_w);
224 int retval;
225
226 if (!port || !test_bit(CH_READY, &port->channel_sts))
227 return;
228
229 pr_debug("%s: port:%p\n", __func__, port);
230
231 retval = msm_smux_open(port->ch_id, port->ctxt, smux_control_event,
232 rx_control_buffer);
233 if (retval < 0) {
234 pr_err(" %s smux_open failed\n", __func__);
235 return;
236 }
237
238}
239
240int ghsuart_ctrl_connect(void *gptr, int port_num)
241{
242 struct ghsuart_ctrl_port *port;
243 struct grmnet *gr;
244 unsigned long flags;
245
246 pr_debug("%s: port#%d\n", __func__, port_num);
247
248 if (port_num > num_ctrl_ports || !gptr) {
249 pr_err("%s: invalid portno#%d\n", __func__, port_num);
250 return -ENODEV;
251 }
252
253 port = ghsuart_ctrl_ports[port_num].port;
254 if (!port) {
255 pr_err("%s: port is null\n", __func__);
256 return -ENODEV;
257 }
258
259 spin_lock_irqsave(&port->port_lock, flags);
260
261 gr = gptr;
262 port->send_cpkt_response = gr->send_cpkt_response;
263 gr->send_encap_cmd = ghsuart_send_cpkt_tomodem;
264 gr->notify_modem = ghsuart_send_cbits_tomodem;
265
266 port->port_usb = gptr;
267 port->to_host = 0;
268 port->to_modem = 0;
269 port->drp_cpkt_cnt = 0;
270 spin_unlock_irqrestore(&port->port_lock, flags);
271
272 if (test_bit(CH_READY, &port->channel_sts))
273 queue_work(port->wq, &port->connect_w);
274
275 return 0;
276}
277
278static void ghsuart_ctrl_disconnect_w(struct work_struct *w)
279{
280 struct ghsuart_ctrl_port *port =
281 container_of(w, struct ghsuart_ctrl_port, disconnect_w);
282
283 if (!test_bit(CH_OPENED, &port->channel_sts))
284 return;
285
286 msm_smux_close(port->ch_id);
287 clear_bit(CH_OPENED, &port->channel_sts);
288}
289
290void ghsuart_ctrl_disconnect(void *gptr, int port_num)
291{
292 struct gctrl_port *port;
293 struct grmnet *gr = NULL;
294 unsigned long flags;
295
296 pr_debug("%s: port#%d\n", __func__, port_num);
297
298 if (port_num > num_ctrl_ports) {
299 pr_err("%s: invalid portno#%d\n", __func__, port_num);
300 return;
301 }
302
303 port = gctrl_ports[port_num].port;
304
305 if (!gptr || !port) {
306 pr_err("%s: grmnet port is null\n", __func__);
307 return;
308 }
309
310 gr = gptr;
311
312 spin_lock_irqsave(&port->port_lock, flags);
313 gr->send_encap_cmd = 0;
314 gr->notify_modem = 0;
315 port->cbits_tomodem = 0;
316 port->port_usb = 0;
317 port->send_cpkt_response = 0;
318 spin_unlock_irqrestore(&port->port_lock, flags);
319
320 queue_work(port->wq, &port->disconnect_w);
321}
322
323static int ghsuart_ctrl_probe(struct platform_device *pdev)
324{
325 struct ghsuart_ctrl_port *port;
326 unsigned long flags;
327
328 pr_debug("%s: name:%s\n", __func__, pdev->name);
329
330 port = ghsuart_ctrl_ports[pdev->id].port;
331 set_bit(CH_READY, &port->channel_sts);
332
333 /* if usb is online, start read */
334 spin_lock_irqsave(&port->port_lock, flags);
335 if (port->port_usb)
336 queue_work(port->wq, &port->connect_w);
337 spin_unlock_irqrestore(&port->port_lock, flags);
338
339 return 0;
340}
341
342static int ghsuart_ctrl_remove(struct platform_device *pdev)
343{
344 struct ghsuart_ctrl_port *port;
345 struct grmnet *gr = NULL;
346 unsigned long flags;
347
348 pr_debug("%s: name:%s\n", __func__, pdev->name);
349
350 port = ghsuart_ctrl_ports[pdev->id].port;
351
352 spin_lock_irqsave(&port->port_lock, flags);
353 if (!port->port_usb) {
354 spin_unlock_irqrestore(&port->port_lock, flags);
355 goto not_ready;
356 }
357
358 gr = port->port_usb;
359
360 spin_unlock_irqrestore(&port->port_lock, flags);
361
362 if (gr && gr->disconnect)
363 gr->disconnect(gr);
364
365 clear_bit(CH_OPENED, &port->channel_sts);
366not_ready:
367 clear_bit(CH_READY, &port->channel_sts);
368
369 return 0;
370}
371
372static void ghsuart_ctrl_port_free(int portno)
373{
374 struct ghsuart_ctrl_port *port = ghsuart_ctrl_ports[portno].port;
375 struct platform_driver *pdrv = &gctrl_ports[portno].pdrv;
376
377 destroy_workqueue(port->wq);
378 if (pdrv)
379 platform_driver_unregister(pdrv);
380 kfree(port);
381}
382
383static int ghsuart_ctrl_port_alloc(int portno, enum gadget_type gtype)
384{
385 struct ghsuart_ctrl_port *port;
386 struct platform_driver *pdrv;
387 int err;
388
389 port = kzalloc(sizeof(struct ghsuart_ctrl_port), GFP_KERNEL);
390 if (!port)
391 return -ENOMEM;
392
393 port->wq = create_singlethread_workqueue(ghsuart_ctrl_names[portno]);
394 if (!port->wq) {
395 pr_err("%s: Unable to create workqueue:%s\n",
396 __func__, ghsuart_ctrl_names[portno]);
397 kfree(port);
398 return -ENOMEM;
399 }
400
401 port->port_num = portno;
402 port->gtype = gtype;
403
404 spin_lock_init(&port->port_lock);
405
406 INIT_WORK(&port->connect_w, ghsuart_ctrl_connect_w);
407 INIT_WORK(&port->disconnect_w, ghsuart_ctrl_disconnect_w);
408
409 port->ch_id = SMUX_USB_RMNET_CTL_0;
410 port->ctxt = port;
411 port->send_pkt = ghsuart_ctrl_receive;
412 ghsuart_ctrl_ports[portno].port = port;
413
414 pdrv = &ghsuart_ctrl_ports[portno].pdrv;
415 pdrv->probe = ghsuart_ctrl_probe;
416 pdrv->remove = ghsuart_ctrl_remove;
417 pdrv->driver.name = ghsuart_ctrl_names[portno];
418 pdrv->driver.owner = THIS_MODULE;
419
420 err = platform_driver_register(pdrv);
421 if (unlikely(err < 0))
422 return err;
423 pr_debug("%s: port:%p portno:%d\n", __func__, port, portno);
424
425 return 0;
426}
427
428int ghsuart_ctrl_setup(unsigned int num_ports, enum gadget_type gtype)
429{
430 int first_port_id = num_ctrl_ports;
431 int total_num_ports = num_ports + num_ctrl_ports;
432 int i;
433 int ret = 0;
434
435 if (!num_ports || total_num_ports > NUM_HSUART_PORTS) {
436 pr_err("%s: Invalid num of ports count:%d\n",
437 __func__, num_ports);
438 return -EINVAL;
439 }
440
441 pr_debug("%s: requested ports:%d\n", __func__, num_ports);
442
443 for (i = first_port_id; i < (first_port_id + num_ports); i++) {
444
445 num_ctrl_ports++;
446 ret = ghsuart_ctrl_port_alloc(i, gtype);
447 if (ret) {
448 num_ctrl_ports--;
449 pr_err("%s: Unable to alloc port:%d\n", __func__, i);
450 goto free_ports;
451 }
452 }
453
454 return first_port_id;
455
456free_ports:
457 for (i = first_port_id; i < num_ctrl_ports; i++)
458 ghsuart_ctrl_port_free(i);
459 num_ctrl_ports = first_port_id;
460 return ret;
461}
462
463#define DEBUG_BUF_SIZE 1024
464static ssize_t ghsuart_ctrl_read_stats(struct file *file, char __user *ubuf,
465 size_t count, loff_t *ppos)
466{
467 struct ghsuart_ctrl_port *port;
468 char *buf;
469 unsigned long flags;
470 int ret;
471 int i;
472 int temp = 0;
473
474 buf = kzalloc(sizeof(char) * DEBUG_BUF_SIZE, GFP_KERNEL);
475 if (!buf)
476 return -ENOMEM;
477
478 for (i = 0; i < num_ctrl_ports; i++) {
479 port = ghsuart_ctrl_ports[i].port;
480 if (!port)
481 continue;
482 spin_lock_irqsave(&port->port_lock, flags);
483
484 temp += scnprintf(buf + temp, DEBUG_BUF_SIZE - temp,
485 "#PORT:%d port: %p\n"
486 "to_usbhost: %lu\n"
487 "to_modem: %lu\n"
488 "cpkt_drp_cnt: %lu\n"
489 "DTR: %s\n",
490 i, port,
491 port->to_host, port->to_modem,
492 port->drp_cpkt_cnt,
493 port->cbits_tomodem ? "HIGH" : "LOW");
494
495 spin_unlock_irqrestore(&port->port_lock, flags);
496 }
497
498 ret = simple_read_from_buffer(ubuf, count, ppos, buf, temp);
499
500 kfree(buf);
501
502 return ret;
503}
504
505static ssize_t ghsuart_ctrl_reset_stats(struct file *file,
506 const char __user *buf, size_t count, loff_t *ppos)
507{
508 struct ghsuart_ctrl_port *port;
509 int i;
510 unsigned long flags;
511
512 for (i = 0; i < num_ctrl_ports; i++) {
513 port = ghsuart_ctrl_ports[i].port;
514 if (!port)
515 continue;
516
517 spin_lock_irqsave(&port->port_lock, flags);
518 port->to_host = 0;
519 port->to_modem = 0;
520 port->drp_cpkt_cnt = 0;
521 spin_unlock_irqrestore(&port->port_lock, flags);
522 }
523 return count;
524}
525
526static const struct file_operations ghsuart_ctrl_stats_ops = {
527 .read = ghsuart_ctrl_read_stats,
528 .write = ghsuart_ctrl_reset_stats,
529};
530
531static struct dentry *ghsuart_ctrl_dent;
532static int ghsuart_ctrl_debugfs_init(void)
533{
534 struct dentry *ghsuart_ctrl_dfile;
535
536 ghsuart_ctrl_dent = debugfs_create_dir("ghsuart_ctrl_xport", 0);
537 if (!ghsuart_ctrl_dent || IS_ERR(ghsuart_ctrl_dent))
538 return -ENODEV;
539
540 ghsuart_ctrl_dfile =
541 debugfs_create_file("status", S_IRUGO | S_IWUSR,
542 ghsuart_ctrl_dent, 0, &gctrl_stats_ops);
543 if (!ghsuart_ctrl_dfile || IS_ERR(ghsuart_ctrl_dfile)) {
544 debugfs_remove(ghsuart_ctrl_dent);
545 ghsuart_ctrl_dent = NULL;
546 return -ENODEV;
547 }
548 return 0;
549}
550
551static void ghsuart_ctrl_debugfs_exit(void)
552{
553 debugfs_remove_recursive(ghsuart_ctrl_dent);
554}
555
556static int __init ghsuart_ctrl_init(void)
557{
558 int ret;
559
560 ret = ghsuart_ctrl_debugfs_init();
561 if (ret) {
562 pr_debug("mode debugfs file is not available\n");
563 return ret;
564 }
565 return 0;
566}
567module_init(ghsuart_ctrl_init);
568
569static void __exit ghsuart_ctrl_exit(void)
570{
571 ghsuart_ctrl_debugfs_exit();
572}
573module_exit(ghsuart_ctrl_exit);
574
575MODULE_DESCRIPTION("HSUART control xport for RmNet");
576MODULE_LICENSE("GPL v2");