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