2 * Stuff used by all variants of the driver
4 * Copyright (c) 2001 by Stefan Eilers <Eilers.Stefan@epost.de>,
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 * =====================================================================
15 * =====================================================================
16 * Version: $Id: common.c,v 1.104.4.22 2006/02/04 18:28:16 hjlipp Exp $
17 * =====================================================================
21 #include <linux/ctype.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
25 /* Version Information */
26 #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers <Eilers.Stefan@epost.de>"
27 #define DRIVER_DESC "Driver for Gigaset 307x"
29 /* Module parameters */
30 int gigaset_debuglevel = DEBUG_DEFAULT;
31 EXPORT_SYMBOL_GPL(gigaset_debuglevel);
32 module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
33 MODULE_PARM_DESC(debug, "debug level");
35 /*======================================================================
36 Prototypes of internal functions
39 //static void gigaset_process_response(int resp_code, int parameter,
40 // struct at_state_t *at_state,
41 // unsigned char ** pstring);
42 static struct cardstate *alloc_cs(struct gigaset_driver *drv);
43 static void free_cs(struct cardstate *cs);
44 static void make_valid(struct cardstate *cs, unsigned mask);
45 static void make_invalid(struct cardstate *cs, unsigned mask);
47 #define VALID_MINOR 0x01
51 /* bitwise byte inversion table */
52 __u8 gigaset_invtab[256] = {
53 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
54 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
55 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
56 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
57 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
58 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
59 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
60 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
61 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
62 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
63 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
64 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
65 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
66 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
67 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
68 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
69 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
70 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
71 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
72 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
73 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
74 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
75 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
76 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
77 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
78 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
79 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
80 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
81 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
82 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
83 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
84 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
86 EXPORT_SYMBOL_GPL(gigaset_invtab);
88 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
89 size_t len, const unsigned char *buf, int from_user)
91 unsigned char outbuf[80];
92 unsigned char inbuf[80 - 1];
94 const unsigned char *in;
95 size_t space = sizeof outbuf - 1;
96 unsigned char *out = outbuf;
102 numin = len < sizeof inbuf ? len : sizeof inbuf;
104 if (copy_from_user(inbuf, (const unsigned char __user *) buf, numin)) {
105 strncpy(inbuf, "<FAULT>", sizeof inbuf);
106 numin = sizeof "<FAULT>" - 1;
110 for (; numin && space; --numin, ++in) {
124 dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
126 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
128 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
132 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
133 cs->control_state = flags;
138 set_current_state(TASK_INTERRUPTIBLE);
139 schedule_timeout(delay * HZ / 1000);
145 int gigaset_enterconfigmode(struct cardstate *cs)
149 if (!atomic_read(&cs->connected)) {
150 err("not connected!");
154 cs->control_state = TIOCM_RTS; //FIXME
156 r = setflags(cs, TIOCM_DTR, 200);
159 r = setflags(cs, 0, 200);
162 for (i = 0; i < 5; ++i) {
163 r = setflags(cs, TIOCM_RTS, 100);
166 r = setflags(cs, 0, 100);
170 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
177 err("error %d on setuartbits!\n", -r);
178 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
179 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
184 static int test_timeout(struct at_state_t *at_state)
186 if (!at_state->timer_expires)
189 if (--at_state->timer_expires) {
190 dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
191 at_state, at_state->timer_expires);
195 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
196 atomic_read(&at_state->timer_index), NULL)) {
197 //FIXME what should we do?
203 static void timer_tick(unsigned long data)
205 struct cardstate *cs = (struct cardstate *) data;
208 struct at_state_t *at_state;
211 spin_lock_irqsave(&cs->lock, flags);
213 for (channel = 0; channel < cs->channels; ++channel)
214 if (test_timeout(&cs->bcs[channel].at_state))
217 if (test_timeout(&cs->at_state))
220 list_for_each_entry(at_state, &cs->temp_at_states, list)
221 if (test_timeout(at_state))
224 if (atomic_read(&cs->running)) {
225 mod_timer(&cs->timer, jiffies + GIG_TICK);
227 dbg(DEBUG_CMD, "scheduling timeout");
228 tasklet_schedule(&cs->event_tasklet);
232 spin_unlock_irqrestore(&cs->lock, flags);
235 int gigaset_get_channel(struct bc_state *bcs)
239 spin_lock_irqsave(&bcs->cs->lock, flags);
240 if (bcs->use_count) {
241 dbg(DEBUG_ANY, "could not allocate channel %d", bcs->channel);
242 spin_unlock_irqrestore(&bcs->cs->lock, flags);
247 dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
248 spin_unlock_irqrestore(&bcs->cs->lock, flags);
252 void gigaset_free_channel(struct bc_state *bcs)
256 spin_lock_irqsave(&bcs->cs->lock, flags);
258 dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
259 spin_unlock_irqrestore(&bcs->cs->lock, flags);
264 dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
265 spin_unlock_irqrestore(&bcs->cs->lock, flags);
268 int gigaset_get_channels(struct cardstate *cs)
273 spin_lock_irqsave(&cs->lock, flags);
274 for (i = 0; i < cs->channels; ++i)
275 if (cs->bcs[i].use_count) {
276 spin_unlock_irqrestore(&cs->lock, flags);
277 dbg(DEBUG_ANY, "could not allocated all channels");
280 for (i = 0; i < cs->channels; ++i)
281 ++cs->bcs[i].use_count;
282 spin_unlock_irqrestore(&cs->lock, flags);
284 dbg(DEBUG_ANY, "allocated all channels");
289 void gigaset_free_channels(struct cardstate *cs)
294 dbg(DEBUG_ANY, "unblocking all channels");
295 spin_lock_irqsave(&cs->lock, flags);
296 for (i = 0; i < cs->channels; ++i)
297 --cs->bcs[i].use_count;
298 spin_unlock_irqrestore(&cs->lock, flags);
301 void gigaset_block_channels(struct cardstate *cs)
306 dbg(DEBUG_ANY, "blocking all channels");
307 spin_lock_irqsave(&cs->lock, flags);
308 for (i = 0; i < cs->channels; ++i)
309 ++cs->bcs[i].use_count;
310 spin_unlock_irqrestore(&cs->lock, flags);
313 static void clear_events(struct cardstate *cs)
318 /* no locking needed (no reader/writer allowed) */
320 head = atomic_read(&cs->ev_head);
321 tail = atomic_read(&cs->ev_tail);
323 while (tail != head) {
324 ev = cs->events + head;
327 head = (head + 1) % MAX_EVENTS;
330 atomic_set(&cs->ev_head, tail);
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)
339 struct event_t *event = NULL;
341 spin_lock_irqsave(&cs->ev_lock, flags);
343 tail = atomic_read(&cs->ev_tail);
344 next = (tail + 1) % MAX_EVENTS;
345 if (unlikely(next == atomic_read(&cs->ev_head)))
346 err("event queue full");
348 event = cs->events + tail;
350 event->at_state = at_state;
354 event->parameter = parameter;
355 atomic_set(&cs->ev_tail, next);
358 spin_unlock_irqrestore(&cs->ev_lock, flags);
362 EXPORT_SYMBOL_GPL(gigaset_add_event);
364 static void free_strings(struct at_state_t *at_state)
368 for (i = 0; i < STR_NUM; ++i) {
369 kfree(at_state->str_var[i]);
370 at_state->str_var[i] = NULL;
374 static void clear_at_state(struct at_state_t *at_state)
376 free_strings(at_state);
379 static void dealloc_at_states(struct cardstate *cs)
381 struct at_state_t *cur, *next;
383 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
384 list_del(&cur->list);
390 static void gigaset_freebcs(struct bc_state *bcs)
394 dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
395 if (!bcs->cs->ops->freebcshw(bcs)) {
396 dbg(DEBUG_INIT, "failed");
399 dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
400 clear_at_state(&bcs->at_state);
401 dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
404 dev_kfree_skb(bcs->skb);
405 for (i = 0; i < AT_NUM; ++i) {
406 kfree(bcs->commands[i]);
407 bcs->commands[i] = NULL;
411 void gigaset_freecs(struct cardstate *cs)
426 spin_lock_irqsave(&cs->lock, flags);
427 atomic_set(&cs->running, 0);
428 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are not rescheduled below */
430 tasklet_kill(&cs->event_tasklet);
431 del_timer_sync(&cs->timer);
433 switch (cs->cs_init) {
437 dbg(DEBUG_INIT, "clearing hw");
438 cs->ops->freecshw(cs);
443 case 2: /* error in initcshw */
444 /* Deregister from LL */
445 make_invalid(cs, VALID_ID);
446 dbg(DEBUG_INIT, "clearing iif");
447 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
450 case 1: /* error when regestering to LL */
451 dbg(DEBUG_INIT, "clearing at_state");
452 clear_at_state(&cs->at_state);
453 dealloc_at_states(cs);
456 case 0: /* error in one call to initbcs */
457 for (i = 0; i < cs->channels; ++i) {
458 dbg(DEBUG_INIT, "clearing bcs[%d]", i);
459 gigaset_freebcs(cs->bcs + i);
463 dbg(DEBUG_INIT, "freeing inbuf");
466 f_bcs: dbg(DEBUG_INIT, "freeing bcs[]");
468 f_cs: dbg(DEBUG_INIT, "freeing cs");
472 EXPORT_SYMBOL_GPL(gigaset_freecs);
474 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
475 struct cardstate *cs, int cid)
479 INIT_LIST_HEAD(&at_state->list);
480 at_state->waiting = 0;
481 at_state->getstring = 0;
482 at_state->pending_commands = 0;
483 at_state->timer_expires = 0;
484 at_state->timer_active = 0;
485 atomic_set(&at_state->timer_index, 0);
486 atomic_set(&at_state->seq_index, 0);
487 at_state->ConState = 0;
488 for (i = 0; i < STR_NUM; ++i)
489 at_state->str_var[i] = NULL;
490 at_state->int_var[VAR_ZDLE] = 0;
491 at_state->int_var[VAR_ZCTP] = -1;
492 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
497 at_state->replystruct = cs->tabnocid;
499 at_state->replystruct = cs->tabcid;
503 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
504 struct cardstate *cs, int inputstate)
505 /* inbuf->read must be allocated before! */
507 atomic_set(&inbuf->head, 0);
508 atomic_set(&inbuf->tail, 0);
510 inbuf->bcs = bcs; /*base driver: NULL*/
511 inbuf->rcvbuf = NULL; //FIXME
512 inbuf->inputstate = inputstate;
515 /* Initialize the b-channel structure */
516 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
517 struct cardstate *cs, int channel)
521 bcs->tx_skb = NULL; //FIXME -> hw part
523 skb_queue_head_init(&bcs->squeue);
529 dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
530 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
534 #ifdef CONFIG_GIGASET_DEBUG
538 dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
539 bcs->fcs = PPP_INITFCS;
541 if (cs->ignoreframes) {
542 bcs->inputstate |= INS_skip_frame;
544 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
545 skb_reserve(bcs->skb, HW_HDR_LEN);
547 warn("could not allocate skb");
548 bcs->inputstate |= INS_skip_frame;
551 bcs->channel = channel;
557 bcs->ignore = cs->ignoreframes;
559 for (i = 0; i < AT_NUM; ++i)
560 bcs->commands[i] = NULL;
562 dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
563 if (cs->ops->initbcshw(bcs))
567 dbg(DEBUG_INIT, " failed");
569 dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
571 dev_kfree_skb(bcs->skb);
577 * Allocate and initialize cardstate structure for Gigaset driver
578 * Calls hardware dependent gigaset_initcshw() function
579 * Calls B channel initialization function gigaset_initbcs() for each B channel
581 * drv hardware driver the device belongs to
582 * channels number of B channels supported by device
583 * onechannel !=0: B channel data and AT commands share one communication channel
584 * ==0: B channels have separate communication channels
585 * ignoreframes number of frames to ignore after setting up B channel
586 * cidmode !=0: start in CallID mode
587 * modulename name of driver module (used for I4L registration)
589 * pointer to cardstate structure
591 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
592 int onechannel, int ignoreframes,
593 int cidmode, const char *modulename)
595 struct cardstate *cs = NULL;
598 dbg(DEBUG_INIT, "allocating cs");
602 dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
603 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
606 dbg(DEBUG_INIT, "allocating inbuf");
607 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
612 cs->channels = channels;
613 cs->onechannel = onechannel;
614 cs->ignoreframes = ignoreframes;
615 INIT_LIST_HEAD(&cs->temp_at_states);
616 atomic_set(&cs->running, 0);
617 init_timer(&cs->timer); /* clear next & prev */
618 spin_lock_init(&cs->ev_lock);
619 atomic_set(&cs->ev_tail, 0);
620 atomic_set(&cs->ev_head, 0);
621 init_MUTEX_LOCKED(&cs->sem);
622 tasklet_init(&cs->event_tasklet, &gigaset_handle_event, (unsigned long) cs);
623 atomic_set(&cs->commands_pending, 0);
628 atomic_set(&cs->cidmode, cidmode != 0);
630 //if(onechannel) { //FIXME
631 cs->tabnocid = gigaset_tab_nocid_m10x;
632 cs->tabcid = gigaset_tab_cid_m10x;
634 // cs->tabnocid = gigaset_tab_nocid;
635 // cs->tabcid = gigaset_tab_cid;
638 init_waitqueue_head(&cs->waitqueue);
641 atomic_set(&cs->mode, M_UNKNOWN);
642 atomic_set(&cs->mstate, MS_UNINITIALIZED);
644 for (i = 0; i < channels; ++i) {
645 dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
646 if (!gigaset_initbcs(cs->bcs + i, cs, i))
652 dbg(DEBUG_INIT, "setting up at_state");
653 spin_lock_init(&cs->lock);
654 gigaset_at_init(&cs->at_state, NULL, cs, 0);
658 dbg(DEBUG_INIT, "setting up inbuf");
659 if (onechannel) { //FIXME distinction necessary?
660 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
662 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
664 atomic_set(&cs->connected, 0);
666 dbg(DEBUG_INIT, "setting up cmdbuf");
667 cs->cmdbuf = cs->lastcmdbuf = NULL;
668 spin_lock_init(&cs->cmdlock);
673 * Tell the ISDN4Linux subsystem (the LL) that
674 * a driver for a USB-Device is available !
675 * If this is done, "isdnctrl" is able to bind a device for this driver even
676 * if no physical usb-device is currently connected.
677 * But this device will just be accessable if a physical USB device is connected
678 * (via "gigaset_probe") .
680 dbg(DEBUG_INIT, "setting up iif");
681 if (!gigaset_register_to_LL(cs, modulename)) {
682 err("register_isdn=>error");
686 make_valid(cs, VALID_ID);
688 dbg(DEBUG_INIT, "setting up hw");
689 if (!cs->ops->initcshw(cs))
696 atomic_set(&cs->running, 1);
697 cs->timer.data = (unsigned long) cs;
698 cs->timer.function = timer_tick;
699 cs->timer.expires = jiffies + GIG_TICK;
700 /* FIXME: can jiffies increase too much until the timer is added?
701 * Same problem(?) with mod_timer() in timer_tick(). */
702 add_timer(&cs->timer);
704 dbg(DEBUG_INIT, "cs initialized!");
710 dbg(DEBUG_INIT, "failed");
714 EXPORT_SYMBOL_GPL(gigaset_initcs);
716 /* ReInitialize the b-channel structure */ /* e.g. called on hangup, disconnect */
717 void gigaset_bcs_reinit(struct bc_state *bcs)
720 struct cardstate *cs = bcs->cs;
723 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
726 spin_lock_irqsave(&cs->lock, flags); //FIXME
727 clear_at_state(&bcs->at_state);
728 bcs->at_state.ConState = 0;
729 bcs->at_state.timer_active = 0;
730 bcs->at_state.timer_expires = 0;
731 bcs->at_state.cid = -1; /* No CID defined */
732 spin_unlock_irqrestore(&cs->lock, flags);
736 #ifdef CONFIG_GIGASET_DEBUG
740 bcs->fcs = PPP_INITFCS;
743 bcs->ignore = cs->ignoreframes;
745 bcs->inputstate |= INS_skip_frame;
748 cs->ops->reinitbcshw(bcs);
751 static void cleanup_cs(struct cardstate *cs)
753 struct cmdbuf_t *cb, *tcb;
757 spin_lock_irqsave(&cs->lock, flags);
759 atomic_set(&cs->mode, M_UNKNOWN);
760 atomic_set(&cs->mstate, MS_UNINITIALIZED);
762 clear_at_state(&cs->at_state);
763 dealloc_at_states(cs);
764 free_strings(&cs->at_state);
765 gigaset_at_init(&cs->at_state, NULL, cs, 0);
767 kfree(cs->inbuf->rcvbuf);
768 cs->inbuf->rcvbuf = NULL;
769 cs->inbuf->inputstate = INS_command;
770 atomic_set(&cs->inbuf->head, 0);
771 atomic_set(&cs->inbuf->tail, 0);
779 cs->cmdbuf = cs->lastcmdbuf = NULL;
785 atomic_set(&cs->commands_pending, 0);
788 spin_unlock_irqrestore(&cs->lock, flags);
790 for (i = 0; i < cs->channels; ++i) {
791 gigaset_freebcs(cs->bcs + i);
792 if (!gigaset_initbcs(cs->bcs + i, cs, i))
793 break; //FIXME error handling
797 cs->cmd_result = -ENODEV;
799 wake_up_interruptible(&cs->waitqueue);
804 int gigaset_start(struct cardstate *cs)
806 if (down_interruptible(&cs->sem))
808 //info("USB device for Gigaset 307x now attached to Dev %d", ucs->minor);
810 atomic_set(&cs->connected, 1);
812 if (atomic_read(&cs->mstate) != MS_LOCKED) {
813 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
814 cs->ops->baud_rate(cs, B115200);
815 cs->ops->set_line_ctrl(cs, CS8);
816 cs->control_state = TIOCM_DTR|TIOCM_RTS;
818 //FIXME use some saved values?
823 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
825 //FIXME what should we do?
829 dbg(DEBUG_CMD, "scheduling START");
830 gigaset_schedule_event(cs);
832 wait_event(cs->waitqueue, !cs->waiting);
841 EXPORT_SYMBOL_GPL(gigaset_start);
843 void gigaset_shutdown(struct cardstate *cs)
849 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
850 //FIXME what should we do?
854 dbg(DEBUG_CMD, "scheduling SHUTDOWN");
855 gigaset_schedule_event(cs);
857 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
862 if (atomic_read(&cs->mstate) != MS_LOCKED) {
864 //gigaset_baud_rate(cs, B115200);
865 //gigaset_set_line_ctrl(cs, CS8);
866 //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
867 //cs->control_state = 0;
869 //FIXME use some saved values?
877 EXPORT_SYMBOL_GPL(gigaset_shutdown);
879 void gigaset_stop(struct cardstate *cs)
883 atomic_set(&cs->connected, 0);
887 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
888 //FIXME what should we do?
892 dbg(DEBUG_CMD, "scheduling STOP");
893 gigaset_schedule_event(cs);
895 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
900 /* Tell the LL that the device is not available .. */
901 gigaset_i4l_cmd(cs, ISDN_STAT_STOP); // FIXME move to event layer?
908 EXPORT_SYMBOL_GPL(gigaset_stop);
910 static LIST_HEAD(drivers);
911 static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
913 struct cardstate *gigaset_get_cs_by_id(int id)
916 static struct cardstate *ret = NULL;
917 static struct cardstate *cs;
918 struct gigaset_driver *drv;
921 spin_lock_irqsave(&driver_lock, flags);
922 list_for_each_entry(drv, &drivers, list) {
923 spin_lock(&drv->lock);
924 for (i = 0; i < drv->minors; ++i) {
925 if (drv->flags[i] & VALID_ID) {
933 spin_unlock(&drv->lock);
937 spin_unlock_irqrestore(&driver_lock, flags);
941 void gigaset_debugdrivers(void)
944 static struct cardstate *cs;
945 struct gigaset_driver *drv;
948 spin_lock_irqsave(&driver_lock, flags);
949 list_for_each_entry(drv, &drivers, list) {
950 dbg(DEBUG_DRIVER, "driver %p", drv);
951 spin_lock(&drv->lock);
952 for (i = 0; i < drv->minors; ++i) {
953 dbg(DEBUG_DRIVER, " index %u", i);
954 dbg(DEBUG_DRIVER, " flags 0x%02x", drv->flags[i]);
956 dbg(DEBUG_DRIVER, " cardstate %p", cs);
957 dbg(DEBUG_DRIVER, " minor_index %u", cs->minor_index);
958 dbg(DEBUG_DRIVER, " driver %p", cs->driver);
959 dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
961 spin_unlock(&drv->lock);
963 spin_unlock_irqrestore(&driver_lock, flags);
965 EXPORT_SYMBOL_GPL(gigaset_debugdrivers);
967 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
969 if (tty->index < 0 || tty->index >= tty->driver->num)
971 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
974 struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
977 static struct cardstate *ret = NULL;
978 struct gigaset_driver *drv;
981 spin_lock_irqsave(&driver_lock, flags);
982 list_for_each_entry(drv, &drivers, list) {
983 if (minor < drv->minor || minor >= drv->minor + drv->minors)
985 index = minor - drv->minor;
986 spin_lock(&drv->lock);
987 if (drv->flags[index] & VALID_MINOR)
988 ret = drv->cs + index;
989 spin_unlock(&drv->lock);
993 spin_unlock_irqrestore(&driver_lock, flags);
997 void gigaset_freedriver(struct gigaset_driver *drv)
1001 spin_lock_irqsave(&driver_lock, flags);
1002 list_del(&drv->list);
1003 spin_unlock_irqrestore(&driver_lock, flags);
1005 gigaset_if_freedriver(drv);
1006 module_put(drv->owner);
1012 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1014 /* gigaset_initdriver
1015 * Allocate and initialize gigaset_driver structure. Initialize interface.
1017 * minor First minor number
1018 * minors Number of minors this driver can handle
1019 * procname Name of the driver (e.g. for /proc/tty/drivers, path in /proc/driver)
1020 * devname Name of the device files (prefix without minor number)
1021 * devfsname Devfs name of the device files without %d
1023 * Pointer to the gigaset_driver structure on success, NULL on failure.
1025 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1026 const char *procname,
1027 const char *devname,
1028 const char *devfsname,
1029 const struct gigaset_ops *ops,
1030 struct module *owner)
1032 struct gigaset_driver *drv;
1033 unsigned long flags;
1036 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1039 if (!try_module_get(owner))
1045 drv->minors = minors;
1046 spin_lock_init(&drv->lock);
1050 INIT_LIST_HEAD(&drv->list);
1052 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1055 drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1059 for (i = 0; i < minors; ++i) {
1061 drv->cs[i].driver = drv;
1062 drv->cs[i].ops = drv->ops;
1063 drv->cs[i].minor_index = i;
1066 gigaset_if_initdriver(drv, procname, devname, devfsname);
1068 spin_lock_irqsave(&driver_lock, flags);
1069 list_add(&drv->list, &drivers);
1070 spin_unlock_irqrestore(&driver_lock, flags);
1081 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1083 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
1085 unsigned long flags;
1087 static struct cardstate *ret = NULL;
1089 spin_lock_irqsave(&drv->lock, flags);
1090 for (i = 0; i < drv->minors; ++i) {
1091 if (!(drv->flags[i] & VALID_MINOR)) {
1092 drv->flags[i] = VALID_MINOR;
1098 spin_unlock_irqrestore(&drv->lock, flags);
1102 static void free_cs(struct cardstate *cs)
1104 unsigned long flags;
1105 struct gigaset_driver *drv = cs->driver;
1106 spin_lock_irqsave(&drv->lock, flags);
1107 drv->flags[cs->minor_index] = 0;
1108 spin_unlock_irqrestore(&drv->lock, flags);
1111 static void make_valid(struct cardstate *cs, unsigned mask)
1113 unsigned long flags;
1114 struct gigaset_driver *drv = cs->driver;
1115 spin_lock_irqsave(&drv->lock, flags);
1116 drv->flags[cs->minor_index] |= mask;
1117 spin_unlock_irqrestore(&drv->lock, flags);
1120 static void make_invalid(struct cardstate *cs, unsigned mask)
1122 unsigned long flags;
1123 struct gigaset_driver *drv = cs->driver;
1124 spin_lock_irqsave(&drv->lock, flags);
1125 drv->flags[cs->minor_index] &= ~mask;
1126 spin_unlock_irqrestore(&drv->lock, flags);
1129 /* For drivers without fixed assignment device<->cardstate (usb) */
1130 struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1132 unsigned long flags;
1133 struct cardstate *cs = NULL;
1136 spin_lock_irqsave(&drv->lock, flags);
1139 for (i = 0; i < drv->minors; ++i) {
1140 if ((drv->flags[i] & VALID_MINOR) &&
1141 !(drv->flags[i] & ASSIGNED)) {
1142 drv->flags[i] |= ASSIGNED;
1148 spin_unlock_irqrestore(&drv->lock, flags);
1151 EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1153 void gigaset_unassign(struct cardstate *cs)
1155 unsigned long flags;
1156 unsigned *minor_flags;
1157 struct gigaset_driver *drv;
1162 spin_lock_irqsave(&drv->lock, flags);
1163 minor_flags = drv->flags + cs->minor_index;
1164 if (*minor_flags & VALID_MINOR)
1165 *minor_flags &= ~ASSIGNED;
1166 spin_unlock_irqrestore(&drv->lock, flags);
1168 EXPORT_SYMBOL_GPL(gigaset_unassign);
1170 void gigaset_blockdriver(struct gigaset_driver *drv)
1172 unsigned long flags;
1173 spin_lock_irqsave(&drv->lock, flags);
1175 spin_unlock_irqrestore(&drv->lock, flags);
1177 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1179 static int __init gigaset_init_module(void)
1181 /* in accordance with the principle of least astonishment,
1182 * setting the 'debug' parameter to 1 activates a sensible
1183 * set of default debug levels
1185 if (gigaset_debuglevel == 1)
1186 gigaset_debuglevel = DEBUG_DEFAULT;
1188 info(DRIVER_AUTHOR);
1193 static void __exit gigaset_exit_module(void)
1197 module_init(gigaset_init_module);
1198 module_exit(gigaset_exit_module);
1200 MODULE_AUTHOR(DRIVER_AUTHOR);
1201 MODULE_DESCRIPTION(DRIVER_DESC);
1203 MODULE_LICENSE("GPL");