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