2 * UART driver for the Greybus "generic" UART module.
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
9 * Heavily based on drivers/usb/class/cdc-acm.c and
10 * drivers/usb/serial/usb-serial.c.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/wait.h>
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <linux/mutex.h>
22 #include <linux/tty.h>
23 #include <linux/serial.h>
24 #include <linux/tty_driver.h>
25 #include <linux/tty_flip.h>
26 #include <linux/serial.h>
27 #include <linux/idr.h>
29 #include <linux/kdev_t.h>
33 #define GB_NUM_MINORS 16 /* 16 is is more than enough */
34 #define GB_NAME "ttyGB"
36 struct gb_tty_line_coding {
46 u32 buffer_payload_max;
47 struct gb_connection *connection;
53 spinlock_t write_lock;
54 struct async_icount iocount;
55 struct async_icount oldcount;
56 wait_queue_head_t wioctl;
60 unsigned int ctrlin; /* input control lines */
61 unsigned int ctrlout; /* output control lines */
62 struct gb_tty_line_coding line_coding;
65 static struct tty_driver *gb_tty_driver;
66 static DEFINE_IDR(tty_minors);
67 static DEFINE_MUTEX(table_lock);
68 static atomic_t reference_count = ATOMIC_INIT(0);
70 /* Define get_version() routine */
71 define_get_version(gb_tty, UART);
73 static int gb_uart_receive_data(struct gb_tty *gb_tty,
74 struct gb_connection *connection,
75 struct gb_uart_recv_data_request *receive_data)
77 struct tty_port *port = &gb_tty->port;
80 unsigned long tty_flags = TTY_NORMAL;
82 count = gb_tty->buffer_payload_max - sizeof(*receive_data);
83 recv_data_size = le16_to_cpu(receive_data->size);
84 if (!recv_data_size || recv_data_size > count)
87 if (receive_data->flags) {
88 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
89 tty_flags = TTY_BREAK;
90 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
91 tty_flags = TTY_PARITY;
92 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
93 tty_flags = TTY_FRAME;
95 /* overrun is special, not associated with a char */
96 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
97 tty_insert_flip_char(port, 0, TTY_OVERRUN);
99 count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
100 tty_flags, recv_data_size);
101 if (count != recv_data_size) {
102 dev_err(&connection->dev,
103 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
104 recv_data_size, count);
107 tty_flip_buffer_push(port);
111 static int gb_uart_request_recv(u8 type, struct gb_operation *op)
113 struct gb_connection *connection = op->connection;
114 struct gb_tty *gb_tty = connection->private;
115 struct gb_message *request = op->request;
116 struct gb_uart_serial_state_request *serial_state;
120 case GB_UART_TYPE_RECEIVE_DATA:
121 ret = gb_uart_receive_data(gb_tty, connection,
124 case GB_UART_TYPE_SERIAL_STATE:
125 serial_state = request->payload;
126 gb_tty->ctrlin = le16_to_cpu(serial_state->control);
129 dev_err(&connection->dev,
130 "unsupported unsolicited request: %02x\n", type);
137 static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
139 struct gb_uart_send_data_request *request;
145 if (size > tty->buffer_payload_max)
146 size = tty->buffer_payload_max;
147 request = tty->buffer;
148 request->size = cpu_to_le16(size);
149 memcpy(&request->data[0], data, size);
150 ret = gb_operation_sync(tty->connection, GB_UART_TYPE_SEND_DATA,
151 request, sizeof(*request) + size, NULL, 0);
158 static int send_line_coding(struct gb_tty *tty)
160 struct gb_uart_set_line_coding_request request;
162 memcpy(&request, &tty->line_coding,
163 sizeof(tty->line_coding));
164 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
165 &request, sizeof(request), NULL, 0);
168 static int send_control(struct gb_tty *gb_tty, u16 control)
170 struct gb_uart_set_control_line_state_request request;
172 request.control = cpu_to_le16(control);
173 return gb_operation_sync(gb_tty->connection,
174 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
175 &request, sizeof(request), NULL, 0);
178 static int send_break(struct gb_tty *gb_tty, u8 state)
180 struct gb_uart_set_break_request request;
182 if ((state != 0) && (state != 1)) {
183 dev_err(&gb_tty->connection->dev,
184 "invalid break state of %d\n", state);
188 request.state = state;
189 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SET_BREAK,
190 &request, sizeof(request), NULL, 0);
194 static struct gb_tty *get_gb_by_minor(unsigned minor)
196 struct gb_tty *gb_tty;
198 mutex_lock(&table_lock);
199 gb_tty = idr_find(&tty_minors, minor);
201 mutex_lock(&gb_tty->mutex);
202 if (gb_tty->disconnected) {
203 mutex_unlock(&gb_tty->mutex);
206 tty_port_get(&gb_tty->port);
207 mutex_unlock(&gb_tty->mutex);
210 mutex_unlock(&table_lock);
214 static int alloc_minor(struct gb_tty *gb_tty)
218 mutex_lock(&table_lock);
219 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
220 mutex_unlock(&table_lock);
222 gb_tty->minor = minor;
226 static void release_minor(struct gb_tty *gb_tty)
228 int minor = gb_tty->minor;
230 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
231 mutex_lock(&table_lock);
232 idr_remove(&tty_minors, minor);
233 mutex_unlock(&table_lock);
236 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
238 struct gb_tty *gb_tty;
241 gb_tty = get_gb_by_minor(tty->index);
245 retval = tty_standard_install(driver, tty);
249 tty->driver_data = gb_tty;
252 tty_port_put(&gb_tty->port);
256 static int gb_tty_open(struct tty_struct *tty, struct file *file)
258 struct gb_tty *gb_tty = tty->driver_data;
260 return tty_port_open(&gb_tty->port, tty, file);
263 static void gb_tty_close(struct tty_struct *tty, struct file *file)
265 struct gb_tty *gb_tty = tty->driver_data;
267 tty_port_close(&gb_tty->port, tty, file);
270 static void gb_tty_cleanup(struct tty_struct *tty)
272 struct gb_tty *gb_tty = tty->driver_data;
274 tty_port_put(&gb_tty->port);
277 static void gb_tty_hangup(struct tty_struct *tty)
279 struct gb_tty *gb_tty = tty->driver_data;
281 tty_port_hangup(&gb_tty->port);
284 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
287 struct gb_tty *gb_tty = tty->driver_data;
289 return send_data(gb_tty, count, buf);
292 static int gb_tty_write_room(struct tty_struct *tty)
294 struct gb_tty *gb_tty = tty->driver_data;
296 return gb_tty->buffer_payload_max;
299 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
304 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
306 struct gb_tty *gb_tty = tty->driver_data;
308 return send_break(gb_tty, state ? 1 : 0);
311 static void gb_tty_set_termios(struct tty_struct *tty,
312 struct ktermios *termios_old)
314 struct gb_tty *gb_tty = tty->driver_data;
315 struct ktermios *termios = &tty->termios;
316 struct gb_tty_line_coding newline;
317 int newctrl = gb_tty->ctrlout;
319 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
320 newline.format = termios->c_cflag & CSTOPB ?
321 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
322 newline.parity = termios->c_cflag & PARENB ?
323 (termios->c_cflag & PARODD ? 1 : 2) +
324 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
326 switch (termios->c_cflag & CSIZE) {
328 newline.data_bits = 5;
331 newline.data_bits = 6;
334 newline.data_bits = 7;
338 newline.data_bits = 8;
342 /* FIXME: needs to clear unsupported bits in the termios */
343 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
345 if (C_BAUD(tty) == B0) {
346 newline.rate = gb_tty->line_coding.rate;
347 newctrl &= GB_UART_CTRL_DTR;
348 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
349 newctrl |= GB_UART_CTRL_DTR;
352 if (newctrl != gb_tty->ctrlout) {
353 gb_tty->ctrlout = newctrl;
354 send_control(gb_tty, newctrl);
357 if (memcpy(&gb_tty->line_coding, &newline, sizeof(newline))) {
358 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
359 send_line_coding(gb_tty);
363 static int gb_tty_tiocmget(struct tty_struct *tty)
365 struct gb_tty *gb_tty = tty->driver_data;
367 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
368 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
369 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
370 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
371 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
375 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
378 struct gb_tty *gb_tty = tty->driver_data;
379 unsigned int newctrl = gb_tty->ctrlout;
381 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
382 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
383 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
384 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
386 newctrl = (newctrl & ~clear) | set;
387 if (gb_tty->ctrlout == newctrl)
390 gb_tty->ctrlout = newctrl;
391 return send_control(gb_tty, newctrl);
394 static void gb_tty_throttle(struct tty_struct *tty)
396 struct gb_tty *gb_tty = tty->driver_data;
397 unsigned char stop_char;
401 stop_char = STOP_CHAR(tty);
402 retval = gb_tty_write(tty, &stop_char, 1);
407 if (tty->termios.c_cflag & CRTSCTS) {
408 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
409 retval = send_control(gb_tty, gb_tty->ctrlout);
414 static void gb_tty_unthrottle(struct tty_struct *tty)
416 struct gb_tty *gb_tty = tty->driver_data;
417 unsigned char start_char;
421 start_char = START_CHAR(tty);
422 retval = gb_tty_write(tty, &start_char, 1);
427 if (tty->termios.c_cflag & CRTSCTS) {
428 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
429 retval = send_control(gb_tty, gb_tty->ctrlout);
433 static int get_serial_info(struct gb_tty *gb_tty,
434 struct serial_struct __user *info)
436 struct serial_struct tmp;
441 memset(&tmp, 0, sizeof(tmp));
442 tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
443 tmp.type = PORT_16550A;
444 tmp.line = gb_tty->minor;
445 tmp.xmit_fifo_size = 16;
446 tmp.baud_base = 9600;
447 tmp.close_delay = gb_tty->port.close_delay / 10;
448 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
449 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
451 if (copy_to_user(info, &tmp, sizeof(tmp)))
456 static int set_serial_info(struct gb_tty *gb_tty,
457 struct serial_struct __user *newinfo)
459 struct serial_struct new_serial;
460 unsigned int closing_wait;
461 unsigned int close_delay;
464 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
467 close_delay = new_serial.close_delay * 10;
468 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
469 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
471 mutex_lock(&gb_tty->port.mutex);
472 if (!capable(CAP_SYS_ADMIN)) {
473 if ((close_delay != gb_tty->port.close_delay) ||
474 (closing_wait != gb_tty->port.closing_wait))
477 retval = -EOPNOTSUPP;
479 gb_tty->port.close_delay = close_delay;
480 gb_tty->port.closing_wait = closing_wait;
482 mutex_unlock(&gb_tty->port.mutex);
486 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
489 DECLARE_WAITQUEUE(wait, current);
490 struct async_icount old;
491 struct async_icount new;
493 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
497 spin_lock_irq(&gb_tty->read_lock);
498 old = gb_tty->oldcount;
499 new = gb_tty->iocount;
500 gb_tty->oldcount = new;
501 spin_unlock_irq(&gb_tty->read_lock);
503 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
505 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
507 if ((arg & TIOCM_RI) && (old.rng != new.rng))
510 add_wait_queue(&gb_tty->wioctl, &wait);
511 set_current_state(TASK_INTERRUPTIBLE);
513 remove_wait_queue(&gb_tty->wioctl, &wait);
514 if (gb_tty->disconnected) {
518 } else if (signal_pending(current)) {
519 retval = -ERESTARTSYS;
526 static int get_serial_usage(struct gb_tty *gb_tty,
527 struct serial_icounter_struct __user *count)
529 struct serial_icounter_struct icount;
532 memset(&icount, 0, sizeof(icount));
533 icount.dsr = gb_tty->iocount.dsr;
534 icount.rng = gb_tty->iocount.rng;
535 icount.dcd = gb_tty->iocount.dcd;
536 icount.frame = gb_tty->iocount.frame;
537 icount.overrun = gb_tty->iocount.overrun;
538 icount.parity = gb_tty->iocount.parity;
539 icount.brk = gb_tty->iocount.brk;
541 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
547 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
550 struct gb_tty *gb_tty = tty->driver_data;
554 return get_serial_info(gb_tty,
555 (struct serial_struct __user *)arg);
557 return set_serial_info(gb_tty,
558 (struct serial_struct __user *)arg);
560 return wait_serial_change(gb_tty, arg);
562 return get_serial_usage(gb_tty,
563 (struct serial_icounter_struct __user *)arg);
570 static const struct tty_operations gb_ops = {
571 .install = gb_tty_install,
573 .close = gb_tty_close,
574 .cleanup = gb_tty_cleanup,
575 .hangup = gb_tty_hangup,
576 .write = gb_tty_write,
577 .write_room = gb_tty_write_room,
578 .ioctl = gb_tty_ioctl,
579 .throttle = gb_tty_throttle,
580 .unthrottle = gb_tty_unthrottle,
581 .chars_in_buffer = gb_tty_chars_in_buffer,
582 .break_ctl = gb_tty_break_ctl,
583 .set_termios = gb_tty_set_termios,
584 .tiocmget = gb_tty_tiocmget,
585 .tiocmset = gb_tty_tiocmset,
588 static struct tty_port_operations null_ops = { };
590 static int gb_tty_init(void);
591 static void gb_tty_exit(void);
593 static int gb_uart_connection_init(struct gb_connection *connection)
595 struct gb_tty *gb_tty;
596 struct device *tty_dev;
600 /* First time here, initialize the tty structures */
601 if (atomic_inc_return(&reference_count) == 1) {
602 retval = gb_tty_init();
604 atomic_dec(&reference_count);
609 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
615 gb_tty->buffer_payload_max =
616 gb_operation_get_payload_size_max(connection);
617 if (!gb_tty->buffer_payload_max) {
622 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
623 if (!gb_tty->buffer) {
628 gb_tty->connection = connection;
629 connection->private = gb_tty;
631 /* Check for compatible protocol version */
632 retval = get_version(gb_tty);
636 minor = alloc_minor(gb_tty);
638 if (minor == -ENOSPC) {
639 dev_err(&connection->dev,
640 "no more free minor numbers\n");
648 gb_tty->minor = minor;
649 spin_lock_init(&gb_tty->write_lock);
650 spin_lock_init(&gb_tty->read_lock);
651 init_waitqueue_head(&gb_tty->wioctl);
652 mutex_init(&gb_tty->mutex);
654 tty_port_init(&gb_tty->port);
655 gb_tty->port.ops = &null_ops;
657 send_control(gb_tty, gb_tty->ctrlout);
659 /* initialize the uart to be 9600n81 */
660 gb_tty->line_coding.rate = cpu_to_le32(9600);
661 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
662 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
663 gb_tty->line_coding.data_bits = 8;
664 send_line_coding(gb_tty);
666 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
668 if (IS_ERR(tty_dev)) {
669 retval = PTR_ERR(tty_dev);
675 tty_port_destroy(&gb_tty->port);
676 release_minor(gb_tty);
678 connection->private = NULL;
679 kfree(gb_tty->buffer);
683 if (atomic_dec_return(&reference_count) == 0)
688 static void gb_uart_connection_exit(struct gb_connection *connection)
690 struct gb_tty *gb_tty = connection->private;
691 struct tty_struct *tty;
696 mutex_lock(&gb_tty->mutex);
697 gb_tty->disconnected = true;
699 wake_up_all(&gb_tty->wioctl);
700 connection->private = NULL;
701 mutex_unlock(&gb_tty->mutex);
703 tty = tty_port_tty_get(&gb_tty->port);
708 /* FIXME - stop all traffic */
710 tty_unregister_device(gb_tty_driver, gb_tty->minor);
712 /* FIXME - free transmit / receive buffers */
714 tty_port_put(&gb_tty->port);
715 tty_port_destroy(&gb_tty->port);
716 kfree(gb_tty->buffer);
719 /* If last device is gone, tear down the tty structures */
720 if (atomic_dec_return(&reference_count) == 0)
724 static int gb_tty_init(void)
728 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
729 if (IS_ERR(gb_tty_driver)) {
730 pr_err("Can not allocate tty driver\n");
732 goto fail_unregister_dev;
735 gb_tty_driver->driver_name = "gb";
736 gb_tty_driver->name = GB_NAME;
737 gb_tty_driver->major = 0;
738 gb_tty_driver->minor_start = 0;
739 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
740 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
741 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
742 gb_tty_driver->init_termios = tty_std_termios;
743 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
744 tty_set_operations(gb_tty_driver, &gb_ops);
746 retval = tty_register_driver(gb_tty_driver);
748 pr_err("Can not register tty driver: %d\n", retval);
749 goto fail_put_gb_tty;
755 put_tty_driver(gb_tty_driver);
760 static void gb_tty_exit(void)
762 tty_unregister_driver(gb_tty_driver);
763 put_tty_driver(gb_tty_driver);
766 static struct gb_protocol uart_protocol = {
768 .id = GREYBUS_PROTOCOL_UART,
771 .connection_init = gb_uart_connection_init,
772 .connection_exit = gb_uart_connection_exit,
773 .request_recv = gb_uart_request_recv,
776 gb_gpbridge_protocol_driver(uart_protocol);