Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / isdn / gigaset / common.c
blob827c32c167951aa3235477992697909ccd770944
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 /* 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");
31 /* driver state flags */
32 #define VALID_MINOR 0x01
33 #define VALID_ID 0x02
35 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
36 size_t len, const unsigned char *buf)
38 unsigned char outbuf[80];
39 unsigned char c;
40 size_t space = sizeof outbuf - 1;
41 unsigned char *out = outbuf;
42 size_t numin = len;
44 while (numin--) {
45 c = *buf++;
46 if (c == '~' || c == '^' || c == '\\') {
47 if (!space--)
48 break;
49 *out++ = '\\';
51 if (c & 0x80) {
52 if (!space--)
53 break;
54 *out++ = '~';
55 c ^= 0x80;
57 if (c < 0x20 || c == 0x7f) {
58 if (!space--)
59 break;
60 *out++ = '^';
61 c ^= 0x40;
63 if (!space--)
64 break;
65 *out++ = c;
67 *out = 0;
69 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
71 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
73 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
75 int r;
77 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
78 cs->control_state = flags;
79 if (r < 0)
80 return r;
82 if (delay) {
83 set_current_state(TASK_INTERRUPTIBLE);
84 schedule_timeout(delay * HZ / 1000);
87 return 0;
90 int gigaset_enterconfigmode(struct cardstate *cs)
92 int i, r;
94 cs->control_state = TIOCM_RTS; //FIXME
96 r = setflags(cs, TIOCM_DTR, 200);
97 if (r < 0)
98 goto error;
99 r = setflags(cs, 0, 200);
100 if (r < 0)
101 goto error;
102 for (i = 0; i < 5; ++i) {
103 r = setflags(cs, TIOCM_RTS, 100);
104 if (r < 0)
105 goto error;
106 r = setflags(cs, 0, 100);
107 if (r < 0)
108 goto error;
110 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
111 if (r < 0)
112 goto error;
114 return 0;
116 error:
117 dev_err(cs->dev, "error %d on setuartbits\n", -r);
118 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
119 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
121 return -1; //r
124 static int test_timeout(struct at_state_t *at_state)
126 if (!at_state->timer_expires)
127 return 0;
129 if (--at_state->timer_expires) {
130 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
131 at_state, at_state->timer_expires);
132 return 0;
135 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
136 at_state->timer_index, NULL)) {
137 //FIXME what should we do?
140 return 1;
143 static void timer_tick(unsigned long data)
145 struct cardstate *cs = (struct cardstate *) data;
146 unsigned long flags;
147 unsigned channel;
148 struct at_state_t *at_state;
149 int timeout = 0;
151 spin_lock_irqsave(&cs->lock, flags);
153 for (channel = 0; channel < cs->channels; ++channel)
154 if (test_timeout(&cs->bcs[channel].at_state))
155 timeout = 1;
157 if (test_timeout(&cs->at_state))
158 timeout = 1;
160 list_for_each_entry(at_state, &cs->temp_at_states, list)
161 if (test_timeout(at_state))
162 timeout = 1;
164 if (cs->running) {
165 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
166 if (timeout) {
167 gig_dbg(DEBUG_CMD, "scheduling timeout");
168 tasklet_schedule(&cs->event_tasklet);
172 spin_unlock_irqrestore(&cs->lock, flags);
175 int gigaset_get_channel(struct bc_state *bcs)
177 unsigned long flags;
179 spin_lock_irqsave(&bcs->cs->lock, flags);
180 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
181 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
182 bcs->channel);
183 spin_unlock_irqrestore(&bcs->cs->lock, flags);
184 return 0;
186 ++bcs->use_count;
187 bcs->busy = 1;
188 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
189 spin_unlock_irqrestore(&bcs->cs->lock, flags);
190 return 1;
193 void gigaset_free_channel(struct bc_state *bcs)
195 unsigned long flags;
197 spin_lock_irqsave(&bcs->cs->lock, flags);
198 if (!bcs->busy) {
199 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
200 spin_unlock_irqrestore(&bcs->cs->lock, flags);
201 return;
203 --bcs->use_count;
204 bcs->busy = 0;
205 module_put(bcs->cs->driver->owner);
206 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
207 spin_unlock_irqrestore(&bcs->cs->lock, flags);
210 int gigaset_get_channels(struct cardstate *cs)
212 unsigned long flags;
213 int i;
215 spin_lock_irqsave(&cs->lock, flags);
216 for (i = 0; i < cs->channels; ++i)
217 if (cs->bcs[i].use_count) {
218 spin_unlock_irqrestore(&cs->lock, flags);
219 gig_dbg(DEBUG_ANY, "could not allocate all channels");
220 return 0;
222 for (i = 0; i < cs->channels; ++i)
223 ++cs->bcs[i].use_count;
224 spin_unlock_irqrestore(&cs->lock, flags);
226 gig_dbg(DEBUG_ANY, "allocated all channels");
228 return 1;
231 void gigaset_free_channels(struct cardstate *cs)
233 unsigned long flags;
234 int i;
236 gig_dbg(DEBUG_ANY, "unblocking all channels");
237 spin_lock_irqsave(&cs->lock, flags);
238 for (i = 0; i < cs->channels; ++i)
239 --cs->bcs[i].use_count;
240 spin_unlock_irqrestore(&cs->lock, flags);
243 void gigaset_block_channels(struct cardstate *cs)
245 unsigned long flags;
246 int i;
248 gig_dbg(DEBUG_ANY, "blocking all channels");
249 spin_lock_irqsave(&cs->lock, flags);
250 for (i = 0; i < cs->channels; ++i)
251 ++cs->bcs[i].use_count;
252 spin_unlock_irqrestore(&cs->lock, flags);
255 static void clear_events(struct cardstate *cs)
257 struct event_t *ev;
258 unsigned head, tail;
259 unsigned long flags;
261 spin_lock_irqsave(&cs->ev_lock, flags);
263 head = cs->ev_head;
264 tail = cs->ev_tail;
266 while (tail != head) {
267 ev = cs->events + head;
268 kfree(ev->ptr);
269 head = (head + 1) % MAX_EVENTS;
272 cs->ev_head = tail;
274 spin_unlock_irqrestore(&cs->ev_lock, flags);
277 struct event_t *gigaset_add_event(struct cardstate *cs,
278 struct at_state_t *at_state, int type,
279 void *ptr, int parameter, void *arg)
281 unsigned long flags;
282 unsigned next, tail;
283 struct event_t *event = NULL;
285 spin_lock_irqsave(&cs->ev_lock, flags);
287 tail = cs->ev_tail;
288 next = (tail + 1) % MAX_EVENTS;
289 if (unlikely(next == cs->ev_head))
290 err("event queue full");
291 else {
292 event = cs->events + tail;
293 event->type = type;
294 event->at_state = at_state;
295 event->cid = -1;
296 event->ptr = ptr;
297 event->arg = arg;
298 event->parameter = parameter;
299 cs->ev_tail = next;
302 spin_unlock_irqrestore(&cs->ev_lock, flags);
304 return event;
306 EXPORT_SYMBOL_GPL(gigaset_add_event);
308 static void free_strings(struct at_state_t *at_state)
310 int i;
312 for (i = 0; i < STR_NUM; ++i) {
313 kfree(at_state->str_var[i]);
314 at_state->str_var[i] = NULL;
318 static void clear_at_state(struct at_state_t *at_state)
320 free_strings(at_state);
323 static void dealloc_at_states(struct cardstate *cs)
325 struct at_state_t *cur, *next;
327 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
328 list_del(&cur->list);
329 free_strings(cur);
330 kfree(cur);
334 static void gigaset_freebcs(struct bc_state *bcs)
336 int i;
338 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
339 if (!bcs->cs->ops->freebcshw(bcs)) {
340 gig_dbg(DEBUG_INIT, "failed");
343 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
344 clear_at_state(&bcs->at_state);
345 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
347 if (bcs->skb)
348 dev_kfree_skb(bcs->skb);
349 for (i = 0; i < AT_NUM; ++i) {
350 kfree(bcs->commands[i]);
351 bcs->commands[i] = NULL;
355 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
357 unsigned long flags;
358 unsigned i;
359 struct cardstate *cs;
360 struct cardstate *ret = NULL;
362 spin_lock_irqsave(&drv->lock, flags);
363 if (drv->blocked)
364 goto exit;
365 for (i = 0; i < drv->minors; ++i) {
366 cs = drv->cs + i;
367 if (!(cs->flags & VALID_MINOR)) {
368 cs->flags = VALID_MINOR;
369 ret = cs;
370 break;
373 exit:
374 spin_unlock_irqrestore(&drv->lock, flags);
375 return ret;
378 static void free_cs(struct cardstate *cs)
380 cs->flags = 0;
383 static void make_valid(struct cardstate *cs, unsigned mask)
385 unsigned long flags;
386 struct gigaset_driver *drv = cs->driver;
387 spin_lock_irqsave(&drv->lock, flags);
388 cs->flags |= mask;
389 spin_unlock_irqrestore(&drv->lock, flags);
392 static void make_invalid(struct cardstate *cs, unsigned mask)
394 unsigned long flags;
395 struct gigaset_driver *drv = cs->driver;
396 spin_lock_irqsave(&drv->lock, flags);
397 cs->flags &= ~mask;
398 spin_unlock_irqrestore(&drv->lock, flags);
401 void gigaset_freecs(struct cardstate *cs)
403 int i;
404 unsigned long flags;
406 if (!cs)
407 return;
409 mutex_lock(&cs->mutex);
411 if (!cs->bcs)
412 goto f_cs;
413 if (!cs->inbuf)
414 goto f_bcs;
416 spin_lock_irqsave(&cs->lock, flags);
417 cs->running = 0;
418 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
419 not rescheduled below */
421 tasklet_kill(&cs->event_tasklet);
422 del_timer_sync(&cs->timer);
424 switch (cs->cs_init) {
425 default:
426 /* clear device sysfs */
427 gigaset_free_dev_sysfs(cs);
429 gigaset_if_free(cs);
431 gig_dbg(DEBUG_INIT, "clearing hw");
432 cs->ops->freecshw(cs);
434 //FIXME cmdbuf
436 /* fall through */
437 case 2: /* error in initcshw */
438 /* Deregister from LL */
439 make_invalid(cs, VALID_ID);
440 gig_dbg(DEBUG_INIT, "clearing iif");
441 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
443 /* fall through */
444 case 1: /* error when regestering to LL */
445 gig_dbg(DEBUG_INIT, "clearing at_state");
446 clear_at_state(&cs->at_state);
447 dealloc_at_states(cs);
449 /* fall through */
450 case 0: /* error in one call to initbcs */
451 for (i = 0; i < cs->channels; ++i) {
452 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
453 gigaset_freebcs(cs->bcs + i);
456 clear_events(cs);
457 gig_dbg(DEBUG_INIT, "freeing inbuf");
458 kfree(cs->inbuf);
460 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
461 kfree(cs->bcs);
462 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
463 mutex_unlock(&cs->mutex);
464 free_cs(cs);
466 EXPORT_SYMBOL_GPL(gigaset_freecs);
468 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
469 struct cardstate *cs, int cid)
471 int i;
473 INIT_LIST_HEAD(&at_state->list);
474 at_state->waiting = 0;
475 at_state->getstring = 0;
476 at_state->pending_commands = 0;
477 at_state->timer_expires = 0;
478 at_state->timer_active = 0;
479 at_state->timer_index = 0;
480 at_state->seq_index = 0;
481 at_state->ConState = 0;
482 for (i = 0; i < STR_NUM; ++i)
483 at_state->str_var[i] = NULL;
484 at_state->int_var[VAR_ZDLE] = 0;
485 at_state->int_var[VAR_ZCTP] = -1;
486 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
487 at_state->cs = cs;
488 at_state->bcs = bcs;
489 at_state->cid = cid;
490 if (!cid)
491 at_state->replystruct = cs->tabnocid;
492 else
493 at_state->replystruct = cs->tabcid;
497 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
498 struct cardstate *cs, int inputstate)
499 /* inbuf->read must be allocated before! */
501 inbuf->head = 0;
502 inbuf->tail = 0;
503 inbuf->cs = cs;
504 inbuf->bcs = bcs; /*base driver: NULL*/
505 inbuf->rcvbuf = NULL;
506 inbuf->inputstate = inputstate;
509 /* append received bytes to inbuf */
510 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
511 unsigned numbytes)
513 unsigned n, head, tail, bytesleft;
515 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
517 if (!numbytes)
518 return 0;
520 bytesleft = numbytes;
521 tail = inbuf->tail;
522 head = inbuf->head;
523 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
525 while (bytesleft) {
526 if (head > tail)
527 n = head - 1 - tail;
528 else if (head == 0)
529 n = (RBUFSIZE-1) - tail;
530 else
531 n = RBUFSIZE - tail;
532 if (!n) {
533 dev_err(inbuf->cs->dev,
534 "buffer overflow (%u bytes lost)\n",
535 bytesleft);
536 break;
538 if (n > bytesleft)
539 n = bytesleft;
540 memcpy(inbuf->data + tail, src, n);
541 bytesleft -= n;
542 tail = (tail + n) % RBUFSIZE;
543 src += n;
545 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
546 inbuf->tail = tail;
547 return numbytes != bytesleft;
549 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
551 /* Initialize the b-channel structure */
552 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
553 struct cardstate *cs, int channel)
555 int i;
557 bcs->tx_skb = NULL; //FIXME -> hw part
559 skb_queue_head_init(&bcs->squeue);
561 bcs->corrupted = 0;
562 bcs->trans_down = 0;
563 bcs->trans_up = 0;
565 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
566 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
568 bcs->rcvbytes = 0;
570 #ifdef CONFIG_GIGASET_DEBUG
571 bcs->emptycount = 0;
572 #endif
574 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
575 bcs->fcs = PPP_INITFCS;
576 bcs->inputstate = 0;
577 if (cs->ignoreframes) {
578 bcs->inputstate |= INS_skip_frame;
579 bcs->skb = NULL;
580 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
581 skb_reserve(bcs->skb, HW_HDR_LEN);
582 else {
583 warn("could not allocate skb");
584 bcs->inputstate |= INS_skip_frame;
587 bcs->channel = channel;
588 bcs->cs = cs;
590 bcs->chstate = 0;
591 bcs->use_count = 1;
592 bcs->busy = 0;
593 bcs->ignore = cs->ignoreframes;
595 for (i = 0; i < AT_NUM; ++i)
596 bcs->commands[i] = NULL;
598 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
599 if (cs->ops->initbcshw(bcs))
600 return bcs;
602 gig_dbg(DEBUG_INIT, " failed");
604 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
605 if (bcs->skb)
606 dev_kfree_skb(bcs->skb);
608 return NULL;
611 /* gigaset_initcs
612 * Allocate and initialize cardstate structure for Gigaset driver
613 * Calls hardware dependent gigaset_initcshw() function
614 * Calls B channel initialization function gigaset_initbcs() for each B channel
615 * parameters:
616 * drv hardware driver the device belongs to
617 * channels number of B channels supported by device
618 * onechannel !=0: B channel data and AT commands share one
619 * communication channel
620 * ==0: B channels have separate communication channels
621 * ignoreframes number of frames to ignore after setting up B channel
622 * cidmode !=0: start in CallID mode
623 * modulename name of driver module (used for I4L registration)
624 * return value:
625 * pointer to cardstate structure
627 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
628 int onechannel, int ignoreframes,
629 int cidmode, const char *modulename)
631 struct cardstate *cs = NULL;
632 unsigned long flags;
633 int i;
635 gig_dbg(DEBUG_INIT, "allocating cs");
636 if (!(cs = alloc_cs(drv))) {
637 err("maximum number of devices exceeded");
638 return NULL;
641 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
642 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
643 if (!cs->bcs) {
644 err("out of memory");
645 goto error;
647 gig_dbg(DEBUG_INIT, "allocating inbuf");
648 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
649 if (!cs->inbuf) {
650 err("out of memory");
651 goto error;
654 cs->cs_init = 0;
655 cs->channels = channels;
656 cs->onechannel = onechannel;
657 cs->ignoreframes = ignoreframes;
658 INIT_LIST_HEAD(&cs->temp_at_states);
659 cs->running = 0;
660 init_timer(&cs->timer); /* clear next & prev */
661 spin_lock_init(&cs->ev_lock);
662 cs->ev_tail = 0;
663 cs->ev_head = 0;
665 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
666 (unsigned long) cs);
667 cs->commands_pending = 0;
668 cs->cur_at_seq = 0;
669 cs->gotfwver = -1;
670 cs->open_count = 0;
671 cs->dev = NULL;
672 cs->tty = NULL;
673 cs->tty_dev = NULL;
674 cs->cidmode = cidmode != 0;
676 //if(onechannel) { //FIXME
677 cs->tabnocid = gigaset_tab_nocid_m10x;
678 cs->tabcid = gigaset_tab_cid_m10x;
679 //} else {
680 // cs->tabnocid = gigaset_tab_nocid;
681 // cs->tabcid = gigaset_tab_cid;
684 init_waitqueue_head(&cs->waitqueue);
685 cs->waiting = 0;
687 cs->mode = M_UNKNOWN;
688 cs->mstate = MS_UNINITIALIZED;
690 for (i = 0; i < channels; ++i) {
691 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
692 if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
693 err("could not allocate channel %d data", i);
694 goto error;
698 ++cs->cs_init;
700 gig_dbg(DEBUG_INIT, "setting up at_state");
701 spin_lock_init(&cs->lock);
702 gigaset_at_init(&cs->at_state, NULL, cs, 0);
703 cs->dle = 0;
704 cs->cbytes = 0;
706 gig_dbg(DEBUG_INIT, "setting up inbuf");
707 if (onechannel) { //FIXME distinction necessary?
708 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
709 } else
710 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
712 cs->connected = 0;
713 cs->isdn_up = 0;
715 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
716 cs->cmdbuf = cs->lastcmdbuf = NULL;
717 spin_lock_init(&cs->cmdlock);
718 cs->curlen = 0;
719 cs->cmdbytes = 0;
721 gig_dbg(DEBUG_INIT, "setting up iif");
722 if (!gigaset_register_to_LL(cs, modulename)) {
723 err("register_isdn failed");
724 goto error;
727 make_valid(cs, VALID_ID);
728 ++cs->cs_init;
729 gig_dbg(DEBUG_INIT, "setting up hw");
730 if (!cs->ops->initcshw(cs)) {
731 err("could not allocate device specific data");
732 goto error;
735 ++cs->cs_init;
737 /* set up character device */
738 gigaset_if_init(cs);
740 /* set up device sysfs */
741 gigaset_init_dev_sysfs(cs);
743 spin_lock_irqsave(&cs->lock, flags);
744 cs->running = 1;
745 spin_unlock_irqrestore(&cs->lock, flags);
746 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
747 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
748 /* FIXME: can jiffies increase too much until the timer is added?
749 * Same problem(?) with mod_timer() in timer_tick(). */
750 add_timer(&cs->timer);
752 gig_dbg(DEBUG_INIT, "cs initialized");
753 return cs;
755 error:
756 gig_dbg(DEBUG_INIT, "failed");
757 gigaset_freecs(cs);
758 return NULL;
760 EXPORT_SYMBOL_GPL(gigaset_initcs);
762 /* ReInitialize the b-channel structure on hangup */
763 void gigaset_bcs_reinit(struct bc_state *bcs)
765 struct sk_buff *skb;
766 struct cardstate *cs = bcs->cs;
767 unsigned long flags;
769 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
770 dev_kfree_skb(skb);
772 spin_lock_irqsave(&cs->lock, flags);
773 clear_at_state(&bcs->at_state);
774 bcs->at_state.ConState = 0;
775 bcs->at_state.timer_active = 0;
776 bcs->at_state.timer_expires = 0;
777 bcs->at_state.cid = -1; /* No CID defined */
778 spin_unlock_irqrestore(&cs->lock, flags);
780 bcs->inputstate = 0;
782 #ifdef CONFIG_GIGASET_DEBUG
783 bcs->emptycount = 0;
784 #endif
786 bcs->fcs = PPP_INITFCS;
787 bcs->chstate = 0;
789 bcs->ignore = cs->ignoreframes;
790 if (bcs->ignore)
791 bcs->inputstate |= INS_skip_frame;
794 cs->ops->reinitbcshw(bcs);
797 static void cleanup_cs(struct cardstate *cs)
799 struct cmdbuf_t *cb, *tcb;
800 int i;
801 unsigned long flags;
803 spin_lock_irqsave(&cs->lock, flags);
805 cs->mode = M_UNKNOWN;
806 cs->mstate = MS_UNINITIALIZED;
808 clear_at_state(&cs->at_state);
809 dealloc_at_states(cs);
810 free_strings(&cs->at_state);
811 gigaset_at_init(&cs->at_state, NULL, cs, 0);
813 kfree(cs->inbuf->rcvbuf);
814 cs->inbuf->rcvbuf = NULL;
815 cs->inbuf->inputstate = INS_command;
816 cs->inbuf->head = 0;
817 cs->inbuf->tail = 0;
819 cb = cs->cmdbuf;
820 while (cb) {
821 tcb = cb;
822 cb = cb->next;
823 kfree(tcb);
825 cs->cmdbuf = cs->lastcmdbuf = NULL;
826 cs->curlen = 0;
827 cs->cmdbytes = 0;
828 cs->gotfwver = -1;
829 cs->dle = 0;
830 cs->cur_at_seq = 0;
831 cs->commands_pending = 0;
832 cs->cbytes = 0;
834 spin_unlock_irqrestore(&cs->lock, flags);
836 for (i = 0; i < cs->channels; ++i) {
837 gigaset_freebcs(cs->bcs + i);
838 if (!gigaset_initbcs(cs->bcs + i, cs, i))
839 break; //FIXME error handling
842 if (cs->waiting) {
843 cs->cmd_result = -ENODEV;
844 cs->waiting = 0;
845 wake_up_interruptible(&cs->waitqueue);
850 int gigaset_start(struct cardstate *cs)
852 unsigned long flags;
854 if (mutex_lock_interruptible(&cs->mutex))
855 return 0;
857 spin_lock_irqsave(&cs->lock, flags);
858 cs->connected = 1;
859 spin_unlock_irqrestore(&cs->lock, flags);
861 if (cs->mstate != MS_LOCKED) {
862 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
863 cs->ops->baud_rate(cs, B115200);
864 cs->ops->set_line_ctrl(cs, CS8);
865 cs->control_state = TIOCM_DTR|TIOCM_RTS;
866 } else {
867 //FIXME use some saved values?
870 cs->waiting = 1;
872 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
873 cs->waiting = 0;
874 //FIXME what should we do?
875 goto error;
878 gig_dbg(DEBUG_CMD, "scheduling START");
879 gigaset_schedule_event(cs);
881 wait_event(cs->waitqueue, !cs->waiting);
883 mutex_unlock(&cs->mutex);
884 return 1;
886 error:
887 mutex_unlock(&cs->mutex);
888 return 0;
890 EXPORT_SYMBOL_GPL(gigaset_start);
892 /* gigaset_shutdown
893 * check if a device is associated to the cardstate structure and stop it
894 * return value: 0 if ok, -1 if no device was associated
896 int gigaset_shutdown(struct cardstate *cs)
898 mutex_lock(&cs->mutex);
900 if (!(cs->flags & VALID_MINOR)) {
901 mutex_unlock(&cs->mutex);
902 return -1;
905 cs->waiting = 1;
907 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
908 //FIXME what should we do?
909 goto exit;
912 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
913 gigaset_schedule_event(cs);
915 wait_event(cs->waitqueue, !cs->waiting);
917 cleanup_cs(cs);
919 exit:
920 mutex_unlock(&cs->mutex);
921 return 0;
923 EXPORT_SYMBOL_GPL(gigaset_shutdown);
925 void gigaset_stop(struct cardstate *cs)
927 mutex_lock(&cs->mutex);
929 cs->waiting = 1;
931 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
932 //FIXME what should we do?
933 goto exit;
936 gig_dbg(DEBUG_CMD, "scheduling STOP");
937 gigaset_schedule_event(cs);
939 wait_event(cs->waitqueue, !cs->waiting);
941 cleanup_cs(cs);
943 exit:
944 mutex_unlock(&cs->mutex);
946 EXPORT_SYMBOL_GPL(gigaset_stop);
948 static LIST_HEAD(drivers);
949 static DEFINE_SPINLOCK(driver_lock);
951 struct cardstate *gigaset_get_cs_by_id(int id)
953 unsigned long flags;
954 struct cardstate *ret = NULL;
955 struct cardstate *cs;
956 struct gigaset_driver *drv;
957 unsigned i;
959 spin_lock_irqsave(&driver_lock, flags);
960 list_for_each_entry(drv, &drivers, list) {
961 spin_lock(&drv->lock);
962 for (i = 0; i < drv->minors; ++i) {
963 cs = drv->cs + i;
964 if ((cs->flags & VALID_ID) && cs->myid == id) {
965 ret = cs;
966 break;
969 spin_unlock(&drv->lock);
970 if (ret)
971 break;
973 spin_unlock_irqrestore(&driver_lock, flags);
974 return ret;
977 void gigaset_debugdrivers(void)
979 unsigned long flags;
980 static struct cardstate *cs;
981 struct gigaset_driver *drv;
982 unsigned i;
984 spin_lock_irqsave(&driver_lock, flags);
985 list_for_each_entry(drv, &drivers, list) {
986 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
987 spin_lock(&drv->lock);
988 for (i = 0; i < drv->minors; ++i) {
989 gig_dbg(DEBUG_DRIVER, " index %u", i);
990 cs = drv->cs + i;
991 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
992 gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
993 gig_dbg(DEBUG_DRIVER, " minor_index %u",
994 cs->minor_index);
995 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
996 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
998 spin_unlock(&drv->lock);
1000 spin_unlock_irqrestore(&driver_lock, flags);
1003 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1005 unsigned long flags;
1006 struct cardstate *ret = NULL;
1007 struct gigaset_driver *drv;
1008 unsigned index;
1010 spin_lock_irqsave(&driver_lock, flags);
1011 list_for_each_entry(drv, &drivers, list) {
1012 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1013 continue;
1014 index = minor - drv->minor;
1015 spin_lock(&drv->lock);
1016 if (drv->cs[index].flags & VALID_MINOR)
1017 ret = drv->cs + index;
1018 spin_unlock(&drv->lock);
1019 if (ret)
1020 break;
1022 spin_unlock_irqrestore(&driver_lock, flags);
1023 return ret;
1026 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1028 if (tty->index < 0 || tty->index >= tty->driver->num)
1029 return NULL;
1030 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1033 void gigaset_freedriver(struct gigaset_driver *drv)
1035 unsigned long flags;
1037 spin_lock_irqsave(&driver_lock, flags);
1038 list_del(&drv->list);
1039 spin_unlock_irqrestore(&driver_lock, flags);
1041 gigaset_if_freedriver(drv);
1043 kfree(drv->cs);
1044 kfree(drv);
1046 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1048 /* gigaset_initdriver
1049 * Allocate and initialize gigaset_driver structure. Initialize interface.
1050 * parameters:
1051 * minor First minor number
1052 * minors Number of minors this driver can handle
1053 * procname Name of the driver
1054 * devname Name of the device files (prefix without minor number)
1055 * return value:
1056 * Pointer to the gigaset_driver structure on success, NULL on failure.
1058 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1059 const char *procname,
1060 const char *devname,
1061 const struct gigaset_ops *ops,
1062 struct module *owner)
1064 struct gigaset_driver *drv;
1065 unsigned long flags;
1066 unsigned i;
1068 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1069 if (!drv)
1070 return NULL;
1072 drv->have_tty = 0;
1073 drv->minor = minor;
1074 drv->minors = minors;
1075 spin_lock_init(&drv->lock);
1076 drv->blocked = 0;
1077 drv->ops = ops;
1078 drv->owner = owner;
1079 INIT_LIST_HEAD(&drv->list);
1081 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1082 if (!drv->cs)
1083 goto error;
1085 for (i = 0; i < minors; ++i) {
1086 drv->cs[i].flags = 0;
1087 drv->cs[i].driver = drv;
1088 drv->cs[i].ops = drv->ops;
1089 drv->cs[i].minor_index = i;
1090 mutex_init(&drv->cs[i].mutex);
1093 gigaset_if_initdriver(drv, procname, devname);
1095 spin_lock_irqsave(&driver_lock, flags);
1096 list_add(&drv->list, &drivers);
1097 spin_unlock_irqrestore(&driver_lock, flags);
1099 return drv;
1101 error:
1102 kfree(drv->cs);
1103 kfree(drv);
1104 return NULL;
1106 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1108 void gigaset_blockdriver(struct gigaset_driver *drv)
1110 drv->blocked = 1;
1112 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1114 static int __init gigaset_init_module(void)
1116 /* in accordance with the principle of least astonishment,
1117 * setting the 'debug' parameter to 1 activates a sensible
1118 * set of default debug levels
1120 if (gigaset_debuglevel == 1)
1121 gigaset_debuglevel = DEBUG_DEFAULT;
1123 info(DRIVER_AUTHOR);
1124 info(DRIVER_DESC);
1125 return 0;
1128 static void __exit gigaset_exit_module(void)
1132 module_init(gigaset_init_module);
1133 module_exit(gigaset_exit_module);
1135 MODULE_AUTHOR(DRIVER_AUTHOR);
1136 MODULE_DESCRIPTION(DRIVER_DESC);
1138 MODULE_LICENSE("GPL");