1 // SPDX-License-Identifier: GPL-2.0
3 * xhci-dbgtty.c - tty glue for xHCI debug capability
5 * Copyright (C) 2017 Intel Corporation
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
10 #include <linux/slab.h>
11 #include <linux/tty.h>
12 #include <linux/tty_flip.h>
15 #include "xhci-dbgcap.h"
17 static int dbc_tty_init(void);
18 static void dbc_tty_exit(void);
20 static struct tty_driver *dbc_tty_driver;
22 static inline struct dbc_port *dbc_to_port(struct xhci_dbc *dbc)
28 dbc_send_packet(struct dbc_port *port, char *packet, unsigned int size)
32 len = kfifo_len(&port->write_fifo);
36 size = kfifo_out(&port->write_fifo, packet, size);
40 static int dbc_start_tx(struct dbc_port *port)
41 __releases(&port->port_lock)
42 __acquires(&port->port_lock)
45 struct dbc_request *req;
47 bool do_tty_wake = false;
48 struct list_head *pool = &port->write_pool;
50 while (!list_empty(pool)) {
51 req = list_entry(pool->next, struct dbc_request, list_pool);
52 len = dbc_send_packet(port, req->buf, DBC_MAX_PACKET);
58 list_del(&req->list_pool);
60 spin_unlock(&port->port_lock);
61 status = dbc_ep_queue(req);
62 spin_lock(&port->port_lock);
65 list_add(&req->list_pool, pool);
70 if (do_tty_wake && port->port.tty)
71 tty_wakeup(port->port.tty);
76 static void dbc_start_rx(struct dbc_port *port)
77 __releases(&port->port_lock)
78 __acquires(&port->port_lock)
80 struct dbc_request *req;
82 struct list_head *pool = &port->read_pool;
84 while (!list_empty(pool)) {
88 req = list_entry(pool->next, struct dbc_request, list_pool);
89 list_del(&req->list_pool);
90 req->length = DBC_MAX_PACKET;
92 spin_unlock(&port->port_lock);
93 status = dbc_ep_queue(req);
94 spin_lock(&port->port_lock);
97 list_add(&req->list_pool, pool);
104 dbc_read_complete(struct xhci_dbc *dbc, struct dbc_request *req)
107 struct dbc_port *port = dbc_to_port(dbc);
109 spin_lock_irqsave(&port->port_lock, flags);
110 list_add_tail(&req->list_pool, &port->read_queue);
111 tasklet_schedule(&port->push);
112 spin_unlock_irqrestore(&port->port_lock, flags);
115 static void dbc_write_complete(struct xhci_dbc *dbc, struct dbc_request *req)
118 struct dbc_port *port = dbc_to_port(dbc);
120 spin_lock_irqsave(&port->port_lock, flags);
121 list_add(&req->list_pool, &port->write_pool);
122 switch (req->status) {
129 dev_warn(dbc->dev, "unexpected write complete status %d\n",
133 spin_unlock_irqrestore(&port->port_lock, flags);
136 static void xhci_dbc_free_req(struct dbc_request *req)
139 dbc_free_request(req);
143 xhci_dbc_alloc_requests(struct xhci_dbc *dbc, unsigned int direction,
144 struct list_head *head,
145 void (*fn)(struct xhci_dbc *, struct dbc_request *))
148 struct dbc_request *req;
150 for (i = 0; i < DBC_QUEUE_SIZE; i++) {
151 req = dbc_alloc_request(dbc, direction, GFP_KERNEL);
155 req->length = DBC_MAX_PACKET;
156 req->buf = kmalloc(req->length, GFP_KERNEL);
158 dbc_free_request(req);
163 list_add_tail(&req->list_pool, head);
166 return list_empty(head) ? -ENOMEM : 0;
170 xhci_dbc_free_requests(struct list_head *head)
172 struct dbc_request *req;
174 while (!list_empty(head)) {
175 req = list_entry(head->next, struct dbc_request, list_pool);
176 list_del(&req->list_pool);
177 xhci_dbc_free_req(req);
181 static int dbc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
183 struct dbc_port *port = driver->driver_state;
185 tty->driver_data = port;
187 return tty_port_install(&port->port, driver, tty);
190 static int dbc_tty_open(struct tty_struct *tty, struct file *file)
192 struct dbc_port *port = tty->driver_data;
194 return tty_port_open(&port->port, tty, file);
197 static void dbc_tty_close(struct tty_struct *tty, struct file *file)
199 struct dbc_port *port = tty->driver_data;
201 tty_port_close(&port->port, tty, file);
204 static int dbc_tty_write(struct tty_struct *tty,
205 const unsigned char *buf,
208 struct dbc_port *port = tty->driver_data;
211 spin_lock_irqsave(&port->port_lock, flags);
213 count = kfifo_in(&port->write_fifo, buf, count);
215 spin_unlock_irqrestore(&port->port_lock, flags);
220 static int dbc_tty_put_char(struct tty_struct *tty, unsigned char ch)
222 struct dbc_port *port = tty->driver_data;
226 spin_lock_irqsave(&port->port_lock, flags);
227 status = kfifo_put(&port->write_fifo, ch);
228 spin_unlock_irqrestore(&port->port_lock, flags);
233 static void dbc_tty_flush_chars(struct tty_struct *tty)
235 struct dbc_port *port = tty->driver_data;
238 spin_lock_irqsave(&port->port_lock, flags);
240 spin_unlock_irqrestore(&port->port_lock, flags);
243 static unsigned int dbc_tty_write_room(struct tty_struct *tty)
245 struct dbc_port *port = tty->driver_data;
249 spin_lock_irqsave(&port->port_lock, flags);
250 room = kfifo_avail(&port->write_fifo);
251 spin_unlock_irqrestore(&port->port_lock, flags);
256 static unsigned int dbc_tty_chars_in_buffer(struct tty_struct *tty)
258 struct dbc_port *port = tty->driver_data;
262 spin_lock_irqsave(&port->port_lock, flags);
263 chars = kfifo_len(&port->write_fifo);
264 spin_unlock_irqrestore(&port->port_lock, flags);
269 static void dbc_tty_unthrottle(struct tty_struct *tty)
271 struct dbc_port *port = tty->driver_data;
274 spin_lock_irqsave(&port->port_lock, flags);
275 tasklet_schedule(&port->push);
276 spin_unlock_irqrestore(&port->port_lock, flags);
279 static const struct tty_operations dbc_tty_ops = {
280 .install = dbc_tty_install,
281 .open = dbc_tty_open,
282 .close = dbc_tty_close,
283 .write = dbc_tty_write,
284 .put_char = dbc_tty_put_char,
285 .flush_chars = dbc_tty_flush_chars,
286 .write_room = dbc_tty_write_room,
287 .chars_in_buffer = dbc_tty_chars_in_buffer,
288 .unthrottle = dbc_tty_unthrottle,
291 static void dbc_rx_push(struct tasklet_struct *t)
293 struct dbc_request *req;
294 struct tty_struct *tty;
296 bool do_push = false;
297 bool disconnect = false;
298 struct dbc_port *port = from_tasklet(port, t, push);
299 struct list_head *queue = &port->read_queue;
301 spin_lock_irqsave(&port->port_lock, flags);
302 tty = port->port.tty;
303 while (!list_empty(queue)) {
304 req = list_first_entry(queue, struct dbc_request, list_pool);
306 if (tty && tty_throttled(tty))
309 switch (req->status) {
316 pr_warn("ttyDBC0: unexpected RX status %d\n",
322 char *packet = req->buf;
323 unsigned int n, size = req->actual;
332 count = tty_insert_flip_string(&port->port, packet,
337 port->n_read += count;
343 list_move(&req->list_pool, &port->read_pool);
347 tty_flip_buffer_push(&port->port);
349 if (!list_empty(queue) && tty) {
350 if (!tty_throttled(tty)) {
352 tasklet_schedule(&port->push);
354 pr_warn("ttyDBC0: RX not scheduled?\n");
361 spin_unlock_irqrestore(&port->port_lock, flags);
364 static int dbc_port_activate(struct tty_port *_port, struct tty_struct *tty)
367 struct dbc_port *port = container_of(_port, struct dbc_port, port);
369 spin_lock_irqsave(&port->port_lock, flags);
371 spin_unlock_irqrestore(&port->port_lock, flags);
376 static const struct tty_port_operations dbc_port_ops = {
377 .activate = dbc_port_activate,
381 xhci_dbc_tty_init_port(struct xhci_dbc *dbc, struct dbc_port *port)
383 tty_port_init(&port->port);
384 spin_lock_init(&port->port_lock);
385 tasklet_setup(&port->push, dbc_rx_push);
386 INIT_LIST_HEAD(&port->read_pool);
387 INIT_LIST_HEAD(&port->read_queue);
388 INIT_LIST_HEAD(&port->write_pool);
390 port->port.ops = &dbc_port_ops;
395 xhci_dbc_tty_exit_port(struct dbc_port *port)
397 tasklet_kill(&port->push);
398 tty_port_destroy(&port->port);
401 static int xhci_dbc_tty_register_device(struct xhci_dbc *dbc)
404 struct device *tty_dev;
405 struct dbc_port *port = dbc_to_port(dbc);
407 if (port->registered)
410 xhci_dbc_tty_init_port(dbc, port);
412 ret = kfifo_alloc(&port->write_fifo, DBC_WRITE_BUF_SIZE, GFP_KERNEL);
416 ret = xhci_dbc_alloc_requests(dbc, BULK_IN, &port->read_pool,
421 ret = xhci_dbc_alloc_requests(dbc, BULK_OUT, &port->write_pool,
424 goto err_free_requests;
426 tty_dev = tty_port_register_device(&port->port,
427 dbc_tty_driver, 0, NULL);
428 if (IS_ERR(tty_dev)) {
429 ret = PTR_ERR(tty_dev);
430 goto err_free_requests;
433 port->registered = true;
438 xhci_dbc_free_requests(&port->read_pool);
439 xhci_dbc_free_requests(&port->write_pool);
441 kfifo_free(&port->write_fifo);
443 xhci_dbc_tty_exit_port(port);
445 dev_err(dbc->dev, "can't register tty port, err %d\n", ret);
450 static void xhci_dbc_tty_unregister_device(struct xhci_dbc *dbc)
452 struct dbc_port *port = dbc_to_port(dbc);
454 if (!port->registered)
456 tty_unregister_device(dbc_tty_driver, 0);
457 xhci_dbc_tty_exit_port(port);
458 port->registered = false;
460 kfifo_free(&port->write_fifo);
461 xhci_dbc_free_requests(&port->read_pool);
462 xhci_dbc_free_requests(&port->read_queue);
463 xhci_dbc_free_requests(&port->write_pool);
466 static const struct dbc_driver dbc_driver = {
467 .configure = xhci_dbc_tty_register_device,
468 .disconnect = xhci_dbc_tty_unregister_device,
471 int xhci_dbc_tty_probe(struct xhci_hcd *xhci)
473 struct xhci_dbc *dbc = xhci->dbc;
474 struct dbc_port *port;
477 /* dbc_tty_init will be called by module init() in the future */
478 status = dbc_tty_init();
482 port = kzalloc(sizeof(*port), GFP_KERNEL);
488 dbc->driver = &dbc_driver;
492 dbc_tty_driver->driver_state = port;
496 /* dbc_tty_exit will be called by module_exit() in the future */
502 * undo what probe did, assume dbc is stopped already.
503 * we also assume tty_unregister_device() is called before this
505 void xhci_dbc_tty_remove(struct xhci_dbc *dbc)
507 struct dbc_port *port = dbc_to_port(dbc);
513 /* dbc_tty_exit will be called by module_exit() in the future */
517 static int dbc_tty_init(void)
521 dbc_tty_driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW |
522 TTY_DRIVER_DYNAMIC_DEV);
523 if (IS_ERR(dbc_tty_driver))
524 return PTR_ERR(dbc_tty_driver);
526 dbc_tty_driver->driver_name = "dbc_serial";
527 dbc_tty_driver->name = "ttyDBC";
529 dbc_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
530 dbc_tty_driver->subtype = SERIAL_TYPE_NORMAL;
531 dbc_tty_driver->init_termios = tty_std_termios;
532 dbc_tty_driver->init_termios.c_cflag =
533 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
534 dbc_tty_driver->init_termios.c_ispeed = 9600;
535 dbc_tty_driver->init_termios.c_ospeed = 9600;
537 tty_set_operations(dbc_tty_driver, &dbc_tty_ops);
539 ret = tty_register_driver(dbc_tty_driver);
541 pr_err("Can't register dbc tty driver\n");
542 tty_driver_kref_put(dbc_tty_driver);
547 static void dbc_tty_exit(void)
549 if (dbc_tty_driver) {
550 tty_unregister_driver(dbc_tty_driver);
551 tty_driver_kref_put(dbc_tty_driver);
552 dbc_tty_driver = NULL;