u_ctrl_hsic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /* Copyright (c) 2011, The Linux Foundation. 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. #include <linux/kernel.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/device.h>
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include <linux/termios.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/bitops.h>
  20. #include <linux/termios.h>
  21. #include <mach/usb_bridge.h>
  22. #include <mach/usb_gadget_xport.h>
  23. /* from cdc-acm.h */
  24. #define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
  25. #define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
  26. #define ACM_CTRL_OVERRUN (1 << 6)
  27. #define ACM_CTRL_PARITY (1 << 5)
  28. #define ACM_CTRL_FRAMING (1 << 4)
  29. #define ACM_CTRL_RI (1 << 3)
  30. #define ACM_CTRL_BRK (1 << 2)
  31. #define ACM_CTRL_DSR (1 << 1)
  32. #define ACM_CTRL_DCD (1 << 0)
  33. static unsigned int no_ctrl_ports;
  34. #define READ_BUF_LEN 1024
  35. #define CH_OPENED 0
  36. #define CH_READY 1
  37. struct gctrl_port {
  38. /* port */
  39. unsigned port_num;
  40. /* gadget */
  41. spinlock_t port_lock;
  42. void *port_usb;
  43. /* work queue*/
  44. struct workqueue_struct *wq;
  45. struct work_struct connect_w;
  46. struct work_struct disconnect_w;
  47. enum gadget_type gtype;
  48. /*ctrl pkt response cb*/
  49. int (*send_cpkt_response)(void *g, void *buf, size_t len);
  50. struct bridge brdg;
  51. /* bridge status */
  52. unsigned long bridge_sts;
  53. /* control bits */
  54. unsigned cbits_tomodem;
  55. unsigned cbits_tohost;
  56. /* counters */
  57. unsigned long to_modem;
  58. unsigned long to_host;
  59. unsigned long drp_cpkt_cnt;
  60. };
  61. static struct {
  62. struct gctrl_port *port;
  63. struct platform_driver pdrv;
  64. char port_name[BRIDGE_NAME_MAX_LEN];
  65. } gctrl_ports[NUM_PORTS];
  66. static int ghsic_ctrl_receive(void *dev, void *buf, size_t actual)
  67. {
  68. struct gctrl_port *port = dev;
  69. int retval = 0;
  70. pr_debug_ratelimited("%s: read complete bytes read: %d\n",
  71. __func__, actual);
  72. /* send it to USB here */
  73. if (port && port->send_cpkt_response) {
  74. retval = port->send_cpkt_response(port->port_usb, buf, actual);
  75. port->to_host++;
  76. }
  77. return retval;
  78. }
  79. static int
  80. ghsic_send_cpkt_tomodem(u8 portno, void *buf, size_t len)
  81. {
  82. void *cbuf;
  83. struct gctrl_port *port;
  84. if (portno >= no_ctrl_ports) {
  85. pr_err("%s: Invalid portno#%d\n", __func__, portno);
  86. return -ENODEV;
  87. }
  88. port = gctrl_ports[portno].port;
  89. if (!port) {
  90. pr_err("%s: port is null\n", __func__);
  91. return -ENODEV;
  92. }
  93. cbuf = kmalloc(len, GFP_ATOMIC);
  94. if (!cbuf)
  95. return -ENOMEM;
  96. memcpy(cbuf, buf, len);
  97. /* drop cpkt if ch is not open */
  98. if (!test_bit(CH_OPENED, &port->bridge_sts)) {
  99. port->drp_cpkt_cnt++;
  100. kfree(cbuf);
  101. return 0;
  102. }
  103. pr_debug("%s: ctrl_pkt:%d bytes\n", __func__, len);
  104. ctrl_bridge_write(port->brdg.ch_id, cbuf, len);
  105. port->to_modem++;
  106. return 0;
  107. }
  108. static void
  109. ghsic_send_cbits_tomodem(void *gptr, u8 portno, int cbits)
  110. {
  111. struct gctrl_port *port;
  112. if (portno >= no_ctrl_ports || !gptr) {
  113. pr_err("%s: Invalid portno#%d\n", __func__, portno);
  114. return;
  115. }
  116. port = gctrl_ports[portno].port;
  117. if (!port) {
  118. pr_err("%s: port is null\n", __func__);
  119. return;
  120. }
  121. if (cbits == port->cbits_tomodem)
  122. return;
  123. port->cbits_tomodem = cbits;
  124. if (!test_bit(CH_OPENED, &port->bridge_sts))
  125. return;
  126. pr_debug("%s: ctrl_tomodem:%d\n", __func__, cbits);
  127. ctrl_bridge_set_cbits(port->brdg.ch_id, cbits);
  128. }
  129. static void ghsic_ctrl_connect_w(struct work_struct *w)
  130. {
  131. struct gserial *gser = NULL;
  132. struct grmnet *gr = NULL;
  133. struct gctrl_port *port =
  134. container_of(w, struct gctrl_port, connect_w);
  135. unsigned long flags;
  136. int retval;
  137. unsigned cbits;
  138. if (!port || !test_bit(CH_READY, &port->bridge_sts))
  139. return;
  140. pr_debug("%s: port:%pK\n", __func__, port);
  141. retval = ctrl_bridge_open(&port->brdg);
  142. if (retval) {
  143. pr_err("%s: ctrl bridge open failed :%d\n", __func__, retval);
  144. return;
  145. }
  146. spin_lock_irqsave(&port->port_lock, flags);
  147. if (!port->port_usb) {
  148. ctrl_bridge_close(port->brdg.ch_id);
  149. spin_unlock_irqrestore(&port->port_lock, flags);
  150. return;
  151. }
  152. set_bit(CH_OPENED, &port->bridge_sts);
  153. spin_unlock_irqrestore(&port->port_lock, flags);
  154. cbits = ctrl_bridge_get_cbits_tohost(port->brdg.ch_id);
  155. if (port->gtype == USB_GADGET_SERIAL && (cbits & ACM_CTRL_DCD)) {
  156. gser = port->port_usb;
  157. if (gser && gser->connect)
  158. gser->connect(gser);
  159. return;
  160. }
  161. if (port->gtype == USB_GADGET_RMNET) {
  162. gr = port->port_usb;
  163. if (gr && gr->connect)
  164. gr->connect(gr);
  165. }
  166. }
  167. int ghsic_ctrl_connect(void *gptr, int port_num)
  168. {
  169. struct gctrl_port *port;
  170. struct gserial *gser;
  171. struct grmnet *gr;
  172. unsigned long flags;
  173. pr_debug("%s: port#%d\n", __func__, port_num);
  174. if (port_num > no_ctrl_ports || !gptr) {
  175. pr_err("%s: invalid portno#%d\n", __func__, port_num);
  176. return -ENODEV;
  177. }
  178. port = gctrl_ports[port_num].port;
  179. if (!port) {
  180. pr_err("%s: port is null\n", __func__);
  181. return -ENODEV;
  182. }
  183. spin_lock_irqsave(&port->port_lock, flags);
  184. if (port->gtype == USB_GADGET_SERIAL) {
  185. gser = gptr;
  186. gser->notify_modem = ghsic_send_cbits_tomodem;
  187. }
  188. if (port->gtype == USB_GADGET_RMNET) {
  189. gr = gptr;
  190. port->send_cpkt_response = gr->send_cpkt_response;
  191. gr->send_encap_cmd = ghsic_send_cpkt_tomodem;
  192. gr->notify_modem = ghsic_send_cbits_tomodem;
  193. }
  194. port->port_usb = gptr;
  195. port->to_host = 0;
  196. port->to_modem = 0;
  197. port->drp_cpkt_cnt = 0;
  198. spin_unlock_irqrestore(&port->port_lock, flags);
  199. queue_work(port->wq, &port->connect_w);
  200. return 0;
  201. }
  202. static void gctrl_disconnect_w(struct work_struct *w)
  203. {
  204. struct gctrl_port *port =
  205. container_of(w, struct gctrl_port, disconnect_w);
  206. if (!test_bit(CH_OPENED, &port->bridge_sts))
  207. return;
  208. /* send the dtr zero */
  209. ctrl_bridge_close(port->brdg.ch_id);
  210. clear_bit(CH_OPENED, &port->bridge_sts);
  211. }
  212. void ghsic_ctrl_disconnect(void *gptr, int port_num)
  213. {
  214. struct gctrl_port *port;
  215. struct gserial *gser = NULL;
  216. struct grmnet *gr = NULL;
  217. unsigned long flags;
  218. pr_debug("%s: port#%d\n", __func__, port_num);
  219. port = gctrl_ports[port_num].port;
  220. if (port_num > no_ctrl_ports) {
  221. pr_err("%s: invalid portno#%d\n", __func__, port_num);
  222. return;
  223. }
  224. if (!gptr || !port) {
  225. pr_err("%s: grmnet port is null\n", __func__);
  226. return;
  227. }
  228. if (port->gtype == USB_GADGET_SERIAL)
  229. gser = gptr;
  230. else
  231. gr = gptr;
  232. spin_lock_irqsave(&port->port_lock, flags);
  233. if (gr) {
  234. gr->send_encap_cmd = 0;
  235. gr->notify_modem = 0;
  236. }
  237. if (gser)
  238. gser->notify_modem = 0;
  239. port->cbits_tomodem = 0;
  240. port->port_usb = 0;
  241. port->send_cpkt_response = 0;
  242. spin_unlock_irqrestore(&port->port_lock, flags);
  243. queue_work(port->wq, &port->disconnect_w);
  244. }
  245. static void ghsic_ctrl_status(void *ctxt, unsigned int ctrl_bits)
  246. {
  247. struct gctrl_port *port = ctxt;
  248. struct gserial *gser;
  249. pr_debug("%s - input control lines: dcd%c dsr%c break%c "
  250. "ring%c framing%c parity%c overrun%c\n", __func__,
  251. ctrl_bits & ACM_CTRL_DCD ? '+' : '-',
  252. ctrl_bits & ACM_CTRL_DSR ? '+' : '-',
  253. ctrl_bits & ACM_CTRL_BRK ? '+' : '-',
  254. ctrl_bits & ACM_CTRL_RI ? '+' : '-',
  255. ctrl_bits & ACM_CTRL_FRAMING ? '+' : '-',
  256. ctrl_bits & ACM_CTRL_PARITY ? '+' : '-',
  257. ctrl_bits & ACM_CTRL_OVERRUN ? '+' : '-');
  258. port->cbits_tohost = ctrl_bits;
  259. gser = port->port_usb;
  260. if (gser && gser->send_modem_ctrl_bits)
  261. gser->send_modem_ctrl_bits(gser, ctrl_bits);
  262. }
  263. static int ghsic_ctrl_get_port_id(const char *pdev_name)
  264. {
  265. struct gctrl_port *port;
  266. int i;
  267. for (i = 0; i < no_ctrl_ports; i++) {
  268. port = gctrl_ports[i].port;
  269. if (!strncmp(port->brdg.name, pdev_name, BRIDGE_NAME_MAX_LEN))
  270. return i;
  271. }
  272. return -EINVAL;
  273. }
  274. static int ghsic_ctrl_probe(struct platform_device *pdev)
  275. {
  276. struct gctrl_port *port;
  277. unsigned long flags;
  278. int id;
  279. pr_debug("%s: name:%s\n", __func__, pdev->name);
  280. id = ghsic_ctrl_get_port_id(pdev->name);
  281. if (id < 0 || id >= no_ctrl_ports) {
  282. pr_err("%s: invalid port: %d\n", __func__, id);
  283. return -EINVAL;
  284. }
  285. port = gctrl_ports[id].port;
  286. set_bit(CH_READY, &port->bridge_sts);
  287. /* if usb is online, start read */
  288. spin_lock_irqsave(&port->port_lock, flags);
  289. if (port->port_usb)
  290. queue_work(port->wq, &port->connect_w);
  291. spin_unlock_irqrestore(&port->port_lock, flags);
  292. return 0;
  293. }
  294. static int ghsic_ctrl_remove(struct platform_device *pdev)
  295. {
  296. struct gctrl_port *port;
  297. struct gserial *gser = NULL;
  298. struct grmnet *gr = NULL;
  299. unsigned long flags;
  300. int id;
  301. pr_debug("%s: name:%s\n", __func__, pdev->name);
  302. id = ghsic_ctrl_get_port_id(pdev->name);
  303. if (id < 0 || id >= no_ctrl_ports) {
  304. pr_err("%s: invalid port: %d\n", __func__, id);
  305. return -EINVAL;
  306. }
  307. port = gctrl_ports[id].port;
  308. spin_lock_irqsave(&port->port_lock, flags);
  309. if (!port->port_usb) {
  310. spin_unlock_irqrestore(&port->port_lock, flags);
  311. goto not_ready;
  312. }
  313. if (port->gtype == USB_GADGET_SERIAL)
  314. gser = port->port_usb;
  315. else
  316. gr = port->port_usb;
  317. port->cbits_tohost = 0;
  318. spin_unlock_irqrestore(&port->port_lock, flags);
  319. if (gr && gr->disconnect)
  320. gr->disconnect(gr);
  321. if (gser && gser->disconnect)
  322. gser->disconnect(gser);
  323. ctrl_bridge_close(port->brdg.ch_id);
  324. clear_bit(CH_OPENED, &port->bridge_sts);
  325. not_ready:
  326. clear_bit(CH_READY, &port->bridge_sts);
  327. return 0;
  328. }
  329. static void ghsic_ctrl_port_free(int portno)
  330. {
  331. struct gctrl_port *port = gctrl_ports[portno].port;
  332. struct platform_driver *pdrv = &gctrl_ports[portno].pdrv;
  333. destroy_workqueue(port->wq);
  334. kfree(port);
  335. if (pdrv)
  336. platform_driver_unregister(pdrv);
  337. }
  338. static int gctrl_port_alloc(int portno, enum gadget_type gtype)
  339. {
  340. struct gctrl_port *port;
  341. struct platform_driver *pdrv;
  342. char *name;
  343. port = kzalloc(sizeof(struct gctrl_port), GFP_KERNEL);
  344. if (!port)
  345. return -ENOMEM;
  346. name = gctrl_ports[portno].port_name;
  347. port->wq = create_singlethread_workqueue(name);
  348. if (!port->wq) {
  349. pr_err("%s: Unable to create workqueue:%s\n", __func__, name);
  350. return -ENOMEM;
  351. }
  352. port->port_num = portno;
  353. port->gtype = gtype;
  354. spin_lock_init(&port->port_lock);
  355. INIT_WORK(&port->connect_w, ghsic_ctrl_connect_w);
  356. INIT_WORK(&port->disconnect_w, gctrl_disconnect_w);
  357. port->brdg.name = name;
  358. port->brdg.ctx = port;
  359. port->brdg.ops.send_pkt = ghsic_ctrl_receive;
  360. if (port->gtype == USB_GADGET_SERIAL)
  361. port->brdg.ops.send_cbits = ghsic_ctrl_status;
  362. gctrl_ports[portno].port = port;
  363. pdrv = &gctrl_ports[portno].pdrv;
  364. pdrv->probe = ghsic_ctrl_probe;
  365. pdrv->remove = ghsic_ctrl_remove;
  366. pdrv->driver.name = name;
  367. pdrv->driver.owner = THIS_MODULE;
  368. platform_driver_register(pdrv);
  369. pr_debug("%s: port:%pK portno:%d\n", __func__, port, portno);
  370. return 0;
  371. }
  372. /*portname will be used to find the bridge channel index*/
  373. void ghsic_ctrl_set_port_name(const char *name, const char *xport_type)
  374. {
  375. static unsigned int port_num;
  376. if (port_num >= NUM_PORTS) {
  377. pr_err("%s: setting xport name for invalid port num %d\n",
  378. __func__, port_num);
  379. return;
  380. }
  381. /*if no xport name is passed set it to xport type e.g. hsic*/
  382. if (!name)
  383. strlcpy(gctrl_ports[port_num].port_name, xport_type,
  384. BRIDGE_NAME_MAX_LEN);
  385. else
  386. strlcpy(gctrl_ports[port_num].port_name, name,
  387. BRIDGE_NAME_MAX_LEN);
  388. /*append _ctrl to get ctrl bridge name e.g. serial_hsic_ctrl*/
  389. strlcat(gctrl_ports[port_num].port_name, "_ctrl", BRIDGE_NAME_MAX_LEN);
  390. port_num++;
  391. }
  392. int ghsic_ctrl_setup(unsigned int num_ports, enum gadget_type gtype)
  393. {
  394. int first_port_id = no_ctrl_ports;
  395. int total_num_ports = num_ports + no_ctrl_ports;
  396. int i;
  397. int ret = 0;
  398. if (!num_ports || total_num_ports > NUM_PORTS) {
  399. pr_err("%s: Invalid num of ports count:%d\n",
  400. __func__, num_ports);
  401. return -EINVAL;
  402. }
  403. pr_debug("%s: requested ports:%d\n", __func__, num_ports);
  404. for (i = first_port_id; i < (first_port_id + num_ports); i++) {
  405. /*probe can be called while port_alloc,so update no_ctrl_ports*/
  406. no_ctrl_ports++;
  407. ret = gctrl_port_alloc(i, gtype);
  408. if (ret) {
  409. no_ctrl_ports--;
  410. pr_err("%s: Unable to alloc port:%d\n", __func__, i);
  411. goto free_ports;
  412. }
  413. }
  414. return first_port_id;
  415. free_ports:
  416. for (i = first_port_id; i < no_ctrl_ports; i++)
  417. ghsic_ctrl_port_free(i);
  418. no_ctrl_ports = first_port_id;
  419. return ret;
  420. }
  421. #if defined(CONFIG_DEBUG_FS)
  422. #define DEBUG_BUF_SIZE 1024
  423. static ssize_t gctrl_read_stats(struct file *file, char __user *ubuf,
  424. size_t count, loff_t *ppos)
  425. {
  426. struct gctrl_port *port;
  427. struct platform_driver *pdrv;
  428. char *buf;
  429. unsigned long flags;
  430. int ret;
  431. int i;
  432. int temp = 0;
  433. buf = kzalloc(sizeof(char) * DEBUG_BUF_SIZE, GFP_KERNEL);
  434. if (!buf)
  435. return -ENOMEM;
  436. for (i = 0; i < no_ctrl_ports; i++) {
  437. port = gctrl_ports[i].port;
  438. if (!port)
  439. continue;
  440. pdrv = &gctrl_ports[i].pdrv;
  441. spin_lock_irqsave(&port->port_lock, flags);
  442. temp += scnprintf(buf + temp, DEBUG_BUF_SIZE - temp,
  443. "\nName: %s\n"
  444. "#PORT:%d port: %pK\n"
  445. "to_usbhost: %lu\n"
  446. "to_modem: %lu\n"
  447. "cpkt_drp_cnt: %lu\n"
  448. "DTR: %s\n"
  449. "ch_open: %d\n"
  450. "ch_ready: %d\n",
  451. pdrv->driver.name,
  452. i, port,
  453. port->to_host, port->to_modem,
  454. port->drp_cpkt_cnt,
  455. port->cbits_tomodem ? "HIGH" : "LOW",
  456. test_bit(CH_OPENED, &port->bridge_sts),
  457. test_bit(CH_READY, &port->bridge_sts));
  458. spin_unlock_irqrestore(&port->port_lock, flags);
  459. }
  460. ret = simple_read_from_buffer(ubuf, count, ppos, buf, temp);
  461. kfree(buf);
  462. return ret;
  463. }
  464. static ssize_t gctrl_reset_stats(struct file *file,
  465. const char __user *buf, size_t count, loff_t *ppos)
  466. {
  467. struct gctrl_port *port;
  468. int i;
  469. unsigned long flags;
  470. for (i = 0; i < no_ctrl_ports; i++) {
  471. port = gctrl_ports[i].port;
  472. if (!port)
  473. continue;
  474. spin_lock_irqsave(&port->port_lock, flags);
  475. port->to_host = 0;
  476. port->to_modem = 0;
  477. port->drp_cpkt_cnt = 0;
  478. spin_unlock_irqrestore(&port->port_lock, flags);
  479. }
  480. return count;
  481. }
  482. const struct file_operations gctrl_stats_ops = {
  483. .read = gctrl_read_stats,
  484. .write = gctrl_reset_stats,
  485. };
  486. struct dentry *gctrl_dent;
  487. struct dentry *gctrl_dfile;
  488. static void gctrl_debugfs_init(void)
  489. {
  490. gctrl_dent = debugfs_create_dir("ghsic_ctrl_xport", 0);
  491. if (IS_ERR(gctrl_dent))
  492. return;
  493. gctrl_dfile =
  494. debugfs_create_file("status", 0444, gctrl_dent, 0,
  495. &gctrl_stats_ops);
  496. if (!gctrl_dfile || IS_ERR(gctrl_dfile))
  497. debugfs_remove(gctrl_dent);
  498. }
  499. static void gctrl_debugfs_exit(void)
  500. {
  501. debugfs_remove(gctrl_dfile);
  502. debugfs_remove(gctrl_dent);
  503. }
  504. #else
  505. static void gctrl_debugfs_init(void) { }
  506. static void gctrl_debugfs_exit(void) { }
  507. #endif
  508. static int __init gctrl_init(void)
  509. {
  510. gctrl_debugfs_init();
  511. return 0;
  512. }
  513. module_init(gctrl_init);
  514. static void __exit gctrl_exit(void)
  515. {
  516. gctrl_debugfs_exit();
  517. }
  518. module_exit(gctrl_exit);
  519. MODULE_DESCRIPTION("hsic control xport driver");
  520. MODULE_LICENSE("GPL v2");