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; |
| 149 | unsigned long flags; |
| 150 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 151 | gig_dbg(DEBUG_IF, "%d+%d: %s()", |
| 152 | tty->driver->minor_start, tty->index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 153 | |
| 154 | tty->driver_data = NULL; |
| 155 | |
| 156 | cs = gigaset_get_cs_by_tty(tty); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 157 | if (!cs || !try_module_get(cs->driver->owner)) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 158 | return -ENODEV; |
| 159 | |
Pavel Shved | 2f9381e | 2011-06-17 06:25:11 +0000 | [diff] [blame] | 160 | if (mutex_lock_interruptible(&cs->mutex)) { |
| 161 | module_put(cs->driver->owner); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 162 | return -ERESTARTSYS; |
Pavel Shved | 2f9381e | 2011-06-17 06:25:11 +0000 | [diff] [blame] | 163 | } |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 164 | tty->driver_data = cs; |
| 165 | |
| 166 | ++cs->open_count; |
| 167 | |
| 168 | if (cs->open_count == 1) { |
| 169 | spin_lock_irqsave(&cs->lock, flags); |
| 170 | cs->tty = tty; |
| 171 | spin_unlock_irqrestore(&cs->lock, flags); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 172 | tty->low_latency = 1; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 175 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static void if_close(struct tty_struct *tty, struct file *filp) |
| 180 | { |
| 181 | struct cardstate *cs; |
| 182 | unsigned long flags; |
| 183 | |
| 184 | cs = (struct cardstate *) tty->driver_data; |
| 185 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 186 | pr_err("%s: no cardstate\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 190 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 191 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 192 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 193 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 194 | if (!cs->connected) |
| 195 | gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ |
| 196 | else if (!cs->open_count) |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 197 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 198 | else { |
| 199 | if (!--cs->open_count) { |
| 200 | spin_lock_irqsave(&cs->lock, flags); |
| 201 | cs->tty = NULL; |
| 202 | spin_unlock_irqrestore(&cs->lock, flags); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 206 | mutex_unlock(&cs->mutex); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 207 | |
| 208 | module_put(cs->driver->owner); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 211 | static int if_ioctl(struct tty_struct *tty, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 212 | unsigned int cmd, unsigned long arg) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 213 | { |
| 214 | struct cardstate *cs; |
| 215 | int retval = -ENODEV; |
| 216 | int int_arg; |
| 217 | unsigned char buf[6]; |
| 218 | unsigned version[4]; |
| 219 | |
| 220 | cs = (struct cardstate *) tty->driver_data; |
| 221 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 222 | pr_err("%s: no cardstate\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 223 | return -ENODEV; |
| 224 | } |
| 225 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 226 | 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] | 227 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 228 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 229 | return -ERESTARTSYS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 230 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 231 | if (!cs->connected) { |
| 232 | gig_dbg(DEBUG_IF, "not connected"); |
| 233 | retval = -ENODEV; |
| 234 | } else if (!cs->open_count) |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 235 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 236 | else { |
| 237 | retval = 0; |
| 238 | switch (cmd) { |
| 239 | case GIGASET_REDIR: |
| 240 | retval = get_user(int_arg, (int __user *) arg); |
| 241 | if (retval >= 0) |
| 242 | retval = if_lock(cs, &int_arg); |
| 243 | if (retval >= 0) |
| 244 | retval = put_user(int_arg, (int __user *) arg); |
| 245 | break; |
| 246 | case GIGASET_CONFIG: |
| 247 | retval = get_user(int_arg, (int __user *) arg); |
| 248 | if (retval >= 0) |
| 249 | retval = if_config(cs, &int_arg); |
| 250 | if (retval >= 0) |
| 251 | retval = put_user(int_arg, (int __user *) arg); |
| 252 | break; |
| 253 | case GIGASET_BRKCHARS: |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 254 | retval = copy_from_user(&buf, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame^] | 255 | (const unsigned char __user *) arg, 6) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 256 | ? -EFAULT : 0; |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 257 | if (retval >= 0) { |
| 258 | gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame^] | 259 | 6, (const unsigned char *) arg); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 260 | retval = cs->ops->brkchars(cs, buf); |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 261 | } |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 262 | break; |
| 263 | case GIGASET_VERSION: |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 264 | retval = copy_from_user(version, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame^] | 265 | (unsigned __user *) arg, sizeof version) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 266 | ? -EFAULT : 0; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 267 | if (retval >= 0) |
| 268 | retval = if_version(cs, version); |
| 269 | if (retval >= 0) |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 270 | retval = copy_to_user((unsigned __user *) arg, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 271 | version, sizeof version) |
| 272 | ? -EFAULT : 0; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 273 | break; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 274 | default: |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 275 | gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x", |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 276 | __func__, cmd); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 277 | retval = -ENOIOCTLCMD; |
| 278 | } |
| 279 | } |
| 280 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 281 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 282 | |
| 283 | return retval; |
| 284 | } |
| 285 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 286 | static int if_tiocmget(struct tty_struct *tty) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 287 | { |
| 288 | struct cardstate *cs; |
| 289 | int retval; |
| 290 | |
| 291 | cs = (struct cardstate *) tty->driver_data; |
| 292 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 293 | pr_err("%s: no cardstate\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 294 | return -ENODEV; |
| 295 | } |
| 296 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 297 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 298 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 299 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 300 | return -ERESTARTSYS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 301 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame^] | 302 | retval = cs->control_state & (TIOCM_RTS | TIOCM_DTR); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 303 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 304 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 305 | |
| 306 | return retval; |
| 307 | } |
| 308 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 309 | static int if_tiocmset(struct tty_struct *tty, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 310 | unsigned int set, unsigned int clear) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 311 | { |
| 312 | struct cardstate *cs; |
| 313 | int retval; |
| 314 | unsigned mc; |
| 315 | |
| 316 | cs = (struct cardstate *) tty->driver_data; |
| 317 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 318 | pr_err("%s: no cardstate\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 319 | return -ENODEV; |
| 320 | } |
| 321 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 322 | gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)", |
| 323 | cs->minor_index, __func__, set, clear); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 324 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 325 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 326 | return -ERESTARTSYS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 327 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 328 | if (!cs->connected) { |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 329 | gig_dbg(DEBUG_IF, "not connected"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 330 | retval = -ENODEV; |
| 331 | } else { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame^] | 332 | mc = (cs->control_state | set) & ~clear & (TIOCM_RTS | TIOCM_DTR); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 333 | retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc); |
| 334 | cs->control_state = mc; |
| 335 | } |
| 336 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 337 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 338 | |
| 339 | return retval; |
| 340 | } |
| 341 | |
| 342 | static int if_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 343 | { |
| 344 | struct cardstate *cs; |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 345 | struct cmdbuf_t *cb; |
| 346 | int retval; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 347 | |
| 348 | cs = (struct cardstate *) tty->driver_data; |
| 349 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 350 | pr_err("%s: no cardstate\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 351 | return -ENODEV; |
| 352 | } |
| 353 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 354 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 355 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 356 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 357 | return -ERESTARTSYS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 358 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 359 | if (!cs->connected) { |
| 360 | gig_dbg(DEBUG_IF, "not connected"); |
| 361 | retval = -ENODEV; |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 362 | goto done; |
| 363 | } |
| 364 | if (!cs->open_count) { |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 365 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 366 | retval = -ENODEV; |
| 367 | goto done; |
| 368 | } |
| 369 | if (cs->mstate != MS_LOCKED) { |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 370 | dev_warn(cs->dev, "can't write to unlocked device\n"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 371 | retval = -EBUSY; |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 372 | goto done; |
| 373 | } |
| 374 | if (count <= 0) { |
| 375 | /* nothing to do */ |
| 376 | retval = 0; |
| 377 | goto done; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 380 | cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL); |
| 381 | if (!cb) { |
| 382 | dev_err(cs->dev, "%s: out of memory\n", __func__); |
| 383 | retval = -ENOMEM; |
| 384 | goto done; |
| 385 | } |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 386 | |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 387 | memcpy(cb->buf, buf, count); |
| 388 | cb->len = count; |
| 389 | cb->offset = 0; |
| 390 | cb->next = NULL; |
| 391 | cb->wake_tasklet = &cs->if_wake_tasklet; |
| 392 | retval = cs->ops->write_cmd(cs, cb); |
| 393 | done: |
| 394 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 395 | return retval; |
| 396 | } |
| 397 | |
| 398 | static int if_write_room(struct tty_struct *tty) |
| 399 | { |
| 400 | struct cardstate *cs; |
| 401 | int retval = -ENODEV; |
| 402 | |
| 403 | cs = (struct cardstate *) tty->driver_data; |
| 404 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 405 | pr_err("%s: no cardstate\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 406 | return -ENODEV; |
| 407 | } |
| 408 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 409 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 410 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 411 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 412 | return -ERESTARTSYS; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 413 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 414 | if (!cs->connected) { |
| 415 | gig_dbg(DEBUG_IF, "not connected"); |
| 416 | retval = -ENODEV; |
| 417 | } else if (!cs->open_count) |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 418 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 419 | else if (cs->mstate != MS_LOCKED) { |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 420 | dev_warn(cs->dev, "can't write to unlocked device\n"); |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 421 | retval = -EBUSY; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 422 | } else |
| 423 | retval = cs->ops->write_room(cs); |
| 424 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 425 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 426 | |
| 427 | return retval; |
| 428 | } |
| 429 | |
| 430 | static int if_chars_in_buffer(struct tty_struct *tty) |
| 431 | { |
| 432 | struct cardstate *cs; |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 433 | int retval = 0; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 434 | |
| 435 | cs = (struct cardstate *) tty->driver_data; |
| 436 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 437 | pr_err("%s: no cardstate\n", __func__); |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 438 | return 0; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 441 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 442 | |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 443 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 444 | |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 445 | if (!cs->connected) |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 446 | gig_dbg(DEBUG_IF, "not connected"); |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 447 | else if (!cs->open_count) |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 448 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 449 | else if (cs->mstate != MS_LOCKED) |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 450 | dev_warn(cs->dev, "can't write to unlocked device\n"); |
Tilman Schmidt | a4304f2 | 2009-08-06 20:33:33 -0700 | [diff] [blame] | 451 | else |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 452 | retval = cs->ops->chars_in_buffer(cs); |
| 453 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 454 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 455 | |
| 456 | return retval; |
| 457 | } |
| 458 | |
| 459 | static void if_throttle(struct tty_struct *tty) |
| 460 | { |
| 461 | struct cardstate *cs; |
| 462 | |
| 463 | cs = (struct cardstate *) tty->driver_data; |
| 464 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 465 | pr_err("%s: no cardstate\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 469 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 470 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 471 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 472 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 473 | if (!cs->connected) |
| 474 | gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ |
| 475 | else if (!cs->open_count) |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 476 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 477 | else |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 478 | gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 479 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 480 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static void if_unthrottle(struct tty_struct *tty) |
| 484 | { |
| 485 | struct cardstate *cs; |
| 486 | |
| 487 | cs = (struct cardstate *) tty->driver_data; |
| 488 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 489 | pr_err("%s: no cardstate\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 493 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 494 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 495 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 496 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 497 | if (!cs->connected) |
| 498 | gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ |
| 499 | else if (!cs->open_count) |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 500 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 501 | else |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 502 | gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 503 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 504 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 507 | static void if_set_termios(struct tty_struct *tty, struct ktermios *old) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 508 | { |
| 509 | struct cardstate *cs; |
| 510 | unsigned int iflag; |
| 511 | unsigned int cflag; |
| 512 | unsigned int old_cflag; |
| 513 | unsigned int control_state, new_state; |
| 514 | |
| 515 | cs = (struct cardstate *) tty->driver_data; |
| 516 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 517 | pr_err("%s: no cardstate\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 518 | return; |
| 519 | } |
| 520 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 521 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 522 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 523 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 524 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 525 | if (!cs->connected) { |
| 526 | gig_dbg(DEBUG_IF, "not connected"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 527 | goto out; |
| 528 | } |
| 529 | |
Tilman Schmidt | 51370e5 | 2009-04-05 06:39:33 +0000 | [diff] [blame] | 530 | if (!cs->open_count) { |
| 531 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 532 | goto out; |
| 533 | } |
| 534 | |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 535 | iflag = tty->termios->c_iflag; |
| 536 | cflag = tty->termios->c_cflag; |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 537 | old_cflag = old ? old->c_cflag : cflag; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 538 | gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x", |
| 539 | cs->minor_index, iflag, cflag, old_cflag); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 540 | |
| 541 | /* get a local copy of the current port settings */ |
| 542 | control_state = cs->control_state; |
| 543 | |
| 544 | /* |
| 545 | * Update baud rate. |
| 546 | * Do not attempt to cache old rates and skip settings, |
| 547 | * disconnects screw such tricks up completely. |
| 548 | * Premature optimization is the root of all evil. |
| 549 | */ |
| 550 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 551 | /* reassert DTR and (maybe) RTS on transition from B0 */ |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 552 | if ((old_cflag & CBAUD) == B0) { |
| 553 | new_state = control_state | TIOCM_DTR; |
| 554 | /* don't set RTS if using hardware flow control */ |
| 555 | if (!(old_cflag & CRTSCTS)) |
| 556 | new_state |= TIOCM_RTS; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 557 | gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s", |
| 558 | cs->minor_index, |
| 559 | (new_state & TIOCM_RTS) ? " only" : "/RTS"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 560 | cs->ops->set_modem_ctrl(cs, control_state, new_state); |
| 561 | control_state = new_state; |
| 562 | } |
| 563 | |
| 564 | cs->ops->baud_rate(cs, cflag & CBAUD); |
| 565 | |
| 566 | if ((cflag & CBAUD) == B0) { |
| 567 | /* Drop RTS and DTR */ |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 568 | 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] | 569 | new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS); |
| 570 | cs->ops->set_modem_ctrl(cs, control_state, new_state); |
| 571 | control_state = new_state; |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Update line control register (LCR) |
| 576 | */ |
| 577 | |
| 578 | cs->ops->set_line_ctrl(cs, cflag); |
| 579 | |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 580 | /* save off the modified port settings */ |
| 581 | cs->control_state = control_state; |
| 582 | |
| 583 | out: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 584 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
| 588 | /* wakeup tasklet for the write operation */ |
| 589 | static void if_wake(unsigned long data) |
| 590 | { |
| 591 | struct cardstate *cs = (struct cardstate *) data; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 592 | |
Jiri Slaby | b963a84 | 2007-02-10 01:44:55 -0800 | [diff] [blame] | 593 | if (cs->tty) |
| 594 | tty_wakeup(cs->tty); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /*** interface to common ***/ |
| 598 | |
| 599 | void gigaset_if_init(struct cardstate *cs) |
| 600 | { |
| 601 | struct gigaset_driver *drv; |
| 602 | |
| 603 | drv = cs->driver; |
| 604 | if (!drv->have_tty) |
| 605 | return; |
| 606 | |
Joe Perches | 5452fee | 2009-11-19 09:55:53 +0000 | [diff] [blame] | 607 | tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs); |
Tilman Schmidt | 7435f50 | 2007-02-12 00:52:24 -0800 | [diff] [blame] | 608 | |
| 609 | mutex_lock(&cs->mutex); |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 610 | cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL); |
Hansjoerg Lipp | 3dda4e3 | 2006-04-22 18:43:00 +0200 | [diff] [blame] | 611 | |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 612 | if (!IS_ERR(cs->tty_dev)) |
| 613 | dev_set_drvdata(cs->tty_dev, cs); |
Hansjoerg Lipp | 3dda4e3 | 2006-04-22 18:43:00 +0200 | [diff] [blame] | 614 | else { |
Tilman Schmidt | 42e3d61 | 2009-05-13 12:44:17 +0000 | [diff] [blame] | 615 | pr_warning("could not register device to the tty subsystem\n"); |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 616 | cs->tty_dev = NULL; |
Hansjoerg Lipp | 3dda4e3 | 2006-04-22 18:43:00 +0200 | [diff] [blame] | 617 | } |
Tilman Schmidt | 7435f50 | 2007-02-12 00:52:24 -0800 | [diff] [blame] | 618 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | void gigaset_if_free(struct cardstate *cs) |
| 622 | { |
| 623 | struct gigaset_driver *drv; |
| 624 | |
| 625 | drv = cs->driver; |
| 626 | if (!drv->have_tty) |
| 627 | return; |
| 628 | |
| 629 | tasklet_disable(&cs->if_wake_tasklet); |
| 630 | tasklet_kill(&cs->if_wake_tasklet); |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 631 | cs->tty_dev = NULL; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 632 | tty_unregister_device(drv->tty, cs->minor_index); |
| 633 | } |
| 634 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 635 | /** |
| 636 | * gigaset_if_receive() - pass a received block of data to the tty device |
| 637 | * @cs: device descriptor structure. |
| 638 | * @buffer: received data. |
| 639 | * @len: number of bytes received. |
| 640 | * |
| 641 | * Called by asyncdata/isocdata if a block of data received from the |
| 642 | * device must be sent to userspace through the ttyG* device. |
| 643 | */ |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 644 | void gigaset_if_receive(struct cardstate *cs, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 645 | unsigned char *buffer, size_t len) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 646 | { |
| 647 | unsigned long flags; |
| 648 | struct tty_struct *tty; |
| 649 | |
| 650 | spin_lock_irqsave(&cs->lock, flags); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 651 | tty = cs->tty; |
| 652 | if (tty == NULL) |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 653 | gig_dbg(DEBUG_IF, "receive on closed device"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 654 | else { |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 655 | tty_insert_flip_string(tty, buffer, len); |
| 656 | tty_flip_buffer_push(tty); |
| 657 | } |
| 658 | spin_unlock_irqrestore(&cs->lock, flags); |
| 659 | } |
| 660 | EXPORT_SYMBOL_GPL(gigaset_if_receive); |
| 661 | |
| 662 | /* gigaset_if_initdriver |
| 663 | * Initialize tty interface. |
| 664 | * parameters: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 665 | * drv Driver |
| 666 | * procname Name of the driver (e.g. for /proc/tty/drivers) |
| 667 | * devname Name of the device files (prefix without minor number) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 668 | */ |
| 669 | void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname, |
Greg Kroah-Hartman | f4eaa37 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 670 | const char *devname) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 671 | { |
| 672 | unsigned minors = drv->minors; |
| 673 | int ret; |
| 674 | struct tty_driver *tty; |
| 675 | |
| 676 | drv->have_tty = 0; |
| 677 | |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 678 | drv->tty = tty = alloc_tty_driver(minors); |
| 679 | if (tty == NULL) |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 680 | goto enomem; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 681 | |
| 682 | tty->magic = TTY_DRIVER_MAGIC, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame^] | 683 | tty->type = TTY_DRIVER_TYPE_SERIAL, |
| 684 | tty->subtype = SERIAL_TYPE_NORMAL, |
| 685 | tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 686 | |
| 687 | tty->driver_name = procname; |
| 688 | tty->name = devname; |
| 689 | tty->minor_start = drv->minor; |
| 690 | tty->num = drv->minors; |
| 691 | |
| 692 | tty->owner = THIS_MODULE; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 693 | |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 694 | tty->init_termios = tty_std_termios; |
| 695 | tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 696 | tty_set_operations(tty, &if_ops); |
| 697 | |
| 698 | ret = tty_register_driver(tty); |
| 699 | if (ret < 0) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 700 | pr_err("error %d registering tty driver\n", ret); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 701 | goto error; |
| 702 | } |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 703 | gig_dbg(DEBUG_IF, "tty driver initialized"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 704 | drv->have_tty = 1; |
| 705 | return; |
| 706 | |
| 707 | enomem: |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 708 | pr_err("out of memory\n"); |
Hansjoerg Lipp | ee8a4b7 | 2006-03-26 01:38:32 -0800 | [diff] [blame] | 709 | error: |
| 710 | if (drv->tty) |
| 711 | put_tty_driver(drv->tty); |
| 712 | } |
| 713 | |
| 714 | void gigaset_if_freedriver(struct gigaset_driver *drv) |
| 715 | { |
| 716 | if (!drv->have_tty) |
| 717 | return; |
| 718 | |
| 719 | drv->have_tty = 0; |
| 720 | tty_unregister_driver(drv->tty); |
| 721 | put_tty_driver(drv->tty); |
| 722 | } |