greybus: uart: Add support for UART error signals
[cascardo/linux.git] / drivers / staging / greybus / uart.c
1 /*
2  * UART driver for the Greybus "generic" UART module.
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  *
9  * Heavily based on drivers/usb/class/cdc-acm.c and
10  * drivers/usb/serial/usb-serial.c.
11  */
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
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>
28 #include <linux/fs.h>
29 #include <linux/kdev_t.h>
30
31 #include "greybus.h"
32
33 #define GB_NUM_MINORS   16      /* 16 is is more than enough */
34 #define GB_NAME         "ttyGB"
35
36 struct gb_tty_line_coding {
37         __le32  rate;
38         __u8    format;
39         __u8    parity;
40         __u8    data_bits;
41 };
42
43 struct gb_tty {
44         struct tty_port port;
45         void *buffer;
46         u32 buffer_payload_max;
47         struct gb_connection *connection;
48         u16 cport_id;
49         unsigned int minor;
50         unsigned char clocal;
51         bool disconnected;
52         spinlock_t read_lock;
53         spinlock_t write_lock;
54         struct async_icount iocount;
55         struct async_icount oldcount;
56         wait_queue_head_t wioctl;
57         struct mutex mutex;
58         u8 version_major;
59         u8 version_minor;
60         unsigned int ctrlin;    /* input control lines */
61         unsigned int ctrlout;   /* output control lines */
62         struct gb_tty_line_coding line_coding;
63 };
64
65 static struct tty_driver *gb_tty_driver;
66 static DEFINE_IDR(tty_minors);
67 static DEFINE_MUTEX(table_lock);
68 static atomic_t reference_count = ATOMIC_INIT(0);
69
70 /* Define get_version() routine */
71 define_get_version(gb_tty, UART);
72
73 static int gb_uart_receive_data(struct gb_tty *gb_tty,
74                                 struct gb_connection *connection,
75                                 struct gb_uart_recv_data_request *receive_data)
76 {
77         struct tty_port *port = &gb_tty->port;
78         u16 recv_data_size;
79         int count;
80         unsigned long tty_flags = TTY_NORMAL;
81
82         count = gb_tty->buffer_payload_max - sizeof(*receive_data);
83         recv_data_size = le16_to_cpu(receive_data->size);
84         if (!recv_data_size || recv_data_size > count)
85                 return -EINVAL;
86
87         if (receive_data->flags) {
88                 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
89                         tty_flags = TTY_BREAK;
90                 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
91                         tty_flags = TTY_PARITY;
92                 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
93                         tty_flags = TTY_FRAME;
94
95                 /* overrun is special, not associated with a char */
96                 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
97                         tty_insert_flip_char(port, 0, TTY_OVERRUN);
98         }
99         count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
100                                                   tty_flags, recv_data_size);
101         if (count != recv_data_size) {
102                 dev_err(&connection->dev,
103                         "UART: RX 0x%08x bytes only wrote 0x%08x\n",
104                         recv_data_size, count);
105         }
106         if (count)
107                 tty_flip_buffer_push(port);
108         return 0;
109 }
110
111 static int gb_uart_request_recv(u8 type, struct gb_operation *op)
112 {
113         struct gb_connection *connection = op->connection;
114         struct gb_tty *gb_tty = connection->private;
115         struct gb_message *request = op->request;
116         struct gb_uart_serial_state_request *serial_state;
117         int ret = 0;
118
119         switch (type) {
120         case GB_UART_TYPE_RECEIVE_DATA:
121                 ret = gb_uart_receive_data(gb_tty, connection,
122                                            request->payload);
123                 break;
124         case GB_UART_TYPE_SERIAL_STATE:
125                 serial_state = request->payload;
126                 gb_tty->ctrlin = le16_to_cpu(serial_state->control);
127                 break;
128         default:
129                 dev_err(&connection->dev,
130                         "unsupported unsolicited request: %02x\n", type);
131                 ret = -EINVAL;
132         }
133
134         return ret;
135 }
136
137 static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
138 {
139         struct gb_uart_send_data_request *request;
140         int ret;
141
142         if (!data || !size)
143                 return 0;
144
145         if (size > tty->buffer_payload_max)
146                 size = tty->buffer_payload_max;
147         request = tty->buffer;
148         request->size = cpu_to_le16(size);
149         memcpy(&request->data[0], data, size);
150         ret = gb_operation_sync(tty->connection, GB_UART_TYPE_SEND_DATA,
151                                 request, sizeof(*request) + size, NULL, 0);
152         if (ret)
153                 return ret;
154         else
155                 return size;
156 }
157
158 static int send_line_coding(struct gb_tty *tty)
159 {
160         struct gb_uart_set_line_coding_request request;
161
162         memcpy(&request, &tty->line_coding,
163                sizeof(tty->line_coding));
164         return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
165                                  &request, sizeof(request), NULL, 0);
166 }
167
168 static int send_control(struct gb_tty *gb_tty, u16 control)
169 {
170         struct gb_uart_set_control_line_state_request request;
171
172         request.control = cpu_to_le16(control);
173         return gb_operation_sync(gb_tty->connection,
174                                  GB_UART_TYPE_SET_CONTROL_LINE_STATE,
175                                  &request, sizeof(request), NULL, 0);
176 }
177
178 static int send_break(struct gb_tty *gb_tty, u8 state)
179 {
180         struct gb_uart_set_break_request request;
181
182         if ((state != 0) && (state != 1)) {
183                 dev_err(&gb_tty->connection->dev,
184                         "invalid break state of %d\n", state);
185                 return -EINVAL;
186         }
187
188         request.state = state;
189         return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SET_BREAK,
190                                  &request, sizeof(request), NULL, 0);
191 }
192
193
194 static struct gb_tty *get_gb_by_minor(unsigned minor)
195 {
196         struct gb_tty *gb_tty;
197
198         mutex_lock(&table_lock);
199         gb_tty = idr_find(&tty_minors, minor);
200         if (gb_tty) {
201                 mutex_lock(&gb_tty->mutex);
202                 if (gb_tty->disconnected) {
203                         mutex_unlock(&gb_tty->mutex);
204                         gb_tty = NULL;
205                 } else {
206                         tty_port_get(&gb_tty->port);
207                         mutex_unlock(&gb_tty->mutex);
208                 }
209         }
210         mutex_unlock(&table_lock);
211         return gb_tty;
212 }
213
214 static int alloc_minor(struct gb_tty *gb_tty)
215 {
216         int minor;
217
218         mutex_lock(&table_lock);
219         minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
220         mutex_unlock(&table_lock);
221         if (minor >= 0)
222                 gb_tty->minor = minor;
223         return minor;
224 }
225
226 static void release_minor(struct gb_tty *gb_tty)
227 {
228         int minor = gb_tty->minor;
229
230         gb_tty->minor = 0;      /* Maybe should use an invalid value instead */
231         mutex_lock(&table_lock);
232         idr_remove(&tty_minors, minor);
233         mutex_unlock(&table_lock);
234 }
235
236 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
237 {
238         struct gb_tty *gb_tty;
239         int retval;
240
241         gb_tty = get_gb_by_minor(tty->index);
242         if (!gb_tty)
243                 return -ENODEV;
244
245         retval = tty_standard_install(driver, tty);
246         if (retval)
247                 goto error;
248
249         tty->driver_data = gb_tty;
250         return 0;
251 error:
252         tty_port_put(&gb_tty->port);
253         return retval;
254 }
255
256 static int gb_tty_open(struct tty_struct *tty, struct file *file)
257 {
258         struct gb_tty *gb_tty = tty->driver_data;
259
260         return tty_port_open(&gb_tty->port, tty, file);
261 }
262
263 static void gb_tty_close(struct tty_struct *tty, struct file *file)
264 {
265         struct gb_tty *gb_tty = tty->driver_data;
266
267         tty_port_close(&gb_tty->port, tty, file);
268 }
269
270 static void gb_tty_cleanup(struct tty_struct *tty)
271 {
272         struct gb_tty *gb_tty = tty->driver_data;
273
274         tty_port_put(&gb_tty->port);
275 }
276
277 static void gb_tty_hangup(struct tty_struct *tty)
278 {
279         struct gb_tty *gb_tty = tty->driver_data;
280
281         tty_port_hangup(&gb_tty->port);
282 }
283
284 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
285                         int count)
286 {
287         struct gb_tty *gb_tty = tty->driver_data;
288
289         return send_data(gb_tty, count, buf);
290 }
291
292 static int gb_tty_write_room(struct tty_struct *tty)
293 {
294         struct gb_tty *gb_tty = tty->driver_data;
295
296         return gb_tty->buffer_payload_max;
297 }
298
299 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
300 {
301         return 0;
302 }
303
304 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
305 {
306         struct gb_tty *gb_tty = tty->driver_data;
307
308         return send_break(gb_tty, state ? 1 : 0);
309 }
310
311 static void gb_tty_set_termios(struct tty_struct *tty,
312                                struct ktermios *termios_old)
313 {
314         struct gb_tty *gb_tty = tty->driver_data;
315         struct ktermios *termios = &tty->termios;
316         struct gb_tty_line_coding newline;
317         int newctrl = gb_tty->ctrlout;
318
319         newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
320         newline.format = termios->c_cflag & CSTOPB ?
321                                 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
322         newline.parity = termios->c_cflag & PARENB ?
323                                 (termios->c_cflag & PARODD ? 1 : 2) +
324                                 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
325
326         switch (termios->c_cflag & CSIZE) {
327         case CS5:
328                 newline.data_bits = 5;
329                 break;
330         case CS6:
331                 newline.data_bits = 6;
332                 break;
333         case CS7:
334                 newline.data_bits = 7;
335                 break;
336         case CS8:
337         default:
338                 newline.data_bits = 8;
339                 break;
340         }
341
342         /* FIXME: needs to clear unsupported bits in the termios */
343         gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
344
345         if (C_BAUD(tty) == B0) {
346                 newline.rate = gb_tty->line_coding.rate;
347                 newctrl &= GB_UART_CTRL_DTR;
348         } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
349                 newctrl |= GB_UART_CTRL_DTR;
350         }
351
352         if (newctrl != gb_tty->ctrlout) {
353                 gb_tty->ctrlout = newctrl;
354                 send_control(gb_tty, newctrl);
355         }
356
357         if (memcpy(&gb_tty->line_coding, &newline, sizeof(newline))) {
358                 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
359                 send_line_coding(gb_tty);
360         }
361 }
362
363 static int gb_tty_tiocmget(struct tty_struct *tty)
364 {
365         struct gb_tty *gb_tty = tty->driver_data;
366
367         return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
368                (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
369                (gb_tty->ctrlin  & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
370                (gb_tty->ctrlin  & GB_UART_CTRL_RI  ? TIOCM_RI  : 0) |
371                (gb_tty->ctrlin  & GB_UART_CTRL_DCD ? TIOCM_CD  : 0) |
372                TIOCM_CTS;
373 }
374
375 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
376                            unsigned int clear)
377 {
378         struct gb_tty *gb_tty = tty->driver_data;
379         unsigned int newctrl = gb_tty->ctrlout;
380
381         set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
382               (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
383         clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
384                 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
385
386         newctrl = (newctrl & ~clear) | set;
387         if (gb_tty->ctrlout == newctrl)
388                 return 0;
389
390         gb_tty->ctrlout = newctrl;
391         return send_control(gb_tty, newctrl);
392 }
393
394 static void gb_tty_throttle(struct tty_struct *tty)
395 {
396         struct gb_tty *gb_tty = tty->driver_data;
397         unsigned char stop_char;
398         int retval;
399
400         if (I_IXOFF(tty)) {
401                 stop_char = STOP_CHAR(tty);
402                 retval = gb_tty_write(tty, &stop_char, 1);
403                 if (retval <= 0)
404                         return;
405         }
406
407         if (tty->termios.c_cflag & CRTSCTS) {
408                 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
409                 retval = send_control(gb_tty, gb_tty->ctrlout);
410         }
411
412 }
413
414 static void gb_tty_unthrottle(struct tty_struct *tty)
415 {
416         struct gb_tty *gb_tty = tty->driver_data;
417         unsigned char start_char;
418         int retval;
419
420         if (I_IXOFF(tty)) {
421                 start_char = START_CHAR(tty);
422                 retval = gb_tty_write(tty, &start_char, 1);
423                 if (retval <= 0)
424                         return;
425         }
426
427         if (tty->termios.c_cflag & CRTSCTS) {
428                 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
429                 retval = send_control(gb_tty, gb_tty->ctrlout);
430         }
431 }
432
433 static int get_serial_info(struct gb_tty *gb_tty,
434                            struct serial_struct __user *info)
435 {
436         struct serial_struct tmp;
437
438         if (!info)
439                 return -EINVAL;
440
441         memset(&tmp, 0, sizeof(tmp));
442         tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
443         tmp.type = PORT_16550A;
444         tmp.line = gb_tty->minor;
445         tmp.xmit_fifo_size = 16;
446         tmp.baud_base = 9600;
447         tmp.close_delay = gb_tty->port.close_delay / 10;
448         tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
449                                 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
450
451         if (copy_to_user(info, &tmp, sizeof(tmp)))
452                 return -EFAULT;
453         return 0;
454 }
455
456 static int set_serial_info(struct gb_tty *gb_tty,
457                            struct serial_struct __user *newinfo)
458 {
459         struct serial_struct new_serial;
460         unsigned int closing_wait;
461         unsigned int close_delay;
462         int retval = 0;
463
464         if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
465                 return -EFAULT;
466
467         close_delay = new_serial.close_delay * 10;
468         closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
469                         ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
470
471         mutex_lock(&gb_tty->port.mutex);
472         if (!capable(CAP_SYS_ADMIN)) {
473                 if ((close_delay != gb_tty->port.close_delay) ||
474                     (closing_wait != gb_tty->port.closing_wait))
475                         retval = -EPERM;
476                 else
477                         retval = -EOPNOTSUPP;
478         } else {
479                 gb_tty->port.close_delay = close_delay;
480                 gb_tty->port.closing_wait = closing_wait;
481         }
482         mutex_unlock(&gb_tty->port.mutex);
483         return retval;
484 }
485
486 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
487 {
488         int retval = 0;
489         DECLARE_WAITQUEUE(wait, current);
490         struct async_icount old;
491         struct async_icount new;
492
493         if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
494                 return -EINVAL;
495
496         do {
497                 spin_lock_irq(&gb_tty->read_lock);
498                 old = gb_tty->oldcount;
499                 new = gb_tty->iocount;
500                 gb_tty->oldcount = new;
501                 spin_unlock_irq(&gb_tty->read_lock);
502
503                 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
504                         break;
505                 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
506                         break;
507                 if ((arg & TIOCM_RI) && (old.rng != new.rng))
508                         break;
509
510                 add_wait_queue(&gb_tty->wioctl, &wait);
511                 set_current_state(TASK_INTERRUPTIBLE);
512                 schedule();
513                 remove_wait_queue(&gb_tty->wioctl, &wait);
514                 if (gb_tty->disconnected) {
515                         if (arg & TIOCM_CD)
516                                 break;
517                         retval = -ENODEV;
518                 } else if (signal_pending(current)) {
519                         retval = -ERESTARTSYS;
520                 }
521         } while (!retval);
522
523         return retval;
524 }
525
526 static int get_serial_usage(struct gb_tty *gb_tty,
527                             struct serial_icounter_struct __user *count)
528 {
529         struct serial_icounter_struct icount;
530         int retval = 0;
531
532         memset(&icount, 0, sizeof(icount));
533         icount.dsr = gb_tty->iocount.dsr;
534         icount.rng = gb_tty->iocount.rng;
535         icount.dcd = gb_tty->iocount.dcd;
536         icount.frame = gb_tty->iocount.frame;
537         icount.overrun = gb_tty->iocount.overrun;
538         icount.parity = gb_tty->iocount.parity;
539         icount.brk = gb_tty->iocount.brk;
540
541         if (copy_to_user(count, &icount, sizeof(icount)) > 0)
542                 retval = -EFAULT;
543
544         return retval;
545 }
546
547 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
548                         unsigned long arg)
549 {
550         struct gb_tty *gb_tty = tty->driver_data;
551
552         switch (cmd) {
553         case TIOCGSERIAL:
554                 return get_serial_info(gb_tty,
555                                        (struct serial_struct __user *)arg);
556         case TIOCSSERIAL:
557                 return set_serial_info(gb_tty,
558                                        (struct serial_struct __user *)arg);
559         case TIOCMIWAIT:
560                 return wait_serial_change(gb_tty, arg);
561         case TIOCGICOUNT:
562                 return get_serial_usage(gb_tty,
563                                         (struct serial_icounter_struct __user *)arg);
564         }
565
566         return -ENOIOCTLCMD;
567 }
568
569
570 static const struct tty_operations gb_ops = {
571         .install =              gb_tty_install,
572         .open =                 gb_tty_open,
573         .close =                gb_tty_close,
574         .cleanup =              gb_tty_cleanup,
575         .hangup =               gb_tty_hangup,
576         .write =                gb_tty_write,
577         .write_room =           gb_tty_write_room,
578         .ioctl =                gb_tty_ioctl,
579         .throttle =             gb_tty_throttle,
580         .unthrottle =           gb_tty_unthrottle,
581         .chars_in_buffer =      gb_tty_chars_in_buffer,
582         .break_ctl =            gb_tty_break_ctl,
583         .set_termios =          gb_tty_set_termios,
584         .tiocmget =             gb_tty_tiocmget,
585         .tiocmset =             gb_tty_tiocmset,
586 };
587
588 static struct tty_port_operations null_ops = { };
589
590 static int gb_tty_init(void);
591 static void gb_tty_exit(void);
592
593 static int gb_uart_connection_init(struct gb_connection *connection)
594 {
595         struct gb_tty *gb_tty;
596         struct device *tty_dev;
597         int retval;
598         int minor;
599
600         /* First time here, initialize the tty structures */
601         if (atomic_inc_return(&reference_count) == 1) {
602                 retval = gb_tty_init();
603                 if (retval) {
604                         atomic_dec(&reference_count);
605                         return retval;
606                 }
607         }
608
609         gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
610         if (!gb_tty) {
611                 retval = -ENOMEM;
612                 goto error_alloc;
613         }
614
615         gb_tty->buffer_payload_max =
616                 gb_operation_get_payload_size_max(connection);
617         if (!gb_tty->buffer_payload_max) {
618                 retval = -EINVAL;
619                 goto error_payload;
620         }
621
622         gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
623         if (!gb_tty->buffer) {
624                 retval = -ENOMEM;
625                 goto error_payload;
626         }
627
628         gb_tty->connection = connection;
629         connection->private = gb_tty;
630
631         /* Check for compatible protocol version */
632         retval = get_version(gb_tty);
633         if (retval)
634                 goto error_version;
635
636         minor = alloc_minor(gb_tty);
637         if (minor < 0) {
638                 if (minor == -ENOSPC) {
639                         dev_err(&connection->dev,
640                                 "no more free minor numbers\n");
641                         retval = -ENODEV;
642                         goto error_version;
643                 }
644                 retval = minor;
645                 goto error_version;
646         }
647
648         gb_tty->minor = minor;
649         spin_lock_init(&gb_tty->write_lock);
650         spin_lock_init(&gb_tty->read_lock);
651         init_waitqueue_head(&gb_tty->wioctl);
652         mutex_init(&gb_tty->mutex);
653
654         tty_port_init(&gb_tty->port);
655         gb_tty->port.ops = &null_ops;
656
657         send_control(gb_tty, gb_tty->ctrlout);
658
659         /* initialize the uart to be 9600n81 */
660         gb_tty->line_coding.rate = cpu_to_le32(9600);
661         gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
662         gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
663         gb_tty->line_coding.data_bits = 8;
664         send_line_coding(gb_tty);
665
666         tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
667                                            &connection->dev);
668         if (IS_ERR(tty_dev)) {
669                 retval = PTR_ERR(tty_dev);
670                 goto error;
671         }
672
673         return 0;
674 error:
675         tty_port_destroy(&gb_tty->port);
676         release_minor(gb_tty);
677 error_version:
678         connection->private = NULL;
679         kfree(gb_tty->buffer);
680 error_payload:
681         kfree(gb_tty);
682 error_alloc:
683         if (atomic_dec_return(&reference_count) == 0)
684                 gb_tty_exit();
685         return retval;
686 }
687
688 static void gb_uart_connection_exit(struct gb_connection *connection)
689 {
690         struct gb_tty *gb_tty = connection->private;
691         struct tty_struct *tty;
692
693         if (!gb_tty)
694                 return;
695
696         mutex_lock(&gb_tty->mutex);
697         gb_tty->disconnected = true;
698
699         wake_up_all(&gb_tty->wioctl);
700         connection->private = NULL;
701         mutex_unlock(&gb_tty->mutex);
702
703         tty = tty_port_tty_get(&gb_tty->port);
704         if (tty) {
705                 tty_vhangup(tty);
706                 tty_kref_put(tty);
707         }
708         /* FIXME - stop all traffic */
709
710         tty_unregister_device(gb_tty_driver, gb_tty->minor);
711
712         /* FIXME - free transmit / receive buffers */
713
714         tty_port_put(&gb_tty->port);
715         tty_port_destroy(&gb_tty->port);
716         kfree(gb_tty->buffer);
717         kfree(gb_tty);
718
719         /* If last device is gone, tear down the tty structures */
720         if (atomic_dec_return(&reference_count) == 0)
721                 gb_tty_exit();
722 }
723
724 static int gb_tty_init(void)
725 {
726         int retval = 0;
727
728         gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
729         if (IS_ERR(gb_tty_driver)) {
730                 pr_err("Can not allocate tty driver\n");
731                 retval = -ENOMEM;
732                 goto fail_unregister_dev;
733         }
734
735         gb_tty_driver->driver_name = "gb";
736         gb_tty_driver->name = GB_NAME;
737         gb_tty_driver->major = 0;
738         gb_tty_driver->minor_start = 0;
739         gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
740         gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
741         gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
742         gb_tty_driver->init_termios = tty_std_termios;
743         gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
744         tty_set_operations(gb_tty_driver, &gb_ops);
745
746         retval = tty_register_driver(gb_tty_driver);
747         if (retval) {
748                 pr_err("Can not register tty driver: %d\n", retval);
749                 goto fail_put_gb_tty;
750         }
751
752         return 0;
753
754 fail_put_gb_tty:
755         put_tty_driver(gb_tty_driver);
756 fail_unregister_dev:
757         return retval;
758 }
759
760 static void gb_tty_exit(void)
761 {
762         tty_unregister_driver(gb_tty_driver);
763         put_tty_driver(gb_tty_driver);
764 }
765
766 static struct gb_protocol uart_protocol = {
767         .name                   = "uart",
768         .id                     = GREYBUS_PROTOCOL_UART,
769         .major                  = 0,
770         .minor                  = 1,
771         .connection_init        = gb_uart_connection_init,
772         .connection_exit        = gb_uart_connection_exit,
773         .request_recv           = gb_uart_request_recv,
774 };
775
776 gb_gpbridge_protocol_driver(uart_protocol);