GNU Linux-libre 4.19.207-gnu1
[releases.git] / drivers / staging / greybus / uart.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * UART driver for the Greybus "generic" UART module.
4  *
5  * Copyright 2014 Google Inc.
6  * Copyright 2014 Linaro Ltd.
7  *
8  * Heavily based on drivers/usb/class/cdc-acm.c and
9  * drivers/usb/serial/usb-serial.c.
10  */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
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>
26 #include <linux/fs.h>
27 #include <linux/kdev_t.h>
28 #include <linux/kfifo.h>
29 #include <linux/workqueue.h>
30 #include <linux/completion.h>
31
32 #include "greybus.h"
33 #include "gbphy.h"
34
35 #define GB_NUM_MINORS   16      /* 16 is more than enough */
36 #define GB_NAME         "ttyGB"
37
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
42
43 struct gb_tty_line_coding {
44         __le32  rate;
45         __u8    format;
46         __u8    parity;
47         __u8    data_bits;
48         __u8    flow_control;
49 };
50
51 struct gb_tty {
52         struct gbphy_device *gbphy_dev;
53         struct tty_port port;
54         void *buffer;
55         size_t buffer_payload_max;
56         struct gb_connection *connection;
57         u16 cport_id;
58         unsigned int minor;
59         unsigned char clocal;
60         bool disconnected;
61         spinlock_t read_lock;
62         spinlock_t write_lock;
63         struct async_icount iocount;
64         struct async_icount oldcount;
65         wait_queue_head_t wioctl;
66         struct mutex mutex;
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;
72         bool close_pending;
73         unsigned int credits;
74         struct completion credits_complete;
75 };
76
77 static struct tty_driver *gb_tty_driver;
78 static DEFINE_IDR(tty_minors);
79 static DEFINE_MUTEX(table_lock);
80
81 static int gb_uart_receive_data_handler(struct gb_operation *op)
82 {
83         struct gb_connection *connection = op->connection;
84         struct gb_tty *gb_tty = gb_connection_get_data(connection);
85         struct tty_port *port = &gb_tty->port;
86         struct gb_message *request = op->request;
87         struct gb_uart_recv_data_request *receive_data;
88         u16 recv_data_size;
89         int count;
90         unsigned long tty_flags = TTY_NORMAL;
91
92         if (request->payload_size < sizeof(*receive_data)) {
93                 dev_err(&gb_tty->gbphy_dev->dev,
94                         "short receive-data request received (%zu < %zu)\n",
95                         request->payload_size, sizeof(*receive_data));
96                 return -EINVAL;
97         }
98
99         receive_data = op->request->payload;
100         recv_data_size = le16_to_cpu(receive_data->size);
101
102         if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
103                 dev_err(&gb_tty->gbphy_dev->dev,
104                         "malformed receive-data request received (%u != %zu)\n",
105                         recv_data_size,
106                         request->payload_size - sizeof(*receive_data));
107                 return -EINVAL;
108         }
109
110         if (!recv_data_size)
111                 return -EINVAL;
112
113         if (receive_data->flags) {
114                 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
115                         tty_flags = TTY_BREAK;
116                 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
117                         tty_flags = TTY_PARITY;
118                 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
119                         tty_flags = TTY_FRAME;
120
121                 /* overrun is special, not associated with a char */
122                 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
123                         tty_insert_flip_char(port, 0, TTY_OVERRUN);
124         }
125         count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
126                                                   tty_flags, recv_data_size);
127         if (count != recv_data_size) {
128                 dev_err(&gb_tty->gbphy_dev->dev,
129                         "UART: RX 0x%08x bytes only wrote 0x%08x\n",
130                         recv_data_size, count);
131         }
132         if (count)
133                 tty_flip_buffer_push(port);
134         return 0;
135 }
136
137 static int gb_uart_serial_state_handler(struct gb_operation *op)
138 {
139         struct gb_connection *connection = op->connection;
140         struct gb_tty *gb_tty = gb_connection_get_data(connection);
141         struct gb_message *request = op->request;
142         struct gb_uart_serial_state_request *serial_state;
143
144         if (request->payload_size < sizeof(*serial_state)) {
145                 dev_err(&gb_tty->gbphy_dev->dev,
146                         "short serial-state event received (%zu < %zu)\n",
147                         request->payload_size, sizeof(*serial_state));
148                 return -EINVAL;
149         }
150
151         serial_state = request->payload;
152         gb_tty->ctrlin = serial_state->control;
153
154         return 0;
155 }
156
157 static int gb_uart_receive_credits_handler(struct gb_operation *op)
158 {
159         struct gb_connection *connection = op->connection;
160         struct gb_tty *gb_tty = gb_connection_get_data(connection);
161         struct gb_message *request = op->request;
162         struct gb_uart_receive_credits_request *credit_request;
163         unsigned long flags;
164         unsigned int incoming_credits;
165         int ret = 0;
166
167         if (request->payload_size < sizeof(*credit_request)) {
168                 dev_err(&gb_tty->gbphy_dev->dev,
169                         "short receive_credits event received (%zu < %zu)\n",
170                         request->payload_size,
171                         sizeof(*credit_request));
172                 return -EINVAL;
173         }
174
175         credit_request = request->payload;
176         incoming_credits = le16_to_cpu(credit_request->count);
177
178         spin_lock_irqsave(&gb_tty->write_lock, flags);
179         gb_tty->credits += incoming_credits;
180         if (gb_tty->credits > GB_UART_FIRMWARE_CREDITS) {
181                 gb_tty->credits -= incoming_credits;
182                 ret = -EINVAL;
183         }
184         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
185
186         if (ret) {
187                 dev_err(&gb_tty->gbphy_dev->dev,
188                         "invalid number of incoming credits: %d\n",
189                         incoming_credits);
190                 return ret;
191         }
192
193         if (!gb_tty->close_pending)
194                 schedule_work(&gb_tty->tx_work);
195
196         /*
197          * the port the tty layer may be waiting for credits
198          */
199         tty_port_tty_wakeup(&gb_tty->port);
200
201         if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
202                 complete(&gb_tty->credits_complete);
203
204         return ret;
205 }
206
207 static int gb_uart_request_handler(struct gb_operation *op)
208 {
209         struct gb_connection *connection = op->connection;
210         struct gb_tty *gb_tty = gb_connection_get_data(connection);
211         int type = op->type;
212         int ret;
213
214         switch (type) {
215         case GB_UART_TYPE_RECEIVE_DATA:
216                 ret = gb_uart_receive_data_handler(op);
217                 break;
218         case GB_UART_TYPE_SERIAL_STATE:
219                 ret = gb_uart_serial_state_handler(op);
220                 break;
221         case GB_UART_TYPE_RECEIVE_CREDITS:
222                 ret = gb_uart_receive_credits_handler(op);
223                 break;
224         default:
225                 dev_err(&gb_tty->gbphy_dev->dev,
226                         "unsupported unsolicited request: 0x%02x\n", type);
227                 ret = -EINVAL;
228         }
229
230         return ret;
231 }
232
233 static void  gb_uart_tx_write_work(struct work_struct *work)
234 {
235         struct gb_uart_send_data_request *request;
236         struct gb_tty *gb_tty;
237         unsigned long flags;
238         unsigned int send_size;
239         int ret;
240
241         gb_tty = container_of(work, struct gb_tty, tx_work);
242         request = gb_tty->buffer;
243
244         while (1) {
245                 if (gb_tty->close_pending)
246                         break;
247
248                 spin_lock_irqsave(&gb_tty->write_lock, flags);
249                 send_size = gb_tty->buffer_payload_max;
250                 if (send_size > gb_tty->credits)
251                         send_size = gb_tty->credits;
252
253                 send_size = kfifo_out_peek(&gb_tty->write_fifo,
254                                            &request->data[0],
255                                            send_size);
256                 if (!send_size) {
257                         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
258                         break;
259                 }
260
261                 gb_tty->credits -= send_size;
262                 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
263
264                 request->size = cpu_to_le16(send_size);
265                 ret = gb_operation_sync(gb_tty->connection,
266                                         GB_UART_TYPE_SEND_DATA,
267                                         request, sizeof(*request) + send_size,
268                                         NULL, 0);
269                 if (ret) {
270                         dev_err(&gb_tty->gbphy_dev->dev,
271                                 "send data error: %d\n", ret);
272                         spin_lock_irqsave(&gb_tty->write_lock, flags);
273                         gb_tty->credits += send_size;
274                         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
275                         if (!gb_tty->close_pending)
276                                 schedule_work(work);
277                         return;
278                 }
279
280                 spin_lock_irqsave(&gb_tty->write_lock, flags);
281                 ret = kfifo_out(&gb_tty->write_fifo, &request->data[0],
282                                 send_size);
283                 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
284
285                 tty_port_tty_wakeup(&gb_tty->port);
286         }
287 }
288
289 static int send_line_coding(struct gb_tty *tty)
290 {
291         struct gb_uart_set_line_coding_request request;
292
293         memcpy(&request, &tty->line_coding,
294                sizeof(tty->line_coding));
295         return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
296                                  &request, sizeof(request), NULL, 0);
297 }
298
299 static int send_control(struct gb_tty *gb_tty, u8 control)
300 {
301         struct gb_uart_set_control_line_state_request request;
302
303         request.control = control;
304         return gb_operation_sync(gb_tty->connection,
305                                  GB_UART_TYPE_SET_CONTROL_LINE_STATE,
306                                  &request, sizeof(request), NULL, 0);
307 }
308
309 static int send_break(struct gb_tty *gb_tty, u8 state)
310 {
311         struct gb_uart_set_break_request request;
312
313         if ((state != 0) && (state != 1)) {
314                 dev_err(&gb_tty->gbphy_dev->dev,
315                         "invalid break state of %d\n", state);
316                 return -EINVAL;
317         }
318
319         request.state = state;
320         return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
321                                  &request, sizeof(request), NULL, 0);
322 }
323
324 static int gb_uart_wait_for_all_credits(struct gb_tty *gb_tty)
325 {
326         int ret;
327
328         if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
329                 return 0;
330
331         ret = wait_for_completion_timeout(&gb_tty->credits_complete,
332                         msecs_to_jiffies(GB_UART_CREDIT_WAIT_TIMEOUT_MSEC));
333         if (!ret) {
334                 dev_err(&gb_tty->gbphy_dev->dev,
335                         "time out waiting for credits\n");
336                 return -ETIMEDOUT;
337         }
338
339         return 0;
340 }
341
342 static int gb_uart_flush(struct gb_tty *gb_tty, u8 flags)
343 {
344         struct gb_uart_serial_flush_request request;
345
346         request.flags = flags;
347         return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_FLUSH_FIFOS,
348                                  &request, sizeof(request), NULL, 0);
349 }
350
351 static struct gb_tty *get_gb_by_minor(unsigned int minor)
352 {
353         struct gb_tty *gb_tty;
354
355         mutex_lock(&table_lock);
356         gb_tty = idr_find(&tty_minors, minor);
357         if (gb_tty) {
358                 mutex_lock(&gb_tty->mutex);
359                 if (gb_tty->disconnected) {
360                         mutex_unlock(&gb_tty->mutex);
361                         gb_tty = NULL;
362                 } else {
363                         tty_port_get(&gb_tty->port);
364                         mutex_unlock(&gb_tty->mutex);
365                 }
366         }
367         mutex_unlock(&table_lock);
368         return gb_tty;
369 }
370
371 static int alloc_minor(struct gb_tty *gb_tty)
372 {
373         int minor;
374
375         mutex_lock(&table_lock);
376         minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
377         mutex_unlock(&table_lock);
378         if (minor >= 0)
379                 gb_tty->minor = minor;
380         return minor;
381 }
382
383 static void release_minor(struct gb_tty *gb_tty)
384 {
385         int minor = gb_tty->minor;
386
387         gb_tty->minor = 0;      /* Maybe should use an invalid value instead */
388         mutex_lock(&table_lock);
389         idr_remove(&tty_minors, minor);
390         mutex_unlock(&table_lock);
391 }
392
393 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
394 {
395         struct gb_tty *gb_tty;
396         int retval;
397
398         gb_tty = get_gb_by_minor(tty->index);
399         if (!gb_tty)
400                 return -ENODEV;
401
402         retval = tty_standard_install(driver, tty);
403         if (retval)
404                 goto error;
405
406         tty->driver_data = gb_tty;
407         return 0;
408 error:
409         tty_port_put(&gb_tty->port);
410         return retval;
411 }
412
413 static int gb_tty_open(struct tty_struct *tty, struct file *file)
414 {
415         struct gb_tty *gb_tty = tty->driver_data;
416
417         return tty_port_open(&gb_tty->port, tty, file);
418 }
419
420 static void gb_tty_close(struct tty_struct *tty, struct file *file)
421 {
422         struct gb_tty *gb_tty = tty->driver_data;
423
424         tty_port_close(&gb_tty->port, tty, file);
425 }
426
427 static void gb_tty_cleanup(struct tty_struct *tty)
428 {
429         struct gb_tty *gb_tty = tty->driver_data;
430
431         tty_port_put(&gb_tty->port);
432 }
433
434 static void gb_tty_hangup(struct tty_struct *tty)
435 {
436         struct gb_tty *gb_tty = tty->driver_data;
437
438         tty_port_hangup(&gb_tty->port);
439 }
440
441 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
442                         int count)
443 {
444         struct gb_tty *gb_tty = tty->driver_data;
445
446         count =  kfifo_in_spinlocked(&gb_tty->write_fifo, buf, count,
447                                      &gb_tty->write_lock);
448         if (count && !gb_tty->close_pending)
449                 schedule_work(&gb_tty->tx_work);
450
451         return count;
452 }
453
454 static int gb_tty_write_room(struct tty_struct *tty)
455 {
456         struct gb_tty *gb_tty = tty->driver_data;
457         unsigned long flags;
458         int room;
459
460         spin_lock_irqsave(&gb_tty->write_lock, flags);
461         room = kfifo_avail(&gb_tty->write_fifo);
462         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
463
464         room -= GB_UART_WRITE_ROOM_MARGIN;
465         if (room < 0)
466                 return 0;
467
468         return room;
469 }
470
471 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
472 {
473         struct gb_tty *gb_tty = tty->driver_data;
474         unsigned long flags;
475         int chars;
476
477         spin_lock_irqsave(&gb_tty->write_lock, flags);
478         chars = kfifo_len(&gb_tty->write_fifo);
479         if (gb_tty->credits < GB_UART_FIRMWARE_CREDITS)
480                 chars += GB_UART_FIRMWARE_CREDITS - gb_tty->credits;
481         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
482
483         return chars;
484 }
485
486 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
487 {
488         struct gb_tty *gb_tty = tty->driver_data;
489
490         return send_break(gb_tty, state ? 1 : 0);
491 }
492
493 static void gb_tty_set_termios(struct tty_struct *tty,
494                                struct ktermios *termios_old)
495 {
496         struct gb_tty *gb_tty = tty->driver_data;
497         struct ktermios *termios = &tty->termios;
498         struct gb_tty_line_coding newline;
499         u8 newctrl = gb_tty->ctrlout;
500
501         newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
502         newline.format = termios->c_cflag & CSTOPB ?
503                                 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
504         newline.parity = termios->c_cflag & PARENB ?
505                                 (termios->c_cflag & PARODD ? 1 : 2) +
506                                 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
507
508         switch (termios->c_cflag & CSIZE) {
509         case CS5:
510                 newline.data_bits = 5;
511                 break;
512         case CS6:
513                 newline.data_bits = 6;
514                 break;
515         case CS7:
516                 newline.data_bits = 7;
517                 break;
518         case CS8:
519         default:
520                 newline.data_bits = 8;
521                 break;
522         }
523
524         /* FIXME: needs to clear unsupported bits in the termios */
525         gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
526
527         if (C_BAUD(tty) == B0) {
528                 newline.rate = gb_tty->line_coding.rate;
529                 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
530         } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
531                 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
532         }
533
534         if (newctrl != gb_tty->ctrlout) {
535                 gb_tty->ctrlout = newctrl;
536                 send_control(gb_tty, newctrl);
537         }
538
539         if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
540                 newline.flow_control = GB_SERIAL_AUTO_RTSCTS_EN;
541         else
542                 newline.flow_control = 0;
543
544         if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
545                 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
546                 send_line_coding(gb_tty);
547         }
548 }
549
550 static int gb_tty_tiocmget(struct tty_struct *tty)
551 {
552         struct gb_tty *gb_tty = tty->driver_data;
553
554         return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
555                (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
556                (gb_tty->ctrlin  & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
557                (gb_tty->ctrlin  & GB_UART_CTRL_RI  ? TIOCM_RI  : 0) |
558                (gb_tty->ctrlin  & GB_UART_CTRL_DCD ? TIOCM_CD  : 0) |
559                TIOCM_CTS;
560 }
561
562 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
563                            unsigned int clear)
564 {
565         struct gb_tty *gb_tty = tty->driver_data;
566         u8 newctrl = gb_tty->ctrlout;
567
568         set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
569               (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
570         clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
571                 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
572
573         newctrl = (newctrl & ~clear) | set;
574         if (gb_tty->ctrlout == newctrl)
575                 return 0;
576
577         gb_tty->ctrlout = newctrl;
578         return send_control(gb_tty, newctrl);
579 }
580
581 static void gb_tty_throttle(struct tty_struct *tty)
582 {
583         struct gb_tty *gb_tty = tty->driver_data;
584         unsigned char stop_char;
585         int retval;
586
587         if (I_IXOFF(tty)) {
588                 stop_char = STOP_CHAR(tty);
589                 retval = gb_tty_write(tty, &stop_char, 1);
590                 if (retval <= 0)
591                         return;
592         }
593
594         if (tty->termios.c_cflag & CRTSCTS) {
595                 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
596                 retval = send_control(gb_tty, gb_tty->ctrlout);
597         }
598 }
599
600 static void gb_tty_unthrottle(struct tty_struct *tty)
601 {
602         struct gb_tty *gb_tty = tty->driver_data;
603         unsigned char start_char;
604         int retval;
605
606         if (I_IXOFF(tty)) {
607                 start_char = START_CHAR(tty);
608                 retval = gb_tty_write(tty, &start_char, 1);
609                 if (retval <= 0)
610                         return;
611         }
612
613         if (tty->termios.c_cflag & CRTSCTS) {
614                 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
615                 retval = send_control(gb_tty, gb_tty->ctrlout);
616         }
617 }
618
619 static int get_serial_info(struct gb_tty *gb_tty,
620                            struct serial_struct __user *info)
621 {
622         struct serial_struct tmp;
623
624         memset(&tmp, 0, sizeof(tmp));
625         tmp.type = PORT_16550A;
626         tmp.line = gb_tty->minor;
627         tmp.xmit_fifo_size = 16;
628         tmp.baud_base = 9600;
629         tmp.close_delay = gb_tty->port.close_delay / 10;
630         tmp.closing_wait =
631                 gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
632                 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
633
634         if (copy_to_user(info, &tmp, sizeof(tmp)))
635                 return -EFAULT;
636         return 0;
637 }
638
639 static int set_serial_info(struct gb_tty *gb_tty,
640                            struct serial_struct __user *newinfo)
641 {
642         struct serial_struct new_serial;
643         unsigned int closing_wait;
644         unsigned int close_delay;
645         int retval = 0;
646
647         if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
648                 return -EFAULT;
649
650         close_delay = new_serial.close_delay * 10;
651         closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
652                         ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
653
654         mutex_lock(&gb_tty->port.mutex);
655         if (!capable(CAP_SYS_ADMIN)) {
656                 if ((close_delay != gb_tty->port.close_delay) ||
657                     (closing_wait != gb_tty->port.closing_wait))
658                         retval = -EPERM;
659         } else {
660                 gb_tty->port.close_delay = close_delay;
661                 gb_tty->port.closing_wait = closing_wait;
662         }
663         mutex_unlock(&gb_tty->port.mutex);
664         return retval;
665 }
666
667 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
668 {
669         int retval = 0;
670         DECLARE_WAITQUEUE(wait, current);
671         struct async_icount old;
672         struct async_icount new;
673
674         if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
675                 return -EINVAL;
676
677         do {
678                 spin_lock_irq(&gb_tty->read_lock);
679                 old = gb_tty->oldcount;
680                 new = gb_tty->iocount;
681                 gb_tty->oldcount = new;
682                 spin_unlock_irq(&gb_tty->read_lock);
683
684                 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
685                         break;
686                 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
687                         break;
688                 if ((arg & TIOCM_RI) && (old.rng != new.rng))
689                         break;
690
691                 add_wait_queue(&gb_tty->wioctl, &wait);
692                 set_current_state(TASK_INTERRUPTIBLE);
693                 schedule();
694                 remove_wait_queue(&gb_tty->wioctl, &wait);
695                 if (gb_tty->disconnected) {
696                         if (arg & TIOCM_CD)
697                                 break;
698                         retval = -ENODEV;
699                 } else if (signal_pending(current)) {
700                         retval = -ERESTARTSYS;
701                 }
702         } while (!retval);
703
704         return retval;
705 }
706
707 static int gb_tty_get_icount(struct tty_struct *tty,
708                              struct serial_icounter_struct *icount)
709 {
710         struct gb_tty *gb_tty = tty->driver_data;
711
712         icount->dsr = gb_tty->iocount.dsr;
713         icount->rng = gb_tty->iocount.rng;
714         icount->dcd = gb_tty->iocount.dcd;
715         icount->frame = gb_tty->iocount.frame;
716         icount->overrun = gb_tty->iocount.overrun;
717         icount->parity = gb_tty->iocount.parity;
718         icount->brk = gb_tty->iocount.brk;
719
720         return 0;
721 }
722
723 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
724                         unsigned long arg)
725 {
726         struct gb_tty *gb_tty = tty->driver_data;
727
728         switch (cmd) {
729         case TIOCGSERIAL:
730                 return get_serial_info(gb_tty,
731                                        (struct serial_struct __user *)arg);
732         case TIOCSSERIAL:
733                 return set_serial_info(gb_tty,
734                                        (struct serial_struct __user *)arg);
735         case TIOCMIWAIT:
736                 return wait_serial_change(gb_tty, arg);
737         }
738
739         return -ENOIOCTLCMD;
740 }
741
742 static void gb_tty_dtr_rts(struct tty_port *port, int on)
743 {
744         struct gb_tty *gb_tty;
745         u8 newctrl;
746
747         gb_tty = container_of(port, struct gb_tty, port);
748         newctrl = gb_tty->ctrlout;
749
750         if (on)
751                 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
752         else
753                 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
754
755         gb_tty->ctrlout = newctrl;
756         send_control(gb_tty, newctrl);
757 }
758
759 static int gb_tty_port_activate(struct tty_port *port,
760                                 struct tty_struct *tty)
761 {
762         struct gb_tty *gb_tty;
763
764         gb_tty = container_of(port, struct gb_tty, port);
765
766         return gbphy_runtime_get_sync(gb_tty->gbphy_dev);
767 }
768
769 static void gb_tty_port_shutdown(struct tty_port *port)
770 {
771         struct gb_tty *gb_tty;
772         unsigned long flags;
773         int ret;
774
775         gb_tty = container_of(port, struct gb_tty, port);
776
777         gb_tty->close_pending = true;
778
779         cancel_work_sync(&gb_tty->tx_work);
780
781         spin_lock_irqsave(&gb_tty->write_lock, flags);
782         kfifo_reset_out(&gb_tty->write_fifo);
783         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
784
785         if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
786                 goto out;
787
788         ret = gb_uart_flush(gb_tty, GB_SERIAL_FLAG_FLUSH_TRANSMITTER);
789         if (ret) {
790                 dev_err(&gb_tty->gbphy_dev->dev,
791                         "error flushing transmitter: %d\n", ret);
792         }
793
794         gb_uart_wait_for_all_credits(gb_tty);
795
796 out:
797         gb_tty->close_pending = false;
798
799         gbphy_runtime_put_autosuspend(gb_tty->gbphy_dev);
800 }
801
802 static const struct tty_operations gb_ops = {
803         .install =              gb_tty_install,
804         .open =                 gb_tty_open,
805         .close =                gb_tty_close,
806         .cleanup =              gb_tty_cleanup,
807         .hangup =               gb_tty_hangup,
808         .write =                gb_tty_write,
809         .write_room =           gb_tty_write_room,
810         .ioctl =                gb_tty_ioctl,
811         .throttle =             gb_tty_throttle,
812         .unthrottle =           gb_tty_unthrottle,
813         .chars_in_buffer =      gb_tty_chars_in_buffer,
814         .break_ctl =            gb_tty_break_ctl,
815         .set_termios =          gb_tty_set_termios,
816         .tiocmget =             gb_tty_tiocmget,
817         .tiocmset =             gb_tty_tiocmset,
818         .get_icount =           gb_tty_get_icount,
819 };
820
821 static const struct tty_port_operations gb_port_ops = {
822         .dtr_rts =              gb_tty_dtr_rts,
823         .activate =             gb_tty_port_activate,
824         .shutdown =             gb_tty_port_shutdown,
825 };
826
827 static int gb_uart_probe(struct gbphy_device *gbphy_dev,
828                          const struct gbphy_device_id *id)
829 {
830         struct gb_connection *connection;
831         size_t max_payload;
832         struct gb_tty *gb_tty;
833         struct device *tty_dev;
834         int retval;
835         int minor;
836
837         gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
838         if (!gb_tty)
839                 return -ENOMEM;
840
841         connection = gb_connection_create(gbphy_dev->bundle,
842                                           le16_to_cpu(gbphy_dev->cport_desc->id),
843                                           gb_uart_request_handler);
844         if (IS_ERR(connection)) {
845                 retval = PTR_ERR(connection);
846                 goto exit_tty_free;
847         }
848
849         max_payload = gb_operation_get_payload_size_max(connection);
850         if (max_payload < sizeof(struct gb_uart_send_data_request)) {
851                 retval = -EINVAL;
852                 goto exit_connection_destroy;
853         }
854
855         gb_tty->buffer_payload_max = max_payload -
856                         sizeof(struct gb_uart_send_data_request);
857
858         gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
859         if (!gb_tty->buffer) {
860                 retval = -ENOMEM;
861                 goto exit_connection_destroy;
862         }
863
864         INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
865
866         retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
867                              GFP_KERNEL);
868         if (retval)
869                 goto exit_buf_free;
870
871         gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
872         init_completion(&gb_tty->credits_complete);
873
874         minor = alloc_minor(gb_tty);
875         if (minor < 0) {
876                 if (minor == -ENOSPC) {
877                         dev_err(&gbphy_dev->dev,
878                                 "no more free minor numbers\n");
879                         retval = -ENODEV;
880                 } else {
881                         retval = minor;
882                 }
883                 goto exit_kfifo_free;
884         }
885
886         gb_tty->minor = minor;
887         spin_lock_init(&gb_tty->write_lock);
888         spin_lock_init(&gb_tty->read_lock);
889         init_waitqueue_head(&gb_tty->wioctl);
890         mutex_init(&gb_tty->mutex);
891
892         tty_port_init(&gb_tty->port);
893         gb_tty->port.ops = &gb_port_ops;
894
895         gb_tty->connection = connection;
896         gb_tty->gbphy_dev = gbphy_dev;
897         gb_connection_set_data(connection, gb_tty);
898         gb_gbphy_set_data(gbphy_dev, gb_tty);
899
900         retval = gb_connection_enable_tx(connection);
901         if (retval)
902                 goto exit_release_minor;
903
904         send_control(gb_tty, gb_tty->ctrlout);
905
906         /* initialize the uart to be 9600n81 */
907         gb_tty->line_coding.rate = cpu_to_le32(9600);
908         gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
909         gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
910         gb_tty->line_coding.data_bits = 8;
911         send_line_coding(gb_tty);
912
913         retval = gb_connection_enable(connection);
914         if (retval)
915                 goto exit_connection_disable;
916
917         tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
918                                            &gbphy_dev->dev);
919         if (IS_ERR(tty_dev)) {
920                 retval = PTR_ERR(tty_dev);
921                 goto exit_connection_disable;
922         }
923
924         gbphy_runtime_put_autosuspend(gbphy_dev);
925         return 0;
926
927 exit_connection_disable:
928         gb_connection_disable(connection);
929 exit_release_minor:
930         release_minor(gb_tty);
931 exit_kfifo_free:
932         kfifo_free(&gb_tty->write_fifo);
933 exit_buf_free:
934         kfree(gb_tty->buffer);
935 exit_connection_destroy:
936         gb_connection_destroy(connection);
937 exit_tty_free:
938         kfree(gb_tty);
939
940         return retval;
941 }
942
943 static void gb_uart_remove(struct gbphy_device *gbphy_dev)
944 {
945         struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
946         struct gb_connection *connection = gb_tty->connection;
947         struct tty_struct *tty;
948         int ret;
949
950         ret = gbphy_runtime_get_sync(gbphy_dev);
951         if (ret)
952                 gbphy_runtime_get_noresume(gbphy_dev);
953
954         mutex_lock(&gb_tty->mutex);
955         gb_tty->disconnected = true;
956
957         wake_up_all(&gb_tty->wioctl);
958         mutex_unlock(&gb_tty->mutex);
959
960         tty = tty_port_tty_get(&gb_tty->port);
961         if (tty) {
962                 tty_vhangup(tty);
963                 tty_kref_put(tty);
964         }
965
966         gb_connection_disable_rx(connection);
967         tty_unregister_device(gb_tty_driver, gb_tty->minor);
968
969         /* FIXME - free transmit / receive buffers */
970
971         gb_connection_disable(connection);
972         tty_port_destroy(&gb_tty->port);
973         gb_connection_destroy(connection);
974         release_minor(gb_tty);
975         kfifo_free(&gb_tty->write_fifo);
976         kfree(gb_tty->buffer);
977         kfree(gb_tty);
978 }
979
980 static int gb_tty_init(void)
981 {
982         int retval = 0;
983
984         gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
985         if (IS_ERR(gb_tty_driver)) {
986                 pr_err("Can not allocate tty driver\n");
987                 retval = -ENOMEM;
988                 goto fail_unregister_dev;
989         }
990
991         gb_tty_driver->driver_name = "gb";
992         gb_tty_driver->name = GB_NAME;
993         gb_tty_driver->major = 0;
994         gb_tty_driver->minor_start = 0;
995         gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
996         gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
997         gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
998         gb_tty_driver->init_termios = tty_std_termios;
999         gb_tty_driver->init_termios.c_cflag = B9600 | CS8 |
1000                 CREAD | HUPCL | CLOCAL;
1001         tty_set_operations(gb_tty_driver, &gb_ops);
1002
1003         retval = tty_register_driver(gb_tty_driver);
1004         if (retval) {
1005                 pr_err("Can not register tty driver: %d\n", retval);
1006                 goto fail_put_gb_tty;
1007         }
1008
1009         return 0;
1010
1011 fail_put_gb_tty:
1012         put_tty_driver(gb_tty_driver);
1013 fail_unregister_dev:
1014         return retval;
1015 }
1016
1017 static void gb_tty_exit(void)
1018 {
1019         tty_unregister_driver(gb_tty_driver);
1020         put_tty_driver(gb_tty_driver);
1021         idr_destroy(&tty_minors);
1022 }
1023
1024 static const struct gbphy_device_id gb_uart_id_table[] = {
1025         { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
1026         { },
1027 };
1028 MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
1029
1030 static struct gbphy_driver uart_driver = {
1031         .name           = "uart",
1032         .probe          = gb_uart_probe,
1033         .remove         = gb_uart_remove,
1034         .id_table       = gb_uart_id_table,
1035 };
1036
1037 static int gb_uart_driver_init(void)
1038 {
1039         int ret;
1040
1041         ret = gb_tty_init();
1042         if (ret)
1043                 return ret;
1044
1045         ret = gb_gbphy_register(&uart_driver);
1046         if (ret) {
1047                 gb_tty_exit();
1048                 return ret;
1049         }
1050
1051         return 0;
1052 }
1053 module_init(gb_uart_driver_init);
1054
1055 static void gb_uart_driver_exit(void)
1056 {
1057         gb_gbphy_deregister(&uart_driver);
1058         gb_tty_exit();
1059 }
1060
1061 module_exit(gb_uart_driver_exit);
1062 MODULE_LICENSE("GPL v2");