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