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 err("not connected!");
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
,
147 .break_ctl
= serial_break
,
149 .tiocmget
= if_tiocmget
,
150 .tiocmset
= if_tiocmset
,
153 static int if_open(struct tty_struct
*tty
, struct file
*filp
)
155 struct cardstate
*cs
;
158 gig_dbg(DEBUG_IF
, "%d+%d: %s()",
159 tty
->driver
->minor_start
, tty
->index
, __func__
);
161 tty
->driver_data
= NULL
;
163 cs
= gigaset_get_cs_by_tty(tty
);
164 if (!cs
|| !try_module_get(cs
->driver
->owner
))
167 if (mutex_lock_interruptible(&cs
->mutex
))
168 return -ERESTARTSYS
; // FIXME -EINTR?
169 tty
->driver_data
= cs
;
173 if (cs
->open_count
== 1) {
174 spin_lock_irqsave(&cs
->lock
, flags
);
176 spin_unlock_irqrestore(&cs
->lock
, flags
);
177 tty
->low_latency
= 1; //FIXME test
180 mutex_unlock(&cs
->mutex
);
184 static void if_close(struct tty_struct
*tty
, struct file
*filp
)
186 struct cardstate
*cs
;
189 cs
= (struct cardstate
*) tty
->driver_data
;
191 err("cs==NULL in %s", __func__
);
195 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
197 mutex_lock(&cs
->mutex
);
200 warn("%s: device not opened", __func__
);
202 if (!--cs
->open_count
) {
203 spin_lock_irqsave(&cs
->lock
, flags
);
205 spin_unlock_irqrestore(&cs
->lock
, flags
);
209 mutex_unlock(&cs
->mutex
);
211 module_put(cs
->driver
->owner
);
214 static int if_ioctl(struct tty_struct
*tty
, struct file
*file
,
215 unsigned int cmd
, unsigned long arg
)
217 struct cardstate
*cs
;
218 int retval
= -ENODEV
;
220 unsigned char buf
[6];
223 cs
= (struct cardstate
*) tty
->driver_data
;
225 err("cs==NULL in %s", __func__
);
229 gig_dbg(DEBUG_IF
, "%u: %s(0x%x)", cs
->minor_index
, __func__
, cmd
);
231 if (mutex_lock_interruptible(&cs
->mutex
))
232 return -ERESTARTSYS
; // FIXME -EINTR?
235 warn("%s: device not opened", __func__
);
240 retval
= get_user(int_arg
, (int __user
*) arg
);
242 retval
= if_lock(cs
, &int_arg
);
244 retval
= put_user(int_arg
, (int __user
*) arg
);
247 retval
= get_user(int_arg
, (int __user
*) arg
);
249 retval
= if_config(cs
, &int_arg
);
251 retval
= put_user(int_arg
, (int __user
*) arg
);
253 case GIGASET_BRKCHARS
:
254 //FIXME test if MS_LOCKED
255 if (!cs
->connected
) {
257 "can't communicate with unplugged device");
261 retval
= copy_from_user(&buf
,
262 (const unsigned char __user
*) arg
, 6)
265 gigaset_dbg_buffer(DEBUG_IF
, "GIGASET_BRKCHARS",
266 6, (const unsigned char *) arg
);
267 retval
= cs
->ops
->brkchars(cs
, buf
);
270 case GIGASET_VERSION
:
271 retval
= copy_from_user(version
,
272 (unsigned __user
*) arg
, sizeof version
)
275 retval
= if_version(cs
, version
);
277 retval
= copy_to_user((unsigned __user
*) arg
,
278 version
, sizeof version
)
282 gig_dbg(DEBUG_ANY
, "%s: arg not supported - 0x%04x",
284 retval
= -ENOIOCTLCMD
;
288 mutex_unlock(&cs
->mutex
);
293 static int if_tiocmget(struct tty_struct
*tty
, struct file
*file
)
295 struct cardstate
*cs
;
298 cs
= (struct cardstate
*) tty
->driver_data
;
300 err("cs==NULL in %s", __func__
);
304 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
306 if (mutex_lock_interruptible(&cs
->mutex
))
307 return -ERESTARTSYS
; // FIXME -EINTR?
309 // FIXME read from device?
310 retval
= cs
->control_state
& (TIOCM_RTS
|TIOCM_DTR
);
312 mutex_unlock(&cs
->mutex
);
317 static int if_tiocmset(struct tty_struct
*tty
, struct file
*file
,
318 unsigned int set
, unsigned int clear
)
320 struct cardstate
*cs
;
324 cs
= (struct cardstate
*) tty
->driver_data
;
326 err("cs==NULL in %s", __func__
);
330 gig_dbg(DEBUG_IF
, "%u: %s(0x%x, 0x%x)",
331 cs
->minor_index
, __func__
, set
, clear
);
333 if (mutex_lock_interruptible(&cs
->mutex
))
334 return -ERESTARTSYS
; // FIXME -EINTR?
336 if (!cs
->connected
) {
337 gig_dbg(DEBUG_ANY
, "can't communicate with unplugged device");
340 mc
= (cs
->control_state
| set
) & ~clear
& (TIOCM_RTS
|TIOCM_DTR
);
341 retval
= cs
->ops
->set_modem_ctrl(cs
, cs
->control_state
, mc
);
342 cs
->control_state
= mc
;
345 mutex_unlock(&cs
->mutex
);
350 static int if_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
352 struct cardstate
*cs
;
353 int retval
= -ENODEV
;
355 cs
= (struct cardstate
*) tty
->driver_data
;
357 err("cs==NULL in %s", __func__
);
361 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
363 if (mutex_lock_interruptible(&cs
->mutex
))
364 return -ERESTARTSYS
; // FIXME -EINTR?
367 warn("%s: device not opened", __func__
);
368 else if (cs
->mstate
!= MS_LOCKED
) {
369 warn("can't write to unlocked device");
371 } else if (!cs
->connected
) {
372 gig_dbg(DEBUG_ANY
, "can't write to unplugged device");
373 retval
= -EBUSY
; //FIXME
375 retval
= cs
->ops
->write_cmd(cs
, buf
, count
,
376 &cs
->if_wake_tasklet
);
379 mutex_unlock(&cs
->mutex
);
384 static int if_write_room(struct tty_struct
*tty
)
386 struct cardstate
*cs
;
387 int retval
= -ENODEV
;
389 cs
= (struct cardstate
*) tty
->driver_data
;
391 err("cs==NULL in %s", __func__
);
395 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
397 if (mutex_lock_interruptible(&cs
->mutex
))
398 return -ERESTARTSYS
; // FIXME -EINTR?
401 warn("%s: device not opened", __func__
);
402 else if (cs
->mstate
!= MS_LOCKED
) {
403 warn("can't write to unlocked device");
405 } else if (!cs
->connected
) {
406 gig_dbg(DEBUG_ANY
, "can't write to unplugged device");
407 retval
= -EBUSY
; //FIXME
409 retval
= cs
->ops
->write_room(cs
);
411 mutex_unlock(&cs
->mutex
);
416 static int if_chars_in_buffer(struct tty_struct
*tty
)
418 struct cardstate
*cs
;
419 int retval
= -ENODEV
;
421 cs
= (struct cardstate
*) tty
->driver_data
;
423 err("cs==NULL in %s", __func__
);
427 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
429 if (mutex_lock_interruptible(&cs
->mutex
))
430 return -ERESTARTSYS
; // FIXME -EINTR?
433 warn("%s: device not opened", __func__
);
434 else if (cs
->mstate
!= MS_LOCKED
) {
435 warn("can't write to unlocked device");
437 } else if (!cs
->connected
) {
438 gig_dbg(DEBUG_ANY
, "can't write to unplugged device");
439 retval
= -EBUSY
; //FIXME
441 retval
= cs
->ops
->chars_in_buffer(cs
);
443 mutex_unlock(&cs
->mutex
);
448 static void if_throttle(struct tty_struct
*tty
)
450 struct cardstate
*cs
;
452 cs
= (struct cardstate
*) tty
->driver_data
;
454 err("cs==NULL in %s", __func__
);
458 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
460 mutex_lock(&cs
->mutex
);
463 warn("%s: device not opened", __func__
);
468 mutex_unlock(&cs
->mutex
);
471 static void if_unthrottle(struct tty_struct
*tty
)
473 struct cardstate
*cs
;
475 cs
= (struct cardstate
*) tty
->driver_data
;
477 err("cs==NULL in %s", __func__
);
481 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
483 mutex_lock(&cs
->mutex
);
486 warn("%s: device not opened", __func__
);
491 mutex_unlock(&cs
->mutex
);
494 static void if_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
496 struct cardstate
*cs
;
499 unsigned int old_cflag
;
500 unsigned int control_state
, new_state
;
502 cs
= (struct cardstate
*) tty
->driver_data
;
504 err("cs==NULL in %s", __func__
);
508 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
510 mutex_lock(&cs
->mutex
);
512 if (!cs
->open_count
) {
513 warn("%s: device not opened", __func__
);
517 if (!cs
->connected
) {
518 gig_dbg(DEBUG_ANY
, "can't communicate with unplugged device");
522 // stolen from mct_u232.c
523 iflag
= tty
->termios
->c_iflag
;
524 cflag
= tty
->termios
->c_cflag
;
525 old_cflag
= old
? old
->c_cflag
: cflag
; //FIXME?
526 gig_dbg(DEBUG_IF
, "%u: iflag %x cflag %x old %x",
527 cs
->minor_index
, iflag
, cflag
, old_cflag
);
529 /* get a local copy of the current port settings */
530 control_state
= cs
->control_state
;
534 * Do not attempt to cache old rates and skip settings,
535 * disconnects screw such tricks up completely.
536 * Premature optimization is the root of all evil.
539 /* reassert DTR and (maybe) RTS on transition from B0 */
540 if ((old_cflag
& CBAUD
) == B0
) {
541 new_state
= control_state
| TIOCM_DTR
;
542 /* don't set RTS if using hardware flow control */
543 if (!(old_cflag
& CRTSCTS
))
544 new_state
|= TIOCM_RTS
;
545 gig_dbg(DEBUG_IF
, "%u: from B0 - set DTR%s",
547 (new_state
& TIOCM_RTS
) ? " only" : "/RTS");
548 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
549 control_state
= new_state
;
552 cs
->ops
->baud_rate(cs
, cflag
& CBAUD
);
554 if ((cflag
& CBAUD
) == B0
) {
555 /* Drop RTS and DTR */
556 gig_dbg(DEBUG_IF
, "%u: to B0 - drop DTR/RTS", cs
->minor_index
);
557 new_state
= control_state
& ~(TIOCM_DTR
| TIOCM_RTS
);
558 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
559 control_state
= new_state
;
563 * Update line control register (LCR)
566 cs
->ops
->set_line_ctrl(cs
, cflag
);
569 //FIXME this hangs M101 [ts 2005-03-09]
570 //FIXME do we need this?
572 * Set flow control: well, I do not really now how to handle DTR/RTS.
573 * Just do what we have seen with SniffUSB on Win98.
575 /* Drop DTR/RTS if no flow control otherwise assert */
576 gig_dbg(DEBUG_IF
, "%u: control_state %x",
577 cs
->minor_index
, control_state
);
578 new_state
= control_state
;
579 if ((iflag
& IXOFF
) || (iflag
& IXON
) || (cflag
& CRTSCTS
))
580 new_state
|= TIOCM_DTR
| TIOCM_RTS
;
582 new_state
&= ~(TIOCM_DTR
| TIOCM_RTS
);
583 if (new_state
!= control_state
) {
584 gig_dbg(DEBUG_IF
, "%u: new_state %x",
585 cs
->minor_index
, new_state
);
586 gigaset_set_modem_ctrl(cs
, control_state
, new_state
);
587 control_state
= new_state
;
591 /* save off the modified port settings */
592 cs
->control_state
= control_state
;
595 mutex_unlock(&cs
->mutex
);
599 /* wakeup tasklet for the write operation */
600 static void if_wake(unsigned long data
)
602 struct cardstate
*cs
= (struct cardstate
*) data
;
608 /*** interface to common ***/
610 void gigaset_if_init(struct cardstate
*cs
)
612 struct gigaset_driver
*drv
;
618 tasklet_init(&cs
->if_wake_tasklet
, &if_wake
, (unsigned long) cs
);
620 mutex_lock(&cs
->mutex
);
621 cs
->tty_dev
= tty_register_device(drv
->tty
, cs
->minor_index
, NULL
);
623 if (!IS_ERR(cs
->tty_dev
))
624 dev_set_drvdata(cs
->tty_dev
, cs
);
626 warn("could not register device to the tty subsystem");
629 mutex_unlock(&cs
->mutex
);
632 void gigaset_if_free(struct cardstate
*cs
)
634 struct gigaset_driver
*drv
;
640 tasklet_disable(&cs
->if_wake_tasklet
);
641 tasklet_kill(&cs
->if_wake_tasklet
);
643 tty_unregister_device(drv
->tty
, cs
->minor_index
);
646 void gigaset_if_receive(struct cardstate
*cs
,
647 unsigned char *buffer
, size_t len
)
650 struct tty_struct
*tty
;
652 spin_lock_irqsave(&cs
->lock
, flags
);
653 if ((tty
= cs
->tty
) == NULL
)
654 gig_dbg(DEBUG_ANY
, "receive on closed device");
656 tty_buffer_request_room(tty
, len
);
657 tty_insert_flip_string(tty
, buffer
, len
);
658 tty_flip_buffer_push(tty
);
660 spin_unlock_irqrestore(&cs
->lock
, flags
);
662 EXPORT_SYMBOL_GPL(gigaset_if_receive
);
664 /* gigaset_if_initdriver
665 * Initialize tty interface.
668 * procname Name of the driver (e.g. for /proc/tty/drivers)
669 * devname Name of the device files (prefix without minor number)
671 void gigaset_if_initdriver(struct gigaset_driver
*drv
, const char *procname
,
674 unsigned minors
= drv
->minors
;
676 struct tty_driver
*tty
;
680 if ((drv
->tty
= alloc_tty_driver(minors
)) == NULL
)
684 tty
->magic
= TTY_DRIVER_MAGIC
,
685 tty
->major
= GIG_MAJOR
,
686 tty
->type
= TTY_DRIVER_TYPE_SERIAL
,
687 tty
->subtype
= SERIAL_TYPE_NORMAL
,
688 tty
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
690 tty
->driver_name
= procname
;
692 tty
->minor_start
= drv
->minor
;
693 tty
->num
= drv
->minors
;
695 tty
->owner
= THIS_MODULE
;
697 tty
->init_termios
= tty_std_termios
; //FIXME
698 tty
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
; //FIXME
699 tty_set_operations(tty
, &if_ops
);
701 ret
= tty_register_driver(tty
);
703 warn("failed to register tty driver (error %d)", ret
);
706 gig_dbg(DEBUG_IF
, "tty driver initialized");
711 warn("could not allocate tty structures");
714 put_tty_driver(drv
->tty
);
717 void gigaset_if_freedriver(struct gigaset_driver
*drv
)
723 tty_unregister_driver(drv
->tty
);
724 put_tty_driver(drv
->tty
);