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 * =====================================================================
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 <Eilers.Stefan@epost.de>"
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 /*======================================================================
32 Prototypes of internal functions
35 //static void gigaset_process_response(int resp_code, int parameter,
36 // struct at_state_t *at_state,
37 // unsigned char ** pstring);
38 static struct cardstate *alloc_cs(struct gigaset_driver *drv);
39 static void free_cs(struct cardstate *cs);
40 static void make_valid(struct cardstate *cs, unsigned mask);
41 static void make_invalid(struct cardstate *cs, unsigned mask);
43 #define VALID_MINOR 0x01
47 /* bitwise byte inversion table */
48 __u8 gigaset_invtab[256] = {
49 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
50 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
51 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
52 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
53 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
54 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
55 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
56 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
57 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
58 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
59 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
60 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
61 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
62 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
63 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
64 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
65 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
66 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
67 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
68 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
69 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
70 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
71 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
72 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
73 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
74 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
75 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
76 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
77 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
78 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
79 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
80 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
82 EXPORT_SYMBOL_GPL(gigaset_invtab);
84 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
85 size_t len, const unsigned char *buf, int from_user)
87 unsigned char outbuf[80];
88 unsigned char inbuf[80 - 1];
90 const unsigned char *in;
91 size_t space = sizeof outbuf - 1;
92 unsigned char *out = outbuf;
98 numin = len < sizeof inbuf ? len : sizeof inbuf;
100 if (copy_from_user(inbuf, (const unsigned char __user *) buf,
102 strncpy(inbuf, "<FAULT>", sizeof inbuf);
103 numin = sizeof "<FAULT>" - 1;
107 for (; numin && space; --numin, ++in) {
121 dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
123 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
125 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
129 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
130 cs->control_state = flags;
135 set_current_state(TASK_INTERRUPTIBLE);
136 schedule_timeout(delay * HZ / 1000);
142 int gigaset_enterconfigmode(struct cardstate *cs)
146 if (!atomic_read(&cs->connected)) {
147 err("not connected!");
151 cs->control_state = TIOCM_RTS; //FIXME
153 r = setflags(cs, TIOCM_DTR, 200);
156 r = setflags(cs, 0, 200);
159 for (i = 0; i < 5; ++i) {
160 r = setflags(cs, TIOCM_RTS, 100);
163 r = setflags(cs, 0, 100);
167 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
174 err("error %d on setuartbits!\n", -r);
175 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
176 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
181 static int test_timeout(struct at_state_t *at_state)
183 if (!at_state->timer_expires)
186 if (--at_state->timer_expires) {
187 dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
188 at_state, at_state->timer_expires);
192 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
193 atomic_read(&at_state->timer_index), NULL)) {
194 //FIXME what should we do?
200 static void timer_tick(unsigned long data)
202 struct cardstate *cs = (struct cardstate *) data;
205 struct at_state_t *at_state;
208 spin_lock_irqsave(&cs->lock, flags);
210 for (channel = 0; channel < cs->channels; ++channel)
211 if (test_timeout(&cs->bcs[channel].at_state))
214 if (test_timeout(&cs->at_state))
217 list_for_each_entry(at_state, &cs->temp_at_states, list)
218 if (test_timeout(at_state))
221 if (atomic_read(&cs->running)) {
222 mod_timer(&cs->timer, jiffies + GIG_TICK);
224 dbg(DEBUG_CMD, "scheduling timeout");
225 tasklet_schedule(&cs->event_tasklet);
229 spin_unlock_irqrestore(&cs->lock, flags);
232 int gigaset_get_channel(struct bc_state *bcs)
236 spin_lock_irqsave(&bcs->cs->lock, flags);
237 if (bcs->use_count) {
238 dbg(DEBUG_ANY, "could not allocate channel %d", bcs->channel);
239 spin_unlock_irqrestore(&bcs->cs->lock, flags);
244 dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
245 spin_unlock_irqrestore(&bcs->cs->lock, flags);
249 void gigaset_free_channel(struct bc_state *bcs)
253 spin_lock_irqsave(&bcs->cs->lock, flags);
255 dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
256 spin_unlock_irqrestore(&bcs->cs->lock, flags);
261 dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
262 spin_unlock_irqrestore(&bcs->cs->lock, flags);
265 int gigaset_get_channels(struct cardstate *cs)
270 spin_lock_irqsave(&cs->lock, flags);
271 for (i = 0; i < cs->channels; ++i)
272 if (cs->bcs[i].use_count) {
273 spin_unlock_irqrestore(&cs->lock, flags);
274 dbg(DEBUG_ANY, "could not allocated all channels");
277 for (i = 0; i < cs->channels; ++i)
278 ++cs->bcs[i].use_count;
279 spin_unlock_irqrestore(&cs->lock, flags);
281 dbg(DEBUG_ANY, "allocated all channels");
286 void gigaset_free_channels(struct cardstate *cs)
291 dbg(DEBUG_ANY, "unblocking all channels");
292 spin_lock_irqsave(&cs->lock, flags);
293 for (i = 0; i < cs->channels; ++i)
294 --cs->bcs[i].use_count;
295 spin_unlock_irqrestore(&cs->lock, flags);
298 void gigaset_block_channels(struct cardstate *cs)
303 dbg(DEBUG_ANY, "blocking all channels");
304 spin_lock_irqsave(&cs->lock, flags);
305 for (i = 0; i < cs->channels; ++i)
306 ++cs->bcs[i].use_count;
307 spin_unlock_irqrestore(&cs->lock, flags);
310 static void clear_events(struct cardstate *cs)
315 /* no locking needed (no reader/writer allowed) */
317 head = atomic_read(&cs->ev_head);
318 tail = atomic_read(&cs->ev_tail);
320 while (tail != head) {
321 ev = cs->events + head;
324 head = (head + 1) % MAX_EVENTS;
327 atomic_set(&cs->ev_head, tail);
330 struct event_t *gigaset_add_event(struct cardstate *cs,
331 struct at_state_t *at_state, int type,
332 void *ptr, int parameter, void *arg)
336 struct event_t *event = NULL;
338 spin_lock_irqsave(&cs->ev_lock, flags);
340 tail = atomic_read(&cs->ev_tail);
341 next = (tail + 1) % MAX_EVENTS;
342 if (unlikely(next == atomic_read(&cs->ev_head)))
343 err("event queue full");
345 event = cs->events + tail;
347 event->at_state = at_state;
351 event->parameter = parameter;
352 atomic_set(&cs->ev_tail, next);
355 spin_unlock_irqrestore(&cs->ev_lock, flags);
359 EXPORT_SYMBOL_GPL(gigaset_add_event);
361 static void free_strings(struct at_state_t *at_state)
365 for (i = 0; i < STR_NUM; ++i) {
366 kfree(at_state->str_var[i]);
367 at_state->str_var[i] = NULL;
371 static void clear_at_state(struct at_state_t *at_state)
373 free_strings(at_state);
376 static void dealloc_at_states(struct cardstate *cs)
378 struct at_state_t *cur, *next;
380 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
381 list_del(&cur->list);
387 static void gigaset_freebcs(struct bc_state *bcs)
391 dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
392 if (!bcs->cs->ops->freebcshw(bcs)) {
393 dbg(DEBUG_INIT, "failed");
396 dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
397 clear_at_state(&bcs->at_state);
398 dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
401 dev_kfree_skb(bcs->skb);
402 for (i = 0; i < AT_NUM; ++i) {
403 kfree(bcs->commands[i]);
404 bcs->commands[i] = NULL;
408 void gigaset_freecs(struct cardstate *cs)
423 spin_lock_irqsave(&cs->lock, flags);
424 atomic_set(&cs->running, 0);
425 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
426 not rescheduled below */
428 tasklet_kill(&cs->event_tasklet);
429 del_timer_sync(&cs->timer);
431 switch (cs->cs_init) {
435 dbg(DEBUG_INIT, "clearing hw");
436 cs->ops->freecshw(cs);
441 case 2: /* error in initcshw */
442 /* Deregister from LL */
443 make_invalid(cs, VALID_ID);
444 dbg(DEBUG_INIT, "clearing iif");
445 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
448 case 1: /* error when regestering to LL */
449 dbg(DEBUG_INIT, "clearing at_state");
450 clear_at_state(&cs->at_state);
451 dealloc_at_states(cs);
454 case 0: /* error in one call to initbcs */
455 for (i = 0; i < cs->channels; ++i) {
456 dbg(DEBUG_INIT, "clearing bcs[%d]", i);
457 gigaset_freebcs(cs->bcs + i);
461 dbg(DEBUG_INIT, "freeing inbuf");
464 f_bcs: dbg(DEBUG_INIT, "freeing bcs[]");
466 f_cs: dbg(DEBUG_INIT, "freeing cs");
470 EXPORT_SYMBOL_GPL(gigaset_freecs);
472 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
473 struct cardstate *cs, int cid)
477 INIT_LIST_HEAD(&at_state->list);
478 at_state->waiting = 0;
479 at_state->getstring = 0;
480 at_state->pending_commands = 0;
481 at_state->timer_expires = 0;
482 at_state->timer_active = 0;
483 atomic_set(&at_state->timer_index, 0);
484 atomic_set(&at_state->seq_index, 0);
485 at_state->ConState = 0;
486 for (i = 0; i < STR_NUM; ++i)
487 at_state->str_var[i] = NULL;
488 at_state->int_var[VAR_ZDLE] = 0;
489 at_state->int_var[VAR_ZCTP] = -1;
490 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
495 at_state->replystruct = cs->tabnocid;
497 at_state->replystruct = cs->tabcid;
501 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
502 struct cardstate *cs, int inputstate)
503 /* inbuf->read must be allocated before! */
505 atomic_set(&inbuf->head, 0);
506 atomic_set(&inbuf->tail, 0);
508 inbuf->bcs = bcs; /*base driver: NULL*/
509 inbuf->rcvbuf = NULL; //FIXME
510 inbuf->inputstate = inputstate;
513 /* Initialize the b-channel structure */
514 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
515 struct cardstate *cs, int channel)
519 bcs->tx_skb = NULL; //FIXME -> hw part
521 skb_queue_head_init(&bcs->squeue);
527 dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
528 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
532 #ifdef CONFIG_GIGASET_DEBUG
536 dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
537 bcs->fcs = PPP_INITFCS;
539 if (cs->ignoreframes) {
540 bcs->inputstate |= INS_skip_frame;
542 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
543 skb_reserve(bcs->skb, HW_HDR_LEN);
545 warn("could not allocate skb");
546 bcs->inputstate |= INS_skip_frame;
549 bcs->channel = channel;
555 bcs->ignore = cs->ignoreframes;
557 for (i = 0; i < AT_NUM; ++i)
558 bcs->commands[i] = NULL;
560 dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
561 if (cs->ops->initbcshw(bcs))
564 dbg(DEBUG_INIT, " failed");
566 dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
568 dev_kfree_skb(bcs->skb);
574 * Allocate and initialize cardstate structure for Gigaset driver
575 * Calls hardware dependent gigaset_initcshw() function
576 * Calls B channel initialization function gigaset_initbcs() for each B channel
578 * drv hardware driver the device belongs to
579 * channels number of B channels supported by device
580 * onechannel !=0: B channel data and AT commands share one
581 * communication channel
582 * ==0: B channels have separate communication channels
583 * ignoreframes number of frames to ignore after setting up B channel
584 * cidmode !=0: start in CallID mode
585 * modulename name of driver module (used for I4L registration)
587 * pointer to cardstate structure
589 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
590 int onechannel, int ignoreframes,
591 int cidmode, const char *modulename)
593 struct cardstate *cs = NULL;
596 dbg(DEBUG_INIT, "allocating cs");
600 dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
601 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
604 dbg(DEBUG_INIT, "allocating inbuf");
605 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
610 cs->channels = channels;
611 cs->onechannel = onechannel;
612 cs->ignoreframes = ignoreframes;
613 INIT_LIST_HEAD(&cs->temp_at_states);
614 atomic_set(&cs->running, 0);
615 init_timer(&cs->timer); /* clear next & prev */
616 spin_lock_init(&cs->ev_lock);
617 atomic_set(&cs->ev_tail, 0);
618 atomic_set(&cs->ev_head, 0);
619 init_MUTEX_LOCKED(&cs->sem);
620 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
622 atomic_set(&cs->commands_pending, 0);
627 atomic_set(&cs->cidmode, cidmode != 0);
629 //if(onechannel) { //FIXME
630 cs->tabnocid = gigaset_tab_nocid_m10x;
631 cs->tabcid = gigaset_tab_cid_m10x;
633 // cs->tabnocid = gigaset_tab_nocid;
634 // cs->tabcid = gigaset_tab_cid;
637 init_waitqueue_head(&cs->waitqueue);
640 atomic_set(&cs->mode, M_UNKNOWN);
641 atomic_set(&cs->mstate, MS_UNINITIALIZED);
643 for (i = 0; i < channels; ++i) {
644 dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
645 if (!gigaset_initbcs(cs->bcs + i, cs, i))
651 dbg(DEBUG_INIT, "setting up at_state");
652 spin_lock_init(&cs->lock);
653 gigaset_at_init(&cs->at_state, NULL, cs, 0);
657 dbg(DEBUG_INIT, "setting up inbuf");
658 if (onechannel) { //FIXME distinction necessary?
659 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
661 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
663 atomic_set(&cs->connected, 0);
665 dbg(DEBUG_INIT, "setting up cmdbuf");
666 cs->cmdbuf = cs->lastcmdbuf = NULL;
667 spin_lock_init(&cs->cmdlock);
671 dbg(DEBUG_INIT, "setting up iif");
672 if (!gigaset_register_to_LL(cs, modulename)) {
673 err("register_isdn=>error");
677 make_valid(cs, VALID_ID);
679 dbg(DEBUG_INIT, "setting up hw");
680 if (!cs->ops->initcshw(cs))
687 atomic_set(&cs->running, 1);
688 cs->timer.data = (unsigned long) cs;
689 cs->timer.function = timer_tick;
690 cs->timer.expires = jiffies + GIG_TICK;
691 /* FIXME: can jiffies increase too much until the timer is added?
692 * Same problem(?) with mod_timer() in timer_tick(). */
693 add_timer(&cs->timer);
695 dbg(DEBUG_INIT, "cs initialized!");
701 dbg(DEBUG_INIT, "failed");
705 EXPORT_SYMBOL_GPL(gigaset_initcs);
707 /* ReInitialize the b-channel structure */
708 /* e.g. called on hangup, disconnect */
709 void gigaset_bcs_reinit(struct bc_state *bcs)
712 struct cardstate *cs = bcs->cs;
715 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
718 spin_lock_irqsave(&cs->lock, flags);
719 clear_at_state(&bcs->at_state);
720 bcs->at_state.ConState = 0;
721 bcs->at_state.timer_active = 0;
722 bcs->at_state.timer_expires = 0;
723 bcs->at_state.cid = -1; /* No CID defined */
724 spin_unlock_irqrestore(&cs->lock, flags);
728 #ifdef CONFIG_GIGASET_DEBUG
732 bcs->fcs = PPP_INITFCS;
735 bcs->ignore = cs->ignoreframes;
737 bcs->inputstate |= INS_skip_frame;
740 cs->ops->reinitbcshw(bcs);
743 static void cleanup_cs(struct cardstate *cs)
745 struct cmdbuf_t *cb, *tcb;
749 spin_lock_irqsave(&cs->lock, flags);
751 atomic_set(&cs->mode, M_UNKNOWN);
752 atomic_set(&cs->mstate, MS_UNINITIALIZED);
754 clear_at_state(&cs->at_state);
755 dealloc_at_states(cs);
756 free_strings(&cs->at_state);
757 gigaset_at_init(&cs->at_state, NULL, cs, 0);
759 kfree(cs->inbuf->rcvbuf);
760 cs->inbuf->rcvbuf = NULL;
761 cs->inbuf->inputstate = INS_command;
762 atomic_set(&cs->inbuf->head, 0);
763 atomic_set(&cs->inbuf->tail, 0);
771 cs->cmdbuf = cs->lastcmdbuf = NULL;
777 atomic_set(&cs->commands_pending, 0);
780 spin_unlock_irqrestore(&cs->lock, flags);
782 for (i = 0; i < cs->channels; ++i) {
783 gigaset_freebcs(cs->bcs + i);
784 if (!gigaset_initbcs(cs->bcs + i, cs, i))
785 break; //FIXME error handling
789 cs->cmd_result = -ENODEV;
791 wake_up_interruptible(&cs->waitqueue);
796 int gigaset_start(struct cardstate *cs)
798 if (down_interruptible(&cs->sem))
801 atomic_set(&cs->connected, 1);
803 if (atomic_read(&cs->mstate) != MS_LOCKED) {
804 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
805 cs->ops->baud_rate(cs, B115200);
806 cs->ops->set_line_ctrl(cs, CS8);
807 cs->control_state = TIOCM_DTR|TIOCM_RTS;
809 //FIXME use some saved values?
814 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
816 //FIXME what should we do?
820 dbg(DEBUG_CMD, "scheduling START");
821 gigaset_schedule_event(cs);
823 wait_event(cs->waitqueue, !cs->waiting);
832 EXPORT_SYMBOL_GPL(gigaset_start);
834 void gigaset_shutdown(struct cardstate *cs)
840 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
841 //FIXME what should we do?
845 dbg(DEBUG_CMD, "scheduling SHUTDOWN");
846 gigaset_schedule_event(cs);
848 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
853 if (atomic_read(&cs->mstate) != MS_LOCKED) {
855 //gigaset_baud_rate(cs, B115200);
856 //gigaset_set_line_ctrl(cs, CS8);
857 //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
858 //cs->control_state = 0;
860 //FIXME use some saved values?
868 EXPORT_SYMBOL_GPL(gigaset_shutdown);
870 void gigaset_stop(struct cardstate *cs)
874 atomic_set(&cs->connected, 0);
878 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
879 //FIXME what should we do?
883 dbg(DEBUG_CMD, "scheduling STOP");
884 gigaset_schedule_event(cs);
886 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
891 /* Tell the LL that the device is not available .. */
892 gigaset_i4l_cmd(cs, ISDN_STAT_STOP); // FIXME move to event layer?
899 EXPORT_SYMBOL_GPL(gigaset_stop);
901 static LIST_HEAD(drivers);
902 static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
904 struct cardstate *gigaset_get_cs_by_id(int id)
907 static struct cardstate *ret = NULL;
908 static struct cardstate *cs;
909 struct gigaset_driver *drv;
912 spin_lock_irqsave(&driver_lock, flags);
913 list_for_each_entry(drv, &drivers, list) {
914 spin_lock(&drv->lock);
915 for (i = 0; i < drv->minors; ++i) {
916 if (drv->flags[i] & VALID_ID) {
924 spin_unlock(&drv->lock);
928 spin_unlock_irqrestore(&driver_lock, flags);
932 void gigaset_debugdrivers(void)
935 static struct cardstate *cs;
936 struct gigaset_driver *drv;
939 spin_lock_irqsave(&driver_lock, flags);
940 list_for_each_entry(drv, &drivers, list) {
941 dbg(DEBUG_DRIVER, "driver %p", drv);
942 spin_lock(&drv->lock);
943 for (i = 0; i < drv->minors; ++i) {
944 dbg(DEBUG_DRIVER, " index %u", i);
945 dbg(DEBUG_DRIVER, " flags 0x%02x", drv->flags[i]);
947 dbg(DEBUG_DRIVER, " cardstate %p", cs);
948 dbg(DEBUG_DRIVER, " minor_index %u",
950 dbg(DEBUG_DRIVER, " driver %p", cs->driver);
951 dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
953 spin_unlock(&drv->lock);
955 spin_unlock_irqrestore(&driver_lock, flags);
957 EXPORT_SYMBOL_GPL(gigaset_debugdrivers);
959 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
961 if (tty->index < 0 || tty->index >= tty->driver->num)
963 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
966 struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
969 static struct cardstate *ret = NULL;
970 struct gigaset_driver *drv;
973 spin_lock_irqsave(&driver_lock, flags);
974 list_for_each_entry(drv, &drivers, list) {
975 if (minor < drv->minor || minor >= drv->minor + drv->minors)
977 index = minor - drv->minor;
978 spin_lock(&drv->lock);
979 if (drv->flags[index] & VALID_MINOR)
980 ret = drv->cs + index;
981 spin_unlock(&drv->lock);
985 spin_unlock_irqrestore(&driver_lock, flags);
989 void gigaset_freedriver(struct gigaset_driver *drv)
993 spin_lock_irqsave(&driver_lock, flags);
994 list_del(&drv->list);
995 spin_unlock_irqrestore(&driver_lock, flags);
997 gigaset_if_freedriver(drv);
998 module_put(drv->owner);
1004 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1006 /* gigaset_initdriver
1007 * Allocate and initialize gigaset_driver structure. Initialize interface.
1009 * minor First minor number
1010 * minors Number of minors this driver can handle
1011 * procname Name of the driver
1012 * devname Name of the device files (prefix without minor number)
1013 * devfsname Devfs name of the device files without %d
1015 * Pointer to the gigaset_driver structure on success, NULL on failure.
1017 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1018 const char *procname,
1019 const char *devname,
1020 const char *devfsname,
1021 const struct gigaset_ops *ops,
1022 struct module *owner)
1024 struct gigaset_driver *drv;
1025 unsigned long flags;
1028 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1031 if (!try_module_get(owner))
1037 drv->minors = minors;
1038 spin_lock_init(&drv->lock);
1042 INIT_LIST_HEAD(&drv->list);
1044 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1047 drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1051 for (i = 0; i < minors; ++i) {
1053 drv->cs[i].driver = drv;
1054 drv->cs[i].ops = drv->ops;
1055 drv->cs[i].minor_index = i;
1058 gigaset_if_initdriver(drv, procname, devname, devfsname);
1060 spin_lock_irqsave(&driver_lock, flags);
1061 list_add(&drv->list, &drivers);
1062 spin_unlock_irqrestore(&driver_lock, flags);
1073 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1075 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
1077 unsigned long flags;
1079 static struct cardstate *ret = NULL;
1081 spin_lock_irqsave(&drv->lock, flags);
1082 for (i = 0; i < drv->minors; ++i) {
1083 if (!(drv->flags[i] & VALID_MINOR)) {
1084 drv->flags[i] = VALID_MINOR;
1090 spin_unlock_irqrestore(&drv->lock, flags);
1094 static void free_cs(struct cardstate *cs)
1096 unsigned long flags;
1097 struct gigaset_driver *drv = cs->driver;
1098 spin_lock_irqsave(&drv->lock, flags);
1099 drv->flags[cs->minor_index] = 0;
1100 spin_unlock_irqrestore(&drv->lock, flags);
1103 static void make_valid(struct cardstate *cs, unsigned mask)
1105 unsigned long flags;
1106 struct gigaset_driver *drv = cs->driver;
1107 spin_lock_irqsave(&drv->lock, flags);
1108 drv->flags[cs->minor_index] |= mask;
1109 spin_unlock_irqrestore(&drv->lock, flags);
1112 static void make_invalid(struct cardstate *cs, unsigned mask)
1114 unsigned long flags;
1115 struct gigaset_driver *drv = cs->driver;
1116 spin_lock_irqsave(&drv->lock, flags);
1117 drv->flags[cs->minor_index] &= ~mask;
1118 spin_unlock_irqrestore(&drv->lock, flags);
1121 /* For drivers without fixed assignment device<->cardstate (usb) */
1122 struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1124 unsigned long flags;
1125 struct cardstate *cs = NULL;
1128 spin_lock_irqsave(&drv->lock, flags);
1131 for (i = 0; i < drv->minors; ++i) {
1132 if ((drv->flags[i] & VALID_MINOR) &&
1133 !(drv->flags[i] & ASSIGNED)) {
1134 drv->flags[i] |= ASSIGNED;
1140 spin_unlock_irqrestore(&drv->lock, flags);
1143 EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1145 void gigaset_unassign(struct cardstate *cs)
1147 unsigned long flags;
1148 unsigned *minor_flags;
1149 struct gigaset_driver *drv;
1154 spin_lock_irqsave(&drv->lock, flags);
1155 minor_flags = drv->flags + cs->minor_index;
1156 if (*minor_flags & VALID_MINOR)
1157 *minor_flags &= ~ASSIGNED;
1158 spin_unlock_irqrestore(&drv->lock, flags);
1160 EXPORT_SYMBOL_GPL(gigaset_unassign);
1162 void gigaset_blockdriver(struct gigaset_driver *drv)
1164 unsigned long flags;
1165 spin_lock_irqsave(&drv->lock, flags);
1167 spin_unlock_irqrestore(&drv->lock, flags);
1169 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1171 static int __init gigaset_init_module(void)
1173 /* in accordance with the principle of least astonishment,
1174 * setting the 'debug' parameter to 1 activates a sensible
1175 * set of default debug levels
1177 if (gigaset_debuglevel == 1)
1178 gigaset_debuglevel = DEBUG_DEFAULT;
1180 info(DRIVER_AUTHOR);
1185 static void __exit gigaset_exit_module(void)
1189 module_init(gigaset_init_module);
1190 module_exit(gigaset_exit_module);
1192 MODULE_AUTHOR(DRIVER_AUTHOR);
1193 MODULE_DESCRIPTION(DRIVER_DESC);
1195 MODULE_LICENSE("GPL");