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_module *gmod;
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);
63 static struct gb_tty *get_gb_by_minor(unsigned minor)
65 struct gb_tty *gb_tty;
67 mutex_lock(&table_lock);
68 gb_tty = idr_find(&tty_minors, minor);
70 mutex_lock(&gb_tty->mutex);
71 if (gb_tty->disconnected) {
72 mutex_unlock(&gb_tty->mutex);
75 tty_port_get(&gb_tty->port);
76 mutex_unlock(&gb_tty->mutex);
79 mutex_unlock(&table_lock);
83 static int alloc_minor(struct gb_tty *gb_tty)
87 mutex_lock(&table_lock);
88 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
89 mutex_unlock(&table_lock);
91 gb_tty->minor = minor;
95 static void release_minor(struct gb_tty *gb_tty)
97 int minor = gb_tty->minor;
99 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
100 mutex_lock(&table_lock);
101 idr_remove(&tty_minors, minor);
102 mutex_unlock(&table_lock);
105 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
107 struct gb_tty *gb_tty;
110 gb_tty = get_gb_by_minor(tty->index);
114 retval = tty_standard_install(driver, tty);
118 tty->driver_data = gb_tty;
121 tty_port_put(&gb_tty->port);
125 static int gb_tty_open(struct tty_struct *tty, struct file *file)
127 struct gb_tty *gb_tty = tty->driver_data;
129 return tty_port_open(&gb_tty->port, tty, file);
132 static void gb_tty_close(struct tty_struct *tty, struct file *file)
134 struct gb_tty *gb_tty = tty->driver_data;
136 tty_port_close(&gb_tty->port, tty, file);
139 static void gb_tty_cleanup(struct tty_struct *tty)
141 struct gb_tty *gb_tty = tty->driver_data;
143 tty_port_put(&gb_tty->port);
146 static void gb_tty_hangup(struct tty_struct *tty)
148 struct gb_tty *gb_tty = tty->driver_data;
150 tty_port_hangup(&gb_tty->port);
153 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
156 // struct gb_tty *gb_tty = tty->driver_data;
158 // FIXME - actually implement...
163 static int gb_tty_write_room(struct tty_struct *tty)
165 // struct gb_tty *gb_tty = tty->driver_data;
167 // FIXME - how much do we want to say we have room for?
171 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
173 // struct gb_tty *gb_tty = tty->driver_data;
175 // FIXME - how many left to send?
179 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
181 // struct gb_tty *gb_tty = tty->driver_data;
183 // FIXME - send a break, if asked to...
187 static void gb_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
189 // FIXME - is this it???
190 tty_termios_copy_hw(&tty->termios, old);
193 static int gb_tty_tiocmget(struct tty_struct *tty)
195 // struct gb_tty *gb_tty = tty->driver_data;
197 // FIXME - get some tiocms!
201 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
204 // struct gb_tty *gb_tty = tty->driver_data;
206 // FIXME - set some tiocms!
210 static void gb_tty_throttle(struct tty_struct *tty)
212 struct gb_tty *gb_tty = tty->driver_data;
214 spin_lock_irq(&gb_tty->read_lock);
215 gb_tty->throttle_req = 1;
216 spin_unlock_irq(&gb_tty->read_lock);
219 static void gb_tty_unthrottle(struct tty_struct *tty)
221 struct gb_tty *gb_tty = tty->driver_data;
222 unsigned int was_throttled;
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);
231 // FIXME - send more data
235 static int get_serial_info(struct gb_tty *gb_tty,
236 struct serial_struct __user *info)
238 struct serial_struct tmp;
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;
251 if (copy_to_user(info, &tmp, sizeof(tmp)))
256 static int set_serial_info(struct gb_tty *gb_tty,
257 struct serial_struct __user *newinfo)
259 struct serial_struct new_serial;
260 unsigned int closing_wait;
261 unsigned int close_delay;
264 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
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;
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))
277 retval = -EOPNOTSUPP;
279 gb_tty->port.close_delay = close_delay;
280 gb_tty->port.closing_wait = closing_wait;
282 mutex_unlock(&gb_tty->port.mutex);
286 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
289 DECLARE_WAITQUEUE(wait, current);
290 struct async_icount old;
291 struct async_icount new;
293 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
297 spin_lock_irq(&gb_tty->read_lock);
298 old = gb_tty->oldcount;
299 new = gb_tty->iocount;
300 gb_tty->oldcount = new;
301 spin_unlock_irq(&gb_tty->read_lock);
303 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
305 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
307 if ((arg & TIOCM_RI) && (old.rng != new.rng))
310 add_wait_queue(&gb_tty->wioctl, &wait);
311 set_current_state(TASK_INTERRUPTIBLE);
313 remove_wait_queue(&gb_tty->wioctl, &wait);
314 if (gb_tty->disconnected) {
318 } else if (signal_pending(current)) {
319 retval = -ERESTARTSYS;
326 static int get_serial_usage(struct gb_tty *gb_tty,
327 struct serial_icounter_struct __user *count)
329 struct serial_icounter_struct icount;
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;
341 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
347 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
350 struct gb_tty *gb_tty = tty->driver_data;
354 return get_serial_info(gb_tty,
355 (struct serial_struct __user *)arg);
357 return set_serial_info(gb_tty,
358 (struct serial_struct __user *)arg);
360 return wait_serial_change(gb_tty, arg);
362 return get_serial_usage(gb_tty,
363 (struct serial_icounter_struct __user *)arg);
370 static const struct tty_operations gb_ops = {
371 .install = gb_tty_install,
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,
389 int gb_tty_probe(struct gb_module *gmod,
390 const struct greybus_module_id *id)
392 struct gb_tty *gb_tty;
393 struct device *tty_dev;
397 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
401 minor = alloc_minor(gb_tty);
403 if (minor == -ENOSPC) {
404 dev_err(&gmod->dev, "no more free minor numbers\n");
410 gb_tty->minor = minor;
412 spin_lock_init(&gb_tty->write_lock);
413 spin_lock_init(&gb_tty->read_lock);
414 init_waitqueue_head(&gb_tty->wioctl);
415 mutex_init(&gb_tty->mutex);
417 /* FIXME - allocate gb buffers */
419 gmod->gb_tty = gb_tty;
421 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
423 if (IS_ERR(tty_dev)) {
424 retval = PTR_ERR(tty_dev);
431 release_minor(gb_tty);
435 void gb_tty_disconnect(struct gb_module *gmod)
437 struct gb_tty *gb_tty = gmod->gb_tty;
438 struct tty_struct *tty;
443 mutex_lock(&gb_tty->mutex);
444 gb_tty->disconnected = true;
446 wake_up_all(&gb_tty->wioctl);
448 mutex_unlock(&gb_tty->mutex);
450 tty = tty_port_tty_get(&gb_tty->port);
455 /* FIXME - stop all traffic */
457 tty_unregister_device(gb_tty_driver, gb_tty->minor);
459 /* FIXME - free transmit / recieve buffers */
461 tty_port_put(&gb_tty->port);
467 static struct greybus_driver tty_gb_driver = {
468 .probe = gb_tty_probe,
469 .disconnect = gb_tty_disconnect,
470 .id_table = id_table,
474 int __init gb_tty_init(void)
478 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
479 if (IS_ERR(gb_tty_driver)) {
480 pr_err("Can not allocate tty driver\n");
482 goto fail_unregister_dev;
485 gb_tty_driver->driver_name = "gb";
486 gb_tty_driver->name = GB_NAME;
487 gb_tty_driver->major = 0;
488 gb_tty_driver->minor_start = 0;
489 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);
496 retval = tty_register_driver(gb_tty_driver);
498 pr_err("Can not register tty driver: %d\n", retval);
499 goto fail_put_gb_tty;
503 retval = greybus_register(&tty_gb_driver);
505 pr_err("Can not register greybus driver.\n");
506 goto fail_unregister_gb_tty;
512 /* fail_unregister_gb_tty: */
513 tty_unregister_driver(gb_tty_driver);
515 put_tty_driver(gb_tty_driver);
520 void __exit gb_tty_exit(void)
522 int major = MAJOR(gb_tty_driver->major);
523 int minor = gb_tty_driver->minor_start;
526 greybus_deregister(&tty_gb_driver);
528 tty_unregister_driver(gb_tty_driver);
529 put_tty_driver(gb_tty_driver);
530 unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
534 module_init(gb_tty_init);
535 module_exit(gb_tty_exit);
536 MODULE_LICENSE("GPL");
537 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");