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
,
49 gig_dbg(DEBUG_CMD
, "scheduling IF_LOCK");
50 gigaset_schedule_event(cs
);
52 wait_event(cs
->waitqueue
, !cs
->waiting
);
54 if (cs
->cmd_result
>= 0) {
55 *arg
= cs
->cmd_result
;
59 return cs
->cmd_result
;
62 static int if_version(struct cardstate
*cs
, unsigned arg
[4])
64 static const unsigned version
[4] = GIG_VERSION
;
65 static const unsigned compat
[4] = GIG_COMPAT
;
66 unsigned cmd
= arg
[0];
68 gig_dbg(DEBUG_IF
, "%u: if_version (%d)", cs
->minor_index
, cmd
);
72 memcpy(arg
, version
, sizeof version
);
75 memcpy(arg
, compat
, sizeof compat
);
79 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_IF_VER
,
85 gig_dbg(DEBUG_CMD
, "scheduling IF_VER");
86 gigaset_schedule_event(cs
);
88 wait_event(cs
->waitqueue
, !cs
->waiting
);
90 if (cs
->cmd_result
>= 0)
93 return cs
->cmd_result
;
99 static int if_config(struct cardstate
*cs
, int *arg
)
101 gig_dbg(DEBUG_IF
, "%u: if_config (%d)", cs
->minor_index
, *arg
);
106 if (cs
->mstate
!= MS_LOCKED
)
109 if (!cs
->connected
) {
110 pr_err("%s: not connected\n", __func__
);
115 return gigaset_enterconfigmode(cs
);
118 /*** the terminal driver ***/
119 /* stolen from usbserial and some other tty drivers */
121 static int if_open(struct tty_struct
*tty
, struct file
*filp
);
122 static void if_close(struct tty_struct
*tty
, struct file
*filp
);
123 static int if_ioctl(struct tty_struct
*tty
, struct file
*file
,
124 unsigned int cmd
, unsigned long arg
);
125 static int if_write_room(struct tty_struct
*tty
);
126 static int if_chars_in_buffer(struct tty_struct
*tty
);
127 static void if_throttle(struct tty_struct
*tty
);
128 static void if_unthrottle(struct tty_struct
*tty
);
129 static void if_set_termios(struct tty_struct
*tty
, struct ktermios
*old
);
130 static int if_tiocmget(struct tty_struct
*tty
, struct file
*file
);
131 static int if_tiocmset(struct tty_struct
*tty
, struct file
*file
,
132 unsigned int set
, unsigned int clear
);
133 static int if_write(struct tty_struct
*tty
,
134 const unsigned char *buf
, int count
);
136 static const struct tty_operations if_ops
= {
141 .write_room
= if_write_room
,
142 .chars_in_buffer
= if_chars_in_buffer
,
143 .set_termios
= if_set_termios
,
144 .throttle
= if_throttle
,
145 .unthrottle
= if_unthrottle
,
146 .tiocmget
= if_tiocmget
,
147 .tiocmset
= if_tiocmset
,
150 static int if_open(struct tty_struct
*tty
, struct file
*filp
)
152 struct cardstate
*cs
;
155 gig_dbg(DEBUG_IF
, "%d+%d: %s()",
156 tty
->driver
->minor_start
, tty
->index
, __func__
);
158 tty
->driver_data
= NULL
;
160 cs
= gigaset_get_cs_by_tty(tty
);
161 if (!cs
|| !try_module_get(cs
->driver
->owner
))
164 if (mutex_lock_interruptible(&cs
->mutex
))
165 return -ERESTARTSYS
; // FIXME -EINTR?
166 tty
->driver_data
= cs
;
170 if (cs
->open_count
== 1) {
171 spin_lock_irqsave(&cs
->lock
, flags
);
173 spin_unlock_irqrestore(&cs
->lock
, flags
);
174 tty
->low_latency
= 1; //FIXME test
177 mutex_unlock(&cs
->mutex
);
181 static void if_close(struct tty_struct
*tty
, struct file
*filp
)
183 struct cardstate
*cs
;
186 cs
= (struct cardstate
*) tty
->driver_data
;
188 pr_err("%s: no cardstate\n", __func__
);
192 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
194 mutex_lock(&cs
->mutex
);
197 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
198 else if (!cs
->open_count
)
199 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
201 if (!--cs
->open_count
) {
202 spin_lock_irqsave(&cs
->lock
, flags
);
204 spin_unlock_irqrestore(&cs
->lock
, flags
);
208 mutex_unlock(&cs
->mutex
);
210 module_put(cs
->driver
->owner
);
213 static int if_ioctl(struct tty_struct
*tty
, struct file
*file
,
214 unsigned int cmd
, unsigned long arg
)
216 struct cardstate
*cs
;
217 int retval
= -ENODEV
;
219 unsigned char buf
[6];
222 cs
= (struct cardstate
*) tty
->driver_data
;
224 pr_err("%s: no cardstate\n", __func__
);
228 gig_dbg(DEBUG_IF
, "%u: %s(0x%x)", cs
->minor_index
, __func__
, cmd
);
230 if (mutex_lock_interruptible(&cs
->mutex
))
231 return -ERESTARTSYS
; // FIXME -EINTR?
233 if (!cs
->connected
) {
234 gig_dbg(DEBUG_IF
, "not connected");
236 } else if (!cs
->open_count
)
237 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
242 retval
= get_user(int_arg
, (int __user
*) arg
);
244 retval
= if_lock(cs
, &int_arg
);
246 retval
= put_user(int_arg
, (int __user
*) arg
);
249 retval
= get_user(int_arg
, (int __user
*) arg
);
251 retval
= if_config(cs
, &int_arg
);
253 retval
= put_user(int_arg
, (int __user
*) arg
);
255 case GIGASET_BRKCHARS
:
256 retval
= copy_from_user(&buf
,
257 (const unsigned char __user
*) arg
, 6)
260 gigaset_dbg_buffer(DEBUG_IF
, "GIGASET_BRKCHARS",
261 6, (const unsigned char *) arg
);
262 retval
= cs
->ops
->brkchars(cs
, buf
);
265 case GIGASET_VERSION
:
266 retval
= copy_from_user(version
,
267 (unsigned __user
*) arg
, sizeof version
)
270 retval
= if_version(cs
, version
);
272 retval
= copy_to_user((unsigned __user
*) arg
,
273 version
, sizeof version
)
277 gig_dbg(DEBUG_ANY
, "%s: arg not supported - 0x%04x",
279 retval
= -ENOIOCTLCMD
;
283 mutex_unlock(&cs
->mutex
);
288 static int if_tiocmget(struct tty_struct
*tty
, struct file
*file
)
290 struct cardstate
*cs
;
293 cs
= (struct cardstate
*) tty
->driver_data
;
295 pr_err("%s: no cardstate\n", __func__
);
299 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
301 if (mutex_lock_interruptible(&cs
->mutex
))
302 return -ERESTARTSYS
; // FIXME -EINTR?
304 // FIXME read from device?
305 retval
= cs
->control_state
& (TIOCM_RTS
|TIOCM_DTR
);
307 mutex_unlock(&cs
->mutex
);
312 static int if_tiocmset(struct tty_struct
*tty
, struct file
*file
,
313 unsigned int set
, unsigned int clear
)
315 struct cardstate
*cs
;
319 cs
= (struct cardstate
*) tty
->driver_data
;
321 pr_err("%s: no cardstate\n", __func__
);
325 gig_dbg(DEBUG_IF
, "%u: %s(0x%x, 0x%x)",
326 cs
->minor_index
, __func__
, set
, clear
);
328 if (mutex_lock_interruptible(&cs
->mutex
))
329 return -ERESTARTSYS
; // FIXME -EINTR?
331 if (!cs
->connected
) {
332 gig_dbg(DEBUG_IF
, "not connected");
335 mc
= (cs
->control_state
| set
) & ~clear
& (TIOCM_RTS
|TIOCM_DTR
);
336 retval
= cs
->ops
->set_modem_ctrl(cs
, cs
->control_state
, mc
);
337 cs
->control_state
= mc
;
340 mutex_unlock(&cs
->mutex
);
345 static int if_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
347 struct cardstate
*cs
;
348 int retval
= -ENODEV
;
350 cs
= (struct cardstate
*) tty
->driver_data
;
352 pr_err("%s: no cardstate\n", __func__
);
356 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
358 if (mutex_lock_interruptible(&cs
->mutex
))
359 return -ERESTARTSYS
; // FIXME -EINTR?
361 if (!cs
->connected
) {
362 gig_dbg(DEBUG_IF
, "not connected");
364 } else if (!cs
->open_count
)
365 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
366 else if (cs
->mstate
!= MS_LOCKED
) {
367 dev_warn(cs
->dev
, "can't write to unlocked device\n");
370 retval
= cs
->ops
->write_cmd(cs
, buf
, count
,
371 &cs
->if_wake_tasklet
);
374 mutex_unlock(&cs
->mutex
);
379 static int if_write_room(struct tty_struct
*tty
)
381 struct cardstate
*cs
;
382 int retval
= -ENODEV
;
384 cs
= (struct cardstate
*) tty
->driver_data
;
386 pr_err("%s: no cardstate\n", __func__
);
390 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
392 if (mutex_lock_interruptible(&cs
->mutex
))
393 return -ERESTARTSYS
; // FIXME -EINTR?
395 if (!cs
->connected
) {
396 gig_dbg(DEBUG_IF
, "not connected");
398 } else if (!cs
->open_count
)
399 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
400 else if (cs
->mstate
!= MS_LOCKED
) {
401 dev_warn(cs
->dev
, "can't write to unlocked device\n");
404 retval
= cs
->ops
->write_room(cs
);
406 mutex_unlock(&cs
->mutex
);
411 static int if_chars_in_buffer(struct tty_struct
*tty
)
413 struct cardstate
*cs
;
416 cs
= (struct cardstate
*) tty
->driver_data
;
418 pr_err("%s: no cardstate\n", __func__
);
422 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
424 mutex_lock(&cs
->mutex
);
427 gig_dbg(DEBUG_IF
, "not connected");
428 else if (!cs
->open_count
)
429 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
430 else if (cs
->mstate
!= MS_LOCKED
)
431 dev_warn(cs
->dev
, "can't write to unlocked device\n");
433 retval
= cs
->ops
->chars_in_buffer(cs
);
435 mutex_unlock(&cs
->mutex
);
440 static void if_throttle(struct tty_struct
*tty
)
442 struct cardstate
*cs
;
444 cs
= (struct cardstate
*) tty
->driver_data
;
446 pr_err("%s: no cardstate\n", __func__
);
450 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
452 mutex_lock(&cs
->mutex
);
455 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
456 else if (!cs
->open_count
)
457 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
462 mutex_unlock(&cs
->mutex
);
465 static void if_unthrottle(struct tty_struct
*tty
)
467 struct cardstate
*cs
;
469 cs
= (struct cardstate
*) tty
->driver_data
;
471 pr_err("%s: no cardstate\n", __func__
);
475 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
477 mutex_lock(&cs
->mutex
);
480 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
481 else if (!cs
->open_count
)
482 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
487 mutex_unlock(&cs
->mutex
);
490 static void if_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
492 struct cardstate
*cs
;
495 unsigned int old_cflag
;
496 unsigned int control_state
, new_state
;
498 cs
= (struct cardstate
*) tty
->driver_data
;
500 pr_err("%s: no cardstate\n", __func__
);
504 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
506 mutex_lock(&cs
->mutex
);
508 if (!cs
->connected
) {
509 gig_dbg(DEBUG_IF
, "not connected");
513 if (!cs
->open_count
) {
514 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
518 // stolen from mct_u232.c
519 iflag
= tty
->termios
->c_iflag
;
520 cflag
= tty
->termios
->c_cflag
;
521 old_cflag
= old
? old
->c_cflag
: cflag
; //FIXME?
522 gig_dbg(DEBUG_IF
, "%u: iflag %x cflag %x old %x",
523 cs
->minor_index
, iflag
, cflag
, old_cflag
);
525 /* get a local copy of the current port settings */
526 control_state
= cs
->control_state
;
530 * Do not attempt to cache old rates and skip settings,
531 * disconnects screw such tricks up completely.
532 * Premature optimization is the root of all evil.
535 /* reassert DTR and (maybe) RTS on transition from B0 */
536 if ((old_cflag
& CBAUD
) == B0
) {
537 new_state
= control_state
| TIOCM_DTR
;
538 /* don't set RTS if using hardware flow control */
539 if (!(old_cflag
& CRTSCTS
))
540 new_state
|= TIOCM_RTS
;
541 gig_dbg(DEBUG_IF
, "%u: from B0 - set DTR%s",
543 (new_state
& TIOCM_RTS
) ? " only" : "/RTS");
544 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
545 control_state
= new_state
;
548 cs
->ops
->baud_rate(cs
, cflag
& CBAUD
);
550 if ((cflag
& CBAUD
) == B0
) {
551 /* Drop RTS and DTR */
552 gig_dbg(DEBUG_IF
, "%u: to B0 - drop DTR/RTS", cs
->minor_index
);
553 new_state
= control_state
& ~(TIOCM_DTR
| TIOCM_RTS
);
554 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
555 control_state
= new_state
;
559 * Update line control register (LCR)
562 cs
->ops
->set_line_ctrl(cs
, cflag
);
564 /* save off the modified port settings */
565 cs
->control_state
= control_state
;
568 mutex_unlock(&cs
->mutex
);
572 /* wakeup tasklet for the write operation */
573 static void if_wake(unsigned long data
)
575 struct cardstate
*cs
= (struct cardstate
*) data
;
581 /*** interface to common ***/
583 void gigaset_if_init(struct cardstate
*cs
)
585 struct gigaset_driver
*drv
;
591 tasklet_init(&cs
->if_wake_tasklet
, &if_wake
, (unsigned long) cs
);
593 mutex_lock(&cs
->mutex
);
594 cs
->tty_dev
= tty_register_device(drv
->tty
, cs
->minor_index
, NULL
);
596 if (!IS_ERR(cs
->tty_dev
))
597 dev_set_drvdata(cs
->tty_dev
, cs
);
599 pr_warning("could not register device to the tty subsystem\n");
602 mutex_unlock(&cs
->mutex
);
605 void gigaset_if_free(struct cardstate
*cs
)
607 struct gigaset_driver
*drv
;
613 tasklet_disable(&cs
->if_wake_tasklet
);
614 tasklet_kill(&cs
->if_wake_tasklet
);
616 tty_unregister_device(drv
->tty
, cs
->minor_index
);
620 * gigaset_if_receive() - pass a received block of data to the tty device
621 * @cs: device descriptor structure.
622 * @buffer: received data.
623 * @len: number of bytes received.
625 * Called by asyncdata/isocdata if a block of data received from the
626 * device must be sent to userspace through the ttyG* device.
628 void gigaset_if_receive(struct cardstate
*cs
,
629 unsigned char *buffer
, size_t len
)
632 struct tty_struct
*tty
;
634 spin_lock_irqsave(&cs
->lock
, flags
);
635 if ((tty
= cs
->tty
) == NULL
)
636 gig_dbg(DEBUG_ANY
, "receive on closed device");
638 tty_buffer_request_room(tty
, len
);
639 tty_insert_flip_string(tty
, buffer
, len
);
640 tty_flip_buffer_push(tty
);
642 spin_unlock_irqrestore(&cs
->lock
, flags
);
644 EXPORT_SYMBOL_GPL(gigaset_if_receive
);
646 /* gigaset_if_initdriver
647 * Initialize tty interface.
650 * procname Name of the driver (e.g. for /proc/tty/drivers)
651 * devname Name of the device files (prefix without minor number)
653 void gigaset_if_initdriver(struct gigaset_driver
*drv
, const char *procname
,
656 unsigned minors
= drv
->minors
;
658 struct tty_driver
*tty
;
662 if ((drv
->tty
= alloc_tty_driver(minors
)) == NULL
)
666 tty
->magic
= TTY_DRIVER_MAGIC
,
667 tty
->major
= GIG_MAJOR
,
668 tty
->type
= TTY_DRIVER_TYPE_SERIAL
,
669 tty
->subtype
= SERIAL_TYPE_NORMAL
,
670 tty
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
672 tty
->driver_name
= procname
;
674 tty
->minor_start
= drv
->minor
;
675 tty
->num
= drv
->minors
;
677 tty
->owner
= THIS_MODULE
;
679 tty
->init_termios
= tty_std_termios
; //FIXME
680 tty
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
; //FIXME
681 tty_set_operations(tty
, &if_ops
);
683 ret
= tty_register_driver(tty
);
685 pr_err("error %d registering tty driver\n", ret
);
688 gig_dbg(DEBUG_IF
, "tty driver initialized");
693 pr_err("out of memory\n");
696 put_tty_driver(drv
->tty
);
699 void gigaset_if_freedriver(struct gigaset_driver
*drv
)
705 tty_unregister_driver(drv
->tty
);
706 put_tty_driver(drv
->tty
);