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
= 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
, 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 ktermios
*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 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 tty
->driver_data
= NULL
;
156 cs
= gigaset_get_cs_by_tty(tty
);
157 if (!cs
|| !try_module_get(cs
->driver
->owner
))
160 if (mutex_lock_interruptible(&cs
->mutex
))
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
;
182 cs
= (struct cardstate
*) tty
->driver_data
;
184 pr_err("%s: no cardstate\n", __func__
);
188 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
190 mutex_lock(&cs
->mutex
);
193 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
194 else if (!cs
->open_count
)
195 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
197 if (!--cs
->open_count
) {
198 spin_lock_irqsave(&cs
->lock
, flags
);
200 spin_unlock_irqrestore(&cs
->lock
, flags
);
204 mutex_unlock(&cs
->mutex
);
206 module_put(cs
->driver
->owner
);
209 static int if_ioctl(struct tty_struct
*tty
, struct file
*file
,
210 unsigned int cmd
, unsigned long arg
)
212 struct cardstate
*cs
;
213 int retval
= -ENODEV
;
215 unsigned char buf
[6];
218 cs
= (struct cardstate
*) tty
->driver_data
;
220 pr_err("%s: no cardstate\n", __func__
);
224 gig_dbg(DEBUG_IF
, "%u: %s(0x%x)", cs
->minor_index
, __func__
, cmd
);
226 if (mutex_lock_interruptible(&cs
->mutex
))
229 if (!cs
->connected
) {
230 gig_dbg(DEBUG_IF
, "not connected");
232 } else if (!cs
->open_count
)
233 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
238 retval
= get_user(int_arg
, (int __user
*) arg
);
240 retval
= if_lock(cs
, &int_arg
);
242 retval
= put_user(int_arg
, (int __user
*) arg
);
245 retval
= get_user(int_arg
, (int __user
*) arg
);
247 retval
= if_config(cs
, &int_arg
);
249 retval
= put_user(int_arg
, (int __user
*) arg
);
251 case GIGASET_BRKCHARS
:
252 retval
= copy_from_user(&buf
,
253 (const unsigned char __user
*) arg
, 6)
256 gigaset_dbg_buffer(DEBUG_IF
, "GIGASET_BRKCHARS",
257 6, (const unsigned char *) arg
);
258 retval
= cs
->ops
->brkchars(cs
, buf
);
261 case GIGASET_VERSION
:
262 retval
= copy_from_user(version
,
263 (unsigned __user
*) arg
, sizeof version
)
266 retval
= if_version(cs
, version
);
268 retval
= copy_to_user((unsigned __user
*) arg
,
269 version
, sizeof version
)
273 gig_dbg(DEBUG_IF
, "%s: arg not supported - 0x%04x",
275 retval
= -ENOIOCTLCMD
;
279 mutex_unlock(&cs
->mutex
);
284 static int if_tiocmget(struct tty_struct
*tty
, struct file
*file
)
286 struct cardstate
*cs
;
289 cs
= (struct cardstate
*) tty
->driver_data
;
291 pr_err("%s: no cardstate\n", __func__
);
295 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
297 if (mutex_lock_interruptible(&cs
->mutex
))
300 retval
= cs
->control_state
& (TIOCM_RTS
|TIOCM_DTR
);
302 mutex_unlock(&cs
->mutex
);
307 static int if_tiocmset(struct tty_struct
*tty
, struct file
*file
,
308 unsigned int set
, unsigned int clear
)
310 struct cardstate
*cs
;
314 cs
= (struct cardstate
*) tty
->driver_data
;
316 pr_err("%s: no cardstate\n", __func__
);
320 gig_dbg(DEBUG_IF
, "%u: %s(0x%x, 0x%x)",
321 cs
->minor_index
, __func__
, set
, clear
);
323 if (mutex_lock_interruptible(&cs
->mutex
))
326 if (!cs
->connected
) {
327 gig_dbg(DEBUG_IF
, "not connected");
330 mc
= (cs
->control_state
| set
) & ~clear
& (TIOCM_RTS
|TIOCM_DTR
);
331 retval
= cs
->ops
->set_modem_ctrl(cs
, cs
->control_state
, mc
);
332 cs
->control_state
= mc
;
335 mutex_unlock(&cs
->mutex
);
340 static int if_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
342 struct cardstate
*cs
;
343 int retval
= -ENODEV
;
345 cs
= (struct cardstate
*) tty
->driver_data
;
347 pr_err("%s: no cardstate\n", __func__
);
351 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
353 if (mutex_lock_interruptible(&cs
->mutex
))
356 if (!cs
->connected
) {
357 gig_dbg(DEBUG_IF
, "not connected");
359 } else if (!cs
->open_count
)
360 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
361 else if (cs
->mstate
!= MS_LOCKED
) {
362 dev_warn(cs
->dev
, "can't write to unlocked device\n");
365 retval
= cs
->ops
->write_cmd(cs
, buf
, count
,
366 &cs
->if_wake_tasklet
);
369 mutex_unlock(&cs
->mutex
);
374 static int if_write_room(struct tty_struct
*tty
)
376 struct cardstate
*cs
;
377 int retval
= -ENODEV
;
379 cs
= (struct cardstate
*) tty
->driver_data
;
381 pr_err("%s: no cardstate\n", __func__
);
385 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
387 if (mutex_lock_interruptible(&cs
->mutex
))
390 if (!cs
->connected
) {
391 gig_dbg(DEBUG_IF
, "not connected");
393 } else if (!cs
->open_count
)
394 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
395 else if (cs
->mstate
!= MS_LOCKED
) {
396 dev_warn(cs
->dev
, "can't write to unlocked device\n");
399 retval
= cs
->ops
->write_room(cs
);
401 mutex_unlock(&cs
->mutex
);
406 static int if_chars_in_buffer(struct tty_struct
*tty
)
408 struct cardstate
*cs
;
411 cs
= (struct cardstate
*) tty
->driver_data
;
413 pr_err("%s: no cardstate\n", __func__
);
417 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
419 mutex_lock(&cs
->mutex
);
422 gig_dbg(DEBUG_IF
, "not connected");
423 else if (!cs
->open_count
)
424 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
425 else if (cs
->mstate
!= MS_LOCKED
)
426 dev_warn(cs
->dev
, "can't write to unlocked device\n");
428 retval
= cs
->ops
->chars_in_buffer(cs
);
430 mutex_unlock(&cs
->mutex
);
435 static void if_throttle(struct tty_struct
*tty
)
437 struct cardstate
*cs
;
439 cs
= (struct cardstate
*) tty
->driver_data
;
441 pr_err("%s: no cardstate\n", __func__
);
445 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
447 mutex_lock(&cs
->mutex
);
450 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
451 else if (!cs
->open_count
)
452 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
454 gig_dbg(DEBUG_IF
, "%s: not implemented\n", __func__
);
456 mutex_unlock(&cs
->mutex
);
459 static void if_unthrottle(struct tty_struct
*tty
)
461 struct cardstate
*cs
;
463 cs
= (struct cardstate
*) tty
->driver_data
;
465 pr_err("%s: no cardstate\n", __func__
);
469 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
471 mutex_lock(&cs
->mutex
);
474 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
475 else if (!cs
->open_count
)
476 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
478 gig_dbg(DEBUG_IF
, "%s: not implemented\n", __func__
);
480 mutex_unlock(&cs
->mutex
);
483 static void if_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
485 struct cardstate
*cs
;
488 unsigned int old_cflag
;
489 unsigned int control_state
, new_state
;
491 cs
= (struct cardstate
*) tty
->driver_data
;
493 pr_err("%s: no cardstate\n", __func__
);
497 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
499 mutex_lock(&cs
->mutex
);
501 if (!cs
->connected
) {
502 gig_dbg(DEBUG_IF
, "not connected");
506 if (!cs
->open_count
) {
507 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
511 iflag
= tty
->termios
->c_iflag
;
512 cflag
= tty
->termios
->c_cflag
;
513 old_cflag
= old
? old
->c_cflag
: cflag
;
514 gig_dbg(DEBUG_IF
, "%u: iflag %x cflag %x old %x",
515 cs
->minor_index
, iflag
, cflag
, old_cflag
);
517 /* get a local copy of the current port settings */
518 control_state
= cs
->control_state
;
522 * Do not attempt to cache old rates and skip settings,
523 * disconnects screw such tricks up completely.
524 * Premature optimization is the root of all evil.
527 /* reassert DTR and (maybe) RTS on transition from B0 */
528 if ((old_cflag
& CBAUD
) == B0
) {
529 new_state
= control_state
| TIOCM_DTR
;
530 /* don't set RTS if using hardware flow control */
531 if (!(old_cflag
& CRTSCTS
))
532 new_state
|= TIOCM_RTS
;
533 gig_dbg(DEBUG_IF
, "%u: from B0 - set DTR%s",
535 (new_state
& TIOCM_RTS
) ? " only" : "/RTS");
536 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
537 control_state
= new_state
;
540 cs
->ops
->baud_rate(cs
, cflag
& CBAUD
);
542 if ((cflag
& CBAUD
) == B0
) {
543 /* Drop RTS and DTR */
544 gig_dbg(DEBUG_IF
, "%u: to B0 - drop DTR/RTS", cs
->minor_index
);
545 new_state
= control_state
& ~(TIOCM_DTR
| TIOCM_RTS
);
546 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
547 control_state
= new_state
;
551 * Update line control register (LCR)
554 cs
->ops
->set_line_ctrl(cs
, cflag
);
556 /* save off the modified port settings */
557 cs
->control_state
= control_state
;
560 mutex_unlock(&cs
->mutex
);
564 /* wakeup tasklet for the write operation */
565 static void if_wake(unsigned long data
)
567 struct cardstate
*cs
= (struct cardstate
*) data
;
573 /*** interface to common ***/
575 void gigaset_if_init(struct cardstate
*cs
)
577 struct gigaset_driver
*drv
;
583 tasklet_init(&cs
->if_wake_tasklet
, if_wake
, (unsigned long) cs
);
585 mutex_lock(&cs
->mutex
);
586 cs
->tty_dev
= tty_register_device(drv
->tty
, cs
->minor_index
, NULL
);
588 if (!IS_ERR(cs
->tty_dev
))
589 dev_set_drvdata(cs
->tty_dev
, cs
);
591 pr_warning("could not register device to the tty subsystem\n");
594 mutex_unlock(&cs
->mutex
);
597 void gigaset_if_free(struct cardstate
*cs
)
599 struct gigaset_driver
*drv
;
605 tasklet_disable(&cs
->if_wake_tasklet
);
606 tasklet_kill(&cs
->if_wake_tasklet
);
608 tty_unregister_device(drv
->tty
, cs
->minor_index
);
612 * gigaset_if_receive() - pass a received block of data to the tty device
613 * @cs: device descriptor structure.
614 * @buffer: received data.
615 * @len: number of bytes received.
617 * Called by asyncdata/isocdata if a block of data received from the
618 * device must be sent to userspace through the ttyG* device.
620 void gigaset_if_receive(struct cardstate
*cs
,
621 unsigned char *buffer
, size_t len
)
624 struct tty_struct
*tty
;
626 spin_lock_irqsave(&cs
->lock
, flags
);
629 gig_dbg(DEBUG_IF
, "receive on closed device");
631 tty_insert_flip_string(tty
, buffer
, len
);
632 tty_flip_buffer_push(tty
);
634 spin_unlock_irqrestore(&cs
->lock
, flags
);
636 EXPORT_SYMBOL_GPL(gigaset_if_receive
);
638 /* gigaset_if_initdriver
639 * Initialize tty interface.
642 * procname Name of the driver (e.g. for /proc/tty/drivers)
643 * devname Name of the device files (prefix without minor number)
645 void gigaset_if_initdriver(struct gigaset_driver
*drv
, const char *procname
,
648 unsigned minors
= drv
->minors
;
650 struct tty_driver
*tty
;
654 drv
->tty
= tty
= alloc_tty_driver(minors
);
658 tty
->magic
= TTY_DRIVER_MAGIC
,
659 tty
->major
= GIG_MAJOR
,
660 tty
->type
= TTY_DRIVER_TYPE_SERIAL
,
661 tty
->subtype
= SERIAL_TYPE_NORMAL
,
662 tty
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
664 tty
->driver_name
= procname
;
666 tty
->minor_start
= drv
->minor
;
667 tty
->num
= drv
->minors
;
669 tty
->owner
= THIS_MODULE
;
671 tty
->init_termios
= tty_std_termios
;
672 tty
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
673 tty_set_operations(tty
, &if_ops
);
675 ret
= tty_register_driver(tty
);
677 pr_err("error %d registering tty driver\n", ret
);
680 gig_dbg(DEBUG_IF
, "tty driver initialized");
685 pr_err("out of memory\n");
688 put_tty_driver(drv
->tty
);
691 void gigaset_if_freedriver(struct gigaset_driver
*drv
)
697 tty_unregister_driver(drv
->tty
);
698 put_tty_driver(drv
->tty
);