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