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