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