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