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