blob: 301868cd587830ff232d8a1db61fbab7d639f509 [file] [log] [blame]
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +08001/*
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +08002 * UART driver for the Greybus "generic" UART module.
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +08003 *
4 * Copyright 2014 Google Inc.
5 *
6 * Released under the GPLv2 only.
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +08007 *
8 * Heavily based on drivers/usb/class/cdc-acm.c and
9 * drivers/usb/serial/usb-serial.c.
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080010 */
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -070011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080012
13#include <linux/kernel.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080014#include <linux/errno.h>
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080015#include <linux/module.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080016#include <linux/sched.h>
17#include <linux/wait.h>
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080018#include <linux/slab.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080019#include <linux/uaccess.h>
20#include <linux/mutex.h>
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080021#include <linux/tty.h>
22#include <linux/serial.h>
23#include <linux/tty_driver.h>
24#include <linux/tty_flip.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080025#include <linux/serial.h>
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +080026#include <linux/idr.h>
Marti Bolivar7fabc882014-09-05 23:56:10 -040027#include <linux/fs.h>
28#include <linux/kdev_t.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050029
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080030#include "greybus.h"
Alex Eldere1e9dbd2014-10-01 21:54:11 -050031#include "module.h"
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080032
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +080033#define GB_NUM_MINORS 255 /* 255 is enough for anyone... */
Marti Bolivar7fabc882014-09-05 23:56:10 -040034#define GB_NAME "ttyGB"
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080035
36struct gb_tty {
37 struct tty_port port;
Alex Eldere1e9dbd2014-10-01 21:54:11 -050038 struct gb_module *gmod;
Alex Elder1cfc6672014-09-30 19:25:21 -050039 u16 cport_id;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080040 unsigned int minor;
41 unsigned char clocal;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +080042 unsigned int throttled:1;
43 unsigned int throttle_req:1;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080044 bool disconnected;
45 int writesize; // FIXME - set this somehow.
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +080046 spinlock_t read_lock;
47 spinlock_t write_lock;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080048 struct async_icount iocount;
49 struct async_icount oldcount;
50 wait_queue_head_t wioctl;
51 struct mutex mutex;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080052};
53
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -070054static const struct greybus_module_id id_table[] = {
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080055 { GREYBUS_DEVICE(0x45, 0x45) }, /* make shit up */
56 { }, /* terminating NULL entry */
57};
58
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +080059static struct tty_driver *gb_tty_driver;
60static DEFINE_IDR(tty_minors);
61static DEFINE_MUTEX(table_lock);
62
63static struct gb_tty *get_gb_by_minor(unsigned minor)
64{
65 struct gb_tty *gb_tty;
66
67 mutex_lock(&table_lock);
68 gb_tty = idr_find(&tty_minors, minor);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080069 if (gb_tty) {
70 mutex_lock(&gb_tty->mutex);
71 if (gb_tty->disconnected) {
72 mutex_unlock(&gb_tty->mutex);
73 gb_tty = NULL;
74 } else {
75 tty_port_get(&gb_tty->port);
76 mutex_unlock(&gb_tty->mutex);
77 }
78 }
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +080079 mutex_unlock(&table_lock);
80 return gb_tty;
81}
82
83static int alloc_minor(struct gb_tty *gb_tty)
84{
85 int minor;
86
87 mutex_lock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -050088 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +080089 mutex_unlock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -050090 if (minor >= 0)
91 gb_tty->minor = minor;
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +080092 return minor;
93}
94
95static void release_minor(struct gb_tty *gb_tty)
96{
Alex Elderff5f0b32014-08-18 18:25:11 -050097 int minor = gb_tty->minor;
98
99 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800100 mutex_lock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -0500101 idr_remove(&tty_minors, minor);
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800102 mutex_unlock(&table_lock);
103}
104
105static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
106{
107 struct gb_tty *gb_tty;
108 int retval;
109
110 gb_tty = get_gb_by_minor(tty->index);
111 if (!gb_tty)
112 return -ENODEV;
113
114 retval = tty_standard_install(driver, tty);
115 if (retval)
116 goto error;
117
118 tty->driver_data = gb_tty;
119 return 0;
120error:
121 tty_port_put(&gb_tty->port);
122 return retval;
123}
124
125static int gb_tty_open(struct tty_struct *tty, struct file *file)
126{
127 struct gb_tty *gb_tty = tty->driver_data;
128
129 return tty_port_open(&gb_tty->port, tty, file);
130}
131
132static void gb_tty_close(struct tty_struct *tty, struct file *file)
133{
134 struct gb_tty *gb_tty = tty->driver_data;
135
136 tty_port_close(&gb_tty->port, tty, file);
137}
138
139static void gb_tty_cleanup(struct tty_struct *tty)
140{
141 struct gb_tty *gb_tty = tty->driver_data;
142
143 tty_port_put(&gb_tty->port);
144}
145
146static void gb_tty_hangup(struct tty_struct *tty)
147{
148 struct gb_tty *gb_tty = tty->driver_data;
149
150 tty_port_hangup(&gb_tty->port);
151}
152
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800153static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
154 int count)
155{
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800156// struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800157
158 // FIXME - actually implement...
159
160 return 0;
161}
162
163static int gb_tty_write_room(struct tty_struct *tty)
164{
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800165// struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800166
167 // FIXME - how much do we want to say we have room for?
168 return 0;
169}
170
171static int gb_tty_chars_in_buffer(struct tty_struct *tty)
172{
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800173// struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800174
175 // FIXME - how many left to send?
176 return 0;
177}
178
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800179static int gb_tty_break_ctl(struct tty_struct *tty, int state)
180{
181// struct gb_tty *gb_tty = tty->driver_data;
182
183 // FIXME - send a break, if asked to...
184 return 0;
185}
186
187static void gb_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
188{
189 // FIXME - is this it???
190 tty_termios_copy_hw(&tty->termios, old);
191}
192
193static int gb_tty_tiocmget(struct tty_struct *tty)
194{
195// struct gb_tty *gb_tty = tty->driver_data;
196
197 // FIXME - get some tiocms!
198 return 0;
199}
200
201static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
202 unsigned int clear)
203{
204// struct gb_tty *gb_tty = tty->driver_data;
205
206 // FIXME - set some tiocms!
207 return 0;
208}
209
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800210static void gb_tty_throttle(struct tty_struct *tty)
211{
212 struct gb_tty *gb_tty = tty->driver_data;
213
214 spin_lock_irq(&gb_tty->read_lock);
215 gb_tty->throttle_req = 1;
216 spin_unlock_irq(&gb_tty->read_lock);
217}
218
219static void gb_tty_unthrottle(struct tty_struct *tty)
220{
221 struct gb_tty *gb_tty = tty->driver_data;
222 unsigned int was_throttled;
223
224 spin_lock_irq(&gb_tty->read_lock);
225 was_throttled = gb_tty->throttled;
226 gb_tty->throttle_req = 0;
227 gb_tty->throttled = 0;
228 spin_unlock_irq(&gb_tty->read_lock);
229
230 if (was_throttled) {
231 // FIXME - send more data
232 }
233}
234
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800235static int get_serial_info(struct gb_tty *gb_tty,
236 struct serial_struct __user *info)
237{
238 struct serial_struct tmp;
239
240 if (!info)
241 return -EINVAL;
242
243 memset(&tmp, 0, sizeof(tmp));
244 tmp.flags = ASYNC_LOW_LATENCY;
245 tmp.xmit_fifo_size = gb_tty->writesize;
246 tmp.baud_base = 0; // FIXME
247 tmp.close_delay = gb_tty->port.close_delay / 10;
248 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
249 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
250
251 if (copy_to_user(info, &tmp, sizeof(tmp)))
252 return -EFAULT;
Greg Kroah-Hartman3be03d42014-09-01 19:10:06 -0700253 return 0;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800254}
255
256static int set_serial_info(struct gb_tty *gb_tty,
257 struct serial_struct __user *newinfo)
258{
259 struct serial_struct new_serial;
260 unsigned int closing_wait;
261 unsigned int close_delay;
Alex Elder69f93ab2014-09-22 18:53:02 -0500262 int retval = 0;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800263
264 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
265 return -EFAULT;
266
267 close_delay = new_serial.close_delay * 10;
268 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
269 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
270
271 mutex_lock(&gb_tty->port.mutex);
272 if (!capable(CAP_SYS_ADMIN)) {
273 if ((close_delay != gb_tty->port.close_delay) ||
274 (closing_wait != gb_tty->port.closing_wait))
275 retval = -EPERM;
276 else
277 retval = -EOPNOTSUPP;
278 } else {
279 gb_tty->port.close_delay = close_delay;
280 gb_tty->port.closing_wait = closing_wait;
281 }
282 mutex_unlock(&gb_tty->port.mutex);
283 return retval;
284}
285
286static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
287{
288 int retval = 0;
289 DECLARE_WAITQUEUE(wait, current);
290 struct async_icount old;
291 struct async_icount new;
292
Alex Eldercaaa8a82014-08-18 18:25:12 -0500293 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800294 return -EINVAL;
295
296 do {
297 spin_lock_irq(&gb_tty->read_lock);
298 old = gb_tty->oldcount;
299 new = gb_tty->iocount;
300 gb_tty->oldcount = new;
Alex Eldercaaa8a82014-08-18 18:25:12 -0500301 spin_unlock_irq(&gb_tty->read_lock);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800302
303 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
304 break;
305 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
306 break;
307 if ((arg & TIOCM_RI) && (old.rng != new.rng))
308 break;
309
310 add_wait_queue(&gb_tty->wioctl, &wait);
311 set_current_state(TASK_INTERRUPTIBLE);
312 schedule();
313 remove_wait_queue(&gb_tty->wioctl, &wait);
314 if (gb_tty->disconnected) {
315 if (arg & TIOCM_CD)
316 break;
Alex Eldercaaa8a82014-08-18 18:25:12 -0500317 retval = -ENODEV;
318 } else if (signal_pending(current)) {
319 retval = -ERESTARTSYS;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800320 }
321 } while (!retval);
322
323 return retval;
324}
325
326static int get_serial_usage(struct gb_tty *gb_tty,
327 struct serial_icounter_struct __user *count)
328{
329 struct serial_icounter_struct icount;
330 int retval = 0;
331
332 memset(&icount, 0, sizeof(icount));
333 icount.dsr = gb_tty->iocount.dsr;
334 icount.rng = gb_tty->iocount.rng;
335 icount.dcd = gb_tty->iocount.dcd;
336 icount.frame = gb_tty->iocount.frame;
337 icount.overrun = gb_tty->iocount.overrun;
338 icount.parity = gb_tty->iocount.parity;
339 icount.brk = gb_tty->iocount.brk;
340
341 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
342 retval = -EFAULT;
343
344 return retval;
345}
346
347static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
348 unsigned long arg)
349{
350 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800351
352 switch (cmd) {
353 case TIOCGSERIAL:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700354 return get_serial_info(gb_tty,
355 (struct serial_struct __user *)arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800356 case TIOCSSERIAL:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700357 return set_serial_info(gb_tty,
358 (struct serial_struct __user *)arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800359 case TIOCMIWAIT:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700360 return wait_serial_change(gb_tty, arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800361 case TIOCGICOUNT:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700362 return get_serial_usage(gb_tty,
363 (struct serial_icounter_struct __user *)arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800364 }
365
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700366 return -ENOIOCTLCMD;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800367}
368
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800369
370static const struct tty_operations gb_ops = {
371 .install = gb_tty_install,
372 .open = gb_tty_open,
373 .close = gb_tty_close,
374 .cleanup = gb_tty_cleanup,
375 .hangup = gb_tty_hangup,
376 .write = gb_tty_write,
377 .write_room = gb_tty_write_room,
378 .ioctl = gb_tty_ioctl,
379 .throttle = gb_tty_throttle,
380 .unthrottle = gb_tty_unthrottle,
381 .chars_in_buffer = gb_tty_chars_in_buffer,
382 .break_ctl = gb_tty_break_ctl,
383 .set_termios = gb_tty_set_termios,
384 .tiocmget = gb_tty_tiocmget,
385 .tiocmset = gb_tty_tiocmset,
386};
387
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800388
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500389int gb_tty_probe(struct gb_module *gmod,
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700390 const struct greybus_module_id *id)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800391{
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800392 struct gb_tty *gb_tty;
393 struct device *tty_dev;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800394 int retval;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800395 int minor;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800396
Greg Kroah-Hartman8bf23e82014-08-30 17:18:04 -0700397 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800398 if (!gb_tty)
399 return -ENOMEM;
400
401 minor = alloc_minor(gb_tty);
Alex Elderff5f0b32014-08-18 18:25:11 -0500402 if (minor < 0) {
403 if (minor == -ENOSPC) {
Alex Elder778c69c2014-09-22 19:19:03 -0500404 dev_err(&gmod->dev, "no more free minor numbers\n");
Alex Elderff5f0b32014-08-18 18:25:11 -0500405 return -ENODEV;
406 }
407 return minor;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800408 }
409
410 gb_tty->minor = minor;
Alex Elder778c69c2014-09-22 19:19:03 -0500411 gb_tty->gmod = gmod;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800412 spin_lock_init(&gb_tty->write_lock);
413 spin_lock_init(&gb_tty->read_lock);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800414 init_waitqueue_head(&gb_tty->wioctl);
415 mutex_init(&gb_tty->mutex);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800416
417 /* FIXME - allocate gb buffers */
418
Alex Elder778c69c2014-09-22 19:19:03 -0500419 gmod->gb_tty = gb_tty;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800420
421 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
Alex Elder778c69c2014-09-22 19:19:03 -0500422 &gmod->dev);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800423 if (IS_ERR(tty_dev)) {
424 retval = PTR_ERR(tty_dev);
425 goto error;
426 }
427
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800428 return 0;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800429error:
Alex Elder778c69c2014-09-22 19:19:03 -0500430 gmod->gb_tty = NULL;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800431 release_minor(gb_tty);
432 return retval;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800433}
434
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500435void gb_tty_disconnect(struct gb_module *gmod)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800436{
Alex Elder778c69c2014-09-22 19:19:03 -0500437 struct gb_tty *gb_tty = gmod->gb_tty;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800438 struct tty_struct *tty;
439
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800440 if (!gb_tty)
441 return;
442
443 mutex_lock(&gb_tty->mutex);
444 gb_tty->disconnected = true;
445
446 wake_up_all(&gb_tty->wioctl);
Alex Elder778c69c2014-09-22 19:19:03 -0500447 gmod->gb_tty = NULL;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800448 mutex_unlock(&gb_tty->mutex);
449
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800450 tty = tty_port_tty_get(&gb_tty->port);
451 if (tty) {
452 tty_vhangup(tty);
453 tty_kref_put(tty);
454 }
455 /* FIXME - stop all traffic */
456
457 tty_unregister_device(gb_tty_driver, gb_tty->minor);
458
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800459 /* FIXME - free transmit / recieve buffers */
460
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800461 tty_port_put(&gb_tty->port);
Greg Kroah-Hartmane5f167f2014-08-30 17:11:41 -0700462
463 kfree(gb_tty);
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800464}
465
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700466#if 0
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800467static struct greybus_driver tty_gb_driver = {
Greg Kroah-Hartmandb6e1fd2014-08-30 16:47:26 -0700468 .probe = gb_tty_probe,
469 .disconnect = gb_tty_disconnect,
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800470 .id_table = id_table,
471};
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700472#endif
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800473
Greg Kroah-Hartmandb6e1fd2014-08-30 16:47:26 -0700474int __init gb_tty_init(void)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800475{
Marti Bolivar7fabc882014-09-05 23:56:10 -0400476 int retval = 0;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800477
Marti Bolivarf8089c02014-09-05 23:56:09 -0400478 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
Marti Bolivar7fabc882014-09-05 23:56:10 -0400479 if (IS_ERR(gb_tty_driver)) {
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700480 pr_err("Can not allocate tty driver\n");
Marti Bolivar7fabc882014-09-05 23:56:10 -0400481 retval = -ENOMEM;
482 goto fail_unregister_dev;
483 }
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800484
485 gb_tty_driver->driver_name = "gb";
Marti Bolivar7fabc882014-09-05 23:56:10 -0400486 gb_tty_driver->name = GB_NAME;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700487 gb_tty_driver->major = 0;
488 gb_tty_driver->minor_start = 0;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800489 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
490 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
491 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
492 gb_tty_driver->init_termios = tty_std_termios;
493 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
494 tty_set_operations(gb_tty_driver, &gb_ops);
495
496 retval = tty_register_driver(gb_tty_driver);
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700497 if (retval) {
498 pr_err("Can not register tty driver: %d\n", retval);
Marti Bolivar7fabc882014-09-05 23:56:10 -0400499 goto fail_put_gb_tty;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700500 }
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800501
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700502#if 0
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800503 retval = greybus_register(&tty_gb_driver);
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700504 if (retval) {
505 pr_err("Can not register greybus driver.\n");
Marti Bolivar7fabc882014-09-05 23:56:10 -0400506 goto fail_unregister_gb_tty;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700507 }
508#endif
Marti Bolivar7fabc882014-09-05 23:56:10 -0400509
510 return 0;
511
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700512/* fail_unregister_gb_tty: */
Marti Bolivar7fabc882014-09-05 23:56:10 -0400513 tty_unregister_driver(gb_tty_driver);
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700514fail_put_gb_tty:
Marti Bolivar7fabc882014-09-05 23:56:10 -0400515 put_tty_driver(gb_tty_driver);
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700516fail_unregister_dev:
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800517 return retval;
518}
519
Greg Kroah-Hartmandb6e1fd2014-08-30 16:47:26 -0700520void __exit gb_tty_exit(void)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800521{
Marti Bolivar7fabc882014-09-05 23:56:10 -0400522 int major = MAJOR(gb_tty_driver->major);
523 int minor = gb_tty_driver->minor_start;
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700524
525#if 0
526 greybus_deregister(&tty_gb_driver);
527#endif
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800528 tty_unregister_driver(gb_tty_driver);
529 put_tty_driver(gb_tty_driver);
Marti Bolivar7fabc882014-09-05 23:56:10 -0400530 unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800531}
532
Greg Kroah-Hartmandb6e1fd2014-08-30 16:47:26 -0700533#if 0
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800534module_init(gb_tty_init);
535module_exit(gb_tty_exit);
536MODULE_LICENSE("GPL");
537MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
Greg Kroah-Hartmandb6e1fd2014-08-30 16:47:26 -0700538#endif