]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/uart-gb.c
greybus: Random spell fixes
[karo-tx-linux.git] / drivers / staging / greybus / uart-gb.c
1 /*
2  * UART driver for the Greybus "generic" UART module.
3  *
4  * Copyright 2014 Google Inc.
5  *
6  * Released under the GPLv2 only.
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.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/serial.h>
26 #include <linux/idr.h>
27 #include <linux/fs.h>
28 #include <linux/kdev_t.h>
29
30 #include "greybus.h"
31 #include "module.h"
32
33 #define GB_NUM_MINORS   255     /* 255 is enough for anyone... */
34 #define GB_NAME         "ttyGB"
35
36 /* Version of the Greybus PWM protocol we support */
37 #define GB_UART_VERSION_MAJOR           0x00
38 #define GB_UART_VERSION_MINOR           0x01
39
40 /* Greybus UART request types */
41 #define GB_UART_REQ_INVALID                     0x00
42 #define GB_UART_REQ_PROTOCOL_VERSION            0x01
43 #define GB_UART_REQ_SEND_DATA                   0x02
44 #define GB_UART_REQ_RECEIVE_DATA                0x03    /* Unsolicited data */
45 #define GB_UART_REQ_SET_LINE_CODING             0x04
46 #define GB_UART_REQ_SET_CONTROL_LINE_STATE      0x05
47 #define GB_UART_REQ_SET_BREAK                   0x06
48 #define GB_UART_REQ_SERIAL_STATE                0x07    /* Unsolicited data */
49 #define GB_UART_TYPE_RESPONSE                   0x80    /* OR'd with rest */
50
51 struct gb_uart_proto_version_response {
52         __u8    major;
53         __u8    minor;
54 };
55
56 struct gb_uart_send_data_request {
57         __le16  size;
58         __u8    data[0];
59 };
60
61 struct gb_serial_line_coding {
62         __le32  rate;
63         __u8    format;
64 #define GB_SERIAL_1_STOP_BITS           0
65 #define GB_SERIAL_1_5_STOP_BITS         1
66 #define GB_SERIAL_2_STOP_BITS           2
67
68         __u8    parity;
69 #define GB_SERIAL_NO_PARITY             0
70 #define GB_SERIAL_ODD_PARITY            1
71 #define GB_SERIAL_EVEN_PARITY           2
72 #define GB_SERIAL_MARK_PARITY           3
73 #define GB_SERIAL_SPACE_PARITY          4
74
75         __u8    data;
76 } __attribute__ ((packed));
77
78 struct gb_uart_set_line_coding_request {
79         struct gb_serial_line_coding    line_coding;
80 };
81
82 /* output control lines */
83 #define GB_UART_CTRL_DTR                0x01
84 #define GB_UART_CTRL_RTS                0x02
85
86 struct gb_uart_set_control_line_state_request {
87         __le16  control;
88 };
89
90 struct gb_uart_set_break_request {
91         __u8    state;
92 };
93
94 /* input control lines and line errors */
95 #define GB_UART_CTRL_DCD                0x01
96 #define GB_UART_CTRL_DSR                0x02
97 #define GB_UART_CTRL_BRK                0x04
98 #define GB_UART_CTRL_RI                 0x08
99
100 #define GB_UART_CTRL_FRAMING            0x10
101 #define GB_UART_CTRL_PARITY             0x20
102 #define GB_UART_CTRL_OVERRUN            0x40
103
104 struct gb_uart_serial_state_request {
105         __u16   control;
106 };
107
108 struct gb_tty {
109         struct tty_port port;
110         struct gb_connection *connection;
111         u16 cport_id;
112         unsigned int minor;
113         unsigned char clocal;
114         bool disconnected;
115         spinlock_t read_lock;
116         spinlock_t write_lock;
117         struct async_icount iocount;
118         struct async_icount oldcount;
119         wait_queue_head_t wioctl;
120         struct mutex mutex;
121         u8 version_major;
122         u8 version_minor;
123         unsigned int ctrlin;    /* input control lines */
124         unsigned int ctrlout;   /* output control lines */
125         struct gb_serial_line_coding line_coding;
126 };
127
128
129 static struct tty_driver *gb_tty_driver;
130 static DEFINE_IDR(tty_minors);
131 static DEFINE_MUTEX(table_lock);
132 static atomic_t reference_count = ATOMIC_INIT(0);
133
134 /*
135  * This request only uses the connection field, and if successful,
136  * fills in the major and minor protocol version of the target.
137  */
138 static int get_version(struct gb_tty *tty)
139 {
140         struct gb_operation *operation;
141         struct gb_uart_proto_version_response *response;
142         int ret;
143
144         operation = gb_operation_create(tty->connection,
145                                         GB_UART_REQ_PROTOCOL_VERSION,
146                                         0, sizeof(*response));
147         if (!operation)
148                 return -ENOMEM;
149
150         /* Synchronous operation--no callback */
151         ret = gb_operation_request_send(operation, NULL);
152         if (ret) {
153                 pr_err("version operation failed (%d)\n", ret);
154                 goto out;
155         }
156
157         if (operation->result) {
158                 ret = gb_operation_status_map(operation->result);
159                 gb_connection_err(tty->connection, "result %hhu",
160                         operation->result);
161         } else {
162                 response = operation->response->payload;
163                 if (response->major > GB_UART_VERSION_MAJOR) {
164                         pr_err("unsupported major version (%hhu > %hhu)\n",
165                                 response->major, GB_UART_VERSION_MAJOR);
166                         ret = -ENOTSUPP;
167                         goto out;
168                 }
169                 tty->version_major = response->major;
170                 tty->version_minor = response->minor;
171
172                 pr_debug("%s: version_major = %u version_minor = %u\n",
173                         __func__, tty->version_major, tty->version_minor);
174         }
175 out:
176         gb_operation_destroy(operation);
177
178         return ret;
179 }
180
181 static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
182 {
183         struct gb_connection *connection = tty->connection;
184         struct gb_operation *operation;
185         struct gb_uart_send_data_request *request;
186         int retval;
187
188         if (!data || !size)
189                 return 0;
190
191         operation = gb_operation_create(connection, GB_UART_REQ_SEND_DATA,
192                                         sizeof(*request) + size, 0);
193         if (!operation)
194                 return -ENOMEM;
195         request = operation->request->payload;
196         request->size = cpu_to_le16(size);
197         memcpy(&request->data[0], data, size);
198
199         /* Synchronous operation--no callback */
200         retval = gb_operation_request_send(operation, NULL);
201         if (retval) {
202                 dev_err(&connection->dev,
203                         "send data operation failed (%d)\n", retval);
204                 goto out;
205         }
206
207         if (operation->result) {
208                 retval = gb_operation_status_map(operation->result);
209                 gb_connection_err(connection, "send data result %hhu",
210                                   operation->result);
211         }
212 out:
213         gb_operation_destroy(operation);
214
215         return retval;
216 }
217
218 static int send_line_coding(struct gb_tty *tty,
219                             struct gb_serial_line_coding *line_coding)
220 {
221         struct gb_connection *connection = tty->connection;
222         struct gb_operation *operation;
223         struct gb_uart_set_line_coding_request *request;
224         int retval;
225
226         operation = gb_operation_create(connection, GB_UART_REQ_SET_LINE_CODING,
227                                         sizeof(*request), 0);
228         if (!operation)
229                 return -ENOMEM;
230         request = operation->request->payload;
231         memcpy(&request->line_coding, line_coding, sizeof(*line_coding));
232
233         /* Synchronous operation--no callback */
234         retval = gb_operation_request_send(operation, NULL);
235         if (retval) {
236                 dev_err(&connection->dev,
237                         "send line coding operation failed (%d)\n", retval);
238                 goto out;
239         }
240
241         if (operation->result) {
242                 retval = gb_operation_status_map(operation->result);
243                 gb_connection_err(connection, "send line coding result %hhu",
244                                   operation->result);
245         }
246 out:
247         gb_operation_destroy(operation);
248
249         return retval;
250 }
251
252 static int send_control(struct gb_tty *tty, u16 control)
253 {
254         struct gb_connection *connection = tty->connection;
255         struct gb_operation *operation;
256         struct gb_uart_set_control_line_state_request *request;
257         int retval;
258
259         operation = gb_operation_create(connection,
260                                         GB_UART_REQ_SET_CONTROL_LINE_STATE,
261                                         sizeof(*request), 0);
262         if (!operation)
263                 return -ENOMEM;
264         request = operation->request->payload;
265         request->control = cpu_to_le16(control);
266
267         /* Synchronous operation--no callback */
268         retval = gb_operation_request_send(operation, NULL);
269         if (retval) {
270                 dev_err(&connection->dev,
271                         "send control operation failed (%d)\n", retval);
272                 goto out;
273         }
274
275         if (operation->result) {
276                 retval = gb_operation_status_map(operation->result);
277                 gb_connection_err(connection, "send control result %hhu",
278                                   operation->result);
279         }
280 out:
281         gb_operation_destroy(operation);
282
283         return retval;
284 }
285
286 static int send_break(struct gb_tty *tty, u8 state)
287 {
288         struct gb_connection *connection = tty->connection;
289         struct gb_operation *operation;
290         struct gb_uart_set_break_request *request;
291         int retval;
292
293         if ((state != 0) && (state != 1)) {
294                 dev_err(&connection->dev, "invalid break state of %d\n", state);
295                 return -EINVAL;
296         }
297
298         operation = gb_operation_create(connection, GB_UART_REQ_SET_BREAK,
299                                         sizeof(*request), 0);
300         if (!operation)
301                 return -ENOMEM;
302         request = operation->request->payload;
303         request->state = state;
304
305         /* Synchronous operation--no callback */
306         retval = gb_operation_request_send(operation, NULL);
307         if (retval) {
308                 dev_err(&connection->dev,
309                         "send break operation failed (%d)\n", retval);
310                 goto out;
311         }
312
313         if (operation->result) {
314                 retval = gb_operation_status_map(operation->result);
315                 gb_connection_err(connection, "send break result %hhu",
316                                   operation->result);
317         }
318 out:
319         gb_operation_destroy(operation);
320
321         return retval;
322 }
323
324
325 static struct gb_tty *get_gb_by_minor(unsigned minor)
326 {
327         struct gb_tty *gb_tty;
328
329         mutex_lock(&table_lock);
330         gb_tty = idr_find(&tty_minors, minor);
331         if (gb_tty) {
332                 mutex_lock(&gb_tty->mutex);
333                 if (gb_tty->disconnected) {
334                         mutex_unlock(&gb_tty->mutex);
335                         gb_tty = NULL;
336                 } else {
337                         tty_port_get(&gb_tty->port);
338                         mutex_unlock(&gb_tty->mutex);
339                 }
340         }
341         mutex_unlock(&table_lock);
342         return gb_tty;
343 }
344
345 static int alloc_minor(struct gb_tty *gb_tty)
346 {
347         int minor;
348
349         mutex_lock(&table_lock);
350         minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
351         mutex_unlock(&table_lock);
352         if (minor >= 0)
353                 gb_tty->minor = minor;
354         return minor;
355 }
356
357 static void release_minor(struct gb_tty *gb_tty)
358 {
359         int minor = gb_tty->minor;
360
361         gb_tty->minor = 0;      /* Maybe should use an invalid value instead */
362         mutex_lock(&table_lock);
363         idr_remove(&tty_minors, minor);
364         mutex_unlock(&table_lock);
365 }
366
367 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
368 {
369         struct gb_tty *gb_tty;
370         int retval;
371
372         gb_tty = get_gb_by_minor(tty->index);
373         if (!gb_tty)
374                 return -ENODEV;
375
376         retval = tty_standard_install(driver, tty);
377         if (retval)
378                 goto error;
379
380         tty->driver_data = gb_tty;
381         return 0;
382 error:
383         tty_port_put(&gb_tty->port);
384         return retval;
385 }
386
387 static int gb_tty_open(struct tty_struct *tty, struct file *file)
388 {
389         struct gb_tty *gb_tty = tty->driver_data;
390
391         return tty_port_open(&gb_tty->port, tty, file);
392 }
393
394 static void gb_tty_close(struct tty_struct *tty, struct file *file)
395 {
396         struct gb_tty *gb_tty = tty->driver_data;
397
398         tty_port_close(&gb_tty->port, tty, file);
399 }
400
401 static void gb_tty_cleanup(struct tty_struct *tty)
402 {
403         struct gb_tty *gb_tty = tty->driver_data;
404
405         tty_port_put(&gb_tty->port);
406 }
407
408 static void gb_tty_hangup(struct tty_struct *tty)
409 {
410         struct gb_tty *gb_tty = tty->driver_data;
411
412         tty_port_hangup(&gb_tty->port);
413 }
414
415 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
416                         int count)
417 {
418         struct gb_tty *gb_tty = tty->driver_data;
419
420         return send_data(gb_tty, count, buf);
421 }
422
423 static int gb_tty_write_room(struct tty_struct *tty)
424 {
425 //      struct gb_tty *gb_tty = tty->driver_data;
426
427         // FIXME - how much do we want to say we have room for?
428         return 0;
429 }
430
431 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
432 {
433 //      struct gb_tty *gb_tty = tty->driver_data;
434
435         // FIXME - how many left to send?
436         return 0;
437 }
438
439 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
440 {
441         struct gb_tty *gb_tty = tty->driver_data;
442
443         return send_break(gb_tty, state ? 1 : 0);
444 }
445
446 static void gb_tty_set_termios(struct tty_struct *tty,
447                                struct ktermios *termios_old)
448 {
449         struct gb_tty *gb_tty = tty->driver_data;
450         struct ktermios *termios = &tty->termios;
451         struct gb_serial_line_coding newline;
452         int newctrl = gb_tty->ctrlout;
453
454         newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
455         newline.format = termios->c_cflag & CSTOPB ? 2 : 0;
456         newline.parity = termios->c_cflag & PARENB ?
457                                 (termios->c_cflag & PARODD ? 1 : 2) +
458                                 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
459
460         switch (termios->c_cflag & CSIZE) {
461         case CS5:
462                 newline.data = 5;
463                 break;
464         case CS6:
465                 newline.data = 6;
466                 break;
467         case CS7:
468                 newline.data = 7;
469                 break;
470         case CS8:
471         default:
472                 newline.data = 8;
473                 break;
474         }
475
476         /* FIXME: needs to clear unsupported bits in the termios */
477         gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
478
479         if (C_BAUD(tty) == B0) {
480                 newline.rate = gb_tty->line_coding.rate;
481                 newctrl &= GB_UART_CTRL_DTR;
482         } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
483                 newctrl |= GB_UART_CTRL_DTR;
484         }
485
486         if (newctrl != gb_tty->ctrlout) {
487                 gb_tty->ctrlout = newctrl;
488                 send_control(gb_tty, newctrl);
489         }
490
491         if (memcpy(&gb_tty->line_coding, &newline, sizeof(newline))) {
492                 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
493                 send_line_coding(gb_tty, &gb_tty->line_coding);
494         }
495 }
496
497 static int gb_tty_tiocmget(struct tty_struct *tty)
498 {
499         struct gb_tty *gb_tty = tty->driver_data;
500
501         return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
502                (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
503                (gb_tty->ctrlin  & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
504                (gb_tty->ctrlin  & GB_UART_CTRL_RI  ? TIOCM_RI  : 0) |
505                (gb_tty->ctrlin  & GB_UART_CTRL_DCD ? TIOCM_CD  : 0) |
506                TIOCM_CTS;
507 }
508
509 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
510                            unsigned int clear)
511 {
512         struct gb_tty *gb_tty = tty->driver_data;
513         unsigned int newctrl = gb_tty->ctrlout;
514
515         set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
516               (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
517         clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
518                 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
519
520         newctrl = (newctrl & ~clear) | set;
521         if (gb_tty->ctrlout == newctrl)
522                 return 0;
523
524         gb_tty->ctrlout = newctrl;
525         return send_control(gb_tty, newctrl);
526 }
527
528 static void gb_tty_throttle(struct tty_struct *tty)
529 {
530         struct gb_tty *gb_tty = tty->driver_data;
531         unsigned char stop_char;
532         int retval;
533
534         if (I_IXOFF(tty)) {
535                 stop_char = STOP_CHAR(tty);
536                 retval = gb_tty_write(tty, &stop_char, 1);
537                 if (retval <= 0)
538                         return;
539         }
540
541         if (tty->termios.c_cflag & CRTSCTS) {
542                 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
543                 retval = send_control(gb_tty, gb_tty->ctrlout);
544         }
545
546 }
547
548 static void gb_tty_unthrottle(struct tty_struct *tty)
549 {
550         struct gb_tty *gb_tty = tty->driver_data;
551         unsigned char start_char;
552         int retval;
553
554         if (I_IXOFF(tty)) {
555                 start_char = START_CHAR(tty);
556                 retval = gb_tty_write(tty, &start_char, 1);
557                 if (retval <= 0)
558                         return;
559         }
560
561         if (tty->termios.c_cflag & CRTSCTS) {
562                 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
563                 retval = send_control(gb_tty, gb_tty->ctrlout);
564         }
565 }
566
567 static int get_serial_info(struct gb_tty *gb_tty,
568                            struct serial_struct __user *info)
569 {
570         struct serial_struct tmp;
571
572         if (!info)
573                 return -EINVAL;
574
575         memset(&tmp, 0, sizeof(tmp));
576         tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
577         tmp.type = PORT_16550A;
578         tmp.line = gb_tty->minor;
579         tmp.xmit_fifo_size = 16;
580         tmp.baud_base = 9600;
581         tmp.close_delay = gb_tty->port.close_delay / 10;
582         tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
583                                 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
584
585         if (copy_to_user(info, &tmp, sizeof(tmp)))
586                 return -EFAULT;
587         return 0;
588 }
589
590 static int set_serial_info(struct gb_tty *gb_tty,
591                            struct serial_struct __user *newinfo)
592 {
593         struct serial_struct new_serial;
594         unsigned int closing_wait;
595         unsigned int close_delay;
596         int retval = 0;
597
598         if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
599                 return -EFAULT;
600
601         close_delay = new_serial.close_delay * 10;
602         closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
603                         ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
604
605         mutex_lock(&gb_tty->port.mutex);
606         if (!capable(CAP_SYS_ADMIN)) {
607                 if ((close_delay != gb_tty->port.close_delay) ||
608                     (closing_wait != gb_tty->port.closing_wait))
609                         retval = -EPERM;
610                 else
611                         retval = -EOPNOTSUPP;
612         } else {
613                 gb_tty->port.close_delay = close_delay;
614                 gb_tty->port.closing_wait = closing_wait;
615         }
616         mutex_unlock(&gb_tty->port.mutex);
617         return retval;
618 }
619
620 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
621 {
622         int retval = 0;
623         DECLARE_WAITQUEUE(wait, current);
624         struct async_icount old;
625         struct async_icount new;
626
627         if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
628                 return -EINVAL;
629
630         do {
631                 spin_lock_irq(&gb_tty->read_lock);
632                 old = gb_tty->oldcount;
633                 new = gb_tty->iocount;
634                 gb_tty->oldcount = new;
635                 spin_unlock_irq(&gb_tty->read_lock);
636
637                 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
638                         break;
639                 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
640                         break;
641                 if ((arg & TIOCM_RI) && (old.rng != new.rng))
642                         break;
643
644                 add_wait_queue(&gb_tty->wioctl, &wait);
645                 set_current_state(TASK_INTERRUPTIBLE);
646                 schedule();
647                 remove_wait_queue(&gb_tty->wioctl, &wait);
648                 if (gb_tty->disconnected) {
649                         if (arg & TIOCM_CD)
650                                 break;
651                         retval = -ENODEV;
652                 } else if (signal_pending(current)) {
653                         retval = -ERESTARTSYS;
654                 }
655         } while (!retval);
656
657         return retval;
658 }
659
660 static int get_serial_usage(struct gb_tty *gb_tty,
661                             struct serial_icounter_struct __user *count)
662 {
663         struct serial_icounter_struct icount;
664         int retval = 0;
665
666         memset(&icount, 0, sizeof(icount));
667         icount.dsr = gb_tty->iocount.dsr;
668         icount.rng = gb_tty->iocount.rng;
669         icount.dcd = gb_tty->iocount.dcd;
670         icount.frame = gb_tty->iocount.frame;
671         icount.overrun = gb_tty->iocount.overrun;
672         icount.parity = gb_tty->iocount.parity;
673         icount.brk = gb_tty->iocount.brk;
674
675         if (copy_to_user(count, &icount, sizeof(icount)) > 0)
676                 retval = -EFAULT;
677
678         return retval;
679 }
680
681 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
682                         unsigned long arg)
683 {
684         struct gb_tty *gb_tty = tty->driver_data;
685
686         switch (cmd) {
687         case TIOCGSERIAL:
688                 return get_serial_info(gb_tty,
689                                        (struct serial_struct __user *)arg);
690         case TIOCSSERIAL:
691                 return set_serial_info(gb_tty,
692                                        (struct serial_struct __user *)arg);
693         case TIOCMIWAIT:
694                 return wait_serial_change(gb_tty, arg);
695         case TIOCGICOUNT:
696                 return get_serial_usage(gb_tty,
697                                         (struct serial_icounter_struct __user *)arg);
698         }
699
700         return -ENOIOCTLCMD;
701 }
702
703
704 static const struct tty_operations gb_ops = {
705         .install =              gb_tty_install,
706         .open =                 gb_tty_open,
707         .close =                gb_tty_close,
708         .cleanup =              gb_tty_cleanup,
709         .hangup =               gb_tty_hangup,
710         .write =                gb_tty_write,
711         .write_room =           gb_tty_write_room,
712         .ioctl =                gb_tty_ioctl,
713         .throttle =             gb_tty_throttle,
714         .unthrottle =           gb_tty_unthrottle,
715         .chars_in_buffer =      gb_tty_chars_in_buffer,
716         .break_ctl =            gb_tty_break_ctl,
717         .set_termios =          gb_tty_set_termios,
718         .tiocmget =             gb_tty_tiocmget,
719         .tiocmset =             gb_tty_tiocmset,
720 };
721
722
723 static int gb_tty_init(void);
724 static void gb_tty_exit(void);
725
726 static int gb_uart_connection_init(struct gb_connection *connection)
727 {
728         struct gb_tty *gb_tty;
729         struct device *tty_dev;
730         int retval;
731         int minor;
732
733         /* First time here, initialize the tty structures */
734         if (atomic_inc_return(&reference_count) == 1) {
735                 retval = gb_tty_init();
736                 if (retval) {
737                         atomic_dec(&reference_count);
738                         return retval;
739                 }
740         }
741
742         gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
743         if (!gb_tty)
744                 return -ENOMEM;
745         gb_tty->connection = connection;
746
747         /* Check for compatible protocol version */
748         retval = get_version(gb_tty);
749         if (retval)
750                 goto error_version;
751
752         minor = alloc_minor(gb_tty);
753         if (minor < 0) {
754                 if (minor == -ENOSPC) {
755                         dev_err(&connection->dev,
756                                 "no more free minor numbers\n");
757                         return -ENODEV;
758                 }
759                 return minor;
760         }
761
762         gb_tty->minor = minor;
763         spin_lock_init(&gb_tty->write_lock);
764         spin_lock_init(&gb_tty->read_lock);
765         init_waitqueue_head(&gb_tty->wioctl);
766         mutex_init(&gb_tty->mutex);
767
768         connection->private = gb_tty;
769
770         send_control(gb_tty, gb_tty->ctrlout);
771
772         /* initialize the uart to be 9600n81 */
773         gb_tty->line_coding.rate = cpu_to_le32(9600);
774         gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
775         gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
776         gb_tty->line_coding.data = 8;
777         send_line_coding(gb_tty, &gb_tty->line_coding);
778
779         tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
780                                            &connection->dev);
781         if (IS_ERR(tty_dev)) {
782                 retval = PTR_ERR(tty_dev);
783                 goto error;
784         }
785
786         return 0;
787 error:
788         release_minor(gb_tty);
789 error_version:
790         connection->private = NULL;
791         kfree(gb_tty);
792         return retval;
793 }
794
795 static void gb_uart_connection_exit(struct gb_connection *connection)
796 {
797         struct gb_tty *gb_tty = connection->private;
798         struct tty_struct *tty;
799
800         if (!gb_tty)
801                 return;
802
803         mutex_lock(&gb_tty->mutex);
804         gb_tty->disconnected = true;
805
806         wake_up_all(&gb_tty->wioctl);
807         connection->private = NULL;
808         mutex_unlock(&gb_tty->mutex);
809
810         tty = tty_port_tty_get(&gb_tty->port);
811         if (tty) {
812                 tty_vhangup(tty);
813                 tty_kref_put(tty);
814         }
815         /* FIXME - stop all traffic */
816
817         tty_unregister_device(gb_tty_driver, gb_tty->minor);
818
819         /* FIXME - free transmit / receive buffers */
820
821         tty_port_put(&gb_tty->port);
822
823         kfree(gb_tty);
824
825         /* If last device is gone, tear down the tty structures */
826         if (atomic_dec_return(&reference_count) == 0)
827                 gb_tty_exit();
828 }
829
830 static int gb_tty_init(void)
831 {
832         int retval = 0;
833
834         gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
835         if (IS_ERR(gb_tty_driver)) {
836                 pr_err("Can not allocate tty driver\n");
837                 retval = -ENOMEM;
838                 goto fail_unregister_dev;
839         }
840
841         gb_tty_driver->driver_name = "gb";
842         gb_tty_driver->name = GB_NAME;
843         gb_tty_driver->major = 0;
844         gb_tty_driver->minor_start = 0;
845         gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
846         gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
847         gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
848         gb_tty_driver->init_termios = tty_std_termios;
849         gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
850         tty_set_operations(gb_tty_driver, &gb_ops);
851
852         retval = tty_register_driver(gb_tty_driver);
853         if (retval) {
854                 pr_err("Can not register tty driver: %d\n", retval);
855                 goto fail_put_gb_tty;
856         }
857
858         return 0;
859
860 fail_put_gb_tty:
861         put_tty_driver(gb_tty_driver);
862 fail_unregister_dev:
863         return retval;
864 }
865
866 static void gb_tty_exit(void)
867 {
868         int major = MAJOR(gb_tty_driver->major);
869         int minor = gb_tty_driver->minor_start;
870
871         tty_unregister_driver(gb_tty_driver);
872         put_tty_driver(gb_tty_driver);
873         unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
874 }
875
876 static struct gb_protocol uart_protocol = {
877         .id                     = GREYBUS_PROTOCOL_UART,
878         .major                  = 0,
879         .minor                  = 1,
880         .connection_init        = gb_uart_connection_init,
881         .connection_exit        = gb_uart_connection_exit,
882         .request_recv           = NULL, /* FIXME we have 2 types of requests!!! */
883 };
884
885 bool gb_uart_protocol_init(void)
886 {
887         return gb_protocol_register(&uart_protocol);
888 }
889
890 void gb_uart_protocol_exit(void)
891 {
892         gb_protocol_deregister(&uart_protocol);
893 }