xhci-dbgtty.c 12 KB

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