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