1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * interface to user space for the gigaset driver
5 * Copyright (c) 2004 by Hansjoerg Lipp <hjlipp@web.de>
7 * =====================================================================
8 * =====================================================================
12 #include <linux/gigaset_dev.h>
13 #include <linux/tty_flip.h>
14 #include <linux/module.h>
18 static int if_lock(struct cardstate *cs, int *arg)
22 gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
28 *arg = cs->mstate == MS_LOCKED;
32 if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {
33 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS);
34 cs->ops->baud_rate(cs, B115200);
35 cs->ops->set_line_ctrl(cs, CS8);
36 cs->control_state = TIOCM_DTR | TIOCM_RTS;
40 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
45 gigaset_schedule_event(cs);
47 wait_event(cs->waitqueue, !cs->waiting);
49 if (cs->cmd_result >= 0) {
50 *arg = cs->cmd_result;
54 return cs->cmd_result;
57 static int if_version(struct cardstate *cs, unsigned arg[4])
59 static const unsigned version[4] = GIG_VERSION;
60 static const unsigned compat[4] = GIG_COMPAT;
61 unsigned cmd = arg[0];
63 gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
67 memcpy(arg, version, sizeof version);
70 memcpy(arg, compat, sizeof compat);
74 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
79 gigaset_schedule_event(cs);
81 wait_event(cs->waitqueue, !cs->waiting);
83 if (cs->cmd_result >= 0)
86 return cs->cmd_result;
92 static int if_config(struct cardstate *cs, int *arg)
94 gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
99 if (cs->mstate != MS_LOCKED)
102 if (!cs->connected) {
103 pr_err("%s: not connected\n", __func__);
108 return gigaset_enterconfigmode(cs);
111 /*** the terminal driver ***/
113 static int if_open(struct tty_struct *tty, struct file *filp)
115 struct cardstate *cs;
117 gig_dbg(DEBUG_IF, "%d+%d: %s()",
118 tty->driver->minor_start, tty->index, __func__);
120 cs = gigaset_get_cs_by_tty(tty);
121 if (!cs || !try_module_get(cs->driver->owner))
124 if (mutex_lock_interruptible(&cs->mutex)) {
125 module_put(cs->driver->owner);
128 tty->driver_data = cs;
132 if (cs->port.count == 1) {
133 tty_port_tty_set(&cs->port, tty);
134 cs->port.low_latency = 1;
137 mutex_unlock(&cs->mutex);
141 static void if_close(struct tty_struct *tty, struct file *filp)
143 struct cardstate *cs = tty->driver_data;
145 if (!cs) { /* happens if we didn't find cs in open */
146 gig_dbg(DEBUG_IF, "%s: no cardstate", __func__);
150 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
152 mutex_lock(&cs->mutex);
155 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
156 else if (!cs->port.count)
157 dev_warn(cs->dev, "%s: device not opened\n", __func__);
158 else if (!--cs->port.count)
159 tty_port_tty_set(&cs->port, NULL);
161 mutex_unlock(&cs->mutex);
163 module_put(cs->driver->owner);
166 static int if_ioctl(struct tty_struct *tty,
167 unsigned int cmd, unsigned long arg)
169 struct cardstate *cs = tty->driver_data;
170 int retval = -ENODEV;
172 unsigned char buf[6];
175 gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
177 if (mutex_lock_interruptible(&cs->mutex))
180 if (!cs->connected) {
181 gig_dbg(DEBUG_IF, "not connected");
187 retval = get_user(int_arg, (int __user *) arg);
189 retval = if_lock(cs, &int_arg);
191 retval = put_user(int_arg, (int __user *) arg);
194 retval = get_user(int_arg, (int __user *) arg);
196 retval = if_config(cs, &int_arg);
198 retval = put_user(int_arg, (int __user *) arg);
200 case GIGASET_BRKCHARS:
201 retval = copy_from_user(&buf,
202 (const unsigned char __user *) arg, 6)
205 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
207 retval = cs->ops->brkchars(cs, buf);
210 case GIGASET_VERSION:
211 retval = copy_from_user(version,
212 (unsigned __user *) arg, sizeof version)
215 retval = if_version(cs, version);
217 retval = copy_to_user((unsigned __user *) arg,
218 version, sizeof version)
222 gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",
224 retval = -ENOIOCTLCMD;
228 mutex_unlock(&cs->mutex);
234 static long if_compat_ioctl(struct tty_struct *tty,
235 unsigned int cmd, unsigned long arg)
237 return if_ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
241 static int if_tiocmget(struct tty_struct *tty)
243 struct cardstate *cs = tty->driver_data;
246 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
248 if (mutex_lock_interruptible(&cs->mutex))
251 retval = cs->control_state & (TIOCM_RTS | TIOCM_DTR);
253 mutex_unlock(&cs->mutex);
258 static int if_tiocmset(struct tty_struct *tty,
259 unsigned int set, unsigned int clear)
261 struct cardstate *cs = tty->driver_data;
265 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
266 cs->minor_index, __func__, set, clear);
268 if (mutex_lock_interruptible(&cs->mutex))
271 if (!cs->connected) {
272 gig_dbg(DEBUG_IF, "not connected");
275 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS | TIOCM_DTR);
276 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
277 cs->control_state = mc;
280 mutex_unlock(&cs->mutex);
285 static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
287 struct cardstate *cs = tty->driver_data;
291 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
293 if (mutex_lock_interruptible(&cs->mutex))
296 if (!cs->connected) {
297 gig_dbg(DEBUG_IF, "not connected");
301 if (cs->mstate != MS_LOCKED) {
302 dev_warn(cs->dev, "can't write to unlocked device\n");
312 cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
314 dev_err(cs->dev, "%s: out of memory\n", __func__);
319 memcpy(cb->buf, buf, count);
323 cb->wake_tasklet = &cs->if_wake_tasklet;
324 retval = cs->ops->write_cmd(cs, cb);
326 mutex_unlock(&cs->mutex);
330 static int if_write_room(struct tty_struct *tty)
332 struct cardstate *cs = tty->driver_data;
335 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
337 if (mutex_lock_interruptible(&cs->mutex))
340 if (!cs->connected) {
341 gig_dbg(DEBUG_IF, "not connected");
343 } else if (cs->mstate != MS_LOCKED) {
344 dev_warn(cs->dev, "can't write to unlocked device\n");
347 retval = cs->ops->write_room(cs);
349 mutex_unlock(&cs->mutex);
354 static int if_chars_in_buffer(struct tty_struct *tty)
356 struct cardstate *cs = tty->driver_data;
359 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
361 mutex_lock(&cs->mutex);
364 gig_dbg(DEBUG_IF, "not connected");
365 else if (cs->mstate != MS_LOCKED)
366 dev_warn(cs->dev, "can't write to unlocked device\n");
368 retval = cs->ops->chars_in_buffer(cs);
370 mutex_unlock(&cs->mutex);
375 static void if_throttle(struct tty_struct *tty)
377 struct cardstate *cs = tty->driver_data;
379 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
381 mutex_lock(&cs->mutex);
384 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
386 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
388 mutex_unlock(&cs->mutex);
391 static void if_unthrottle(struct tty_struct *tty)
393 struct cardstate *cs = tty->driver_data;
395 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
397 mutex_lock(&cs->mutex);
400 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
402 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
404 mutex_unlock(&cs->mutex);
407 static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
409 struct cardstate *cs = tty->driver_data;
412 unsigned int old_cflag;
413 unsigned int control_state, new_state;
415 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
417 mutex_lock(&cs->mutex);
419 if (!cs->connected) {
420 gig_dbg(DEBUG_IF, "not connected");
424 iflag = tty->termios.c_iflag;
425 cflag = tty->termios.c_cflag;
426 old_cflag = old ? old->c_cflag : cflag;
427 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
428 cs->minor_index, iflag, cflag, old_cflag);
430 /* get a local copy of the current port settings */
431 control_state = cs->control_state;
435 * Do not attempt to cache old rates and skip settings,
436 * disconnects screw such tricks up completely.
437 * Premature optimization is the root of all evil.
440 /* reassert DTR and (maybe) RTS on transition from B0 */
441 if ((old_cflag & CBAUD) == B0) {
442 new_state = control_state | TIOCM_DTR;
443 /* don't set RTS if using hardware flow control */
444 if (!(old_cflag & CRTSCTS))
445 new_state |= TIOCM_RTS;
446 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
448 (new_state & TIOCM_RTS) ? " only" : "/RTS");
449 cs->ops->set_modem_ctrl(cs, control_state, new_state);
450 control_state = new_state;
453 cs->ops->baud_rate(cs, cflag & CBAUD);
455 if ((cflag & CBAUD) == B0) {
456 /* Drop RTS and DTR */
457 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
458 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
459 cs->ops->set_modem_ctrl(cs, control_state, new_state);
460 control_state = new_state;
464 * Update line control register (LCR)
467 cs->ops->set_line_ctrl(cs, cflag);
469 /* save off the modified port settings */
470 cs->control_state = control_state;
473 mutex_unlock(&cs->mutex);
476 static const struct tty_operations if_ops = {
481 .compat_ioctl = if_compat_ioctl,
484 .write_room = if_write_room,
485 .chars_in_buffer = if_chars_in_buffer,
486 .set_termios = if_set_termios,
487 .throttle = if_throttle,
488 .unthrottle = if_unthrottle,
489 .tiocmget = if_tiocmget,
490 .tiocmset = if_tiocmset,
494 /* wakeup tasklet for the write operation */
495 static void if_wake(unsigned long data)
497 struct cardstate *cs = (struct cardstate *)data;
499 tty_port_tty_wakeup(&cs->port);
502 /*** interface to common ***/
504 void gigaset_if_init(struct cardstate *cs)
506 struct gigaset_driver *drv;
512 tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
514 mutex_lock(&cs->mutex);
515 cs->tty_dev = tty_port_register_device(&cs->port, drv->tty,
516 cs->minor_index, NULL);
518 if (!IS_ERR(cs->tty_dev))
519 dev_set_drvdata(cs->tty_dev, cs);
521 pr_warning("could not register device to the tty subsystem\n");
524 mutex_unlock(&cs->mutex);
527 void gigaset_if_free(struct cardstate *cs)
529 struct gigaset_driver *drv;
535 tasklet_disable(&cs->if_wake_tasklet);
536 tasklet_kill(&cs->if_wake_tasklet);
538 tty_unregister_device(drv->tty, cs->minor_index);
542 * gigaset_if_receive() - pass a received block of data to the tty device
543 * @cs: device descriptor structure.
544 * @buffer: received data.
545 * @len: number of bytes received.
547 * Called by asyncdata/isocdata if a block of data received from the
548 * device must be sent to userspace through the ttyG* device.
550 void gigaset_if_receive(struct cardstate *cs,
551 unsigned char *buffer, size_t len)
553 tty_insert_flip_string(&cs->port, buffer, len);
554 tty_flip_buffer_push(&cs->port);
556 EXPORT_SYMBOL_GPL(gigaset_if_receive);
558 /* gigaset_if_initdriver
559 * Initialize tty interface.
562 * procname Name of the driver (e.g. for /proc/tty/drivers)
563 * devname Name of the device files (prefix without minor number)
565 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
569 struct tty_driver *tty;
573 drv->tty = tty = alloc_tty_driver(drv->minors);
577 tty->type = TTY_DRIVER_TYPE_SERIAL;
578 tty->subtype = SERIAL_TYPE_NORMAL;
579 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
581 tty->driver_name = procname;
583 tty->minor_start = drv->minor;
585 tty->init_termios = tty_std_termios;
586 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
587 tty_set_operations(tty, &if_ops);
589 ret = tty_register_driver(tty);
591 pr_err("error %d registering tty driver\n", ret);
594 gig_dbg(DEBUG_IF, "tty driver initialized");
599 pr_err("out of memory\n");
602 put_tty_driver(drv->tty);
605 void gigaset_if_freedriver(struct gigaset_driver *drv)
611 tty_unregister_driver(drv->tty);
612 put_tty_driver(drv->tty);