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