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/signal.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/idr.h>
28 #include <linux/kdev_t.h>
29 #include <linux/kfifo.h>
30 #include <linux/workqueue.h>
31 #include <linux/completion.h>
36 #define GB_NUM_MINORS 16 /* 16 is more than enough */
37 #define GB_NAME "ttyGB"
39 #define GB_UART_WRITE_FIFO_SIZE PAGE_SIZE
40 #define GB_UART_WRITE_ROOM_MARGIN 1 /* leave some space in fifo */
41 #define GB_UART_FIRMWARE_CREDITS 4096
42 #define GB_UART_CREDIT_WAIT_TIMEOUT_MSEC 10000
44 struct gb_tty_line_coding {
53 struct gbphy_device *gbphy_dev;
56 size_t buffer_payload_max;
57 struct gb_connection *connection;
63 spinlock_t write_lock;
64 struct async_icount iocount;
65 struct async_icount oldcount;
66 wait_queue_head_t wioctl;
68 u8 ctrlin; /* input control lines */
69 u8 ctrlout; /* output control lines */
70 struct gb_tty_line_coding line_coding;
71 struct work_struct tx_work;
72 struct kfifo write_fifo;
75 struct completion credits_complete;
78 static struct tty_driver *gb_tty_driver;
79 static DEFINE_IDR(tty_minors);
80 static DEFINE_MUTEX(table_lock);
82 static int gb_uart_receive_data_handler(struct gb_operation *op)
84 struct gb_connection *connection = op->connection;
85 struct gb_tty *gb_tty = gb_connection_get_data(connection);
86 struct tty_port *port = &gb_tty->port;
87 struct gb_message *request = op->request;
88 struct gb_uart_recv_data_request *receive_data;
91 unsigned long tty_flags = TTY_NORMAL;
93 if (request->payload_size < sizeof(*receive_data)) {
94 dev_err(&gb_tty->gbphy_dev->dev,
95 "short receive-data request received (%zu < %zu)\n",
96 request->payload_size, sizeof(*receive_data));
100 receive_data = op->request->payload;
101 recv_data_size = le16_to_cpu(receive_data->size);
103 if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
104 dev_err(&gb_tty->gbphy_dev->dev,
105 "malformed receive-data request received (%u != %zu)\n",
107 request->payload_size - sizeof(*receive_data));
114 if (receive_data->flags) {
115 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
116 tty_flags = TTY_BREAK;
117 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
118 tty_flags = TTY_PARITY;
119 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
120 tty_flags = TTY_FRAME;
122 /* overrun is special, not associated with a char */
123 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
124 tty_insert_flip_char(port, 0, TTY_OVERRUN);
126 count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
127 tty_flags, recv_data_size);
128 if (count != recv_data_size) {
129 dev_err(&gb_tty->gbphy_dev->dev,
130 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
131 recv_data_size, count);
134 tty_flip_buffer_push(port);
138 static int gb_uart_serial_state_handler(struct gb_operation *op)
140 struct gb_connection *connection = op->connection;
141 struct gb_tty *gb_tty = gb_connection_get_data(connection);
142 struct gb_message *request = op->request;
143 struct gb_uart_serial_state_request *serial_state;
145 if (request->payload_size < sizeof(*serial_state)) {
146 dev_err(&gb_tty->gbphy_dev->dev,
147 "short serial-state event received (%zu < %zu)\n",
148 request->payload_size, sizeof(*serial_state));
152 serial_state = request->payload;
153 gb_tty->ctrlin = serial_state->control;
158 static int gb_uart_receive_credits_handler(struct gb_operation *op)
160 struct gb_connection *connection = op->connection;
161 struct gb_tty *gb_tty = gb_connection_get_data(connection);
162 struct gb_message *request = op->request;
163 struct gb_uart_receive_credits_request *credit_request;
165 unsigned int incoming_credits;
168 if (request->payload_size < sizeof(*credit_request)) {
169 dev_err(&gb_tty->gbphy_dev->dev,
170 "short receive_credits event received (%zu < %zu)\n",
171 request->payload_size,
172 sizeof(*credit_request));
176 credit_request = request->payload;
177 incoming_credits = le16_to_cpu(credit_request->count);
179 spin_lock_irqsave(&gb_tty->write_lock, flags);
180 gb_tty->credits += incoming_credits;
181 if (gb_tty->credits > GB_UART_FIRMWARE_CREDITS) {
182 gb_tty->credits -= incoming_credits;
185 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
188 dev_err(&gb_tty->gbphy_dev->dev,
189 "invalid number of incoming credits: %d\n",
194 if (!gb_tty->close_pending)
195 schedule_work(&gb_tty->tx_work);
198 * the port the tty layer may be waiting for credits
200 tty_port_tty_wakeup(&gb_tty->port);
202 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
203 complete(&gb_tty->credits_complete);
208 static int gb_uart_request_handler(struct gb_operation *op)
210 struct gb_connection *connection = op->connection;
211 struct gb_tty *gb_tty = gb_connection_get_data(connection);
216 case GB_UART_TYPE_RECEIVE_DATA:
217 ret = gb_uart_receive_data_handler(op);
219 case GB_UART_TYPE_SERIAL_STATE:
220 ret = gb_uart_serial_state_handler(op);
222 case GB_UART_TYPE_RECEIVE_CREDITS:
223 ret = gb_uart_receive_credits_handler(op);
226 dev_err(&gb_tty->gbphy_dev->dev,
227 "unsupported unsolicited request: 0x%02x\n", type);
234 static void gb_uart_tx_write_work(struct work_struct *work)
236 struct gb_uart_send_data_request *request;
237 struct gb_tty *gb_tty;
239 unsigned int send_size;
242 gb_tty = container_of(work, struct gb_tty, tx_work);
243 request = gb_tty->buffer;
246 if (gb_tty->close_pending)
249 spin_lock_irqsave(&gb_tty->write_lock, flags);
250 send_size = gb_tty->buffer_payload_max;
251 if (send_size > gb_tty->credits)
252 send_size = gb_tty->credits;
254 send_size = kfifo_out_peek(&gb_tty->write_fifo,
258 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
262 gb_tty->credits -= send_size;
263 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
265 request->size = cpu_to_le16(send_size);
266 ret = gb_operation_sync(gb_tty->connection,
267 GB_UART_TYPE_SEND_DATA,
268 request, sizeof(*request) + send_size,
271 dev_err(&gb_tty->gbphy_dev->dev,
272 "send data error: %d\n", ret);
273 spin_lock_irqsave(&gb_tty->write_lock, flags);
274 gb_tty->credits += send_size;
275 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
276 if (!gb_tty->close_pending)
281 spin_lock_irqsave(&gb_tty->write_lock, flags);
282 ret = kfifo_out(&gb_tty->write_fifo, &request->data[0],
284 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
286 tty_port_tty_wakeup(&gb_tty->port);
290 static int send_line_coding(struct gb_tty *tty)
292 struct gb_uart_set_line_coding_request request;
294 memcpy(&request, &tty->line_coding,
295 sizeof(tty->line_coding));
296 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
297 &request, sizeof(request), NULL, 0);
300 static int send_control(struct gb_tty *gb_tty, u8 control)
302 struct gb_uart_set_control_line_state_request request;
304 request.control = control;
305 return gb_operation_sync(gb_tty->connection,
306 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
307 &request, sizeof(request), NULL, 0);
310 static int send_break(struct gb_tty *gb_tty, u8 state)
312 struct gb_uart_set_break_request request;
314 if ((state != 0) && (state != 1)) {
315 dev_err(&gb_tty->gbphy_dev->dev,
316 "invalid break state of %d\n", state);
320 request.state = state;
321 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
322 &request, sizeof(request), NULL, 0);
325 static int gb_uart_wait_for_all_credits(struct gb_tty *gb_tty)
329 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
332 ret = wait_for_completion_timeout(&gb_tty->credits_complete,
333 msecs_to_jiffies(GB_UART_CREDIT_WAIT_TIMEOUT_MSEC));
335 dev_err(&gb_tty->gbphy_dev->dev,
336 "time out waiting for credits\n");
343 static int gb_uart_flush(struct gb_tty *gb_tty, u8 flags)
345 struct gb_uart_serial_flush_request request;
347 request.flags = flags;
348 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_FLUSH_FIFOS,
349 &request, sizeof(request), NULL, 0);
352 static struct gb_tty *get_gb_by_minor(unsigned int minor)
354 struct gb_tty *gb_tty;
356 mutex_lock(&table_lock);
357 gb_tty = idr_find(&tty_minors, minor);
359 mutex_lock(&gb_tty->mutex);
360 if (gb_tty->disconnected) {
361 mutex_unlock(&gb_tty->mutex);
364 tty_port_get(&gb_tty->port);
365 mutex_unlock(&gb_tty->mutex);
368 mutex_unlock(&table_lock);
372 static int alloc_minor(struct gb_tty *gb_tty)
376 mutex_lock(&table_lock);
377 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
378 mutex_unlock(&table_lock);
380 gb_tty->minor = minor;
384 static void release_minor(struct gb_tty *gb_tty)
386 int minor = gb_tty->minor;
388 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
389 mutex_lock(&table_lock);
390 idr_remove(&tty_minors, minor);
391 mutex_unlock(&table_lock);
394 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
396 struct gb_tty *gb_tty;
399 gb_tty = get_gb_by_minor(tty->index);
403 retval = tty_standard_install(driver, tty);
407 tty->driver_data = gb_tty;
410 tty_port_put(&gb_tty->port);
414 static int gb_tty_open(struct tty_struct *tty, struct file *file)
416 struct gb_tty *gb_tty = tty->driver_data;
418 return tty_port_open(&gb_tty->port, tty, file);
421 static void gb_tty_close(struct tty_struct *tty, struct file *file)
423 struct gb_tty *gb_tty = tty->driver_data;
425 tty_port_close(&gb_tty->port, tty, file);
428 static void gb_tty_cleanup(struct tty_struct *tty)
430 struct gb_tty *gb_tty = tty->driver_data;
432 tty_port_put(&gb_tty->port);
435 static void gb_tty_hangup(struct tty_struct *tty)
437 struct gb_tty *gb_tty = tty->driver_data;
439 tty_port_hangup(&gb_tty->port);
442 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
445 struct gb_tty *gb_tty = tty->driver_data;
447 count = kfifo_in_spinlocked(&gb_tty->write_fifo, buf, count,
448 &gb_tty->write_lock);
449 if (count && !gb_tty->close_pending)
450 schedule_work(&gb_tty->tx_work);
455 static int gb_tty_write_room(struct tty_struct *tty)
457 struct gb_tty *gb_tty = tty->driver_data;
461 spin_lock_irqsave(&gb_tty->write_lock, flags);
462 room = kfifo_avail(&gb_tty->write_fifo);
463 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
465 room -= GB_UART_WRITE_ROOM_MARGIN;
472 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
474 struct gb_tty *gb_tty = tty->driver_data;
478 spin_lock_irqsave(&gb_tty->write_lock, flags);
479 chars = kfifo_len(&gb_tty->write_fifo);
480 if (gb_tty->credits < GB_UART_FIRMWARE_CREDITS)
481 chars += GB_UART_FIRMWARE_CREDITS - gb_tty->credits;
482 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
487 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
489 struct gb_tty *gb_tty = tty->driver_data;
491 return send_break(gb_tty, state ? 1 : 0);
494 static void gb_tty_set_termios(struct tty_struct *tty,
495 struct ktermios *termios_old)
497 struct gb_tty *gb_tty = tty->driver_data;
498 struct ktermios *termios = &tty->termios;
499 struct gb_tty_line_coding newline;
500 u8 newctrl = gb_tty->ctrlout;
502 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
503 newline.format = termios->c_cflag & CSTOPB ?
504 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
505 newline.parity = termios->c_cflag & PARENB ?
506 (termios->c_cflag & PARODD ? 1 : 2) +
507 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
509 switch (termios->c_cflag & CSIZE) {
511 newline.data_bits = 5;
514 newline.data_bits = 6;
517 newline.data_bits = 7;
521 newline.data_bits = 8;
525 /* FIXME: needs to clear unsupported bits in the termios */
526 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
528 if (C_BAUD(tty) == B0) {
529 newline.rate = gb_tty->line_coding.rate;
530 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
531 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
532 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
535 if (newctrl != gb_tty->ctrlout) {
536 gb_tty->ctrlout = newctrl;
537 send_control(gb_tty, newctrl);
540 if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
541 newline.flow_control = GB_SERIAL_AUTO_RTSCTS_EN;
543 newline.flow_control = 0;
545 if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
546 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
547 send_line_coding(gb_tty);
551 static int gb_tty_tiocmget(struct tty_struct *tty)
553 struct gb_tty *gb_tty = tty->driver_data;
555 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
556 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
557 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
558 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
559 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
563 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
566 struct gb_tty *gb_tty = tty->driver_data;
567 u8 newctrl = gb_tty->ctrlout;
569 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
570 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
571 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
572 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
574 newctrl = (newctrl & ~clear) | set;
575 if (gb_tty->ctrlout == newctrl)
578 gb_tty->ctrlout = newctrl;
579 return send_control(gb_tty, newctrl);
582 static void gb_tty_throttle(struct tty_struct *tty)
584 struct gb_tty *gb_tty = tty->driver_data;
585 unsigned char stop_char;
589 stop_char = STOP_CHAR(tty);
590 retval = gb_tty_write(tty, &stop_char, 1);
595 if (tty->termios.c_cflag & CRTSCTS) {
596 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
597 retval = send_control(gb_tty, gb_tty->ctrlout);
601 static void gb_tty_unthrottle(struct tty_struct *tty)
603 struct gb_tty *gb_tty = tty->driver_data;
604 unsigned char start_char;
608 start_char = START_CHAR(tty);
609 retval = gb_tty_write(tty, &start_char, 1);
614 if (tty->termios.c_cflag & CRTSCTS) {
615 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
616 retval = send_control(gb_tty, gb_tty->ctrlout);
620 static int get_serial_info(struct gb_tty *gb_tty,
621 struct serial_struct __user *info)
623 struct serial_struct tmp;
625 memset(&tmp, 0, sizeof(tmp));
626 tmp.type = PORT_16550A;
627 tmp.line = gb_tty->minor;
628 tmp.xmit_fifo_size = 16;
629 tmp.baud_base = 9600;
630 tmp.close_delay = gb_tty->port.close_delay / 10;
632 gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
633 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
635 if (copy_to_user(info, &tmp, sizeof(tmp)))
640 static int set_serial_info(struct gb_tty *gb_tty,
641 struct serial_struct __user *newinfo)
643 struct serial_struct new_serial;
644 unsigned int closing_wait;
645 unsigned int close_delay;
648 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
651 close_delay = new_serial.close_delay * 10;
652 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
653 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
655 mutex_lock(&gb_tty->port.mutex);
656 if (!capable(CAP_SYS_ADMIN)) {
657 if ((close_delay != gb_tty->port.close_delay) ||
658 (closing_wait != gb_tty->port.closing_wait))
661 gb_tty->port.close_delay = close_delay;
662 gb_tty->port.closing_wait = closing_wait;
664 mutex_unlock(&gb_tty->port.mutex);
668 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
671 DECLARE_WAITQUEUE(wait, current);
672 struct async_icount old;
673 struct async_icount new;
675 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
679 spin_lock_irq(&gb_tty->read_lock);
680 old = gb_tty->oldcount;
681 new = gb_tty->iocount;
682 gb_tty->oldcount = new;
683 spin_unlock_irq(&gb_tty->read_lock);
685 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
687 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
689 if ((arg & TIOCM_RI) && (old.rng != new.rng))
692 add_wait_queue(&gb_tty->wioctl, &wait);
693 set_current_state(TASK_INTERRUPTIBLE);
695 remove_wait_queue(&gb_tty->wioctl, &wait);
696 if (gb_tty->disconnected) {
700 } else if (signal_pending(current)) {
701 retval = -ERESTARTSYS;
708 static int gb_tty_get_icount(struct tty_struct *tty,
709 struct serial_icounter_struct *icount)
711 struct gb_tty *gb_tty = tty->driver_data;
713 icount->dsr = gb_tty->iocount.dsr;
714 icount->rng = gb_tty->iocount.rng;
715 icount->dcd = gb_tty->iocount.dcd;
716 icount->frame = gb_tty->iocount.frame;
717 icount->overrun = gb_tty->iocount.overrun;
718 icount->parity = gb_tty->iocount.parity;
719 icount->brk = gb_tty->iocount.brk;
724 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
727 struct gb_tty *gb_tty = tty->driver_data;
731 return get_serial_info(gb_tty,
732 (struct serial_struct __user *)arg);
734 return set_serial_info(gb_tty,
735 (struct serial_struct __user *)arg);
737 return wait_serial_change(gb_tty, arg);
743 static void gb_tty_dtr_rts(struct tty_port *port, int on)
745 struct gb_tty *gb_tty;
748 gb_tty = container_of(port, struct gb_tty, port);
749 newctrl = gb_tty->ctrlout;
752 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
754 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
756 gb_tty->ctrlout = newctrl;
757 send_control(gb_tty, newctrl);
760 static int gb_tty_port_activate(struct tty_port *port,
761 struct tty_struct *tty)
763 struct gb_tty *gb_tty;
765 gb_tty = container_of(port, struct gb_tty, port);
767 return gbphy_runtime_get_sync(gb_tty->gbphy_dev);
770 static void gb_tty_port_shutdown(struct tty_port *port)
772 struct gb_tty *gb_tty;
776 gb_tty = container_of(port, struct gb_tty, port);
778 gb_tty->close_pending = true;
780 cancel_work_sync(&gb_tty->tx_work);
782 spin_lock_irqsave(&gb_tty->write_lock, flags);
783 kfifo_reset_out(&gb_tty->write_fifo);
784 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
786 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
789 ret = gb_uart_flush(gb_tty, GB_SERIAL_FLAG_FLUSH_TRANSMITTER);
791 dev_err(&gb_tty->gbphy_dev->dev,
792 "error flushing transmitter: %d\n", ret);
795 gb_uart_wait_for_all_credits(gb_tty);
798 gb_tty->close_pending = false;
800 gbphy_runtime_put_autosuspend(gb_tty->gbphy_dev);
803 static void gb_tty_port_destruct(struct tty_port *port)
805 struct gb_tty *gb_tty = container_of(port, struct gb_tty, port);
807 if (gb_tty->minor != GB_NUM_MINORS)
808 release_minor(gb_tty);
809 kfifo_free(&gb_tty->write_fifo);
810 kfree(gb_tty->buffer);
814 static const struct tty_operations gb_ops = {
815 .install = gb_tty_install,
817 .close = gb_tty_close,
818 .cleanup = gb_tty_cleanup,
819 .hangup = gb_tty_hangup,
820 .write = gb_tty_write,
821 .write_room = gb_tty_write_room,
822 .ioctl = gb_tty_ioctl,
823 .throttle = gb_tty_throttle,
824 .unthrottle = gb_tty_unthrottle,
825 .chars_in_buffer = gb_tty_chars_in_buffer,
826 .break_ctl = gb_tty_break_ctl,
827 .set_termios = gb_tty_set_termios,
828 .tiocmget = gb_tty_tiocmget,
829 .tiocmset = gb_tty_tiocmset,
830 .get_icount = gb_tty_get_icount,
833 static const struct tty_port_operations gb_port_ops = {
834 .dtr_rts = gb_tty_dtr_rts,
835 .activate = gb_tty_port_activate,
836 .shutdown = gb_tty_port_shutdown,
837 .destruct = gb_tty_port_destruct,
840 static int gb_uart_probe(struct gbphy_device *gbphy_dev,
841 const struct gbphy_device_id *id)
843 struct gb_connection *connection;
845 struct gb_tty *gb_tty;
846 struct device *tty_dev;
850 connection = gb_connection_create(gbphy_dev->bundle,
851 le16_to_cpu(gbphy_dev->cport_desc->id),
852 gb_uart_request_handler);
853 if (IS_ERR(connection))
854 return PTR_ERR(connection);
856 max_payload = gb_operation_get_payload_size_max(connection);
857 if (max_payload < sizeof(struct gb_uart_send_data_request)) {
859 goto exit_connection_destroy;
862 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
865 goto exit_connection_destroy;
868 tty_port_init(&gb_tty->port);
869 gb_tty->port.ops = &gb_port_ops;
870 gb_tty->minor = GB_NUM_MINORS;
872 gb_tty->buffer_payload_max = max_payload -
873 sizeof(struct gb_uart_send_data_request);
875 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
876 if (!gb_tty->buffer) {
881 INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
883 retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
888 gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
889 init_completion(&gb_tty->credits_complete);
891 minor = alloc_minor(gb_tty);
893 if (minor == -ENOSPC) {
894 dev_err(&gbphy_dev->dev,
895 "no more free minor numbers\n");
903 gb_tty->minor = minor;
904 spin_lock_init(&gb_tty->write_lock);
905 spin_lock_init(&gb_tty->read_lock);
906 init_waitqueue_head(&gb_tty->wioctl);
907 mutex_init(&gb_tty->mutex);
909 gb_tty->connection = connection;
910 gb_tty->gbphy_dev = gbphy_dev;
911 gb_connection_set_data(connection, gb_tty);
912 gb_gbphy_set_data(gbphy_dev, gb_tty);
914 retval = gb_connection_enable_tx(connection);
918 send_control(gb_tty, gb_tty->ctrlout);
920 /* initialize the uart to be 9600n81 */
921 gb_tty->line_coding.rate = cpu_to_le32(9600);
922 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
923 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
924 gb_tty->line_coding.data_bits = 8;
925 send_line_coding(gb_tty);
927 retval = gb_connection_enable(connection);
929 goto exit_connection_disable;
931 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
933 if (IS_ERR(tty_dev)) {
934 retval = PTR_ERR(tty_dev);
935 goto exit_connection_disable;
938 gbphy_runtime_put_autosuspend(gbphy_dev);
941 exit_connection_disable:
942 gb_connection_disable(connection);
944 tty_port_put(&gb_tty->port);
945 exit_connection_destroy:
946 gb_connection_destroy(connection);
951 static void gb_uart_remove(struct gbphy_device *gbphy_dev)
953 struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
954 struct gb_connection *connection = gb_tty->connection;
955 struct tty_struct *tty;
958 ret = gbphy_runtime_get_sync(gbphy_dev);
960 gbphy_runtime_get_noresume(gbphy_dev);
962 mutex_lock(&gb_tty->mutex);
963 gb_tty->disconnected = true;
965 wake_up_all(&gb_tty->wioctl);
966 mutex_unlock(&gb_tty->mutex);
968 tty = tty_port_tty_get(&gb_tty->port);
974 gb_connection_disable_rx(connection);
975 tty_unregister_device(gb_tty_driver, gb_tty->minor);
977 gb_connection_disable(connection);
978 gb_connection_destroy(connection);
980 tty_port_put(&gb_tty->port);
983 static int gb_tty_init(void)
987 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
988 if (IS_ERR(gb_tty_driver)) {
989 pr_err("Can not allocate tty driver\n");
991 goto fail_unregister_dev;
994 gb_tty_driver->driver_name = "gb";
995 gb_tty_driver->name = GB_NAME;
996 gb_tty_driver->major = 0;
997 gb_tty_driver->minor_start = 0;
998 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
999 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1000 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1001 gb_tty_driver->init_termios = tty_std_termios;
1002 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 |
1003 CREAD | HUPCL | CLOCAL;
1004 tty_set_operations(gb_tty_driver, &gb_ops);
1006 retval = tty_register_driver(gb_tty_driver);
1008 pr_err("Can not register tty driver: %d\n", retval);
1009 goto fail_put_gb_tty;
1015 put_tty_driver(gb_tty_driver);
1016 fail_unregister_dev:
1020 static void gb_tty_exit(void)
1022 tty_unregister_driver(gb_tty_driver);
1023 put_tty_driver(gb_tty_driver);
1024 idr_destroy(&tty_minors);
1027 static const struct gbphy_device_id gb_uart_id_table[] = {
1028 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
1031 MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
1033 static struct gbphy_driver uart_driver = {
1035 .probe = gb_uart_probe,
1036 .remove = gb_uart_remove,
1037 .id_table = gb_uart_id_table,
1040 static int gb_uart_driver_init(void)
1044 ret = gb_tty_init();
1048 ret = gb_gbphy_register(&uart_driver);
1056 module_init(gb_uart_driver_init);
1058 static void gb_uart_driver_exit(void)
1060 gb_gbphy_deregister(&uart_driver);
1064 module_exit(gb_uart_driver_exit);
1065 MODULE_LICENSE("GPL v2");