blob: 967f0419dad725ffeb863ccc9f7c4eb2789a7a48 [file] [log] [blame]
Lu Baolu4786d2e2018-05-21 16:39:49 +03001// SPDX-License-Identifier: GPL-2.0
Lee Jones0e1acec2020-07-03 18:41:34 +01002/*
Lu Baoludfba2172017-12-08 17:59:10 +02003 * 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
10#include <linux/slab.h>
11#include <linux/tty.h>
12#include <linux/tty_flip.h>
13
14#include "xhci.h"
15#include "xhci-dbgcap.h"
16
17static unsigned int
18dbc_send_packet(struct dbc_port *port, char *packet, unsigned int size)
19{
20 unsigned int len;
21
22 len = kfifo_len(&port->write_fifo);
23 if (len < size)
24 size = len;
25 if (size != 0)
26 size = kfifo_out(&port->write_fifo, packet, size);
27 return size;
28}
29
30static int dbc_start_tx(struct dbc_port *port)
31 __releases(&port->port_lock)
32 __acquires(&port->port_lock)
33{
34 int len;
35 struct dbc_request *req;
36 int status = 0;
37 bool do_tty_wake = false;
38 struct list_head *pool = &port->write_pool;
39
40 while (!list_empty(pool)) {
41 req = list_entry(pool->next, struct dbc_request, list_pool);
42 len = dbc_send_packet(port, req->buf, DBC_MAX_PACKET);
43 if (len == 0)
44 break;
45 do_tty_wake = true;
46
47 req->length = len;
48 list_del(&req->list_pool);
49
50 spin_unlock(&port->port_lock);
51 status = dbc_ep_queue(port->out, req, GFP_ATOMIC);
52 spin_lock(&port->port_lock);
53
54 if (status) {
55 list_add(&req->list_pool, pool);
56 break;
57 }
58 }
59
60 if (do_tty_wake && port->port.tty)
61 tty_wakeup(port->port.tty);
62
63 return status;
64}
65
66static void dbc_start_rx(struct dbc_port *port)
67 __releases(&port->port_lock)
68 __acquires(&port->port_lock)
69{
70 struct dbc_request *req;
71 int status;
72 struct list_head *pool = &port->read_pool;
73
74 while (!list_empty(pool)) {
75 if (!port->port.tty)
76 break;
77
78 req = list_entry(pool->next, struct dbc_request, list_pool);
79 list_del(&req->list_pool);
80 req->length = DBC_MAX_PACKET;
81
82 spin_unlock(&port->port_lock);
83 status = dbc_ep_queue(port->in, req, GFP_ATOMIC);
84 spin_lock(&port->port_lock);
85
86 if (status) {
87 list_add(&req->list_pool, pool);
88 break;
89 }
90 }
91}
92
93static void
Mathias Nymanf39f3af2020-07-23 17:45:20 +030094dbc_read_complete(struct xhci_dbc *dbc, struct dbc_request *req)
Lu Baoludfba2172017-12-08 17:59:10 +020095{
Lu Baolua098dc82018-03-08 17:17:15 +020096 unsigned long flags;
Lu Baoludfba2172017-12-08 17:59:10 +020097 struct dbc_port *port = &dbc->port;
98
Lu Baolua098dc82018-03-08 17:17:15 +020099 spin_lock_irqsave(&port->port_lock, flags);
Lu Baoludfba2172017-12-08 17:59:10 +0200100 list_add_tail(&req->list_pool, &port->read_queue);
101 tasklet_schedule(&port->push);
Lu Baolua098dc82018-03-08 17:17:15 +0200102 spin_unlock_irqrestore(&port->port_lock, flags);
Lu Baoludfba2172017-12-08 17:59:10 +0200103}
104
Mathias Nymanf39f3af2020-07-23 17:45:20 +0300105static void dbc_write_complete(struct xhci_dbc *dbc, struct dbc_request *req)
Lu Baoludfba2172017-12-08 17:59:10 +0200106{
Lu Baolua098dc82018-03-08 17:17:15 +0200107 unsigned long flags;
Lu Baoludfba2172017-12-08 17:59:10 +0200108 struct dbc_port *port = &dbc->port;
109
Lu Baolua098dc82018-03-08 17:17:15 +0200110 spin_lock_irqsave(&port->port_lock, flags);
Lu Baoludfba2172017-12-08 17:59:10 +0200111 list_add(&req->list_pool, &port->write_pool);
112 switch (req->status) {
113 case 0:
114 dbc_start_tx(port);
115 break;
116 case -ESHUTDOWN:
117 break;
118 default:
Mathias Nymanf39f3af2020-07-23 17:45:20 +0300119 dev_warn(dbc->dev, "unexpected write complete status %d\n",
Lu Baoludfba2172017-12-08 17:59:10 +0200120 req->status);
121 break;
122 }
Lu Baolua098dc82018-03-08 17:17:15 +0200123 spin_unlock_irqrestore(&port->port_lock, flags);
Lu Baoludfba2172017-12-08 17:59:10 +0200124}
125
Colin Ian King66222f02017-12-11 16:45:36 +0000126static void xhci_dbc_free_req(struct dbc_ep *dep, struct dbc_request *req)
Lu Baoludfba2172017-12-08 17:59:10 +0200127{
128 kfree(req->buf);
129 dbc_free_request(dep, req);
130}
131
132static int
133xhci_dbc_alloc_requests(struct dbc_ep *dep, struct list_head *head,
Mathias Nymanf39f3af2020-07-23 17:45:20 +0300134 void (*fn)(struct xhci_dbc *, struct dbc_request *))
Lu Baoludfba2172017-12-08 17:59:10 +0200135{
136 int i;
137 struct dbc_request *req;
138
139 for (i = 0; i < DBC_QUEUE_SIZE; i++) {
Christophe JAILLETb62a31b2019-08-30 16:39:14 +0300140 req = dbc_alloc_request(dep, GFP_KERNEL);
Lu Baoludfba2172017-12-08 17:59:10 +0200141 if (!req)
142 break;
143
144 req->length = DBC_MAX_PACKET;
145 req->buf = kmalloc(req->length, GFP_KERNEL);
146 if (!req->buf) {
Christophe JAILLET5a030e62019-08-30 16:39:13 +0300147 dbc_free_request(dep, req);
Lu Baoludfba2172017-12-08 17:59:10 +0200148 break;
149 }
150
151 req->complete = fn;
152 list_add_tail(&req->list_pool, head);
153 }
154
155 return list_empty(head) ? -ENOMEM : 0;
156}
157
158static void
159xhci_dbc_free_requests(struct dbc_ep *dep, struct list_head *head)
160{
161 struct dbc_request *req;
162
163 while (!list_empty(head)) {
164 req = list_entry(head->next, struct dbc_request, list_pool);
165 list_del(&req->list_pool);
166 xhci_dbc_free_req(dep, req);
167 }
168}
169
170static int dbc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
171{
172 struct dbc_port *port = driver->driver_state;
173
174 tty->driver_data = port;
175
176 return tty_port_install(&port->port, driver, tty);
177}
178
179static int dbc_tty_open(struct tty_struct *tty, struct file *file)
180{
181 struct dbc_port *port = tty->driver_data;
182
183 return tty_port_open(&port->port, tty, file);
184}
185
186static void dbc_tty_close(struct tty_struct *tty, struct file *file)
187{
188 struct dbc_port *port = tty->driver_data;
189
190 tty_port_close(&port->port, tty, file);
191}
192
193static int dbc_tty_write(struct tty_struct *tty,
194 const unsigned char *buf,
195 int count)
196{
197 struct dbc_port *port = tty->driver_data;
198 unsigned long flags;
199
200 spin_lock_irqsave(&port->port_lock, flags);
201 if (count)
202 count = kfifo_in(&port->write_fifo, buf, count);
203 dbc_start_tx(port);
204 spin_unlock_irqrestore(&port->port_lock, flags);
205
206 return count;
207}
208
209static int dbc_tty_put_char(struct tty_struct *tty, unsigned char ch)
210{
211 struct dbc_port *port = tty->driver_data;
212 unsigned long flags;
213 int status;
214
215 spin_lock_irqsave(&port->port_lock, flags);
216 status = kfifo_put(&port->write_fifo, ch);
217 spin_unlock_irqrestore(&port->port_lock, flags);
218
219 return status;
220}
221
222static void dbc_tty_flush_chars(struct tty_struct *tty)
223{
224 struct dbc_port *port = tty->driver_data;
225 unsigned long flags;
226
227 spin_lock_irqsave(&port->port_lock, flags);
228 dbc_start_tx(port);
229 spin_unlock_irqrestore(&port->port_lock, flags);
230}
231
232static int dbc_tty_write_room(struct tty_struct *tty)
233{
234 struct dbc_port *port = tty->driver_data;
235 unsigned long flags;
236 int room = 0;
237
238 spin_lock_irqsave(&port->port_lock, flags);
239 room = kfifo_avail(&port->write_fifo);
240 spin_unlock_irqrestore(&port->port_lock, flags);
241
242 return room;
243}
244
245static int dbc_tty_chars_in_buffer(struct tty_struct *tty)
246{
247 struct dbc_port *port = tty->driver_data;
248 unsigned long flags;
249 int chars = 0;
250
251 spin_lock_irqsave(&port->port_lock, flags);
252 chars = kfifo_len(&port->write_fifo);
253 spin_unlock_irqrestore(&port->port_lock, flags);
254
255 return chars;
256}
257
258static void dbc_tty_unthrottle(struct tty_struct *tty)
259{
260 struct dbc_port *port = tty->driver_data;
261 unsigned long flags;
262
263 spin_lock_irqsave(&port->port_lock, flags);
264 tasklet_schedule(&port->push);
265 spin_unlock_irqrestore(&port->port_lock, flags);
266}
267
268static const struct tty_operations dbc_tty_ops = {
269 .install = dbc_tty_install,
270 .open = dbc_tty_open,
271 .close = dbc_tty_close,
272 .write = dbc_tty_write,
273 .put_char = dbc_tty_put_char,
274 .flush_chars = dbc_tty_flush_chars,
275 .write_room = dbc_tty_write_room,
276 .chars_in_buffer = dbc_tty_chars_in_buffer,
277 .unthrottle = dbc_tty_unthrottle,
278};
279
280static struct tty_driver *dbc_tty_driver;
281
282int xhci_dbc_tty_register_driver(struct xhci_hcd *xhci)
283{
284 int status;
285 struct xhci_dbc *dbc = xhci->dbc;
286
287 dbc_tty_driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW |
288 TTY_DRIVER_DYNAMIC_DEV);
289 if (IS_ERR(dbc_tty_driver)) {
290 status = PTR_ERR(dbc_tty_driver);
291 dbc_tty_driver = NULL;
292 return status;
293 }
294
295 dbc_tty_driver->driver_name = "dbc_serial";
296 dbc_tty_driver->name = "ttyDBC";
297
298 dbc_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
299 dbc_tty_driver->subtype = SERIAL_TYPE_NORMAL;
300 dbc_tty_driver->init_termios = tty_std_termios;
301 dbc_tty_driver->init_termios.c_cflag =
302 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
303 dbc_tty_driver->init_termios.c_ispeed = 9600;
304 dbc_tty_driver->init_termios.c_ospeed = 9600;
305 dbc_tty_driver->driver_state = &dbc->port;
306
307 tty_set_operations(dbc_tty_driver, &dbc_tty_ops);
308
309 status = tty_register_driver(dbc_tty_driver);
310 if (status) {
311 xhci_err(xhci,
312 "can't register dbc tty driver, err %d\n", status);
313 put_tty_driver(dbc_tty_driver);
314 dbc_tty_driver = NULL;
315 }
316
317 return status;
318}
319
320void xhci_dbc_tty_unregister_driver(void)
321{
Zhengjun Xing7fc65d42018-04-13 15:55:34 +0300322 if (dbc_tty_driver) {
323 tty_unregister_driver(dbc_tty_driver);
324 put_tty_driver(dbc_tty_driver);
325 dbc_tty_driver = NULL;
326 }
Lu Baoludfba2172017-12-08 17:59:10 +0200327}
328
329static void dbc_rx_push(unsigned long _port)
330{
331 struct dbc_request *req;
332 struct tty_struct *tty;
Lu Baolua098dc82018-03-08 17:17:15 +0200333 unsigned long flags;
Lu Baoludfba2172017-12-08 17:59:10 +0200334 bool do_push = false;
335 bool disconnect = false;
336 struct dbc_port *port = (void *)_port;
337 struct list_head *queue = &port->read_queue;
338
Lu Baolua098dc82018-03-08 17:17:15 +0200339 spin_lock_irqsave(&port->port_lock, flags);
Lu Baoludfba2172017-12-08 17:59:10 +0200340 tty = port->port.tty;
341 while (!list_empty(queue)) {
342 req = list_first_entry(queue, struct dbc_request, list_pool);
343
344 if (tty && tty_throttled(tty))
345 break;
346
347 switch (req->status) {
348 case 0:
349 break;
350 case -ESHUTDOWN:
351 disconnect = true;
352 break;
353 default:
354 pr_warn("ttyDBC0: unexpected RX status %d\n",
355 req->status);
356 break;
357 }
358
359 if (req->actual) {
360 char *packet = req->buf;
361 unsigned int n, size = req->actual;
362 int count;
363
364 n = port->n_read;
365 if (n) {
366 packet += n;
367 size -= n;
368 }
369
370 count = tty_insert_flip_string(&port->port, packet,
371 size);
372 if (count)
373 do_push = true;
374 if (count != size) {
375 port->n_read += count;
376 break;
377 }
378 port->n_read = 0;
379 }
380
381 list_move(&req->list_pool, &port->read_pool);
382 }
383
384 if (do_push)
385 tty_flip_buffer_push(&port->port);
386
387 if (!list_empty(queue) && tty) {
388 if (!tty_throttled(tty)) {
389 if (do_push)
390 tasklet_schedule(&port->push);
391 else
392 pr_warn("ttyDBC0: RX not scheduled?\n");
393 }
394 }
395
396 if (!disconnect)
397 dbc_start_rx(port);
398
Lu Baolua098dc82018-03-08 17:17:15 +0200399 spin_unlock_irqrestore(&port->port_lock, flags);
Lu Baoludfba2172017-12-08 17:59:10 +0200400}
401
402static int dbc_port_activate(struct tty_port *_port, struct tty_struct *tty)
403{
Lu Baolua098dc82018-03-08 17:17:15 +0200404 unsigned long flags;
Lu Baoludfba2172017-12-08 17:59:10 +0200405 struct dbc_port *port = container_of(_port, struct dbc_port, port);
406
Lu Baolua098dc82018-03-08 17:17:15 +0200407 spin_lock_irqsave(&port->port_lock, flags);
Lu Baoludfba2172017-12-08 17:59:10 +0200408 dbc_start_rx(port);
Lu Baolua098dc82018-03-08 17:17:15 +0200409 spin_unlock_irqrestore(&port->port_lock, flags);
Lu Baoludfba2172017-12-08 17:59:10 +0200410
411 return 0;
412}
413
414static const struct tty_port_operations dbc_port_ops = {
415 .activate = dbc_port_activate,
416};
417
418static void
Mathias Nyman91aaf972020-07-23 17:45:19 +0300419xhci_dbc_tty_init_port(struct xhci_dbc *dbc, struct dbc_port *port)
Lu Baoludfba2172017-12-08 17:59:10 +0200420{
421 tty_port_init(&port->port);
422 spin_lock_init(&port->port_lock);
423 tasklet_init(&port->push, dbc_rx_push, (unsigned long)port);
424 INIT_LIST_HEAD(&port->read_pool);
425 INIT_LIST_HEAD(&port->read_queue);
426 INIT_LIST_HEAD(&port->write_pool);
427
Mathias Nyman91aaf972020-07-23 17:45:19 +0300428 port->in = get_in_ep(dbc);
429 port->out = get_out_ep(dbc);
Lu Baoludfba2172017-12-08 17:59:10 +0200430 port->port.ops = &dbc_port_ops;
431 port->n_read = 0;
432}
433
434static void
435xhci_dbc_tty_exit_port(struct dbc_port *port)
436{
437 tasklet_kill(&port->push);
438 tty_port_destroy(&port->port);
439}
440
Mathias Nymanb396fa32020-07-23 17:45:18 +0300441int xhci_dbc_tty_register_device(struct xhci_dbc *dbc)
Lu Baoludfba2172017-12-08 17:59:10 +0200442{
443 int ret;
444 struct device *tty_dev;
Lu Baoludfba2172017-12-08 17:59:10 +0200445 struct dbc_port *port = &dbc->port;
446
Mathias Nyman91aaf972020-07-23 17:45:19 +0300447 xhci_dbc_tty_init_port(dbc, port);
Lu Baoludfba2172017-12-08 17:59:10 +0200448 tty_dev = tty_port_register_device(&port->port,
449 dbc_tty_driver, 0, NULL);
Dan Carpenter29f65332018-03-16 16:32:58 +0200450 if (IS_ERR(tty_dev)) {
451 ret = PTR_ERR(tty_dev);
Lu Baoludfba2172017-12-08 17:59:10 +0200452 goto register_fail;
Dan Carpenter29f65332018-03-16 16:32:58 +0200453 }
Lu Baoludfba2172017-12-08 17:59:10 +0200454
455 ret = kfifo_alloc(&port->write_fifo, DBC_WRITE_BUF_SIZE, GFP_KERNEL);
456 if (ret)
457 goto buf_alloc_fail;
458
459 ret = xhci_dbc_alloc_requests(port->in, &port->read_pool,
460 dbc_read_complete);
461 if (ret)
462 goto request_fail;
463
464 ret = xhci_dbc_alloc_requests(port->out, &port->write_pool,
465 dbc_write_complete);
466 if (ret)
467 goto request_fail;
468
469 port->registered = true;
470
471 return 0;
472
473request_fail:
474 xhci_dbc_free_requests(port->in, &port->read_pool);
475 xhci_dbc_free_requests(port->out, &port->write_pool);
476 kfifo_free(&port->write_fifo);
477
478buf_alloc_fail:
479 tty_unregister_device(dbc_tty_driver, 0);
480
481register_fail:
482 xhci_dbc_tty_exit_port(port);
483
Mathias Nymanb396fa32020-07-23 17:45:18 +0300484 dev_err(dbc->dev, "can't register tty port, err %d\n", ret);
Lu Baoludfba2172017-12-08 17:59:10 +0200485
486 return ret;
487}
488
Mathias Nymanb396fa32020-07-23 17:45:18 +0300489void xhci_dbc_tty_unregister_device(struct xhci_dbc *dbc)
Lu Baoludfba2172017-12-08 17:59:10 +0200490{
Lu Baoludfba2172017-12-08 17:59:10 +0200491 struct dbc_port *port = &dbc->port;
492
493 tty_unregister_device(dbc_tty_driver, 0);
494 xhci_dbc_tty_exit_port(port);
495 port->registered = false;
496
497 kfifo_free(&port->write_fifo);
Mathias Nyman91aaf972020-07-23 17:45:19 +0300498 xhci_dbc_free_requests(get_out_ep(dbc), &port->read_pool);
499 xhci_dbc_free_requests(get_out_ep(dbc), &port->read_queue);
500 xhci_dbc_free_requests(get_in_ep(dbc), &port->write_pool);
Lu Baoludfba2172017-12-08 17:59:10 +0200501}