Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Stuff used by all variants of the driver |
| 3 | * |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 4 | * Copyright (c) 2001 by Stefan Eilers, |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 5 | * Hansjoerg Lipp <hjlipp@web.de>, |
| 6 | * Tilman Schmidt <tilman@imap.cc>. |
| 7 | * |
| 8 | * ===================================================================== |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * ===================================================================== |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "gigaset.h" |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/moduleparam.h> |
| 19 | |
| 20 | /* Version Information */ |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 21 | #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers" |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 22 | #define DRIVER_DESC "Driver for Gigaset 307x" |
| 23 | |
Tilman Schmidt | 2038724 | 2009-10-06 12:18:41 +0000 | [diff] [blame] | 24 | #ifdef CONFIG_GIGASET_DEBUG |
| 25 | #define DRIVER_DESC_DEBUG " (debug build)" |
| 26 | #else |
| 27 | #define DRIVER_DESC_DEBUG "" |
| 28 | #endif |
| 29 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 30 | /* Module parameters */ |
Tilman Schmidt | 073886f | 2009-12-05 08:04:16 +0000 | [diff] [blame] | 31 | int gigaset_debuglevel; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 32 | EXPORT_SYMBOL_GPL(gigaset_debuglevel); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 33 | module_param_named(debug, gigaset_debuglevel, int, S_IRUGO | S_IWUSR); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 34 | MODULE_PARM_DESC(debug, "debug level"); |
| 35 | |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 36 | /* driver state flags */ |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 37 | #define VALID_MINOR 0x01 |
| 38 | #define VALID_ID 0x02 |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 39 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 40 | /** |
| 41 | * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging |
| 42 | * @level: debugging level. |
| 43 | * @msg: message prefix. |
| 44 | * @len: number of bytes to dump. |
| 45 | * @buf: data to dump. |
| 46 | * |
| 47 | * If the current debugging level includes one of the bits set in @level, |
| 48 | * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio, |
| 49 | * prefixed by the text @msg. |
| 50 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 51 | void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg, |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 52 | size_t len, const unsigned char *buf) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 53 | { |
| 54 | unsigned char outbuf[80]; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 55 | unsigned char c; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 56 | size_t space = sizeof outbuf - 1; |
| 57 | unsigned char *out = outbuf; |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 58 | size_t numin = len; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 59 | |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 60 | while (numin--) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 61 | c = *buf++; |
| 62 | if (c == '~' || c == '^' || c == '\\') { |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 63 | if (!space--) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 64 | break; |
| 65 | *out++ = '\\'; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 66 | } |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 67 | if (c & 0x80) { |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 68 | if (!space--) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 69 | break; |
| 70 | *out++ = '~'; |
| 71 | c ^= 0x80; |
| 72 | } |
| 73 | if (c < 0x20 || c == 0x7f) { |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 74 | if (!space--) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 75 | break; |
| 76 | *out++ = '^'; |
| 77 | c ^= 0x40; |
| 78 | } |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 79 | if (!space--) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 80 | break; |
| 81 | *out++ = c; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 82 | } |
| 83 | *out = 0; |
| 84 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 85 | gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 86 | } |
| 87 | EXPORT_SYMBOL_GPL(gigaset_dbg_buffer); |
| 88 | |
| 89 | static int setflags(struct cardstate *cs, unsigned flags, unsigned delay) |
| 90 | { |
| 91 | int r; |
| 92 | |
| 93 | r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags); |
| 94 | cs->control_state = flags; |
| 95 | if (r < 0) |
| 96 | return r; |
| 97 | |
| 98 | if (delay) { |
| 99 | set_current_state(TASK_INTERRUPTIBLE); |
| 100 | schedule_timeout(delay * HZ / 1000); |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | int gigaset_enterconfigmode(struct cardstate *cs) |
| 107 | { |
| 108 | int i, r; |
| 109 | |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 110 | cs->control_state = TIOCM_RTS; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 111 | |
| 112 | r = setflags(cs, TIOCM_DTR, 200); |
| 113 | if (r < 0) |
| 114 | goto error; |
| 115 | r = setflags(cs, 0, 200); |
| 116 | if (r < 0) |
| 117 | goto error; |
| 118 | for (i = 0; i < 5; ++i) { |
| 119 | r = setflags(cs, TIOCM_RTS, 100); |
| 120 | if (r < 0) |
| 121 | goto error; |
| 122 | r = setflags(cs, 0, 100); |
| 123 | if (r < 0) |
| 124 | goto error; |
| 125 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 126 | r = setflags(cs, TIOCM_RTS | TIOCM_DTR, 800); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 127 | if (r < 0) |
| 128 | goto error; |
| 129 | |
| 130 | return 0; |
| 131 | |
| 132 | error: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 133 | dev_err(cs->dev, "error %d on setuartbits\n", -r); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 134 | cs->control_state = TIOCM_RTS | TIOCM_DTR; |
| 135 | cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS | TIOCM_DTR); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 136 | |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 137 | return -1; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static int test_timeout(struct at_state_t *at_state) |
| 141 | { |
| 142 | if (!at_state->timer_expires) |
| 143 | return 0; |
| 144 | |
| 145 | if (--at_state->timer_expires) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 146 | gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu", |
| 147 | at_state, at_state->timer_expires); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 151 | gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL, |
| 152 | at_state->timer_index, NULL); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | static void timer_tick(unsigned long data) |
| 157 | { |
| 158 | struct cardstate *cs = (struct cardstate *) data; |
| 159 | unsigned long flags; |
| 160 | unsigned channel; |
| 161 | struct at_state_t *at_state; |
| 162 | int timeout = 0; |
| 163 | |
| 164 | spin_lock_irqsave(&cs->lock, flags); |
| 165 | |
| 166 | for (channel = 0; channel < cs->channels; ++channel) |
| 167 | if (test_timeout(&cs->bcs[channel].at_state)) |
| 168 | timeout = 1; |
| 169 | |
| 170 | if (test_timeout(&cs->at_state)) |
| 171 | timeout = 1; |
| 172 | |
| 173 | list_for_each_entry(at_state, &cs->temp_at_states, list) |
| 174 | if (test_timeout(at_state)) |
| 175 | timeout = 1; |
| 176 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 177 | if (cs->running) { |
Tilman Schmidt | ec81b5e6 | 2006-04-10 22:55:03 -0700 | [diff] [blame] | 178 | mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK)); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 179 | if (timeout) { |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 180 | gig_dbg(DEBUG_EVENT, "scheduling timeout"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 181 | tasklet_schedule(&cs->event_tasklet); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | spin_unlock_irqrestore(&cs->lock, flags); |
| 186 | } |
| 187 | |
| 188 | int gigaset_get_channel(struct bc_state *bcs) |
| 189 | { |
| 190 | unsigned long flags; |
| 191 | |
| 192 | spin_lock_irqsave(&bcs->cs->lock, flags); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 193 | if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) { |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 194 | gig_dbg(DEBUG_CHANNEL, "could not allocate channel %d", |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 195 | bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 196 | spin_unlock_irqrestore(&bcs->cs->lock, flags); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 197 | return -EBUSY; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 198 | } |
| 199 | ++bcs->use_count; |
| 200 | bcs->busy = 1; |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 201 | gig_dbg(DEBUG_CHANNEL, "allocated channel %d", bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 202 | spin_unlock_irqrestore(&bcs->cs->lock, flags); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 203 | return 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Tilman Schmidt | 7bb5fdc | 2009-10-06 12:19:17 +0000 | [diff] [blame] | 206 | struct bc_state *gigaset_get_free_channel(struct cardstate *cs) |
| 207 | { |
| 208 | unsigned long flags; |
| 209 | int i; |
| 210 | |
| 211 | spin_lock_irqsave(&cs->lock, flags); |
| 212 | if (!try_module_get(cs->driver->owner)) { |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 213 | gig_dbg(DEBUG_CHANNEL, |
Tilman Schmidt | 7bb5fdc | 2009-10-06 12:19:17 +0000 | [diff] [blame] | 214 | "could not get module for allocating channel"); |
| 215 | spin_unlock_irqrestore(&cs->lock, flags); |
| 216 | return NULL; |
| 217 | } |
| 218 | for (i = 0; i < cs->channels; ++i) |
| 219 | if (!cs->bcs[i].use_count) { |
| 220 | ++cs->bcs[i].use_count; |
| 221 | cs->bcs[i].busy = 1; |
| 222 | spin_unlock_irqrestore(&cs->lock, flags); |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 223 | gig_dbg(DEBUG_CHANNEL, "allocated channel %d", i); |
Tilman Schmidt | 7bb5fdc | 2009-10-06 12:19:17 +0000 | [diff] [blame] | 224 | return cs->bcs + i; |
| 225 | } |
| 226 | module_put(cs->driver->owner); |
| 227 | spin_unlock_irqrestore(&cs->lock, flags); |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 228 | gig_dbg(DEBUG_CHANNEL, "no free channel"); |
Tilman Schmidt | 7bb5fdc | 2009-10-06 12:19:17 +0000 | [diff] [blame] | 229 | return NULL; |
| 230 | } |
| 231 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 232 | void gigaset_free_channel(struct bc_state *bcs) |
| 233 | { |
| 234 | unsigned long flags; |
| 235 | |
| 236 | spin_lock_irqsave(&bcs->cs->lock, flags); |
| 237 | if (!bcs->busy) { |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 238 | gig_dbg(DEBUG_CHANNEL, "could not free channel %d", |
| 239 | bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 240 | spin_unlock_irqrestore(&bcs->cs->lock, flags); |
| 241 | return; |
| 242 | } |
| 243 | --bcs->use_count; |
| 244 | bcs->busy = 0; |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 245 | module_put(bcs->cs->driver->owner); |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 246 | gig_dbg(DEBUG_CHANNEL, "freed channel %d", bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 247 | spin_unlock_irqrestore(&bcs->cs->lock, flags); |
| 248 | } |
| 249 | |
| 250 | int gigaset_get_channels(struct cardstate *cs) |
| 251 | { |
| 252 | unsigned long flags; |
| 253 | int i; |
| 254 | |
| 255 | spin_lock_irqsave(&cs->lock, flags); |
| 256 | for (i = 0; i < cs->channels; ++i) |
| 257 | if (cs->bcs[i].use_count) { |
| 258 | spin_unlock_irqrestore(&cs->lock, flags); |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 259 | gig_dbg(DEBUG_CHANNEL, |
| 260 | "could not allocate all channels"); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 261 | return -EBUSY; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 262 | } |
| 263 | for (i = 0; i < cs->channels; ++i) |
| 264 | ++cs->bcs[i].use_count; |
| 265 | spin_unlock_irqrestore(&cs->lock, flags); |
| 266 | |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 267 | gig_dbg(DEBUG_CHANNEL, "allocated all channels"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 268 | |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 269 | return 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void gigaset_free_channels(struct cardstate *cs) |
| 273 | { |
| 274 | unsigned long flags; |
| 275 | int i; |
| 276 | |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 277 | gig_dbg(DEBUG_CHANNEL, "unblocking all channels"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 278 | spin_lock_irqsave(&cs->lock, flags); |
| 279 | for (i = 0; i < cs->channels; ++i) |
| 280 | --cs->bcs[i].use_count; |
| 281 | spin_unlock_irqrestore(&cs->lock, flags); |
| 282 | } |
| 283 | |
| 284 | void gigaset_block_channels(struct cardstate *cs) |
| 285 | { |
| 286 | unsigned long flags; |
| 287 | int i; |
| 288 | |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 289 | gig_dbg(DEBUG_CHANNEL, "blocking all channels"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 290 | spin_lock_irqsave(&cs->lock, flags); |
| 291 | for (i = 0; i < cs->channels; ++i) |
| 292 | ++cs->bcs[i].use_count; |
| 293 | spin_unlock_irqrestore(&cs->lock, flags); |
| 294 | } |
| 295 | |
| 296 | static void clear_events(struct cardstate *cs) |
| 297 | { |
| 298 | struct event_t *ev; |
| 299 | unsigned head, tail; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 300 | unsigned long flags; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 301 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 302 | spin_lock_irqsave(&cs->ev_lock, flags); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 303 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 304 | head = cs->ev_head; |
| 305 | tail = cs->ev_tail; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 306 | |
| 307 | while (tail != head) { |
| 308 | ev = cs->events + head; |
| 309 | kfree(ev->ptr); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 310 | head = (head + 1) % MAX_EVENTS; |
| 311 | } |
| 312 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 313 | cs->ev_head = tail; |
| 314 | |
| 315 | spin_unlock_irqrestore(&cs->ev_lock, flags); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 318 | /** |
| 319 | * gigaset_add_event() - add event to device event queue |
| 320 | * @cs: device descriptor structure. |
| 321 | * @at_state: connection state structure. |
| 322 | * @type: event type. |
| 323 | * @ptr: pointer parameter for event. |
| 324 | * @parameter: integer parameter for event. |
| 325 | * @arg: pointer parameter for event. |
| 326 | * |
| 327 | * Allocate an event queue entry from the device's event queue, and set it up |
| 328 | * with the parameters given. |
| 329 | * |
| 330 | * Return value: added event |
| 331 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 332 | struct event_t *gigaset_add_event(struct cardstate *cs, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 333 | struct at_state_t *at_state, int type, |
| 334 | void *ptr, int parameter, void *arg) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 335 | { |
| 336 | unsigned long flags; |
| 337 | unsigned next, tail; |
| 338 | struct event_t *event = NULL; |
| 339 | |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 340 | gig_dbg(DEBUG_EVENT, "queueing event %d", type); |
| 341 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 342 | spin_lock_irqsave(&cs->ev_lock, flags); |
| 343 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 344 | tail = cs->ev_tail; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 345 | next = (tail + 1) % MAX_EVENTS; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 346 | if (unlikely(next == cs->ev_head)) |
Tilman Schmidt | 5002779 | 2008-07-23 21:28:27 -0700 | [diff] [blame] | 347 | dev_err(cs->dev, "event queue full\n"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 348 | else { |
| 349 | event = cs->events + tail; |
| 350 | event->type = type; |
| 351 | event->at_state = at_state; |
| 352 | event->cid = -1; |
| 353 | event->ptr = ptr; |
| 354 | event->arg = arg; |
| 355 | event->parameter = parameter; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 356 | cs->ev_tail = next; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | spin_unlock_irqrestore(&cs->ev_lock, flags); |
| 360 | |
| 361 | return event; |
| 362 | } |
| 363 | EXPORT_SYMBOL_GPL(gigaset_add_event); |
| 364 | |
Tilman Schmidt | 7643ffb | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 365 | static void clear_at_state(struct at_state_t *at_state) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 366 | { |
| 367 | int i; |
| 368 | |
| 369 | for (i = 0; i < STR_NUM; ++i) { |
| 370 | kfree(at_state->str_var[i]); |
| 371 | at_state->str_var[i] = NULL; |
| 372 | } |
| 373 | } |
| 374 | |
Tilman Schmidt | 7643ffb | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 375 | static void dealloc_temp_at_states(struct cardstate *cs) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 376 | { |
| 377 | struct at_state_t *cur, *next; |
| 378 | |
| 379 | list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) { |
| 380 | list_del(&cur->list); |
Tilman Schmidt | 7643ffb | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 381 | clear_at_state(cur); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 382 | kfree(cur); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static void gigaset_freebcs(struct bc_state *bcs) |
| 387 | { |
| 388 | int i; |
| 389 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 390 | gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 391 | bcs->cs->ops->freebcshw(bcs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 392 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 393 | gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 394 | clear_at_state(&bcs->at_state); |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 395 | gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel); |
Tilman Schmidt | e7752ee | 2010-06-21 13:54:19 +0000 | [diff] [blame] | 396 | dev_kfree_skb(bcs->rx_skb); |
| 397 | bcs->rx_skb = NULL; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 398 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 399 | for (i = 0; i < AT_NUM; ++i) { |
| 400 | kfree(bcs->commands[i]); |
| 401 | bcs->commands[i] = NULL; |
| 402 | } |
| 403 | } |
| 404 | |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 405 | static struct cardstate *alloc_cs(struct gigaset_driver *drv) |
| 406 | { |
| 407 | unsigned long flags; |
| 408 | unsigned i; |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 409 | struct cardstate *cs; |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 410 | struct cardstate *ret = NULL; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 411 | |
| 412 | spin_lock_irqsave(&drv->lock, flags); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 413 | if (drv->blocked) |
| 414 | goto exit; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 415 | for (i = 0; i < drv->minors; ++i) { |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 416 | cs = drv->cs + i; |
| 417 | if (!(cs->flags & VALID_MINOR)) { |
| 418 | cs->flags = VALID_MINOR; |
| 419 | ret = cs; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 420 | break; |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 421 | } |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 422 | } |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 423 | exit: |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 424 | spin_unlock_irqrestore(&drv->lock, flags); |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | static void free_cs(struct cardstate *cs) |
| 429 | { |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 430 | cs->flags = 0; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static void make_valid(struct cardstate *cs, unsigned mask) |
| 434 | { |
| 435 | unsigned long flags; |
| 436 | struct gigaset_driver *drv = cs->driver; |
| 437 | spin_lock_irqsave(&drv->lock, flags); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 438 | cs->flags |= mask; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 439 | spin_unlock_irqrestore(&drv->lock, flags); |
| 440 | } |
| 441 | |
| 442 | static void make_invalid(struct cardstate *cs, unsigned mask) |
| 443 | { |
| 444 | unsigned long flags; |
| 445 | struct gigaset_driver *drv = cs->driver; |
| 446 | spin_lock_irqsave(&drv->lock, flags); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 447 | cs->flags &= ~mask; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 448 | spin_unlock_irqrestore(&drv->lock, flags); |
| 449 | } |
| 450 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 451 | /** |
| 452 | * gigaset_freecs() - free all associated ressources of a device |
| 453 | * @cs: device descriptor structure. |
| 454 | * |
| 455 | * Stops all tasklets and timers, unregisters the device from all |
| 456 | * subsystems it was registered to, deallocates the device structure |
| 457 | * @cs and all structures referenced from it. |
| 458 | * Operations on the device should be stopped before calling this. |
| 459 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 460 | void gigaset_freecs(struct cardstate *cs) |
| 461 | { |
| 462 | int i; |
| 463 | unsigned long flags; |
| 464 | |
| 465 | if (!cs) |
| 466 | return; |
| 467 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 468 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 469 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 470 | spin_lock_irqsave(&cs->lock, flags); |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 471 | cs->running = 0; |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 472 | spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are |
| 473 | not rescheduled below */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 474 | |
| 475 | tasklet_kill(&cs->event_tasklet); |
| 476 | del_timer_sync(&cs->timer); |
| 477 | |
| 478 | switch (cs->cs_init) { |
| 479 | default: |
Tilman Schmidt | 088ec0c | 2009-10-06 12:19:07 +0000 | [diff] [blame] | 480 | /* clear B channel structures */ |
| 481 | for (i = 0; i < cs->channels; ++i) { |
| 482 | gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i); |
| 483 | gigaset_freebcs(cs->bcs + i); |
| 484 | } |
| 485 | |
Hansjoerg Lipp | 3dda4e3 | 2006-04-22 18:43:00 +0200 | [diff] [blame] | 486 | /* clear device sysfs */ |
| 487 | gigaset_free_dev_sysfs(cs); |
| 488 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 489 | gigaset_if_free(cs); |
| 490 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 491 | gig_dbg(DEBUG_INIT, "clearing hw"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 492 | cs->ops->freecshw(cs); |
| 493 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 494 | /* fall through */ |
| 495 | case 2: /* error in initcshw */ |
| 496 | /* Deregister from LL */ |
| 497 | make_invalid(cs, VALID_ID); |
Tilman Schmidt | bc35b4e | 2010-03-14 12:58:05 +0000 | [diff] [blame] | 498 | gigaset_isdn_unregdev(cs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 499 | |
| 500 | /* fall through */ |
Tilman Schmidt | 088ec0c | 2009-10-06 12:19:07 +0000 | [diff] [blame] | 501 | case 1: /* error when registering to LL */ |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 502 | gig_dbg(DEBUG_INIT, "clearing at_state"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 503 | clear_at_state(&cs->at_state); |
Tilman Schmidt | 7643ffb | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 504 | dealloc_temp_at_states(cs); |
Tilman Schmidt | bc882b1 | 2013-01-21 11:57:21 +0000 | [diff] [blame] | 505 | clear_events(cs); |
Jiri Slaby | 1862c14 | 2012-11-19 20:11:42 +0100 | [diff] [blame] | 506 | tty_port_destroy(&cs->port); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 507 | |
| 508 | /* fall through */ |
Tilman Schmidt | 088ec0c | 2009-10-06 12:19:07 +0000 | [diff] [blame] | 509 | case 0: /* error in basic setup */ |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 510 | gig_dbg(DEBUG_INIT, "freeing inbuf"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 511 | kfree(cs->inbuf); |
Tilman Schmidt | bc882b1 | 2013-01-21 11:57:21 +0000 | [diff] [blame] | 512 | kfree(cs->bcs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 513 | } |
Tilman Schmidt | bc882b1 | 2013-01-21 11:57:21 +0000 | [diff] [blame] | 514 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 515 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 516 | free_cs(cs); |
| 517 | } |
| 518 | EXPORT_SYMBOL_GPL(gigaset_freecs); |
| 519 | |
| 520 | void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 521 | struct cardstate *cs, int cid) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 522 | { |
| 523 | int i; |
| 524 | |
| 525 | INIT_LIST_HEAD(&at_state->list); |
| 526 | at_state->waiting = 0; |
| 527 | at_state->getstring = 0; |
| 528 | at_state->pending_commands = 0; |
| 529 | at_state->timer_expires = 0; |
| 530 | at_state->timer_active = 0; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 531 | at_state->timer_index = 0; |
| 532 | at_state->seq_index = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 533 | at_state->ConState = 0; |
| 534 | for (i = 0; i < STR_NUM; ++i) |
| 535 | at_state->str_var[i] = NULL; |
| 536 | at_state->int_var[VAR_ZDLE] = 0; |
| 537 | at_state->int_var[VAR_ZCTP] = -1; |
| 538 | at_state->int_var[VAR_ZSAU] = ZSAU_NULL; |
| 539 | at_state->cs = cs; |
| 540 | at_state->bcs = bcs; |
| 541 | at_state->cid = cid; |
| 542 | if (!cid) |
| 543 | at_state->replystruct = cs->tabnocid; |
| 544 | else |
| 545 | at_state->replystruct = cs->tabcid; |
| 546 | } |
| 547 | |
| 548 | |
Tilman Schmidt | 2032e2c | 2009-10-25 09:30:07 +0000 | [diff] [blame] | 549 | static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 550 | /* inbuf->read must be allocated before! */ |
| 551 | { |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 552 | inbuf->head = 0; |
| 553 | inbuf->tail = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 554 | inbuf->cs = cs; |
Tilman Schmidt | 2032e2c | 2009-10-25 09:30:07 +0000 | [diff] [blame] | 555 | inbuf->inputstate = INS_command; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 558 | /** |
| 559 | * gigaset_fill_inbuf() - append received data to input buffer |
| 560 | * @inbuf: buffer structure. |
| 561 | * @src: received data. |
| 562 | * @numbytes: number of bytes received. |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 563 | * |
| 564 | * Return value: !=0 if some data was appended |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 565 | */ |
Tilman Schmidt | 714e823 | 2006-04-10 22:55:09 -0700 | [diff] [blame] | 566 | int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src, |
| 567 | unsigned numbytes) |
| 568 | { |
| 569 | unsigned n, head, tail, bytesleft; |
| 570 | |
| 571 | gig_dbg(DEBUG_INTR, "received %u bytes", numbytes); |
| 572 | |
| 573 | if (!numbytes) |
| 574 | return 0; |
| 575 | |
| 576 | bytesleft = numbytes; |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 577 | tail = inbuf->tail; |
| 578 | head = inbuf->head; |
Tilman Schmidt | 714e823 | 2006-04-10 22:55:09 -0700 | [diff] [blame] | 579 | gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail); |
| 580 | |
| 581 | while (bytesleft) { |
| 582 | if (head > tail) |
| 583 | n = head - 1 - tail; |
| 584 | else if (head == 0) |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 585 | n = (RBUFSIZE - 1) - tail; |
Tilman Schmidt | 714e823 | 2006-04-10 22:55:09 -0700 | [diff] [blame] | 586 | else |
| 587 | n = RBUFSIZE - tail; |
| 588 | if (!n) { |
| 589 | dev_err(inbuf->cs->dev, |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 590 | "buffer overflow (%u bytes lost)\n", |
| 591 | bytesleft); |
Tilman Schmidt | 714e823 | 2006-04-10 22:55:09 -0700 | [diff] [blame] | 592 | break; |
| 593 | } |
| 594 | if (n > bytesleft) |
| 595 | n = bytesleft; |
| 596 | memcpy(inbuf->data + tail, src, n); |
| 597 | bytesleft -= n; |
| 598 | tail = (tail + n) % RBUFSIZE; |
| 599 | src += n; |
| 600 | } |
| 601 | gig_dbg(DEBUG_INTR, "setting tail to %u", tail); |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 602 | inbuf->tail = tail; |
Tilman Schmidt | 714e823 | 2006-04-10 22:55:09 -0700 | [diff] [blame] | 603 | return numbytes != bytesleft; |
| 604 | } |
| 605 | EXPORT_SYMBOL_GPL(gigaset_fill_inbuf); |
| 606 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 607 | /* Initialize the b-channel structure */ |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 608 | static int gigaset_initbcs(struct bc_state *bcs, struct cardstate *cs, |
| 609 | int channel) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 610 | { |
| 611 | int i; |
| 612 | |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 613 | bcs->tx_skb = NULL; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 614 | |
| 615 | skb_queue_head_init(&bcs->squeue); |
| 616 | |
| 617 | bcs->corrupted = 0; |
| 618 | bcs->trans_down = 0; |
| 619 | bcs->trans_up = 0; |
| 620 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 621 | gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 622 | gigaset_at_init(&bcs->at_state, bcs, cs, -1); |
| 623 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 624 | #ifdef CONFIG_GIGASET_DEBUG |
| 625 | bcs->emptycount = 0; |
| 626 | #endif |
| 627 | |
Tilman Schmidt | e7752ee | 2010-06-21 13:54:19 +0000 | [diff] [blame] | 628 | bcs->rx_bufsize = 0; |
| 629 | bcs->rx_skb = NULL; |
| 630 | bcs->rx_fcs = PPP_INITFCS; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 631 | bcs->inputstate = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 632 | bcs->channel = channel; |
| 633 | bcs->cs = cs; |
| 634 | |
| 635 | bcs->chstate = 0; |
| 636 | bcs->use_count = 1; |
| 637 | bcs->busy = 0; |
| 638 | bcs->ignore = cs->ignoreframes; |
| 639 | |
| 640 | for (i = 0; i < AT_NUM; ++i) |
| 641 | bcs->commands[i] = NULL; |
| 642 | |
Tilman Schmidt | 1b4843c | 2010-06-21 13:55:20 +0000 | [diff] [blame] | 643 | spin_lock_init(&bcs->aplock); |
| 644 | bcs->ap = NULL; |
| 645 | bcs->apconnstate = 0; |
| 646 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 647 | gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 648 | return cs->ops->initbcshw(bcs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 651 | /** |
| 652 | * gigaset_initcs() - initialize device structure |
| 653 | * @drv: hardware driver the device belongs to |
| 654 | * @channels: number of B channels supported by device |
| 655 | * @onechannel: !=0 if B channel data and AT commands share one |
| 656 | * communication channel (M10x), |
| 657 | * ==0 if B channels have separate communication channels (base) |
| 658 | * @ignoreframes: number of frames to ignore after setting up B channel |
| 659 | * @cidmode: !=0: start in CallID mode |
| 660 | * @modulename: name of driver module for LL registration |
| 661 | * |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 662 | * Allocate and initialize cardstate structure for Gigaset driver |
| 663 | * Calls hardware dependent gigaset_initcshw() function |
| 664 | * Calls B channel initialization function gigaset_initbcs() for each B channel |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 665 | * |
| 666 | * Return value: |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 667 | * pointer to cardstate structure |
| 668 | */ |
| 669 | struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels, |
| 670 | int onechannel, int ignoreframes, |
| 671 | int cidmode, const char *modulename) |
| 672 | { |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 673 | struct cardstate *cs; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 674 | unsigned long flags; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 675 | int i; |
| 676 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 677 | gig_dbg(DEBUG_INIT, "allocating cs"); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 678 | cs = alloc_cs(drv); |
| 679 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 680 | pr_err("maximum number of devices exceeded\n"); |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 681 | return NULL; |
| 682 | } |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 683 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 684 | cs->cs_init = 0; |
| 685 | cs->channels = channels; |
| 686 | cs->onechannel = onechannel; |
| 687 | cs->ignoreframes = ignoreframes; |
| 688 | INIT_LIST_HEAD(&cs->temp_at_states); |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 689 | cs->running = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 690 | init_timer(&cs->timer); /* clear next & prev */ |
| 691 | spin_lock_init(&cs->ev_lock); |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 692 | cs->ev_tail = 0; |
| 693 | cs->ev_head = 0; |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 694 | |
Joe Perches | 5452fee | 2009-11-19 09:55:53 +0000 | [diff] [blame] | 695 | tasklet_init(&cs->event_tasklet, gigaset_handle_event, |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 696 | (unsigned long) cs); |
Jiri Slaby | 48a7466 | 2012-03-05 14:52:55 +0100 | [diff] [blame] | 697 | tty_port_init(&cs->port); |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 698 | cs->commands_pending = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 699 | cs->cur_at_seq = 0; |
| 700 | cs->gotfwver = -1; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 701 | cs->dev = NULL; |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 702 | cs->tty_dev = NULL; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 703 | cs->cidmode = cidmode != 0; |
Tilman Schmidt | 528efc6 | 2009-05-13 12:44:17 +0000 | [diff] [blame] | 704 | cs->tabnocid = gigaset_tab_nocid; |
| 705 | cs->tabcid = gigaset_tab_cid; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 706 | |
| 707 | init_waitqueue_head(&cs->waitqueue); |
| 708 | cs->waiting = 0; |
| 709 | |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 710 | cs->mode = M_UNKNOWN; |
| 711 | cs->mstate = MS_UNINITIALIZED; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 712 | |
Tilman Schmidt | bc882b1 | 2013-01-21 11:57:21 +0000 | [diff] [blame] | 713 | cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL); |
| 714 | cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL); |
| 715 | if (!cs->bcs || !cs->inbuf) { |
| 716 | pr_err("out of memory\n"); |
| 717 | goto error; |
| 718 | } |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 719 | ++cs->cs_init; |
| 720 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 721 | gig_dbg(DEBUG_INIT, "setting up at_state"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 722 | spin_lock_init(&cs->lock); |
| 723 | gigaset_at_init(&cs->at_state, NULL, cs, 0); |
| 724 | cs->dle = 0; |
| 725 | cs->cbytes = 0; |
| 726 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 727 | gig_dbg(DEBUG_INIT, "setting up inbuf"); |
Tilman Schmidt | 2032e2c | 2009-10-25 09:30:07 +0000 | [diff] [blame] | 728 | gigaset_inbuf_init(cs->inbuf, cs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 729 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 730 | cs->connected = 0; |
| 731 | cs->isdn_up = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 732 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 733 | gig_dbg(DEBUG_INIT, "setting up cmdbuf"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 734 | cs->cmdbuf = cs->lastcmdbuf = NULL; |
| 735 | spin_lock_init(&cs->cmdlock); |
| 736 | cs->curlen = 0; |
| 737 | cs->cmdbytes = 0; |
| 738 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 739 | gig_dbg(DEBUG_INIT, "setting up iif"); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 740 | if (gigaset_isdn_regdev(cs, modulename) < 0) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 741 | pr_err("error registering ISDN device\n"); |
Jiri Slaby | 1862c14 | 2012-11-19 20:11:42 +0100 | [diff] [blame] | 742 | goto error; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | make_valid(cs, VALID_ID); |
| 746 | ++cs->cs_init; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 747 | gig_dbg(DEBUG_INIT, "setting up hw"); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 748 | if (cs->ops->initcshw(cs) < 0) |
Jiri Slaby | 1862c14 | 2012-11-19 20:11:42 +0100 | [diff] [blame] | 749 | goto error; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 750 | |
| 751 | ++cs->cs_init; |
| 752 | |
Tilman Schmidt | 7435f50 | 2007-02-12 00:52:24 -0800 | [diff] [blame] | 753 | /* set up character device */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 754 | gigaset_if_init(cs); |
| 755 | |
Hansjoerg Lipp | 3dda4e3 | 2006-04-22 18:43:00 +0200 | [diff] [blame] | 756 | /* set up device sysfs */ |
| 757 | gigaset_init_dev_sysfs(cs); |
| 758 | |
Tilman Schmidt | 088ec0c | 2009-10-06 12:19:07 +0000 | [diff] [blame] | 759 | /* set up channel data structures */ |
| 760 | for (i = 0; i < channels; ++i) { |
| 761 | gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 762 | if (gigaset_initbcs(cs->bcs + i, cs, i) < 0) { |
Tilman Schmidt | 088ec0c | 2009-10-06 12:19:07 +0000 | [diff] [blame] | 763 | pr_err("could not allocate channel %d data\n", i); |
Jiri Slaby | 1862c14 | 2012-11-19 20:11:42 +0100 | [diff] [blame] | 764 | goto error; |
Tilman Schmidt | 088ec0c | 2009-10-06 12:19:07 +0000 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 768 | spin_lock_irqsave(&cs->lock, flags); |
| 769 | cs->running = 1; |
| 770 | spin_unlock_irqrestore(&cs->lock, flags); |
Tilman Schmidt | ec81b5e6 | 2006-04-10 22:55:03 -0700 | [diff] [blame] | 771 | setup_timer(&cs->timer, timer_tick, (unsigned long) cs); |
| 772 | cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 773 | add_timer(&cs->timer); |
| 774 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 775 | gig_dbg(DEBUG_INIT, "cs initialized"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 776 | return cs; |
Jiri Slaby | 1862c14 | 2012-11-19 20:11:42 +0100 | [diff] [blame] | 777 | |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 778 | error: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 779 | gig_dbg(DEBUG_INIT, "failed"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 780 | gigaset_freecs(cs); |
| 781 | return NULL; |
| 782 | } |
| 783 | EXPORT_SYMBOL_GPL(gigaset_initcs); |
| 784 | |
Tilman Schmidt | 73a8881 | 2006-04-22 02:35:30 -0700 | [diff] [blame] | 785 | /* ReInitialize the b-channel structure on hangup */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 786 | void gigaset_bcs_reinit(struct bc_state *bcs) |
| 787 | { |
| 788 | struct sk_buff *skb; |
| 789 | struct cardstate *cs = bcs->cs; |
| 790 | unsigned long flags; |
| 791 | |
| 792 | while ((skb = skb_dequeue(&bcs->squeue)) != NULL) |
| 793 | dev_kfree_skb(skb); |
| 794 | |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 795 | spin_lock_irqsave(&cs->lock, flags); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 796 | clear_at_state(&bcs->at_state); |
| 797 | bcs->at_state.ConState = 0; |
| 798 | bcs->at_state.timer_active = 0; |
| 799 | bcs->at_state.timer_expires = 0; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 800 | bcs->at_state.cid = -1; /* No CID defined */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 801 | spin_unlock_irqrestore(&cs->lock, flags); |
| 802 | |
| 803 | bcs->inputstate = 0; |
| 804 | |
| 805 | #ifdef CONFIG_GIGASET_DEBUG |
| 806 | bcs->emptycount = 0; |
| 807 | #endif |
| 808 | |
Tilman Schmidt | e7752ee | 2010-06-21 13:54:19 +0000 | [diff] [blame] | 809 | bcs->rx_fcs = PPP_INITFCS; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 810 | bcs->chstate = 0; |
| 811 | |
| 812 | bcs->ignore = cs->ignoreframes; |
Tilman Schmidt | e7752ee | 2010-06-21 13:54:19 +0000 | [diff] [blame] | 813 | dev_kfree_skb(bcs->rx_skb); |
| 814 | bcs->rx_skb = NULL; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 815 | |
| 816 | cs->ops->reinitbcshw(bcs); |
| 817 | } |
| 818 | |
| 819 | static void cleanup_cs(struct cardstate *cs) |
| 820 | { |
| 821 | struct cmdbuf_t *cb, *tcb; |
| 822 | int i; |
| 823 | unsigned long flags; |
| 824 | |
| 825 | spin_lock_irqsave(&cs->lock, flags); |
| 826 | |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 827 | cs->mode = M_UNKNOWN; |
| 828 | cs->mstate = MS_UNINITIALIZED; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 829 | |
| 830 | clear_at_state(&cs->at_state); |
Tilman Schmidt | 7643ffb | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 831 | dealloc_temp_at_states(cs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 832 | gigaset_at_init(&cs->at_state, NULL, cs, 0); |
| 833 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 834 | cs->inbuf->inputstate = INS_command; |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 835 | cs->inbuf->head = 0; |
| 836 | cs->inbuf->tail = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 837 | |
| 838 | cb = cs->cmdbuf; |
| 839 | while (cb) { |
| 840 | tcb = cb; |
| 841 | cb = cb->next; |
| 842 | kfree(tcb); |
| 843 | } |
| 844 | cs->cmdbuf = cs->lastcmdbuf = NULL; |
| 845 | cs->curlen = 0; |
| 846 | cs->cmdbytes = 0; |
| 847 | cs->gotfwver = -1; |
| 848 | cs->dle = 0; |
| 849 | cs->cur_at_seq = 0; |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 850 | cs->commands_pending = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 851 | cs->cbytes = 0; |
| 852 | |
| 853 | spin_unlock_irqrestore(&cs->lock, flags); |
| 854 | |
| 855 | for (i = 0; i < cs->channels; ++i) { |
| 856 | gigaset_freebcs(cs->bcs + i); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 857 | if (gigaset_initbcs(cs->bcs + i, cs, i) < 0) |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 858 | pr_err("could not allocate channel %d data\n", i); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | if (cs->waiting) { |
| 862 | cs->cmd_result = -ENODEV; |
| 863 | cs->waiting = 0; |
| 864 | wake_up_interruptible(&cs->waitqueue); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 869 | /** |
| 870 | * gigaset_start() - start device operations |
| 871 | * @cs: device descriptor structure. |
| 872 | * |
| 873 | * Prepares the device for use by setting up communication parameters, |
| 874 | * scheduling an EV_START event to initiate device initialization, and |
| 875 | * waiting for completion of the initialization. |
| 876 | * |
| 877 | * Return value: |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 878 | * 0 on success, error code < 0 on failure |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 879 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 880 | int gigaset_start(struct cardstate *cs) |
| 881 | { |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 882 | unsigned long flags; |
| 883 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 884 | if (mutex_lock_interruptible(&cs->mutex)) |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 885 | return -EBUSY; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 886 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 887 | spin_lock_irqsave(&cs->lock, flags); |
| 888 | cs->connected = 1; |
| 889 | spin_unlock_irqrestore(&cs->lock, flags); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 890 | |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 891 | if (cs->mstate != MS_LOCKED) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 892 | cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 893 | cs->ops->baud_rate(cs, B115200); |
| 894 | cs->ops->set_line_ctrl(cs, CS8); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 895 | cs->control_state = TIOCM_DTR | TIOCM_RTS; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | cs->waiting = 1; |
| 899 | |
| 900 | if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) { |
| 901 | cs->waiting = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 902 | goto error; |
| 903 | } |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 904 | gigaset_schedule_event(cs); |
| 905 | |
| 906 | wait_event(cs->waitqueue, !cs->waiting); |
| 907 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 908 | mutex_unlock(&cs->mutex); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 909 | return 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 910 | |
| 911 | error: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 912 | mutex_unlock(&cs->mutex); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 913 | return -ENOMEM; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 914 | } |
| 915 | EXPORT_SYMBOL_GPL(gigaset_start); |
| 916 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 917 | /** |
| 918 | * gigaset_shutdown() - shut down device operations |
| 919 | * @cs: device descriptor structure. |
| 920 | * |
| 921 | * Deactivates the device by scheduling an EV_SHUTDOWN event and |
| 922 | * waiting for completion of the shutdown. |
| 923 | * |
| 924 | * Return value: |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 925 | * 0 - success, -ENODEV - error (no device associated) |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 926 | */ |
| 927 | int gigaset_shutdown(struct cardstate *cs) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 928 | { |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 929 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 930 | |
Tilman Schmidt | 5d49c101 | 2008-03-07 19:47:08 +0100 | [diff] [blame] | 931 | if (!(cs->flags & VALID_MINOR)) { |
| 932 | mutex_unlock(&cs->mutex); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 933 | return -ENODEV; |
Tilman Schmidt | 5d49c101 | 2008-03-07 19:47:08 +0100 | [diff] [blame] | 934 | } |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 935 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 936 | cs->waiting = 1; |
| 937 | |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 938 | if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 939 | goto exit; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 940 | gigaset_schedule_event(cs); |
| 941 | |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 942 | wait_event(cs->waitqueue, !cs->waiting); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 943 | |
| 944 | cleanup_cs(cs); |
| 945 | |
| 946 | exit: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 947 | mutex_unlock(&cs->mutex); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 948 | return 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 949 | } |
| 950 | EXPORT_SYMBOL_GPL(gigaset_shutdown); |
| 951 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 952 | /** |
| 953 | * gigaset_stop() - stop device operations |
| 954 | * @cs: device descriptor structure. |
| 955 | * |
| 956 | * Stops operations on the device by scheduling an EV_STOP event and |
| 957 | * waiting for completion of the shutdown. |
| 958 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 959 | void gigaset_stop(struct cardstate *cs) |
| 960 | { |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 961 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 962 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 963 | cs->waiting = 1; |
| 964 | |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 965 | if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 966 | goto exit; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 967 | gigaset_schedule_event(cs); |
| 968 | |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 969 | wait_event(cs->waitqueue, !cs->waiting); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 970 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 971 | cleanup_cs(cs); |
| 972 | |
| 973 | exit: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 974 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 975 | } |
| 976 | EXPORT_SYMBOL_GPL(gigaset_stop); |
| 977 | |
| 978 | static LIST_HEAD(drivers); |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 979 | static DEFINE_SPINLOCK(driver_lock); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 980 | |
| 981 | struct cardstate *gigaset_get_cs_by_id(int id) |
| 982 | { |
| 983 | unsigned long flags; |
Tilman Schmidt | 35dc845 | 2007-03-29 01:20:34 -0700 | [diff] [blame] | 984 | struct cardstate *ret = NULL; |
| 985 | struct cardstate *cs; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 986 | struct gigaset_driver *drv; |
| 987 | unsigned i; |
| 988 | |
| 989 | spin_lock_irqsave(&driver_lock, flags); |
| 990 | list_for_each_entry(drv, &drivers, list) { |
| 991 | spin_lock(&drv->lock); |
| 992 | for (i = 0; i < drv->minors; ++i) { |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 993 | cs = drv->cs + i; |
| 994 | if ((cs->flags & VALID_ID) && cs->myid == id) { |
| 995 | ret = cs; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 996 | break; |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 997 | } |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 998 | } |
| 999 | spin_unlock(&drv->lock); |
| 1000 | if (ret) |
| 1001 | break; |
| 1002 | } |
| 1003 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
Adrian Bunk | 8ca445d | 2006-04-10 22:55:18 -0700 | [diff] [blame] | 1007 | static struct cardstate *gigaset_get_cs_by_minor(unsigned minor) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1008 | { |
| 1009 | unsigned long flags; |
Tilman Schmidt | 35dc845 | 2007-03-29 01:20:34 -0700 | [diff] [blame] | 1010 | struct cardstate *ret = NULL; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1011 | struct gigaset_driver *drv; |
| 1012 | unsigned index; |
| 1013 | |
| 1014 | spin_lock_irqsave(&driver_lock, flags); |
| 1015 | list_for_each_entry(drv, &drivers, list) { |
| 1016 | if (minor < drv->minor || minor >= drv->minor + drv->minors) |
| 1017 | continue; |
| 1018 | index = minor - drv->minor; |
| 1019 | spin_lock(&drv->lock); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 1020 | if (drv->cs[index].flags & VALID_MINOR) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1021 | ret = drv->cs + index; |
| 1022 | spin_unlock(&drv->lock); |
| 1023 | if (ret) |
| 1024 | break; |
| 1025 | } |
| 1026 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1027 | return ret; |
| 1028 | } |
| 1029 | |
Adrian Bunk | 8ca445d | 2006-04-10 22:55:18 -0700 | [diff] [blame] | 1030 | struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty) |
| 1031 | { |
Adrian Bunk | 8ca445d | 2006-04-10 22:55:18 -0700 | [diff] [blame] | 1032 | return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start); |
| 1033 | } |
| 1034 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 1035 | /** |
| 1036 | * gigaset_freedriver() - free all associated ressources of a driver |
| 1037 | * @drv: driver descriptor structure. |
| 1038 | * |
| 1039 | * Unregisters the driver from the system and deallocates the driver |
| 1040 | * structure @drv and all structures referenced from it. |
| 1041 | * All devices should be shut down before calling this. |
| 1042 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1043 | void gigaset_freedriver(struct gigaset_driver *drv) |
| 1044 | { |
| 1045 | unsigned long flags; |
| 1046 | |
| 1047 | spin_lock_irqsave(&driver_lock, flags); |
| 1048 | list_del(&drv->list); |
| 1049 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1050 | |
| 1051 | gigaset_if_freedriver(drv); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1052 | |
| 1053 | kfree(drv->cs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1054 | kfree(drv); |
| 1055 | } |
| 1056 | EXPORT_SYMBOL_GPL(gigaset_freedriver); |
| 1057 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 1058 | /** |
| 1059 | * gigaset_initdriver() - initialize driver structure |
| 1060 | * @minor: First minor number |
| 1061 | * @minors: Number of minors this driver can handle |
| 1062 | * @procname: Name of the driver |
| 1063 | * @devname: Name of the device files (prefix without minor number) |
| 1064 | * |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1065 | * Allocate and initialize gigaset_driver structure. Initialize interface. |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 1066 | * |
| 1067 | * Return value: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1068 | * Pointer to the gigaset_driver structure on success, NULL on failure. |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1069 | */ |
| 1070 | struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1071 | const char *procname, |
| 1072 | const char *devname, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1073 | const struct gigaset_ops *ops, |
| 1074 | struct module *owner) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1075 | { |
| 1076 | struct gigaset_driver *drv; |
| 1077 | unsigned long flags; |
| 1078 | unsigned i; |
| 1079 | |
| 1080 | drv = kmalloc(sizeof *drv, GFP_KERNEL); |
| 1081 | if (!drv) |
| 1082 | return NULL; |
Adrian Bunk | f4675c7 | 2006-04-10 22:55:19 -0700 | [diff] [blame] | 1083 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1084 | drv->have_tty = 0; |
| 1085 | drv->minor = minor; |
| 1086 | drv->minors = minors; |
| 1087 | spin_lock_init(&drv->lock); |
| 1088 | drv->blocked = 0; |
| 1089 | drv->ops = ops; |
| 1090 | drv->owner = owner; |
| 1091 | INIT_LIST_HEAD(&drv->list); |
| 1092 | |
| 1093 | drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL); |
| 1094 | if (!drv->cs) |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 1095 | goto error; |
Adrian Bunk | f4675c7 | 2006-04-10 22:55:19 -0700 | [diff] [blame] | 1096 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1097 | for (i = 0; i < minors; ++i) { |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 1098 | drv->cs[i].flags = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1099 | drv->cs[i].driver = drv; |
| 1100 | drv->cs[i].ops = drv->ops; |
| 1101 | drv->cs[i].minor_index = i; |
Tilman Schmidt | 5d49c101 | 2008-03-07 19:47:08 +0100 | [diff] [blame] | 1102 | mutex_init(&drv->cs[i].mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Greg Kroah-Hartman | f4eaa37 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1105 | gigaset_if_initdriver(drv, procname, devname); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1106 | |
| 1107 | spin_lock_irqsave(&driver_lock, flags); |
| 1108 | list_add(&drv->list, &drivers); |
| 1109 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1110 | |
| 1111 | return drv; |
| 1112 | |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 1113 | error: |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1114 | kfree(drv); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1115 | return NULL; |
| 1116 | } |
| 1117 | EXPORT_SYMBOL_GPL(gigaset_initdriver); |
| 1118 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 1119 | /** |
| 1120 | * gigaset_blockdriver() - block driver |
| 1121 | * @drv: driver descriptor structure. |
| 1122 | * |
| 1123 | * Prevents the driver from attaching new devices, in preparation for |
| 1124 | * deregistration. |
| 1125 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1126 | void gigaset_blockdriver(struct gigaset_driver *drv) |
| 1127 | { |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1128 | drv->blocked = 1; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1129 | } |
| 1130 | EXPORT_SYMBOL_GPL(gigaset_blockdriver); |
| 1131 | |
| 1132 | static int __init gigaset_init_module(void) |
| 1133 | { |
| 1134 | /* in accordance with the principle of least astonishment, |
| 1135 | * setting the 'debug' parameter to 1 activates a sensible |
| 1136 | * set of default debug levels |
| 1137 | */ |
| 1138 | if (gigaset_debuglevel == 1) |
| 1139 | gigaset_debuglevel = DEBUG_DEFAULT; |
| 1140 | |
Tilman Schmidt | 2038724 | 2009-10-06 12:18:41 +0000 | [diff] [blame] | 1141 | pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n"); |
Tilman Schmidt | bc35b4e | 2010-03-14 12:58:05 +0000 | [diff] [blame] | 1142 | gigaset_isdn_regdrv(); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | static void __exit gigaset_exit_module(void) |
| 1147 | { |
Tilman Schmidt | bc35b4e | 2010-03-14 12:58:05 +0000 | [diff] [blame] | 1148 | gigaset_isdn_unregdrv(); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | module_init(gigaset_init_module); |
| 1152 | module_exit(gigaset_exit_module); |
| 1153 | |
| 1154 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1155 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1156 | |
| 1157 | MODULE_LICENSE("GPL"); |