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>
17 #include <linux/module.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
,
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
);
127 static int if_tiocmset(struct tty_struct
*tty
,
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
;
150 gig_dbg(DEBUG_IF
, "%d+%d: %s()",
151 tty
->driver
->minor_start
, tty
->index
, __func__
);
153 cs
= gigaset_get_cs_by_tty(tty
);
154 if (!cs
|| !try_module_get(cs
->driver
->owner
))
157 if (mutex_lock_interruptible(&cs
->mutex
)) {
158 module_put(cs
->driver
->owner
);
161 tty
->driver_data
= cs
;
165 if (cs
->port
.count
== 1) {
166 tty_port_tty_set(&cs
->port
, tty
);
167 tty
->low_latency
= 1;
170 mutex_unlock(&cs
->mutex
);
174 static void if_close(struct tty_struct
*tty
, struct file
*filp
)
176 struct cardstate
*cs
= tty
->driver_data
;
178 if (!cs
) { /* happens if we didn't find cs in open */
179 gig_dbg(DEBUG_IF
, "%s: no cardstate", __func__
);
183 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
185 mutex_lock(&cs
->mutex
);
188 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
189 else if (!cs
->port
.count
)
190 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
191 else if (!--cs
->port
.count
)
192 tty_port_tty_set(&cs
->port
, NULL
);
194 mutex_unlock(&cs
->mutex
);
196 module_put(cs
->driver
->owner
);
199 static int if_ioctl(struct tty_struct
*tty
,
200 unsigned int cmd
, unsigned long arg
)
202 struct cardstate
*cs
= tty
->driver_data
;
203 int retval
= -ENODEV
;
205 unsigned char buf
[6];
208 gig_dbg(DEBUG_IF
, "%u: %s(0x%x)", cs
->minor_index
, __func__
, cmd
);
210 if (mutex_lock_interruptible(&cs
->mutex
))
213 if (!cs
->connected
) {
214 gig_dbg(DEBUG_IF
, "not connected");
220 retval
= get_user(int_arg
, (int __user
*) arg
);
222 retval
= if_lock(cs
, &int_arg
);
224 retval
= put_user(int_arg
, (int __user
*) arg
);
227 retval
= get_user(int_arg
, (int __user
*) arg
);
229 retval
= if_config(cs
, &int_arg
);
231 retval
= put_user(int_arg
, (int __user
*) arg
);
233 case GIGASET_BRKCHARS
:
234 retval
= copy_from_user(&buf
,
235 (const unsigned char __user
*) arg
, 6)
238 gigaset_dbg_buffer(DEBUG_IF
, "GIGASET_BRKCHARS",
239 6, (const unsigned char *) arg
);
240 retval
= cs
->ops
->brkchars(cs
, buf
);
243 case GIGASET_VERSION
:
244 retval
= copy_from_user(version
,
245 (unsigned __user
*) arg
, sizeof version
)
248 retval
= if_version(cs
, version
);
250 retval
= copy_to_user((unsigned __user
*) arg
,
251 version
, sizeof version
)
255 gig_dbg(DEBUG_IF
, "%s: arg not supported - 0x%04x",
257 retval
= -ENOIOCTLCMD
;
261 mutex_unlock(&cs
->mutex
);
266 static int if_tiocmget(struct tty_struct
*tty
)
268 struct cardstate
*cs
= tty
->driver_data
;
271 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
273 if (mutex_lock_interruptible(&cs
->mutex
))
276 retval
= cs
->control_state
& (TIOCM_RTS
| TIOCM_DTR
);
278 mutex_unlock(&cs
->mutex
);
283 static int if_tiocmset(struct tty_struct
*tty
,
284 unsigned int set
, unsigned int clear
)
286 struct cardstate
*cs
= tty
->driver_data
;
290 gig_dbg(DEBUG_IF
, "%u: %s(0x%x, 0x%x)",
291 cs
->minor_index
, __func__
, set
, clear
);
293 if (mutex_lock_interruptible(&cs
->mutex
))
296 if (!cs
->connected
) {
297 gig_dbg(DEBUG_IF
, "not connected");
300 mc
= (cs
->control_state
| set
) & ~clear
& (TIOCM_RTS
| TIOCM_DTR
);
301 retval
= cs
->ops
->set_modem_ctrl(cs
, cs
->control_state
, mc
);
302 cs
->control_state
= mc
;
305 mutex_unlock(&cs
->mutex
);
310 static int if_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
312 struct cardstate
*cs
= tty
->driver_data
;
316 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
318 if (mutex_lock_interruptible(&cs
->mutex
))
321 if (!cs
->connected
) {
322 gig_dbg(DEBUG_IF
, "not connected");
326 if (cs
->mstate
!= MS_LOCKED
) {
327 dev_warn(cs
->dev
, "can't write to unlocked device\n");
337 cb
= kmalloc(sizeof(struct cmdbuf_t
) + count
, GFP_KERNEL
);
339 dev_err(cs
->dev
, "%s: out of memory\n", __func__
);
344 memcpy(cb
->buf
, buf
, count
);
348 cb
->wake_tasklet
= &cs
->if_wake_tasklet
;
349 retval
= cs
->ops
->write_cmd(cs
, cb
);
351 mutex_unlock(&cs
->mutex
);
355 static int if_write_room(struct tty_struct
*tty
)
357 struct cardstate
*cs
= tty
->driver_data
;
358 int retval
= -ENODEV
;
360 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
362 if (mutex_lock_interruptible(&cs
->mutex
))
365 if (!cs
->connected
) {
366 gig_dbg(DEBUG_IF
, "not connected");
368 } else if (cs
->mstate
!= MS_LOCKED
) {
369 dev_warn(cs
->dev
, "can't write to unlocked device\n");
372 retval
= cs
->ops
->write_room(cs
);
374 mutex_unlock(&cs
->mutex
);
379 static int if_chars_in_buffer(struct tty_struct
*tty
)
381 struct cardstate
*cs
= tty
->driver_data
;
384 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
386 mutex_lock(&cs
->mutex
);
389 gig_dbg(DEBUG_IF
, "not connected");
390 else if (cs
->mstate
!= MS_LOCKED
)
391 dev_warn(cs
->dev
, "can't write to unlocked device\n");
393 retval
= cs
->ops
->chars_in_buffer(cs
);
395 mutex_unlock(&cs
->mutex
);
400 static void if_throttle(struct tty_struct
*tty
)
402 struct cardstate
*cs
= tty
->driver_data
;
404 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
406 mutex_lock(&cs
->mutex
);
409 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
411 gig_dbg(DEBUG_IF
, "%s: not implemented\n", __func__
);
413 mutex_unlock(&cs
->mutex
);
416 static void if_unthrottle(struct tty_struct
*tty
)
418 struct cardstate
*cs
= tty
->driver_data
;
420 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
422 mutex_lock(&cs
->mutex
);
425 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
427 gig_dbg(DEBUG_IF
, "%s: not implemented\n", __func__
);
429 mutex_unlock(&cs
->mutex
);
432 static void if_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
434 struct cardstate
*cs
= tty
->driver_data
;
437 unsigned int old_cflag
;
438 unsigned int control_state
, new_state
;
440 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
442 mutex_lock(&cs
->mutex
);
444 if (!cs
->connected
) {
445 gig_dbg(DEBUG_IF
, "not connected");
449 iflag
= tty
->termios
->c_iflag
;
450 cflag
= tty
->termios
->c_cflag
;
451 old_cflag
= old
? old
->c_cflag
: cflag
;
452 gig_dbg(DEBUG_IF
, "%u: iflag %x cflag %x old %x",
453 cs
->minor_index
, iflag
, cflag
, old_cflag
);
455 /* get a local copy of the current port settings */
456 control_state
= cs
->control_state
;
460 * Do not attempt to cache old rates and skip settings,
461 * disconnects screw such tricks up completely.
462 * Premature optimization is the root of all evil.
465 /* reassert DTR and (maybe) RTS on transition from B0 */
466 if ((old_cflag
& CBAUD
) == B0
) {
467 new_state
= control_state
| TIOCM_DTR
;
468 /* don't set RTS if using hardware flow control */
469 if (!(old_cflag
& CRTSCTS
))
470 new_state
|= TIOCM_RTS
;
471 gig_dbg(DEBUG_IF
, "%u: from B0 - set DTR%s",
473 (new_state
& TIOCM_RTS
) ? " only" : "/RTS");
474 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
475 control_state
= new_state
;
478 cs
->ops
->baud_rate(cs
, cflag
& CBAUD
);
480 if ((cflag
& CBAUD
) == B0
) {
481 /* Drop RTS and DTR */
482 gig_dbg(DEBUG_IF
, "%u: to B0 - drop DTR/RTS", cs
->minor_index
);
483 new_state
= control_state
& ~(TIOCM_DTR
| TIOCM_RTS
);
484 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
485 control_state
= new_state
;
489 * Update line control register (LCR)
492 cs
->ops
->set_line_ctrl(cs
, cflag
);
494 /* save off the modified port settings */
495 cs
->control_state
= control_state
;
498 mutex_unlock(&cs
->mutex
);
502 /* wakeup tasklet for the write operation */
503 static void if_wake(unsigned long data
)
505 struct cardstate
*cs
= (struct cardstate
*)data
;
506 struct tty_struct
*tty
= tty_port_tty_get(&cs
->port
);
514 /*** interface to common ***/
516 void gigaset_if_init(struct cardstate
*cs
)
518 struct gigaset_driver
*drv
;
524 tasklet_init(&cs
->if_wake_tasklet
, if_wake
, (unsigned long) cs
);
526 mutex_lock(&cs
->mutex
);
527 cs
->tty_dev
= tty_register_device(drv
->tty
, cs
->minor_index
, NULL
);
529 if (!IS_ERR(cs
->tty_dev
))
530 dev_set_drvdata(cs
->tty_dev
, cs
);
532 pr_warning("could not register device to the tty subsystem\n");
535 mutex_unlock(&cs
->mutex
);
538 void gigaset_if_free(struct cardstate
*cs
)
540 struct gigaset_driver
*drv
;
546 tasklet_disable(&cs
->if_wake_tasklet
);
547 tasklet_kill(&cs
->if_wake_tasklet
);
549 tty_unregister_device(drv
->tty
, cs
->minor_index
);
553 * gigaset_if_receive() - pass a received block of data to the tty device
554 * @cs: device descriptor structure.
555 * @buffer: received data.
556 * @len: number of bytes received.
558 * Called by asyncdata/isocdata if a block of data received from the
559 * device must be sent to userspace through the ttyG* device.
561 void gigaset_if_receive(struct cardstate
*cs
,
562 unsigned char *buffer
, size_t len
)
564 struct tty_struct
*tty
= tty_port_tty_get(&cs
->port
);
567 gig_dbg(DEBUG_IF
, "receive on closed device");
571 tty_insert_flip_string(tty
, buffer
, len
);
572 tty_flip_buffer_push(tty
);
575 EXPORT_SYMBOL_GPL(gigaset_if_receive
);
577 /* gigaset_if_initdriver
578 * Initialize tty interface.
581 * procname Name of the driver (e.g. for /proc/tty/drivers)
582 * devname Name of the device files (prefix without minor number)
584 void gigaset_if_initdriver(struct gigaset_driver
*drv
, const char *procname
,
588 struct tty_driver
*tty
;
592 drv
->tty
= tty
= alloc_tty_driver(drv
->minors
);
596 tty
->type
= TTY_DRIVER_TYPE_SERIAL
;
597 tty
->subtype
= SERIAL_TYPE_NORMAL
;
598 tty
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
600 tty
->driver_name
= procname
;
602 tty
->minor_start
= drv
->minor
;
604 tty
->init_termios
= tty_std_termios
;
605 tty
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
606 tty_set_operations(tty
, &if_ops
);
608 ret
= tty_register_driver(tty
);
610 pr_err("error %d registering tty driver\n", ret
);
613 gig_dbg(DEBUG_IF
, "tty driver initialized");
618 pr_err("out of memory\n");
621 put_tty_driver(drv
->tty
);
624 void gigaset_if_freedriver(struct gigaset_driver
*drv
)
630 tty_unregister_driver(drv
->tty
);
631 put_tty_driver(drv
->tty
);