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