2 * interface to user space for the gigaset driver
4 * Copyright (c) 2004 by Hansjoerg Lipp <hjlipp@web.de>
6 * =====================================================================
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 * =====================================================================
15 #include <linux/gigaset_dev.h>
16 #include <linux/tty_flip.h>
17 #include <linux/module.h>
21 static int if_lock(struct cardstate *cs, int *arg)
25 gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
31 *arg = cs->mstate == MS_LOCKED;
35 if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {
36 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
37 cs->ops->baud_rate(cs, B115200);
38 cs->ops->set_line_ctrl(cs, CS8);
39 cs->control_state = TIOCM_DTR|TIOCM_RTS;
43 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
48 gigaset_schedule_event(cs);
50 wait_event(cs->waitqueue, !cs->waiting);
52 if (cs->cmd_result >= 0) {
53 *arg = cs->cmd_result;
57 return cs->cmd_result;
60 static int if_version(struct cardstate *cs, unsigned arg[4])
62 static const unsigned version[4] = GIG_VERSION;
63 static const unsigned compat[4] = GIG_COMPAT;
64 unsigned cmd = arg[0];
66 gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
70 memcpy(arg, version, sizeof version);
73 memcpy(arg, compat, sizeof compat);
77 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
82 gigaset_schedule_event(cs);
84 wait_event(cs->waitqueue, !cs->waiting);
86 if (cs->cmd_result >= 0)
89 return cs->cmd_result;
95 static int if_config(struct cardstate *cs, int *arg)
97 gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
102 if (cs->mstate != MS_LOCKED)
105 if (!cs->connected) {
106 pr_err("%s: not connected\n", __func__);
111 return gigaset_enterconfigmode(cs);
114 /*** the terminal driver ***/
115 /* stolen from usbserial and some other tty drivers */
117 static int if_open(struct tty_struct *tty, struct file *filp);
118 static void if_close(struct tty_struct *tty, struct file *filp);
119 static int if_ioctl(struct tty_struct *tty,
120 unsigned int cmd, unsigned long arg);
121 static int if_write_room(struct tty_struct *tty);
122 static int if_chars_in_buffer(struct tty_struct *tty);
123 static void if_throttle(struct tty_struct *tty);
124 static void if_unthrottle(struct tty_struct *tty);
125 static void if_set_termios(struct tty_struct *tty, struct ktermios *old);
126 static int if_tiocmget(struct tty_struct *tty);
127 static int if_tiocmset(struct tty_struct *tty,
128 unsigned int set, unsigned int clear);
129 static int if_write(struct tty_struct *tty,
130 const unsigned char *buf, int count);
132 static const struct tty_operations if_ops = {
137 .write_room = if_write_room,
138 .chars_in_buffer = if_chars_in_buffer,
139 .set_termios = if_set_termios,
140 .throttle = if_throttle,
141 .unthrottle = if_unthrottle,
142 .tiocmget = if_tiocmget,
143 .tiocmset = if_tiocmset,
146 static int if_open(struct tty_struct *tty, struct file *filp)
148 struct cardstate *cs;
151 gig_dbg(DEBUG_IF, "%d+%d: %s()",
152 tty->driver->minor_start, tty->index, __func__);
154 cs = gigaset_get_cs_by_tty(tty);
155 if (!cs || !try_module_get(cs->driver->owner))
158 if (mutex_lock_interruptible(&cs->mutex)) {
159 module_put(cs->driver->owner);
162 tty->driver_data = cs;
166 if (cs->open_count == 1) {
167 spin_lock_irqsave(&cs->lock, flags);
169 spin_unlock_irqrestore(&cs->lock, flags);
170 tty->low_latency = 1;
173 mutex_unlock(&cs->mutex);
177 static void if_close(struct tty_struct *tty, struct file *filp)
179 struct cardstate *cs = tty->driver_data;
182 if (!cs) { /* happens if we didn't find cs in open */
183 printk(KERN_DEBUG "%s: no cardstate\n", __func__);
187 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
189 mutex_lock(&cs->mutex);
192 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
193 else if (!cs->open_count)
194 dev_warn(cs->dev, "%s: device not opened\n", __func__);
196 if (!--cs->open_count) {
197 spin_lock_irqsave(&cs->lock, flags);
199 spin_unlock_irqrestore(&cs->lock, flags);
203 mutex_unlock(&cs->mutex);
205 module_put(cs->driver->owner);
208 static int if_ioctl(struct tty_struct *tty,
209 unsigned int cmd, unsigned long arg)
211 struct cardstate *cs = tty->driver_data;
212 int retval = -ENODEV;
214 unsigned char buf[6];
217 gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
219 if (mutex_lock_interruptible(&cs->mutex))
222 if (!cs->connected) {
223 gig_dbg(DEBUG_IF, "not connected");
229 retval = get_user(int_arg, (int __user *) arg);
231 retval = if_lock(cs, &int_arg);
233 retval = put_user(int_arg, (int __user *) arg);
236 retval = get_user(int_arg, (int __user *) arg);
238 retval = if_config(cs, &int_arg);
240 retval = put_user(int_arg, (int __user *) arg);
242 case GIGASET_BRKCHARS:
243 retval = copy_from_user(&buf,
244 (const unsigned char __user *) arg, 6)
247 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
248 6, (const unsigned char *) arg);
249 retval = cs->ops->brkchars(cs, buf);
252 case GIGASET_VERSION:
253 retval = copy_from_user(version,
254 (unsigned __user *) arg, sizeof version)
257 retval = if_version(cs, version);
259 retval = copy_to_user((unsigned __user *) arg,
260 version, sizeof version)
264 gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",
266 retval = -ENOIOCTLCMD;
270 mutex_unlock(&cs->mutex);
275 static int if_tiocmget(struct tty_struct *tty)
277 struct cardstate *cs = tty->driver_data;
280 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
282 if (mutex_lock_interruptible(&cs->mutex))
285 retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);
287 mutex_unlock(&cs->mutex);
292 static int if_tiocmset(struct tty_struct *tty,
293 unsigned int set, unsigned int clear)
295 struct cardstate *cs = tty->driver_data;
299 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
300 cs->minor_index, __func__, set, clear);
302 if (mutex_lock_interruptible(&cs->mutex))
305 if (!cs->connected) {
306 gig_dbg(DEBUG_IF, "not connected");
309 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);
310 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
311 cs->control_state = mc;
314 mutex_unlock(&cs->mutex);
319 static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
321 struct cardstate *cs = tty->driver_data;
325 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
327 if (mutex_lock_interruptible(&cs->mutex))
330 if (!cs->connected) {
331 gig_dbg(DEBUG_IF, "not connected");
335 if (cs->mstate != MS_LOCKED) {
336 dev_warn(cs->dev, "can't write to unlocked device\n");
346 cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
348 dev_err(cs->dev, "%s: out of memory\n", __func__);
353 memcpy(cb->buf, buf, count);
357 cb->wake_tasklet = &cs->if_wake_tasklet;
358 retval = cs->ops->write_cmd(cs, cb);
360 mutex_unlock(&cs->mutex);
364 static int if_write_room(struct tty_struct *tty)
366 struct cardstate *cs = tty->driver_data;
367 int retval = -ENODEV;
369 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
371 if (mutex_lock_interruptible(&cs->mutex))
374 if (!cs->connected) {
375 gig_dbg(DEBUG_IF, "not connected");
377 } else if (cs->mstate != MS_LOCKED) {
378 dev_warn(cs->dev, "can't write to unlocked device\n");
381 retval = cs->ops->write_room(cs);
383 mutex_unlock(&cs->mutex);
388 static int if_chars_in_buffer(struct tty_struct *tty)
390 struct cardstate *cs = tty->driver_data;
393 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
395 mutex_lock(&cs->mutex);
398 gig_dbg(DEBUG_IF, "not connected");
399 else if (cs->mstate != MS_LOCKED)
400 dev_warn(cs->dev, "can't write to unlocked device\n");
402 retval = cs->ops->chars_in_buffer(cs);
404 mutex_unlock(&cs->mutex);
409 static void if_throttle(struct tty_struct *tty)
411 struct cardstate *cs = tty->driver_data;
413 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
415 mutex_lock(&cs->mutex);
418 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
420 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
422 mutex_unlock(&cs->mutex);
425 static void if_unthrottle(struct tty_struct *tty)
427 struct cardstate *cs = tty->driver_data;
429 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
431 mutex_lock(&cs->mutex);
434 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
436 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
438 mutex_unlock(&cs->mutex);
441 static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
443 struct cardstate *cs = tty->driver_data;
446 unsigned int old_cflag;
447 unsigned int control_state, new_state;
449 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
451 mutex_lock(&cs->mutex);
453 if (!cs->connected) {
454 gig_dbg(DEBUG_IF, "not connected");
458 iflag = tty->termios->c_iflag;
459 cflag = tty->termios->c_cflag;
460 old_cflag = old ? old->c_cflag : cflag;
461 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
462 cs->minor_index, iflag, cflag, old_cflag);
464 /* get a local copy of the current port settings */
465 control_state = cs->control_state;
469 * Do not attempt to cache old rates and skip settings,
470 * disconnects screw such tricks up completely.
471 * Premature optimization is the root of all evil.
474 /* reassert DTR and (maybe) RTS on transition from B0 */
475 if ((old_cflag & CBAUD) == B0) {
476 new_state = control_state | TIOCM_DTR;
477 /* don't set RTS if using hardware flow control */
478 if (!(old_cflag & CRTSCTS))
479 new_state |= TIOCM_RTS;
480 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
482 (new_state & TIOCM_RTS) ? " only" : "/RTS");
483 cs->ops->set_modem_ctrl(cs, control_state, new_state);
484 control_state = new_state;
487 cs->ops->baud_rate(cs, cflag & CBAUD);
489 if ((cflag & CBAUD) == B0) {
490 /* Drop RTS and DTR */
491 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
492 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
493 cs->ops->set_modem_ctrl(cs, control_state, new_state);
494 control_state = new_state;
498 * Update line control register (LCR)
501 cs->ops->set_line_ctrl(cs, cflag);
503 /* save off the modified port settings */
504 cs->control_state = control_state;
507 mutex_unlock(&cs->mutex);
511 /* wakeup tasklet for the write operation */
512 static void if_wake(unsigned long data)
514 struct cardstate *cs = (struct cardstate *) data;
520 /*** interface to common ***/
522 void gigaset_if_init(struct cardstate *cs)
524 struct gigaset_driver *drv;
530 tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
532 mutex_lock(&cs->mutex);
533 cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL);
535 if (!IS_ERR(cs->tty_dev))
536 dev_set_drvdata(cs->tty_dev, cs);
538 pr_warning("could not register device to the tty subsystem\n");
541 mutex_unlock(&cs->mutex);
544 void gigaset_if_free(struct cardstate *cs)
546 struct gigaset_driver *drv;
552 tasklet_disable(&cs->if_wake_tasklet);
553 tasklet_kill(&cs->if_wake_tasklet);
555 tty_unregister_device(drv->tty, cs->minor_index);
559 * gigaset_if_receive() - pass a received block of data to the tty device
560 * @cs: device descriptor structure.
561 * @buffer: received data.
562 * @len: number of bytes received.
564 * Called by asyncdata/isocdata if a block of data received from the
565 * device must be sent to userspace through the ttyG* device.
567 void gigaset_if_receive(struct cardstate *cs,
568 unsigned char *buffer, size_t len)
571 struct tty_struct *tty;
573 spin_lock_irqsave(&cs->lock, flags);
576 gig_dbg(DEBUG_IF, "receive on closed device");
578 tty_insert_flip_string(tty, buffer, len);
579 tty_flip_buffer_push(tty);
581 spin_unlock_irqrestore(&cs->lock, flags);
583 EXPORT_SYMBOL_GPL(gigaset_if_receive);
585 /* gigaset_if_initdriver
586 * Initialize tty interface.
589 * procname Name of the driver (e.g. for /proc/tty/drivers)
590 * devname Name of the device files (prefix without minor number)
592 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
596 struct tty_driver *tty;
600 drv->tty = tty = alloc_tty_driver(drv->minors);
604 tty->type = TTY_DRIVER_TYPE_SERIAL,
605 tty->subtype = SERIAL_TYPE_NORMAL,
606 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
608 tty->driver_name = procname;
610 tty->minor_start = drv->minor;
612 tty->init_termios = tty_std_termios;
613 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
614 tty_set_operations(tty, &if_ops);
616 ret = tty_register_driver(tty);
618 pr_err("error %d registering tty driver\n", ret);
621 gig_dbg(DEBUG_IF, "tty driver initialized");
626 pr_err("out of memory\n");
629 put_tty_driver(drv->tty);
632 void gigaset_if_freedriver(struct gigaset_driver *drv)
638 tty_unregister_driver(drv->tty);
639 put_tty_driver(drv->tty);