xhci-dbgtty.c 11 KB

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