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 struct cardstate *alloc_cs(struct gigaset_driver *drv);
36 static void free_cs(struct cardstate *cs);
37 static void make_valid(struct cardstate *cs, unsigned mask);
38 static void make_invalid(struct cardstate *cs, unsigned mask);
40 #define VALID_MINOR 0x01
44 /* bitwise byte inversion table */
45 __u8 gigaset_invtab[256] = {
46 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
47 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
48 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
49 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
50 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
51 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
52 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
53 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
54 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
55 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
56 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
57 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
58 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
59 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
60 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
61 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
62 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
63 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
64 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
65 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
66 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
67 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
68 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
69 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
70 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
71 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
72 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
73 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
74 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
75 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
76 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
77 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
79 EXPORT_SYMBOL_GPL(gigaset_invtab);
81 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
82 size_t len, const unsigned char *buf)
84 unsigned char outbuf[80];
86 size_t space = sizeof outbuf - 1;
87 unsigned char *out = outbuf;
92 if (c == '~' || c == '^' || c == '\\') {
103 if (c < 0x20 || c == 0x7f) {
115 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
117 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
119 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
123 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
124 cs->control_state = flags;
129 set_current_state(TASK_INTERRUPTIBLE);
130 schedule_timeout(delay * HZ / 1000);
136 int gigaset_enterconfigmode(struct cardstate *cs)
140 if (!atomic_read(&cs->connected)) {
141 err("not connected!");
145 cs->control_state = TIOCM_RTS; //FIXME
147 r = setflags(cs, TIOCM_DTR, 200);
150 r = setflags(cs, 0, 200);
153 for (i = 0; i < 5; ++i) {
154 r = setflags(cs, TIOCM_RTS, 100);
157 r = setflags(cs, 0, 100);
161 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
168 dev_err(cs->dev, "error %d on setuartbits\n", -r);
169 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
170 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
175 static int test_timeout(struct at_state_t *at_state)
177 if (!at_state->timer_expires)
180 if (--at_state->timer_expires) {
181 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
182 at_state, at_state->timer_expires);
186 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
187 atomic_read(&at_state->timer_index), NULL)) {
188 //FIXME what should we do?
194 static void timer_tick(unsigned long data)
196 struct cardstate *cs = (struct cardstate *) data;
199 struct at_state_t *at_state;
202 spin_lock_irqsave(&cs->lock, flags);
204 for (channel = 0; channel < cs->channels; ++channel)
205 if (test_timeout(&cs->bcs[channel].at_state))
208 if (test_timeout(&cs->at_state))
211 list_for_each_entry(at_state, &cs->temp_at_states, list)
212 if (test_timeout(at_state))
215 if (atomic_read(&cs->running)) {
216 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
218 gig_dbg(DEBUG_CMD, "scheduling timeout");
219 tasklet_schedule(&cs->event_tasklet);
223 spin_unlock_irqrestore(&cs->lock, flags);
226 int gigaset_get_channel(struct bc_state *bcs)
230 spin_lock_irqsave(&bcs->cs->lock, flags);
231 if (bcs->use_count) {
232 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
234 spin_unlock_irqrestore(&bcs->cs->lock, flags);
239 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
240 spin_unlock_irqrestore(&bcs->cs->lock, flags);
244 void gigaset_free_channel(struct bc_state *bcs)
248 spin_lock_irqsave(&bcs->cs->lock, flags);
250 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
251 spin_unlock_irqrestore(&bcs->cs->lock, flags);
256 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
257 spin_unlock_irqrestore(&bcs->cs->lock, flags);
260 int gigaset_get_channels(struct cardstate *cs)
265 spin_lock_irqsave(&cs->lock, flags);
266 for (i = 0; i < cs->channels; ++i)
267 if (cs->bcs[i].use_count) {
268 spin_unlock_irqrestore(&cs->lock, flags);
269 gig_dbg(DEBUG_ANY, "could not allocate all channels");
272 for (i = 0; i < cs->channels; ++i)
273 ++cs->bcs[i].use_count;
274 spin_unlock_irqrestore(&cs->lock, flags);
276 gig_dbg(DEBUG_ANY, "allocated all channels");
281 void gigaset_free_channels(struct cardstate *cs)
286 gig_dbg(DEBUG_ANY, "unblocking all channels");
287 spin_lock_irqsave(&cs->lock, flags);
288 for (i = 0; i < cs->channels; ++i)
289 --cs->bcs[i].use_count;
290 spin_unlock_irqrestore(&cs->lock, flags);
293 void gigaset_block_channels(struct cardstate *cs)
298 gig_dbg(DEBUG_ANY, "blocking all channels");
299 spin_lock_irqsave(&cs->lock, flags);
300 for (i = 0; i < cs->channels; ++i)
301 ++cs->bcs[i].use_count;
302 spin_unlock_irqrestore(&cs->lock, flags);
305 static void clear_events(struct cardstate *cs)
310 /* no locking needed (no reader/writer allowed) */
312 head = atomic_read(&cs->ev_head);
313 tail = atomic_read(&cs->ev_tail);
315 while (tail != head) {
316 ev = cs->events + head;
319 head = (head + 1) % MAX_EVENTS;
322 atomic_set(&cs->ev_head, tail);
325 struct event_t *gigaset_add_event(struct cardstate *cs,
326 struct at_state_t *at_state, int type,
327 void *ptr, int parameter, void *arg)
331 struct event_t *event = NULL;
333 spin_lock_irqsave(&cs->ev_lock, flags);
335 tail = atomic_read(&cs->ev_tail);
336 next = (tail + 1) % MAX_EVENTS;
337 if (unlikely(next == atomic_read(&cs->ev_head)))
338 err("event queue full");
340 event = cs->events + tail;
342 event->at_state = at_state;
346 event->parameter = parameter;
347 atomic_set(&cs->ev_tail, next);
350 spin_unlock_irqrestore(&cs->ev_lock, flags);
354 EXPORT_SYMBOL_GPL(gigaset_add_event);
356 static void free_strings(struct at_state_t *at_state)
360 for (i = 0; i < STR_NUM; ++i) {
361 kfree(at_state->str_var[i]);
362 at_state->str_var[i] = NULL;
366 static void clear_at_state(struct at_state_t *at_state)
368 free_strings(at_state);
371 static void dealloc_at_states(struct cardstate *cs)
373 struct at_state_t *cur, *next;
375 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
376 list_del(&cur->list);
382 static void gigaset_freebcs(struct bc_state *bcs)
386 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
387 if (!bcs->cs->ops->freebcshw(bcs)) {
388 gig_dbg(DEBUG_INIT, "failed");
391 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
392 clear_at_state(&bcs->at_state);
393 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
396 dev_kfree_skb(bcs->skb);
397 for (i = 0; i < AT_NUM; ++i) {
398 kfree(bcs->commands[i]);
399 bcs->commands[i] = NULL;
403 void gigaset_freecs(struct cardstate *cs)
418 spin_lock_irqsave(&cs->lock, flags);
419 atomic_set(&cs->running, 0);
420 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
421 not rescheduled below */
423 tasklet_kill(&cs->event_tasklet);
424 del_timer_sync(&cs->timer);
426 switch (cs->cs_init) {
430 gig_dbg(DEBUG_INIT, "clearing hw");
431 cs->ops->freecshw(cs);
436 case 2: /* error in initcshw */
437 /* Deregister from LL */
438 make_invalid(cs, VALID_ID);
439 gig_dbg(DEBUG_INIT, "clearing iif");
440 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
443 case 1: /* error when regestering to LL */
444 gig_dbg(DEBUG_INIT, "clearing at_state");
445 clear_at_state(&cs->at_state);
446 dealloc_at_states(cs);
449 case 0: /* error in one call to initbcs */
450 for (i = 0; i < cs->channels; ++i) {
451 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
452 gigaset_freebcs(cs->bcs + i);
456 gig_dbg(DEBUG_INIT, "freeing inbuf");
459 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
461 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
465 EXPORT_SYMBOL_GPL(gigaset_freecs);
467 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
468 struct cardstate *cs, int cid)
472 INIT_LIST_HEAD(&at_state->list);
473 at_state->waiting = 0;
474 at_state->getstring = 0;
475 at_state->pending_commands = 0;
476 at_state->timer_expires = 0;
477 at_state->timer_active = 0;
478 atomic_set(&at_state->timer_index, 0);
479 atomic_set(&at_state->seq_index, 0);
480 at_state->ConState = 0;
481 for (i = 0; i < STR_NUM; ++i)
482 at_state->str_var[i] = NULL;
483 at_state->int_var[VAR_ZDLE] = 0;
484 at_state->int_var[VAR_ZCTP] = -1;
485 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
490 at_state->replystruct = cs->tabnocid;
492 at_state->replystruct = cs->tabcid;
496 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
497 struct cardstate *cs, int inputstate)
498 /* inbuf->read must be allocated before! */
500 atomic_set(&inbuf->head, 0);
501 atomic_set(&inbuf->tail, 0);
503 inbuf->bcs = bcs; /*base driver: NULL*/
504 inbuf->rcvbuf = NULL; //FIXME
505 inbuf->inputstate = inputstate;
508 /* append received bytes to inbuf */
509 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
512 unsigned n, head, tail, bytesleft;
514 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
519 bytesleft = numbytes;
520 tail = atomic_read(&inbuf->tail);
521 head = atomic_read(&inbuf->head);
522 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
528 n = (RBUFSIZE-1) - tail;
532 dev_err(inbuf->cs->dev,
533 "buffer overflow (%u bytes lost)", bytesleft);
538 memcpy(inbuf->data + tail, src, n);
540 tail = (tail + n) % RBUFSIZE;
543 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
544 atomic_set(&inbuf->tail, tail);
545 return numbytes != bytesleft;
547 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
549 /* Initialize the b-channel structure */
550 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
551 struct cardstate *cs, int channel)
555 bcs->tx_skb = NULL; //FIXME -> hw part
557 skb_queue_head_init(&bcs->squeue);
563 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
564 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
568 #ifdef CONFIG_GIGASET_DEBUG
572 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
573 bcs->fcs = PPP_INITFCS;
575 if (cs->ignoreframes) {
576 bcs->inputstate |= INS_skip_frame;
578 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
579 skb_reserve(bcs->skb, HW_HDR_LEN);
581 dev_warn(cs->dev, "could not allocate skb\n");
582 bcs->inputstate |= INS_skip_frame;
585 bcs->channel = channel;
591 bcs->ignore = cs->ignoreframes;
593 for (i = 0; i < AT_NUM; ++i)
594 bcs->commands[i] = NULL;
596 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
597 if (cs->ops->initbcshw(bcs))
600 gig_dbg(DEBUG_INIT, " failed");
602 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
604 dev_kfree_skb(bcs->skb);
610 * Allocate and initialize cardstate structure for Gigaset driver
611 * Calls hardware dependent gigaset_initcshw() function
612 * Calls B channel initialization function gigaset_initbcs() for each B channel
614 * drv hardware driver the device belongs to
615 * channels number of B channels supported by device
616 * onechannel !=0: B channel data and AT commands share one
617 * communication channel
618 * ==0: B channels have separate communication channels
619 * ignoreframes number of frames to ignore after setting up B channel
620 * cidmode !=0: start in CallID mode
621 * modulename name of driver module (used for I4L registration)
623 * pointer to cardstate structure
625 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
626 int onechannel, int ignoreframes,
627 int cidmode, const char *modulename)
629 struct cardstate *cs = NULL;
632 gig_dbg(DEBUG_INIT, "allocating cs");
636 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
637 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
640 gig_dbg(DEBUG_INIT, "allocating inbuf");
641 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
646 cs->channels = channels;
647 cs->onechannel = onechannel;
648 cs->ignoreframes = ignoreframes;
649 INIT_LIST_HEAD(&cs->temp_at_states);
650 atomic_set(&cs->running, 0);
651 init_timer(&cs->timer); /* clear next & prev */
652 spin_lock_init(&cs->ev_lock);
653 atomic_set(&cs->ev_tail, 0);
654 atomic_set(&cs->ev_head, 0);
655 init_MUTEX_LOCKED(&cs->sem);
656 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
658 atomic_set(&cs->commands_pending, 0);
664 atomic_set(&cs->cidmode, cidmode != 0);
666 //if(onechannel) { //FIXME
667 cs->tabnocid = gigaset_tab_nocid_m10x;
668 cs->tabcid = gigaset_tab_cid_m10x;
670 // cs->tabnocid = gigaset_tab_nocid;
671 // cs->tabcid = gigaset_tab_cid;
674 init_waitqueue_head(&cs->waitqueue);
677 atomic_set(&cs->mode, M_UNKNOWN);
678 atomic_set(&cs->mstate, MS_UNINITIALIZED);
680 for (i = 0; i < channels; ++i) {
681 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
682 if (!gigaset_initbcs(cs->bcs + i, cs, i))
688 gig_dbg(DEBUG_INIT, "setting up at_state");
689 spin_lock_init(&cs->lock);
690 gigaset_at_init(&cs->at_state, NULL, cs, 0);
694 gig_dbg(DEBUG_INIT, "setting up inbuf");
695 if (onechannel) { //FIXME distinction necessary?
696 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
698 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
700 atomic_set(&cs->connected, 0);
702 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
703 cs->cmdbuf = cs->lastcmdbuf = NULL;
704 spin_lock_init(&cs->cmdlock);
708 gig_dbg(DEBUG_INIT, "setting up iif");
709 if (!gigaset_register_to_LL(cs, modulename)) {
710 err("register_isdn failed");
714 make_valid(cs, VALID_ID);
716 gig_dbg(DEBUG_INIT, "setting up hw");
717 if (!cs->ops->initcshw(cs))
724 atomic_set(&cs->running, 1);
725 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
726 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
727 /* FIXME: can jiffies increase too much until the timer is added?
728 * Same problem(?) with mod_timer() in timer_tick(). */
729 add_timer(&cs->timer);
731 gig_dbg(DEBUG_INIT, "cs initialized");
737 gig_dbg(DEBUG_INIT, "failed");
741 EXPORT_SYMBOL_GPL(gigaset_initcs);
743 /* ReInitialize the b-channel structure */
744 /* e.g. called on hangup, disconnect */
745 void gigaset_bcs_reinit(struct bc_state *bcs)
748 struct cardstate *cs = bcs->cs;
751 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
754 spin_lock_irqsave(&cs->lock, flags);
755 clear_at_state(&bcs->at_state);
756 bcs->at_state.ConState = 0;
757 bcs->at_state.timer_active = 0;
758 bcs->at_state.timer_expires = 0;
759 bcs->at_state.cid = -1; /* No CID defined */
760 spin_unlock_irqrestore(&cs->lock, flags);
764 #ifdef CONFIG_GIGASET_DEBUG
768 bcs->fcs = PPP_INITFCS;
771 bcs->ignore = cs->ignoreframes;
773 bcs->inputstate |= INS_skip_frame;
776 cs->ops->reinitbcshw(bcs);
779 static void cleanup_cs(struct cardstate *cs)
781 struct cmdbuf_t *cb, *tcb;
785 spin_lock_irqsave(&cs->lock, flags);
787 atomic_set(&cs->mode, M_UNKNOWN);
788 atomic_set(&cs->mstate, MS_UNINITIALIZED);
790 clear_at_state(&cs->at_state);
791 dealloc_at_states(cs);
792 free_strings(&cs->at_state);
793 gigaset_at_init(&cs->at_state, NULL, cs, 0);
795 kfree(cs->inbuf->rcvbuf);
796 cs->inbuf->rcvbuf = NULL;
797 cs->inbuf->inputstate = INS_command;
798 atomic_set(&cs->inbuf->head, 0);
799 atomic_set(&cs->inbuf->tail, 0);
807 cs->cmdbuf = cs->lastcmdbuf = NULL;
813 atomic_set(&cs->commands_pending, 0);
816 spin_unlock_irqrestore(&cs->lock, flags);
818 for (i = 0; i < cs->channels; ++i) {
819 gigaset_freebcs(cs->bcs + i);
820 if (!gigaset_initbcs(cs->bcs + i, cs, i))
821 break; //FIXME error handling
825 cs->cmd_result = -ENODEV;
827 wake_up_interruptible(&cs->waitqueue);
832 int gigaset_start(struct cardstate *cs)
834 if (down_interruptible(&cs->sem))
837 atomic_set(&cs->connected, 1);
839 if (atomic_read(&cs->mstate) != MS_LOCKED) {
840 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
841 cs->ops->baud_rate(cs, B115200);
842 cs->ops->set_line_ctrl(cs, CS8);
843 cs->control_state = TIOCM_DTR|TIOCM_RTS;
845 //FIXME use some saved values?
850 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
852 //FIXME what should we do?
856 gig_dbg(DEBUG_CMD, "scheduling START");
857 gigaset_schedule_event(cs);
859 wait_event(cs->waitqueue, !cs->waiting);
861 /* set up device sysfs */
862 gigaset_init_dev_sysfs(cs);
871 EXPORT_SYMBOL_GPL(gigaset_start);
873 void gigaset_shutdown(struct cardstate *cs)
879 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
880 //FIXME what should we do?
884 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
885 gigaset_schedule_event(cs);
887 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
888 warn("%s: aborted", __func__);
892 if (atomic_read(&cs->mstate) != MS_LOCKED) {
894 //gigaset_baud_rate(cs, B115200);
895 //gigaset_set_line_ctrl(cs, CS8);
896 //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
897 //cs->control_state = 0;
899 //FIXME use some saved values?
907 EXPORT_SYMBOL_GPL(gigaset_shutdown);
909 void gigaset_stop(struct cardstate *cs)
913 /* clear device sysfs */
914 gigaset_free_dev_sysfs(cs);
916 atomic_set(&cs->connected, 0);
920 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
921 //FIXME what should we do?
925 gig_dbg(DEBUG_CMD, "scheduling STOP");
926 gigaset_schedule_event(cs);
928 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
929 warn("%s: aborted", __func__);
933 /* Tell the LL that the device is not available .. */
934 gigaset_i4l_cmd(cs, ISDN_STAT_STOP); // FIXME move to event layer?
941 EXPORT_SYMBOL_GPL(gigaset_stop);
943 static LIST_HEAD(drivers);
944 static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
946 struct cardstate *gigaset_get_cs_by_id(int id)
949 static struct cardstate *ret = NULL;
950 static struct cardstate *cs;
951 struct gigaset_driver *drv;
954 spin_lock_irqsave(&driver_lock, flags);
955 list_for_each_entry(drv, &drivers, list) {
956 spin_lock(&drv->lock);
957 for (i = 0; i < drv->minors; ++i) {
958 if (drv->flags[i] & VALID_ID) {
966 spin_unlock(&drv->lock);
970 spin_unlock_irqrestore(&driver_lock, flags);
974 void gigaset_debugdrivers(void)
977 static struct cardstate *cs;
978 struct gigaset_driver *drv;
981 spin_lock_irqsave(&driver_lock, flags);
982 list_for_each_entry(drv, &drivers, list) {
983 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
984 spin_lock(&drv->lock);
985 for (i = 0; i < drv->minors; ++i) {
986 gig_dbg(DEBUG_DRIVER, " index %u", i);
987 gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
990 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
991 gig_dbg(DEBUG_DRIVER, " minor_index %u",
993 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
994 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
996 spin_unlock(&drv->lock);
998 spin_unlock_irqrestore(&driver_lock, flags);
1000 EXPORT_SYMBOL_GPL(gigaset_debugdrivers);
1002 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1004 if (tty->index < 0 || tty->index >= tty->driver->num)
1006 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1009 struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1011 unsigned long flags;
1012 static struct cardstate *ret = NULL;
1013 struct gigaset_driver *drv;
1016 spin_lock_irqsave(&driver_lock, flags);
1017 list_for_each_entry(drv, &drivers, list) {
1018 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1020 index = minor - drv->minor;
1021 spin_lock(&drv->lock);
1022 if (drv->flags[index] & VALID_MINOR)
1023 ret = drv->cs + index;
1024 spin_unlock(&drv->lock);
1028 spin_unlock_irqrestore(&driver_lock, flags);
1032 void gigaset_freedriver(struct gigaset_driver *drv)
1034 unsigned long flags;
1036 spin_lock_irqsave(&driver_lock, flags);
1037 list_del(&drv->list);
1038 spin_unlock_irqrestore(&driver_lock, flags);
1040 gigaset_if_freedriver(drv);
1041 module_put(drv->owner);
1047 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1049 /* gigaset_initdriver
1050 * Allocate and initialize gigaset_driver structure. Initialize interface.
1052 * minor First minor number
1053 * minors Number of minors this driver can handle
1054 * procname Name of the driver
1055 * devname Name of the device files (prefix without minor number)
1056 * devfsname Devfs name of the device files without %d
1058 * Pointer to the gigaset_driver structure on success, NULL on failure.
1060 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1061 const char *procname,
1062 const char *devname,
1063 const char *devfsname,
1064 const struct gigaset_ops *ops,
1065 struct module *owner)
1067 struct gigaset_driver *drv;
1068 unsigned long flags;
1071 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1074 if (!try_module_get(owner))
1080 drv->minors = minors;
1081 spin_lock_init(&drv->lock);
1085 INIT_LIST_HEAD(&drv->list);
1087 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1090 drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1094 for (i = 0; i < minors; ++i) {
1096 drv->cs[i].driver = drv;
1097 drv->cs[i].ops = drv->ops;
1098 drv->cs[i].minor_index = i;
1101 gigaset_if_initdriver(drv, procname, devname, devfsname);
1103 spin_lock_irqsave(&driver_lock, flags);
1104 list_add(&drv->list, &drivers);
1105 spin_unlock_irqrestore(&driver_lock, flags);
1116 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1118 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
1120 unsigned long flags;
1122 static struct cardstate *ret = NULL;
1124 spin_lock_irqsave(&drv->lock, flags);
1125 for (i = 0; i < drv->minors; ++i) {
1126 if (!(drv->flags[i] & VALID_MINOR)) {
1127 drv->flags[i] = VALID_MINOR;
1133 spin_unlock_irqrestore(&drv->lock, flags);
1137 static void free_cs(struct cardstate *cs)
1139 unsigned long flags;
1140 struct gigaset_driver *drv = cs->driver;
1141 spin_lock_irqsave(&drv->lock, flags);
1142 drv->flags[cs->minor_index] = 0;
1143 spin_unlock_irqrestore(&drv->lock, flags);
1146 static void make_valid(struct cardstate *cs, unsigned mask)
1148 unsigned long flags;
1149 struct gigaset_driver *drv = cs->driver;
1150 spin_lock_irqsave(&drv->lock, flags);
1151 drv->flags[cs->minor_index] |= mask;
1152 spin_unlock_irqrestore(&drv->lock, flags);
1155 static void make_invalid(struct cardstate *cs, unsigned mask)
1157 unsigned long flags;
1158 struct gigaset_driver *drv = cs->driver;
1159 spin_lock_irqsave(&drv->lock, flags);
1160 drv->flags[cs->minor_index] &= ~mask;
1161 spin_unlock_irqrestore(&drv->lock, flags);
1164 /* For drivers without fixed assignment device<->cardstate (usb) */
1165 struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1167 unsigned long flags;
1168 struct cardstate *cs = NULL;
1171 spin_lock_irqsave(&drv->lock, flags);
1174 for (i = 0; i < drv->minors; ++i) {
1175 if ((drv->flags[i] & VALID_MINOR) &&
1176 !(drv->flags[i] & ASSIGNED)) {
1177 drv->flags[i] |= ASSIGNED;
1183 spin_unlock_irqrestore(&drv->lock, flags);
1186 EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1188 void gigaset_unassign(struct cardstate *cs)
1190 unsigned long flags;
1191 unsigned *minor_flags;
1192 struct gigaset_driver *drv;
1197 spin_lock_irqsave(&drv->lock, flags);
1198 minor_flags = drv->flags + cs->minor_index;
1199 if (*minor_flags & VALID_MINOR)
1200 *minor_flags &= ~ASSIGNED;
1201 spin_unlock_irqrestore(&drv->lock, flags);
1203 EXPORT_SYMBOL_GPL(gigaset_unassign);
1205 void gigaset_blockdriver(struct gigaset_driver *drv)
1207 unsigned long flags;
1208 spin_lock_irqsave(&drv->lock, flags);
1210 spin_unlock_irqrestore(&drv->lock, flags);
1212 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1214 static int __init gigaset_init_module(void)
1216 /* in accordance with the principle of least astonishment,
1217 * setting the 'debug' parameter to 1 activates a sensible
1218 * set of default debug levels
1220 if (gigaset_debuglevel == 1)
1221 gigaset_debuglevel = DEBUG_DEFAULT;
1223 info(DRIVER_AUTHOR);
1228 static void __exit gigaset_exit_module(void)
1232 module_init(gigaset_init_module);
1233 module_exit(gigaset_exit_module);
1235 MODULE_AUTHOR(DRIVER_AUTHOR);
1236 MODULE_DESCRIPTION(DRIVER_DESC);
1238 MODULE_LICENSE("GPL");