Linux 2.6.34-rc3
[pohmelfs.git] / drivers / isdn / gigaset / common.c
blobbdc01cb9f0ab247960bc786fad05820c910fd532
1 /*
2 * Stuff used by all variants of the driver
4 * Copyright (c) 2001 by Stefan Eilers,
5 * Hansjoerg Lipp <hjlipp@web.de>,
6 * Tilman Schmidt <tilman@imap.cc>.
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 * =====================================================================
16 #include "gigaset.h"
17 #include <linux/ctype.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
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"
25 #ifdef CONFIG_GIGASET_DEBUG
26 #define DRIVER_DESC_DEBUG " (debug build)"
27 #else
28 #define DRIVER_DESC_DEBUG ""
29 #endif
31 /* Module parameters */
32 int gigaset_debuglevel;
33 EXPORT_SYMBOL_GPL(gigaset_debuglevel);
34 module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
35 MODULE_PARM_DESC(debug, "debug level");
37 /* driver state flags */
38 #define VALID_MINOR 0x01
39 #define VALID_ID 0x02
41 /**
42 * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging
43 * @level: debugging level.
44 * @msg: message prefix.
45 * @len: number of bytes to dump.
46 * @buf: data to dump.
48 * If the current debugging level includes one of the bits set in @level,
49 * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio,
50 * prefixed by the text @msg.
52 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
53 size_t len, const unsigned char *buf)
55 unsigned char outbuf[80];
56 unsigned char c;
57 size_t space = sizeof outbuf - 1;
58 unsigned char *out = outbuf;
59 size_t numin = len;
61 while (numin--) {
62 c = *buf++;
63 if (c == '~' || c == '^' || c == '\\') {
64 if (!space--)
65 break;
66 *out++ = '\\';
68 if (c & 0x80) {
69 if (!space--)
70 break;
71 *out++ = '~';
72 c ^= 0x80;
74 if (c < 0x20 || c == 0x7f) {
75 if (!space--)
76 break;
77 *out++ = '^';
78 c ^= 0x40;
80 if (!space--)
81 break;
82 *out++ = c;
84 *out = 0;
86 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
88 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
90 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
92 int r;
94 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
95 cs->control_state = flags;
96 if (r < 0)
97 return r;
99 if (delay) {
100 set_current_state(TASK_INTERRUPTIBLE);
101 schedule_timeout(delay * HZ / 1000);
104 return 0;
107 int gigaset_enterconfigmode(struct cardstate *cs)
109 int i, r;
111 cs->control_state = TIOCM_RTS;
113 r = setflags(cs, TIOCM_DTR, 200);
114 if (r < 0)
115 goto error;
116 r = setflags(cs, 0, 200);
117 if (r < 0)
118 goto error;
119 for (i = 0; i < 5; ++i) {
120 r = setflags(cs, TIOCM_RTS, 100);
121 if (r < 0)
122 goto error;
123 r = setflags(cs, 0, 100);
124 if (r < 0)
125 goto error;
127 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
128 if (r < 0)
129 goto error;
131 return 0;
133 error:
134 dev_err(cs->dev, "error %d on setuartbits\n", -r);
135 cs->control_state = TIOCM_RTS|TIOCM_DTR;
136 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
138 return -1;
141 static int test_timeout(struct at_state_t *at_state)
143 if (!at_state->timer_expires)
144 return 0;
146 if (--at_state->timer_expires) {
147 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
148 at_state, at_state->timer_expires);
149 return 0;
152 gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
153 at_state->timer_index, NULL);
154 return 1;
157 static void timer_tick(unsigned long data)
159 struct cardstate *cs = (struct cardstate *) data;
160 unsigned long flags;
161 unsigned channel;
162 struct at_state_t *at_state;
163 int timeout = 0;
165 spin_lock_irqsave(&cs->lock, flags);
167 for (channel = 0; channel < cs->channels; ++channel)
168 if (test_timeout(&cs->bcs[channel].at_state))
169 timeout = 1;
171 if (test_timeout(&cs->at_state))
172 timeout = 1;
174 list_for_each_entry(at_state, &cs->temp_at_states, list)
175 if (test_timeout(at_state))
176 timeout = 1;
178 if (cs->running) {
179 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
180 if (timeout) {
181 gig_dbg(DEBUG_EVENT, "scheduling timeout");
182 tasklet_schedule(&cs->event_tasklet);
186 spin_unlock_irqrestore(&cs->lock, flags);
189 int gigaset_get_channel(struct bc_state *bcs)
191 unsigned long flags;
193 spin_lock_irqsave(&bcs->cs->lock, flags);
194 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
195 gig_dbg(DEBUG_CHANNEL, "could not allocate channel %d",
196 bcs->channel);
197 spin_unlock_irqrestore(&bcs->cs->lock, flags);
198 return 0;
200 ++bcs->use_count;
201 bcs->busy = 1;
202 gig_dbg(DEBUG_CHANNEL, "allocated channel %d", bcs->channel);
203 spin_unlock_irqrestore(&bcs->cs->lock, flags);
204 return 1;
207 struct bc_state *gigaset_get_free_channel(struct cardstate *cs)
209 unsigned long flags;
210 int i;
212 spin_lock_irqsave(&cs->lock, flags);
213 if (!try_module_get(cs->driver->owner)) {
214 gig_dbg(DEBUG_CHANNEL,
215 "could not get module for allocating channel");
216 spin_unlock_irqrestore(&cs->lock, flags);
217 return NULL;
219 for (i = 0; i < cs->channels; ++i)
220 if (!cs->bcs[i].use_count) {
221 ++cs->bcs[i].use_count;
222 cs->bcs[i].busy = 1;
223 spin_unlock_irqrestore(&cs->lock, flags);
224 gig_dbg(DEBUG_CHANNEL, "allocated channel %d", i);
225 return cs->bcs + i;
227 module_put(cs->driver->owner);
228 spin_unlock_irqrestore(&cs->lock, flags);
229 gig_dbg(DEBUG_CHANNEL, "no free channel");
230 return NULL;
233 void gigaset_free_channel(struct bc_state *bcs)
235 unsigned long flags;
237 spin_lock_irqsave(&bcs->cs->lock, flags);
238 if (!bcs->busy) {
239 gig_dbg(DEBUG_CHANNEL, "could not free channel %d",
240 bcs->channel);
241 spin_unlock_irqrestore(&bcs->cs->lock, flags);
242 return;
244 --bcs->use_count;
245 bcs->busy = 0;
246 module_put(bcs->cs->driver->owner);
247 gig_dbg(DEBUG_CHANNEL, "freed channel %d", bcs->channel);
248 spin_unlock_irqrestore(&bcs->cs->lock, flags);
251 int gigaset_get_channels(struct cardstate *cs)
253 unsigned long flags;
254 int i;
256 spin_lock_irqsave(&cs->lock, flags);
257 for (i = 0; i < cs->channels; ++i)
258 if (cs->bcs[i].use_count) {
259 spin_unlock_irqrestore(&cs->lock, flags);
260 gig_dbg(DEBUG_CHANNEL,
261 "could not allocate all channels");
262 return 0;
264 for (i = 0; i < cs->channels; ++i)
265 ++cs->bcs[i].use_count;
266 spin_unlock_irqrestore(&cs->lock, flags);
268 gig_dbg(DEBUG_CHANNEL, "allocated all channels");
270 return 1;
273 void gigaset_free_channels(struct cardstate *cs)
275 unsigned long flags;
276 int i;
278 gig_dbg(DEBUG_CHANNEL, "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);
285 void gigaset_block_channels(struct cardstate *cs)
287 unsigned long flags;
288 int i;
290 gig_dbg(DEBUG_CHANNEL, "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);
297 static void clear_events(struct cardstate *cs)
299 struct event_t *ev;
300 unsigned head, tail;
301 unsigned long flags;
303 spin_lock_irqsave(&cs->ev_lock, flags);
305 head = cs->ev_head;
306 tail = cs->ev_tail;
308 while (tail != head) {
309 ev = cs->events + head;
310 kfree(ev->ptr);
311 head = (head + 1) % MAX_EVENTS;
314 cs->ev_head = tail;
316 spin_unlock_irqrestore(&cs->ev_lock, flags);
320 * gigaset_add_event() - add event to device event queue
321 * @cs: device descriptor structure.
322 * @at_state: connection state structure.
323 * @type: event type.
324 * @ptr: pointer parameter for event.
325 * @parameter: integer parameter for event.
326 * @arg: pointer parameter for event.
328 * Allocate an event queue entry from the device's event queue, and set it up
329 * with the parameters given.
331 * Return value: added event
333 struct event_t *gigaset_add_event(struct cardstate *cs,
334 struct at_state_t *at_state, int type,
335 void *ptr, int parameter, void *arg)
337 unsigned long flags;
338 unsigned next, tail;
339 struct event_t *event = NULL;
341 gig_dbg(DEBUG_EVENT, "queueing event %d", type);
343 spin_lock_irqsave(&cs->ev_lock, flags);
345 tail = cs->ev_tail;
346 next = (tail + 1) % MAX_EVENTS;
347 if (unlikely(next == cs->ev_head))
348 dev_err(cs->dev, "event queue full\n");
349 else {
350 event = cs->events + tail;
351 event->type = type;
352 event->at_state = at_state;
353 event->cid = -1;
354 event->ptr = ptr;
355 event->arg = arg;
356 event->parameter = parameter;
357 cs->ev_tail = next;
360 spin_unlock_irqrestore(&cs->ev_lock, flags);
362 return event;
364 EXPORT_SYMBOL_GPL(gigaset_add_event);
366 static void free_strings(struct at_state_t *at_state)
368 int i;
370 for (i = 0; i < STR_NUM; ++i) {
371 kfree(at_state->str_var[i]);
372 at_state->str_var[i] = NULL;
376 static void clear_at_state(struct at_state_t *at_state)
378 free_strings(at_state);
381 static void dealloc_at_states(struct cardstate *cs)
383 struct at_state_t *cur, *next;
385 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
386 list_del(&cur->list);
387 free_strings(cur);
388 kfree(cur);
392 static void gigaset_freebcs(struct bc_state *bcs)
394 int i;
396 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
397 if (!bcs->cs->ops->freebcshw(bcs))
398 gig_dbg(DEBUG_INIT, "failed");
400 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
401 clear_at_state(&bcs->at_state);
402 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
403 dev_kfree_skb(bcs->skb);
404 bcs->skb = NULL;
406 for (i = 0; i < AT_NUM; ++i) {
407 kfree(bcs->commands[i]);
408 bcs->commands[i] = NULL;
412 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
414 unsigned long flags;
415 unsigned i;
416 struct cardstate *cs;
417 struct cardstate *ret = NULL;
419 spin_lock_irqsave(&drv->lock, flags);
420 if (drv->blocked)
421 goto exit;
422 for (i = 0; i < drv->minors; ++i) {
423 cs = drv->cs + i;
424 if (!(cs->flags & VALID_MINOR)) {
425 cs->flags = VALID_MINOR;
426 ret = cs;
427 break;
430 exit:
431 spin_unlock_irqrestore(&drv->lock, flags);
432 return ret;
435 static void free_cs(struct cardstate *cs)
437 cs->flags = 0;
440 static void make_valid(struct cardstate *cs, unsigned mask)
442 unsigned long flags;
443 struct gigaset_driver *drv = cs->driver;
444 spin_lock_irqsave(&drv->lock, flags);
445 cs->flags |= mask;
446 spin_unlock_irqrestore(&drv->lock, flags);
449 static void make_invalid(struct cardstate *cs, unsigned mask)
451 unsigned long flags;
452 struct gigaset_driver *drv = cs->driver;
453 spin_lock_irqsave(&drv->lock, flags);
454 cs->flags &= ~mask;
455 spin_unlock_irqrestore(&drv->lock, flags);
459 * gigaset_freecs() - free all associated ressources of a device
460 * @cs: device descriptor structure.
462 * Stops all tasklets and timers, unregisters the device from all
463 * subsystems it was registered to, deallocates the device structure
464 * @cs and all structures referenced from it.
465 * Operations on the device should be stopped before calling this.
467 void gigaset_freecs(struct cardstate *cs)
469 int i;
470 unsigned long flags;
472 if (!cs)
473 return;
475 mutex_lock(&cs->mutex);
477 if (!cs->bcs)
478 goto f_cs;
479 if (!cs->inbuf)
480 goto f_bcs;
482 spin_lock_irqsave(&cs->lock, flags);
483 cs->running = 0;
484 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
485 not rescheduled below */
487 tasklet_kill(&cs->event_tasklet);
488 del_timer_sync(&cs->timer);
490 switch (cs->cs_init) {
491 default:
492 /* clear B channel structures */
493 for (i = 0; i < cs->channels; ++i) {
494 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
495 gigaset_freebcs(cs->bcs + i);
498 /* clear device sysfs */
499 gigaset_free_dev_sysfs(cs);
501 gigaset_if_free(cs);
503 gig_dbg(DEBUG_INIT, "clearing hw");
504 cs->ops->freecshw(cs);
506 /* fall through */
507 case 2: /* error in initcshw */
508 /* Deregister from LL */
509 make_invalid(cs, VALID_ID);
510 gigaset_isdn_unregdev(cs);
512 /* fall through */
513 case 1: /* error when registering to LL */
514 gig_dbg(DEBUG_INIT, "clearing at_state");
515 clear_at_state(&cs->at_state);
516 dealloc_at_states(cs);
518 /* fall through */
519 case 0: /* error in basic setup */
520 clear_events(cs);
521 gig_dbg(DEBUG_INIT, "freeing inbuf");
522 kfree(cs->inbuf);
524 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
525 kfree(cs->bcs);
526 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
527 mutex_unlock(&cs->mutex);
528 free_cs(cs);
530 EXPORT_SYMBOL_GPL(gigaset_freecs);
532 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
533 struct cardstate *cs, int cid)
535 int i;
537 INIT_LIST_HEAD(&at_state->list);
538 at_state->waiting = 0;
539 at_state->getstring = 0;
540 at_state->pending_commands = 0;
541 at_state->timer_expires = 0;
542 at_state->timer_active = 0;
543 at_state->timer_index = 0;
544 at_state->seq_index = 0;
545 at_state->ConState = 0;
546 for (i = 0; i < STR_NUM; ++i)
547 at_state->str_var[i] = NULL;
548 at_state->int_var[VAR_ZDLE] = 0;
549 at_state->int_var[VAR_ZCTP] = -1;
550 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
551 at_state->cs = cs;
552 at_state->bcs = bcs;
553 at_state->cid = cid;
554 if (!cid)
555 at_state->replystruct = cs->tabnocid;
556 else
557 at_state->replystruct = cs->tabcid;
561 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
562 /* inbuf->read must be allocated before! */
564 inbuf->head = 0;
565 inbuf->tail = 0;
566 inbuf->cs = cs;
567 inbuf->inputstate = INS_command;
571 * gigaset_fill_inbuf() - append received data to input buffer
572 * @inbuf: buffer structure.
573 * @src: received data.
574 * @numbytes: number of bytes received.
576 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
577 unsigned numbytes)
579 unsigned n, head, tail, bytesleft;
581 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
583 if (!numbytes)
584 return 0;
586 bytesleft = numbytes;
587 tail = inbuf->tail;
588 head = inbuf->head;
589 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
591 while (bytesleft) {
592 if (head > tail)
593 n = head - 1 - tail;
594 else if (head == 0)
595 n = (RBUFSIZE-1) - tail;
596 else
597 n = RBUFSIZE - tail;
598 if (!n) {
599 dev_err(inbuf->cs->dev,
600 "buffer overflow (%u bytes lost)\n",
601 bytesleft);
602 break;
604 if (n > bytesleft)
605 n = bytesleft;
606 memcpy(inbuf->data + tail, src, n);
607 bytesleft -= n;
608 tail = (tail + n) % RBUFSIZE;
609 src += n;
611 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
612 inbuf->tail = tail;
613 return numbytes != bytesleft;
615 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
617 /* Initialize the b-channel structure */
618 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
619 struct cardstate *cs, int channel)
621 int i;
623 bcs->tx_skb = NULL;
625 skb_queue_head_init(&bcs->squeue);
627 bcs->corrupted = 0;
628 bcs->trans_down = 0;
629 bcs->trans_up = 0;
631 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
632 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
634 #ifdef CONFIG_GIGASET_DEBUG
635 bcs->emptycount = 0;
636 #endif
638 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
639 bcs->fcs = PPP_INITFCS;
640 bcs->inputstate = 0;
641 if (cs->ignoreframes) {
642 bcs->skb = NULL;
643 } else {
644 bcs->skb = dev_alloc_skb(SBUFSIZE + cs->hw_hdr_len);
645 if (bcs->skb != NULL)
646 skb_reserve(bcs->skb, cs->hw_hdr_len);
647 else
648 pr_err("out of memory\n");
651 bcs->channel = channel;
652 bcs->cs = cs;
654 bcs->chstate = 0;
655 bcs->use_count = 1;
656 bcs->busy = 0;
657 bcs->ignore = cs->ignoreframes;
659 for (i = 0; i < AT_NUM; ++i)
660 bcs->commands[i] = NULL;
662 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
663 if (cs->ops->initbcshw(bcs))
664 return bcs;
666 gig_dbg(DEBUG_INIT, " failed");
668 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
669 dev_kfree_skb(bcs->skb);
670 bcs->skb = NULL;
672 return NULL;
676 * gigaset_initcs() - initialize device structure
677 * @drv: hardware driver the device belongs to
678 * @channels: number of B channels supported by device
679 * @onechannel: !=0 if B channel data and AT commands share one
680 * communication channel (M10x),
681 * ==0 if B channels have separate communication channels (base)
682 * @ignoreframes: number of frames to ignore after setting up B channel
683 * @cidmode: !=0: start in CallID mode
684 * @modulename: name of driver module for LL registration
686 * Allocate and initialize cardstate structure for Gigaset driver
687 * Calls hardware dependent gigaset_initcshw() function
688 * Calls B channel initialization function gigaset_initbcs() for each B channel
690 * Return value:
691 * pointer to cardstate structure
693 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
694 int onechannel, int ignoreframes,
695 int cidmode, const char *modulename)
697 struct cardstate *cs;
698 unsigned long flags;
699 int i;
701 gig_dbg(DEBUG_INIT, "allocating cs");
702 cs = alloc_cs(drv);
703 if (!cs) {
704 pr_err("maximum number of devices exceeded\n");
705 return NULL;
708 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
709 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
710 if (!cs->bcs) {
711 pr_err("out of memory\n");
712 goto error;
714 gig_dbg(DEBUG_INIT, "allocating inbuf");
715 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
716 if (!cs->inbuf) {
717 pr_err("out of memory\n");
718 goto error;
721 cs->cs_init = 0;
722 cs->channels = channels;
723 cs->onechannel = onechannel;
724 cs->ignoreframes = ignoreframes;
725 INIT_LIST_HEAD(&cs->temp_at_states);
726 cs->running = 0;
727 init_timer(&cs->timer); /* clear next & prev */
728 spin_lock_init(&cs->ev_lock);
729 cs->ev_tail = 0;
730 cs->ev_head = 0;
732 tasklet_init(&cs->event_tasklet, gigaset_handle_event,
733 (unsigned long) cs);
734 cs->commands_pending = 0;
735 cs->cur_at_seq = 0;
736 cs->gotfwver = -1;
737 cs->open_count = 0;
738 cs->dev = NULL;
739 cs->tty = NULL;
740 cs->tty_dev = NULL;
741 cs->cidmode = cidmode != 0;
742 cs->tabnocid = gigaset_tab_nocid;
743 cs->tabcid = gigaset_tab_cid;
745 init_waitqueue_head(&cs->waitqueue);
746 cs->waiting = 0;
748 cs->mode = M_UNKNOWN;
749 cs->mstate = MS_UNINITIALIZED;
751 ++cs->cs_init;
753 gig_dbg(DEBUG_INIT, "setting up at_state");
754 spin_lock_init(&cs->lock);
755 gigaset_at_init(&cs->at_state, NULL, cs, 0);
756 cs->dle = 0;
757 cs->cbytes = 0;
759 gig_dbg(DEBUG_INIT, "setting up inbuf");
760 gigaset_inbuf_init(cs->inbuf, cs);
762 cs->connected = 0;
763 cs->isdn_up = 0;
765 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
766 cs->cmdbuf = cs->lastcmdbuf = NULL;
767 spin_lock_init(&cs->cmdlock);
768 cs->curlen = 0;
769 cs->cmdbytes = 0;
771 gig_dbg(DEBUG_INIT, "setting up iif");
772 if (!gigaset_isdn_regdev(cs, modulename)) {
773 pr_err("error registering ISDN device\n");
774 goto error;
777 make_valid(cs, VALID_ID);
778 ++cs->cs_init;
779 gig_dbg(DEBUG_INIT, "setting up hw");
780 if (!cs->ops->initcshw(cs))
781 goto error;
783 ++cs->cs_init;
785 /* set up character device */
786 gigaset_if_init(cs);
788 /* set up device sysfs */
789 gigaset_init_dev_sysfs(cs);
791 /* set up channel data structures */
792 for (i = 0; i < channels; ++i) {
793 gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
794 if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
795 pr_err("could not allocate channel %d data\n", i);
796 goto error;
800 spin_lock_irqsave(&cs->lock, flags);
801 cs->running = 1;
802 spin_unlock_irqrestore(&cs->lock, flags);
803 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
804 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
805 /* FIXME: can jiffies increase too much until the timer is added?
806 * Same problem(?) with mod_timer() in timer_tick(). */
807 add_timer(&cs->timer);
809 gig_dbg(DEBUG_INIT, "cs initialized");
810 return cs;
812 error:
813 gig_dbg(DEBUG_INIT, "failed");
814 gigaset_freecs(cs);
815 return NULL;
817 EXPORT_SYMBOL_GPL(gigaset_initcs);
819 /* ReInitialize the b-channel structure on hangup */
820 void gigaset_bcs_reinit(struct bc_state *bcs)
822 struct sk_buff *skb;
823 struct cardstate *cs = bcs->cs;
824 unsigned long flags;
826 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
827 dev_kfree_skb(skb);
829 spin_lock_irqsave(&cs->lock, flags);
830 clear_at_state(&bcs->at_state);
831 bcs->at_state.ConState = 0;
832 bcs->at_state.timer_active = 0;
833 bcs->at_state.timer_expires = 0;
834 bcs->at_state.cid = -1; /* No CID defined */
835 spin_unlock_irqrestore(&cs->lock, flags);
837 bcs->inputstate = 0;
839 #ifdef CONFIG_GIGASET_DEBUG
840 bcs->emptycount = 0;
841 #endif
843 bcs->fcs = PPP_INITFCS;
844 bcs->chstate = 0;
846 bcs->ignore = cs->ignoreframes;
847 if (bcs->ignore) {
848 dev_kfree_skb(bcs->skb);
849 bcs->skb = NULL;
852 cs->ops->reinitbcshw(bcs);
855 static void cleanup_cs(struct cardstate *cs)
857 struct cmdbuf_t *cb, *tcb;
858 int i;
859 unsigned long flags;
861 spin_lock_irqsave(&cs->lock, flags);
863 cs->mode = M_UNKNOWN;
864 cs->mstate = MS_UNINITIALIZED;
866 clear_at_state(&cs->at_state);
867 dealloc_at_states(cs);
868 free_strings(&cs->at_state);
869 gigaset_at_init(&cs->at_state, NULL, cs, 0);
871 cs->inbuf->inputstate = INS_command;
872 cs->inbuf->head = 0;
873 cs->inbuf->tail = 0;
875 cb = cs->cmdbuf;
876 while (cb) {
877 tcb = cb;
878 cb = cb->next;
879 kfree(tcb);
881 cs->cmdbuf = cs->lastcmdbuf = NULL;
882 cs->curlen = 0;
883 cs->cmdbytes = 0;
884 cs->gotfwver = -1;
885 cs->dle = 0;
886 cs->cur_at_seq = 0;
887 cs->commands_pending = 0;
888 cs->cbytes = 0;
890 spin_unlock_irqrestore(&cs->lock, flags);
892 for (i = 0; i < cs->channels; ++i) {
893 gigaset_freebcs(cs->bcs + i);
894 if (!gigaset_initbcs(cs->bcs + i, cs, i))
895 pr_err("could not allocate channel %d data\n", i);
898 if (cs->waiting) {
899 cs->cmd_result = -ENODEV;
900 cs->waiting = 0;
901 wake_up_interruptible(&cs->waitqueue);
907 * gigaset_start() - start device operations
908 * @cs: device descriptor structure.
910 * Prepares the device for use by setting up communication parameters,
911 * scheduling an EV_START event to initiate device initialization, and
912 * waiting for completion of the initialization.
914 * Return value:
915 * 1 - success, 0 - error
917 int gigaset_start(struct cardstate *cs)
919 unsigned long flags;
921 if (mutex_lock_interruptible(&cs->mutex))
922 return 0;
924 spin_lock_irqsave(&cs->lock, flags);
925 cs->connected = 1;
926 spin_unlock_irqrestore(&cs->lock, flags);
928 if (cs->mstate != MS_LOCKED) {
929 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
930 cs->ops->baud_rate(cs, B115200);
931 cs->ops->set_line_ctrl(cs, CS8);
932 cs->control_state = TIOCM_DTR|TIOCM_RTS;
935 cs->waiting = 1;
937 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
938 cs->waiting = 0;
939 goto error;
941 gigaset_schedule_event(cs);
943 wait_event(cs->waitqueue, !cs->waiting);
945 mutex_unlock(&cs->mutex);
946 return 1;
948 error:
949 mutex_unlock(&cs->mutex);
950 return 0;
952 EXPORT_SYMBOL_GPL(gigaset_start);
955 * gigaset_shutdown() - shut down device operations
956 * @cs: device descriptor structure.
958 * Deactivates the device by scheduling an EV_SHUTDOWN event and
959 * waiting for completion of the shutdown.
961 * Return value:
962 * 0 - success, -1 - error (no device associated)
964 int gigaset_shutdown(struct cardstate *cs)
966 mutex_lock(&cs->mutex);
968 if (!(cs->flags & VALID_MINOR)) {
969 mutex_unlock(&cs->mutex);
970 return -1;
973 cs->waiting = 1;
975 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL))
976 goto exit;
977 gigaset_schedule_event(cs);
979 wait_event(cs->waitqueue, !cs->waiting);
981 cleanup_cs(cs);
983 exit:
984 mutex_unlock(&cs->mutex);
985 return 0;
987 EXPORT_SYMBOL_GPL(gigaset_shutdown);
990 * gigaset_stop() - stop device operations
991 * @cs: device descriptor structure.
993 * Stops operations on the device by scheduling an EV_STOP event and
994 * waiting for completion of the shutdown.
996 void gigaset_stop(struct cardstate *cs)
998 mutex_lock(&cs->mutex);
1000 cs->waiting = 1;
1002 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL))
1003 goto exit;
1004 gigaset_schedule_event(cs);
1006 wait_event(cs->waitqueue, !cs->waiting);
1008 cleanup_cs(cs);
1010 exit:
1011 mutex_unlock(&cs->mutex);
1013 EXPORT_SYMBOL_GPL(gigaset_stop);
1015 static LIST_HEAD(drivers);
1016 static DEFINE_SPINLOCK(driver_lock);
1018 struct cardstate *gigaset_get_cs_by_id(int id)
1020 unsigned long flags;
1021 struct cardstate *ret = NULL;
1022 struct cardstate *cs;
1023 struct gigaset_driver *drv;
1024 unsigned i;
1026 spin_lock_irqsave(&driver_lock, flags);
1027 list_for_each_entry(drv, &drivers, list) {
1028 spin_lock(&drv->lock);
1029 for (i = 0; i < drv->minors; ++i) {
1030 cs = drv->cs + i;
1031 if ((cs->flags & VALID_ID) && cs->myid == id) {
1032 ret = cs;
1033 break;
1036 spin_unlock(&drv->lock);
1037 if (ret)
1038 break;
1040 spin_unlock_irqrestore(&driver_lock, flags);
1041 return ret;
1044 void gigaset_debugdrivers(void)
1046 unsigned long flags;
1047 static struct cardstate *cs;
1048 struct gigaset_driver *drv;
1049 unsigned i;
1051 spin_lock_irqsave(&driver_lock, flags);
1052 list_for_each_entry(drv, &drivers, list) {
1053 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
1054 spin_lock(&drv->lock);
1055 for (i = 0; i < drv->minors; ++i) {
1056 gig_dbg(DEBUG_DRIVER, " index %u", i);
1057 cs = drv->cs + i;
1058 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
1059 gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
1060 gig_dbg(DEBUG_DRIVER, " minor_index %u",
1061 cs->minor_index);
1062 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
1063 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
1065 spin_unlock(&drv->lock);
1067 spin_unlock_irqrestore(&driver_lock, flags);
1070 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1072 unsigned long flags;
1073 struct cardstate *ret = NULL;
1074 struct gigaset_driver *drv;
1075 unsigned index;
1077 spin_lock_irqsave(&driver_lock, flags);
1078 list_for_each_entry(drv, &drivers, list) {
1079 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1080 continue;
1081 index = minor - drv->minor;
1082 spin_lock(&drv->lock);
1083 if (drv->cs[index].flags & VALID_MINOR)
1084 ret = drv->cs + index;
1085 spin_unlock(&drv->lock);
1086 if (ret)
1087 break;
1089 spin_unlock_irqrestore(&driver_lock, flags);
1090 return ret;
1093 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1095 if (tty->index < 0 || tty->index >= tty->driver->num)
1096 return NULL;
1097 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1101 * gigaset_freedriver() - free all associated ressources of a driver
1102 * @drv: driver descriptor structure.
1104 * Unregisters the driver from the system and deallocates the driver
1105 * structure @drv and all structures referenced from it.
1106 * All devices should be shut down before calling this.
1108 void gigaset_freedriver(struct gigaset_driver *drv)
1110 unsigned long flags;
1112 spin_lock_irqsave(&driver_lock, flags);
1113 list_del(&drv->list);
1114 spin_unlock_irqrestore(&driver_lock, flags);
1116 gigaset_if_freedriver(drv);
1118 kfree(drv->cs);
1119 kfree(drv);
1121 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1124 * gigaset_initdriver() - initialize driver structure
1125 * @minor: First minor number
1126 * @minors: Number of minors this driver can handle
1127 * @procname: Name of the driver
1128 * @devname: Name of the device files (prefix without minor number)
1130 * Allocate and initialize gigaset_driver structure. Initialize interface.
1132 * Return value:
1133 * Pointer to the gigaset_driver structure on success, NULL on failure.
1135 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1136 const char *procname,
1137 const char *devname,
1138 const struct gigaset_ops *ops,
1139 struct module *owner)
1141 struct gigaset_driver *drv;
1142 unsigned long flags;
1143 unsigned i;
1145 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1146 if (!drv)
1147 return NULL;
1149 drv->have_tty = 0;
1150 drv->minor = minor;
1151 drv->minors = minors;
1152 spin_lock_init(&drv->lock);
1153 drv->blocked = 0;
1154 drv->ops = ops;
1155 drv->owner = owner;
1156 INIT_LIST_HEAD(&drv->list);
1158 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1159 if (!drv->cs)
1160 goto error;
1162 for (i = 0; i < minors; ++i) {
1163 drv->cs[i].flags = 0;
1164 drv->cs[i].driver = drv;
1165 drv->cs[i].ops = drv->ops;
1166 drv->cs[i].minor_index = i;
1167 mutex_init(&drv->cs[i].mutex);
1170 gigaset_if_initdriver(drv, procname, devname);
1172 spin_lock_irqsave(&driver_lock, flags);
1173 list_add(&drv->list, &drivers);
1174 spin_unlock_irqrestore(&driver_lock, flags);
1176 return drv;
1178 error:
1179 kfree(drv->cs);
1180 kfree(drv);
1181 return NULL;
1183 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1186 * gigaset_blockdriver() - block driver
1187 * @drv: driver descriptor structure.
1189 * Prevents the driver from attaching new devices, in preparation for
1190 * deregistration.
1192 void gigaset_blockdriver(struct gigaset_driver *drv)
1194 drv->blocked = 1;
1196 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1198 static int __init gigaset_init_module(void)
1200 /* in accordance with the principle of least astonishment,
1201 * setting the 'debug' parameter to 1 activates a sensible
1202 * set of default debug levels
1204 if (gigaset_debuglevel == 1)
1205 gigaset_debuglevel = DEBUG_DEFAULT;
1207 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
1208 gigaset_isdn_regdrv();
1209 return 0;
1212 static void __exit gigaset_exit_module(void)
1214 gigaset_isdn_unregdrv();
1217 module_init(gigaset_init_module);
1218 module_exit(gigaset_exit_module);
1220 MODULE_AUTHOR(DRIVER_AUTHOR);
1221 MODULE_DESCRIPTION(DRIVER_DESC);
1223 MODULE_LICENSE("GPL");