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