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 size_t 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;
58 u8 ctrlin; /* input control lines */
59 u8 ctrlout; /* output control lines */
60 struct gb_tty_line_coding line_coding;
63 static struct tty_driver *gb_tty_driver;
64 static DEFINE_IDR(tty_minors);
65 static DEFINE_MUTEX(table_lock);
66 static atomic_t reference_count = ATOMIC_INIT(0);
68 static int gb_uart_receive_data(struct gb_tty *gb_tty,
69 struct gb_connection *connection,
70 struct gb_uart_recv_data_request *receive_data)
72 struct tty_port *port = &gb_tty->port;
75 unsigned long tty_flags = TTY_NORMAL;
77 count = gb_tty->buffer_payload_max - sizeof(*receive_data);
78 recv_data_size = le16_to_cpu(receive_data->size);
79 if (!recv_data_size || recv_data_size > count)
82 if (receive_data->flags) {
83 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
84 tty_flags = TTY_BREAK;
85 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
86 tty_flags = TTY_PARITY;
87 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
88 tty_flags = TTY_FRAME;
90 /* overrun is special, not associated with a char */
91 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
92 tty_insert_flip_char(port, 0, TTY_OVERRUN);
94 count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
95 tty_flags, recv_data_size);
96 if (count != recv_data_size) {
97 dev_err(&connection->bundle->dev,
98 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
99 recv_data_size, count);
102 tty_flip_buffer_push(port);
106 static int gb_uart_request_recv(u8 type, struct gb_operation *op)
108 struct gb_connection *connection = op->connection;
109 struct gb_tty *gb_tty = connection->private;
110 struct gb_message *request = op->request;
111 struct gb_uart_serial_state_request *serial_state;
115 case GB_UART_TYPE_RECEIVE_DATA:
116 ret = gb_uart_receive_data(gb_tty, connection,
119 case GB_UART_TYPE_SERIAL_STATE:
120 serial_state = request->payload;
121 gb_tty->ctrlin = serial_state->control;
124 dev_err(&connection->bundle->dev,
125 "unsupported unsolicited request: 0x%02x\n", type);
132 static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
134 struct gb_uart_send_data_request *request;
140 if (size > tty->buffer_payload_max)
141 size = tty->buffer_payload_max;
142 request = tty->buffer;
143 request->size = cpu_to_le16(size);
144 memcpy(&request->data[0], data, size);
145 ret = gb_operation_sync(tty->connection, GB_UART_TYPE_SEND_DATA,
146 request, sizeof(*request) + size, NULL, 0);
153 static int send_line_coding(struct gb_tty *tty)
155 struct gb_uart_set_line_coding_request request;
157 memcpy(&request, &tty->line_coding,
158 sizeof(tty->line_coding));
159 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
160 &request, sizeof(request), NULL, 0);
163 static int send_control(struct gb_tty *gb_tty, u8 control)
165 struct gb_uart_set_control_line_state_request request;
167 request.control = control;
168 return gb_operation_sync(gb_tty->connection,
169 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
170 &request, sizeof(request), NULL, 0);
173 static int send_break(struct gb_tty *gb_tty, u8 state)
175 struct gb_uart_set_break_request request;
177 if ((state != 0) && (state != 1)) {
178 dev_err(&gb_tty->connection->bundle->dev,
179 "invalid break state of %d\n", state);
183 request.state = state;
184 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
185 &request, sizeof(request), NULL, 0);
189 static struct gb_tty *get_gb_by_minor(unsigned minor)
191 struct gb_tty *gb_tty;
193 mutex_lock(&table_lock);
194 gb_tty = idr_find(&tty_minors, minor);
196 mutex_lock(&gb_tty->mutex);
197 if (gb_tty->disconnected) {
198 mutex_unlock(&gb_tty->mutex);
201 tty_port_get(&gb_tty->port);
202 mutex_unlock(&gb_tty->mutex);
205 mutex_unlock(&table_lock);
209 static int alloc_minor(struct gb_tty *gb_tty)
213 mutex_lock(&table_lock);
214 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
215 mutex_unlock(&table_lock);
217 gb_tty->minor = minor;
221 static void release_minor(struct gb_tty *gb_tty)
223 int minor = gb_tty->minor;
225 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
226 mutex_lock(&table_lock);
227 idr_remove(&tty_minors, minor);
228 mutex_unlock(&table_lock);
231 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
233 struct gb_tty *gb_tty;
236 gb_tty = get_gb_by_minor(tty->index);
240 retval = tty_standard_install(driver, tty);
244 tty->driver_data = gb_tty;
247 tty_port_put(&gb_tty->port);
251 static int gb_tty_open(struct tty_struct *tty, struct file *file)
253 struct gb_tty *gb_tty = tty->driver_data;
255 return tty_port_open(&gb_tty->port, tty, file);
258 static void gb_tty_close(struct tty_struct *tty, struct file *file)
260 struct gb_tty *gb_tty = tty->driver_data;
262 tty_port_close(&gb_tty->port, tty, file);
265 static void gb_tty_cleanup(struct tty_struct *tty)
267 struct gb_tty *gb_tty = tty->driver_data;
269 tty_port_put(&gb_tty->port);
272 static void gb_tty_hangup(struct tty_struct *tty)
274 struct gb_tty *gb_tty = tty->driver_data;
276 tty_port_hangup(&gb_tty->port);
279 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
282 struct gb_tty *gb_tty = tty->driver_data;
284 return send_data(gb_tty, count, buf);
287 static int gb_tty_write_room(struct tty_struct *tty)
289 struct gb_tty *gb_tty = tty->driver_data;
291 return gb_tty->buffer_payload_max;
294 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
299 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
301 struct gb_tty *gb_tty = tty->driver_data;
303 return send_break(gb_tty, state ? 1 : 0);
306 static void gb_tty_set_termios(struct tty_struct *tty,
307 struct ktermios *termios_old)
309 struct gb_tty *gb_tty = tty->driver_data;
310 struct ktermios *termios = &tty->termios;
311 struct gb_tty_line_coding newline;
312 u8 newctrl = gb_tty->ctrlout;
314 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
315 newline.format = termios->c_cflag & CSTOPB ?
316 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
317 newline.parity = termios->c_cflag & PARENB ?
318 (termios->c_cflag & PARODD ? 1 : 2) +
319 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
321 switch (termios->c_cflag & CSIZE) {
323 newline.data_bits = 5;
326 newline.data_bits = 6;
329 newline.data_bits = 7;
333 newline.data_bits = 8;
337 /* FIXME: needs to clear unsupported bits in the termios */
338 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
340 if (C_BAUD(tty) == B0) {
341 newline.rate = gb_tty->line_coding.rate;
342 newctrl &= GB_UART_CTRL_DTR;
343 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
344 newctrl |= GB_UART_CTRL_DTR;
347 if (newctrl != gb_tty->ctrlout) {
348 gb_tty->ctrlout = newctrl;
349 send_control(gb_tty, newctrl);
352 if (memcpy(&gb_tty->line_coding, &newline, sizeof(newline))) {
353 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
354 send_line_coding(gb_tty);
358 static int gb_tty_tiocmget(struct tty_struct *tty)
360 struct gb_tty *gb_tty = tty->driver_data;
362 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
363 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
364 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
365 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
366 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
370 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
373 struct gb_tty *gb_tty = tty->driver_data;
374 u8 newctrl = gb_tty->ctrlout;
376 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
377 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
378 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
379 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
381 newctrl = (newctrl & ~clear) | set;
382 if (gb_tty->ctrlout == newctrl)
385 gb_tty->ctrlout = newctrl;
386 return send_control(gb_tty, newctrl);
389 static void gb_tty_throttle(struct tty_struct *tty)
391 struct gb_tty *gb_tty = tty->driver_data;
392 unsigned char stop_char;
396 stop_char = STOP_CHAR(tty);
397 retval = gb_tty_write(tty, &stop_char, 1);
402 if (tty->termios.c_cflag & CRTSCTS) {
403 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
404 retval = send_control(gb_tty, gb_tty->ctrlout);
409 static void gb_tty_unthrottle(struct tty_struct *tty)
411 struct gb_tty *gb_tty = tty->driver_data;
412 unsigned char start_char;
416 start_char = START_CHAR(tty);
417 retval = gb_tty_write(tty, &start_char, 1);
422 if (tty->termios.c_cflag & CRTSCTS) {
423 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
424 retval = send_control(gb_tty, gb_tty->ctrlout);
428 static int get_serial_info(struct gb_tty *gb_tty,
429 struct serial_struct __user *info)
431 struct serial_struct tmp;
436 memset(&tmp, 0, sizeof(tmp));
437 tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
438 tmp.type = PORT_16550A;
439 tmp.line = gb_tty->minor;
440 tmp.xmit_fifo_size = 16;
441 tmp.baud_base = 9600;
442 tmp.close_delay = gb_tty->port.close_delay / 10;
443 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
444 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
446 if (copy_to_user(info, &tmp, sizeof(tmp)))
451 static int set_serial_info(struct gb_tty *gb_tty,
452 struct serial_struct __user *newinfo)
454 struct serial_struct new_serial;
455 unsigned int closing_wait;
456 unsigned int close_delay;
459 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
462 close_delay = new_serial.close_delay * 10;
463 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
464 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
466 mutex_lock(&gb_tty->port.mutex);
467 if (!capable(CAP_SYS_ADMIN)) {
468 if ((close_delay != gb_tty->port.close_delay) ||
469 (closing_wait != gb_tty->port.closing_wait))
472 retval = -EOPNOTSUPP;
474 gb_tty->port.close_delay = close_delay;
475 gb_tty->port.closing_wait = closing_wait;
477 mutex_unlock(&gb_tty->port.mutex);
481 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
484 DECLARE_WAITQUEUE(wait, current);
485 struct async_icount old;
486 struct async_icount new;
488 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
492 spin_lock_irq(&gb_tty->read_lock);
493 old = gb_tty->oldcount;
494 new = gb_tty->iocount;
495 gb_tty->oldcount = new;
496 spin_unlock_irq(&gb_tty->read_lock);
498 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
500 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
502 if ((arg & TIOCM_RI) && (old.rng != new.rng))
505 add_wait_queue(&gb_tty->wioctl, &wait);
506 set_current_state(TASK_INTERRUPTIBLE);
508 remove_wait_queue(&gb_tty->wioctl, &wait);
509 if (gb_tty->disconnected) {
513 } else if (signal_pending(current)) {
514 retval = -ERESTARTSYS;
521 static int get_serial_usage(struct gb_tty *gb_tty,
522 struct serial_icounter_struct __user *count)
524 struct serial_icounter_struct icount;
527 memset(&icount, 0, sizeof(icount));
528 icount.dsr = gb_tty->iocount.dsr;
529 icount.rng = gb_tty->iocount.rng;
530 icount.dcd = gb_tty->iocount.dcd;
531 icount.frame = gb_tty->iocount.frame;
532 icount.overrun = gb_tty->iocount.overrun;
533 icount.parity = gb_tty->iocount.parity;
534 icount.brk = gb_tty->iocount.brk;
536 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
542 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
545 struct gb_tty *gb_tty = tty->driver_data;
549 return get_serial_info(gb_tty,
550 (struct serial_struct __user *)arg);
552 return set_serial_info(gb_tty,
553 (struct serial_struct __user *)arg);
555 return wait_serial_change(gb_tty, arg);
557 return get_serial_usage(gb_tty,
558 (struct serial_icounter_struct __user *)arg);
565 static const struct tty_operations gb_ops = {
566 .install = gb_tty_install,
568 .close = gb_tty_close,
569 .cleanup = gb_tty_cleanup,
570 .hangup = gb_tty_hangup,
571 .write = gb_tty_write,
572 .write_room = gb_tty_write_room,
573 .ioctl = gb_tty_ioctl,
574 .throttle = gb_tty_throttle,
575 .unthrottle = gb_tty_unthrottle,
576 .chars_in_buffer = gb_tty_chars_in_buffer,
577 .break_ctl = gb_tty_break_ctl,
578 .set_termios = gb_tty_set_termios,
579 .tiocmget = gb_tty_tiocmget,
580 .tiocmset = gb_tty_tiocmset,
583 static struct tty_port_operations null_ops = { };
585 static int gb_tty_init(void);
586 static void gb_tty_exit(void);
588 static int gb_uart_connection_init(struct gb_connection *connection)
590 struct gb_tty *gb_tty;
591 struct device *tty_dev;
595 /* First time here, initialize the tty structures */
596 if (atomic_inc_return(&reference_count) == 1) {
597 retval = gb_tty_init();
599 atomic_dec(&reference_count);
604 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
610 gb_tty->buffer_payload_max =
611 gb_operation_get_payload_size_max(connection) -
612 sizeof(struct gb_uart_send_data_request);
614 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
615 if (!gb_tty->buffer) {
620 gb_tty->connection = connection;
621 connection->private = gb_tty;
623 minor = alloc_minor(gb_tty);
625 if (minor == -ENOSPC) {
626 dev_err(&connection->bundle->dev,
627 "no more free minor numbers\n");
635 gb_tty->minor = minor;
636 spin_lock_init(&gb_tty->write_lock);
637 spin_lock_init(&gb_tty->read_lock);
638 init_waitqueue_head(&gb_tty->wioctl);
639 mutex_init(&gb_tty->mutex);
641 tty_port_init(&gb_tty->port);
642 gb_tty->port.ops = &null_ops;
644 send_control(gb_tty, gb_tty->ctrlout);
646 /* initialize the uart to be 9600n81 */
647 gb_tty->line_coding.rate = cpu_to_le32(9600);
648 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
649 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
650 gb_tty->line_coding.data_bits = 8;
651 send_line_coding(gb_tty);
653 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
654 &connection->bundle->dev);
655 if (IS_ERR(tty_dev)) {
656 retval = PTR_ERR(tty_dev);
662 tty_port_destroy(&gb_tty->port);
663 release_minor(gb_tty);
665 connection->private = NULL;
666 kfree(gb_tty->buffer);
670 if (atomic_dec_return(&reference_count) == 0)
675 static void gb_uart_connection_exit(struct gb_connection *connection)
677 struct gb_tty *gb_tty = connection->private;
678 struct tty_struct *tty;
683 mutex_lock(&gb_tty->mutex);
684 gb_tty->disconnected = true;
686 wake_up_all(&gb_tty->wioctl);
687 connection->private = NULL;
688 mutex_unlock(&gb_tty->mutex);
690 tty = tty_port_tty_get(&gb_tty->port);
695 /* FIXME - stop all traffic */
697 tty_unregister_device(gb_tty_driver, gb_tty->minor);
699 /* FIXME - free transmit / receive buffers */
701 tty_port_destroy(&gb_tty->port);
702 kfree(gb_tty->buffer);
705 /* If last device is gone, tear down the tty structures */
706 if (atomic_dec_return(&reference_count) == 0)
710 static int gb_tty_init(void)
714 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
715 if (IS_ERR(gb_tty_driver)) {
716 pr_err("Can not allocate tty driver\n");
718 goto fail_unregister_dev;
721 gb_tty_driver->driver_name = "gb";
722 gb_tty_driver->name = GB_NAME;
723 gb_tty_driver->major = 0;
724 gb_tty_driver->minor_start = 0;
725 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
726 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
727 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
728 gb_tty_driver->init_termios = tty_std_termios;
729 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
730 tty_set_operations(gb_tty_driver, &gb_ops);
732 retval = tty_register_driver(gb_tty_driver);
734 pr_err("Can not register tty driver: %d\n", retval);
735 goto fail_put_gb_tty;
741 put_tty_driver(gb_tty_driver);
746 static void gb_tty_exit(void)
748 tty_unregister_driver(gb_tty_driver);
749 put_tty_driver(gb_tty_driver);
750 idr_destroy(&tty_minors);
753 static struct gb_protocol uart_protocol = {
755 .id = GREYBUS_PROTOCOL_UART,
756 .major = GB_UART_VERSION_MAJOR,
757 .minor = GB_UART_VERSION_MINOR,
758 .connection_init = gb_uart_connection_init,
759 .connection_exit = gb_uart_connection_exit,
760 .request_recv = gb_uart_request_recv,
763 gb_builtin_protocol_driver(uart_protocol);