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
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
)
110 if (!cs
->connected
) {
111 err("not connected!");
116 return gigaset_enterconfigmode(cs
);
119 /*** the terminal driver ***/
120 /* stolen from usbserial and some other tty drivers */
122 static int if_open(struct tty_struct
*tty
, struct file
*filp
);
123 static void if_close(struct tty_struct
*tty
, struct file
*filp
);
124 static int if_ioctl(struct tty_struct
*tty
, struct file
*file
,
125 unsigned int cmd
, unsigned long arg
);
126 static int if_write_room(struct tty_struct
*tty
);
127 static int if_chars_in_buffer(struct tty_struct
*tty
);
128 static void if_throttle(struct tty_struct
*tty
);
129 static void if_unthrottle(struct tty_struct
*tty
);
130 static void if_set_termios(struct tty_struct
*tty
, struct termios
*old
);
131 static int if_tiocmget(struct tty_struct
*tty
, struct file
*file
);
132 static int if_tiocmset(struct tty_struct
*tty
, struct file
*file
,
133 unsigned int set
, unsigned int clear
);
134 static int if_write(struct tty_struct
*tty
,
135 const unsigned char *buf
, int count
);
137 static const struct tty_operations if_ops
= {
142 .write_room
= if_write_room
,
143 .chars_in_buffer
= if_chars_in_buffer
,
144 .set_termios
= if_set_termios
,
145 .throttle
= if_throttle
,
146 .unthrottle
= if_unthrottle
,
148 .break_ctl
= serial_break
,
150 .tiocmget
= if_tiocmget
,
151 .tiocmset
= if_tiocmset
,
154 static int if_open(struct tty_struct
*tty
, struct file
*filp
)
156 struct cardstate
*cs
;
159 gig_dbg(DEBUG_IF
, "%d+%d: %s()",
160 tty
->driver
->minor_start
, tty
->index
, __func__
);
162 tty
->driver_data
= NULL
;
164 cs
= gigaset_get_cs_by_tty(tty
);
168 if (mutex_lock_interruptible(&cs
->mutex
))
169 return -ERESTARTSYS
; // FIXME -EINTR?
170 tty
->driver_data
= cs
;
174 if (cs
->open_count
== 1) {
175 spin_lock_irqsave(&cs
->lock
, flags
);
177 spin_unlock_irqrestore(&cs
->lock
, flags
);
178 tty
->low_latency
= 1; //FIXME test
181 mutex_unlock(&cs
->mutex
);
185 static void if_close(struct tty_struct
*tty
, struct file
*filp
)
187 struct cardstate
*cs
;
190 cs
= (struct cardstate
*) tty
->driver_data
;
192 err("cs==NULL in %s", __func__
);
196 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
198 mutex_lock(&cs
->mutex
);
201 warn("%s: device not opened", __func__
);
203 if (!--cs
->open_count
) {
204 spin_lock_irqsave(&cs
->lock
, flags
);
206 spin_unlock_irqrestore(&cs
->lock
, flags
);
210 mutex_unlock(&cs
->mutex
);
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 err("cs==NULL in %s", __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?
234 warn("%s: device not opened", __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 //FIXME test if MS_LOCKED
254 if (!cs
->connected
) {
256 "can't communicate with unplugged device");
260 retval
= copy_from_user(&buf
,
261 (const unsigned char __user
*) arg
, 6)
264 gigaset_dbg_buffer(DEBUG_IF
, "GIGASET_BRKCHARS",
265 6, (const unsigned char *) arg
);
266 retval
= cs
->ops
->brkchars(cs
, buf
);
269 case GIGASET_VERSION
:
270 retval
= copy_from_user(version
,
271 (unsigned __user
*) arg
, sizeof version
)
274 retval
= if_version(cs
, version
);
276 retval
= copy_to_user((unsigned __user
*) arg
,
277 version
, sizeof version
)
281 gig_dbg(DEBUG_ANY
, "%s: arg not supported - 0x%04x",
283 retval
= -ENOIOCTLCMD
;
287 mutex_unlock(&cs
->mutex
);
292 static int if_tiocmget(struct tty_struct
*tty
, struct file
*file
)
294 struct cardstate
*cs
;
297 cs
= (struct cardstate
*) tty
->driver_data
;
299 err("cs==NULL in %s", __func__
);
303 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
305 if (mutex_lock_interruptible(&cs
->mutex
))
306 return -ERESTARTSYS
; // FIXME -EINTR?
308 // FIXME read from device?
309 retval
= cs
->control_state
& (TIOCM_RTS
|TIOCM_DTR
);
311 mutex_unlock(&cs
->mutex
);
316 static int if_tiocmset(struct tty_struct
*tty
, struct file
*file
,
317 unsigned int set
, unsigned int clear
)
319 struct cardstate
*cs
;
323 cs
= (struct cardstate
*) tty
->driver_data
;
325 err("cs==NULL in %s", __func__
);
329 gig_dbg(DEBUG_IF
, "%u: %s(0x%x, 0x%x)",
330 cs
->minor_index
, __func__
, set
, clear
);
332 if (mutex_lock_interruptible(&cs
->mutex
))
333 return -ERESTARTSYS
; // FIXME -EINTR?
335 if (!cs
->connected
) {
336 gig_dbg(DEBUG_ANY
, "can't communicate with unplugged device");
339 mc
= (cs
->control_state
| set
) & ~clear
& (TIOCM_RTS
|TIOCM_DTR
);
340 retval
= cs
->ops
->set_modem_ctrl(cs
, cs
->control_state
, mc
);
341 cs
->control_state
= mc
;
344 mutex_unlock(&cs
->mutex
);
349 static int if_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
351 struct cardstate
*cs
;
352 int retval
= -ENODEV
;
354 cs
= (struct cardstate
*) tty
->driver_data
;
356 err("cs==NULL in %s", __func__
);
360 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
362 if (mutex_lock_interruptible(&cs
->mutex
))
363 return -ERESTARTSYS
; // FIXME -EINTR?
366 warn("%s: device not opened", __func__
);
367 else if (atomic_read(&cs
->mstate
) != MS_LOCKED
) {
368 warn("can't write to unlocked device");
370 } else if (!cs
->connected
) {
371 gig_dbg(DEBUG_ANY
, "can't write to unplugged device");
372 retval
= -EBUSY
; //FIXME
374 retval
= cs
->ops
->write_cmd(cs
, buf
, count
,
375 &cs
->if_wake_tasklet
);
378 mutex_unlock(&cs
->mutex
);
383 static int if_write_room(struct tty_struct
*tty
)
385 struct cardstate
*cs
;
386 int retval
= -ENODEV
;
388 cs
= (struct cardstate
*) tty
->driver_data
;
390 err("cs==NULL in %s", __func__
);
394 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
396 if (mutex_lock_interruptible(&cs
->mutex
))
397 return -ERESTARTSYS
; // FIXME -EINTR?
400 warn("%s: device not opened", __func__
);
401 else if (atomic_read(&cs
->mstate
) != MS_LOCKED
) {
402 warn("can't write to unlocked device");
403 retval
= -EBUSY
; //FIXME
404 } else if (!cs
->connected
) {
405 gig_dbg(DEBUG_ANY
, "can't write to unplugged device");
406 retval
= -EBUSY
; //FIXME
408 retval
= cs
->ops
->write_room(cs
);
410 mutex_unlock(&cs
->mutex
);
415 static int if_chars_in_buffer(struct tty_struct
*tty
)
417 struct cardstate
*cs
;
418 int retval
= -ENODEV
;
420 cs
= (struct cardstate
*) tty
->driver_data
;
422 err("cs==NULL in %s", __func__
);
426 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
428 if (mutex_lock_interruptible(&cs
->mutex
))
429 return -ERESTARTSYS
; // FIXME -EINTR?
432 warn("%s: device not opened", __func__
);
433 else if (atomic_read(&cs
->mstate
) != MS_LOCKED
) {
434 warn("can't write to unlocked device");
436 } else if (!cs
->connected
) {
437 gig_dbg(DEBUG_ANY
, "can't write to unplugged device");
438 retval
= -EBUSY
; //FIXME
440 retval
= cs
->ops
->chars_in_buffer(cs
);
442 mutex_unlock(&cs
->mutex
);
447 static void if_throttle(struct tty_struct
*tty
)
449 struct cardstate
*cs
;
451 cs
= (struct cardstate
*) tty
->driver_data
;
453 err("cs==NULL in %s", __func__
);
457 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
459 mutex_lock(&cs
->mutex
);
462 warn("%s: device not opened", __func__
);
467 mutex_unlock(&cs
->mutex
);
470 static void if_unthrottle(struct tty_struct
*tty
)
472 struct cardstate
*cs
;
474 cs
= (struct cardstate
*) tty
->driver_data
;
476 err("cs==NULL in %s", __func__
);
480 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
482 mutex_lock(&cs
->mutex
);
485 warn("%s: device not opened", __func__
);
490 mutex_unlock(&cs
->mutex
);
493 static void if_set_termios(struct tty_struct
*tty
, struct termios
*old
)
495 struct cardstate
*cs
;
498 unsigned int old_cflag
;
499 unsigned int control_state
, new_state
;
501 cs
= (struct cardstate
*) tty
->driver_data
;
503 err("cs==NULL in %s", __func__
);
507 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
509 mutex_lock(&cs
->mutex
);
511 if (!cs
->open_count
) {
512 warn("%s: device not opened", __func__
);
516 if (!cs
->connected
) {
517 gig_dbg(DEBUG_ANY
, "can't communicate with unplugged device");
521 // stolen from mct_u232.c
522 iflag
= tty
->termios
->c_iflag
;
523 cflag
= tty
->termios
->c_cflag
;
524 old_cflag
= old
? old
->c_cflag
: cflag
; //FIXME?
525 gig_dbg(DEBUG_IF
, "%u: iflag %x cflag %x old %x",
526 cs
->minor_index
, iflag
, cflag
, old_cflag
);
528 /* get a local copy of the current port settings */
529 control_state
= cs
->control_state
;
533 * Do not attempt to cache old rates and skip settings,
534 * disconnects screw such tricks up completely.
535 * Premature optimization is the root of all evil.
538 /* reassert DTR and (maybe) RTS on transition from B0 */
539 if ((old_cflag
& CBAUD
) == B0
) {
540 new_state
= control_state
| TIOCM_DTR
;
541 /* don't set RTS if using hardware flow control */
542 if (!(old_cflag
& CRTSCTS
))
543 new_state
|= TIOCM_RTS
;
544 gig_dbg(DEBUG_IF
, "%u: from B0 - set DTR%s",
546 (new_state
& TIOCM_RTS
) ? " only" : "/RTS");
547 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
548 control_state
= new_state
;
551 cs
->ops
->baud_rate(cs
, cflag
& CBAUD
);
553 if ((cflag
& CBAUD
) == B0
) {
554 /* Drop RTS and DTR */
555 gig_dbg(DEBUG_IF
, "%u: to B0 - drop DTR/RTS", cs
->minor_index
);
556 new_state
= control_state
& ~(TIOCM_DTR
| TIOCM_RTS
);
557 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
558 control_state
= new_state
;
562 * Update line control register (LCR)
565 cs
->ops
->set_line_ctrl(cs
, cflag
);
568 //FIXME this hangs M101 [ts 2005-03-09]
569 //FIXME do we need this?
571 * Set flow control: well, I do not really now how to handle DTR/RTS.
572 * Just do what we have seen with SniffUSB on Win98.
574 /* Drop DTR/RTS if no flow control otherwise assert */
575 gig_dbg(DEBUG_IF
, "%u: control_state %x",
576 cs
->minor_index
, control_state
);
577 new_state
= control_state
;
578 if ((iflag
& IXOFF
) || (iflag
& IXON
) || (cflag
& CRTSCTS
))
579 new_state
|= TIOCM_DTR
| TIOCM_RTS
;
581 new_state
&= ~(TIOCM_DTR
| TIOCM_RTS
);
582 if (new_state
!= control_state
) {
583 gig_dbg(DEBUG_IF
, "%u: new_state %x",
584 cs
->minor_index
, new_state
);
585 gigaset_set_modem_ctrl(cs
, control_state
, new_state
);
586 control_state
= new_state
;
590 /* save off the modified port settings */
591 cs
->control_state
= control_state
;
594 mutex_unlock(&cs
->mutex
);
598 /* wakeup tasklet for the write operation */
599 static void if_wake(unsigned long data
)
601 struct cardstate
*cs
= (struct cardstate
*) data
;
602 struct tty_struct
*tty
;
608 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
609 tty
->ldisc
.write_wakeup
) {
610 gig_dbg(DEBUG_IF
, "write wakeup call");
611 tty
->ldisc
.write_wakeup(tty
);
614 wake_up_interruptible(&tty
->write_wait
);
617 /*** interface to common ***/
619 void gigaset_if_init(struct cardstate
*cs
)
621 struct gigaset_driver
*drv
;
627 tasklet_init(&cs
->if_wake_tasklet
, &if_wake
, (unsigned long) cs
);
628 cs
->tty_dev
= tty_register_device(drv
->tty
, cs
->minor_index
, NULL
);
630 if (!IS_ERR(cs
->tty_dev
))
631 dev_set_drvdata(cs
->tty_dev
, cs
);
633 warn("could not register device to the tty subsystem");
638 void gigaset_if_free(struct cardstate
*cs
)
640 struct gigaset_driver
*drv
;
646 tasklet_disable(&cs
->if_wake_tasklet
);
647 tasklet_kill(&cs
->if_wake_tasklet
);
649 tty_unregister_device(drv
->tty
, cs
->minor_index
);
652 void gigaset_if_receive(struct cardstate
*cs
,
653 unsigned char *buffer
, size_t len
)
656 struct tty_struct
*tty
;
658 spin_lock_irqsave(&cs
->lock
, flags
);
659 if ((tty
= cs
->tty
) == NULL
)
660 gig_dbg(DEBUG_ANY
, "receive on closed device");
662 tty_buffer_request_room(tty
, len
);
663 tty_insert_flip_string(tty
, buffer
, len
);
664 tty_flip_buffer_push(tty
);
666 spin_unlock_irqrestore(&cs
->lock
, flags
);
668 EXPORT_SYMBOL_GPL(gigaset_if_receive
);
670 /* gigaset_if_initdriver
671 * Initialize tty interface.
674 * procname Name of the driver (e.g. for /proc/tty/drivers)
675 * devname Name of the device files (prefix without minor number)
677 void gigaset_if_initdriver(struct gigaset_driver
*drv
, const char *procname
,
680 unsigned minors
= drv
->minors
;
682 struct tty_driver
*tty
;
686 if ((drv
->tty
= alloc_tty_driver(minors
)) == NULL
)
690 tty
->magic
= TTY_DRIVER_MAGIC
,
691 tty
->major
= GIG_MAJOR
,
692 tty
->type
= TTY_DRIVER_TYPE_SERIAL
,
693 tty
->subtype
= SERIAL_TYPE_NORMAL
,
694 tty
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
696 tty
->driver_name
= procname
;
698 tty
->minor_start
= drv
->minor
;
699 tty
->num
= drv
->minors
;
701 tty
->owner
= THIS_MODULE
;
703 tty
->init_termios
= tty_std_termios
; //FIXME
704 tty
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
; //FIXME
705 tty_set_operations(tty
, &if_ops
);
707 ret
= tty_register_driver(tty
);
709 warn("failed to register tty driver (error %d)", ret
);
712 gig_dbg(DEBUG_IF
, "tty driver initialized");
717 warn("could not allocate tty structures");
720 put_tty_driver(drv
->tty
);
723 void gigaset_if_freedriver(struct gigaset_driver
*drv
)
729 tty_unregister_driver(drv
->tty
);
730 put_tty_driver(drv
->tty
);