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