Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * interface to user space for the gigaset driver |
| 3 | * |
| 4 | * Copyright (c) 2004 by Hansjoerg Lipp <hjlipp@web.de> |
| 5 | * |
| 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 | * ===================================================================== |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include "gigaset.h" |
| 15 | #include <linux/gigaset_dev.h> |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 16 | #include <linux/tty_flip.h> |
Paul Gortmaker | 07a97fe | 2011-08-30 12:08:51 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 18 | |
| 19 | /*** our ioctls ***/ |
| 20 | |
| 21 | static int if_lock(struct cardstate *cs, int *arg) |
| 22 | { |
| 23 | int cmd = *arg; |
| 24 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 25 | gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 26 | |
| 27 | if (cmd > 1) |
| 28 | return -EINVAL; |
| 29 | |
| 30 | if (cmd < 0) { |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 31 | *arg = cs->mstate == MS_LOCKED; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 32 | return 0; |
| 33 | } |
| 34 | |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 35 | if (!cmd && cs->mstate == MS_LOCKED && cs->connected) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 36 | cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 37 | cs->ops->baud_rate(cs, B115200); |
| 38 | cs->ops->set_line_ctrl(cs, CS8); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 39 | cs->control_state = TIOCM_DTR | TIOCM_RTS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | cs->waiting = 1; |
| 43 | if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 44 | NULL, cmd, NULL)) { |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 45 | cs->waiting = 0; |
| 46 | return -ENOMEM; |
| 47 | } |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 48 | gigaset_schedule_event(cs); |
| 49 | |
| 50 | wait_event(cs->waitqueue, !cs->waiting); |
| 51 | |
| 52 | if (cs->cmd_result >= 0) { |
| 53 | *arg = cs->cmd_result; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | return cs->cmd_result; |
| 58 | } |
| 59 | |
| 60 | static int if_version(struct cardstate *cs, unsigned arg[4]) |
| 61 | { |
| 62 | static const unsigned version[4] = GIG_VERSION; |
| 63 | static const unsigned compat[4] = GIG_COMPAT; |
| 64 | unsigned cmd = arg[0]; |
| 65 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 66 | gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 67 | |
| 68 | switch (cmd) { |
| 69 | case GIGVER_DRIVER: |
| 70 | memcpy(arg, version, sizeof version); |
| 71 | return 0; |
| 72 | case GIGVER_COMPAT: |
| 73 | memcpy(arg, compat, sizeof compat); |
| 74 | return 0; |
| 75 | case GIGVER_FWBASE: |
| 76 | cs->waiting = 1; |
| 77 | if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 78 | NULL, 0, arg)) { |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 79 | cs->waiting = 0; |
| 80 | return -ENOMEM; |
| 81 | } |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 82 | gigaset_schedule_event(cs); |
| 83 | |
| 84 | wait_event(cs->waitqueue, !cs->waiting); |
| 85 | |
| 86 | if (cs->cmd_result >= 0) |
| 87 | return 0; |
| 88 | |
| 89 | return cs->cmd_result; |
| 90 | default: |
| 91 | return -EINVAL; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static int if_config(struct cardstate *cs, int *arg) |
| 96 | { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 97 | gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 98 | |
| 99 | if (*arg != 1) |
| 100 | return -EINVAL; |
| 101 | |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 102 | if (cs->mstate != MS_LOCKED) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 103 | return -EBUSY; |
| 104 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 105 | if (!cs->connected) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 106 | pr_err("%s: not connected\n", __func__); |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 107 | return -ENODEV; |
| 108 | } |
| 109 | |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 110 | *arg = 0; |
| 111 | return gigaset_enterconfigmode(cs); |
| 112 | } |
| 113 | |
| 114 | /*** the terminal driver ***/ |
| 115 | /* stolen from usbserial and some other tty drivers */ |
| 116 | |
| 117 | static int if_open(struct tty_struct *tty, struct file *filp); |
| 118 | static void if_close(struct tty_struct *tty, struct file *filp); |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 119 | static int if_ioctl(struct tty_struct *tty, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 120 | unsigned int cmd, unsigned long arg); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 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); |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 125 | static void if_set_termios(struct tty_struct *tty, struct ktermios *old); |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 126 | static int if_tiocmget(struct tty_struct *tty); |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 127 | static int if_tiocmset(struct tty_struct *tty, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 128 | unsigned int set, unsigned int clear); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 129 | static int if_write(struct tty_struct *tty, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 130 | const unsigned char *buf, int count); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 131 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 132 | static const struct tty_operations if_ops = { |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 133 | .open = if_open, |
| 134 | .close = if_close, |
| 135 | .ioctl = if_ioctl, |
| 136 | .write = if_write, |
| 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, |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 142 | .tiocmget = if_tiocmget, |
| 143 | .tiocmset = if_tiocmset, |
| 144 | }; |
| 145 | |
| 146 | static int if_open(struct tty_struct *tty, struct file *filp) |
| 147 | { |
| 148 | struct cardstate *cs; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 149 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 150 | gig_dbg(DEBUG_IF, "%d+%d: %s()", |
| 151 | tty->driver->minor_start, tty->index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 152 | |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 153 | cs = gigaset_get_cs_by_tty(tty); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 154 | if (!cs || !try_module_get(cs->driver->owner)) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 155 | return -ENODEV; |
| 156 | |
Pavel Shved | 2f9381e | 2011-06-17 06:25:11 +0000 | [diff] [blame] | 157 | if (mutex_lock_interruptible(&cs->mutex)) { |
| 158 | module_put(cs->driver->owner); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 159 | return -ERESTARTSYS; |
Pavel Shved | 2f9381e | 2011-06-17 06:25:11 +0000 | [diff] [blame] | 160 | } |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 161 | tty->driver_data = cs; |
| 162 | |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 163 | ++cs->port.count; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 164 | |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 165 | if (cs->port.count == 1) { |
| 166 | tty_port_tty_set(&cs->port, tty); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 167 | tty->low_latency = 1; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 170 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static void if_close(struct tty_struct *tty, struct file *filp) |
| 175 | { |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 176 | struct cardstate *cs = tty->driver_data; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 177 | |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 178 | if (!cs) { /* happens if we didn't find cs in open */ |
Tilman Schmidt | ef37ea3 | 2012-03-25 19:12:37 +0200 | [diff] [blame] | 179 | gig_dbg(DEBUG_IF, "%s: no cardstate", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 183 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 184 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 185 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 186 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 187 | if (!cs->connected) |
| 188 | gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 189 | else if (!cs->port.count) |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 190 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 191 | else if (!--cs->port.count) |
| 192 | tty_port_tty_set(&cs->port, NULL); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 193 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 194 | mutex_unlock(&cs->mutex); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 195 | |
| 196 | module_put(cs->driver->owner); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 199 | static int if_ioctl(struct tty_struct *tty, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 200 | unsigned int cmd, unsigned long arg) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 201 | { |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 202 | struct cardstate *cs = tty->driver_data; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 203 | int retval = -ENODEV; |
| 204 | int int_arg; |
| 205 | unsigned char buf[6]; |
| 206 | unsigned version[4]; |
| 207 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 208 | gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 209 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 210 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 211 | return -ERESTARTSYS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 212 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 213 | if (!cs->connected) { |
| 214 | gig_dbg(DEBUG_IF, "not connected"); |
| 215 | retval = -ENODEV; |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 216 | } else { |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 217 | retval = 0; |
| 218 | switch (cmd) { |
| 219 | case GIGASET_REDIR: |
| 220 | retval = get_user(int_arg, (int __user *) arg); |
| 221 | if (retval >= 0) |
| 222 | retval = if_lock(cs, &int_arg); |
| 223 | if (retval >= 0) |
| 224 | retval = put_user(int_arg, (int __user *) arg); |
| 225 | break; |
| 226 | case GIGASET_CONFIG: |
| 227 | retval = get_user(int_arg, (int __user *) arg); |
| 228 | if (retval >= 0) |
| 229 | retval = if_config(cs, &int_arg); |
| 230 | if (retval >= 0) |
| 231 | retval = put_user(int_arg, (int __user *) arg); |
| 232 | break; |
| 233 | case GIGASET_BRKCHARS: |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 234 | retval = copy_from_user(&buf, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 235 | (const unsigned char __user *) arg, 6) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 236 | ? -EFAULT : 0; |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 237 | if (retval >= 0) { |
| 238 | gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 239 | 6, (const unsigned char *) arg); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 240 | retval = cs->ops->brkchars(cs, buf); |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 241 | } |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 242 | break; |
| 243 | case GIGASET_VERSION: |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 244 | retval = copy_from_user(version, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 245 | (unsigned __user *) arg, sizeof version) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 246 | ? -EFAULT : 0; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 247 | if (retval >= 0) |
| 248 | retval = if_version(cs, version); |
| 249 | if (retval >= 0) |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 250 | retval = copy_to_user((unsigned __user *) arg, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 251 | version, sizeof version) |
| 252 | ? -EFAULT : 0; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 253 | break; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 254 | default: |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 255 | gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x", |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 256 | __func__, cmd); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 257 | retval = -ENOIOCTLCMD; |
| 258 | } |
| 259 | } |
| 260 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 261 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 262 | |
| 263 | return retval; |
| 264 | } |
| 265 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 266 | static int if_tiocmget(struct tty_struct *tty) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 267 | { |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 268 | struct cardstate *cs = tty->driver_data; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 269 | int retval; |
| 270 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 271 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 272 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 273 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 274 | return -ERESTARTSYS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 275 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 276 | retval = cs->control_state & (TIOCM_RTS | TIOCM_DTR); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 277 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 278 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 279 | |
| 280 | return retval; |
| 281 | } |
| 282 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 283 | static int if_tiocmset(struct tty_struct *tty, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 284 | unsigned int set, unsigned int clear) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 285 | { |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 286 | struct cardstate *cs = tty->driver_data; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 287 | int retval; |
| 288 | unsigned mc; |
| 289 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 290 | gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)", |
| 291 | cs->minor_index, __func__, set, clear); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 292 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 293 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 294 | return -ERESTARTSYS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 295 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 296 | if (!cs->connected) { |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 297 | gig_dbg(DEBUG_IF, "not connected"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 298 | retval = -ENODEV; |
| 299 | } else { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 300 | mc = (cs->control_state | set) & ~clear & (TIOCM_RTS | TIOCM_DTR); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 301 | retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc); |
| 302 | cs->control_state = mc; |
| 303 | } |
| 304 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 305 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 306 | |
| 307 | return retval; |
| 308 | } |
| 309 | |
| 310 | static int if_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 311 | { |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 312 | struct cardstate *cs = tty->driver_data; |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 313 | struct cmdbuf_t *cb; |
| 314 | int retval; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 315 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 316 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 317 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 318 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 319 | return -ERESTARTSYS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 320 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 321 | if (!cs->connected) { |
| 322 | gig_dbg(DEBUG_IF, "not connected"); |
| 323 | retval = -ENODEV; |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 324 | goto done; |
| 325 | } |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 326 | if (cs->mstate != MS_LOCKED) { |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 327 | dev_warn(cs->dev, "can't write to unlocked device\n"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 328 | retval = -EBUSY; |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 329 | goto done; |
| 330 | } |
| 331 | if (count <= 0) { |
| 332 | /* nothing to do */ |
| 333 | retval = 0; |
| 334 | goto done; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 337 | cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL); |
| 338 | if (!cb) { |
| 339 | dev_err(cs->dev, "%s: out of memory\n", __func__); |
| 340 | retval = -ENOMEM; |
| 341 | goto done; |
| 342 | } |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 343 | |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 344 | memcpy(cb->buf, buf, count); |
| 345 | cb->len = count; |
| 346 | cb->offset = 0; |
| 347 | cb->next = NULL; |
| 348 | cb->wake_tasklet = &cs->if_wake_tasklet; |
| 349 | retval = cs->ops->write_cmd(cs, cb); |
| 350 | done: |
| 351 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 352 | return retval; |
| 353 | } |
| 354 | |
| 355 | static int if_write_room(struct tty_struct *tty) |
| 356 | { |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 357 | struct cardstate *cs = tty->driver_data; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 358 | int retval = -ENODEV; |
| 359 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 360 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 361 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 362 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 363 | return -ERESTARTSYS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 364 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 365 | if (!cs->connected) { |
| 366 | gig_dbg(DEBUG_IF, "not connected"); |
| 367 | retval = -ENODEV; |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 368 | } else if (cs->mstate != MS_LOCKED) { |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 369 | dev_warn(cs->dev, "can't write to unlocked device\n"); |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 370 | retval = -EBUSY; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 371 | } else |
| 372 | retval = cs->ops->write_room(cs); |
| 373 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 374 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 375 | |
| 376 | return retval; |
| 377 | } |
| 378 | |
| 379 | static int if_chars_in_buffer(struct tty_struct *tty) |
| 380 | { |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 381 | struct cardstate *cs = tty->driver_data; |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 382 | int retval = 0; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 383 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 384 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 385 | |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 386 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 387 | |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 388 | if (!cs->connected) |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 389 | gig_dbg(DEBUG_IF, "not connected"); |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 390 | else if (cs->mstate != MS_LOCKED) |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 391 | dev_warn(cs->dev, "can't write to unlocked device\n"); |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 392 | else |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 393 | retval = cs->ops->chars_in_buffer(cs); |
| 394 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 395 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 396 | |
| 397 | return retval; |
| 398 | } |
| 399 | |
| 400 | static void if_throttle(struct tty_struct *tty) |
| 401 | { |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 402 | struct cardstate *cs = tty->driver_data; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 403 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 404 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 405 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 406 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 407 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 408 | if (!cs->connected) |
| 409 | gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 410 | else |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 411 | gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 412 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 413 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static void if_unthrottle(struct tty_struct *tty) |
| 417 | { |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 418 | struct cardstate *cs = tty->driver_data; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 419 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 420 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 421 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 422 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 423 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 424 | if (!cs->connected) |
| 425 | gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 426 | else |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 427 | gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 428 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 429 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 432 | static void if_set_termios(struct tty_struct *tty, struct ktermios *old) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 433 | { |
Jiri Slaby | fc258f8 | 2012-03-05 14:52:54 +0100 | [diff] [blame] | 434 | struct cardstate *cs = tty->driver_data; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 435 | unsigned int iflag; |
| 436 | unsigned int cflag; |
| 437 | unsigned int old_cflag; |
| 438 | unsigned int control_state, new_state; |
| 439 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 440 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 441 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 442 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 443 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 444 | if (!cs->connected) { |
| 445 | gig_dbg(DEBUG_IF, "not connected"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 446 | goto out; |
| 447 | } |
| 448 | |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 449 | iflag = tty->termios->c_iflag; |
| 450 | cflag = tty->termios->c_cflag; |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 451 | old_cflag = old ? old->c_cflag : cflag; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 452 | gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x", |
| 453 | cs->minor_index, iflag, cflag, old_cflag); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 454 | |
| 455 | /* get a local copy of the current port settings */ |
| 456 | control_state = cs->control_state; |
| 457 | |
| 458 | /* |
| 459 | * Update baud rate. |
| 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. |
| 463 | */ |
| 464 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 465 | /* reassert DTR and (maybe) RTS on transition from B0 */ |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 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; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 471 | gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s", |
| 472 | cs->minor_index, |
| 473 | (new_state & TIOCM_RTS) ? " only" : "/RTS"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 474 | cs->ops->set_modem_ctrl(cs, control_state, new_state); |
| 475 | control_state = new_state; |
| 476 | } |
| 477 | |
| 478 | cs->ops->baud_rate(cs, cflag & CBAUD); |
| 479 | |
| 480 | if ((cflag & CBAUD) == B0) { |
| 481 | /* Drop RTS and DTR */ |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 482 | gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 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; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Update line control register (LCR) |
| 490 | */ |
| 491 | |
| 492 | cs->ops->set_line_ctrl(cs, cflag); |
| 493 | |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 494 | /* save off the modified port settings */ |
| 495 | cs->control_state = control_state; |
| 496 | |
| 497 | out: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 498 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
| 502 | /* wakeup tasklet for the write operation */ |
| 503 | static void if_wake(unsigned long data) |
| 504 | { |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 505 | struct cardstate *cs = (struct cardstate *)data; |
| 506 | struct tty_struct *tty = tty_port_tty_get(&cs->port); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 507 | |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 508 | if (tty) { |
| 509 | tty_wakeup(tty); |
| 510 | tty_kref_put(tty); |
| 511 | } |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /*** interface to common ***/ |
| 515 | |
| 516 | void gigaset_if_init(struct cardstate *cs) |
| 517 | { |
| 518 | struct gigaset_driver *drv; |
| 519 | |
| 520 | drv = cs->driver; |
| 521 | if (!drv->have_tty) |
| 522 | return; |
| 523 | |
Joe Perches | 5452fee | 2009-11-19 09:55:53 +0000 | [diff] [blame] | 524 | tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs); |
Tilman Schmidt | 7435f50 | 2007-02-12 00:52:24 -0800 | [diff] [blame] | 525 | |
| 526 | mutex_lock(&cs->mutex); |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 527 | cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL); |
Hansjoerg Lipp | 3dda4e3 | 2006-04-22 18:43:00 +0200 | [diff] [blame] | 528 | |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 529 | if (!IS_ERR(cs->tty_dev)) |
| 530 | dev_set_drvdata(cs->tty_dev, cs); |
Hansjoerg Lipp | 3dda4e3 | 2006-04-22 18:43:00 +0200 | [diff] [blame] | 531 | else { |
Tilman Schmidt | 42e3d61 | 2009-05-13 12:44:17 +0000 | [diff] [blame] | 532 | pr_warning("could not register device to the tty subsystem\n"); |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 533 | cs->tty_dev = NULL; |
Hansjoerg Lipp | 3dda4e3 | 2006-04-22 18:43:00 +0200 | [diff] [blame] | 534 | } |
Tilman Schmidt | 7435f50 | 2007-02-12 00:52:24 -0800 | [diff] [blame] | 535 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void gigaset_if_free(struct cardstate *cs) |
| 539 | { |
| 540 | struct gigaset_driver *drv; |
| 541 | |
| 542 | drv = cs->driver; |
| 543 | if (!drv->have_tty) |
| 544 | return; |
| 545 | |
| 546 | tasklet_disable(&cs->if_wake_tasklet); |
| 547 | tasklet_kill(&cs->if_wake_tasklet); |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 548 | cs->tty_dev = NULL; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 549 | tty_unregister_device(drv->tty, cs->minor_index); |
| 550 | } |
| 551 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 552 | /** |
| 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. |
| 557 | * |
| 558 | * Called by asyncdata/isocdata if a block of data received from the |
| 559 | * device must be sent to userspace through the ttyG* device. |
| 560 | */ |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 561 | void gigaset_if_receive(struct cardstate *cs, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 562 | unsigned char *buffer, size_t len) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 563 | { |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 564 | struct tty_struct *tty = tty_port_tty_get(&cs->port); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 565 | |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 566 | if (tty == NULL) { |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 567 | gig_dbg(DEBUG_IF, "receive on closed device"); |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 568 | return; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 569 | } |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 570 | |
| 571 | tty_insert_flip_string(tty, buffer, len); |
| 572 | tty_flip_buffer_push(tty); |
| 573 | tty_kref_put(tty); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 574 | } |
| 575 | EXPORT_SYMBOL_GPL(gigaset_if_receive); |
| 576 | |
| 577 | /* gigaset_if_initdriver |
| 578 | * Initialize tty interface. |
| 579 | * parameters: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 580 | * drv Driver |
| 581 | * procname Name of the driver (e.g. for /proc/tty/drivers) |
| 582 | * devname Name of the device files (prefix without minor number) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 583 | */ |
| 584 | void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname, |
Greg Kroah-Hartman | f4eaa37 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 585 | const char *devname) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 586 | { |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 587 | int ret; |
| 588 | struct tty_driver *tty; |
| 589 | |
| 590 | drv->have_tty = 0; |
| 591 | |
Jiri Slaby | 2f16669 | 2012-03-05 14:51:52 +0100 | [diff] [blame] | 592 | drv->tty = tty = alloc_tty_driver(drv->minors); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 593 | if (tty == NULL) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 594 | goto enomem; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 595 | |
Joe Perches | 5bd4973 | 2012-02-27 14:12:07 -0500 | [diff] [blame] | 596 | tty->type = TTY_DRIVER_TYPE_SERIAL; |
| 597 | tty->subtype = SERIAL_TYPE_NORMAL; |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 598 | tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 599 | |
| 600 | tty->driver_name = procname; |
| 601 | tty->name = devname; |
| 602 | tty->minor_start = drv->minor; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 603 | |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 604 | tty->init_termios = tty_std_termios; |
| 605 | tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 606 | tty_set_operations(tty, &if_ops); |
| 607 | |
| 608 | ret = tty_register_driver(tty); |
| 609 | if (ret < 0) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 610 | pr_err("error %d registering tty driver\n", ret); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 611 | goto error; |
| 612 | } |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 613 | gig_dbg(DEBUG_IF, "tty driver initialized"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 614 | drv->have_tty = 1; |
| 615 | return; |
| 616 | |
| 617 | enomem: |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 618 | pr_err("out of memory\n"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 619 | error: |
| 620 | if (drv->tty) |
| 621 | put_tty_driver(drv->tty); |
| 622 | } |
| 623 | |
| 624 | void gigaset_if_freedriver(struct gigaset_driver *drv) |
| 625 | { |
| 626 | if (!drv->have_tty) |
| 627 | return; |
| 628 | |
| 629 | drv->have_tty = 0; |
| 630 | tty_unregister_driver(drv->tty); |
| 631 | put_tty_driver(drv->tty); |
| 632 | } |