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