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>
20 static int if_lock(struct cardstate
*cs
, int *arg
)
24 gig_dbg(DEBUG_IF
, "%u: if_lock (%d)", cs
->minor_index
, cmd
);
30 *arg
= cs
->mstate
== MS_LOCKED
;
34 if (!cmd
&& cs
->mstate
== MS_LOCKED
&& cs
->connected
) {
35 cs
->ops
->set_modem_ctrl(cs
, 0, TIOCM_DTR
|TIOCM_RTS
);
36 cs
->ops
->baud_rate(cs
, B115200
);
37 cs
->ops
->set_line_ctrl(cs
, CS8
);
38 cs
->control_state
= TIOCM_DTR
|TIOCM_RTS
;
42 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_IF_LOCK
,
47 gigaset_schedule_event(cs
);
49 wait_event(cs
->waitqueue
, !cs
->waiting
);
51 if (cs
->cmd_result
>= 0) {
52 *arg
= cs
->cmd_result
;
56 return cs
->cmd_result
;
59 static int if_version(struct cardstate
*cs
, unsigned arg
[4])
61 static const unsigned version
[4] = GIG_VERSION
;
62 static const unsigned compat
[4] = GIG_COMPAT
;
63 unsigned cmd
= arg
[0];
65 gig_dbg(DEBUG_IF
, "%u: if_version (%d)", cs
->minor_index
, cmd
);
69 memcpy(arg
, version
, sizeof version
);
72 memcpy(arg
, compat
, sizeof compat
);
76 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_IF_VER
,
81 gigaset_schedule_event(cs
);
83 wait_event(cs
->waitqueue
, !cs
->waiting
);
85 if (cs
->cmd_result
>= 0)
88 return cs
->cmd_result
;
94 static int if_config(struct cardstate
*cs
, int *arg
)
96 gig_dbg(DEBUG_IF
, "%u: if_config (%d)", cs
->minor_index
, *arg
);
101 if (cs
->mstate
!= MS_LOCKED
)
104 if (!cs
->connected
) {
105 pr_err("%s: not connected\n", __func__
);
110 return gigaset_enterconfigmode(cs
);
113 /*** the terminal driver ***/
114 /* stolen from usbserial and some other tty drivers */
116 static int if_open(struct tty_struct
*tty
, struct file
*filp
);
117 static void if_close(struct tty_struct
*tty
, struct file
*filp
);
118 static int if_ioctl(struct tty_struct
*tty
, struct file
*file
,
119 unsigned int cmd
, unsigned long arg
);
120 static int if_write_room(struct tty_struct
*tty
);
121 static int if_chars_in_buffer(struct tty_struct
*tty
);
122 static void if_throttle(struct tty_struct
*tty
);
123 static void if_unthrottle(struct tty_struct
*tty
);
124 static void if_set_termios(struct tty_struct
*tty
, struct ktermios
*old
);
125 static int if_tiocmget(struct tty_struct
*tty
, struct file
*file
);
126 static int if_tiocmset(struct tty_struct
*tty
, struct file
*file
,
127 unsigned int set
, unsigned int clear
);
128 static int if_write(struct tty_struct
*tty
,
129 const unsigned char *buf
, int count
);
131 static const struct tty_operations if_ops
= {
136 .write_room
= if_write_room
,
137 .chars_in_buffer
= if_chars_in_buffer
,
138 .set_termios
= if_set_termios
,
139 .throttle
= if_throttle
,
140 .unthrottle
= if_unthrottle
,
141 .tiocmget
= if_tiocmget
,
142 .tiocmset
= if_tiocmset
,
145 static int if_open(struct tty_struct
*tty
, struct file
*filp
)
147 struct cardstate
*cs
;
150 gig_dbg(DEBUG_IF
, "%d+%d: %s()",
151 tty
->driver
->minor_start
, tty
->index
, __func__
);
153 tty
->driver_data
= NULL
;
155 cs
= gigaset_get_cs_by_tty(tty
);
156 if (!cs
|| !try_module_get(cs
->driver
->owner
))
159 if (mutex_lock_interruptible(&cs
->mutex
))
161 tty
->driver_data
= cs
;
165 if (cs
->open_count
== 1) {
166 spin_lock_irqsave(&cs
->lock
, flags
);
168 spin_unlock_irqrestore(&cs
->lock
, flags
);
169 tty
->low_latency
= 1;
172 mutex_unlock(&cs
->mutex
);
176 static void if_close(struct tty_struct
*tty
, struct file
*filp
)
178 struct cardstate
*cs
;
181 cs
= (struct cardstate
*) tty
->driver_data
;
183 pr_err("%s: no cardstate\n", __func__
);
187 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
189 mutex_lock(&cs
->mutex
);
192 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
193 else if (!cs
->open_count
)
194 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
196 if (!--cs
->open_count
) {
197 spin_lock_irqsave(&cs
->lock
, flags
);
199 spin_unlock_irqrestore(&cs
->lock
, flags
);
203 mutex_unlock(&cs
->mutex
);
205 module_put(cs
->driver
->owner
);
208 static int if_ioctl(struct tty_struct
*tty
, struct file
*file
,
209 unsigned int cmd
, unsigned long arg
)
211 struct cardstate
*cs
;
212 int retval
= -ENODEV
;
214 unsigned char buf
[6];
217 cs
= (struct cardstate
*) tty
->driver_data
;
219 pr_err("%s: no cardstate\n", __func__
);
223 gig_dbg(DEBUG_IF
, "%u: %s(0x%x)", cs
->minor_index
, __func__
, cmd
);
225 if (mutex_lock_interruptible(&cs
->mutex
))
228 if (!cs
->connected
) {
229 gig_dbg(DEBUG_IF
, "not connected");
231 } else if (!cs
->open_count
)
232 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
237 retval
= get_user(int_arg
, (int __user
*) arg
);
239 retval
= if_lock(cs
, &int_arg
);
241 retval
= put_user(int_arg
, (int __user
*) arg
);
244 retval
= get_user(int_arg
, (int __user
*) arg
);
246 retval
= if_config(cs
, &int_arg
);
248 retval
= put_user(int_arg
, (int __user
*) arg
);
250 case GIGASET_BRKCHARS
:
251 retval
= copy_from_user(&buf
,
252 (const unsigned char __user
*) arg
, 6)
255 gigaset_dbg_buffer(DEBUG_IF
, "GIGASET_BRKCHARS",
256 6, (const unsigned char *) arg
);
257 retval
= cs
->ops
->brkchars(cs
, buf
);
260 case GIGASET_VERSION
:
261 retval
= copy_from_user(version
,
262 (unsigned __user
*) arg
, sizeof version
)
265 retval
= if_version(cs
, version
);
267 retval
= copy_to_user((unsigned __user
*) arg
,
268 version
, sizeof version
)
272 gig_dbg(DEBUG_IF
, "%s: arg not supported - 0x%04x",
274 retval
= -ENOIOCTLCMD
;
278 mutex_unlock(&cs
->mutex
);
283 static int if_tiocmget(struct tty_struct
*tty
, struct file
*file
)
285 struct cardstate
*cs
;
288 cs
= (struct cardstate
*) tty
->driver_data
;
290 pr_err("%s: no cardstate\n", __func__
);
294 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
296 if (mutex_lock_interruptible(&cs
->mutex
))
299 retval
= cs
->control_state
& (TIOCM_RTS
|TIOCM_DTR
);
301 mutex_unlock(&cs
->mutex
);
306 static int if_tiocmset(struct tty_struct
*tty
, struct file
*file
,
307 unsigned int set
, unsigned int clear
)
309 struct cardstate
*cs
;
313 cs
= (struct cardstate
*) tty
->driver_data
;
315 pr_err("%s: no cardstate\n", __func__
);
319 gig_dbg(DEBUG_IF
, "%u: %s(0x%x, 0x%x)",
320 cs
->minor_index
, __func__
, set
, clear
);
322 if (mutex_lock_interruptible(&cs
->mutex
))
325 if (!cs
->connected
) {
326 gig_dbg(DEBUG_IF
, "not connected");
329 mc
= (cs
->control_state
| set
) & ~clear
& (TIOCM_RTS
|TIOCM_DTR
);
330 retval
= cs
->ops
->set_modem_ctrl(cs
, cs
->control_state
, mc
);
331 cs
->control_state
= mc
;
334 mutex_unlock(&cs
->mutex
);
339 static int if_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
341 struct cardstate
*cs
;
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");
361 if (!cs
->open_count
) {
362 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
366 if (cs
->mstate
!= MS_LOCKED
) {
367 dev_warn(cs
->dev
, "can't write to unlocked device\n");
377 cb
= kmalloc(sizeof(struct cmdbuf_t
) + count
, GFP_KERNEL
);
379 dev_err(cs
->dev
, "%s: out of memory\n", __func__
);
384 memcpy(cb
->buf
, buf
, count
);
388 cb
->wake_tasklet
= &cs
->if_wake_tasklet
;
389 retval
= cs
->ops
->write_cmd(cs
, cb
);
391 mutex_unlock(&cs
->mutex
);
395 static int if_write_room(struct tty_struct
*tty
)
397 struct cardstate
*cs
;
398 int retval
= -ENODEV
;
400 cs
= (struct cardstate
*) tty
->driver_data
;
402 pr_err("%s: no cardstate\n", __func__
);
406 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
408 if (mutex_lock_interruptible(&cs
->mutex
))
411 if (!cs
->connected
) {
412 gig_dbg(DEBUG_IF
, "not connected");
414 } else if (!cs
->open_count
)
415 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
416 else if (cs
->mstate
!= MS_LOCKED
) {
417 dev_warn(cs
->dev
, "can't write to unlocked device\n");
420 retval
= cs
->ops
->write_room(cs
);
422 mutex_unlock(&cs
->mutex
);
427 static int if_chars_in_buffer(struct tty_struct
*tty
)
429 struct cardstate
*cs
;
432 cs
= (struct cardstate
*) tty
->driver_data
;
434 pr_err("%s: no cardstate\n", __func__
);
438 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
440 mutex_lock(&cs
->mutex
);
443 gig_dbg(DEBUG_IF
, "not connected");
444 else if (!cs
->open_count
)
445 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
446 else if (cs
->mstate
!= MS_LOCKED
)
447 dev_warn(cs
->dev
, "can't write to unlocked device\n");
449 retval
= cs
->ops
->chars_in_buffer(cs
);
451 mutex_unlock(&cs
->mutex
);
456 static void if_throttle(struct tty_struct
*tty
)
458 struct cardstate
*cs
;
460 cs
= (struct cardstate
*) tty
->driver_data
;
462 pr_err("%s: no cardstate\n", __func__
);
466 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
468 mutex_lock(&cs
->mutex
);
471 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
472 else if (!cs
->open_count
)
473 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
475 gig_dbg(DEBUG_IF
, "%s: not implemented\n", __func__
);
477 mutex_unlock(&cs
->mutex
);
480 static void if_unthrottle(struct tty_struct
*tty
)
482 struct cardstate
*cs
;
484 cs
= (struct cardstate
*) tty
->driver_data
;
486 pr_err("%s: no cardstate\n", __func__
);
490 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
492 mutex_lock(&cs
->mutex
);
495 gig_dbg(DEBUG_IF
, "not connected"); /* nothing to do */
496 else if (!cs
->open_count
)
497 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
499 gig_dbg(DEBUG_IF
, "%s: not implemented\n", __func__
);
501 mutex_unlock(&cs
->mutex
);
504 static void if_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
506 struct cardstate
*cs
;
509 unsigned int old_cflag
;
510 unsigned int control_state
, new_state
;
512 cs
= (struct cardstate
*) tty
->driver_data
;
514 pr_err("%s: no cardstate\n", __func__
);
518 gig_dbg(DEBUG_IF
, "%u: %s()", cs
->minor_index
, __func__
);
520 mutex_lock(&cs
->mutex
);
522 if (!cs
->connected
) {
523 gig_dbg(DEBUG_IF
, "not connected");
527 if (!cs
->open_count
) {
528 dev_warn(cs
->dev
, "%s: device not opened\n", __func__
);
532 iflag
= tty
->termios
->c_iflag
;
533 cflag
= tty
->termios
->c_cflag
;
534 old_cflag
= old
? old
->c_cflag
: cflag
;
535 gig_dbg(DEBUG_IF
, "%u: iflag %x cflag %x old %x",
536 cs
->minor_index
, iflag
, cflag
, old_cflag
);
538 /* get a local copy of the current port settings */
539 control_state
= cs
->control_state
;
543 * Do not attempt to cache old rates and skip settings,
544 * disconnects screw such tricks up completely.
545 * Premature optimization is the root of all evil.
548 /* reassert DTR and (maybe) RTS on transition from B0 */
549 if ((old_cflag
& CBAUD
) == B0
) {
550 new_state
= control_state
| TIOCM_DTR
;
551 /* don't set RTS if using hardware flow control */
552 if (!(old_cflag
& CRTSCTS
))
553 new_state
|= TIOCM_RTS
;
554 gig_dbg(DEBUG_IF
, "%u: from B0 - set DTR%s",
556 (new_state
& TIOCM_RTS
) ? " only" : "/RTS");
557 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
558 control_state
= new_state
;
561 cs
->ops
->baud_rate(cs
, cflag
& CBAUD
);
563 if ((cflag
& CBAUD
) == B0
) {
564 /* Drop RTS and DTR */
565 gig_dbg(DEBUG_IF
, "%u: to B0 - drop DTR/RTS", cs
->minor_index
);
566 new_state
= control_state
& ~(TIOCM_DTR
| TIOCM_RTS
);
567 cs
->ops
->set_modem_ctrl(cs
, control_state
, new_state
);
568 control_state
= new_state
;
572 * Update line control register (LCR)
575 cs
->ops
->set_line_ctrl(cs
, cflag
);
577 /* save off the modified port settings */
578 cs
->control_state
= control_state
;
581 mutex_unlock(&cs
->mutex
);
585 /* wakeup tasklet for the write operation */
586 static void if_wake(unsigned long data
)
588 struct cardstate
*cs
= (struct cardstate
*) data
;
594 /*** interface to common ***/
596 void gigaset_if_init(struct cardstate
*cs
)
598 struct gigaset_driver
*drv
;
604 tasklet_init(&cs
->if_wake_tasklet
, if_wake
, (unsigned long) cs
);
606 mutex_lock(&cs
->mutex
);
607 cs
->tty_dev
= tty_register_device(drv
->tty
, cs
->minor_index
, NULL
);
609 if (!IS_ERR(cs
->tty_dev
))
610 dev_set_drvdata(cs
->tty_dev
, cs
);
612 pr_warning("could not register device to the tty subsystem\n");
615 mutex_unlock(&cs
->mutex
);
618 void gigaset_if_free(struct cardstate
*cs
)
620 struct gigaset_driver
*drv
;
626 tasklet_disable(&cs
->if_wake_tasklet
);
627 tasklet_kill(&cs
->if_wake_tasklet
);
629 tty_unregister_device(drv
->tty
, cs
->minor_index
);
633 * gigaset_if_receive() - pass a received block of data to the tty device
634 * @cs: device descriptor structure.
635 * @buffer: received data.
636 * @len: number of bytes received.
638 * Called by asyncdata/isocdata if a block of data received from the
639 * device must be sent to userspace through the ttyG* device.
641 void gigaset_if_receive(struct cardstate
*cs
,
642 unsigned char *buffer
, size_t len
)
645 struct tty_struct
*tty
;
647 spin_lock_irqsave(&cs
->lock
, flags
);
650 gig_dbg(DEBUG_IF
, "receive on closed device");
652 tty_insert_flip_string(tty
, buffer
, len
);
653 tty_flip_buffer_push(tty
);
655 spin_unlock_irqrestore(&cs
->lock
, flags
);
657 EXPORT_SYMBOL_GPL(gigaset_if_receive
);
659 /* gigaset_if_initdriver
660 * Initialize tty interface.
663 * procname Name of the driver (e.g. for /proc/tty/drivers)
664 * devname Name of the device files (prefix without minor number)
666 void gigaset_if_initdriver(struct gigaset_driver
*drv
, const char *procname
,
669 unsigned minors
= drv
->minors
;
671 struct tty_driver
*tty
;
675 drv
->tty
= tty
= alloc_tty_driver(minors
);
679 tty
->magic
= TTY_DRIVER_MAGIC
,
680 tty
->type
= TTY_DRIVER_TYPE_SERIAL
,
681 tty
->subtype
= SERIAL_TYPE_NORMAL
,
682 tty
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
684 tty
->driver_name
= procname
;
686 tty
->minor_start
= drv
->minor
;
687 tty
->num
= drv
->minors
;
689 tty
->owner
= THIS_MODULE
;
691 tty
->init_termios
= tty_std_termios
;
692 tty
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
693 tty_set_operations(tty
, &if_ops
);
695 ret
= tty_register_driver(tty
);
697 pr_err("error %d registering tty driver\n", ret
);
700 gig_dbg(DEBUG_IF
, "tty driver initialized");
705 pr_err("out of memory\n");
708 put_tty_driver(drv
->tty
);
711 void gigaset_if_freedriver(struct gigaset_driver
*drv
)
717 tty_unregister_driver(drv
->tty
);
718 put_tty_driver(drv
->tty
);
This page took 0.048684 seconds and 5 git commands to generate.