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.h>
17 #include <linux/tty_flip.h>
21 static int if_lock(struct cardstate *cs, int *arg)
25 dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
31 *arg = atomic_read(&cs->mstate) == MS_LOCKED; //FIXME remove?
35 if (!cmd && atomic_read(&cs->mstate) == MS_LOCKED
36 && atomic_read(&cs->connected)) {
37 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
38 cs->ops->baud_rate(cs, B115200);
39 cs->ops->set_line_ctrl(cs, CS8);
40 cs->control_state = TIOCM_DTR|TIOCM_RTS;
44 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
50 dbg(DEBUG_CMD, "scheduling IF_LOCK");
51 gigaset_schedule_event(cs);
53 wait_event(cs->waitqueue, !cs->waiting);
55 if (cs->cmd_result >= 0) {
56 *arg = cs->cmd_result;
60 return cs->cmd_result;
63 static int if_version(struct cardstate *cs, unsigned arg[4])
65 static const unsigned version[4] = GIG_VERSION;
66 static const unsigned compat[4] = GIG_COMPAT;
67 unsigned cmd = arg[0];
69 dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
73 memcpy(arg, version, sizeof version);
76 memcpy(arg, compat, sizeof compat);
80 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
86 dbg(DEBUG_CMD, "scheduling IF_VER");
87 gigaset_schedule_event(cs);
89 wait_event(cs->waitqueue, !cs->waiting);
91 if (cs->cmd_result >= 0)
94 return cs->cmd_result;
100 static int if_config(struct cardstate *cs, int *arg)
102 dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
107 if (atomic_read(&cs->mstate) != MS_LOCKED)
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, struct file *file,
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 termios *old);
126 static int if_tiocmget(struct tty_struct *tty, struct file *file);
127 static int if_tiocmset(struct tty_struct *tty, struct file *file,
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 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,
143 .break_ctl = serial_break,
145 .tiocmget = if_tiocmget,
146 .tiocmset = if_tiocmset,
149 static int if_open(struct tty_struct *tty, struct file *filp)
151 struct cardstate *cs;
154 dbg(DEBUG_IF, "%d+%d: %s()", tty->driver->minor_start, tty->index,
157 tty->driver_data = NULL;
159 cs = gigaset_get_cs_by_tty(tty);
163 if (down_interruptible(&cs->sem))
164 return -ERESTARTSYS; // FIXME -EINTR?
165 tty->driver_data = cs;
169 if (cs->open_count == 1) {
170 spin_lock_irqsave(&cs->lock, flags);
172 spin_unlock_irqrestore(&cs->lock, flags);
173 tty->low_latency = 1; //FIXME test
180 static void if_close(struct tty_struct *tty, struct file *filp)
182 struct cardstate *cs;
185 cs = (struct cardstate *) tty->driver_data;
187 err("cs==NULL in %s", __FUNCTION__);
191 dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __FUNCTION__);
196 warn("%s: device not opened", __FUNCTION__);
198 if (!--cs->open_count) {
199 spin_lock_irqsave(&cs->lock, flags);
201 spin_unlock_irqrestore(&cs->lock, flags);
208 static int if_ioctl(struct tty_struct *tty, struct file *file,
209 unsigned int cmd, unsigned long arg)
211 struct cardstate *cs;
212 int retval = -ENODEV;
214 unsigned char buf[6];
217 cs = (struct cardstate *) tty->driver_data;
219 err("cs==NULL in %s", __FUNCTION__);
223 dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __FUNCTION__, cmd);
225 if (down_interruptible(&cs->sem))
226 return -ERESTARTSYS; // FIXME -EINTR?
229 warn("%s: device not opened", __FUNCTION__);
234 retval = get_user(int_arg, (int __user *) arg);
236 retval = if_lock(cs, &int_arg);
238 retval = put_user(int_arg, (int __user *) arg);
241 retval = get_user(int_arg, (int __user *) arg);
243 retval = if_config(cs, &int_arg);
245 retval = put_user(int_arg, (int __user *) arg);
247 case GIGASET_BRKCHARS:
248 //FIXME test if MS_LOCKED
249 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
250 6, (const unsigned char *) arg, 1);
251 if (!atomic_read(&cs->connected)) {
253 "can't communicate with unplugged device");
257 retval = copy_from_user(&buf,
258 (const unsigned char __user *) arg, 6)
261 retval = cs->ops->brkchars(cs, buf);
263 case GIGASET_VERSION:
264 retval = copy_from_user(version,
265 (unsigned __user *) arg,
266 sizeof version) ? -EFAULT : 0;
268 retval = if_version(cs, version);
270 retval = copy_to_user((unsigned __user *) arg,
271 version, sizeof version)
275 dbg(DEBUG_ANY, "%s: arg not supported - 0x%04x",
277 retval = -ENOIOCTLCMD;
286 static int if_tiocmget(struct tty_struct *tty, struct file *file)
288 struct cardstate *cs;
291 cs = (struct cardstate *) tty->driver_data;
293 err("cs==NULL in %s", __FUNCTION__);
297 dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __FUNCTION__);
299 if (down_interruptible(&cs->sem))
300 return -ERESTARTSYS; // FIXME -EINTR?
302 // FIXME read from device?
303 retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);
310 static int if_tiocmset(struct tty_struct *tty, struct file *file,
311 unsigned int set, unsigned int clear)
313 struct cardstate *cs;
317 cs = (struct cardstate *) tty->driver_data;
319 err("cs==NULL in %s", __FUNCTION__);
324 "%u: %s(0x%x, 0x%x)", cs->minor_index, __FUNCTION__, set, clear);
326 if (down_interruptible(&cs->sem))
327 return -ERESTARTSYS; // FIXME -EINTR?
329 if (!atomic_read(&cs->connected)) {
330 dbg(DEBUG_ANY, "can't communicate with unplugged device");
333 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);
334 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
335 cs->control_state = mc;
343 static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
345 struct cardstate *cs;
346 int retval = -ENODEV;
348 cs = (struct cardstate *) tty->driver_data;
350 err("cs==NULL in %s", __FUNCTION__);
354 dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __FUNCTION__);
356 if (down_interruptible(&cs->sem))
357 return -ERESTARTSYS; // FIXME -EINTR?
360 warn("%s: device not opened", __FUNCTION__);
361 else if (atomic_read(&cs->mstate) != MS_LOCKED) {
362 warn("can't write to unlocked device");
364 } else if (!atomic_read(&cs->connected)) {
365 dbg(DEBUG_ANY, "can't write to unplugged device");
366 retval = -EBUSY; //FIXME
368 retval = cs->ops->write_cmd(cs, buf, count,
369 &cs->if_wake_tasklet);
377 static int if_write_room(struct tty_struct *tty)
379 struct cardstate *cs;
380 int retval = -ENODEV;
382 cs = (struct cardstate *) tty->driver_data;
384 err("cs==NULL in %s", __FUNCTION__);
388 dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __FUNCTION__);
390 if (down_interruptible(&cs->sem))
391 return -ERESTARTSYS; // FIXME -EINTR?
394 warn("%s: device not opened", __FUNCTION__);
395 else if (atomic_read(&cs->mstate) != MS_LOCKED) {
396 warn("can't write to unlocked device");
397 retval = -EBUSY; //FIXME
398 } else if (!atomic_read(&cs->connected)) {
399 dbg(DEBUG_ANY, "can't write to unplugged device");
400 retval = -EBUSY; //FIXME
402 retval = cs->ops->write_room(cs);
409 static int if_chars_in_buffer(struct tty_struct *tty)
411 struct cardstate *cs;
412 int retval = -ENODEV;
414 cs = (struct cardstate *) tty->driver_data;
416 err("cs==NULL in %s", __FUNCTION__);
420 dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __FUNCTION__);
422 if (down_interruptible(&cs->sem))
423 return -ERESTARTSYS; // FIXME -EINTR?
426 warn("%s: device not opened", __FUNCTION__);
427 else if (atomic_read(&cs->mstate) != MS_LOCKED) {
428 warn("can't write to unlocked device");
430 } else if (!atomic_read(&cs->connected)) {
431 dbg(DEBUG_ANY, "can't write to unplugged device");
432 retval = -EBUSY; //FIXME
434 retval = cs->ops->chars_in_buffer(cs);
441 static void if_throttle(struct tty_struct *tty)
443 struct cardstate *cs;
445 cs = (struct cardstate *) tty->driver_data;
447 err("cs==NULL in %s", __FUNCTION__);
451 dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __FUNCTION__);
456 warn("%s: device not opened", __FUNCTION__);
464 static void if_unthrottle(struct tty_struct *tty)
466 struct cardstate *cs;
468 cs = (struct cardstate *) tty->driver_data;
470 err("cs==NULL in %s", __FUNCTION__);
474 dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __FUNCTION__);
479 warn("%s: device not opened", __FUNCTION__);
487 static void if_set_termios(struct tty_struct *tty, struct termios *old)
489 struct cardstate *cs;
492 unsigned int old_cflag;
493 unsigned int control_state, new_state;
495 cs = (struct cardstate *) tty->driver_data;
497 err("cs==NULL in %s", __FUNCTION__);
501 dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __FUNCTION__);
505 if (!cs->open_count) {
506 warn("%s: device not opened", __FUNCTION__);
510 if (!atomic_read(&cs->connected)) {
511 dbg(DEBUG_ANY, "can't communicate with unplugged device");
515 // stolen from mct_u232.c
516 iflag = tty->termios->c_iflag;
517 cflag = tty->termios->c_cflag;
518 old_cflag = old ? old->c_cflag : cflag; //FIXME?
519 dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x", cs->minor_index,
520 iflag, cflag, old_cflag);
522 /* get a local copy of the current port settings */
523 control_state = cs->control_state;
527 * Do not attempt to cache old rates and skip settings,
528 * disconnects screw such tricks up completely.
529 * Premature optimization is the root of all evil.
532 /* reassert DTR and (maybe) RTS on transition from B0 */
533 if ((old_cflag & CBAUD) == B0) {
534 new_state = control_state | TIOCM_DTR;
535 /* don't set RTS if using hardware flow control */
536 if (!(old_cflag & CRTSCTS))
537 new_state |= TIOCM_RTS;
538 dbg(DEBUG_IF, "%u: from B0 - set DTR%s", cs->minor_index,
539 (new_state & TIOCM_RTS) ? " only" : "/RTS");
540 cs->ops->set_modem_ctrl(cs, control_state, new_state);
541 control_state = new_state;
544 cs->ops->baud_rate(cs, cflag & CBAUD);
546 if ((cflag & CBAUD) == B0) {
547 /* Drop RTS and DTR */
548 dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
549 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
550 cs->ops->set_modem_ctrl(cs, control_state, new_state);
551 control_state = new_state;
555 * Update line control register (LCR)
558 cs->ops->set_line_ctrl(cs, cflag);
561 //FIXME this hangs M101 [ts 2005-03-09]
562 //FIXME do we need this?
564 * Set flow control: well, I do not really now how to handle DTR/RTS.
565 * Just do what we have seen with SniffUSB on Win98.
567 /* Drop DTR/RTS if no flow control otherwise assert */
568 dbg(DEBUG_IF, "%u: control_state %x", cs->minor_index, control_state);
569 new_state = control_state;
570 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
571 new_state |= TIOCM_DTR | TIOCM_RTS;
573 new_state &= ~(TIOCM_DTR | TIOCM_RTS);
574 if (new_state != control_state) {
575 dbg(DEBUG_IF, "%u: new_state %x", cs->minor_index, new_state);
576 gigaset_set_modem_ctrl(cs, control_state, new_state);
577 control_state = new_state;
581 /* save off the modified port settings */
582 cs->control_state = control_state;
589 /* wakeup tasklet for the write operation */
590 static void if_wake(unsigned long data)
592 struct cardstate *cs = (struct cardstate *) data;
593 struct tty_struct *tty;
599 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
600 tty->ldisc.write_wakeup) {
601 dbg(DEBUG_IF, "write wakeup call");
602 tty->ldisc.write_wakeup(tty);
605 wake_up_interruptible(&tty->write_wait);
608 /*** interface to common ***/
610 void gigaset_if_init(struct cardstate *cs)
612 struct gigaset_driver *drv;
618 tasklet_init(&cs->if_wake_tasklet, &if_wake, (unsigned long) cs);
619 tty_register_device(drv->tty, cs->minor_index, NULL);
622 void gigaset_if_free(struct cardstate *cs)
624 struct gigaset_driver *drv;
630 tasklet_disable(&cs->if_wake_tasklet);
631 tasklet_kill(&cs->if_wake_tasklet);
632 tty_unregister_device(drv->tty, cs->minor_index);
635 void gigaset_if_receive(struct cardstate *cs,
636 unsigned char *buffer, size_t len)
639 struct tty_struct *tty;
641 spin_lock_irqsave(&cs->lock, flags);
642 if ((tty = cs->tty) == NULL)
643 dbg(DEBUG_ANY, "receive on closed device");
645 tty_buffer_request_room(tty, len);
646 tty_insert_flip_string(tty, buffer, len);
647 tty_flip_buffer_push(tty);
649 spin_unlock_irqrestore(&cs->lock, flags);
651 EXPORT_SYMBOL_GPL(gigaset_if_receive);
653 /* gigaset_if_initdriver
654 * Initialize tty interface.
657 * procname Name of the driver (e.g. for /proc/tty/drivers)
658 * devname Name of the device files (prefix without minor number)
659 * devfsname Devfs name of the device files without %d
661 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
662 const char *devname, const char *devfsname)
664 unsigned minors = drv->minors;
666 struct tty_driver *tty;
670 if ((drv->tty = alloc_tty_driver(minors)) == NULL)
674 tty->magic = TTY_DRIVER_MAGIC,
675 tty->major = GIG_MAJOR,
676 tty->type = TTY_DRIVER_TYPE_SERIAL,
677 tty->subtype = SERIAL_TYPE_NORMAL,
678 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
680 tty->driver_name = procname;
682 tty->minor_start = drv->minor;
683 tty->num = drv->minors;
685 tty->owner = THIS_MODULE;
686 tty->devfs_name = devfsname;
688 tty->init_termios = tty_std_termios; //FIXME
689 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; //FIXME
690 tty_set_operations(tty, &if_ops);
692 ret = tty_register_driver(tty);
694 warn("failed to register tty driver (error %d)", ret);
697 dbg(DEBUG_IF, "tty driver initialized");
702 warn("could not allocate tty structures");
705 put_tty_driver(drv->tty);
708 void gigaset_if_freedriver(struct gigaset_driver *drv)
714 tty_unregister_driver(drv->tty);
715 put_tty_driver(drv->tty);