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