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