xhci-dbgtty.c 11 KB

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