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 gig_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 gig_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 gig_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 gig_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 gig_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 gig_dbg(DEBUG_IF, "%d+%d: %s()",
155 tty->driver->minor_start, tty->index, __func__);
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", __func__);
191 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
196 warn("%s: device not opened", __func__);
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", __func__);
223 gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
225 if (down_interruptible(&cs->sem))
226 return -ERESTARTSYS; // FIXME -EINTR?
229 warn("%s: device not opened", __func__);
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 if (!atomic_read(&cs->connected)) {
251 "can't communicate with unplugged device");
255 retval = copy_from_user(&buf,
256 (const unsigned char __user *) arg, 6)
259 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
260 6, (const unsigned char *) arg);
261 retval = cs->ops->brkchars(cs, buf);
264 case GIGASET_VERSION:
265 retval = copy_from_user(version,
266 (unsigned __user *) arg, sizeof version)
269 retval = if_version(cs, version);
271 retval = copy_to_user((unsigned __user *) arg,
272 version, sizeof version)
276 gig_dbg(DEBUG_ANY, "%s: arg not supported - 0x%04x",
278 retval = -ENOIOCTLCMD;
287 static int if_tiocmget(struct tty_struct *tty, struct file *file)
289 struct cardstate *cs;
292 cs = (struct cardstate *) tty->driver_data;
294 err("cs==NULL in %s", __func__);
298 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
300 if (down_interruptible(&cs->sem))
301 return -ERESTARTSYS; // FIXME -EINTR?
303 // FIXME read from device?
304 retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);
311 static int if_tiocmset(struct tty_struct *tty, struct file *file,
312 unsigned int set, unsigned int clear)
314 struct cardstate *cs;
318 cs = (struct cardstate *) tty->driver_data;
320 err("cs==NULL in %s", __func__);
324 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
325 cs->minor_index, __func__, set, clear);
327 if (down_interruptible(&cs->sem))
328 return -ERESTARTSYS; // FIXME -EINTR?
330 if (!atomic_read(&cs->connected)) {
331 gig_dbg(DEBUG_ANY, "can't communicate with unplugged device");
334 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);
335 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
336 cs->control_state = mc;
344 static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
346 struct cardstate *cs;
347 int retval = -ENODEV;
349 cs = (struct cardstate *) tty->driver_data;
351 err("cs==NULL in %s", __func__);
355 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
357 if (down_interruptible(&cs->sem))
358 return -ERESTARTSYS; // FIXME -EINTR?
361 warn("%s: device not opened", __func__);
362 else if (atomic_read(&cs->mstate) != MS_LOCKED) {
363 warn("can't write to unlocked device");
365 } else if (!atomic_read(&cs->connected)) {
366 gig_dbg(DEBUG_ANY, "can't write to unplugged device");
367 retval = -EBUSY; //FIXME
369 retval = cs->ops->write_cmd(cs, buf, count,
370 &cs->if_wake_tasklet);
378 static int if_write_room(struct tty_struct *tty)
380 struct cardstate *cs;
381 int retval = -ENODEV;
383 cs = (struct cardstate *) tty->driver_data;
385 err("cs==NULL in %s", __func__);
389 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
391 if (down_interruptible(&cs->sem))
392 return -ERESTARTSYS; // FIXME -EINTR?
395 warn("%s: device not opened", __func__);
396 else if (atomic_read(&cs->mstate) != MS_LOCKED) {
397 warn("can't write to unlocked device");
398 retval = -EBUSY; //FIXME
399 } else if (!atomic_read(&cs->connected)) {
400 gig_dbg(DEBUG_ANY, "can't write to unplugged device");
401 retval = -EBUSY; //FIXME
403 retval = cs->ops->write_room(cs);
410 static int if_chars_in_buffer(struct tty_struct *tty)
412 struct cardstate *cs;
413 int retval = -ENODEV;
415 cs = (struct cardstate *) tty->driver_data;
417 err("cs==NULL in %s", __func__);
421 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
423 if (down_interruptible(&cs->sem))
424 return -ERESTARTSYS; // FIXME -EINTR?
427 warn("%s: device not opened", __func__);
428 else if (atomic_read(&cs->mstate) != MS_LOCKED) {
429 warn("can't write to unlocked device");
431 } else if (!atomic_read(&cs->connected)) {
432 gig_dbg(DEBUG_ANY, "can't write to unplugged device");
433 retval = -EBUSY; //FIXME
435 retval = cs->ops->chars_in_buffer(cs);
442 static void if_throttle(struct tty_struct *tty)
444 struct cardstate *cs;
446 cs = (struct cardstate *) tty->driver_data;
448 err("cs==NULL in %s", __func__);
452 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
457 warn("%s: device not opened", __func__);
465 static void if_unthrottle(struct tty_struct *tty)
467 struct cardstate *cs;
469 cs = (struct cardstate *) tty->driver_data;
471 err("cs==NULL in %s", __func__);
475 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
480 warn("%s: device not opened", __func__);
488 static void if_set_termios(struct tty_struct *tty, struct termios *old)
490 struct cardstate *cs;
493 unsigned int old_cflag;
494 unsigned int control_state, new_state;
496 cs = (struct cardstate *) tty->driver_data;
498 err("cs==NULL in %s", __func__);
502 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
506 if (!cs->open_count) {
507 warn("%s: device not opened", __func__);
511 if (!atomic_read(&cs->connected)) {
512 gig_dbg(DEBUG_ANY, "can't communicate with unplugged device");
516 // stolen from mct_u232.c
517 iflag = tty->termios->c_iflag;
518 cflag = tty->termios->c_cflag;
519 old_cflag = old ? old->c_cflag : cflag; //FIXME?
520 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
521 cs->minor_index, iflag, cflag, old_cflag);
523 /* get a local copy of the current port settings */
524 control_state = cs->control_state;
528 * Do not attempt to cache old rates and skip settings,
529 * disconnects screw such tricks up completely.
530 * Premature optimization is the root of all evil.
533 /* reassert DTR and (maybe) RTS on transition from B0 */
534 if ((old_cflag & CBAUD) == B0) {
535 new_state = control_state | TIOCM_DTR;
536 /* don't set RTS if using hardware flow control */
537 if (!(old_cflag & CRTSCTS))
538 new_state |= TIOCM_RTS;
539 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
541 (new_state & TIOCM_RTS) ? " only" : "/RTS");
542 cs->ops->set_modem_ctrl(cs, control_state, new_state);
543 control_state = new_state;
546 cs->ops->baud_rate(cs, cflag & CBAUD);
548 if ((cflag & CBAUD) == B0) {
549 /* Drop RTS and DTR */
550 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
551 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
552 cs->ops->set_modem_ctrl(cs, control_state, new_state);
553 control_state = new_state;
557 * Update line control register (LCR)
560 cs->ops->set_line_ctrl(cs, cflag);
563 //FIXME this hangs M101 [ts 2005-03-09]
564 //FIXME do we need this?
566 * Set flow control: well, I do not really now how to handle DTR/RTS.
567 * Just do what we have seen with SniffUSB on Win98.
569 /* Drop DTR/RTS if no flow control otherwise assert */
570 gig_dbg(DEBUG_IF, "%u: control_state %x",
571 cs->minor_index, control_state);
572 new_state = control_state;
573 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
574 new_state |= TIOCM_DTR | TIOCM_RTS;
576 new_state &= ~(TIOCM_DTR | TIOCM_RTS);
577 if (new_state != control_state) {
578 gig_dbg(DEBUG_IF, "%u: new_state %x",
579 cs->minor_index, new_state);
580 gigaset_set_modem_ctrl(cs, control_state, new_state);
581 control_state = new_state;
585 /* save off the modified port settings */
586 cs->control_state = control_state;
593 /* wakeup tasklet for the write operation */
594 static void if_wake(unsigned long data)
596 struct cardstate *cs = (struct cardstate *) data;
597 struct tty_struct *tty;
603 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
604 tty->ldisc.write_wakeup) {
605 gig_dbg(DEBUG_IF, "write wakeup call");
606 tty->ldisc.write_wakeup(tty);
609 wake_up_interruptible(&tty->write_wait);
612 /*** interface to common ***/
614 void gigaset_if_init(struct cardstate *cs)
616 struct gigaset_driver *drv;
622 tasklet_init(&cs->if_wake_tasklet, &if_wake, (unsigned long) cs);
623 tty_register_device(drv->tty, cs->minor_index, NULL);
626 void gigaset_if_free(struct cardstate *cs)
628 struct gigaset_driver *drv;
634 tasklet_disable(&cs->if_wake_tasklet);
635 tasklet_kill(&cs->if_wake_tasklet);
636 tty_unregister_device(drv->tty, cs->minor_index);
639 void gigaset_if_receive(struct cardstate *cs,
640 unsigned char *buffer, size_t len)
643 struct tty_struct *tty;
645 spin_lock_irqsave(&cs->lock, flags);
646 if ((tty = cs->tty) == NULL)
647 gig_dbg(DEBUG_ANY, "receive on closed device");
649 tty_buffer_request_room(tty, len);
650 tty_insert_flip_string(tty, buffer, len);
651 tty_flip_buffer_push(tty);
653 spin_unlock_irqrestore(&cs->lock, flags);
655 EXPORT_SYMBOL_GPL(gigaset_if_receive);
657 /* gigaset_if_initdriver
658 * Initialize tty interface.
661 * procname Name of the driver (e.g. for /proc/tty/drivers)
662 * devname Name of the device files (prefix without minor number)
663 * devfsname Devfs name of the device files without %d
665 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
666 const char *devname, const char *devfsname)
668 unsigned minors = drv->minors;
670 struct tty_driver *tty;
674 if ((drv->tty = alloc_tty_driver(minors)) == NULL)
678 tty->magic = TTY_DRIVER_MAGIC,
679 tty->major = GIG_MAJOR,
680 tty->type = TTY_DRIVER_TYPE_SERIAL,
681 tty->subtype = SERIAL_TYPE_NORMAL,
682 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
684 tty->driver_name = procname;
686 tty->minor_start = drv->minor;
687 tty->num = drv->minors;
689 tty->owner = THIS_MODULE;
690 tty->devfs_name = devfsname;
692 tty->init_termios = tty_std_termios; //FIXME
693 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; //FIXME
694 tty_set_operations(tty, &if_ops);
696 ret = tty_register_driver(tty);
698 warn("failed to register tty driver (error %d)", ret);
701 gig_dbg(DEBUG_IF, "tty driver initialized");
706 warn("could not allocate tty structures");
709 put_tty_driver(drv->tty);
712 void gigaset_if_freedriver(struct gigaset_driver *drv)
718 tty_unregister_driver(drv->tty);
719 put_tty_driver(drv->tty);