Lu Baolu | 4786d2e | 2018-05-21 16:39:49 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Lee Jones | 0e1acec | 2020-07-03 18:41:34 +0100 | [diff] [blame] | 2 | /* |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 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 | |
| 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 | |
Mathias Nyman | 4521f16 | 2020-07-23 17:45:27 +0300 | [diff] [blame] | 17 | static int dbc_tty_init(void); |
| 18 | static void dbc_tty_exit(void); |
| 19 | |
| 20 | static struct tty_driver *dbc_tty_driver; |
| 21 | |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 22 | static inline struct dbc_port *dbc_to_port(struct xhci_dbc *dbc) |
| 23 | { |
| 24 | return dbc->priv; |
| 25 | } |
| 26 | |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 27 | static unsigned int |
| 28 | dbc_send_packet(struct dbc_port *port, char *packet, unsigned int size) |
| 29 | { |
| 30 | unsigned int len; |
| 31 | |
| 32 | len = kfifo_len(&port->write_fifo); |
| 33 | if (len < size) |
| 34 | size = len; |
| 35 | if (size != 0) |
| 36 | size = kfifo_out(&port->write_fifo, packet, size); |
| 37 | return size; |
| 38 | } |
| 39 | |
| 40 | static int dbc_start_tx(struct dbc_port *port) |
| 41 | __releases(&port->port_lock) |
| 42 | __acquires(&port->port_lock) |
| 43 | { |
| 44 | int len; |
| 45 | struct dbc_request *req; |
| 46 | int status = 0; |
| 47 | bool do_tty_wake = false; |
| 48 | struct list_head *pool = &port->write_pool; |
| 49 | |
| 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); |
| 53 | if (len == 0) |
| 54 | break; |
| 55 | do_tty_wake = true; |
| 56 | |
| 57 | req->length = len; |
| 58 | list_del(&req->list_pool); |
| 59 | |
| 60 | spin_unlock(&port->port_lock); |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 61 | status = dbc_ep_queue(req); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 62 | spin_lock(&port->port_lock); |
| 63 | |
| 64 | if (status) { |
| 65 | list_add(&req->list_pool, pool); |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (do_tty_wake && port->port.tty) |
| 71 | tty_wakeup(port->port.tty); |
| 72 | |
| 73 | return status; |
| 74 | } |
| 75 | |
| 76 | static void dbc_start_rx(struct dbc_port *port) |
| 77 | __releases(&port->port_lock) |
| 78 | __acquires(&port->port_lock) |
| 79 | { |
| 80 | struct dbc_request *req; |
| 81 | int status; |
| 82 | struct list_head *pool = &port->read_pool; |
| 83 | |
| 84 | while (!list_empty(pool)) { |
| 85 | if (!port->port.tty) |
| 86 | break; |
| 87 | |
| 88 | req = list_entry(pool->next, struct dbc_request, list_pool); |
| 89 | list_del(&req->list_pool); |
| 90 | req->length = DBC_MAX_PACKET; |
| 91 | |
| 92 | spin_unlock(&port->port_lock); |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 93 | status = dbc_ep_queue(req); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 94 | spin_lock(&port->port_lock); |
| 95 | |
| 96 | if (status) { |
| 97 | list_add(&req->list_pool, pool); |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void |
Mathias Nyman | f39f3af | 2020-07-23 17:45:20 +0300 | [diff] [blame] | 104 | dbc_read_complete(struct xhci_dbc *dbc, struct dbc_request *req) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 105 | { |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 106 | unsigned long flags; |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 107 | struct dbc_port *port = dbc_to_port(dbc); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 108 | |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 109 | spin_lock_irqsave(&port->port_lock, flags); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 110 | list_add_tail(&req->list_pool, &port->read_queue); |
| 111 | tasklet_schedule(&port->push); |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 112 | spin_unlock_irqrestore(&port->port_lock, flags); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Mathias Nyman | f39f3af | 2020-07-23 17:45:20 +0300 | [diff] [blame] | 115 | static void dbc_write_complete(struct xhci_dbc *dbc, struct dbc_request *req) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 116 | { |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 117 | unsigned long flags; |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 118 | struct dbc_port *port = dbc_to_port(dbc); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 119 | |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 120 | spin_lock_irqsave(&port->port_lock, flags); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 121 | list_add(&req->list_pool, &port->write_pool); |
| 122 | switch (req->status) { |
| 123 | case 0: |
| 124 | dbc_start_tx(port); |
| 125 | break; |
| 126 | case -ESHUTDOWN: |
| 127 | break; |
| 128 | default: |
Mathias Nyman | f39f3af | 2020-07-23 17:45:20 +0300 | [diff] [blame] | 129 | dev_warn(dbc->dev, "unexpected write complete status %d\n", |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 130 | req->status); |
| 131 | break; |
| 132 | } |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 133 | spin_unlock_irqrestore(&port->port_lock, flags); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 136 | static void xhci_dbc_free_req(struct dbc_request *req) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 137 | { |
| 138 | kfree(req->buf); |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 139 | dbc_free_request(req); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static int |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 143 | xhci_dbc_alloc_requests(struct xhci_dbc *dbc, unsigned int direction, |
| 144 | struct list_head *head, |
Mathias Nyman | f39f3af | 2020-07-23 17:45:20 +0300 | [diff] [blame] | 145 | void (*fn)(struct xhci_dbc *, struct dbc_request *)) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 146 | { |
| 147 | int i; |
| 148 | struct dbc_request *req; |
| 149 | |
| 150 | for (i = 0; i < DBC_QUEUE_SIZE; i++) { |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 151 | req = dbc_alloc_request(dbc, direction, GFP_KERNEL); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 152 | if (!req) |
| 153 | break; |
| 154 | |
| 155 | req->length = DBC_MAX_PACKET; |
| 156 | req->buf = kmalloc(req->length, GFP_KERNEL); |
| 157 | if (!req->buf) { |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 158 | dbc_free_request(req); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 159 | break; |
| 160 | } |
| 161 | |
| 162 | req->complete = fn; |
| 163 | list_add_tail(&req->list_pool, head); |
| 164 | } |
| 165 | |
| 166 | return list_empty(head) ? -ENOMEM : 0; |
| 167 | } |
| 168 | |
| 169 | static void |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 170 | xhci_dbc_free_requests(struct list_head *head) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 171 | { |
| 172 | struct dbc_request *req; |
| 173 | |
| 174 | while (!list_empty(head)) { |
| 175 | req = list_entry(head->next, struct dbc_request, list_pool); |
| 176 | list_del(&req->list_pool); |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 177 | xhci_dbc_free_req(req); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | static int dbc_tty_install(struct tty_driver *driver, struct tty_struct *tty) |
| 182 | { |
| 183 | struct dbc_port *port = driver->driver_state; |
| 184 | |
| 185 | tty->driver_data = port; |
| 186 | |
| 187 | return tty_port_install(&port->port, driver, tty); |
| 188 | } |
| 189 | |
| 190 | static int dbc_tty_open(struct tty_struct *tty, struct file *file) |
| 191 | { |
| 192 | struct dbc_port *port = tty->driver_data; |
| 193 | |
| 194 | return tty_port_open(&port->port, tty, file); |
| 195 | } |
| 196 | |
| 197 | static void dbc_tty_close(struct tty_struct *tty, struct file *file) |
| 198 | { |
| 199 | struct dbc_port *port = tty->driver_data; |
| 200 | |
| 201 | tty_port_close(&port->port, tty, file); |
| 202 | } |
| 203 | |
| 204 | static int dbc_tty_write(struct tty_struct *tty, |
| 205 | const unsigned char *buf, |
| 206 | int count) |
| 207 | { |
| 208 | struct dbc_port *port = tty->driver_data; |
| 209 | unsigned long flags; |
| 210 | |
| 211 | spin_lock_irqsave(&port->port_lock, flags); |
| 212 | if (count) |
| 213 | count = kfifo_in(&port->write_fifo, buf, count); |
| 214 | dbc_start_tx(port); |
| 215 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 216 | |
| 217 | return count; |
| 218 | } |
| 219 | |
| 220 | static int dbc_tty_put_char(struct tty_struct *tty, unsigned char ch) |
| 221 | { |
| 222 | struct dbc_port *port = tty->driver_data; |
| 223 | unsigned long flags; |
| 224 | int status; |
| 225 | |
| 226 | spin_lock_irqsave(&port->port_lock, flags); |
| 227 | status = kfifo_put(&port->write_fifo, ch); |
| 228 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 229 | |
| 230 | return status; |
| 231 | } |
| 232 | |
| 233 | static void dbc_tty_flush_chars(struct tty_struct *tty) |
| 234 | { |
| 235 | struct dbc_port *port = tty->driver_data; |
| 236 | unsigned long flags; |
| 237 | |
| 238 | spin_lock_irqsave(&port->port_lock, flags); |
| 239 | dbc_start_tx(port); |
| 240 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 241 | } |
| 242 | |
| 243 | static int dbc_tty_write_room(struct tty_struct *tty) |
| 244 | { |
| 245 | struct dbc_port *port = tty->driver_data; |
| 246 | unsigned long flags; |
| 247 | int room = 0; |
| 248 | |
| 249 | spin_lock_irqsave(&port->port_lock, flags); |
| 250 | room = kfifo_avail(&port->write_fifo); |
| 251 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 252 | |
| 253 | return room; |
| 254 | } |
| 255 | |
| 256 | static int dbc_tty_chars_in_buffer(struct tty_struct *tty) |
| 257 | { |
| 258 | struct dbc_port *port = tty->driver_data; |
| 259 | unsigned long flags; |
| 260 | int chars = 0; |
| 261 | |
| 262 | spin_lock_irqsave(&port->port_lock, flags); |
| 263 | chars = kfifo_len(&port->write_fifo); |
| 264 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 265 | |
| 266 | return chars; |
| 267 | } |
| 268 | |
| 269 | static void dbc_tty_unthrottle(struct tty_struct *tty) |
| 270 | { |
| 271 | struct dbc_port *port = tty->driver_data; |
| 272 | unsigned long flags; |
| 273 | |
| 274 | spin_lock_irqsave(&port->port_lock, flags); |
| 275 | tasklet_schedule(&port->push); |
| 276 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 277 | } |
| 278 | |
| 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, |
| 289 | }; |
| 290 | |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 291 | static void dbc_rx_push(unsigned long _port) |
| 292 | { |
| 293 | struct dbc_request *req; |
| 294 | struct tty_struct *tty; |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 295 | unsigned long flags; |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 296 | bool do_push = false; |
| 297 | bool disconnect = false; |
| 298 | struct dbc_port *port = (void *)_port; |
| 299 | struct list_head *queue = &port->read_queue; |
| 300 | |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 301 | spin_lock_irqsave(&port->port_lock, flags); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 302 | tty = port->port.tty; |
| 303 | while (!list_empty(queue)) { |
| 304 | req = list_first_entry(queue, struct dbc_request, list_pool); |
| 305 | |
| 306 | if (tty && tty_throttled(tty)) |
| 307 | break; |
| 308 | |
| 309 | switch (req->status) { |
| 310 | case 0: |
| 311 | break; |
| 312 | case -ESHUTDOWN: |
| 313 | disconnect = true; |
| 314 | break; |
| 315 | default: |
| 316 | pr_warn("ttyDBC0: unexpected RX status %d\n", |
| 317 | req->status); |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | if (req->actual) { |
| 322 | char *packet = req->buf; |
| 323 | unsigned int n, size = req->actual; |
| 324 | int count; |
| 325 | |
| 326 | n = port->n_read; |
| 327 | if (n) { |
| 328 | packet += n; |
| 329 | size -= n; |
| 330 | } |
| 331 | |
| 332 | count = tty_insert_flip_string(&port->port, packet, |
| 333 | size); |
| 334 | if (count) |
| 335 | do_push = true; |
| 336 | if (count != size) { |
| 337 | port->n_read += count; |
| 338 | break; |
| 339 | } |
| 340 | port->n_read = 0; |
| 341 | } |
| 342 | |
| 343 | list_move(&req->list_pool, &port->read_pool); |
| 344 | } |
| 345 | |
| 346 | if (do_push) |
| 347 | tty_flip_buffer_push(&port->port); |
| 348 | |
| 349 | if (!list_empty(queue) && tty) { |
| 350 | if (!tty_throttled(tty)) { |
| 351 | if (do_push) |
| 352 | tasklet_schedule(&port->push); |
| 353 | else |
| 354 | pr_warn("ttyDBC0: RX not scheduled?\n"); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (!disconnect) |
| 359 | dbc_start_rx(port); |
| 360 | |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 361 | spin_unlock_irqrestore(&port->port_lock, flags); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static int dbc_port_activate(struct tty_port *_port, struct tty_struct *tty) |
| 365 | { |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 366 | unsigned long flags; |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 367 | struct dbc_port *port = container_of(_port, struct dbc_port, port); |
| 368 | |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 369 | spin_lock_irqsave(&port->port_lock, flags); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 370 | dbc_start_rx(port); |
Lu Baolu | a098dc8 | 2018-03-08 17:17:15 +0200 | [diff] [blame] | 371 | spin_unlock_irqrestore(&port->port_lock, flags); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static const struct tty_port_operations dbc_port_ops = { |
| 377 | .activate = dbc_port_activate, |
| 378 | }; |
| 379 | |
| 380 | static void |
Mathias Nyman | 91aaf97 | 2020-07-23 17:45:19 +0300 | [diff] [blame] | 381 | xhci_dbc_tty_init_port(struct xhci_dbc *dbc, struct dbc_port *port) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 382 | { |
| 383 | tty_port_init(&port->port); |
| 384 | spin_lock_init(&port->port_lock); |
| 385 | tasklet_init(&port->push, dbc_rx_push, (unsigned long)port); |
| 386 | INIT_LIST_HEAD(&port->read_pool); |
| 387 | INIT_LIST_HEAD(&port->read_queue); |
| 388 | INIT_LIST_HEAD(&port->write_pool); |
| 389 | |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 390 | port->port.ops = &dbc_port_ops; |
| 391 | port->n_read = 0; |
| 392 | } |
| 393 | |
| 394 | static void |
| 395 | xhci_dbc_tty_exit_port(struct dbc_port *port) |
| 396 | { |
| 397 | tasklet_kill(&port->push); |
| 398 | tty_port_destroy(&port->port); |
| 399 | } |
| 400 | |
Wei Yongjun | 2525291 | 2020-07-28 01:11:49 +0800 | [diff] [blame] | 401 | static int xhci_dbc_tty_register_device(struct xhci_dbc *dbc) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 402 | { |
| 403 | int ret; |
| 404 | struct device *tty_dev; |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 405 | struct dbc_port *port = dbc_to_port(dbc); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 406 | |
Mathias Nyman | 688915b | 2020-07-23 17:45:29 +0300 | [diff] [blame] | 407 | if (port->registered) |
| 408 | return -EBUSY; |
| 409 | |
Mathias Nyman | 91aaf97 | 2020-07-23 17:45:19 +0300 | [diff] [blame] | 410 | xhci_dbc_tty_init_port(dbc, port); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 411 | tty_dev = tty_port_register_device(&port->port, |
| 412 | dbc_tty_driver, 0, NULL); |
Dan Carpenter | 29f6533 | 2018-03-16 16:32:58 +0200 | [diff] [blame] | 413 | if (IS_ERR(tty_dev)) { |
| 414 | ret = PTR_ERR(tty_dev); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 415 | goto register_fail; |
Dan Carpenter | 29f6533 | 2018-03-16 16:32:58 +0200 | [diff] [blame] | 416 | } |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 417 | |
| 418 | ret = kfifo_alloc(&port->write_fifo, DBC_WRITE_BUF_SIZE, GFP_KERNEL); |
| 419 | if (ret) |
| 420 | goto buf_alloc_fail; |
| 421 | |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 422 | ret = xhci_dbc_alloc_requests(dbc, BULK_IN, &port->read_pool, |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 423 | dbc_read_complete); |
| 424 | if (ret) |
| 425 | goto request_fail; |
| 426 | |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 427 | ret = xhci_dbc_alloc_requests(dbc, BULK_OUT, &port->write_pool, |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 428 | dbc_write_complete); |
| 429 | if (ret) |
| 430 | goto request_fail; |
| 431 | |
| 432 | port->registered = true; |
| 433 | |
| 434 | return 0; |
| 435 | |
| 436 | request_fail: |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 437 | xhci_dbc_free_requests(&port->read_pool); |
| 438 | xhci_dbc_free_requests(&port->write_pool); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 439 | kfifo_free(&port->write_fifo); |
| 440 | |
| 441 | buf_alloc_fail: |
| 442 | tty_unregister_device(dbc_tty_driver, 0); |
| 443 | |
| 444 | register_fail: |
| 445 | xhci_dbc_tty_exit_port(port); |
| 446 | |
Mathias Nyman | b396fa3 | 2020-07-23 17:45:18 +0300 | [diff] [blame] | 447 | dev_err(dbc->dev, "can't register tty port, err %d\n", ret); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 448 | |
| 449 | return ret; |
| 450 | } |
| 451 | |
Wei Yongjun | 2525291 | 2020-07-28 01:11:49 +0800 | [diff] [blame] | 452 | static void xhci_dbc_tty_unregister_device(struct xhci_dbc *dbc) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 453 | { |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 454 | struct dbc_port *port = dbc_to_port(dbc); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 455 | |
Mathias Nyman | 688915b | 2020-07-23 17:45:29 +0300 | [diff] [blame] | 456 | if (!port->registered) |
| 457 | return; |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 458 | tty_unregister_device(dbc_tty_driver, 0); |
| 459 | xhci_dbc_tty_exit_port(port); |
| 460 | port->registered = false; |
| 461 | |
| 462 | kfifo_free(&port->write_fifo); |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 463 | xhci_dbc_free_requests(&port->read_pool); |
| 464 | xhci_dbc_free_requests(&port->read_queue); |
| 465 | xhci_dbc_free_requests(&port->write_pool); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 466 | } |
Mathias Nyman | 4521f16 | 2020-07-23 17:45:27 +0300 | [diff] [blame] | 467 | |
Mathias Nyman | 6ae6470 | 2020-07-23 17:45:28 +0300 | [diff] [blame] | 468 | static const struct dbc_driver dbc_driver = { |
| 469 | .configure = xhci_dbc_tty_register_device, |
| 470 | .disconnect = xhci_dbc_tty_unregister_device, |
| 471 | }; |
| 472 | |
Mathias Nyman | 4521f16 | 2020-07-23 17:45:27 +0300 | [diff] [blame] | 473 | int xhci_dbc_tty_probe(struct xhci_hcd *xhci) |
| 474 | { |
| 475 | struct xhci_dbc *dbc = xhci->dbc; |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 476 | struct dbc_port *port; |
Mathias Nyman | 4521f16 | 2020-07-23 17:45:27 +0300 | [diff] [blame] | 477 | int status; |
| 478 | |
| 479 | /* dbc_tty_init will be called by module init() in the future */ |
| 480 | status = dbc_tty_init(); |
| 481 | if (status) |
| 482 | return status; |
| 483 | |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 484 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
| 485 | if (!port) { |
| 486 | status = -ENOMEM; |
| 487 | goto out; |
| 488 | } |
Mathias Nyman | 6ae6470 | 2020-07-23 17:45:28 +0300 | [diff] [blame] | 489 | |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 490 | dbc->driver = &dbc_driver; |
| 491 | dbc->priv = port; |
| 492 | |
| 493 | |
| 494 | dbc_tty_driver->driver_state = port; |
Mathias Nyman | 4521f16 | 2020-07-23 17:45:27 +0300 | [diff] [blame] | 495 | |
| 496 | return 0; |
| 497 | out: |
Mathias Nyman | 4521f16 | 2020-07-23 17:45:27 +0300 | [diff] [blame] | 498 | /* dbc_tty_exit will be called by module_exit() in the future */ |
| 499 | dbc_tty_exit(); |
| 500 | return status; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * undo what probe did, assume dbc is stopped already. |
| 505 | * we also assume tty_unregister_device() is called before this |
| 506 | */ |
| 507 | void xhci_dbc_tty_remove(struct xhci_dbc *dbc) |
| 508 | { |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 509 | struct dbc_port *port = dbc_to_port(dbc); |
| 510 | |
Mathias Nyman | 6ae6470 | 2020-07-23 17:45:28 +0300 | [diff] [blame] | 511 | dbc->driver = NULL; |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 512 | dbc->priv = NULL; |
| 513 | kfree(port); |
Mathias Nyman | 6ae6470 | 2020-07-23 17:45:28 +0300 | [diff] [blame] | 514 | |
Mathias Nyman | 4521f16 | 2020-07-23 17:45:27 +0300 | [diff] [blame] | 515 | /* dbc_tty_exit will be called by module_exit() in the future */ |
| 516 | dbc_tty_exit(); |
| 517 | } |
| 518 | |
| 519 | static int dbc_tty_init(void) |
| 520 | { |
| 521 | int ret; |
| 522 | |
| 523 | dbc_tty_driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW | |
| 524 | TTY_DRIVER_DYNAMIC_DEV); |
| 525 | if (IS_ERR(dbc_tty_driver)) |
| 526 | return PTR_ERR(dbc_tty_driver); |
| 527 | |
| 528 | dbc_tty_driver->driver_name = "dbc_serial"; |
| 529 | dbc_tty_driver->name = "ttyDBC"; |
| 530 | |
| 531 | dbc_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 532 | dbc_tty_driver->subtype = SERIAL_TYPE_NORMAL; |
| 533 | dbc_tty_driver->init_termios = tty_std_termios; |
| 534 | dbc_tty_driver->init_termios.c_cflag = |
| 535 | B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 536 | dbc_tty_driver->init_termios.c_ispeed = 9600; |
| 537 | dbc_tty_driver->init_termios.c_ospeed = 9600; |
| 538 | |
| 539 | tty_set_operations(dbc_tty_driver, &dbc_tty_ops); |
| 540 | |
| 541 | ret = tty_register_driver(dbc_tty_driver); |
| 542 | if (ret) { |
| 543 | pr_err("Can't register dbc tty driver\n"); |
| 544 | put_tty_driver(dbc_tty_driver); |
| 545 | } |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | static void dbc_tty_exit(void) |
| 550 | { |
| 551 | if (dbc_tty_driver) { |
| 552 | tty_unregister_driver(dbc_tty_driver); |
| 553 | put_tty_driver(dbc_tty_driver); |
| 554 | dbc_tty_driver = NULL; |
| 555 | } |
| 556 | } |