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;
23 dbc_send_packet(struct dbc_port *port, char *packet, unsigned int size)
27 len = kfifo_len(&port->write_fifo);
31 size = kfifo_out(&port->write_fifo, packet, size);
35 static int dbc_start_tx(struct dbc_port *port)
36 __releases(&port->port_lock)
37 __acquires(&port->port_lock)
40 struct dbc_request *req;
42 bool do_tty_wake = false;
43 struct list_head *pool = &port->write_pool;
45 while (!list_empty(pool)) {
46 req = list_entry(pool->next, struct dbc_request, list_pool);
47 len = dbc_send_packet(port, req->buf, DBC_MAX_PACKET);
53 list_del(&req->list_pool);
55 spin_unlock(&port->port_lock);
56 status = dbc_ep_queue(req);
57 spin_lock(&port->port_lock);
60 list_add(&req->list_pool, pool);
65 if (do_tty_wake && port->port.tty)
66 tty_wakeup(port->port.tty);
71 static void dbc_start_rx(struct dbc_port *port)
72 __releases(&port->port_lock)
73 __acquires(&port->port_lock)
75 struct dbc_request *req;
77 struct list_head *pool = &port->read_pool;
79 while (!list_empty(pool)) {
83 req = list_entry(pool->next, struct dbc_request, list_pool);
84 list_del(&req->list_pool);
85 req->length = DBC_MAX_PACKET;
87 spin_unlock(&port->port_lock);
88 status = dbc_ep_queue(req);
89 spin_lock(&port->port_lock);
92 list_add(&req->list_pool, pool);
99 dbc_read_complete(struct xhci_dbc *dbc, struct dbc_request *req)
102 struct dbc_port *port = &dbc->port;
104 spin_lock_irqsave(&port->port_lock, flags);
105 list_add_tail(&req->list_pool, &port->read_queue);
106 tasklet_schedule(&port->push);
107 spin_unlock_irqrestore(&port->port_lock, flags);
110 static void dbc_write_complete(struct xhci_dbc *dbc, struct dbc_request *req)
113 struct dbc_port *port = &dbc->port;
115 spin_lock_irqsave(&port->port_lock, flags);
116 list_add(&req->list_pool, &port->write_pool);
117 switch (req->status) {
124 dev_warn(dbc->dev, "unexpected write complete status %d\n",
128 spin_unlock_irqrestore(&port->port_lock, flags);
131 static void xhci_dbc_free_req(struct dbc_request *req)
134 dbc_free_request(req);
138 xhci_dbc_alloc_requests(struct xhci_dbc *dbc, unsigned int direction,
139 struct list_head *head,
140 void (*fn)(struct xhci_dbc *, struct dbc_request *))
143 struct dbc_request *req;
145 for (i = 0; i < DBC_QUEUE_SIZE; i++) {
146 req = dbc_alloc_request(dbc, direction, GFP_KERNEL);
150 req->length = DBC_MAX_PACKET;
151 req->buf = kmalloc(req->length, GFP_KERNEL);
153 dbc_free_request(req);
158 list_add_tail(&req->list_pool, head);
161 return list_empty(head) ? -ENOMEM : 0;
165 xhci_dbc_free_requests(struct list_head *head)
167 struct dbc_request *req;
169 while (!list_empty(head)) {
170 req = list_entry(head->next, struct dbc_request, list_pool);
171 list_del(&req->list_pool);
172 xhci_dbc_free_req(req);
176 static int dbc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
178 struct dbc_port *port = driver->driver_state;
180 tty->driver_data = port;
182 return tty_port_install(&port->port, driver, tty);
185 static int dbc_tty_open(struct tty_struct *tty, struct file *file)
187 struct dbc_port *port = tty->driver_data;
189 return tty_port_open(&port->port, tty, file);
192 static void dbc_tty_close(struct tty_struct *tty, struct file *file)
194 struct dbc_port *port = tty->driver_data;
196 tty_port_close(&port->port, tty, file);
199 static int dbc_tty_write(struct tty_struct *tty,
200 const unsigned char *buf,
203 struct dbc_port *port = tty->driver_data;
206 spin_lock_irqsave(&port->port_lock, flags);
208 count = kfifo_in(&port->write_fifo, buf, count);
210 spin_unlock_irqrestore(&port->port_lock, flags);
215 static int dbc_tty_put_char(struct tty_struct *tty, unsigned char ch)
217 struct dbc_port *port = tty->driver_data;
221 spin_lock_irqsave(&port->port_lock, flags);
222 status = kfifo_put(&port->write_fifo, ch);
223 spin_unlock_irqrestore(&port->port_lock, flags);
228 static void dbc_tty_flush_chars(struct tty_struct *tty)
230 struct dbc_port *port = tty->driver_data;
233 spin_lock_irqsave(&port->port_lock, flags);
235 spin_unlock_irqrestore(&port->port_lock, flags);
238 static int dbc_tty_write_room(struct tty_struct *tty)
240 struct dbc_port *port = tty->driver_data;
244 spin_lock_irqsave(&port->port_lock, flags);
245 room = kfifo_avail(&port->write_fifo);
246 spin_unlock_irqrestore(&port->port_lock, flags);
251 static int dbc_tty_chars_in_buffer(struct tty_struct *tty)
253 struct dbc_port *port = tty->driver_data;
257 spin_lock_irqsave(&port->port_lock, flags);
258 chars = kfifo_len(&port->write_fifo);
259 spin_unlock_irqrestore(&port->port_lock, flags);
264 static void dbc_tty_unthrottle(struct tty_struct *tty)
266 struct dbc_port *port = tty->driver_data;
269 spin_lock_irqsave(&port->port_lock, flags);
270 tasklet_schedule(&port->push);
271 spin_unlock_irqrestore(&port->port_lock, flags);
274 static const struct tty_operations dbc_tty_ops = {
275 .install = dbc_tty_install,
276 .open = dbc_tty_open,
277 .close = dbc_tty_close,
278 .write = dbc_tty_write,
279 .put_char = dbc_tty_put_char,
280 .flush_chars = dbc_tty_flush_chars,
281 .write_room = dbc_tty_write_room,
282 .chars_in_buffer = dbc_tty_chars_in_buffer,
283 .unthrottle = dbc_tty_unthrottle,
286 static void dbc_rx_push(unsigned long _port)
288 struct dbc_request *req;
289 struct tty_struct *tty;
291 bool do_push = false;
292 bool disconnect = false;
293 struct dbc_port *port = (void *)_port;
294 struct list_head *queue = &port->read_queue;
296 spin_lock_irqsave(&port->port_lock, flags);
297 tty = port->port.tty;
298 while (!list_empty(queue)) {
299 req = list_first_entry(queue, struct dbc_request, list_pool);
301 if (tty && tty_throttled(tty))
304 switch (req->status) {
311 pr_warn("ttyDBC0: unexpected RX status %d\n",
317 char *packet = req->buf;
318 unsigned int n, size = req->actual;
327 count = tty_insert_flip_string(&port->port, packet,
332 port->n_read += count;
338 list_move(&req->list_pool, &port->read_pool);
342 tty_flip_buffer_push(&port->port);
344 if (!list_empty(queue) && tty) {
345 if (!tty_throttled(tty)) {
347 tasklet_schedule(&port->push);
349 pr_warn("ttyDBC0: RX not scheduled?\n");
356 spin_unlock_irqrestore(&port->port_lock, flags);
359 static int dbc_port_activate(struct tty_port *_port, struct tty_struct *tty)
362 struct dbc_port *port = container_of(_port, struct dbc_port, port);
364 spin_lock_irqsave(&port->port_lock, flags);
366 spin_unlock_irqrestore(&port->port_lock, flags);
371 static const struct tty_port_operations dbc_port_ops = {
372 .activate = dbc_port_activate,
376 xhci_dbc_tty_init_port(struct xhci_dbc *dbc, struct dbc_port *port)
378 tty_port_init(&port->port);
379 spin_lock_init(&port->port_lock);
380 tasklet_init(&port->push, dbc_rx_push, (unsigned long)port);
381 INIT_LIST_HEAD(&port->read_pool);
382 INIT_LIST_HEAD(&port->read_queue);
383 INIT_LIST_HEAD(&port->write_pool);
385 port->port.ops = &dbc_port_ops;
390 xhci_dbc_tty_exit_port(struct dbc_port *port)
392 tasklet_kill(&port->push);
393 tty_port_destroy(&port->port);
396 int xhci_dbc_tty_register_device(struct xhci_dbc *dbc)
399 struct device *tty_dev;
400 struct dbc_port *port = &dbc->port;
402 xhci_dbc_tty_init_port(dbc, port);
403 tty_dev = tty_port_register_device(&port->port,
404 dbc_tty_driver, 0, NULL);
405 if (IS_ERR(tty_dev)) {
406 ret = PTR_ERR(tty_dev);
410 ret = kfifo_alloc(&port->write_fifo, DBC_WRITE_BUF_SIZE, GFP_KERNEL);
414 ret = xhci_dbc_alloc_requests(dbc, BULK_IN, &port->read_pool,
419 ret = xhci_dbc_alloc_requests(dbc, BULK_OUT, &port->write_pool,
424 port->registered = true;
429 xhci_dbc_free_requests(&port->read_pool);
430 xhci_dbc_free_requests(&port->write_pool);
431 kfifo_free(&port->write_fifo);
434 tty_unregister_device(dbc_tty_driver, 0);
437 xhci_dbc_tty_exit_port(port);
439 dev_err(dbc->dev, "can't register tty port, err %d\n", ret);
444 void xhci_dbc_tty_unregister_device(struct xhci_dbc *dbc)
446 struct dbc_port *port = &dbc->port;
448 tty_unregister_device(dbc_tty_driver, 0);
449 xhci_dbc_tty_exit_port(port);
450 port->registered = false;
452 kfifo_free(&port->write_fifo);
453 xhci_dbc_free_requests(&port->read_pool);
454 xhci_dbc_free_requests(&port->read_queue);
455 xhci_dbc_free_requests(&port->write_pool);
458 static const struct dbc_driver dbc_driver = {
459 .configure = xhci_dbc_tty_register_device,
460 .disconnect = xhci_dbc_tty_unregister_device,
463 int xhci_dbc_tty_probe(struct xhci_hcd *xhci)
465 struct xhci_dbc *dbc = xhci->dbc;
468 /* dbc_tty_init will be called by module init() in the future */
469 status = dbc_tty_init();
473 dbc->driver = &dbc_driver;
475 dbc_tty_driver->driver_state = &dbc->port;
480 /* dbc_tty_exit will be called by module_exit() in the future */
486 * undo what probe did, assume dbc is stopped already.
487 * we also assume tty_unregister_device() is called before this
489 void xhci_dbc_tty_remove(struct xhci_dbc *dbc)
493 /* dbc_tty_exit will be called by module_exit() in the future */
497 static int dbc_tty_init(void)
501 dbc_tty_driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW |
502 TTY_DRIVER_DYNAMIC_DEV);
503 if (IS_ERR(dbc_tty_driver))
504 return PTR_ERR(dbc_tty_driver);
506 dbc_tty_driver->driver_name = "dbc_serial";
507 dbc_tty_driver->name = "ttyDBC";
509 dbc_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
510 dbc_tty_driver->subtype = SERIAL_TYPE_NORMAL;
511 dbc_tty_driver->init_termios = tty_std_termios;
512 dbc_tty_driver->init_termios.c_cflag =
513 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
514 dbc_tty_driver->init_termios.c_ispeed = 9600;
515 dbc_tty_driver->init_termios.c_ospeed = 9600;
517 tty_set_operations(dbc_tty_driver, &dbc_tty_ops);
519 ret = tty_register_driver(dbc_tty_driver);
521 pr_err("Can't register dbc tty driver\n");
522 put_tty_driver(dbc_tty_driver);
527 static void dbc_tty_exit(void)
529 if (dbc_tty_driver) {
530 tty_unregister_driver(dbc_tty_driver);
531 put_tty_driver(dbc_tty_driver);
532 dbc_tty_driver = NULL;