2 * UART driver for the Greybus "generic" UART module.
4 * Copyright 2014 Google Inc.
6 * Released under the GPLv2 only.
8 * Heavily based on drivers/usb/class/cdc-acm.c and
9 * drivers/usb/serial/usb-serial.c.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
28 #include <linux/kdev_t.h>
33 #define GB_NUM_MINORS 255 /* 255 is enough for anyone... */
34 #define GB_NAME "ttyGB"
38 struct gb_connection *connection;
42 unsigned int throttled:1;
43 unsigned int throttle_req:1;
45 int writesize; // FIXME - set this somehow.
47 spinlock_t write_lock;
48 struct async_icount iocount;
49 struct async_icount oldcount;
50 wait_queue_head_t wioctl;
54 static const struct greybus_module_id id_table[] = {
55 { GREYBUS_DEVICE(0x45, 0x45) }, /* make shit up */
56 { }, /* terminating NULL entry */
59 static struct tty_driver *gb_tty_driver;
60 static DEFINE_IDR(tty_minors);
61 static DEFINE_MUTEX(table_lock);
62 static atomic_t reference_count = ATOMIC_INIT(0);
64 static int gb_tty_init(void);
65 static void gb_tty_exit(void);
67 static struct gb_tty *get_gb_by_minor(unsigned minor)
69 struct gb_tty *gb_tty;
71 mutex_lock(&table_lock);
72 gb_tty = idr_find(&tty_minors, minor);
74 mutex_lock(&gb_tty->mutex);
75 if (gb_tty->disconnected) {
76 mutex_unlock(&gb_tty->mutex);
79 tty_port_get(&gb_tty->port);
80 mutex_unlock(&gb_tty->mutex);
83 mutex_unlock(&table_lock);
87 static int alloc_minor(struct gb_tty *gb_tty)
91 mutex_lock(&table_lock);
92 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
93 mutex_unlock(&table_lock);
95 gb_tty->minor = minor;
99 static void release_minor(struct gb_tty *gb_tty)
101 int minor = gb_tty->minor;
103 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
104 mutex_lock(&table_lock);
105 idr_remove(&tty_minors, minor);
106 mutex_unlock(&table_lock);
109 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
111 struct gb_tty *gb_tty;
114 gb_tty = get_gb_by_minor(tty->index);
118 retval = tty_standard_install(driver, tty);
122 tty->driver_data = gb_tty;
125 tty_port_put(&gb_tty->port);
129 static int gb_tty_open(struct tty_struct *tty, struct file *file)
131 struct gb_tty *gb_tty = tty->driver_data;
133 return tty_port_open(&gb_tty->port, tty, file);
136 static void gb_tty_close(struct tty_struct *tty, struct file *file)
138 struct gb_tty *gb_tty = tty->driver_data;
140 tty_port_close(&gb_tty->port, tty, file);
143 static void gb_tty_cleanup(struct tty_struct *tty)
145 struct gb_tty *gb_tty = tty->driver_data;
147 tty_port_put(&gb_tty->port);
150 static void gb_tty_hangup(struct tty_struct *tty)
152 struct gb_tty *gb_tty = tty->driver_data;
154 tty_port_hangup(&gb_tty->port);
157 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
160 // struct gb_tty *gb_tty = tty->driver_data;
162 // FIXME - actually implement...
167 static int gb_tty_write_room(struct tty_struct *tty)
169 // struct gb_tty *gb_tty = tty->driver_data;
171 // FIXME - how much do we want to say we have room for?
175 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
177 // struct gb_tty *gb_tty = tty->driver_data;
179 // FIXME - how many left to send?
183 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
185 // struct gb_tty *gb_tty = tty->driver_data;
187 // FIXME - send a break, if asked to...
191 static void gb_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
193 // FIXME - is this it???
194 tty_termios_copy_hw(&tty->termios, old);
197 static int gb_tty_tiocmget(struct tty_struct *tty)
199 // struct gb_tty *gb_tty = tty->driver_data;
201 // FIXME - get some tiocms!
205 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
208 // struct gb_tty *gb_tty = tty->driver_data;
210 // FIXME - set some tiocms!
214 static void gb_tty_throttle(struct tty_struct *tty)
216 struct gb_tty *gb_tty = tty->driver_data;
218 spin_lock_irq(&gb_tty->read_lock);
219 gb_tty->throttle_req = 1;
220 spin_unlock_irq(&gb_tty->read_lock);
223 static void gb_tty_unthrottle(struct tty_struct *tty)
225 struct gb_tty *gb_tty = tty->driver_data;
226 unsigned int was_throttled;
228 spin_lock_irq(&gb_tty->read_lock);
229 was_throttled = gb_tty->throttled;
230 gb_tty->throttle_req = 0;
231 gb_tty->throttled = 0;
232 spin_unlock_irq(&gb_tty->read_lock);
235 // FIXME - send more data
239 static int get_serial_info(struct gb_tty *gb_tty,
240 struct serial_struct __user *info)
242 struct serial_struct tmp;
247 memset(&tmp, 0, sizeof(tmp));
248 tmp.flags = ASYNC_LOW_LATENCY;
249 tmp.xmit_fifo_size = gb_tty->writesize;
250 tmp.baud_base = 0; // FIXME
251 tmp.close_delay = gb_tty->port.close_delay / 10;
252 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
253 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
255 if (copy_to_user(info, &tmp, sizeof(tmp)))
260 static int set_serial_info(struct gb_tty *gb_tty,
261 struct serial_struct __user *newinfo)
263 struct serial_struct new_serial;
264 unsigned int closing_wait;
265 unsigned int close_delay;
268 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
271 close_delay = new_serial.close_delay * 10;
272 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
273 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
275 mutex_lock(&gb_tty->port.mutex);
276 if (!capable(CAP_SYS_ADMIN)) {
277 if ((close_delay != gb_tty->port.close_delay) ||
278 (closing_wait != gb_tty->port.closing_wait))
281 retval = -EOPNOTSUPP;
283 gb_tty->port.close_delay = close_delay;
284 gb_tty->port.closing_wait = closing_wait;
286 mutex_unlock(&gb_tty->port.mutex);
290 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
293 DECLARE_WAITQUEUE(wait, current);
294 struct async_icount old;
295 struct async_icount new;
297 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
301 spin_lock_irq(&gb_tty->read_lock);
302 old = gb_tty->oldcount;
303 new = gb_tty->iocount;
304 gb_tty->oldcount = new;
305 spin_unlock_irq(&gb_tty->read_lock);
307 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
309 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
311 if ((arg & TIOCM_RI) && (old.rng != new.rng))
314 add_wait_queue(&gb_tty->wioctl, &wait);
315 set_current_state(TASK_INTERRUPTIBLE);
317 remove_wait_queue(&gb_tty->wioctl, &wait);
318 if (gb_tty->disconnected) {
322 } else if (signal_pending(current)) {
323 retval = -ERESTARTSYS;
330 static int get_serial_usage(struct gb_tty *gb_tty,
331 struct serial_icounter_struct __user *count)
333 struct serial_icounter_struct icount;
336 memset(&icount, 0, sizeof(icount));
337 icount.dsr = gb_tty->iocount.dsr;
338 icount.rng = gb_tty->iocount.rng;
339 icount.dcd = gb_tty->iocount.dcd;
340 icount.frame = gb_tty->iocount.frame;
341 icount.overrun = gb_tty->iocount.overrun;
342 icount.parity = gb_tty->iocount.parity;
343 icount.brk = gb_tty->iocount.brk;
345 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
351 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
354 struct gb_tty *gb_tty = tty->driver_data;
358 return get_serial_info(gb_tty,
359 (struct serial_struct __user *)arg);
361 return set_serial_info(gb_tty,
362 (struct serial_struct __user *)arg);
364 return wait_serial_change(gb_tty, arg);
366 return get_serial_usage(gb_tty,
367 (struct serial_icounter_struct __user *)arg);
374 static const struct tty_operations gb_ops = {
375 .install = gb_tty_install,
377 .close = gb_tty_close,
378 .cleanup = gb_tty_cleanup,
379 .hangup = gb_tty_hangup,
380 .write = gb_tty_write,
381 .write_room = gb_tty_write_room,
382 .ioctl = gb_tty_ioctl,
383 .throttle = gb_tty_throttle,
384 .unthrottle = gb_tty_unthrottle,
385 .chars_in_buffer = gb_tty_chars_in_buffer,
386 .break_ctl = gb_tty_break_ctl,
387 .set_termios = gb_tty_set_termios,
388 .tiocmget = gb_tty_tiocmget,
389 .tiocmset = gb_tty_tiocmset,
393 static int gb_uart_connection_init(struct gb_connection *connection)
395 struct gb_tty *gb_tty;
396 struct device *tty_dev;
400 /* First time here, initialize the tty structures */
401 if (atomic_inc_return(&reference_count) == 1) {
402 retval = gb_tty_init();
404 atomic_dec(&reference_count);
409 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
413 minor = alloc_minor(gb_tty);
415 if (minor == -ENOSPC) {
416 dev_err(&connection->dev, "no more free minor numbers\n");
422 gb_tty->minor = minor;
423 gb_tty->connection = connection;
424 spin_lock_init(&gb_tty->write_lock);
425 spin_lock_init(&gb_tty->read_lock);
426 init_waitqueue_head(&gb_tty->wioctl);
427 mutex_init(&gb_tty->mutex);
429 /* FIXME - allocate gb buffers */
431 connection->private = gb_tty;
433 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
435 if (IS_ERR(tty_dev)) {
436 retval = PTR_ERR(tty_dev);
442 connection->private = NULL;
443 release_minor(gb_tty);
447 static void gb_uart_connection_exit(struct gb_connection *connection)
449 struct gb_tty *gb_tty = connection->private;
450 struct tty_struct *tty;
455 mutex_lock(&gb_tty->mutex);
456 gb_tty->disconnected = true;
458 wake_up_all(&gb_tty->wioctl);
459 connection->private = NULL;
460 mutex_unlock(&gb_tty->mutex);
462 tty = tty_port_tty_get(&gb_tty->port);
467 /* FIXME - stop all traffic */
469 tty_unregister_device(gb_tty_driver, gb_tty->minor);
471 /* FIXME - free transmit / recieve buffers */
473 tty_port_put(&gb_tty->port);
477 /* If last device is gone, tear down the tty structures */
478 if (atomic_dec_return(&reference_count) == 0)
482 static int gb_tty_init(void)
486 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
487 if (IS_ERR(gb_tty_driver)) {
488 pr_err("Can not allocate tty driver\n");
490 goto fail_unregister_dev;
493 gb_tty_driver->driver_name = "gb";
494 gb_tty_driver->name = GB_NAME;
495 gb_tty_driver->major = 0;
496 gb_tty_driver->minor_start = 0;
497 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
498 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
499 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
500 gb_tty_driver->init_termios = tty_std_termios;
501 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
502 tty_set_operations(gb_tty_driver, &gb_ops);
504 retval = tty_register_driver(gb_tty_driver);
506 pr_err("Can not register tty driver: %d\n", retval);
507 goto fail_put_gb_tty;
513 put_tty_driver(gb_tty_driver);
518 static void gb_tty_exit(void)
520 int major = MAJOR(gb_tty_driver->major);
521 int minor = gb_tty_driver->minor_start;
523 tty_unregister_driver(gb_tty_driver);
524 put_tty_driver(gb_tty_driver);
525 unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
528 struct gb_connection_handler gb_uart_connection_handler = {
529 .connection_init = gb_uart_connection_init,
530 .connection_exit = gb_uart_connection_exit,