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 | |
| 36 | /* bitwise byte inversion table */ |
| 37 | __u8 gigaset_invtab[256] = { |
| 38 | 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, |
| 39 | 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, |
| 40 | 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, |
| 41 | 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, |
| 42 | 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, |
| 43 | 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, |
| 44 | 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, |
| 45 | 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, |
| 46 | 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, |
| 47 | 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, |
| 48 | 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, |
| 49 | 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, |
| 50 | 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, |
| 51 | 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, |
| 52 | 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, |
| 53 | 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, |
| 54 | 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, |
| 55 | 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, |
| 56 | 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, |
| 57 | 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, |
| 58 | 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, |
| 59 | 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, |
| 60 | 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, |
| 61 | 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, |
| 62 | 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, |
| 63 | 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, |
| 64 | 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, |
| 65 | 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, |
| 66 | 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, |
| 67 | 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, |
| 68 | 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, |
| 69 | 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff |
| 70 | }; |
| 71 | EXPORT_SYMBOL_GPL(gigaset_invtab); |
| 72 | |
| 73 | void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg, |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 74 | size_t len, const unsigned char *buf) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 75 | { |
| 76 | unsigned char outbuf[80]; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 77 | unsigned char c; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 78 | size_t space = sizeof outbuf - 1; |
| 79 | unsigned char *out = outbuf; |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 80 | size_t numin = len; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 81 | |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 82 | while (numin--) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 83 | c = *buf++; |
| 84 | if (c == '~' || c == '^' || c == '\\') { |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 85 | if (!space--) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 86 | break; |
| 87 | *out++ = '\\'; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 88 | } |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 89 | if (c & 0x80) { |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 90 | if (!space--) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 91 | break; |
| 92 | *out++ = '~'; |
| 93 | c ^= 0x80; |
| 94 | } |
| 95 | if (c < 0x20 || c == 0x7f) { |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 96 | if (!space--) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 97 | break; |
| 98 | *out++ = '^'; |
| 99 | c ^= 0x40; |
| 100 | } |
Tilman Schmidt | 0137150 | 2006-04-10 22:55:11 -0700 | [diff] [blame] | 101 | if (!space--) |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 102 | break; |
| 103 | *out++ = c; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 104 | } |
| 105 | *out = 0; |
| 106 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 107 | gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 108 | } |
| 109 | EXPORT_SYMBOL_GPL(gigaset_dbg_buffer); |
| 110 | |
| 111 | static int setflags(struct cardstate *cs, unsigned flags, unsigned delay) |
| 112 | { |
| 113 | int r; |
| 114 | |
| 115 | r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags); |
| 116 | cs->control_state = flags; |
| 117 | if (r < 0) |
| 118 | return r; |
| 119 | |
| 120 | if (delay) { |
| 121 | set_current_state(TASK_INTERRUPTIBLE); |
| 122 | schedule_timeout(delay * HZ / 1000); |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int gigaset_enterconfigmode(struct cardstate *cs) |
| 129 | { |
| 130 | int i, r; |
| 131 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 132 | cs->control_state = TIOCM_RTS; //FIXME |
| 133 | |
| 134 | r = setflags(cs, TIOCM_DTR, 200); |
| 135 | if (r < 0) |
| 136 | goto error; |
| 137 | r = setflags(cs, 0, 200); |
| 138 | if (r < 0) |
| 139 | goto error; |
| 140 | for (i = 0; i < 5; ++i) { |
| 141 | r = setflags(cs, TIOCM_RTS, 100); |
| 142 | if (r < 0) |
| 143 | goto error; |
| 144 | r = setflags(cs, 0, 100); |
| 145 | if (r < 0) |
| 146 | goto error; |
| 147 | } |
| 148 | r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800); |
| 149 | if (r < 0) |
| 150 | goto error; |
| 151 | |
| 152 | return 0; |
| 153 | |
| 154 | error: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 155 | dev_err(cs->dev, "error %d on setuartbits\n", -r); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 156 | cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value? |
| 157 | cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR); |
| 158 | |
| 159 | return -1; //r |
| 160 | } |
| 161 | |
| 162 | static int test_timeout(struct at_state_t *at_state) |
| 163 | { |
| 164 | if (!at_state->timer_expires) |
| 165 | return 0; |
| 166 | |
| 167 | if (--at_state->timer_expires) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 168 | gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu", |
| 169 | at_state, at_state->timer_expires); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL, |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 174 | at_state->timer_index, NULL)) { |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 175 | //FIXME what should we do? |
| 176 | } |
| 177 | |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | static void timer_tick(unsigned long data) |
| 182 | { |
| 183 | struct cardstate *cs = (struct cardstate *) data; |
| 184 | unsigned long flags; |
| 185 | unsigned channel; |
| 186 | struct at_state_t *at_state; |
| 187 | int timeout = 0; |
| 188 | |
| 189 | spin_lock_irqsave(&cs->lock, flags); |
| 190 | |
| 191 | for (channel = 0; channel < cs->channels; ++channel) |
| 192 | if (test_timeout(&cs->bcs[channel].at_state)) |
| 193 | timeout = 1; |
| 194 | |
| 195 | if (test_timeout(&cs->at_state)) |
| 196 | timeout = 1; |
| 197 | |
| 198 | list_for_each_entry(at_state, &cs->temp_at_states, list) |
| 199 | if (test_timeout(at_state)) |
| 200 | timeout = 1; |
| 201 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 202 | if (cs->running) { |
Tilman Schmidt | ec81b5e6 | 2006-04-10 22:55:03 -0700 | [diff] [blame] | 203 | mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK)); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 204 | if (timeout) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 205 | gig_dbg(DEBUG_CMD, "scheduling timeout"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 206 | tasklet_schedule(&cs->event_tasklet); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | spin_unlock_irqrestore(&cs->lock, flags); |
| 211 | } |
| 212 | |
| 213 | int gigaset_get_channel(struct bc_state *bcs) |
| 214 | { |
| 215 | unsigned long flags; |
| 216 | |
| 217 | spin_lock_irqsave(&bcs->cs->lock, flags); |
| 218 | if (bcs->use_count) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 219 | gig_dbg(DEBUG_ANY, "could not allocate channel %d", |
| 220 | bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 221 | spin_unlock_irqrestore(&bcs->cs->lock, flags); |
| 222 | return 0; |
| 223 | } |
| 224 | ++bcs->use_count; |
| 225 | bcs->busy = 1; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 226 | gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 227 | spin_unlock_irqrestore(&bcs->cs->lock, flags); |
| 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | void gigaset_free_channel(struct bc_state *bcs) |
| 232 | { |
| 233 | unsigned long flags; |
| 234 | |
| 235 | spin_lock_irqsave(&bcs->cs->lock, flags); |
| 236 | if (!bcs->busy) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 237 | gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 238 | spin_unlock_irqrestore(&bcs->cs->lock, flags); |
| 239 | return; |
| 240 | } |
| 241 | --bcs->use_count; |
| 242 | bcs->busy = 0; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 243 | gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 244 | spin_unlock_irqrestore(&bcs->cs->lock, flags); |
| 245 | } |
| 246 | |
| 247 | int gigaset_get_channels(struct cardstate *cs) |
| 248 | { |
| 249 | unsigned long flags; |
| 250 | int i; |
| 251 | |
| 252 | spin_lock_irqsave(&cs->lock, flags); |
| 253 | for (i = 0; i < cs->channels; ++i) |
| 254 | if (cs->bcs[i].use_count) { |
| 255 | spin_unlock_irqrestore(&cs->lock, flags); |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 256 | gig_dbg(DEBUG_ANY, "could not allocate all channels"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | for (i = 0; i < cs->channels; ++i) |
| 260 | ++cs->bcs[i].use_count; |
| 261 | spin_unlock_irqrestore(&cs->lock, flags); |
| 262 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 263 | gig_dbg(DEBUG_ANY, "allocated all channels"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 264 | |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | void gigaset_free_channels(struct cardstate *cs) |
| 269 | { |
| 270 | unsigned long flags; |
| 271 | int i; |
| 272 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 273 | gig_dbg(DEBUG_ANY, "unblocking all channels"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 274 | spin_lock_irqsave(&cs->lock, flags); |
| 275 | for (i = 0; i < cs->channels; ++i) |
| 276 | --cs->bcs[i].use_count; |
| 277 | spin_unlock_irqrestore(&cs->lock, flags); |
| 278 | } |
| 279 | |
| 280 | void gigaset_block_channels(struct cardstate *cs) |
| 281 | { |
| 282 | unsigned long flags; |
| 283 | int i; |
| 284 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 285 | gig_dbg(DEBUG_ANY, "blocking all channels"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 286 | spin_lock_irqsave(&cs->lock, flags); |
| 287 | for (i = 0; i < cs->channels; ++i) |
| 288 | ++cs->bcs[i].use_count; |
| 289 | spin_unlock_irqrestore(&cs->lock, flags); |
| 290 | } |
| 291 | |
| 292 | static void clear_events(struct cardstate *cs) |
| 293 | { |
| 294 | struct event_t *ev; |
| 295 | unsigned head, tail; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 296 | unsigned long flags; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 297 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 298 | spin_lock_irqsave(&cs->ev_lock, flags); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 299 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 300 | head = cs->ev_head; |
| 301 | tail = cs->ev_tail; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 302 | |
| 303 | while (tail != head) { |
| 304 | ev = cs->events + head; |
| 305 | kfree(ev->ptr); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 306 | head = (head + 1) % MAX_EVENTS; |
| 307 | } |
| 308 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 309 | cs->ev_head = tail; |
| 310 | |
| 311 | spin_unlock_irqrestore(&cs->ev_lock, flags); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | struct event_t *gigaset_add_event(struct cardstate *cs, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 315 | struct at_state_t *at_state, int type, |
| 316 | void *ptr, int parameter, void *arg) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 317 | { |
| 318 | unsigned long flags; |
| 319 | unsigned next, tail; |
| 320 | struct event_t *event = NULL; |
| 321 | |
| 322 | spin_lock_irqsave(&cs->ev_lock, flags); |
| 323 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 324 | tail = cs->ev_tail; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 325 | next = (tail + 1) % MAX_EVENTS; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 326 | if (unlikely(next == cs->ev_head)) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 327 | err("event queue full"); |
| 328 | else { |
| 329 | event = cs->events + tail; |
| 330 | event->type = type; |
| 331 | event->at_state = at_state; |
| 332 | event->cid = -1; |
| 333 | event->ptr = ptr; |
| 334 | event->arg = arg; |
| 335 | event->parameter = parameter; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 336 | cs->ev_tail = next; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | spin_unlock_irqrestore(&cs->ev_lock, flags); |
| 340 | |
| 341 | return event; |
| 342 | } |
| 343 | EXPORT_SYMBOL_GPL(gigaset_add_event); |
| 344 | |
| 345 | static void free_strings(struct at_state_t *at_state) |
| 346 | { |
| 347 | int i; |
| 348 | |
| 349 | for (i = 0; i < STR_NUM; ++i) { |
| 350 | kfree(at_state->str_var[i]); |
| 351 | at_state->str_var[i] = NULL; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static void clear_at_state(struct at_state_t *at_state) |
| 356 | { |
| 357 | free_strings(at_state); |
| 358 | } |
| 359 | |
| 360 | static void dealloc_at_states(struct cardstate *cs) |
| 361 | { |
| 362 | struct at_state_t *cur, *next; |
| 363 | |
| 364 | list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) { |
| 365 | list_del(&cur->list); |
| 366 | free_strings(cur); |
| 367 | kfree(cur); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | static void gigaset_freebcs(struct bc_state *bcs) |
| 372 | { |
| 373 | int i; |
| 374 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 375 | gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 376 | if (!bcs->cs->ops->freebcshw(bcs)) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 377 | gig_dbg(DEBUG_INIT, "failed"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 380 | gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 381 | clear_at_state(&bcs->at_state); |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 382 | gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 383 | |
| 384 | if (bcs->skb) |
| 385 | dev_kfree_skb(bcs->skb); |
| 386 | for (i = 0; i < AT_NUM; ++i) { |
| 387 | kfree(bcs->commands[i]); |
| 388 | bcs->commands[i] = NULL; |
| 389 | } |
| 390 | } |
| 391 | |
Tilman Schmidt | 70440cf | 2006-04-10 22:55:14 -0700 | [diff] [blame] | 392 | static struct cardstate *alloc_cs(struct gigaset_driver *drv) |
| 393 | { |
| 394 | unsigned long flags; |
| 395 | unsigned i; |
| 396 | static struct cardstate *ret = NULL; |
| 397 | |
| 398 | spin_lock_irqsave(&drv->lock, flags); |
| 399 | for (i = 0; i < drv->minors; ++i) { |
| 400 | if (!(drv->flags[i] & VALID_MINOR)) { |
| 401 | drv->flags[i] = VALID_MINOR; |
| 402 | ret = drv->cs + i; |
| 403 | } |
| 404 | if (ret) |
| 405 | break; |
| 406 | } |
| 407 | spin_unlock_irqrestore(&drv->lock, flags); |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | static void free_cs(struct cardstate *cs) |
| 412 | { |
| 413 | unsigned long flags; |
| 414 | struct gigaset_driver *drv = cs->driver; |
| 415 | spin_lock_irqsave(&drv->lock, flags); |
| 416 | drv->flags[cs->minor_index] = 0; |
| 417 | spin_unlock_irqrestore(&drv->lock, flags); |
| 418 | } |
| 419 | |
| 420 | static void make_valid(struct cardstate *cs, unsigned mask) |
| 421 | { |
| 422 | unsigned long flags; |
| 423 | struct gigaset_driver *drv = cs->driver; |
| 424 | spin_lock_irqsave(&drv->lock, flags); |
| 425 | drv->flags[cs->minor_index] |= mask; |
| 426 | spin_unlock_irqrestore(&drv->lock, flags); |
| 427 | } |
| 428 | |
| 429 | static void make_invalid(struct cardstate *cs, unsigned mask) |
| 430 | { |
| 431 | unsigned long flags; |
| 432 | struct gigaset_driver *drv = cs->driver; |
| 433 | spin_lock_irqsave(&drv->lock, flags); |
| 434 | drv->flags[cs->minor_index] &= ~mask; |
| 435 | spin_unlock_irqrestore(&drv->lock, flags); |
| 436 | } |
| 437 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 438 | void gigaset_freecs(struct cardstate *cs) |
| 439 | { |
| 440 | int i; |
| 441 | unsigned long flags; |
| 442 | |
| 443 | if (!cs) |
| 444 | return; |
| 445 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 446 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 447 | |
| 448 | if (!cs->bcs) |
| 449 | goto f_cs; |
| 450 | if (!cs->inbuf) |
| 451 | goto f_bcs; |
| 452 | |
| 453 | spin_lock_irqsave(&cs->lock, flags); |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 454 | cs->running = 0; |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 455 | spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are |
| 456 | not rescheduled below */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 457 | |
| 458 | tasklet_kill(&cs->event_tasklet); |
| 459 | del_timer_sync(&cs->timer); |
| 460 | |
| 461 | switch (cs->cs_init) { |
| 462 | default: |
| 463 | gigaset_if_free(cs); |
| 464 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 465 | gig_dbg(DEBUG_INIT, "clearing hw"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 466 | cs->ops->freecshw(cs); |
| 467 | |
| 468 | //FIXME cmdbuf |
| 469 | |
| 470 | /* fall through */ |
| 471 | case 2: /* error in initcshw */ |
| 472 | /* Deregister from LL */ |
| 473 | make_invalid(cs, VALID_ID); |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 474 | gig_dbg(DEBUG_INIT, "clearing iif"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 475 | gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD); |
| 476 | |
| 477 | /* fall through */ |
| 478 | case 1: /* error when regestering to LL */ |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 479 | gig_dbg(DEBUG_INIT, "clearing at_state"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 480 | clear_at_state(&cs->at_state); |
| 481 | dealloc_at_states(cs); |
| 482 | |
| 483 | /* fall through */ |
| 484 | case 0: /* error in one call to initbcs */ |
| 485 | for (i = 0; i < cs->channels; ++i) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 486 | gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 487 | gigaset_freebcs(cs->bcs + i); |
| 488 | } |
| 489 | |
| 490 | clear_events(cs); |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 491 | gig_dbg(DEBUG_INIT, "freeing inbuf"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 492 | kfree(cs->inbuf); |
| 493 | } |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 494 | f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 495 | kfree(cs->bcs); |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 496 | f_cs: gig_dbg(DEBUG_INIT, "freeing cs"); |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 497 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 498 | free_cs(cs); |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(gigaset_freecs); |
| 501 | |
| 502 | 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] | 503 | struct cardstate *cs, int cid) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 504 | { |
| 505 | int i; |
| 506 | |
| 507 | INIT_LIST_HEAD(&at_state->list); |
| 508 | at_state->waiting = 0; |
| 509 | at_state->getstring = 0; |
| 510 | at_state->pending_commands = 0; |
| 511 | at_state->timer_expires = 0; |
| 512 | at_state->timer_active = 0; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 513 | at_state->timer_index = 0; |
| 514 | at_state->seq_index = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 515 | at_state->ConState = 0; |
| 516 | for (i = 0; i < STR_NUM; ++i) |
| 517 | at_state->str_var[i] = NULL; |
| 518 | at_state->int_var[VAR_ZDLE] = 0; |
| 519 | at_state->int_var[VAR_ZCTP] = -1; |
| 520 | at_state->int_var[VAR_ZSAU] = ZSAU_NULL; |
| 521 | at_state->cs = cs; |
| 522 | at_state->bcs = bcs; |
| 523 | at_state->cid = cid; |
| 524 | if (!cid) |
| 525 | at_state->replystruct = cs->tabnocid; |
| 526 | else |
| 527 | at_state->replystruct = cs->tabcid; |
| 528 | } |
| 529 | |
| 530 | |
| 531 | 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] | 532 | struct cardstate *cs, int inputstate) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 533 | /* inbuf->read must be allocated before! */ |
| 534 | { |
| 535 | atomic_set(&inbuf->head, 0); |
| 536 | atomic_set(&inbuf->tail, 0); |
| 537 | inbuf->cs = cs; |
| 538 | inbuf->bcs = bcs; /*base driver: NULL*/ |
| 539 | inbuf->rcvbuf = NULL; //FIXME |
| 540 | inbuf->inputstate = inputstate; |
| 541 | } |
| 542 | |
Tilman Schmidt | 714e823 | 2006-04-10 22:55:09 -0700 | [diff] [blame] | 543 | /* append received bytes to inbuf */ |
| 544 | int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src, |
| 545 | unsigned numbytes) |
| 546 | { |
| 547 | unsigned n, head, tail, bytesleft; |
| 548 | |
| 549 | gig_dbg(DEBUG_INTR, "received %u bytes", numbytes); |
| 550 | |
| 551 | if (!numbytes) |
| 552 | return 0; |
| 553 | |
| 554 | bytesleft = numbytes; |
| 555 | tail = atomic_read(&inbuf->tail); |
| 556 | head = atomic_read(&inbuf->head); |
| 557 | gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail); |
| 558 | |
| 559 | while (bytesleft) { |
| 560 | if (head > tail) |
| 561 | n = head - 1 - tail; |
| 562 | else if (head == 0) |
| 563 | n = (RBUFSIZE-1) - tail; |
| 564 | else |
| 565 | n = RBUFSIZE - tail; |
| 566 | if (!n) { |
| 567 | dev_err(inbuf->cs->dev, |
| 568 | "buffer overflow (%u bytes lost)", bytesleft); |
| 569 | break; |
| 570 | } |
| 571 | if (n > bytesleft) |
| 572 | n = bytesleft; |
| 573 | memcpy(inbuf->data + tail, src, n); |
| 574 | bytesleft -= n; |
| 575 | tail = (tail + n) % RBUFSIZE; |
| 576 | src += n; |
| 577 | } |
| 578 | gig_dbg(DEBUG_INTR, "setting tail to %u", tail); |
| 579 | atomic_set(&inbuf->tail, tail); |
| 580 | return numbytes != bytesleft; |
| 581 | } |
| 582 | EXPORT_SYMBOL_GPL(gigaset_fill_inbuf); |
| 583 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 584 | /* Initialize the b-channel structure */ |
| 585 | static struct bc_state *gigaset_initbcs(struct bc_state *bcs, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 586 | struct cardstate *cs, int channel) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 587 | { |
| 588 | int i; |
| 589 | |
| 590 | bcs->tx_skb = NULL; //FIXME -> hw part |
| 591 | |
| 592 | skb_queue_head_init(&bcs->squeue); |
| 593 | |
| 594 | bcs->corrupted = 0; |
| 595 | bcs->trans_down = 0; |
| 596 | bcs->trans_up = 0; |
| 597 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 598 | gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 599 | gigaset_at_init(&bcs->at_state, bcs, cs, -1); |
| 600 | |
| 601 | bcs->rcvbytes = 0; |
| 602 | |
| 603 | #ifdef CONFIG_GIGASET_DEBUG |
| 604 | bcs->emptycount = 0; |
| 605 | #endif |
| 606 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 607 | gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 608 | bcs->fcs = PPP_INITFCS; |
| 609 | bcs->inputstate = 0; |
| 610 | if (cs->ignoreframes) { |
| 611 | bcs->inputstate |= INS_skip_frame; |
| 612 | bcs->skb = NULL; |
| 613 | } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL) |
| 614 | skb_reserve(bcs->skb, HW_HDR_LEN); |
| 615 | else { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 616 | dev_warn(cs->dev, "could not allocate skb\n"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 617 | bcs->inputstate |= INS_skip_frame; |
| 618 | } |
| 619 | |
| 620 | bcs->channel = channel; |
| 621 | bcs->cs = cs; |
| 622 | |
| 623 | bcs->chstate = 0; |
| 624 | bcs->use_count = 1; |
| 625 | bcs->busy = 0; |
| 626 | bcs->ignore = cs->ignoreframes; |
| 627 | |
| 628 | for (i = 0; i < AT_NUM; ++i) |
| 629 | bcs->commands[i] = NULL; |
| 630 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 631 | gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 632 | if (cs->ops->initbcshw(bcs)) |
| 633 | return bcs; |
| 634 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 635 | gig_dbg(DEBUG_INIT, " failed"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 636 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 637 | gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 638 | if (bcs->skb) |
| 639 | dev_kfree_skb(bcs->skb); |
| 640 | |
| 641 | return NULL; |
| 642 | } |
| 643 | |
| 644 | /* gigaset_initcs |
| 645 | * Allocate and initialize cardstate structure for Gigaset driver |
| 646 | * Calls hardware dependent gigaset_initcshw() function |
| 647 | * Calls B channel initialization function gigaset_initbcs() for each B channel |
| 648 | * parameters: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 649 | * drv hardware driver the device belongs to |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 650 | * channels number of B channels supported by device |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 651 | * onechannel !=0: B channel data and AT commands share one |
| 652 | * communication channel |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 653 | * ==0: B channels have separate communication channels |
| 654 | * ignoreframes number of frames to ignore after setting up B channel |
| 655 | * cidmode !=0: start in CallID mode |
| 656 | * modulename name of driver module (used for I4L registration) |
| 657 | * return value: |
| 658 | * pointer to cardstate structure |
| 659 | */ |
| 660 | struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels, |
| 661 | int onechannel, int ignoreframes, |
| 662 | int cidmode, const char *modulename) |
| 663 | { |
| 664 | struct cardstate *cs = NULL; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 665 | unsigned long flags; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 666 | int i; |
| 667 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 668 | gig_dbg(DEBUG_INIT, "allocating cs"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 669 | cs = alloc_cs(drv); |
| 670 | if (!cs) |
| 671 | goto error; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 672 | gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 673 | cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL); |
| 674 | if (!cs->bcs) |
| 675 | goto error; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 676 | gig_dbg(DEBUG_INIT, "allocating inbuf"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 677 | cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL); |
| 678 | if (!cs->inbuf) |
| 679 | goto error; |
| 680 | |
| 681 | cs->cs_init = 0; |
| 682 | cs->channels = channels; |
| 683 | cs->onechannel = onechannel; |
| 684 | cs->ignoreframes = ignoreframes; |
| 685 | INIT_LIST_HEAD(&cs->temp_at_states); |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 686 | cs->running = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 687 | init_timer(&cs->timer); /* clear next & prev */ |
| 688 | spin_lock_init(&cs->ev_lock); |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 689 | cs->ev_tail = 0; |
| 690 | cs->ev_head = 0; |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 691 | mutex_init(&cs->mutex); |
| 692 | mutex_lock(&cs->mutex); |
| 693 | |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 694 | tasklet_init(&cs->event_tasklet, &gigaset_handle_event, |
| 695 | (unsigned long) cs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 696 | atomic_set(&cs->commands_pending, 0); |
| 697 | cs->cur_at_seq = 0; |
| 698 | cs->gotfwver = -1; |
| 699 | cs->open_count = 0; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 700 | cs->dev = NULL; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 701 | cs->tty = NULL; |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 702 | cs->cidmode = cidmode != 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 703 | |
| 704 | //if(onechannel) { //FIXME |
| 705 | cs->tabnocid = gigaset_tab_nocid_m10x; |
| 706 | cs->tabcid = gigaset_tab_cid_m10x; |
| 707 | //} else { |
| 708 | // cs->tabnocid = gigaset_tab_nocid; |
| 709 | // cs->tabcid = gigaset_tab_cid; |
| 710 | //} |
| 711 | |
| 712 | init_waitqueue_head(&cs->waitqueue); |
| 713 | cs->waiting = 0; |
| 714 | |
| 715 | atomic_set(&cs->mode, M_UNKNOWN); |
| 716 | atomic_set(&cs->mstate, MS_UNINITIALIZED); |
| 717 | |
| 718 | for (i = 0; i < channels; ++i) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 719 | gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 720 | if (!gigaset_initbcs(cs->bcs + i, cs, i)) |
| 721 | goto error; |
| 722 | } |
| 723 | |
| 724 | ++cs->cs_init; |
| 725 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 726 | gig_dbg(DEBUG_INIT, "setting up at_state"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 727 | spin_lock_init(&cs->lock); |
| 728 | gigaset_at_init(&cs->at_state, NULL, cs, 0); |
| 729 | cs->dle = 0; |
| 730 | cs->cbytes = 0; |
| 731 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 732 | gig_dbg(DEBUG_INIT, "setting up inbuf"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 733 | if (onechannel) { //FIXME distinction necessary? |
| 734 | gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command); |
| 735 | } else |
| 736 | gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command); |
| 737 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 738 | cs->connected = 0; |
| 739 | cs->isdn_up = 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 740 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 741 | gig_dbg(DEBUG_INIT, "setting up cmdbuf"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 742 | cs->cmdbuf = cs->lastcmdbuf = NULL; |
| 743 | spin_lock_init(&cs->cmdlock); |
| 744 | cs->curlen = 0; |
| 745 | cs->cmdbytes = 0; |
| 746 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 747 | gig_dbg(DEBUG_INIT, "setting up iif"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 748 | if (!gigaset_register_to_LL(cs, modulename)) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 749 | err("register_isdn failed"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 750 | goto error; |
| 751 | } |
| 752 | |
| 753 | make_valid(cs, VALID_ID); |
| 754 | ++cs->cs_init; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 755 | gig_dbg(DEBUG_INIT, "setting up hw"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 756 | if (!cs->ops->initcshw(cs)) |
| 757 | goto error; |
| 758 | |
| 759 | ++cs->cs_init; |
| 760 | |
| 761 | gigaset_if_init(cs); |
| 762 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 763 | spin_lock_irqsave(&cs->lock, flags); |
| 764 | cs->running = 1; |
| 765 | spin_unlock_irqrestore(&cs->lock, flags); |
Tilman Schmidt | ec81b5e6 | 2006-04-10 22:55:03 -0700 | [diff] [blame] | 766 | setup_timer(&cs->timer, timer_tick, (unsigned long) cs); |
| 767 | cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 768 | /* FIXME: can jiffies increase too much until the timer is added? |
| 769 | * Same problem(?) with mod_timer() in timer_tick(). */ |
| 770 | add_timer(&cs->timer); |
| 771 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 772 | gig_dbg(DEBUG_INIT, "cs initialized"); |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 773 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 774 | return cs; |
| 775 | |
| 776 | error: if (cs) |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 777 | mutex_unlock(&cs->mutex); |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 778 | gig_dbg(DEBUG_INIT, "failed"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 779 | gigaset_freecs(cs); |
| 780 | return NULL; |
| 781 | } |
| 782 | EXPORT_SYMBOL_GPL(gigaset_initcs); |
| 783 | |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 784 | /* ReInitialize the b-channel structure */ |
| 785 | /* e.g. called on hangup, disconnect */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 786 | void gigaset_bcs_reinit(struct bc_state *bcs) |
| 787 | { |
| 788 | struct sk_buff *skb; |
| 789 | struct cardstate *cs = bcs->cs; |
| 790 | unsigned long flags; |
| 791 | |
| 792 | while ((skb = skb_dequeue(&bcs->squeue)) != NULL) |
| 793 | dev_kfree_skb(skb); |
| 794 | |
Tilman Schmidt | 917f508 | 2006-04-10 22:55:00 -0700 | [diff] [blame] | 795 | spin_lock_irqsave(&cs->lock, flags); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 796 | clear_at_state(&bcs->at_state); |
| 797 | bcs->at_state.ConState = 0; |
| 798 | bcs->at_state.timer_active = 0; |
| 799 | bcs->at_state.timer_expires = 0; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 800 | bcs->at_state.cid = -1; /* No CID defined */ |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 801 | spin_unlock_irqrestore(&cs->lock, flags); |
| 802 | |
| 803 | bcs->inputstate = 0; |
| 804 | |
| 805 | #ifdef CONFIG_GIGASET_DEBUG |
| 806 | bcs->emptycount = 0; |
| 807 | #endif |
| 808 | |
| 809 | bcs->fcs = PPP_INITFCS; |
| 810 | bcs->chstate = 0; |
| 811 | |
| 812 | bcs->ignore = cs->ignoreframes; |
| 813 | if (bcs->ignore) |
| 814 | bcs->inputstate |= INS_skip_frame; |
| 815 | |
| 816 | |
| 817 | cs->ops->reinitbcshw(bcs); |
| 818 | } |
| 819 | |
| 820 | static void cleanup_cs(struct cardstate *cs) |
| 821 | { |
| 822 | struct cmdbuf_t *cb, *tcb; |
| 823 | int i; |
| 824 | unsigned long flags; |
| 825 | |
| 826 | spin_lock_irqsave(&cs->lock, flags); |
| 827 | |
| 828 | atomic_set(&cs->mode, M_UNKNOWN); |
| 829 | atomic_set(&cs->mstate, MS_UNINITIALIZED); |
| 830 | |
| 831 | clear_at_state(&cs->at_state); |
| 832 | dealloc_at_states(cs); |
| 833 | free_strings(&cs->at_state); |
| 834 | gigaset_at_init(&cs->at_state, NULL, cs, 0); |
| 835 | |
| 836 | kfree(cs->inbuf->rcvbuf); |
| 837 | cs->inbuf->rcvbuf = NULL; |
| 838 | cs->inbuf->inputstate = INS_command; |
| 839 | atomic_set(&cs->inbuf->head, 0); |
| 840 | atomic_set(&cs->inbuf->tail, 0); |
| 841 | |
| 842 | cb = cs->cmdbuf; |
| 843 | while (cb) { |
| 844 | tcb = cb; |
| 845 | cb = cb->next; |
| 846 | kfree(tcb); |
| 847 | } |
| 848 | cs->cmdbuf = cs->lastcmdbuf = NULL; |
| 849 | cs->curlen = 0; |
| 850 | cs->cmdbytes = 0; |
| 851 | cs->gotfwver = -1; |
| 852 | cs->dle = 0; |
| 853 | cs->cur_at_seq = 0; |
| 854 | atomic_set(&cs->commands_pending, 0); |
| 855 | cs->cbytes = 0; |
| 856 | |
| 857 | spin_unlock_irqrestore(&cs->lock, flags); |
| 858 | |
| 859 | for (i = 0; i < cs->channels; ++i) { |
| 860 | gigaset_freebcs(cs->bcs + i); |
| 861 | if (!gigaset_initbcs(cs->bcs + i, cs, i)) |
| 862 | break; //FIXME error handling |
| 863 | } |
| 864 | |
| 865 | if (cs->waiting) { |
| 866 | cs->cmd_result = -ENODEV; |
| 867 | cs->waiting = 0; |
| 868 | wake_up_interruptible(&cs->waitqueue); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | |
| 873 | int gigaset_start(struct cardstate *cs) |
| 874 | { |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 875 | unsigned long flags; |
| 876 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 877 | if (mutex_lock_interruptible(&cs->mutex)) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 878 | return 0; |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 879 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 880 | spin_lock_irqsave(&cs->lock, flags); |
| 881 | cs->connected = 1; |
| 882 | spin_unlock_irqrestore(&cs->lock, flags); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 883 | |
| 884 | if (atomic_read(&cs->mstate) != MS_LOCKED) { |
| 885 | cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS); |
| 886 | cs->ops->baud_rate(cs, B115200); |
| 887 | cs->ops->set_line_ctrl(cs, CS8); |
| 888 | cs->control_state = TIOCM_DTR|TIOCM_RTS; |
| 889 | } else { |
| 890 | //FIXME use some saved values? |
| 891 | } |
| 892 | |
| 893 | cs->waiting = 1; |
| 894 | |
| 895 | if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) { |
| 896 | cs->waiting = 0; |
| 897 | //FIXME what should we do? |
| 898 | goto error; |
| 899 | } |
| 900 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 901 | gig_dbg(DEBUG_CMD, "scheduling START"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 902 | gigaset_schedule_event(cs); |
| 903 | |
| 904 | wait_event(cs->waitqueue, !cs->waiting); |
| 905 | |
Tilman Schmidt | b1d4746 | 2006-04-10 22:55:07 -0700 | [diff] [blame] | 906 | /* set up device sysfs */ |
| 907 | gigaset_init_dev_sysfs(cs); |
| 908 | |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 909 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 910 | return 1; |
| 911 | |
| 912 | error: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 913 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 914 | return 0; |
| 915 | } |
| 916 | EXPORT_SYMBOL_GPL(gigaset_start); |
| 917 | |
| 918 | void gigaset_shutdown(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 | |
| 922 | cs->waiting = 1; |
| 923 | |
| 924 | if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, 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 SHUTDOWN"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 930 | gigaset_schedule_event(cs); |
| 931 | |
| 932 | if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 933 | warn("%s: aborted", __func__); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 934 | //FIXME |
| 935 | } |
| 936 | |
| 937 | if (atomic_read(&cs->mstate) != MS_LOCKED) { |
| 938 | //FIXME? |
| 939 | //gigaset_baud_rate(cs, B115200); |
| 940 | //gigaset_set_line_ctrl(cs, CS8); |
| 941 | //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0); |
| 942 | //cs->control_state = 0; |
| 943 | } else { |
| 944 | //FIXME use some saved values? |
| 945 | } |
| 946 | |
| 947 | cleanup_cs(cs); |
| 948 | |
| 949 | exit: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 950 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 951 | } |
| 952 | EXPORT_SYMBOL_GPL(gigaset_shutdown); |
| 953 | |
| 954 | void gigaset_stop(struct cardstate *cs) |
| 955 | { |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 956 | mutex_lock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 957 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 958 | cs->waiting = 1; |
| 959 | |
| 960 | if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) { |
| 961 | //FIXME what should we do? |
| 962 | goto exit; |
| 963 | } |
| 964 | |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 965 | gig_dbg(DEBUG_CMD, "scheduling STOP"); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 966 | gigaset_schedule_event(cs); |
| 967 | |
| 968 | if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 969 | warn("%s: aborted", __func__); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 970 | //FIXME |
| 971 | } |
| 972 | |
Tilman Schmidt | 69049cc | 2006-04-10 22:55:16 -0700 | [diff] [blame^] | 973 | /* clear device sysfs */ |
| 974 | gigaset_free_dev_sysfs(cs); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 975 | |
| 976 | cleanup_cs(cs); |
| 977 | |
| 978 | exit: |
Tilman Schmidt | abfd1dc | 2006-04-10 22:55:12 -0700 | [diff] [blame] | 979 | mutex_unlock(&cs->mutex); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 980 | } |
| 981 | EXPORT_SYMBOL_GPL(gigaset_stop); |
| 982 | |
| 983 | static LIST_HEAD(drivers); |
| 984 | static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED; |
| 985 | |
| 986 | struct cardstate *gigaset_get_cs_by_id(int id) |
| 987 | { |
| 988 | unsigned long flags; |
| 989 | static struct cardstate *ret = NULL; |
| 990 | static struct cardstate *cs; |
| 991 | struct gigaset_driver *drv; |
| 992 | unsigned i; |
| 993 | |
| 994 | spin_lock_irqsave(&driver_lock, flags); |
| 995 | list_for_each_entry(drv, &drivers, list) { |
| 996 | spin_lock(&drv->lock); |
| 997 | for (i = 0; i < drv->minors; ++i) { |
| 998 | if (drv->flags[i] & VALID_ID) { |
| 999 | cs = drv->cs + i; |
| 1000 | if (cs->myid == id) |
| 1001 | ret = cs; |
| 1002 | } |
| 1003 | if (ret) |
| 1004 | break; |
| 1005 | } |
| 1006 | spin_unlock(&drv->lock); |
| 1007 | if (ret) |
| 1008 | break; |
| 1009 | } |
| 1010 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1011 | return ret; |
| 1012 | } |
| 1013 | |
| 1014 | void gigaset_debugdrivers(void) |
| 1015 | { |
| 1016 | unsigned long flags; |
| 1017 | static struct cardstate *cs; |
| 1018 | struct gigaset_driver *drv; |
| 1019 | unsigned i; |
| 1020 | |
| 1021 | spin_lock_irqsave(&driver_lock, flags); |
| 1022 | list_for_each_entry(drv, &drivers, list) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1023 | gig_dbg(DEBUG_DRIVER, "driver %p", drv); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1024 | spin_lock(&drv->lock); |
| 1025 | for (i = 0; i < drv->minors; ++i) { |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1026 | gig_dbg(DEBUG_DRIVER, " index %u", i); |
| 1027 | gig_dbg(DEBUG_DRIVER, " flags 0x%02x", |
| 1028 | drv->flags[i]); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1029 | cs = drv->cs + i; |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1030 | gig_dbg(DEBUG_DRIVER, " cardstate %p", cs); |
| 1031 | gig_dbg(DEBUG_DRIVER, " minor_index %u", |
| 1032 | cs->minor_index); |
| 1033 | gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver); |
| 1034 | gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid); |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1035 | } |
| 1036 | spin_unlock(&drv->lock); |
| 1037 | } |
| 1038 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1039 | } |
| 1040 | EXPORT_SYMBOL_GPL(gigaset_debugdrivers); |
| 1041 | |
| 1042 | struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty) |
| 1043 | { |
| 1044 | if (tty->index < 0 || tty->index >= tty->driver->num) |
| 1045 | return NULL; |
| 1046 | return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start); |
| 1047 | } |
| 1048 | |
| 1049 | struct cardstate *gigaset_get_cs_by_minor(unsigned minor) |
| 1050 | { |
| 1051 | unsigned long flags; |
| 1052 | static struct cardstate *ret = NULL; |
| 1053 | struct gigaset_driver *drv; |
| 1054 | unsigned index; |
| 1055 | |
| 1056 | spin_lock_irqsave(&driver_lock, flags); |
| 1057 | list_for_each_entry(drv, &drivers, list) { |
| 1058 | if (minor < drv->minor || minor >= drv->minor + drv->minors) |
| 1059 | continue; |
| 1060 | index = minor - drv->minor; |
| 1061 | spin_lock(&drv->lock); |
| 1062 | if (drv->flags[index] & VALID_MINOR) |
| 1063 | ret = drv->cs + index; |
| 1064 | spin_unlock(&drv->lock); |
| 1065 | if (ret) |
| 1066 | break; |
| 1067 | } |
| 1068 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
| 1072 | void gigaset_freedriver(struct gigaset_driver *drv) |
| 1073 | { |
| 1074 | unsigned long flags; |
| 1075 | |
| 1076 | spin_lock_irqsave(&driver_lock, flags); |
| 1077 | list_del(&drv->list); |
| 1078 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1079 | |
| 1080 | gigaset_if_freedriver(drv); |
| 1081 | module_put(drv->owner); |
| 1082 | |
| 1083 | kfree(drv->cs); |
| 1084 | kfree(drv->flags); |
| 1085 | kfree(drv); |
| 1086 | } |
| 1087 | EXPORT_SYMBOL_GPL(gigaset_freedriver); |
| 1088 | |
| 1089 | /* gigaset_initdriver |
| 1090 | * Allocate and initialize gigaset_driver structure. Initialize interface. |
| 1091 | * parameters: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1092 | * minor First minor number |
| 1093 | * minors Number of minors this driver can handle |
| 1094 | * procname Name of the driver |
| 1095 | * devname Name of the device files (prefix without minor number) |
| 1096 | * devfsname Devfs name of the device files without %d |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1097 | * return value: |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1098 | * Pointer to the gigaset_driver structure on success, NULL on failure. |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1099 | */ |
| 1100 | struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors, |
Tilman Schmidt | 784d585 | 2006-04-10 22:55:04 -0700 | [diff] [blame] | 1101 | const char *procname, |
| 1102 | const char *devname, |
| 1103 | const char *devfsname, |
| 1104 | const struct gigaset_ops *ops, |
| 1105 | struct module *owner) |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1106 | { |
| 1107 | struct gigaset_driver *drv; |
| 1108 | unsigned long flags; |
| 1109 | unsigned i; |
| 1110 | |
| 1111 | drv = kmalloc(sizeof *drv, GFP_KERNEL); |
| 1112 | if (!drv) |
| 1113 | return NULL; |
| 1114 | if (!try_module_get(owner)) |
| 1115 | return NULL; |
| 1116 | |
| 1117 | drv->cs = NULL; |
| 1118 | drv->have_tty = 0; |
| 1119 | drv->minor = minor; |
| 1120 | drv->minors = minors; |
| 1121 | spin_lock_init(&drv->lock); |
| 1122 | drv->blocked = 0; |
| 1123 | drv->ops = ops; |
| 1124 | drv->owner = owner; |
| 1125 | INIT_LIST_HEAD(&drv->list); |
| 1126 | |
| 1127 | drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL); |
| 1128 | if (!drv->cs) |
| 1129 | goto out1; |
| 1130 | drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL); |
| 1131 | if (!drv->flags) |
| 1132 | goto out2; |
| 1133 | |
| 1134 | for (i = 0; i < minors; ++i) { |
| 1135 | drv->flags[i] = 0; |
| 1136 | drv->cs[i].driver = drv; |
| 1137 | drv->cs[i].ops = drv->ops; |
| 1138 | drv->cs[i].minor_index = i; |
| 1139 | } |
| 1140 | |
| 1141 | gigaset_if_initdriver(drv, procname, devname, devfsname); |
| 1142 | |
| 1143 | spin_lock_irqsave(&driver_lock, flags); |
| 1144 | list_add(&drv->list, &drivers); |
| 1145 | spin_unlock_irqrestore(&driver_lock, flags); |
| 1146 | |
| 1147 | return drv; |
| 1148 | |
| 1149 | out2: |
| 1150 | kfree(drv->cs); |
| 1151 | out1: |
| 1152 | kfree(drv); |
| 1153 | module_put(owner); |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | EXPORT_SYMBOL_GPL(gigaset_initdriver); |
| 1157 | |
Hansjoerg Lipp | 6fd5ea6 | 2006-03-26 01:38:29 -0800 | [diff] [blame] | 1158 | /* For drivers without fixed assignment device<->cardstate (usb) */ |
| 1159 | struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv) |
| 1160 | { |
| 1161 | unsigned long flags; |
| 1162 | struct cardstate *cs = NULL; |
| 1163 | unsigned i; |
| 1164 | |
| 1165 | spin_lock_irqsave(&drv->lock, flags); |
| 1166 | if (drv->blocked) |
| 1167 | goto exit; |
| 1168 | for (i = 0; i < drv->minors; ++i) { |
| 1169 | if ((drv->flags[i] & VALID_MINOR) && |
| 1170 | !(drv->flags[i] & ASSIGNED)) { |
| 1171 | drv->flags[i] |= ASSIGNED; |
| 1172 | cs = drv->cs + i; |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | exit: |
| 1177 | spin_unlock_irqrestore(&drv->lock, flags); |
| 1178 | return cs; |
| 1179 | } |
| 1180 | EXPORT_SYMBOL_GPL(gigaset_getunassignedcs); |
| 1181 | |
| 1182 | void gigaset_unassign(struct cardstate *cs) |
| 1183 | { |
| 1184 | unsigned long flags; |
| 1185 | unsigned *minor_flags; |
| 1186 | struct gigaset_driver *drv; |
| 1187 | |
| 1188 | if (!cs) |
| 1189 | return; |
| 1190 | drv = cs->driver; |
| 1191 | spin_lock_irqsave(&drv->lock, flags); |
| 1192 | minor_flags = drv->flags + cs->minor_index; |
| 1193 | if (*minor_flags & VALID_MINOR) |
| 1194 | *minor_flags &= ~ASSIGNED; |
| 1195 | spin_unlock_irqrestore(&drv->lock, flags); |
| 1196 | } |
| 1197 | EXPORT_SYMBOL_GPL(gigaset_unassign); |
| 1198 | |
| 1199 | void gigaset_blockdriver(struct gigaset_driver *drv) |
| 1200 | { |
| 1201 | unsigned long flags; |
| 1202 | spin_lock_irqsave(&drv->lock, flags); |
| 1203 | drv->blocked = 1; |
| 1204 | spin_unlock_irqrestore(&drv->lock, flags); |
| 1205 | } |
| 1206 | EXPORT_SYMBOL_GPL(gigaset_blockdriver); |
| 1207 | |
| 1208 | static int __init gigaset_init_module(void) |
| 1209 | { |
| 1210 | /* in accordance with the principle of least astonishment, |
| 1211 | * setting the 'debug' parameter to 1 activates a sensible |
| 1212 | * set of default debug levels |
| 1213 | */ |
| 1214 | if (gigaset_debuglevel == 1) |
| 1215 | gigaset_debuglevel = DEBUG_DEFAULT; |
| 1216 | |
| 1217 | info(DRIVER_AUTHOR); |
| 1218 | info(DRIVER_DESC); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | static void __exit gigaset_exit_module(void) |
| 1223 | { |
| 1224 | } |
| 1225 | |
| 1226 | module_init(gigaset_init_module); |
| 1227 | module_exit(gigaset_exit_module); |
| 1228 | |
| 1229 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1230 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1231 | |
| 1232 | MODULE_LICENSE("GPL"); |