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>
20 static int if_lock(struct cardstate *cs, int *arg)
24 gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
30 *arg = cs->mstate == MS_LOCKED;
34 if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {
35 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
36 cs->ops->baud_rate(cs, B115200);
37 cs->ops->set_line_ctrl(cs, CS8);
38 cs->control_state = TIOCM_DTR|TIOCM_RTS;
42 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
47 gigaset_schedule_event(cs);
49 wait_event(cs->waitqueue, !cs->waiting);
51 if (cs->cmd_result >= 0) {
52 *arg = cs->cmd_result;
56 return cs->cmd_result;
59 static int if_version(struct cardstate *cs, unsigned arg[4])
61 static const unsigned version[4] = GIG_VERSION;
62 static const unsigned compat[4] = GIG_COMPAT;
63 unsigned cmd = arg[0];
65 gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
69 memcpy(arg, version, sizeof version);
72 memcpy(arg, compat, sizeof compat);
76 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
81 gigaset_schedule_event(cs);
83 wait_event(cs->waitqueue, !cs->waiting);
85 if (cs->cmd_result >= 0)
88 return cs->cmd_result;
94 static int if_config(struct cardstate *cs, int *arg)
96 gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
101 if (cs->mstate != MS_LOCKED)
104 if (!cs->connected) {
105 pr_err("%s: not connected\n", __func__);
110 return gigaset_enterconfigmode(cs);
113 /*** the terminal driver ***/
114 /* stolen from usbserial and some other tty drivers */
116 static int if_open(struct tty_struct *tty, struct file *filp);
117 static void if_close(struct tty_struct *tty, struct file *filp);
118 static int if_ioctl(struct tty_struct *tty,
119 unsigned int cmd, unsigned long arg);
120 static int if_write_room(struct tty_struct *tty);
121 static int if_chars_in_buffer(struct tty_struct *tty);
122 static void if_throttle(struct tty_struct *tty);
123 static void if_unthrottle(struct tty_struct *tty);
124 static void if_set_termios(struct tty_struct *tty, struct ktermios *old);
125 static int if_tiocmget(struct tty_struct *tty);
126 static int if_tiocmset(struct tty_struct *tty,
127 unsigned int set, unsigned int clear);
128 static int if_write(struct tty_struct *tty,
129 const unsigned char *buf, int count);
131 static const struct tty_operations if_ops = {
136 .write_room = if_write_room,
137 .chars_in_buffer = if_chars_in_buffer,
138 .set_termios = if_set_termios,
139 .throttle = if_throttle,
140 .unthrottle = if_unthrottle,
141 .tiocmget = if_tiocmget,
142 .tiocmset = if_tiocmset,
145 static int if_open(struct tty_struct *tty, struct file *filp)
147 struct cardstate *cs;
150 gig_dbg(DEBUG_IF, "%d+%d: %s()",
151 tty->driver->minor_start, tty->index, __func__);
153 tty->driver_data = NULL;
155 cs = gigaset_get_cs_by_tty(tty);
156 if (!cs || !try_module_get(cs->driver->owner))
159 if (mutex_lock_interruptible(&cs->mutex)) {
160 module_put(cs->driver->owner);
163 tty->driver_data = cs;
167 if (cs->open_count == 1) {
168 spin_lock_irqsave(&cs->lock, flags);
170 spin_unlock_irqrestore(&cs->lock, flags);
171 tty->low_latency = 1;
174 mutex_unlock(&cs->mutex);
178 static void if_close(struct tty_struct *tty, struct file *filp)
180 struct cardstate *cs;
183 cs = (struct cardstate *) tty->driver_data;
185 pr_err("%s: no cardstate\n", __func__);
189 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
191 mutex_lock(&cs->mutex);
194 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
195 else if (!cs->open_count)
196 dev_warn(cs->dev, "%s: device not opened\n", __func__);
198 if (!--cs->open_count) {
199 spin_lock_irqsave(&cs->lock, flags);
201 spin_unlock_irqrestore(&cs->lock, flags);
205 mutex_unlock(&cs->mutex);
207 module_put(cs->driver->owner);
210 static int if_ioctl(struct tty_struct *tty,
211 unsigned int cmd, unsigned long arg)
213 struct cardstate *cs;
214 int retval = -ENODEV;
216 unsigned char buf[6];
219 cs = (struct cardstate *) tty->driver_data;
221 pr_err("%s: no cardstate\n", __func__);
225 gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
227 if (mutex_lock_interruptible(&cs->mutex))
230 if (!cs->connected) {
231 gig_dbg(DEBUG_IF, "not connected");
233 } else if (!cs->open_count)
234 dev_warn(cs->dev, "%s: device not opened\n", __func__);
239 retval = get_user(int_arg, (int __user *) arg);
241 retval = if_lock(cs, &int_arg);
243 retval = put_user(int_arg, (int __user *) arg);
246 retval = get_user(int_arg, (int __user *) arg);
248 retval = if_config(cs, &int_arg);
250 retval = put_user(int_arg, (int __user *) arg);
252 case GIGASET_BRKCHARS:
253 retval = copy_from_user(&buf,
254 (const unsigned char __user *) arg, 6)
257 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
258 6, (const unsigned char *) arg);
259 retval = cs->ops->brkchars(cs, buf);
262 case GIGASET_VERSION:
263 retval = copy_from_user(version,
264 (unsigned __user *) arg, sizeof version)
267 retval = if_version(cs, version);
269 retval = copy_to_user((unsigned __user *) arg,
270 version, sizeof version)
274 gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",
276 retval = -ENOIOCTLCMD;
280 mutex_unlock(&cs->mutex);
285 static int if_tiocmget(struct tty_struct *tty)
287 struct cardstate *cs;
290 cs = (struct cardstate *) tty->driver_data;
292 pr_err("%s: no cardstate\n", __func__);
296 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
298 if (mutex_lock_interruptible(&cs->mutex))
301 retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);
303 mutex_unlock(&cs->mutex);
308 static int if_tiocmset(struct tty_struct *tty,
309 unsigned int set, unsigned int clear)
311 struct cardstate *cs;
315 cs = (struct cardstate *) tty->driver_data;
317 pr_err("%s: no cardstate\n", __func__);
321 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
322 cs->minor_index, __func__, set, clear);
324 if (mutex_lock_interruptible(&cs->mutex))
327 if (!cs->connected) {
328 gig_dbg(DEBUG_IF, "not connected");
331 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);
332 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
333 cs->control_state = mc;
336 mutex_unlock(&cs->mutex);
341 static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
343 struct cardstate *cs;
347 cs = (struct cardstate *) tty->driver_data;
349 pr_err("%s: no cardstate\n", __func__);
353 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
355 if (mutex_lock_interruptible(&cs->mutex))
358 if (!cs->connected) {
359 gig_dbg(DEBUG_IF, "not connected");
363 if (!cs->open_count) {
364 dev_warn(cs->dev, "%s: device not opened\n", __func__);
368 if (cs->mstate != MS_LOCKED) {
369 dev_warn(cs->dev, "can't write to unlocked device\n");
379 cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
381 dev_err(cs->dev, "%s: out of memory\n", __func__);
386 memcpy(cb->buf, buf, count);
390 cb->wake_tasklet = &cs->if_wake_tasklet;
391 retval = cs->ops->write_cmd(cs, cb);
393 mutex_unlock(&cs->mutex);
397 static int if_write_room(struct tty_struct *tty)
399 struct cardstate *cs;
400 int retval = -ENODEV;
402 cs = (struct cardstate *) tty->driver_data;
404 pr_err("%s: no cardstate\n", __func__);
408 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
410 if (mutex_lock_interruptible(&cs->mutex))
413 if (!cs->connected) {
414 gig_dbg(DEBUG_IF, "not connected");
416 } else if (!cs->open_count)
417 dev_warn(cs->dev, "%s: device not opened\n", __func__);
418 else if (cs->mstate != MS_LOCKED) {
419 dev_warn(cs->dev, "can't write to unlocked device\n");
422 retval = cs->ops->write_room(cs);
424 mutex_unlock(&cs->mutex);
429 static int if_chars_in_buffer(struct tty_struct *tty)
431 struct cardstate *cs;
434 cs = (struct cardstate *) tty->driver_data;
436 pr_err("%s: no cardstate\n", __func__);
440 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
442 mutex_lock(&cs->mutex);
445 gig_dbg(DEBUG_IF, "not connected");
446 else if (!cs->open_count)
447 dev_warn(cs->dev, "%s: device not opened\n", __func__);
448 else if (cs->mstate != MS_LOCKED)
449 dev_warn(cs->dev, "can't write to unlocked device\n");
451 retval = cs->ops->chars_in_buffer(cs);
453 mutex_unlock(&cs->mutex);
458 static void if_throttle(struct tty_struct *tty)
460 struct cardstate *cs;
462 cs = (struct cardstate *) tty->driver_data;
464 pr_err("%s: no cardstate\n", __func__);
468 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
470 mutex_lock(&cs->mutex);
473 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
474 else if (!cs->open_count)
475 dev_warn(cs->dev, "%s: device not opened\n", __func__);
477 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
479 mutex_unlock(&cs->mutex);
482 static void if_unthrottle(struct tty_struct *tty)
484 struct cardstate *cs;
486 cs = (struct cardstate *) tty->driver_data;
488 pr_err("%s: no cardstate\n", __func__);
492 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
494 mutex_lock(&cs->mutex);
497 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
498 else if (!cs->open_count)
499 dev_warn(cs->dev, "%s: device not opened\n", __func__);
501 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
503 mutex_unlock(&cs->mutex);
506 static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
508 struct cardstate *cs;
511 unsigned int old_cflag;
512 unsigned int control_state, new_state;
514 cs = (struct cardstate *) tty->driver_data;
516 pr_err("%s: no cardstate\n", __func__);
520 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
522 mutex_lock(&cs->mutex);
524 if (!cs->connected) {
525 gig_dbg(DEBUG_IF, "not connected");
529 if (!cs->open_count) {
530 dev_warn(cs->dev, "%s: device not opened\n", __func__);
534 iflag = tty->termios->c_iflag;
535 cflag = tty->termios->c_cflag;
536 old_cflag = old ? old->c_cflag : cflag;
537 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
538 cs->minor_index, iflag, cflag, old_cflag);
540 /* get a local copy of the current port settings */
541 control_state = cs->control_state;
545 * Do not attempt to cache old rates and skip settings,
546 * disconnects screw such tricks up completely.
547 * Premature optimization is the root of all evil.
550 /* reassert DTR and (maybe) RTS on transition from B0 */
551 if ((old_cflag & CBAUD) == B0) {
552 new_state = control_state | TIOCM_DTR;
553 /* don't set RTS if using hardware flow control */
554 if (!(old_cflag & CRTSCTS))
555 new_state |= TIOCM_RTS;
556 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
558 (new_state & TIOCM_RTS) ? " only" : "/RTS");
559 cs->ops->set_modem_ctrl(cs, control_state, new_state);
560 control_state = new_state;
563 cs->ops->baud_rate(cs, cflag & CBAUD);
565 if ((cflag & CBAUD) == B0) {
566 /* Drop RTS and DTR */
567 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
568 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
569 cs->ops->set_modem_ctrl(cs, control_state, new_state);
570 control_state = new_state;
574 * Update line control register (LCR)
577 cs->ops->set_line_ctrl(cs, cflag);
579 /* save off the modified port settings */
580 cs->control_state = control_state;
583 mutex_unlock(&cs->mutex);
587 /* wakeup tasklet for the write operation */
588 static void if_wake(unsigned long data)
590 struct cardstate *cs = (struct cardstate *) data;
596 /*** interface to common ***/
598 void gigaset_if_init(struct cardstate *cs)
600 struct gigaset_driver *drv;
606 tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
608 mutex_lock(&cs->mutex);
609 cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL);
611 if (!IS_ERR(cs->tty_dev))
612 dev_set_drvdata(cs->tty_dev, cs);
614 pr_warning("could not register device to the tty subsystem\n");
617 mutex_unlock(&cs->mutex);
620 void gigaset_if_free(struct cardstate *cs)
622 struct gigaset_driver *drv;
628 tasklet_disable(&cs->if_wake_tasklet);
629 tasklet_kill(&cs->if_wake_tasklet);
631 tty_unregister_device(drv->tty, cs->minor_index);
635 * gigaset_if_receive() - pass a received block of data to the tty device
636 * @cs: device descriptor structure.
637 * @buffer: received data.
638 * @len: number of bytes received.
640 * Called by asyncdata/isocdata if a block of data received from the
641 * device must be sent to userspace through the ttyG* device.
643 void gigaset_if_receive(struct cardstate *cs,
644 unsigned char *buffer, size_t len)
647 struct tty_struct *tty;
649 spin_lock_irqsave(&cs->lock, flags);
652 gig_dbg(DEBUG_IF, "receive on closed device");
654 tty_insert_flip_string(tty, buffer, len);
655 tty_flip_buffer_push(tty);
657 spin_unlock_irqrestore(&cs->lock, flags);
659 EXPORT_SYMBOL_GPL(gigaset_if_receive);
661 /* gigaset_if_initdriver
662 * Initialize tty interface.
665 * procname Name of the driver (e.g. for /proc/tty/drivers)
666 * devname Name of the device files (prefix without minor number)
668 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
671 unsigned minors = drv->minors;
673 struct tty_driver *tty;
677 drv->tty = tty = alloc_tty_driver(minors);
681 tty->magic = TTY_DRIVER_MAGIC,
682 tty->type = TTY_DRIVER_TYPE_SERIAL,
683 tty->subtype = SERIAL_TYPE_NORMAL,
684 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
686 tty->driver_name = procname;
688 tty->minor_start = drv->minor;
689 tty->num = drv->minors;
691 tty->owner = THIS_MODULE;
693 tty->init_termios = tty_std_termios;
694 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
695 tty_set_operations(tty, &if_ops);
697 ret = tty_register_driver(tty);
699 pr_err("error %d registering tty driver\n", ret);
702 gig_dbg(DEBUG_IF, "tty driver initialized");
707 pr_err("out of memory\n");
710 put_tty_driver(drv->tty);
713 void gigaset_if_freedriver(struct gigaset_driver *drv)
719 tty_unregister_driver(drv->tty);
720 put_tty_driver(drv->tty);