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>
30 #include <linux/kfifo.h>
31 #include <linux/workqueue.h>
36 #define GB_NUM_MINORS 16 /* 16 is 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
43 struct gb_tty_line_coding
{
52 struct gbphy_device
*gbphy_dev
;
55 size_t buffer_payload_max
;
56 struct gb_connection
*connection
;
62 spinlock_t write_lock
;
63 struct async_icount iocount
;
64 struct async_icount oldcount
;
65 wait_queue_head_t wioctl
;
67 u8 ctrlin
; /* input control lines */
68 u8 ctrlout
; /* output control lines */
69 struct gb_tty_line_coding line_coding
;
70 struct work_struct tx_work
;
71 struct kfifo write_fifo
;
76 static struct tty_driver
*gb_tty_driver
;
77 static DEFINE_IDR(tty_minors
);
78 static DEFINE_MUTEX(table_lock
);
80 static int gb_uart_receive_data_handler(struct gb_operation
*op
)
82 struct gb_connection
*connection
= op
->connection
;
83 struct gb_tty
*gb_tty
= gb_connection_get_data(connection
);
84 struct tty_port
*port
= &gb_tty
->port
;
85 struct gb_message
*request
= op
->request
;
86 struct gb_uart_recv_data_request
*receive_data
;
89 unsigned long tty_flags
= TTY_NORMAL
;
91 if (request
->payload_size
< sizeof(*receive_data
)) {
92 dev_err(&gb_tty
->gbphy_dev
->dev
,
93 "short receive-data request received (%zu < %zu)\n",
94 request
->payload_size
, sizeof(*receive_data
));
98 receive_data
= op
->request
->payload
;
99 recv_data_size
= le16_to_cpu(receive_data
->size
);
101 if (recv_data_size
!= request
->payload_size
- sizeof(*receive_data
)) {
102 dev_err(&gb_tty
->gbphy_dev
->dev
,
103 "malformed receive-data request received (%u != %zu)\n",
105 request
->payload_size
- sizeof(*receive_data
));
112 if (receive_data
->flags
) {
113 if (receive_data
->flags
& GB_UART_RECV_FLAG_BREAK
)
114 tty_flags
= TTY_BREAK
;
115 else if (receive_data
->flags
& GB_UART_RECV_FLAG_PARITY
)
116 tty_flags
= TTY_PARITY
;
117 else if (receive_data
->flags
& GB_UART_RECV_FLAG_FRAMING
)
118 tty_flags
= TTY_FRAME
;
120 /* overrun is special, not associated with a char */
121 if (receive_data
->flags
& GB_UART_RECV_FLAG_OVERRUN
)
122 tty_insert_flip_char(port
, 0, TTY_OVERRUN
);
124 count
= tty_insert_flip_string_fixed_flag(port
, receive_data
->data
,
125 tty_flags
, recv_data_size
);
126 if (count
!= recv_data_size
) {
127 dev_err(&gb_tty
->gbphy_dev
->dev
,
128 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
129 recv_data_size
, count
);
132 tty_flip_buffer_push(port
);
136 static int gb_uart_serial_state_handler(struct gb_operation
*op
)
138 struct gb_connection
*connection
= op
->connection
;
139 struct gb_tty
*gb_tty
= gb_connection_get_data(connection
);
140 struct gb_message
*request
= op
->request
;
141 struct gb_uart_serial_state_request
*serial_state
;
143 if (request
->payload_size
< sizeof(*serial_state
)) {
144 dev_err(&gb_tty
->gbphy_dev
->dev
,
145 "short serial-state event received (%zu < %zu)\n",
146 request
->payload_size
, sizeof(*serial_state
));
150 serial_state
= request
->payload
;
151 gb_tty
->ctrlin
= serial_state
->control
;
156 static int gb_uart_receive_credits_handler(struct gb_operation
*op
)
158 struct gb_connection
*connection
= op
->connection
;
159 struct gb_tty
*gb_tty
= gb_connection_get_data(connection
);
160 struct gb_message
*request
= op
->request
;
161 struct gb_uart_receive_credits_request
*credit_request
;
163 unsigned int incoming_credits
;
166 if (request
->payload_size
< sizeof(*credit_request
)) {
167 dev_err(&gb_tty
->gbphy_dev
->dev
,
168 "short receive_credits event received (%zu < %zu)\n",
169 request
->payload_size
,
170 sizeof(*credit_request
));
174 credit_request
= request
->payload
;
175 incoming_credits
= le16_to_cpu(credit_request
->count
);
177 spin_lock_irqsave(&gb_tty
->write_lock
, flags
);
178 gb_tty
->credits
+= incoming_credits
;
179 if (gb_tty
->credits
> GB_UART_FIRMWARE_CREDITS
) {
180 gb_tty
->credits
-= incoming_credits
;
183 spin_unlock_irqrestore(&gb_tty
->write_lock
, flags
);
186 dev_err(&gb_tty
->gbphy_dev
->dev
,
187 "invalid number of incoming credits: %d\n",
192 if (!gb_tty
->close_pending
)
193 schedule_work(&gb_tty
->tx_work
);
196 * the port the tty layer may be waiting for credits
198 tty_port_tty_wakeup(&gb_tty
->port
);
203 static int gb_uart_request_handler(struct gb_operation
*op
)
205 struct gb_connection
*connection
= op
->connection
;
206 struct gb_tty
*gb_tty
= gb_connection_get_data(connection
);
211 case GB_UART_TYPE_RECEIVE_DATA
:
212 ret
= gb_uart_receive_data_handler(op
);
214 case GB_UART_TYPE_SERIAL_STATE
:
215 ret
= gb_uart_serial_state_handler(op
);
217 case GB_UART_TYPE_RECEIVE_CREDITS
:
218 ret
= gb_uart_receive_credits_handler(op
);
221 dev_err(&gb_tty
->gbphy_dev
->dev
,
222 "unsupported unsolicited request: 0x%02x\n", type
);
229 static void gb_uart_tx_write_work(struct work_struct
*work
)
231 struct gb_uart_send_data_request
*request
;
232 struct gb_tty
*gb_tty
;
234 unsigned int send_size
;
237 gb_tty
= container_of(work
, struct gb_tty
, tx_work
);
238 request
= gb_tty
->buffer
;
241 if (gb_tty
->close_pending
)
244 spin_lock_irqsave(&gb_tty
->write_lock
, flags
);
245 send_size
= gb_tty
->buffer_payload_max
;
246 if (send_size
> gb_tty
->credits
)
247 send_size
= gb_tty
->credits
;
249 send_size
= kfifo_out_peek(&gb_tty
->write_fifo
,
253 spin_unlock_irqrestore(&gb_tty
->write_lock
, flags
);
257 gb_tty
->credits
-= send_size
;
258 spin_unlock_irqrestore(&gb_tty
->write_lock
, flags
);
260 request
->size
= cpu_to_le16(send_size
);
261 ret
= gb_operation_sync(gb_tty
->connection
,
262 GB_UART_TYPE_SEND_DATA
,
263 request
, sizeof(*request
) + send_size
,
266 dev_err(&gb_tty
->gbphy_dev
->dev
,
267 "send data error: %d\n", ret
);
268 spin_lock_irqsave(&gb_tty
->write_lock
, flags
);
269 gb_tty
->credits
+= send_size
;
270 spin_unlock_irqrestore(&gb_tty
->write_lock
, flags
);
271 if (!gb_tty
->close_pending
)
276 spin_lock_irqsave(&gb_tty
->write_lock
, flags
);
277 ret
= kfifo_out(&gb_tty
->write_fifo
, &request
->data
[0],
279 spin_unlock_irqrestore(&gb_tty
->write_lock
, flags
);
281 tty_port_tty_wakeup(&gb_tty
->port
);
285 static int send_line_coding(struct gb_tty
*tty
)
287 struct gb_uart_set_line_coding_request request
;
289 memcpy(&request
, &tty
->line_coding
,
290 sizeof(tty
->line_coding
));
291 return gb_operation_sync(tty
->connection
, GB_UART_TYPE_SET_LINE_CODING
,
292 &request
, sizeof(request
), NULL
, 0);
295 static int send_control(struct gb_tty
*gb_tty
, u8 control
)
297 struct gb_uart_set_control_line_state_request request
;
299 request
.control
= control
;
300 return gb_operation_sync(gb_tty
->connection
,
301 GB_UART_TYPE_SET_CONTROL_LINE_STATE
,
302 &request
, sizeof(request
), NULL
, 0);
305 static int send_break(struct gb_tty
*gb_tty
, u8 state
)
307 struct gb_uart_set_break_request request
;
309 if ((state
!= 0) && (state
!= 1)) {
310 dev_err(&gb_tty
->gbphy_dev
->dev
,
311 "invalid break state of %d\n", state
);
315 request
.state
= state
;
316 return gb_operation_sync(gb_tty
->connection
, GB_UART_TYPE_SEND_BREAK
,
317 &request
, sizeof(request
), NULL
, 0);
321 static struct gb_tty
*get_gb_by_minor(unsigned minor
)
323 struct gb_tty
*gb_tty
;
325 mutex_lock(&table_lock
);
326 gb_tty
= idr_find(&tty_minors
, minor
);
328 mutex_lock(&gb_tty
->mutex
);
329 if (gb_tty
->disconnected
) {
330 mutex_unlock(&gb_tty
->mutex
);
333 tty_port_get(&gb_tty
->port
);
334 mutex_unlock(&gb_tty
->mutex
);
337 mutex_unlock(&table_lock
);
341 static int alloc_minor(struct gb_tty
*gb_tty
)
345 mutex_lock(&table_lock
);
346 minor
= idr_alloc(&tty_minors
, gb_tty
, 0, GB_NUM_MINORS
, GFP_KERNEL
);
347 mutex_unlock(&table_lock
);
349 gb_tty
->minor
= minor
;
353 static void release_minor(struct gb_tty
*gb_tty
)
355 int minor
= gb_tty
->minor
;
357 gb_tty
->minor
= 0; /* Maybe should use an invalid value instead */
358 mutex_lock(&table_lock
);
359 idr_remove(&tty_minors
, minor
);
360 mutex_unlock(&table_lock
);
363 static int gb_tty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
365 struct gb_tty
*gb_tty
;
368 gb_tty
= get_gb_by_minor(tty
->index
);
372 retval
= tty_standard_install(driver
, tty
);
376 tty
->driver_data
= gb_tty
;
379 tty_port_put(&gb_tty
->port
);
383 static int gb_tty_open(struct tty_struct
*tty
, struct file
*file
)
385 struct gb_tty
*gb_tty
= tty
->driver_data
;
387 return tty_port_open(&gb_tty
->port
, tty
, file
);
390 static void gb_tty_close(struct tty_struct
*tty
, struct file
*file
)
392 struct gb_tty
*gb_tty
= tty
->driver_data
;
394 tty_port_close(&gb_tty
->port
, tty
, file
);
397 static void gb_tty_cleanup(struct tty_struct
*tty
)
399 struct gb_tty
*gb_tty
= tty
->driver_data
;
401 tty_port_put(&gb_tty
->port
);
404 static void gb_tty_hangup(struct tty_struct
*tty
)
406 struct gb_tty
*gb_tty
= tty
->driver_data
;
408 tty_port_hangup(&gb_tty
->port
);
411 static int gb_tty_write(struct tty_struct
*tty
, const unsigned char *buf
,
414 struct gb_tty
*gb_tty
= tty
->driver_data
;
416 count
= kfifo_in_spinlocked(&gb_tty
->write_fifo
, buf
, count
,
417 &gb_tty
->write_lock
);
418 if (count
&& !gb_tty
->close_pending
)
419 schedule_work(&gb_tty
->tx_work
);
424 static int gb_tty_write_room(struct tty_struct
*tty
)
426 struct gb_tty
*gb_tty
= tty
->driver_data
;
430 spin_lock_irqsave(&gb_tty
->write_lock
, flags
);
431 room
= kfifo_avail(&gb_tty
->write_fifo
);
432 spin_unlock_irqrestore(&gb_tty
->write_lock
, flags
);
434 room
-= GB_UART_WRITE_ROOM_MARGIN
;
441 static int gb_tty_chars_in_buffer(struct tty_struct
*tty
)
443 struct gb_tty
*gb_tty
= tty
->driver_data
;
447 spin_lock_irqsave(&gb_tty
->write_lock
, flags
);
448 chars
= kfifo_len(&gb_tty
->write_fifo
);
449 if (gb_tty
->credits
< GB_UART_FIRMWARE_CREDITS
)
450 chars
+= GB_UART_FIRMWARE_CREDITS
- gb_tty
->credits
;
451 spin_unlock_irqrestore(&gb_tty
->write_lock
, flags
);
456 static int gb_tty_break_ctl(struct tty_struct
*tty
, int state
)
458 struct gb_tty
*gb_tty
= tty
->driver_data
;
460 return send_break(gb_tty
, state
? 1 : 0);
463 static void gb_tty_set_termios(struct tty_struct
*tty
,
464 struct ktermios
*termios_old
)
466 struct gb_tty
*gb_tty
= tty
->driver_data
;
467 struct ktermios
*termios
= &tty
->termios
;
468 struct gb_tty_line_coding newline
;
469 u8 newctrl
= gb_tty
->ctrlout
;
471 newline
.rate
= cpu_to_le32(tty_get_baud_rate(tty
));
472 newline
.format
= termios
->c_cflag
& CSTOPB
?
473 GB_SERIAL_2_STOP_BITS
: GB_SERIAL_1_STOP_BITS
;
474 newline
.parity
= termios
->c_cflag
& PARENB
?
475 (termios
->c_cflag
& PARODD
? 1 : 2) +
476 (termios
->c_cflag
& CMSPAR
? 2 : 0) : 0;
478 switch (termios
->c_cflag
& CSIZE
) {
480 newline
.data_bits
= 5;
483 newline
.data_bits
= 6;
486 newline
.data_bits
= 7;
490 newline
.data_bits
= 8;
494 /* FIXME: needs to clear unsupported bits in the termios */
495 gb_tty
->clocal
= ((termios
->c_cflag
& CLOCAL
) != 0);
497 if (C_BAUD(tty
) == B0
) {
498 newline
.rate
= gb_tty
->line_coding
.rate
;
499 newctrl
&= ~(GB_UART_CTRL_DTR
| GB_UART_CTRL_RTS
);
500 } else if (termios_old
&& (termios_old
->c_cflag
& CBAUD
) == B0
) {
501 newctrl
|= (GB_UART_CTRL_DTR
| GB_UART_CTRL_RTS
);
504 if (newctrl
!= gb_tty
->ctrlout
) {
505 gb_tty
->ctrlout
= newctrl
;
506 send_control(gb_tty
, newctrl
);
509 if (C_CRTSCTS(tty
) && C_BAUD(tty
) != B0
)
510 newline
.flow_control
|= GB_SERIAL_AUTO_RTSCTS_EN
;
512 newline
.flow_control
&= ~GB_SERIAL_AUTO_RTSCTS_EN
;
514 if (memcmp(&gb_tty
->line_coding
, &newline
, sizeof(newline
))) {
515 memcpy(&gb_tty
->line_coding
, &newline
, sizeof(newline
));
516 send_line_coding(gb_tty
);
520 static int gb_tty_tiocmget(struct tty_struct
*tty
)
522 struct gb_tty
*gb_tty
= tty
->driver_data
;
524 return (gb_tty
->ctrlout
& GB_UART_CTRL_DTR
? TIOCM_DTR
: 0) |
525 (gb_tty
->ctrlout
& GB_UART_CTRL_RTS
? TIOCM_RTS
: 0) |
526 (gb_tty
->ctrlin
& GB_UART_CTRL_DSR
? TIOCM_DSR
: 0) |
527 (gb_tty
->ctrlin
& GB_UART_CTRL_RI
? TIOCM_RI
: 0) |
528 (gb_tty
->ctrlin
& GB_UART_CTRL_DCD
? TIOCM_CD
: 0) |
532 static int gb_tty_tiocmset(struct tty_struct
*tty
, unsigned int set
,
535 struct gb_tty
*gb_tty
= tty
->driver_data
;
536 u8 newctrl
= gb_tty
->ctrlout
;
538 set
= (set
& TIOCM_DTR
? GB_UART_CTRL_DTR
: 0) |
539 (set
& TIOCM_RTS
? GB_UART_CTRL_RTS
: 0);
540 clear
= (clear
& TIOCM_DTR
? GB_UART_CTRL_DTR
: 0) |
541 (clear
& TIOCM_RTS
? GB_UART_CTRL_RTS
: 0);
543 newctrl
= (newctrl
& ~clear
) | set
;
544 if (gb_tty
->ctrlout
== newctrl
)
547 gb_tty
->ctrlout
= newctrl
;
548 return send_control(gb_tty
, newctrl
);
551 static void gb_tty_throttle(struct tty_struct
*tty
)
553 struct gb_tty
*gb_tty
= tty
->driver_data
;
554 unsigned char stop_char
;
558 stop_char
= STOP_CHAR(tty
);
559 retval
= gb_tty_write(tty
, &stop_char
, 1);
564 if (tty
->termios
.c_cflag
& CRTSCTS
) {
565 gb_tty
->ctrlout
&= ~GB_UART_CTRL_RTS
;
566 retval
= send_control(gb_tty
, gb_tty
->ctrlout
);
571 static void gb_tty_unthrottle(struct tty_struct
*tty
)
573 struct gb_tty
*gb_tty
= tty
->driver_data
;
574 unsigned char start_char
;
578 start_char
= START_CHAR(tty
);
579 retval
= gb_tty_write(tty
, &start_char
, 1);
584 if (tty
->termios
.c_cflag
& CRTSCTS
) {
585 gb_tty
->ctrlout
|= GB_UART_CTRL_RTS
;
586 retval
= send_control(gb_tty
, gb_tty
->ctrlout
);
590 static int get_serial_info(struct gb_tty
*gb_tty
,
591 struct serial_struct __user
*info
)
593 struct serial_struct tmp
;
598 memset(&tmp
, 0, sizeof(tmp
));
599 tmp
.flags
= ASYNC_LOW_LATENCY
| ASYNC_SKIP_TEST
;
600 tmp
.type
= PORT_16550A
;
601 tmp
.line
= gb_tty
->minor
;
602 tmp
.xmit_fifo_size
= 16;
603 tmp
.baud_base
= 9600;
604 tmp
.close_delay
= gb_tty
->port
.close_delay
/ 10;
605 tmp
.closing_wait
= gb_tty
->port
.closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
606 ASYNC_CLOSING_WAIT_NONE
: gb_tty
->port
.closing_wait
/ 10;
608 if (copy_to_user(info
, &tmp
, sizeof(tmp
)))
613 static int set_serial_info(struct gb_tty
*gb_tty
,
614 struct serial_struct __user
*newinfo
)
616 struct serial_struct new_serial
;
617 unsigned int closing_wait
;
618 unsigned int close_delay
;
621 if (copy_from_user(&new_serial
, newinfo
, sizeof(new_serial
)))
624 close_delay
= new_serial
.close_delay
* 10;
625 closing_wait
= new_serial
.closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
626 ASYNC_CLOSING_WAIT_NONE
: new_serial
.closing_wait
* 10;
628 mutex_lock(&gb_tty
->port
.mutex
);
629 if (!capable(CAP_SYS_ADMIN
)) {
630 if ((close_delay
!= gb_tty
->port
.close_delay
) ||
631 (closing_wait
!= gb_tty
->port
.closing_wait
))
634 retval
= -EOPNOTSUPP
;
636 gb_tty
->port
.close_delay
= close_delay
;
637 gb_tty
->port
.closing_wait
= closing_wait
;
639 mutex_unlock(&gb_tty
->port
.mutex
);
643 static int wait_serial_change(struct gb_tty
*gb_tty
, unsigned long arg
)
646 DECLARE_WAITQUEUE(wait
, current
);
647 struct async_icount old
;
648 struct async_icount
new;
650 if (!(arg
& (TIOCM_DSR
| TIOCM_RI
| TIOCM_CD
)))
654 spin_lock_irq(&gb_tty
->read_lock
);
655 old
= gb_tty
->oldcount
;
656 new = gb_tty
->iocount
;
657 gb_tty
->oldcount
= new;
658 spin_unlock_irq(&gb_tty
->read_lock
);
660 if ((arg
& TIOCM_DSR
) && (old
.dsr
!= new.dsr
))
662 if ((arg
& TIOCM_CD
) && (old
.dcd
!= new.dcd
))
664 if ((arg
& TIOCM_RI
) && (old
.rng
!= new.rng
))
667 add_wait_queue(&gb_tty
->wioctl
, &wait
);
668 set_current_state(TASK_INTERRUPTIBLE
);
670 remove_wait_queue(&gb_tty
->wioctl
, &wait
);
671 if (gb_tty
->disconnected
) {
675 } else if (signal_pending(current
)) {
676 retval
= -ERESTARTSYS
;
684 static int get_serial_usage(struct gb_tty
*gb_tty
,
685 struct serial_icounter_struct __user
*count
)
687 struct serial_icounter_struct icount
;
690 memset(&icount
, 0, sizeof(icount
));
691 icount
.dsr
= gb_tty
->iocount
.dsr
;
692 icount
.rng
= gb_tty
->iocount
.rng
;
693 icount
.dcd
= gb_tty
->iocount
.dcd
;
694 icount
.frame
= gb_tty
->iocount
.frame
;
695 icount
.overrun
= gb_tty
->iocount
.overrun
;
696 icount
.parity
= gb_tty
->iocount
.parity
;
697 icount
.brk
= gb_tty
->iocount
.brk
;
699 if (copy_to_user(count
, &icount
, sizeof(icount
)) > 0)
705 static int gb_tty_ioctl(struct tty_struct
*tty
, unsigned int cmd
,
708 struct gb_tty
*gb_tty
= tty
->driver_data
;
712 return get_serial_info(gb_tty
,
713 (struct serial_struct __user
*)arg
);
715 return set_serial_info(gb_tty
,
716 (struct serial_struct __user
*)arg
);
718 return wait_serial_change(gb_tty
, arg
);
720 return get_serial_usage(gb_tty
,
721 (struct serial_icounter_struct __user
*)arg
);
727 static void gb_tty_dtr_rts(struct tty_port
*port
, int on
)
729 struct gb_tty
*gb_tty
;
732 gb_tty
= container_of(port
, struct gb_tty
, port
);
733 newctrl
= gb_tty
->ctrlout
;
736 newctrl
|= (GB_UART_CTRL_DTR
| GB_UART_CTRL_RTS
);
738 newctrl
&= ~(GB_UART_CTRL_DTR
| GB_UART_CTRL_RTS
);
740 gb_tty
->ctrlout
= newctrl
;
741 send_control(gb_tty
, newctrl
);
744 static void gb_tty_port_shutdown(struct tty_port
*port
)
746 struct gb_tty
*gb_tty
;
749 gb_tty
= container_of(port
, struct gb_tty
, port
);
751 gb_tty
->close_pending
= true;
753 cancel_work_sync(&gb_tty
->tx_work
);
755 spin_lock_irqsave(&gb_tty
->write_lock
, flags
);
756 kfifo_reset_out(&gb_tty
->write_fifo
);
757 spin_unlock_irqrestore(&gb_tty
->write_lock
, flags
);
759 gb_tty
->close_pending
= false;
762 static const struct tty_operations gb_ops
= {
763 .install
= gb_tty_install
,
765 .close
= gb_tty_close
,
766 .cleanup
= gb_tty_cleanup
,
767 .hangup
= gb_tty_hangup
,
768 .write
= gb_tty_write
,
769 .write_room
= gb_tty_write_room
,
770 .ioctl
= gb_tty_ioctl
,
771 .throttle
= gb_tty_throttle
,
772 .unthrottle
= gb_tty_unthrottle
,
773 .chars_in_buffer
= gb_tty_chars_in_buffer
,
774 .break_ctl
= gb_tty_break_ctl
,
775 .set_termios
= gb_tty_set_termios
,
776 .tiocmget
= gb_tty_tiocmget
,
777 .tiocmset
= gb_tty_tiocmset
,
780 static struct tty_port_operations gb_port_ops
= {
781 .dtr_rts
= gb_tty_dtr_rts
,
782 .shutdown
= gb_tty_port_shutdown
,
785 static int gb_uart_probe(struct gbphy_device
*gbphy_dev
,
786 const struct gbphy_device_id
*id
)
788 struct gb_connection
*connection
;
790 struct gb_tty
*gb_tty
;
791 struct device
*tty_dev
;
795 gb_tty
= kzalloc(sizeof(*gb_tty
), GFP_KERNEL
);
799 connection
= gb_connection_create(gbphy_dev
->bundle
,
800 le16_to_cpu(gbphy_dev
->cport_desc
->id
),
801 gb_uart_request_handler
);
802 if (IS_ERR(connection
)) {
803 retval
= PTR_ERR(connection
);
807 max_payload
= gb_operation_get_payload_size_max(connection
);
808 if (max_payload
< sizeof(struct gb_uart_send_data_request
)) {
810 goto exit_connection_destroy
;
813 gb_tty
->buffer_payload_max
= max_payload
-
814 sizeof(struct gb_uart_send_data_request
);
816 gb_tty
->buffer
= kzalloc(gb_tty
->buffer_payload_max
, GFP_KERNEL
);
817 if (!gb_tty
->buffer
) {
819 goto exit_connection_destroy
;
822 INIT_WORK(&gb_tty
->tx_work
, gb_uart_tx_write_work
);
824 retval
= kfifo_alloc(&gb_tty
->write_fifo
, GB_UART_WRITE_FIFO_SIZE
,
829 gb_tty
->credits
= GB_UART_FIRMWARE_CREDITS
;
831 minor
= alloc_minor(gb_tty
);
833 if (minor
== -ENOSPC
) {
834 dev_err(&connection
->bundle
->dev
,
835 "no more free minor numbers\n");
840 goto exit_kfifo_free
;
843 gb_tty
->minor
= minor
;
844 spin_lock_init(&gb_tty
->write_lock
);
845 spin_lock_init(&gb_tty
->read_lock
);
846 init_waitqueue_head(&gb_tty
->wioctl
);
847 mutex_init(&gb_tty
->mutex
);
849 tty_port_init(&gb_tty
->port
);
850 gb_tty
->port
.ops
= &gb_port_ops
;
852 gb_tty
->connection
= connection
;
853 gb_tty
->gbphy_dev
= gbphy_dev
;
854 gb_connection_set_data(connection
, gb_tty
);
855 gb_gbphy_set_data(gbphy_dev
, gb_tty
);
857 retval
= gb_connection_enable_tx(connection
);
859 goto exit_release_minor
;
861 retval
= gb_gbphy_get_version(connection
);
863 goto exit_connection_disable
;
865 send_control(gb_tty
, gb_tty
->ctrlout
);
867 /* initialize the uart to be 9600n81 */
868 gb_tty
->line_coding
.rate
= cpu_to_le32(9600);
869 gb_tty
->line_coding
.format
= GB_SERIAL_1_STOP_BITS
;
870 gb_tty
->line_coding
.parity
= GB_SERIAL_NO_PARITY
;
871 gb_tty
->line_coding
.data_bits
= 8;
872 send_line_coding(gb_tty
);
874 retval
= gb_connection_enable(connection
);
876 goto exit_connection_disable
;
878 tty_dev
= tty_port_register_device(&gb_tty
->port
, gb_tty_driver
, minor
,
880 if (IS_ERR(tty_dev
)) {
881 retval
= PTR_ERR(tty_dev
);
882 goto exit_connection_disable
;
888 exit_connection_disable
:
889 gb_connection_disable(connection
);
891 release_minor(gb_tty
);
893 kfifo_free(&gb_tty
->write_fifo
);
895 kfree(gb_tty
->buffer
);
896 exit_connection_destroy
:
897 gb_connection_destroy(connection
);
904 static void gb_uart_remove(struct gbphy_device
*gbphy_dev
)
906 struct gb_tty
*gb_tty
= gb_gbphy_get_data(gbphy_dev
);
907 struct gb_connection
*connection
= gb_tty
->connection
;
908 struct tty_struct
*tty
;
910 mutex_lock(&gb_tty
->mutex
);
911 gb_tty
->disconnected
= true;
913 wake_up_all(&gb_tty
->wioctl
);
914 mutex_unlock(&gb_tty
->mutex
);
916 tty
= tty_port_tty_get(&gb_tty
->port
);
922 gb_connection_disable_rx(connection
);
923 tty_unregister_device(gb_tty_driver
, gb_tty
->minor
);
925 /* FIXME - free transmit / receive buffers */
927 gb_connection_disable(connection
);
928 tty_port_destroy(&gb_tty
->port
);
929 gb_connection_destroy(connection
);
930 kfifo_free(&gb_tty
->write_fifo
);
931 kfree(gb_tty
->buffer
);
935 static int gb_tty_init(void)
939 gb_tty_driver
= tty_alloc_driver(GB_NUM_MINORS
, 0);
940 if (IS_ERR(gb_tty_driver
)) {
941 pr_err("Can not allocate tty driver\n");
943 goto fail_unregister_dev
;
946 gb_tty_driver
->driver_name
= "gb";
947 gb_tty_driver
->name
= GB_NAME
;
948 gb_tty_driver
->major
= 0;
949 gb_tty_driver
->minor_start
= 0;
950 gb_tty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
951 gb_tty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
952 gb_tty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
953 gb_tty_driver
->init_termios
= tty_std_termios
;
954 gb_tty_driver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
955 tty_set_operations(gb_tty_driver
, &gb_ops
);
957 retval
= tty_register_driver(gb_tty_driver
);
959 pr_err("Can not register tty driver: %d\n", retval
);
960 goto fail_put_gb_tty
;
966 put_tty_driver(gb_tty_driver
);
971 static void gb_tty_exit(void)
973 tty_unregister_driver(gb_tty_driver
);
974 put_tty_driver(gb_tty_driver
);
975 idr_destroy(&tty_minors
);
978 static const struct gbphy_device_id gb_uart_id_table
[] = {
979 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART
) },
982 MODULE_DEVICE_TABLE(gbphy
, gb_uart_id_table
);
984 static struct gbphy_driver uart_driver
= {
986 .probe
= gb_uart_probe
,
987 .remove
= gb_uart_remove
,
988 .id_table
= gb_uart_id_table
,
991 static int gb_uart_driver_init(void)
999 ret
= gb_gbphy_register(&uart_driver
);
1007 module_init(gb_uart_driver_init
);
1009 static void gb_uart_driver_exit(void)
1011 gb_gbphy_deregister(&uart_driver
);
1015 module_exit(gb_uart_driver_exit
);
1016 MODULE_LICENSE("GPL v2");