xhci-dbgtty.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /**
  2. * xhci-dbgtty.c - tty glue for xHCI debug capability
  3. *
  4. * Copyright (C) 2017 Intel Corporation
  5. *
  6. * Author: Lu Baolu <baolu.lu@linux.intel.com>
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/tty.h>
  10. #include <linux/tty_flip.h>
  11. #include "xhci.h"
  12. #include "xhci-dbgcap.h"
  13. static unsigned int
  14. dbc_send_packet(struct dbc_port *port, char *packet, unsigned int size)
  15. {
  16. unsigned int len;
  17. len = kfifo_len(&port->write_fifo);
  18. if (len < size)
  19. size = len;
  20. if (size != 0)
  21. size = kfifo_out(&port->write_fifo, packet, size);
  22. return size;
  23. }
  24. static int dbc_start_tx(struct dbc_port *port)
  25. __releases(&port->port_lock)
  26. __acquires(&port->port_lock)
  27. {
  28. int len;
  29. struct dbc_request *req;
  30. int status = 0;
  31. bool do_tty_wake = false;
  32. struct list_head *pool = &port->write_pool;
  33. while (!list_empty(pool)) {
  34. req = list_entry(pool->next, struct dbc_request, list_pool);
  35. len = dbc_send_packet(port, req->buf, DBC_MAX_PACKET);
  36. if (len == 0)
  37. break;
  38. do_tty_wake = true;
  39. req->length = len;
  40. list_del(&req->list_pool);
  41. spin_unlock(&port->port_lock);
  42. status = dbc_ep_queue(port->out, req, GFP_ATOMIC);
  43. spin_lock(&port->port_lock);
  44. if (status) {
  45. list_add(&req->list_pool, pool);
  46. break;
  47. }
  48. }
  49. if (do_tty_wake && port->port.tty)
  50. tty_wakeup(port->port.tty);
  51. return status;
  52. }
  53. static void dbc_start_rx(struct dbc_port *port)
  54. __releases(&port->port_lock)
  55. __acquires(&port->port_lock)
  56. {
  57. struct dbc_request *req;
  58. int status;
  59. struct list_head *pool = &port->read_pool;
  60. while (!list_empty(pool)) {
  61. if (!port->port.tty)
  62. break;
  63. req = list_entry(pool->next, struct dbc_request, list_pool);
  64. list_del(&req->list_pool);
  65. req->length = DBC_MAX_PACKET;
  66. spin_unlock(&port->port_lock);
  67. status = dbc_ep_queue(port->in, req, GFP_ATOMIC);
  68. spin_lock(&port->port_lock);
  69. if (status) {
  70. list_add(&req->list_pool, pool);
  71. break;
  72. }
  73. }
  74. }
  75. static void
  76. dbc_read_complete(struct xhci_hcd *xhci, struct dbc_request *req)
  77. {
  78. struct xhci_dbc *dbc = xhci->dbc;
  79. struct dbc_port *port = &dbc->port;
  80. spin_lock(&port->port_lock);
  81. list_add_tail(&req->list_pool, &port->read_queue);
  82. tasklet_schedule(&port->push);
  83. spin_unlock(&port->port_lock);
  84. }
  85. static void dbc_write_complete(struct xhci_hcd *xhci, struct dbc_request *req)
  86. {
  87. struct xhci_dbc *dbc = xhci->dbc;
  88. struct dbc_port *port = &dbc->port;
  89. spin_lock(&port->port_lock);
  90. list_add(&req->list_pool, &port->write_pool);
  91. switch (req->status) {
  92. case 0:
  93. dbc_start_tx(port);
  94. break;
  95. case -ESHUTDOWN:
  96. break;
  97. default:
  98. xhci_warn(xhci, "unexpected write complete status %d\n",
  99. req->status);
  100. break;
  101. }
  102. spin_unlock(&port->port_lock);
  103. }
  104. static void xhci_dbc_free_req(struct dbc_ep *dep, struct dbc_request *req)
  105. {
  106. kfree(req->buf);
  107. dbc_free_request(dep, req);
  108. }
  109. static int
  110. xhci_dbc_alloc_requests(struct dbc_ep *dep, struct list_head *head,
  111. void (*fn)(struct xhci_hcd *, struct dbc_request *))
  112. {
  113. int i;
  114. struct dbc_request *req;
  115. for (i = 0; i < DBC_QUEUE_SIZE; i++) {
  116. req = dbc_alloc_request(dep, GFP_ATOMIC);
  117. if (!req)
  118. break;
  119. req->length = DBC_MAX_PACKET;
  120. req->buf = kmalloc(req->length, GFP_KERNEL);
  121. if (!req->buf) {
  122. xhci_dbc_free_req(dep, req);
  123. break;
  124. }
  125. req->complete = fn;
  126. list_add_tail(&req->list_pool, head);
  127. }
  128. return list_empty(head) ? -ENOMEM : 0;
  129. }
  130. static void
  131. xhci_dbc_free_requests(struct dbc_ep *dep, struct list_head *head)
  132. {
  133. struct dbc_request *req;
  134. while (!list_empty(head)) {
  135. req = list_entry(head->next, struct dbc_request, list_pool);
  136. list_del(&req->list_pool);
  137. xhci_dbc_free_req(dep, req);
  138. }
  139. }
  140. static int dbc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  141. {
  142. struct dbc_port *port = driver->driver_state;
  143. tty->driver_data = port;
  144. return tty_port_install(&port->port, driver, tty);
  145. }
  146. static int dbc_tty_open(struct tty_struct *tty, struct file *file)
  147. {
  148. struct dbc_port *port = tty->driver_data;
  149. return tty_port_open(&port->port, tty, file);
  150. }
  151. static void dbc_tty_close(struct tty_struct *tty, struct file *file)
  152. {
  153. struct dbc_port *port = tty->driver_data;
  154. tty_port_close(&port->port, tty, file);
  155. }
  156. static int dbc_tty_write(struct tty_struct *tty,
  157. const unsigned char *buf,
  158. int count)
  159. {
  160. struct dbc_port *port = tty->driver_data;
  161. unsigned long flags;
  162. spin_lock_irqsave(&port->port_lock, flags);
  163. if (count)
  164. count = kfifo_in(&port->write_fifo, buf, count);
  165. dbc_start_tx(port);
  166. spin_unlock_irqrestore(&port->port_lock, flags);
  167. return count;
  168. }
  169. static int dbc_tty_put_char(struct tty_struct *tty, unsigned char ch)
  170. {
  171. struct dbc_port *port = tty->driver_data;
  172. unsigned long flags;
  173. int status;
  174. spin_lock_irqsave(&port->port_lock, flags);
  175. status = kfifo_put(&port->write_fifo, ch);
  176. spin_unlock_irqrestore(&port->port_lock, flags);
  177. return status;
  178. }
  179. static void dbc_tty_flush_chars(struct tty_struct *tty)
  180. {
  181. struct dbc_port *port = tty->driver_data;
  182. unsigned long flags;
  183. spin_lock_irqsave(&port->port_lock, flags);
  184. dbc_start_tx(port);
  185. spin_unlock_irqrestore(&port->port_lock, flags);
  186. }
  187. static int dbc_tty_write_room(struct tty_struct *tty)
  188. {
  189. struct dbc_port *port = tty->driver_data;
  190. unsigned long flags;
  191. int room = 0;
  192. spin_lock_irqsave(&port->port_lock, flags);
  193. room = kfifo_avail(&port->write_fifo);
  194. spin_unlock_irqrestore(&port->port_lock, flags);
  195. return room;
  196. }
  197. static int dbc_tty_chars_in_buffer(struct tty_struct *tty)
  198. {
  199. struct dbc_port *port = tty->driver_data;
  200. unsigned long flags;
  201. int chars = 0;
  202. spin_lock_irqsave(&port->port_lock, flags);
  203. chars = kfifo_len(&port->write_fifo);
  204. spin_unlock_irqrestore(&port->port_lock, flags);
  205. return chars;
  206. }
  207. static void dbc_tty_unthrottle(struct tty_struct *tty)
  208. {
  209. struct dbc_port *port = tty->driver_data;
  210. unsigned long flags;
  211. spin_lock_irqsave(&port->port_lock, flags);
  212. tasklet_schedule(&port->push);
  213. spin_unlock_irqrestore(&port->port_lock, flags);
  214. }
  215. static const struct tty_operations dbc_tty_ops = {
  216. .install = dbc_tty_install,
  217. .open = dbc_tty_open,
  218. .close = dbc_tty_close,
  219. .write = dbc_tty_write,
  220. .put_char = dbc_tty_put_char,
  221. .flush_chars = dbc_tty_flush_chars,
  222. .write_room = dbc_tty_write_room,
  223. .chars_in_buffer = dbc_tty_chars_in_buffer,
  224. .unthrottle = dbc_tty_unthrottle,
  225. };
  226. static struct tty_driver *dbc_tty_driver;
  227. int xhci_dbc_tty_register_driver(struct xhci_hcd *xhci)
  228. {
  229. int status;
  230. struct xhci_dbc *dbc = xhci->dbc;
  231. dbc_tty_driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW |
  232. TTY_DRIVER_DYNAMIC_DEV);
  233. if (IS_ERR(dbc_tty_driver)) {
  234. status = PTR_ERR(dbc_tty_driver);
  235. dbc_tty_driver = NULL;
  236. return status;
  237. }
  238. dbc_tty_driver->driver_name = "dbc_serial";
  239. dbc_tty_driver->name = "ttyDBC";
  240. dbc_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  241. dbc_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  242. dbc_tty_driver->init_termios = tty_std_termios;
  243. dbc_tty_driver->init_termios.c_cflag =
  244. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  245. dbc_tty_driver->init_termios.c_ispeed = 9600;
  246. dbc_tty_driver->init_termios.c_ospeed = 9600;
  247. dbc_tty_driver->driver_state = &dbc->port;
  248. tty_set_operations(dbc_tty_driver, &dbc_tty_ops);
  249. status = tty_register_driver(dbc_tty_driver);
  250. if (status) {
  251. xhci_err(xhci,
  252. "can't register dbc tty driver, err %d\n", status);
  253. put_tty_driver(dbc_tty_driver);
  254. dbc_tty_driver = NULL;
  255. }
  256. return status;
  257. }
  258. void xhci_dbc_tty_unregister_driver(void)
  259. {
  260. tty_unregister_driver(dbc_tty_driver);
  261. put_tty_driver(dbc_tty_driver);
  262. dbc_tty_driver = NULL;
  263. }
  264. static void dbc_rx_push(unsigned long _port)
  265. {
  266. struct dbc_request *req;
  267. struct tty_struct *tty;
  268. bool do_push = false;
  269. bool disconnect = false;
  270. struct dbc_port *port = (void *)_port;
  271. struct list_head *queue = &port->read_queue;
  272. spin_lock_irq(&port->port_lock);
  273. tty = port->port.tty;
  274. while (!list_empty(queue)) {
  275. req = list_first_entry(queue, struct dbc_request, list_pool);
  276. if (tty && tty_throttled(tty))
  277. break;
  278. switch (req->status) {
  279. case 0:
  280. break;
  281. case -ESHUTDOWN:
  282. disconnect = true;
  283. break;
  284. default:
  285. pr_warn("ttyDBC0: unexpected RX status %d\n",
  286. req->status);
  287. break;
  288. }
  289. if (req->actual) {
  290. char *packet = req->buf;
  291. unsigned int n, size = req->actual;
  292. int count;
  293. n = port->n_read;
  294. if (n) {
  295. packet += n;
  296. size -= n;
  297. }
  298. count = tty_insert_flip_string(&port->port, packet,
  299. size);
  300. if (count)
  301. do_push = true;
  302. if (count != size) {
  303. port->n_read += count;
  304. break;
  305. }
  306. port->n_read = 0;
  307. }
  308. list_move(&req->list_pool, &port->read_pool);
  309. }
  310. if (do_push)
  311. tty_flip_buffer_push(&port->port);
  312. if (!list_empty(queue) && tty) {
  313. if (!tty_throttled(tty)) {
  314. if (do_push)
  315. tasklet_schedule(&port->push);
  316. else
  317. pr_warn("ttyDBC0: RX not scheduled?\n");
  318. }
  319. }
  320. if (!disconnect)
  321. dbc_start_rx(port);
  322. spin_unlock_irq(&port->port_lock);
  323. }
  324. static int dbc_port_activate(struct tty_port *_port, struct tty_struct *tty)
  325. {
  326. struct dbc_port *port = container_of(_port, struct dbc_port, port);
  327. spin_lock_irq(&port->port_lock);
  328. dbc_start_rx(port);
  329. spin_unlock_irq(&port->port_lock);
  330. return 0;
  331. }
  332. static const struct tty_port_operations dbc_port_ops = {
  333. .activate = dbc_port_activate,
  334. };
  335. static void
  336. xhci_dbc_tty_init_port(struct xhci_hcd *xhci, struct dbc_port *port)
  337. {
  338. tty_port_init(&port->port);
  339. spin_lock_init(&port->port_lock);
  340. tasklet_init(&port->push, dbc_rx_push, (unsigned long)port);
  341. INIT_LIST_HEAD(&port->read_pool);
  342. INIT_LIST_HEAD(&port->read_queue);
  343. INIT_LIST_HEAD(&port->write_pool);
  344. port->in = get_in_ep(xhci);
  345. port->out = get_out_ep(xhci);
  346. port->port.ops = &dbc_port_ops;
  347. port->n_read = 0;
  348. }
  349. static void
  350. xhci_dbc_tty_exit_port(struct dbc_port *port)
  351. {
  352. tasklet_kill(&port->push);
  353. tty_port_destroy(&port->port);
  354. }
  355. int xhci_dbc_tty_register_device(struct xhci_hcd *xhci)
  356. {
  357. int ret;
  358. struct device *tty_dev;
  359. struct xhci_dbc *dbc = xhci->dbc;
  360. struct dbc_port *port = &dbc->port;
  361. xhci_dbc_tty_init_port(xhci, port);
  362. tty_dev = tty_port_register_device(&port->port,
  363. dbc_tty_driver, 0, NULL);
  364. ret = IS_ERR_OR_NULL(tty_dev);
  365. if (ret)
  366. goto register_fail;
  367. ret = kfifo_alloc(&port->write_fifo, DBC_WRITE_BUF_SIZE, GFP_KERNEL);
  368. if (ret)
  369. goto buf_alloc_fail;
  370. ret = xhci_dbc_alloc_requests(port->in, &port->read_pool,
  371. dbc_read_complete);
  372. if (ret)
  373. goto request_fail;
  374. ret = xhci_dbc_alloc_requests(port->out, &port->write_pool,
  375. dbc_write_complete);
  376. if (ret)
  377. goto request_fail;
  378. port->registered = true;
  379. return 0;
  380. request_fail:
  381. xhci_dbc_free_requests(port->in, &port->read_pool);
  382. xhci_dbc_free_requests(port->out, &port->write_pool);
  383. kfifo_free(&port->write_fifo);
  384. buf_alloc_fail:
  385. tty_unregister_device(dbc_tty_driver, 0);
  386. register_fail:
  387. xhci_dbc_tty_exit_port(port);
  388. xhci_err(xhci, "can't register tty port, err %d\n", ret);
  389. return ret;
  390. }
  391. void xhci_dbc_tty_unregister_device(struct xhci_hcd *xhci)
  392. {
  393. struct xhci_dbc *dbc = xhci->dbc;
  394. struct dbc_port *port = &dbc->port;
  395. tty_unregister_device(dbc_tty_driver, 0);
  396. xhci_dbc_tty_exit_port(port);
  397. port->registered = false;
  398. kfifo_free(&port->write_fifo);
  399. xhci_dbc_free_requests(get_out_ep(xhci), &port->read_pool);
  400. xhci_dbc_free_requests(get_out_ep(xhci), &port->read_queue);
  401. xhci_dbc_free_requests(get_in_ep(xhci), &port->write_pool);
  402. }