1 // SPDX-License-Identifier: GPL-2.0
3 * UART driver for the Greybus "generic" UART module.
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
8 * Heavily based on drivers/usb/class/cdc-acm.c and
9 * drivers/usb/serial/usb-serial.c.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/sched/signal.h>
17 #include <linux/wait.h>
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
20 #include <linux/mutex.h>
21 #include <linux/tty.h>
22 #include <linux/serial.h>
23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h>
25 #include <linux/idr.h>
27 #include <linux/kdev_t.h>
28 #include <linux/kfifo.h>
29 #include <linux/workqueue.h>
30 #include <linux/completion.h>
31 #include <linux/greybus.h>
35 #define GB_NUM_MINORS 16 /* 16 is more than enough */
36 #define GB_NAME "ttyGB"
38 #define GB_UART_WRITE_FIFO_SIZE PAGE_SIZE
39 #define GB_UART_WRITE_ROOM_MARGIN 1 /* leave some space in fifo */
40 #define GB_UART_FIRMWARE_CREDITS 4096
41 #define GB_UART_CREDIT_WAIT_TIMEOUT_MSEC 10000
44 struct gbphy_device *gbphy_dev;
47 size_t buffer_payload_max;
48 struct gb_connection *connection;
54 spinlock_t write_lock;
55 struct async_icount iocount;
56 struct async_icount oldcount;
57 wait_queue_head_t wioctl;
59 u8 ctrlin; /* input control lines */
60 u8 ctrlout; /* output control lines */
61 struct gb_uart_set_line_coding_request line_coding;
62 struct work_struct tx_work;
63 struct kfifo write_fifo;
66 struct completion credits_complete;
69 static struct tty_driver *gb_tty_driver;
70 static DEFINE_IDR(tty_minors);
71 static DEFINE_MUTEX(table_lock);
73 static int gb_uart_receive_data_handler(struct gb_operation *op)
75 struct gb_connection *connection = op->connection;
76 struct gb_tty *gb_tty = gb_connection_get_data(connection);
77 struct tty_port *port = &gb_tty->port;
78 struct gb_message *request = op->request;
79 struct gb_uart_recv_data_request *receive_data;
82 unsigned long tty_flags = TTY_NORMAL;
84 if (request->payload_size < sizeof(*receive_data)) {
85 dev_err(&gb_tty->gbphy_dev->dev,
86 "short receive-data request received (%zu < %zu)\n",
87 request->payload_size, sizeof(*receive_data));
91 receive_data = op->request->payload;
92 recv_data_size = le16_to_cpu(receive_data->size);
94 if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
95 dev_err(&gb_tty->gbphy_dev->dev,
96 "malformed receive-data request received (%u != %zu)\n",
98 request->payload_size - sizeof(*receive_data));
105 if (receive_data->flags) {
106 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
107 tty_flags = TTY_BREAK;
108 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
109 tty_flags = TTY_PARITY;
110 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
111 tty_flags = TTY_FRAME;
113 /* overrun is special, not associated with a char */
114 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
115 tty_insert_flip_char(port, 0, TTY_OVERRUN);
117 count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
118 tty_flags, recv_data_size);
119 if (count != recv_data_size) {
120 dev_err(&gb_tty->gbphy_dev->dev,
121 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
122 recv_data_size, count);
125 tty_flip_buffer_push(port);
129 static int gb_uart_serial_state_handler(struct gb_operation *op)
131 struct gb_connection *connection = op->connection;
132 struct gb_tty *gb_tty = gb_connection_get_data(connection);
133 struct gb_message *request = op->request;
134 struct gb_uart_serial_state_request *serial_state;
136 if (request->payload_size < sizeof(*serial_state)) {
137 dev_err(&gb_tty->gbphy_dev->dev,
138 "short serial-state event received (%zu < %zu)\n",
139 request->payload_size, sizeof(*serial_state));
143 serial_state = request->payload;
144 gb_tty->ctrlin = serial_state->control;
149 static int gb_uart_receive_credits_handler(struct gb_operation *op)
151 struct gb_connection *connection = op->connection;
152 struct gb_tty *gb_tty = gb_connection_get_data(connection);
153 struct gb_message *request = op->request;
154 struct gb_uart_receive_credits_request *credit_request;
156 unsigned int incoming_credits;
159 if (request->payload_size < sizeof(*credit_request)) {
160 dev_err(&gb_tty->gbphy_dev->dev,
161 "short receive_credits event received (%zu < %zu)\n",
162 request->payload_size,
163 sizeof(*credit_request));
167 credit_request = request->payload;
168 incoming_credits = le16_to_cpu(credit_request->count);
170 spin_lock_irqsave(&gb_tty->write_lock, flags);
171 gb_tty->credits += incoming_credits;
172 if (gb_tty->credits > GB_UART_FIRMWARE_CREDITS) {
173 gb_tty->credits -= incoming_credits;
176 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
179 dev_err(&gb_tty->gbphy_dev->dev,
180 "invalid number of incoming credits: %d\n",
185 if (!gb_tty->close_pending)
186 schedule_work(&gb_tty->tx_work);
189 * the port the tty layer may be waiting for credits
191 tty_port_tty_wakeup(&gb_tty->port);
193 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
194 complete(&gb_tty->credits_complete);
199 static int gb_uart_request_handler(struct gb_operation *op)
201 struct gb_connection *connection = op->connection;
202 struct gb_tty *gb_tty = gb_connection_get_data(connection);
207 case GB_UART_TYPE_RECEIVE_DATA:
208 ret = gb_uart_receive_data_handler(op);
210 case GB_UART_TYPE_SERIAL_STATE:
211 ret = gb_uart_serial_state_handler(op);
213 case GB_UART_TYPE_RECEIVE_CREDITS:
214 ret = gb_uart_receive_credits_handler(op);
217 dev_err(&gb_tty->gbphy_dev->dev,
218 "unsupported unsolicited request: 0x%02x\n", type);
225 static void gb_uart_tx_write_work(struct work_struct *work)
227 struct gb_uart_send_data_request *request;
228 struct gb_tty *gb_tty;
230 unsigned int send_size;
233 gb_tty = container_of(work, struct gb_tty, tx_work);
234 request = gb_tty->buffer;
237 if (gb_tty->close_pending)
240 spin_lock_irqsave(&gb_tty->write_lock, flags);
241 send_size = gb_tty->buffer_payload_max;
242 if (send_size > gb_tty->credits)
243 send_size = gb_tty->credits;
245 send_size = kfifo_out_peek(&gb_tty->write_fifo,
249 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
253 gb_tty->credits -= send_size;
254 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
256 request->size = cpu_to_le16(send_size);
257 ret = gb_operation_sync(gb_tty->connection,
258 GB_UART_TYPE_SEND_DATA,
259 request, sizeof(*request) + send_size,
262 dev_err(&gb_tty->gbphy_dev->dev,
263 "send data error: %d\n", ret);
264 spin_lock_irqsave(&gb_tty->write_lock, flags);
265 gb_tty->credits += send_size;
266 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
267 if (!gb_tty->close_pending)
272 spin_lock_irqsave(&gb_tty->write_lock, flags);
273 ret = kfifo_out(&gb_tty->write_fifo, &request->data[0],
275 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
277 tty_port_tty_wakeup(&gb_tty->port);
281 static int send_line_coding(struct gb_tty *tty)
283 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
284 &tty->line_coding, sizeof(tty->line_coding),
288 static int send_control(struct gb_tty *gb_tty, u8 control)
290 struct gb_uart_set_control_line_state_request request;
292 request.control = control;
293 return gb_operation_sync(gb_tty->connection,
294 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
295 &request, sizeof(request), NULL, 0);
298 static int send_break(struct gb_tty *gb_tty, u8 state)
300 struct gb_uart_set_break_request request;
302 if ((state != 0) && (state != 1)) {
303 dev_err(&gb_tty->gbphy_dev->dev,
304 "invalid break state of %d\n", state);
308 request.state = state;
309 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
310 &request, sizeof(request), NULL, 0);
313 static int gb_uart_wait_for_all_credits(struct gb_tty *gb_tty)
317 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
320 ret = wait_for_completion_timeout(&gb_tty->credits_complete,
321 msecs_to_jiffies(GB_UART_CREDIT_WAIT_TIMEOUT_MSEC));
323 dev_err(&gb_tty->gbphy_dev->dev,
324 "time out waiting for credits\n");
331 static int gb_uart_flush(struct gb_tty *gb_tty, u8 flags)
333 struct gb_uart_serial_flush_request request;
335 request.flags = flags;
336 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_FLUSH_FIFOS,
337 &request, sizeof(request), NULL, 0);
340 static struct gb_tty *get_gb_by_minor(unsigned int minor)
342 struct gb_tty *gb_tty;
344 mutex_lock(&table_lock);
345 gb_tty = idr_find(&tty_minors, minor);
347 mutex_lock(&gb_tty->mutex);
348 if (gb_tty->disconnected) {
349 mutex_unlock(&gb_tty->mutex);
352 tty_port_get(&gb_tty->port);
353 mutex_unlock(&gb_tty->mutex);
356 mutex_unlock(&table_lock);
360 static int alloc_minor(struct gb_tty *gb_tty)
364 mutex_lock(&table_lock);
365 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
366 mutex_unlock(&table_lock);
368 gb_tty->minor = minor;
372 static void release_minor(struct gb_tty *gb_tty)
374 int minor = gb_tty->minor;
376 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
377 mutex_lock(&table_lock);
378 idr_remove(&tty_minors, minor);
379 mutex_unlock(&table_lock);
382 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
384 struct gb_tty *gb_tty;
387 gb_tty = get_gb_by_minor(tty->index);
391 retval = tty_standard_install(driver, tty);
395 tty->driver_data = gb_tty;
398 tty_port_put(&gb_tty->port);
402 static int gb_tty_open(struct tty_struct *tty, struct file *file)
404 struct gb_tty *gb_tty = tty->driver_data;
406 return tty_port_open(&gb_tty->port, tty, file);
409 static void gb_tty_close(struct tty_struct *tty, struct file *file)
411 struct gb_tty *gb_tty = tty->driver_data;
413 tty_port_close(&gb_tty->port, tty, file);
416 static void gb_tty_cleanup(struct tty_struct *tty)
418 struct gb_tty *gb_tty = tty->driver_data;
420 tty_port_put(&gb_tty->port);
423 static void gb_tty_hangup(struct tty_struct *tty)
425 struct gb_tty *gb_tty = tty->driver_data;
427 tty_port_hangup(&gb_tty->port);
430 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
433 struct gb_tty *gb_tty = tty->driver_data;
435 count = kfifo_in_spinlocked(&gb_tty->write_fifo, buf, count,
436 &gb_tty->write_lock);
437 if (count && !gb_tty->close_pending)
438 schedule_work(&gb_tty->tx_work);
443 static unsigned int gb_tty_write_room(struct tty_struct *tty)
445 struct gb_tty *gb_tty = tty->driver_data;
449 spin_lock_irqsave(&gb_tty->write_lock, flags);
450 room = kfifo_avail(&gb_tty->write_fifo);
451 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
453 room -= GB_UART_WRITE_ROOM_MARGIN;
460 static unsigned int gb_tty_chars_in_buffer(struct tty_struct *tty)
462 struct gb_tty *gb_tty = tty->driver_data;
466 spin_lock_irqsave(&gb_tty->write_lock, flags);
467 chars = kfifo_len(&gb_tty->write_fifo);
468 if (gb_tty->credits < GB_UART_FIRMWARE_CREDITS)
469 chars += GB_UART_FIRMWARE_CREDITS - gb_tty->credits;
470 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
475 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
477 struct gb_tty *gb_tty = tty->driver_data;
479 return send_break(gb_tty, state ? 1 : 0);
482 static void gb_tty_set_termios(struct tty_struct *tty,
483 struct ktermios *termios_old)
485 struct gb_uart_set_line_coding_request newline;
486 struct gb_tty *gb_tty = tty->driver_data;
487 struct ktermios *termios = &tty->termios;
488 u8 newctrl = gb_tty->ctrlout;
490 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
491 newline.format = termios->c_cflag & CSTOPB ?
492 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
493 newline.parity = termios->c_cflag & PARENB ?
494 (termios->c_cflag & PARODD ? 1 : 2) +
495 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
497 newline.data_bits = tty_get_char_size(termios->c_cflag);
499 /* FIXME: needs to clear unsupported bits in the termios */
500 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
502 if (C_BAUD(tty) == B0) {
503 newline.rate = gb_tty->line_coding.rate;
504 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
505 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
506 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
509 if (newctrl != gb_tty->ctrlout) {
510 gb_tty->ctrlout = newctrl;
511 send_control(gb_tty, newctrl);
514 if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
515 newline.flow_control = GB_SERIAL_AUTO_RTSCTS_EN;
517 newline.flow_control = 0;
519 if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
520 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
521 send_line_coding(gb_tty);
525 static int gb_tty_tiocmget(struct tty_struct *tty)
527 struct gb_tty *gb_tty = tty->driver_data;
529 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
530 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
531 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
532 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
533 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
537 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
540 struct gb_tty *gb_tty = tty->driver_data;
541 u8 newctrl = gb_tty->ctrlout;
543 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
544 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
545 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
546 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
548 newctrl = (newctrl & ~clear) | set;
549 if (gb_tty->ctrlout == newctrl)
552 gb_tty->ctrlout = newctrl;
553 return send_control(gb_tty, newctrl);
556 static void gb_tty_throttle(struct tty_struct *tty)
558 struct gb_tty *gb_tty = tty->driver_data;
559 unsigned char stop_char;
563 stop_char = STOP_CHAR(tty);
564 retval = gb_tty_write(tty, &stop_char, 1);
569 if (tty->termios.c_cflag & CRTSCTS) {
570 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
571 retval = send_control(gb_tty, gb_tty->ctrlout);
575 static void gb_tty_unthrottle(struct tty_struct *tty)
577 struct gb_tty *gb_tty = tty->driver_data;
578 unsigned char start_char;
582 start_char = START_CHAR(tty);
583 retval = gb_tty_write(tty, &start_char, 1);
588 if (tty->termios.c_cflag & CRTSCTS) {
589 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
590 retval = send_control(gb_tty, gb_tty->ctrlout);
594 static int get_serial_info(struct tty_struct *tty,
595 struct serial_struct *ss)
597 struct gb_tty *gb_tty = tty->driver_data;
599 ss->line = gb_tty->minor;
600 ss->close_delay = jiffies_to_msecs(gb_tty->port.close_delay) / 10;
602 gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
603 ASYNC_CLOSING_WAIT_NONE :
604 jiffies_to_msecs(gb_tty->port.closing_wait) / 10;
609 static int set_serial_info(struct tty_struct *tty,
610 struct serial_struct *ss)
612 struct gb_tty *gb_tty = tty->driver_data;
613 unsigned int closing_wait;
614 unsigned int close_delay;
617 close_delay = msecs_to_jiffies(ss->close_delay * 10);
618 closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
619 ASYNC_CLOSING_WAIT_NONE :
620 msecs_to_jiffies(ss->closing_wait * 10);
622 mutex_lock(&gb_tty->port.mutex);
623 if (!capable(CAP_SYS_ADMIN)) {
624 if ((close_delay != gb_tty->port.close_delay) ||
625 (closing_wait != gb_tty->port.closing_wait))
628 gb_tty->port.close_delay = close_delay;
629 gb_tty->port.closing_wait = closing_wait;
631 mutex_unlock(&gb_tty->port.mutex);
635 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
638 DECLARE_WAITQUEUE(wait, current);
639 struct async_icount old;
640 struct async_icount new;
642 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
646 spin_lock_irq(&gb_tty->read_lock);
647 old = gb_tty->oldcount;
648 new = gb_tty->iocount;
649 gb_tty->oldcount = new;
650 spin_unlock_irq(&gb_tty->read_lock);
652 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
654 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
656 if ((arg & TIOCM_RI) && (old.rng != new.rng))
659 add_wait_queue(&gb_tty->wioctl, &wait);
660 set_current_state(TASK_INTERRUPTIBLE);
662 remove_wait_queue(&gb_tty->wioctl, &wait);
663 if (gb_tty->disconnected) {
667 } else if (signal_pending(current)) {
668 retval = -ERESTARTSYS;
675 static int gb_tty_get_icount(struct tty_struct *tty,
676 struct serial_icounter_struct *icount)
678 struct gb_tty *gb_tty = tty->driver_data;
680 icount->dsr = gb_tty->iocount.dsr;
681 icount->rng = gb_tty->iocount.rng;
682 icount->dcd = gb_tty->iocount.dcd;
683 icount->frame = gb_tty->iocount.frame;
684 icount->overrun = gb_tty->iocount.overrun;
685 icount->parity = gb_tty->iocount.parity;
686 icount->brk = gb_tty->iocount.brk;
691 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
694 struct gb_tty *gb_tty = tty->driver_data;
698 return wait_serial_change(gb_tty, arg);
704 static void gb_tty_dtr_rts(struct tty_port *port, int on)
706 struct gb_tty *gb_tty;
709 gb_tty = container_of(port, struct gb_tty, port);
710 newctrl = gb_tty->ctrlout;
713 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
715 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
717 gb_tty->ctrlout = newctrl;
718 send_control(gb_tty, newctrl);
721 static int gb_tty_port_activate(struct tty_port *port,
722 struct tty_struct *tty)
724 struct gb_tty *gb_tty;
726 gb_tty = container_of(port, struct gb_tty, port);
728 return gbphy_runtime_get_sync(gb_tty->gbphy_dev);
731 static void gb_tty_port_shutdown(struct tty_port *port)
733 struct gb_tty *gb_tty;
737 gb_tty = container_of(port, struct gb_tty, port);
739 gb_tty->close_pending = true;
741 cancel_work_sync(&gb_tty->tx_work);
743 spin_lock_irqsave(&gb_tty->write_lock, flags);
744 kfifo_reset_out(&gb_tty->write_fifo);
745 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
747 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
750 ret = gb_uart_flush(gb_tty, GB_SERIAL_FLAG_FLUSH_TRANSMITTER);
752 dev_err(&gb_tty->gbphy_dev->dev,
753 "error flushing transmitter: %d\n", ret);
756 gb_uart_wait_for_all_credits(gb_tty);
759 gb_tty->close_pending = false;
761 gbphy_runtime_put_autosuspend(gb_tty->gbphy_dev);
764 static const struct tty_operations gb_ops = {
765 .install = gb_tty_install,
767 .close = gb_tty_close,
768 .cleanup = gb_tty_cleanup,
769 .hangup = gb_tty_hangup,
770 .write = gb_tty_write,
771 .write_room = gb_tty_write_room,
772 .ioctl = gb_tty_ioctl,
773 .throttle = gb_tty_throttle,
774 .unthrottle = gb_tty_unthrottle,
775 .chars_in_buffer = gb_tty_chars_in_buffer,
776 .break_ctl = gb_tty_break_ctl,
777 .set_termios = gb_tty_set_termios,
778 .tiocmget = gb_tty_tiocmget,
779 .tiocmset = gb_tty_tiocmset,
780 .get_icount = gb_tty_get_icount,
781 .set_serial = set_serial_info,
782 .get_serial = get_serial_info,
785 static const struct tty_port_operations gb_port_ops = {
786 .dtr_rts = gb_tty_dtr_rts,
787 .activate = gb_tty_port_activate,
788 .shutdown = gb_tty_port_shutdown,
791 static int gb_uart_probe(struct gbphy_device *gbphy_dev,
792 const struct gbphy_device_id *id)
794 struct gb_connection *connection;
796 struct gb_tty *gb_tty;
797 struct device *tty_dev;
801 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
805 connection = gb_connection_create(gbphy_dev->bundle,
806 le16_to_cpu(gbphy_dev->cport_desc->id),
807 gb_uart_request_handler);
808 if (IS_ERR(connection)) {
809 retval = PTR_ERR(connection);
813 max_payload = gb_operation_get_payload_size_max(connection);
814 if (max_payload < sizeof(struct gb_uart_send_data_request)) {
816 goto exit_connection_destroy;
819 gb_tty->buffer_payload_max = max_payload -
820 sizeof(struct gb_uart_send_data_request);
822 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
823 if (!gb_tty->buffer) {
825 goto exit_connection_destroy;
828 INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
830 retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
835 gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
836 init_completion(&gb_tty->credits_complete);
838 minor = alloc_minor(gb_tty);
840 if (minor == -ENOSPC) {
841 dev_err(&gbphy_dev->dev,
842 "no more free minor numbers\n");
847 goto exit_kfifo_free;
850 gb_tty->minor = minor;
851 spin_lock_init(&gb_tty->write_lock);
852 spin_lock_init(&gb_tty->read_lock);
853 init_waitqueue_head(&gb_tty->wioctl);
854 mutex_init(&gb_tty->mutex);
856 tty_port_init(&gb_tty->port);
857 gb_tty->port.ops = &gb_port_ops;
859 gb_tty->connection = connection;
860 gb_tty->gbphy_dev = gbphy_dev;
861 gb_connection_set_data(connection, gb_tty);
862 gb_gbphy_set_data(gbphy_dev, gb_tty);
864 retval = gb_connection_enable_tx(connection);
866 goto exit_release_minor;
868 send_control(gb_tty, gb_tty->ctrlout);
870 /* initialize the uart to be 9600n81 */
871 gb_tty->line_coding.rate = cpu_to_le32(9600);
872 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
873 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
874 gb_tty->line_coding.data_bits = 8;
875 send_line_coding(gb_tty);
877 retval = gb_connection_enable(connection);
879 goto exit_connection_disable;
881 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
883 if (IS_ERR(tty_dev)) {
884 retval = PTR_ERR(tty_dev);
885 goto exit_connection_disable;
888 gbphy_runtime_put_autosuspend(gbphy_dev);
891 exit_connection_disable:
892 gb_connection_disable(connection);
894 release_minor(gb_tty);
896 kfifo_free(&gb_tty->write_fifo);
898 kfree(gb_tty->buffer);
899 exit_connection_destroy:
900 gb_connection_destroy(connection);
907 static void gb_uart_remove(struct gbphy_device *gbphy_dev)
909 struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
910 struct gb_connection *connection = gb_tty->connection;
911 struct tty_struct *tty;
914 ret = gbphy_runtime_get_sync(gbphy_dev);
916 gbphy_runtime_get_noresume(gbphy_dev);
918 mutex_lock(&gb_tty->mutex);
919 gb_tty->disconnected = true;
921 wake_up_all(&gb_tty->wioctl);
922 mutex_unlock(&gb_tty->mutex);
924 tty = tty_port_tty_get(&gb_tty->port);
930 gb_connection_disable_rx(connection);
931 tty_unregister_device(gb_tty_driver, gb_tty->minor);
933 /* FIXME - free transmit / receive buffers */
935 gb_connection_disable(connection);
936 tty_port_destroy(&gb_tty->port);
937 gb_connection_destroy(connection);
938 release_minor(gb_tty);
939 kfifo_free(&gb_tty->write_fifo);
940 kfree(gb_tty->buffer);
944 static int gb_tty_init(void)
948 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
949 if (IS_ERR(gb_tty_driver)) {
950 pr_err("Can not allocate tty driver\n");
952 goto fail_unregister_dev;
955 gb_tty_driver->driver_name = "gb";
956 gb_tty_driver->name = GB_NAME;
957 gb_tty_driver->major = 0;
958 gb_tty_driver->minor_start = 0;
959 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
960 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
961 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
962 gb_tty_driver->init_termios = tty_std_termios;
963 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 |
964 CREAD | HUPCL | CLOCAL;
965 tty_set_operations(gb_tty_driver, &gb_ops);
967 retval = tty_register_driver(gb_tty_driver);
969 pr_err("Can not register tty driver: %d\n", retval);
970 goto fail_put_gb_tty;
976 tty_driver_kref_put(gb_tty_driver);
981 static void gb_tty_exit(void)
983 tty_unregister_driver(gb_tty_driver);
984 tty_driver_kref_put(gb_tty_driver);
985 idr_destroy(&tty_minors);
988 static const struct gbphy_device_id gb_uart_id_table[] = {
989 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
992 MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
994 static struct gbphy_driver uart_driver = {
996 .probe = gb_uart_probe,
997 .remove = gb_uart_remove,
998 .id_table = gb_uart_id_table,
1001 static int gb_uart_driver_init(void)
1005 ret = gb_tty_init();
1009 ret = gb_gbphy_register(&uart_driver);
1017 module_init(gb_uart_driver_init);
1019 static void gb_uart_driver_exit(void)
1021 gb_gbphy_deregister(&uart_driver);
1025 module_exit(gb_uart_driver_exit);
1026 MODULE_LICENSE("GPL v2");