gigaset: add Kernel CAPI interface (v3)
[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 struct bc_state *gigaset_get_free_channel(struct cardstate *cs)
211 {
212         unsigned long flags;
213         int i;
214
215         spin_lock_irqsave(&cs->lock, flags);
216         if (!try_module_get(cs->driver->owner)) {
217                 gig_dbg(DEBUG_ANY,
218                         "could not get module for allocating channel");
219                 spin_unlock_irqrestore(&cs->lock, flags);
220                 return NULL;
221         }
222         for (i = 0; i < cs->channels; ++i)
223                 if (!cs->bcs[i].use_count) {
224                         ++cs->bcs[i].use_count;
225                         cs->bcs[i].busy = 1;
226                         spin_unlock_irqrestore(&cs->lock, flags);
227                         gig_dbg(DEBUG_ANY, "allocated channel %d", i);
228                         return cs->bcs + i;
229                 }
230         module_put(cs->driver->owner);
231         spin_unlock_irqrestore(&cs->lock, flags);
232         gig_dbg(DEBUG_ANY, "no free channel");
233         return NULL;
234 }
235
236 void gigaset_free_channel(struct bc_state *bcs)
237 {
238         unsigned long flags;
239
240         spin_lock_irqsave(&bcs->cs->lock, flags);
241         if (!bcs->busy) {
242                 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
243                 spin_unlock_irqrestore(&bcs->cs->lock, flags);
244                 return;
245         }
246         --bcs->use_count;
247         bcs->busy = 0;
248         module_put(bcs->cs->driver->owner);
249         gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
250         spin_unlock_irqrestore(&bcs->cs->lock, flags);
251 }
252
253 int gigaset_get_channels(struct cardstate *cs)
254 {
255         unsigned long flags;
256         int i;
257
258         spin_lock_irqsave(&cs->lock, flags);
259         for (i = 0; i < cs->channels; ++i)
260                 if (cs->bcs[i].use_count) {
261                         spin_unlock_irqrestore(&cs->lock, flags);
262                         gig_dbg(DEBUG_ANY, "could not allocate all channels");
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
269         gig_dbg(DEBUG_ANY, "allocated all channels");
270
271         return 1;
272 }
273
274 void gigaset_free_channels(struct cardstate *cs)
275 {
276         unsigned long flags;
277         int i;
278
279         gig_dbg(DEBUG_ANY, "unblocking all channels");
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
291         gig_dbg(DEBUG_ANY, "blocking all channels");
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;
302         unsigned long flags;
303
304         spin_lock_irqsave(&cs->ev_lock, flags);
305
306         head = cs->ev_head;
307         tail = cs->ev_tail;
308
309         while (tail != head) {
310                 ev = cs->events + head;
311                 kfree(ev->ptr);
312                 head = (head + 1) % MAX_EVENTS;
313         }
314
315         cs->ev_head = tail;
316
317         spin_unlock_irqrestore(&cs->ev_lock, flags);
318 }
319
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  */
334 struct event_t *gigaset_add_event(struct cardstate *cs,
335                                   struct at_state_t *at_state, int type,
336                                   void *ptr, int parameter, void *arg)
337 {
338         unsigned long flags;
339         unsigned next, tail;
340         struct event_t *event = NULL;
341
342         spin_lock_irqsave(&cs->ev_lock, flags);
343
344         tail = cs->ev_tail;
345         next = (tail + 1) % MAX_EVENTS;
346         if (unlikely(next == cs->ev_head))
347                 dev_err(cs->dev, "event queue full\n");
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;
356                 cs->ev_tail = next;
357         }
358
359         spin_unlock_irqrestore(&cs->ev_lock, flags);
360
361         return event;
362 }
363 EXPORT_SYMBOL_GPL(gigaset_add_event);
364
365 static 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
375 static void clear_at_state(struct at_state_t *at_state)
376 {
377         free_strings(at_state);
378 }
379
380 static 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
391 static void gigaset_freebcs(struct bc_state *bcs)
392 {
393         int i;
394
395         gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
396         if (!bcs->cs->ops->freebcshw(bcs)) {
397                 gig_dbg(DEBUG_INIT, "failed");
398         }
399
400         gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
401         clear_at_state(&bcs->at_state);
402         gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
403
404         if (bcs->skb)
405                 dev_kfree_skb(bcs->skb);
406         for (i = 0; i < AT_NUM; ++i) {
407                 kfree(bcs->commands[i]);
408                 bcs->commands[i] = NULL;
409         }
410 }
411
412 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
413 {
414         unsigned long flags;
415         unsigned i;
416         struct cardstate *cs;
417         struct cardstate *ret = NULL;
418
419         spin_lock_irqsave(&drv->lock, flags);
420         if (drv->blocked)
421                 goto exit;
422         for (i = 0; i < drv->minors; ++i) {
423                 cs = drv->cs + i;
424                 if (!(cs->flags & VALID_MINOR)) {
425                         cs->flags = VALID_MINOR;
426                         ret = cs;
427                         break;
428                 }
429         }
430 exit:
431         spin_unlock_irqrestore(&drv->lock, flags);
432         return ret;
433 }
434
435 static void free_cs(struct cardstate *cs)
436 {
437         cs->flags = 0;
438 }
439
440 static void make_valid(struct cardstate *cs, unsigned mask)
441 {
442         unsigned long flags;
443         struct gigaset_driver *drv = cs->driver;
444         spin_lock_irqsave(&drv->lock, flags);
445         cs->flags |= mask;
446         spin_unlock_irqrestore(&drv->lock, flags);
447 }
448
449 static void make_invalid(struct cardstate *cs, unsigned mask)
450 {
451         unsigned long flags;
452         struct gigaset_driver *drv = cs->driver;
453         spin_lock_irqsave(&drv->lock, flags);
454         cs->flags &= ~mask;
455         spin_unlock_irqrestore(&drv->lock, flags);
456 }
457
458 /**
459  * gigaset_freecs() - free all associated ressources of a device
460  * @cs:         device descriptor structure.
461  *
462  * Stops all tasklets and timers, unregisters the device from all
463  * subsystems it was registered to, deallocates the device structure
464  * @cs and all structures referenced from it.
465  * Operations on the device should be stopped before calling this.
466  */
467 void gigaset_freecs(struct cardstate *cs)
468 {
469         int i;
470         unsigned long flags;
471
472         if (!cs)
473                 return;
474
475         mutex_lock(&cs->mutex);
476
477         if (!cs->bcs)
478                 goto f_cs;
479         if (!cs->inbuf)
480                 goto f_bcs;
481
482         spin_lock_irqsave(&cs->lock, flags);
483         cs->running = 0;
484         spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
485                                                      not rescheduled below */
486
487         tasklet_kill(&cs->event_tasklet);
488         del_timer_sync(&cs->timer);
489
490         switch (cs->cs_init) {
491         default:
492                 /* clear B channel structures */
493                 for (i = 0; i < cs->channels; ++i) {
494                         gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
495                         gigaset_freebcs(cs->bcs + i);
496                 }
497
498                 /* clear device sysfs */
499                 gigaset_free_dev_sysfs(cs);
500
501                 gigaset_if_free(cs);
502
503                 gig_dbg(DEBUG_INIT, "clearing hw");
504                 cs->ops->freecshw(cs);
505
506                 //FIXME cmdbuf
507
508                 /* fall through */
509         case 2: /* error in initcshw */
510                 /* Deregister from LL */
511                 make_invalid(cs, VALID_ID);
512                 gigaset_isdn_unregister(cs);
513
514                 /* fall through */
515         case 1: /* error when registering to LL */
516                 gig_dbg(DEBUG_INIT, "clearing at_state");
517                 clear_at_state(&cs->at_state);
518                 dealloc_at_states(cs);
519
520                 /* fall through */
521         case 0: /* error in basic setup */
522                 clear_events(cs);
523                 gig_dbg(DEBUG_INIT, "freeing inbuf");
524                 kfree(cs->inbuf);
525         }
526 f_bcs:  gig_dbg(DEBUG_INIT, "freeing bcs[]");
527         kfree(cs->bcs);
528 f_cs:   gig_dbg(DEBUG_INIT, "freeing cs");
529         mutex_unlock(&cs->mutex);
530         free_cs(cs);
531 }
532 EXPORT_SYMBOL_GPL(gigaset_freecs);
533
534 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
535                      struct cardstate *cs, int cid)
536 {
537         int i;
538
539         INIT_LIST_HEAD(&at_state->list);
540         at_state->waiting = 0;
541         at_state->getstring = 0;
542         at_state->pending_commands = 0;
543         at_state->timer_expires = 0;
544         at_state->timer_active = 0;
545         at_state->timer_index = 0;
546         at_state->seq_index = 0;
547         at_state->ConState = 0;
548         for (i = 0; i < STR_NUM; ++i)
549                 at_state->str_var[i] = NULL;
550         at_state->int_var[VAR_ZDLE] = 0;
551         at_state->int_var[VAR_ZCTP] = -1;
552         at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
553         at_state->cs = cs;
554         at_state->bcs = bcs;
555         at_state->cid = cid;
556         if (!cid)
557                 at_state->replystruct = cs->tabnocid;
558         else
559                 at_state->replystruct = cs->tabcid;
560 }
561
562
563 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
564                                struct cardstate *cs, int inputstate)
565 /* inbuf->read must be allocated before! */
566 {
567         inbuf->head = 0;
568         inbuf->tail = 0;
569         inbuf->cs = cs;
570         inbuf->bcs = bcs; /*base driver: NULL*/
571         inbuf->rcvbuf = NULL;
572         inbuf->inputstate = inputstate;
573 }
574
575 /**
576  * gigaset_fill_inbuf() - append received data to input buffer
577  * @inbuf:      buffer structure.
578  * @src:        received data.
579  * @numbytes:   number of bytes received.
580  */
581 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
582                        unsigned numbytes)
583 {
584         unsigned n, head, tail, bytesleft;
585
586         gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
587
588         if (!numbytes)
589                 return 0;
590
591         bytesleft = numbytes;
592         tail = inbuf->tail;
593         head = inbuf->head;
594         gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
595
596         while (bytesleft) {
597                 if (head > tail)
598                         n = head - 1 - tail;
599                 else if (head == 0)
600                         n = (RBUFSIZE-1) - tail;
601                 else
602                         n = RBUFSIZE - tail;
603                 if (!n) {
604                         dev_err(inbuf->cs->dev,
605                                 "buffer overflow (%u bytes lost)\n",
606                                 bytesleft);
607                         break;
608                 }
609                 if (n > bytesleft)
610                         n = bytesleft;
611                 memcpy(inbuf->data + tail, src, n);
612                 bytesleft -= n;
613                 tail = (tail + n) % RBUFSIZE;
614                 src += n;
615         }
616         gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
617         inbuf->tail = tail;
618         return numbytes != bytesleft;
619 }
620 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
621
622 /* Initialize the b-channel structure */
623 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
624                                         struct cardstate *cs, int channel)
625 {
626         int i;
627
628         bcs->tx_skb = NULL; //FIXME -> hw part
629
630         skb_queue_head_init(&bcs->squeue);
631
632         bcs->corrupted = 0;
633         bcs->trans_down = 0;
634         bcs->trans_up = 0;
635
636         gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
637         gigaset_at_init(&bcs->at_state, bcs, cs, -1);
638
639 #ifdef CONFIG_GIGASET_DEBUG
640         bcs->emptycount = 0;
641 #endif
642
643         gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
644         bcs->fcs = PPP_INITFCS;
645         bcs->inputstate = 0;
646         if (cs->ignoreframes) {
647                 bcs->inputstate |= INS_skip_frame;
648                 bcs->skb = NULL;
649         } else {
650                 bcs->skb = dev_alloc_skb(SBUFSIZE + cs->hw_hdr_len);
651                 if (bcs->skb != NULL)
652                         skb_reserve(bcs->skb, cs->hw_hdr_len);
653                 else {
654                         pr_err("out of memory\n");
655                         bcs->inputstate |= INS_skip_frame;
656                 }
657         }
658
659         bcs->channel = channel;
660         bcs->cs = cs;
661
662         bcs->chstate = 0;
663         bcs->use_count = 1;
664         bcs->busy = 0;
665         bcs->ignore = cs->ignoreframes;
666
667         for (i = 0; i < AT_NUM; ++i)
668                 bcs->commands[i] = NULL;
669
670         gig_dbg(DEBUG_INIT, "  setting up bcs[%d]->hw", channel);
671         if (cs->ops->initbcshw(bcs))
672                 return bcs;
673
674         gig_dbg(DEBUG_INIT, "  failed");
675
676         gig_dbg(DEBUG_INIT, "  freeing bcs[%d]->skb", channel);
677         if (bcs->skb)
678                 dev_kfree_skb(bcs->skb);
679
680         return NULL;
681 }
682
683 /**
684  * gigaset_initcs() - initialize device structure
685  * @drv:        hardware driver the device belongs to
686  * @channels:   number of B channels supported by device
687  * @onechannel: !=0 if B channel data and AT commands share one
688  *                  communication channel (M10x),
689  *              ==0 if B channels have separate communication channels (base)
690  * @ignoreframes:       number of frames to ignore after setting up B channel
691  * @cidmode:    !=0: start in CallID mode
692  * @modulename: name of driver module for LL registration
693  *
694  * Allocate and initialize cardstate structure for Gigaset driver
695  * Calls hardware dependent gigaset_initcshw() function
696  * Calls B channel initialization function gigaset_initbcs() for each B channel
697  *
698  * Return value:
699  *      pointer to cardstate structure
700  */
701 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
702                                  int onechannel, int ignoreframes,
703                                  int cidmode, const char *modulename)
704 {
705         struct cardstate *cs = NULL;
706         unsigned long flags;
707         int i;
708
709         gig_dbg(DEBUG_INIT, "allocating cs");
710         if (!(cs = alloc_cs(drv))) {
711                 pr_err("maximum number of devices exceeded\n");
712                 return NULL;
713         }
714
715         gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
716         cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
717         if (!cs->bcs) {
718                 pr_err("out of memory\n");
719                 goto error;
720         }
721         gig_dbg(DEBUG_INIT, "allocating inbuf");
722         cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
723         if (!cs->inbuf) {
724                 pr_err("out of memory\n");
725                 goto error;
726         }
727
728         cs->cs_init = 0;
729         cs->channels = channels;
730         cs->onechannel = onechannel;
731         cs->ignoreframes = ignoreframes;
732         INIT_LIST_HEAD(&cs->temp_at_states);
733         cs->running = 0;
734         init_timer(&cs->timer); /* clear next & prev */
735         spin_lock_init(&cs->ev_lock);
736         cs->ev_tail = 0;
737         cs->ev_head = 0;
738
739         tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
740                      (unsigned long) cs);
741         cs->commands_pending = 0;
742         cs->cur_at_seq = 0;
743         cs->gotfwver = -1;
744         cs->open_count = 0;
745         cs->dev = NULL;
746         cs->tty = NULL;
747         cs->tty_dev = NULL;
748         cs->cidmode = cidmode != 0;
749         cs->tabnocid = gigaset_tab_nocid;
750         cs->tabcid = gigaset_tab_cid;
751
752         init_waitqueue_head(&cs->waitqueue);
753         cs->waiting = 0;
754
755         cs->mode = M_UNKNOWN;
756         cs->mstate = MS_UNINITIALIZED;
757
758         ++cs->cs_init;
759
760         gig_dbg(DEBUG_INIT, "setting up at_state");
761         spin_lock_init(&cs->lock);
762         gigaset_at_init(&cs->at_state, NULL, cs, 0);
763         cs->dle = 0;
764         cs->cbytes = 0;
765
766         gig_dbg(DEBUG_INIT, "setting up inbuf");
767         if (onechannel) {                       //FIXME distinction necessary?
768                 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
769         } else
770                 gigaset_inbuf_init(cs->inbuf, NULL,    cs, INS_command);
771
772         cs->connected = 0;
773         cs->isdn_up = 0;
774
775         gig_dbg(DEBUG_INIT, "setting up cmdbuf");
776         cs->cmdbuf = cs->lastcmdbuf = NULL;
777         spin_lock_init(&cs->cmdlock);
778         cs->curlen = 0;
779         cs->cmdbytes = 0;
780
781         gig_dbg(DEBUG_INIT, "setting up iif");
782         if (!gigaset_isdn_register(cs, modulename)) {
783                 pr_err("error registering ISDN device\n");
784                 goto error;
785         }
786
787         make_valid(cs, VALID_ID);
788         ++cs->cs_init;
789         gig_dbg(DEBUG_INIT, "setting up hw");
790         if (!cs->ops->initcshw(cs))
791                 goto error;
792
793         ++cs->cs_init;
794
795         /* set up character device */
796         gigaset_if_init(cs);
797
798         /* set up device sysfs */
799         gigaset_init_dev_sysfs(cs);
800
801         /* set up channel data structures */
802         for (i = 0; i < channels; ++i) {
803                 gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
804                 if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
805                         pr_err("could not allocate channel %d data\n", i);
806                         goto error;
807                 }
808         }
809
810         spin_lock_irqsave(&cs->lock, flags);
811         cs->running = 1;
812         spin_unlock_irqrestore(&cs->lock, flags);
813         setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
814         cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
815         /* FIXME: can jiffies increase too much until the timer is added?
816          * Same problem(?) with mod_timer() in timer_tick(). */
817         add_timer(&cs->timer);
818
819         gig_dbg(DEBUG_INIT, "cs initialized");
820         return cs;
821
822 error:
823         gig_dbg(DEBUG_INIT, "failed");
824         gigaset_freecs(cs);
825         return NULL;
826 }
827 EXPORT_SYMBOL_GPL(gigaset_initcs);
828
829 /* ReInitialize the b-channel structure on hangup */
830 void gigaset_bcs_reinit(struct bc_state *bcs)
831 {
832         struct sk_buff *skb;
833         struct cardstate *cs = bcs->cs;
834         unsigned long flags;
835
836         while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
837                 dev_kfree_skb(skb);
838
839         spin_lock_irqsave(&cs->lock, flags);
840         clear_at_state(&bcs->at_state);
841         bcs->at_state.ConState = 0;
842         bcs->at_state.timer_active = 0;
843         bcs->at_state.timer_expires = 0;
844         bcs->at_state.cid = -1;                 /* No CID defined */
845         spin_unlock_irqrestore(&cs->lock, flags);
846
847         bcs->inputstate = 0;
848
849 #ifdef CONFIG_GIGASET_DEBUG
850         bcs->emptycount = 0;
851 #endif
852
853         bcs->fcs = PPP_INITFCS;
854         bcs->chstate = 0;
855
856         bcs->ignore = cs->ignoreframes;
857         if (bcs->ignore)
858                 bcs->inputstate |= INS_skip_frame;
859
860
861         cs->ops->reinitbcshw(bcs);
862 }
863
864 static void cleanup_cs(struct cardstate *cs)
865 {
866         struct cmdbuf_t *cb, *tcb;
867         int i;
868         unsigned long flags;
869
870         spin_lock_irqsave(&cs->lock, flags);
871
872         cs->mode = M_UNKNOWN;
873         cs->mstate = MS_UNINITIALIZED;
874
875         clear_at_state(&cs->at_state);
876         dealloc_at_states(cs);
877         free_strings(&cs->at_state);
878         gigaset_at_init(&cs->at_state, NULL, cs, 0);
879
880         kfree(cs->inbuf->rcvbuf);
881         cs->inbuf->rcvbuf = NULL;
882         cs->inbuf->inputstate = INS_command;
883         cs->inbuf->head = 0;
884         cs->inbuf->tail = 0;
885
886         cb = cs->cmdbuf;
887         while (cb) {
888                 tcb = cb;
889                 cb = cb->next;
890                 kfree(tcb);
891         }
892         cs->cmdbuf = cs->lastcmdbuf = NULL;
893         cs->curlen = 0;
894         cs->cmdbytes = 0;
895         cs->gotfwver = -1;
896         cs->dle = 0;
897         cs->cur_at_seq = 0;
898         cs->commands_pending = 0;
899         cs->cbytes = 0;
900
901         spin_unlock_irqrestore(&cs->lock, flags);
902
903         for (i = 0; i < cs->channels; ++i) {
904                 gigaset_freebcs(cs->bcs + i);
905                 if (!gigaset_initbcs(cs->bcs + i, cs, i))
906                         pr_err("could not allocate channel %d data\n", i);
907         }
908
909         if (cs->waiting) {
910                 cs->cmd_result = -ENODEV;
911                 cs->waiting = 0;
912                 wake_up_interruptible(&cs->waitqueue);
913         }
914 }
915
916
917 /**
918  * gigaset_start() - start device operations
919  * @cs:         device descriptor structure.
920  *
921  * Prepares the device for use by setting up communication parameters,
922  * scheduling an EV_START event to initiate device initialization, and
923  * waiting for completion of the initialization.
924  *
925  * Return value:
926  *      1 - success, 0 - error
927  */
928 int gigaset_start(struct cardstate *cs)
929 {
930         unsigned long flags;
931
932         if (mutex_lock_interruptible(&cs->mutex))
933                 return 0;
934
935         spin_lock_irqsave(&cs->lock, flags);
936         cs->connected = 1;
937         spin_unlock_irqrestore(&cs->lock, flags);
938
939         if (cs->mstate != MS_LOCKED) {
940                 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
941                 cs->ops->baud_rate(cs, B115200);
942                 cs->ops->set_line_ctrl(cs, CS8);
943                 cs->control_state = TIOCM_DTR|TIOCM_RTS;
944         } else {
945                 //FIXME use some saved values?
946         }
947
948         cs->waiting = 1;
949
950         if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
951                 cs->waiting = 0;
952                 //FIXME what should we do?
953                 goto error;
954         }
955
956         gig_dbg(DEBUG_CMD, "scheduling START");
957         gigaset_schedule_event(cs);
958
959         wait_event(cs->waitqueue, !cs->waiting);
960
961         mutex_unlock(&cs->mutex);
962         return 1;
963
964 error:
965         mutex_unlock(&cs->mutex);
966         return 0;
967 }
968 EXPORT_SYMBOL_GPL(gigaset_start);
969
970 /**
971  * gigaset_shutdown() - shut down device operations
972  * @cs:         device descriptor structure.
973  *
974  * Deactivates the device by scheduling an EV_SHUTDOWN event and
975  * waiting for completion of the shutdown.
976  *
977  * Return value:
978  *      0 - success, -1 - error (no device associated)
979  */
980 int gigaset_shutdown(struct cardstate *cs)
981 {
982         mutex_lock(&cs->mutex);
983
984         if (!(cs->flags & VALID_MINOR)) {
985                 mutex_unlock(&cs->mutex);
986                 return -1;
987         }
988
989         cs->waiting = 1;
990
991         if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
992                 //FIXME what should we do?
993                 goto exit;
994         }
995
996         gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
997         gigaset_schedule_event(cs);
998
999         wait_event(cs->waitqueue, !cs->waiting);
1000
1001         cleanup_cs(cs);
1002
1003 exit:
1004         mutex_unlock(&cs->mutex);
1005         return 0;
1006 }
1007 EXPORT_SYMBOL_GPL(gigaset_shutdown);
1008
1009 /**
1010  * gigaset_stop() - stop device operations
1011  * @cs:         device descriptor structure.
1012  *
1013  * Stops operations on the device by scheduling an EV_STOP event and
1014  * waiting for completion of the shutdown.
1015  */
1016 void gigaset_stop(struct cardstate *cs)
1017 {
1018         mutex_lock(&cs->mutex);
1019
1020         cs->waiting = 1;
1021
1022         if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
1023                 //FIXME what should we do?
1024                 goto exit;
1025         }
1026
1027         gig_dbg(DEBUG_CMD, "scheduling STOP");
1028         gigaset_schedule_event(cs);
1029
1030         wait_event(cs->waitqueue, !cs->waiting);
1031
1032         cleanup_cs(cs);
1033
1034 exit:
1035         mutex_unlock(&cs->mutex);
1036 }
1037 EXPORT_SYMBOL_GPL(gigaset_stop);
1038
1039 static LIST_HEAD(drivers);
1040 static DEFINE_SPINLOCK(driver_lock);
1041
1042 struct cardstate *gigaset_get_cs_by_id(int id)
1043 {
1044         unsigned long flags;
1045         struct cardstate *ret = NULL;
1046         struct cardstate *cs;
1047         struct gigaset_driver *drv;
1048         unsigned i;
1049
1050         spin_lock_irqsave(&driver_lock, flags);
1051         list_for_each_entry(drv, &drivers, list) {
1052                 spin_lock(&drv->lock);
1053                 for (i = 0; i < drv->minors; ++i) {
1054                         cs = drv->cs + i;
1055                         if ((cs->flags & VALID_ID) && cs->myid == id) {
1056                                 ret = cs;
1057                                 break;
1058                         }
1059                 }
1060                 spin_unlock(&drv->lock);
1061                 if (ret)
1062                         break;
1063         }
1064         spin_unlock_irqrestore(&driver_lock, flags);
1065         return ret;
1066 }
1067
1068 void gigaset_debugdrivers(void)
1069 {
1070         unsigned long flags;
1071         static struct cardstate *cs;
1072         struct gigaset_driver *drv;
1073         unsigned i;
1074
1075         spin_lock_irqsave(&driver_lock, flags);
1076         list_for_each_entry(drv, &drivers, list) {
1077                 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
1078                 spin_lock(&drv->lock);
1079                 for (i = 0; i < drv->minors; ++i) {
1080                         gig_dbg(DEBUG_DRIVER, "  index %u", i);
1081                         cs = drv->cs + i;
1082                         gig_dbg(DEBUG_DRIVER, "    cardstate %p", cs);
1083                         gig_dbg(DEBUG_DRIVER, "    flags 0x%02x", cs->flags);
1084                         gig_dbg(DEBUG_DRIVER, "    minor_index %u",
1085                                 cs->minor_index);
1086                         gig_dbg(DEBUG_DRIVER, "    driver %p", cs->driver);
1087                         gig_dbg(DEBUG_DRIVER, "    i4l id %d", cs->myid);
1088                 }
1089                 spin_unlock(&drv->lock);
1090         }
1091         spin_unlock_irqrestore(&driver_lock, flags);
1092 }
1093
1094 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1095 {
1096         unsigned long flags;
1097         struct cardstate *ret = NULL;
1098         struct gigaset_driver *drv;
1099         unsigned index;
1100
1101         spin_lock_irqsave(&driver_lock, flags);
1102         list_for_each_entry(drv, &drivers, list) {
1103                 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1104                         continue;
1105                 index = minor - drv->minor;
1106                 spin_lock(&drv->lock);
1107                 if (drv->cs[index].flags & VALID_MINOR)
1108                         ret = drv->cs + index;
1109                 spin_unlock(&drv->lock);
1110                 if (ret)
1111                         break;
1112         }
1113         spin_unlock_irqrestore(&driver_lock, flags);
1114         return ret;
1115 }
1116
1117 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1118 {
1119         if (tty->index < 0 || tty->index >= tty->driver->num)
1120                 return NULL;
1121         return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1122 }
1123
1124 /**
1125  * gigaset_freedriver() - free all associated ressources of a driver
1126  * @drv:        driver descriptor structure.
1127  *
1128  * Unregisters the driver from the system and deallocates the driver
1129  * structure @drv and all structures referenced from it.
1130  * All devices should be shut down before calling this.
1131  */
1132 void gigaset_freedriver(struct gigaset_driver *drv)
1133 {
1134         unsigned long flags;
1135
1136         spin_lock_irqsave(&driver_lock, flags);
1137         list_del(&drv->list);
1138         spin_unlock_irqrestore(&driver_lock, flags);
1139
1140         gigaset_if_freedriver(drv);
1141
1142         kfree(drv->cs);
1143         kfree(drv);
1144 }
1145 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1146
1147 /**
1148  * gigaset_initdriver() - initialize driver structure
1149  * @minor:      First minor number
1150  * @minors:     Number of minors this driver can handle
1151  * @procname:   Name of the driver
1152  * @devname:    Name of the device files (prefix without minor number)
1153  *
1154  * Allocate and initialize gigaset_driver structure. Initialize interface.
1155  *
1156  * Return value:
1157  *      Pointer to the gigaset_driver structure on success, NULL on failure.
1158  */
1159 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1160                                           const char *procname,
1161                                           const char *devname,
1162                                           const struct gigaset_ops *ops,
1163                                           struct module *owner)
1164 {
1165         struct gigaset_driver *drv;
1166         unsigned long flags;
1167         unsigned i;
1168
1169         drv = kmalloc(sizeof *drv, GFP_KERNEL);
1170         if (!drv)
1171                 return NULL;
1172
1173         drv->have_tty = 0;
1174         drv->minor = minor;
1175         drv->minors = minors;
1176         spin_lock_init(&drv->lock);
1177         drv->blocked = 0;
1178         drv->ops = ops;
1179         drv->owner = owner;
1180         INIT_LIST_HEAD(&drv->list);
1181
1182         drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1183         if (!drv->cs)
1184                 goto error;
1185
1186         for (i = 0; i < minors; ++i) {
1187                 drv->cs[i].flags = 0;
1188                 drv->cs[i].driver = drv;
1189                 drv->cs[i].ops = drv->ops;
1190                 drv->cs[i].minor_index = i;
1191                 mutex_init(&drv->cs[i].mutex);
1192         }
1193
1194         gigaset_if_initdriver(drv, procname, devname);
1195
1196         spin_lock_irqsave(&driver_lock, flags);
1197         list_add(&drv->list, &drivers);
1198         spin_unlock_irqrestore(&driver_lock, flags);
1199
1200         return drv;
1201
1202 error:
1203         kfree(drv->cs);
1204         kfree(drv);
1205         return NULL;
1206 }
1207 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1208
1209 /**
1210  * gigaset_blockdriver() - block driver
1211  * @drv:        driver descriptor structure.
1212  *
1213  * Prevents the driver from attaching new devices, in preparation for
1214  * deregistration.
1215  */
1216 void gigaset_blockdriver(struct gigaset_driver *drv)
1217 {
1218         drv->blocked = 1;
1219 }
1220 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1221
1222 static int __init gigaset_init_module(void)
1223 {
1224         /* in accordance with the principle of least astonishment,
1225          * setting the 'debug' parameter to 1 activates a sensible
1226          * set of default debug levels
1227          */
1228         if (gigaset_debuglevel == 1)
1229                 gigaset_debuglevel = DEBUG_DEFAULT;
1230
1231         pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
1232         return 0;
1233 }
1234
1235 static void __exit gigaset_exit_module(void)
1236 {
1237 }
1238
1239 module_init(gigaset_init_module);
1240 module_exit(gigaset_exit_module);
1241
1242 MODULE_AUTHOR(DRIVER_AUTHOR);
1243 MODULE_DESCRIPTION(DRIVER_DESC);
1244
1245 MODULE_LICENSE("GPL");