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