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