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