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