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); |
| 33 | module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR); |
| 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 | } |
| 126 | r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800); |
| 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); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 134 | cs->control_state = TIOCM_RTS|TIOCM_DTR; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 135 | cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR); |
| 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); |
| 197 | return 0; |
| 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); |
| 203 | return 1; |
| 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"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 261 | return 0; |
| 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 | |
| 269 | return 1; |
| 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 | |
| 365 | static void free_strings(struct at_state_t *at_state) |
| 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 | |
| 375 | static void clear_at_state(struct at_state_t *at_state) |
| 376 | { |
| 377 | free_strings(at_state); |
| 378 | } |
| 379 | |
| 380 | static void dealloc_at_states(struct cardstate *cs) |
| 381 | { |
| 382 | struct at_state_t *cur, *next; |
| 383 | |
| 384 | list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) { |
| 385 | list_del(&cur->list); |
| 386 | free_strings(cur); |
| 387 | kfree(cur); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | static void gigaset_freebcs(struct bc_state *bcs) |
| 392 | { |
| 393 | int i; |
| 394 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 395 | gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 396 | if (!bcs->cs->ops->freebcshw(bcs)) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 397 | gig_dbg(DEBUG_INIT, "failed"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 398 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 399 | gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 400 | clear_at_state(&bcs->at_state); |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 401 | gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel); |
Tilman Schmidt | e7752ee | 2010-06-21 13:54:19 +0000 | [diff] [blame] | 402 | dev_kfree_skb(bcs->rx_skb); |
| 403 | bcs->rx_skb = NULL; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 404 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 405 | for (i = 0; i < AT_NUM; ++i) { |
| 406 | kfree(bcs->commands[i]); |
| 407 | bcs->commands[i] = NULL; |
| 408 | } |
| 409 | } |
| 410 | |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 411 | static struct cardstate *alloc_cs(struct gigaset_driver *drv) |
| 412 | { |
| 413 | unsigned long flags; |
| 414 | unsigned i; |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 415 | struct cardstate *cs; |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 416 | struct cardstate *ret = NULL; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 417 | |
| 418 | spin_lock_irqsave(&drv->lock, flags); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 419 | if (drv->blocked) |
| 420 | goto exit; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 421 | for (i = 0; i < drv->minors; ++i) { |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 422 | cs = drv->cs + i; |
| 423 | if (!(cs->flags & VALID_MINOR)) { |
| 424 | cs->flags = VALID_MINOR; |
| 425 | ret = cs; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 426 | break; |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 427 | } |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 428 | } |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 429 | exit: |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 430 | spin_unlock_irqrestore(&drv->lock, flags); |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | static void free_cs(struct cardstate *cs) |
| 435 | { |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 436 | cs->flags = 0; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static void make_valid(struct cardstate *cs, unsigned mask) |
| 440 | { |
| 441 | unsigned long flags; |
| 442 | struct gigaset_driver *drv = cs->driver; |
| 443 | spin_lock_irqsave(&drv->lock, flags); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 444 | cs->flags |= mask; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 445 | spin_unlock_irqrestore(&drv->lock, flags); |
| 446 | } |
| 447 | |
| 448 | static void make_invalid(struct cardstate *cs, unsigned mask) |
| 449 | { |
| 450 | unsigned long flags; |
| 451 | struct gigaset_driver *drv = cs->driver; |
| 452 | spin_lock_irqsave(&drv->lock, flags); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 453 | cs->flags &= ~mask; |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 454 | spin_unlock_irqrestore(&drv->lock, flags); |
| 455 | } |
| 456 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 457 | /** |
| 458 | * gigaset_freecs() - free all associated ressources of a device |
| 459 | * @cs: device descriptor structure. |
| 460 | * |
| 461 | * Stops all tasklets and timers, unregisters the device from all |
| 462 | * subsystems it was registered to, deallocates the device structure |
| 463 | * @cs and all structures referenced from it. |
| 464 | * Operations on the device should be stopped before calling this. |
| 465 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 466 | void gigaset_freecs(struct cardstate *cs) |
| 467 | { |
| 468 | int i; |
| 469 | unsigned long flags; |
| 470 | |
| 471 | if (!cs) |
| 472 | return; |
| 473 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 474 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 475 | |
| 476 | if (!cs->bcs) |
| 477 | goto f_cs; |
| 478 | if (!cs->inbuf) |
| 479 | goto f_bcs; |
| 480 | |
| 481 | spin_lock_irqsave(&cs->lock, flags); |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 482 | cs->running = 0; |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 483 | spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are |
| 484 | not rescheduled below */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 485 | |
| 486 | tasklet_kill(&cs->event_tasklet); |
| 487 | del_timer_sync(&cs->timer); |
| 488 | |
| 489 | switch (cs->cs_init) { |
| 490 | default: |
Tilman Schmidt | 088ec0c | 2009-10-06 12:19:07 +0000 | [diff] [blame] | 491 | /* clear B channel structures */ |
| 492 | for (i = 0; i < cs->channels; ++i) { |
| 493 | gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i); |
| 494 | gigaset_freebcs(cs->bcs + i); |
| 495 | } |
| 496 | |
Hansjoerg Lipp | 3dda4e3 | 2006-04-22 18:43:00 +0200 | [diff] [blame] | 497 | /* clear device sysfs */ |
| 498 | gigaset_free_dev_sysfs(cs); |
| 499 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 500 | gigaset_if_free(cs); |
| 501 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 502 | gig_dbg(DEBUG_INIT, "clearing hw"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 503 | cs->ops->freecshw(cs); |
| 504 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 505 | /* fall through */ |
| 506 | case 2: /* error in initcshw */ |
| 507 | /* Deregister from LL */ |
| 508 | make_invalid(cs, VALID_ID); |
Tilman Schmidt | bc35b4e | 2010-03-14 12:58:05 +0000 | [diff] [blame] | 509 | gigaset_isdn_unregdev(cs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 510 | |
| 511 | /* fall through */ |
Tilman Schmidt | 088ec0c | 2009-10-06 12:19:07 +0000 | [diff] [blame] | 512 | case 1: /* error when registering to LL */ |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 513 | gig_dbg(DEBUG_INIT, "clearing at_state"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 514 | clear_at_state(&cs->at_state); |
| 515 | dealloc_at_states(cs); |
| 516 | |
| 517 | /* fall through */ |
Tilman Schmidt | 088ec0c | 2009-10-06 12:19:07 +0000 | [diff] [blame] | 518 | case 0: /* error in basic setup */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 519 | clear_events(cs); |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 520 | gig_dbg(DEBUG_INIT, "freeing inbuf"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 521 | kfree(cs->inbuf); |
| 522 | } |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 523 | f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 524 | kfree(cs->bcs); |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 525 | f_cs: gig_dbg(DEBUG_INIT, "freeing cs"); |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 526 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 527 | free_cs(cs); |
| 528 | } |
| 529 | EXPORT_SYMBOL_GPL(gigaset_freecs); |
| 530 | |
| 531 | 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] | 532 | struct cardstate *cs, int cid) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 533 | { |
| 534 | int i; |
| 535 | |
| 536 | INIT_LIST_HEAD(&at_state->list); |
| 537 | at_state->waiting = 0; |
| 538 | at_state->getstring = 0; |
| 539 | at_state->pending_commands = 0; |
| 540 | at_state->timer_expires = 0; |
| 541 | at_state->timer_active = 0; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 542 | at_state->timer_index = 0; |
| 543 | at_state->seq_index = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 544 | at_state->ConState = 0; |
| 545 | for (i = 0; i < STR_NUM; ++i) |
| 546 | at_state->str_var[i] = NULL; |
| 547 | at_state->int_var[VAR_ZDLE] = 0; |
| 548 | at_state->int_var[VAR_ZCTP] = -1; |
| 549 | at_state->int_var[VAR_ZSAU] = ZSAU_NULL; |
| 550 | at_state->cs = cs; |
| 551 | at_state->bcs = bcs; |
| 552 | at_state->cid = cid; |
| 553 | if (!cid) |
| 554 | at_state->replystruct = cs->tabnocid; |
| 555 | else |
| 556 | at_state->replystruct = cs->tabcid; |
| 557 | } |
| 558 | |
| 559 | |
Tilman Schmidt | 2032e2c | 2009-10-25 09:30:07 +0000 | [diff] [blame] | 560 | static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 561 | /* inbuf->read must be allocated before! */ |
| 562 | { |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 563 | inbuf->head = 0; |
| 564 | inbuf->tail = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 565 | inbuf->cs = cs; |
Tilman Schmidt | 2032e2c | 2009-10-25 09:30:07 +0000 | [diff] [blame] | 566 | inbuf->inputstate = INS_command; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 569 | /** |
| 570 | * gigaset_fill_inbuf() - append received data to input buffer |
| 571 | * @inbuf: buffer structure. |
| 572 | * @src: received data. |
| 573 | * @numbytes: number of bytes received. |
| 574 | */ |
Tilman Schmidt | 714e823 | 2006-04-10 22:55:09 -0700 | [diff] [blame] | 575 | int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src, |
| 576 | unsigned numbytes) |
| 577 | { |
| 578 | unsigned n, head, tail, bytesleft; |
| 579 | |
| 580 | gig_dbg(DEBUG_INTR, "received %u bytes", numbytes); |
| 581 | |
| 582 | if (!numbytes) |
| 583 | return 0; |
| 584 | |
| 585 | bytesleft = numbytes; |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 586 | tail = inbuf->tail; |
| 587 | head = inbuf->head; |
Tilman Schmidt | 714e823 | 2006-04-10 22:55:09 -0700 | [diff] [blame] | 588 | gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail); |
| 589 | |
| 590 | while (bytesleft) { |
| 591 | if (head > tail) |
| 592 | n = head - 1 - tail; |
| 593 | else if (head == 0) |
| 594 | n = (RBUFSIZE-1) - tail; |
| 595 | else |
| 596 | n = RBUFSIZE - tail; |
| 597 | if (!n) { |
| 598 | dev_err(inbuf->cs->dev, |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 599 | "buffer overflow (%u bytes lost)\n", |
| 600 | bytesleft); |
Tilman Schmidt | 714e823 | 2006-04-10 22:55:09 -0700 | [diff] [blame] | 601 | break; |
| 602 | } |
| 603 | if (n > bytesleft) |
| 604 | n = bytesleft; |
| 605 | memcpy(inbuf->data + tail, src, n); |
| 606 | bytesleft -= n; |
| 607 | tail = (tail + n) % RBUFSIZE; |
| 608 | src += n; |
| 609 | } |
| 610 | gig_dbg(DEBUG_INTR, "setting tail to %u", tail); |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 611 | inbuf->tail = tail; |
Tilman Schmidt | 714e823 | 2006-04-10 22:55:09 -0700 | [diff] [blame] | 612 | return numbytes != bytesleft; |
| 613 | } |
| 614 | EXPORT_SYMBOL_GPL(gigaset_fill_inbuf); |
| 615 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 616 | /* Initialize the b-channel structure */ |
| 617 | static struct bc_state *gigaset_initbcs(struct bc_state *bcs, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 618 | struct cardstate *cs, int channel) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 619 | { |
| 620 | int i; |
| 621 | |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 622 | bcs->tx_skb = NULL; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 623 | |
| 624 | skb_queue_head_init(&bcs->squeue); |
| 625 | |
| 626 | bcs->corrupted = 0; |
| 627 | bcs->trans_down = 0; |
| 628 | bcs->trans_up = 0; |
| 629 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 630 | gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 631 | gigaset_at_init(&bcs->at_state, bcs, cs, -1); |
| 632 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 633 | #ifdef CONFIG_GIGASET_DEBUG |
| 634 | bcs->emptycount = 0; |
| 635 | #endif |
| 636 | |
Tilman Schmidt | e7752ee | 2010-06-21 13:54:19 +0000 | [diff] [blame] | 637 | bcs->rx_bufsize = 0; |
| 638 | bcs->rx_skb = NULL; |
| 639 | bcs->rx_fcs = PPP_INITFCS; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 640 | bcs->inputstate = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 641 | bcs->channel = channel; |
| 642 | bcs->cs = cs; |
| 643 | |
| 644 | bcs->chstate = 0; |
| 645 | bcs->use_count = 1; |
| 646 | bcs->busy = 0; |
| 647 | bcs->ignore = cs->ignoreframes; |
| 648 | |
| 649 | for (i = 0; i < AT_NUM; ++i) |
| 650 | bcs->commands[i] = NULL; |
| 651 | |
Tilman Schmidt | 1b4843c | 2010-06-21 13:55:20 +0000 | [diff] [blame] | 652 | spin_lock_init(&bcs->aplock); |
| 653 | bcs->ap = NULL; |
| 654 | bcs->apconnstate = 0; |
| 655 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 656 | gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 657 | if (cs->ops->initbcshw(bcs)) |
| 658 | return bcs; |
| 659 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 660 | gig_dbg(DEBUG_INIT, " failed"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 661 | return NULL; |
| 662 | } |
| 663 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 664 | /** |
| 665 | * gigaset_initcs() - initialize device structure |
| 666 | * @drv: hardware driver the device belongs to |
| 667 | * @channels: number of B channels supported by device |
| 668 | * @onechannel: !=0 if B channel data and AT commands share one |
| 669 | * communication channel (M10x), |
| 670 | * ==0 if B channels have separate communication channels (base) |
| 671 | * @ignoreframes: number of frames to ignore after setting up B channel |
| 672 | * @cidmode: !=0: start in CallID mode |
| 673 | * @modulename: name of driver module for LL registration |
| 674 | * |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 675 | * Allocate and initialize cardstate structure for Gigaset driver |
| 676 | * Calls hardware dependent gigaset_initcshw() function |
| 677 | * Calls B channel initialization function gigaset_initbcs() for each B channel |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 678 | * |
| 679 | * Return value: |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 680 | * pointer to cardstate structure |
| 681 | */ |
| 682 | struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels, |
| 683 | int onechannel, int ignoreframes, |
| 684 | int cidmode, const char *modulename) |
| 685 | { |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 686 | struct cardstate *cs; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 687 | unsigned long flags; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 688 | int i; |
| 689 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 690 | gig_dbg(DEBUG_INIT, "allocating cs"); |
Tilman Schmidt | d9ba9c9 | 2009-10-25 09:30:17 +0000 | [diff] [blame] | 691 | cs = alloc_cs(drv); |
| 692 | if (!cs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 693 | pr_err("maximum number of devices exceeded\n"); |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 694 | return NULL; |
| 695 | } |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 696 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 697 | gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 698 | cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL); |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 699 | if (!cs->bcs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 700 | pr_err("out of memory\n"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 701 | goto error; |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 702 | } |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 703 | gig_dbg(DEBUG_INIT, "allocating inbuf"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 704 | cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL); |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 705 | if (!cs->inbuf) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 706 | pr_err("out of memory\n"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 707 | goto error; |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 708 | } |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 709 | |
| 710 | cs->cs_init = 0; |
| 711 | cs->channels = channels; |
| 712 | cs->onechannel = onechannel; |
| 713 | cs->ignoreframes = ignoreframes; |
| 714 | INIT_LIST_HEAD(&cs->temp_at_states); |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 715 | cs->running = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 716 | init_timer(&cs->timer); /* clear next & prev */ |
| 717 | spin_lock_init(&cs->ev_lock); |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 718 | cs->ev_tail = 0; |
| 719 | cs->ev_head = 0; |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 720 | |
Joe Perches | 5452fee | 2009-11-19 09:55:53 +0000 | [diff] [blame] | 721 | tasklet_init(&cs->event_tasklet, gigaset_handle_event, |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 722 | (unsigned long) cs); |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 723 | cs->commands_pending = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 724 | cs->cur_at_seq = 0; |
| 725 | cs->gotfwver = -1; |
| 726 | cs->open_count = 0; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 727 | cs->dev = NULL; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 728 | cs->tty = NULL; |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 729 | cs->tty_dev = NULL; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 730 | cs->cidmode = cidmode != 0; |
Tilman Schmidt | 528efc6 | 2009-05-13 12:44:17 +0000 | [diff] [blame] | 731 | cs->tabnocid = gigaset_tab_nocid; |
| 732 | cs->tabcid = gigaset_tab_cid; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 733 | |
| 734 | init_waitqueue_head(&cs->waitqueue); |
| 735 | cs->waiting = 0; |
| 736 | |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 737 | cs->mode = M_UNKNOWN; |
| 738 | cs->mstate = MS_UNINITIALIZED; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 739 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 740 | ++cs->cs_init; |
| 741 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 742 | gig_dbg(DEBUG_INIT, "setting up at_state"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 743 | spin_lock_init(&cs->lock); |
| 744 | gigaset_at_init(&cs->at_state, NULL, cs, 0); |
| 745 | cs->dle = 0; |
| 746 | cs->cbytes = 0; |
| 747 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 748 | gig_dbg(DEBUG_INIT, "setting up inbuf"); |
Tilman Schmidt | 2032e2c | 2009-10-25 09:30:07 +0000 | [diff] [blame] | 749 | gigaset_inbuf_init(cs->inbuf, cs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 750 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 751 | cs->connected = 0; |
| 752 | cs->isdn_up = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 753 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 754 | gig_dbg(DEBUG_INIT, "setting up cmdbuf"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 755 | cs->cmdbuf = cs->lastcmdbuf = NULL; |
| 756 | spin_lock_init(&cs->cmdlock); |
| 757 | cs->curlen = 0; |
| 758 | cs->cmdbytes = 0; |
| 759 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 760 | gig_dbg(DEBUG_INIT, "setting up iif"); |
Tilman Schmidt | bc35b4e | 2010-03-14 12:58:05 +0000 | [diff] [blame] | 761 | if (!gigaset_isdn_regdev(cs, modulename)) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 762 | pr_err("error registering ISDN device\n"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 763 | goto error; |
| 764 | } |
| 765 | |
| 766 | make_valid(cs, VALID_ID); |
| 767 | ++cs->cs_init; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 768 | gig_dbg(DEBUG_INIT, "setting up hw"); |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 769 | if (!cs->ops->initcshw(cs)) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 770 | goto error; |
| 771 | |
| 772 | ++cs->cs_init; |
| 773 | |
Tilman Schmidt | 7435f50 | 2007-02-12 00:52:24 -0800 | [diff] [blame] | 774 | /* set up character device */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 775 | gigaset_if_init(cs); |
| 776 | |
Hansjoerg Lipp | 3dda4e3 | 2006-04-22 18:43:00 +0200 | [diff] [blame] | 777 | /* set up device sysfs */ |
| 778 | gigaset_init_dev_sysfs(cs); |
| 779 | |
Tilman Schmidt | 088ec0c | 2009-10-06 12:19:07 +0000 | [diff] [blame] | 780 | /* set up channel data structures */ |
| 781 | for (i = 0; i < channels; ++i) { |
| 782 | gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i); |
| 783 | if (!gigaset_initbcs(cs->bcs + i, cs, i)) { |
| 784 | pr_err("could not allocate channel %d data\n", i); |
| 785 | goto error; |
| 786 | } |
| 787 | } |
| 788 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 789 | spin_lock_irqsave(&cs->lock, flags); |
| 790 | cs->running = 1; |
| 791 | spin_unlock_irqrestore(&cs->lock, flags); |
Tilman Schmidt | ec81b5e6 | 2006-04-10 22:55:03 -0700 | [diff] [blame] | 792 | setup_timer(&cs->timer, timer_tick, (unsigned long) cs); |
| 793 | cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 794 | add_timer(&cs->timer); |
| 795 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 796 | gig_dbg(DEBUG_INIT, "cs initialized"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 797 | return cs; |
| 798 | |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 799 | error: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 800 | gig_dbg(DEBUG_INIT, "failed"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 801 | gigaset_freecs(cs); |
| 802 | return NULL; |
| 803 | } |
| 804 | EXPORT_SYMBOL_GPL(gigaset_initcs); |
| 805 | |
Tilman Schmidt | 73a8881 | 2006-04-22 02:35:30 -0700 | [diff] [blame] | 806 | /* ReInitialize the b-channel structure on hangup */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 807 | void gigaset_bcs_reinit(struct bc_state *bcs) |
| 808 | { |
| 809 | struct sk_buff *skb; |
| 810 | struct cardstate *cs = bcs->cs; |
| 811 | unsigned long flags; |
| 812 | |
| 813 | while ((skb = skb_dequeue(&bcs->squeue)) != NULL) |
| 814 | dev_kfree_skb(skb); |
| 815 | |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 816 | spin_lock_irqsave(&cs->lock, flags); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 817 | clear_at_state(&bcs->at_state); |
| 818 | bcs->at_state.ConState = 0; |
| 819 | bcs->at_state.timer_active = 0; |
| 820 | bcs->at_state.timer_expires = 0; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 821 | bcs->at_state.cid = -1; /* No CID defined */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 822 | spin_unlock_irqrestore(&cs->lock, flags); |
| 823 | |
| 824 | bcs->inputstate = 0; |
| 825 | |
| 826 | #ifdef CONFIG_GIGASET_DEBUG |
| 827 | bcs->emptycount = 0; |
| 828 | #endif |
| 829 | |
Tilman Schmidt | e7752ee | 2010-06-21 13:54:19 +0000 | [diff] [blame] | 830 | bcs->rx_fcs = PPP_INITFCS; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 831 | bcs->chstate = 0; |
| 832 | |
| 833 | bcs->ignore = cs->ignoreframes; |
Tilman Schmidt | e7752ee | 2010-06-21 13:54:19 +0000 | [diff] [blame] | 834 | dev_kfree_skb(bcs->rx_skb); |
| 835 | bcs->rx_skb = NULL; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 836 | |
| 837 | cs->ops->reinitbcshw(bcs); |
| 838 | } |
| 839 | |
| 840 | static void cleanup_cs(struct cardstate *cs) |
| 841 | { |
| 842 | struct cmdbuf_t *cb, *tcb; |
| 843 | int i; |
| 844 | unsigned long flags; |
| 845 | |
| 846 | spin_lock_irqsave(&cs->lock, flags); |
| 847 | |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 848 | cs->mode = M_UNKNOWN; |
| 849 | cs->mstate = MS_UNINITIALIZED; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 850 | |
| 851 | clear_at_state(&cs->at_state); |
| 852 | dealloc_at_states(cs); |
| 853 | free_strings(&cs->at_state); |
| 854 | gigaset_at_init(&cs->at_state, NULL, cs, 0); |
| 855 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 856 | cs->inbuf->inputstate = INS_command; |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 857 | cs->inbuf->head = 0; |
| 858 | cs->inbuf->tail = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 859 | |
| 860 | cb = cs->cmdbuf; |
| 861 | while (cb) { |
| 862 | tcb = cb; |
| 863 | cb = cb->next; |
| 864 | kfree(tcb); |
| 865 | } |
| 866 | cs->cmdbuf = cs->lastcmdbuf = NULL; |
| 867 | cs->curlen = 0; |
| 868 | cs->cmdbytes = 0; |
| 869 | cs->gotfwver = -1; |
| 870 | cs->dle = 0; |
| 871 | cs->cur_at_seq = 0; |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 872 | cs->commands_pending = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 873 | cs->cbytes = 0; |
| 874 | |
| 875 | spin_unlock_irqrestore(&cs->lock, flags); |
| 876 | |
| 877 | for (i = 0; i < cs->channels; ++i) { |
| 878 | gigaset_freebcs(cs->bcs + i); |
| 879 | if (!gigaset_initbcs(cs->bcs + i, cs, i)) |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 880 | pr_err("could not allocate channel %d data\n", i); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | if (cs->waiting) { |
| 884 | cs->cmd_result = -ENODEV; |
| 885 | cs->waiting = 0; |
| 886 | wake_up_interruptible(&cs->waitqueue); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 891 | /** |
| 892 | * gigaset_start() - start device operations |
| 893 | * @cs: device descriptor structure. |
| 894 | * |
| 895 | * Prepares the device for use by setting up communication parameters, |
| 896 | * scheduling an EV_START event to initiate device initialization, and |
| 897 | * waiting for completion of the initialization. |
| 898 | * |
| 899 | * Return value: |
| 900 | * 1 - success, 0 - error |
| 901 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 902 | int gigaset_start(struct cardstate *cs) |
| 903 | { |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 904 | unsigned long flags; |
| 905 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 906 | if (mutex_lock_interruptible(&cs->mutex)) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 907 | return 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 908 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame] | 909 | spin_lock_irqsave(&cs->lock, flags); |
| 910 | cs->connected = 1; |
| 911 | spin_unlock_irqrestore(&cs->lock, flags); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 912 | |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 913 | if (cs->mstate != MS_LOCKED) { |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 914 | cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS); |
| 915 | cs->ops->baud_rate(cs, B115200); |
| 916 | cs->ops->set_line_ctrl(cs, CS8); |
| 917 | cs->control_state = TIOCM_DTR|TIOCM_RTS; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | cs->waiting = 1; |
| 921 | |
| 922 | if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) { |
| 923 | cs->waiting = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 924 | goto error; |
| 925 | } |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 926 | gigaset_schedule_event(cs); |
| 927 | |
| 928 | wait_event(cs->waitqueue, !cs->waiting); |
| 929 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 930 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 931 | return 1; |
| 932 | |
| 933 | error: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 934 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 935 | return 0; |
| 936 | } |
| 937 | EXPORT_SYMBOL_GPL(gigaset_start); |
| 938 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 939 | /** |
| 940 | * gigaset_shutdown() - shut down device operations |
| 941 | * @cs: device descriptor structure. |
| 942 | * |
| 943 | * Deactivates the device by scheduling an EV_SHUTDOWN event and |
| 944 | * waiting for completion of the shutdown. |
| 945 | * |
| 946 | * Return value: |
| 947 | * 0 - success, -1 - error (no device associated) |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 948 | */ |
| 949 | int gigaset_shutdown(struct cardstate *cs) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 950 | { |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 951 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 952 | |
Tilman Schmidt | 5d49c101 | 2008-03-07 19:47:08 +0100 | [diff] [blame] | 953 | if (!(cs->flags & VALID_MINOR)) { |
| 954 | mutex_unlock(&cs->mutex); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 955 | return -1; |
Tilman Schmidt | 5d49c101 | 2008-03-07 19:47:08 +0100 | [diff] [blame] | 956 | } |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 957 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 958 | cs->waiting = 1; |
| 959 | |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 960 | 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] | 961 | goto exit; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 962 | gigaset_schedule_event(cs); |
| 963 | |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 964 | wait_event(cs->waitqueue, !cs->waiting); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 965 | |
| 966 | cleanup_cs(cs); |
| 967 | |
| 968 | exit: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 969 | mutex_unlock(&cs->mutex); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 970 | return 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 971 | } |
| 972 | EXPORT_SYMBOL_GPL(gigaset_shutdown); |
| 973 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 974 | /** |
| 975 | * gigaset_stop() - stop device operations |
| 976 | * @cs: device descriptor structure. |
| 977 | * |
| 978 | * Stops operations on the device by scheduling an EV_STOP event and |
| 979 | * waiting for completion of the shutdown. |
| 980 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 981 | void gigaset_stop(struct cardstate *cs) |
| 982 | { |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 983 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 984 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 985 | cs->waiting = 1; |
| 986 | |
Tilman Schmidt | 1528b18 | 2010-02-22 13:09:52 +0000 | [diff] [blame] | 987 | 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] | 988 | goto exit; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 989 | gigaset_schedule_event(cs); |
| 990 | |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 991 | wait_event(cs->waitqueue, !cs->waiting); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 992 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 993 | cleanup_cs(cs); |
| 994 | |
| 995 | exit: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 996 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 997 | } |
| 998 | EXPORT_SYMBOL_GPL(gigaset_stop); |
| 999 | |
| 1000 | static LIST_HEAD(drivers); |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 1001 | static DEFINE_SPINLOCK(driver_lock); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1002 | |
| 1003 | struct cardstate *gigaset_get_cs_by_id(int id) |
| 1004 | { |
| 1005 | unsigned long flags; |
Tilman Schmidt | 35dc845 | 2007-03-29 01:20:34 -0700 | [diff] [blame] | 1006 | struct cardstate *ret = NULL; |
| 1007 | struct cardstate *cs; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1008 | struct gigaset_driver *drv; |
| 1009 | unsigned i; |
| 1010 | |
| 1011 | spin_lock_irqsave(&driver_lock, flags); |
| 1012 | list_for_each_entry(drv, &drivers, list) { |
| 1013 | spin_lock(&drv->lock); |
| 1014 | for (i = 0; i < drv->minors; ++i) { |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 1015 | cs = drv->cs + i; |
| 1016 | if ((cs->flags & VALID_ID) && cs->myid == id) { |
| 1017 | ret = cs; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1018 | break; |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 1019 | } |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1020 | } |
| 1021 | spin_unlock(&drv->lock); |
| 1022 | if (ret) |
| 1023 | break; |
| 1024 | } |
| 1025 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1026 | return ret; |
| 1027 | } |
| 1028 | |
Adrian Bunk | 8ca445d | 2006-04-10 22:55:18 -0700 | [diff] [blame] | 1029 | static struct cardstate *gigaset_get_cs_by_minor(unsigned minor) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1030 | { |
| 1031 | unsigned long flags; |
Tilman Schmidt | 35dc845 | 2007-03-29 01:20:34 -0700 | [diff] [blame] | 1032 | struct cardstate *ret = NULL; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1033 | struct gigaset_driver *drv; |
| 1034 | unsigned index; |
| 1035 | |
| 1036 | spin_lock_irqsave(&driver_lock, flags); |
| 1037 | list_for_each_entry(drv, &drivers, list) { |
| 1038 | if (minor < drv->minor || minor >= drv->minor + drv->minors) |
| 1039 | continue; |
| 1040 | index = minor - drv->minor; |
| 1041 | spin_lock(&drv->lock); |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 1042 | if (drv->cs[index].flags & VALID_MINOR) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1043 | ret = drv->cs + index; |
| 1044 | spin_unlock(&drv->lock); |
| 1045 | if (ret) |
| 1046 | break; |
| 1047 | } |
| 1048 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
Adrian Bunk | 8ca445d | 2006-04-10 22:55:18 -0700 | [diff] [blame] | 1052 | struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty) |
| 1053 | { |
| 1054 | if (tty->index < 0 || tty->index >= tty->driver->num) |
| 1055 | return NULL; |
| 1056 | return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start); |
| 1057 | } |
| 1058 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 1059 | /** |
| 1060 | * gigaset_freedriver() - free all associated ressources of a driver |
| 1061 | * @drv: driver descriptor structure. |
| 1062 | * |
| 1063 | * Unregisters the driver from the system and deallocates the driver |
| 1064 | * structure @drv and all structures referenced from it. |
| 1065 | * All devices should be shut down before calling this. |
| 1066 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1067 | void gigaset_freedriver(struct gigaset_driver *drv) |
| 1068 | { |
| 1069 | unsigned long flags; |
| 1070 | |
| 1071 | spin_lock_irqsave(&driver_lock, flags); |
| 1072 | list_del(&drv->list); |
| 1073 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1074 | |
| 1075 | gigaset_if_freedriver(drv); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1076 | |
| 1077 | kfree(drv->cs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1078 | kfree(drv); |
| 1079 | } |
| 1080 | EXPORT_SYMBOL_GPL(gigaset_freedriver); |
| 1081 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 1082 | /** |
| 1083 | * gigaset_initdriver() - initialize driver structure |
| 1084 | * @minor: First minor number |
| 1085 | * @minors: Number of minors this driver can handle |
| 1086 | * @procname: Name of the driver |
| 1087 | * @devname: Name of the device files (prefix without minor number) |
| 1088 | * |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1089 | * Allocate and initialize gigaset_driver structure. Initialize interface. |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 1090 | * |
| 1091 | * Return value: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1092 | * Pointer to the gigaset_driver structure on success, NULL on failure. |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1093 | */ |
| 1094 | struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1095 | const char *procname, |
| 1096 | const char *devname, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1097 | const struct gigaset_ops *ops, |
| 1098 | struct module *owner) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1099 | { |
| 1100 | struct gigaset_driver *drv; |
| 1101 | unsigned long flags; |
| 1102 | unsigned i; |
| 1103 | |
| 1104 | drv = kmalloc(sizeof *drv, GFP_KERNEL); |
| 1105 | if (!drv) |
| 1106 | return NULL; |
Adrian Bunk | f4675c7 | 2006-04-10 22:55:19 -0700 | [diff] [blame] | 1107 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1108 | drv->have_tty = 0; |
| 1109 | drv->minor = minor; |
| 1110 | drv->minors = minors; |
| 1111 | spin_lock_init(&drv->lock); |
| 1112 | drv->blocked = 0; |
| 1113 | drv->ops = ops; |
| 1114 | drv->owner = owner; |
| 1115 | INIT_LIST_HEAD(&drv->list); |
| 1116 | |
| 1117 | drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL); |
| 1118 | if (!drv->cs) |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 1119 | goto error; |
Adrian Bunk | f4675c7 | 2006-04-10 22:55:19 -0700 | [diff] [blame] | 1120 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1121 | for (i = 0; i < minors; ++i) { |
Tilman Schmidt | e468c04 | 2008-02-06 01:38:29 -0800 | [diff] [blame] | 1122 | drv->cs[i].flags = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1123 | drv->cs[i].driver = drv; |
| 1124 | drv->cs[i].ops = drv->ops; |
| 1125 | drv->cs[i].minor_index = i; |
Tilman Schmidt | 5d49c101 | 2008-03-07 19:47:08 +0100 | [diff] [blame] | 1126 | mutex_init(&drv->cs[i].mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1127 | } |
| 1128 | |
Greg Kroah-Hartman | f4eaa37 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1129 | gigaset_if_initdriver(drv, procname, devname); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1130 | |
| 1131 | spin_lock_irqsave(&driver_lock, flags); |
| 1132 | list_add(&drv->list, &drivers); |
| 1133 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1134 | |
| 1135 | return drv; |
| 1136 | |
Tilman Schmidt | e702ff0 | 2007-01-26 00:56:56 -0800 | [diff] [blame] | 1137 | error: |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1138 | kfree(drv->cs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1139 | kfree(drv); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1140 | return NULL; |
| 1141 | } |
| 1142 | EXPORT_SYMBOL_GPL(gigaset_initdriver); |
| 1143 | |
Tilman Schmidt | 1cec972 | 2009-10-06 12:19:01 +0000 | [diff] [blame] | 1144 | /** |
| 1145 | * gigaset_blockdriver() - block driver |
| 1146 | * @drv: driver descriptor structure. |
| 1147 | * |
| 1148 | * Prevents the driver from attaching new devices, in preparation for |
| 1149 | * deregistration. |
| 1150 | */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1151 | void gigaset_blockdriver(struct gigaset_driver *drv) |
| 1152 | { |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1153 | drv->blocked = 1; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1154 | } |
| 1155 | EXPORT_SYMBOL_GPL(gigaset_blockdriver); |
| 1156 | |
| 1157 | static int __init gigaset_init_module(void) |
| 1158 | { |
| 1159 | /* in accordance with the principle of least astonishment, |
| 1160 | * setting the 'debug' parameter to 1 activates a sensible |
| 1161 | * set of default debug levels |
| 1162 | */ |
| 1163 | if (gigaset_debuglevel == 1) |
| 1164 | gigaset_debuglevel = DEBUG_DEFAULT; |
| 1165 | |
Tilman Schmidt | 2038724 | 2009-10-06 12:18:41 +0000 | [diff] [blame] | 1166 | pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n"); |
Tilman Schmidt | bc35b4e | 2010-03-14 12:58:05 +0000 | [diff] [blame] | 1167 | gigaset_isdn_regdrv(); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | static void __exit gigaset_exit_module(void) |
| 1172 | { |
Tilman Schmidt | bc35b4e | 2010-03-14 12:58:05 +0000 | [diff] [blame] | 1173 | gigaset_isdn_unregdrv(); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | module_init(gigaset_init_module); |
| 1177 | module_exit(gigaset_exit_module); |
| 1178 | |
| 1179 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1180 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1181 | |
| 1182 | MODULE_LICENSE("GPL"); |