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, int from_user)
84 unsigned char outbuf[80];
85 unsigned char inbuf[80 - 1];
88 const unsigned char *in;
89 size_t space = sizeof outbuf - 1;
90 unsigned char *out = outbuf;
96 numin = len < sizeof inbuf ? len : sizeof inbuf;
98 if (copy_from_user(inbuf, (const unsigned char __user *) buf,
100 gig_dbg(level, "%s (%u bytes) - copy_from_user failed",
101 msg, (unsigned) len);
106 while (numin-- > 0) {
108 if (c == '~' || c == '^' || c == '\\') {
119 if (c < 0x20 || c == 0x7f) {
131 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
133 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
135 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
139 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
140 cs->control_state = flags;
145 set_current_state(TASK_INTERRUPTIBLE);
146 schedule_timeout(delay * HZ / 1000);
152 int gigaset_enterconfigmode(struct cardstate *cs)
156 if (!atomic_read(&cs->connected)) {
157 err("not connected!");
161 cs->control_state = TIOCM_RTS; //FIXME
163 r = setflags(cs, TIOCM_DTR, 200);
166 r = setflags(cs, 0, 200);
169 for (i = 0; i < 5; ++i) {
170 r = setflags(cs, TIOCM_RTS, 100);
173 r = setflags(cs, 0, 100);
177 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
184 dev_err(cs->dev, "error %d on setuartbits\n", -r);
185 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
186 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
191 static int test_timeout(struct at_state_t *at_state)
193 if (!at_state->timer_expires)
196 if (--at_state->timer_expires) {
197 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
198 at_state, at_state->timer_expires);
202 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
203 atomic_read(&at_state->timer_index), NULL)) {
204 //FIXME what should we do?
210 static void timer_tick(unsigned long data)
212 struct cardstate *cs = (struct cardstate *) data;
215 struct at_state_t *at_state;
218 spin_lock_irqsave(&cs->lock, flags);
220 for (channel = 0; channel < cs->channels; ++channel)
221 if (test_timeout(&cs->bcs[channel].at_state))
224 if (test_timeout(&cs->at_state))
227 list_for_each_entry(at_state, &cs->temp_at_states, list)
228 if (test_timeout(at_state))
231 if (atomic_read(&cs->running)) {
232 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
234 gig_dbg(DEBUG_CMD, "scheduling timeout");
235 tasklet_schedule(&cs->event_tasklet);
239 spin_unlock_irqrestore(&cs->lock, flags);
242 int gigaset_get_channel(struct bc_state *bcs)
246 spin_lock_irqsave(&bcs->cs->lock, flags);
247 if (bcs->use_count) {
248 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
250 spin_unlock_irqrestore(&bcs->cs->lock, flags);
255 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
256 spin_unlock_irqrestore(&bcs->cs->lock, flags);
260 void gigaset_free_channel(struct bc_state *bcs)
264 spin_lock_irqsave(&bcs->cs->lock, flags);
266 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
267 spin_unlock_irqrestore(&bcs->cs->lock, flags);
272 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
273 spin_unlock_irqrestore(&bcs->cs->lock, flags);
276 int gigaset_get_channels(struct cardstate *cs)
281 spin_lock_irqsave(&cs->lock, flags);
282 for (i = 0; i < cs->channels; ++i)
283 if (cs->bcs[i].use_count) {
284 spin_unlock_irqrestore(&cs->lock, flags);
285 gig_dbg(DEBUG_ANY, "could not allocate all channels");
288 for (i = 0; i < cs->channels; ++i)
289 ++cs->bcs[i].use_count;
290 spin_unlock_irqrestore(&cs->lock, flags);
292 gig_dbg(DEBUG_ANY, "allocated all channels");
297 void gigaset_free_channels(struct cardstate *cs)
302 gig_dbg(DEBUG_ANY, "unblocking all channels");
303 spin_lock_irqsave(&cs->lock, flags);
304 for (i = 0; i < cs->channels; ++i)
305 --cs->bcs[i].use_count;
306 spin_unlock_irqrestore(&cs->lock, flags);
309 void gigaset_block_channels(struct cardstate *cs)
314 gig_dbg(DEBUG_ANY, "blocking all channels");
315 spin_lock_irqsave(&cs->lock, flags);
316 for (i = 0; i < cs->channels; ++i)
317 ++cs->bcs[i].use_count;
318 spin_unlock_irqrestore(&cs->lock, flags);
321 static void clear_events(struct cardstate *cs)
326 /* no locking needed (no reader/writer allowed) */
328 head = atomic_read(&cs->ev_head);
329 tail = atomic_read(&cs->ev_tail);
331 while (tail != head) {
332 ev = cs->events + head;
335 head = (head + 1) % MAX_EVENTS;
338 atomic_set(&cs->ev_head, tail);
341 struct event_t *gigaset_add_event(struct cardstate *cs,
342 struct at_state_t *at_state, int type,
343 void *ptr, int parameter, void *arg)
347 struct event_t *event = NULL;
349 spin_lock_irqsave(&cs->ev_lock, flags);
351 tail = atomic_read(&cs->ev_tail);
352 next = (tail + 1) % MAX_EVENTS;
353 if (unlikely(next == atomic_read(&cs->ev_head)))
354 err("event queue full");
356 event = cs->events + tail;
358 event->at_state = at_state;
362 event->parameter = parameter;
363 atomic_set(&cs->ev_tail, next);
366 spin_unlock_irqrestore(&cs->ev_lock, flags);
370 EXPORT_SYMBOL_GPL(gigaset_add_event);
372 static void free_strings(struct at_state_t *at_state)
376 for (i = 0; i < STR_NUM; ++i) {
377 kfree(at_state->str_var[i]);
378 at_state->str_var[i] = NULL;
382 static void clear_at_state(struct at_state_t *at_state)
384 free_strings(at_state);
387 static void dealloc_at_states(struct cardstate *cs)
389 struct at_state_t *cur, *next;
391 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
392 list_del(&cur->list);
398 static void gigaset_freebcs(struct bc_state *bcs)
402 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
403 if (!bcs->cs->ops->freebcshw(bcs)) {
404 gig_dbg(DEBUG_INIT, "failed");
407 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
408 clear_at_state(&bcs->at_state);
409 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
412 dev_kfree_skb(bcs->skb);
413 for (i = 0; i < AT_NUM; ++i) {
414 kfree(bcs->commands[i]);
415 bcs->commands[i] = NULL;
419 void gigaset_freecs(struct cardstate *cs)
434 spin_lock_irqsave(&cs->lock, flags);
435 atomic_set(&cs->running, 0);
436 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
437 not rescheduled below */
439 tasklet_kill(&cs->event_tasklet);
440 del_timer_sync(&cs->timer);
442 switch (cs->cs_init) {
446 gig_dbg(DEBUG_INIT, "clearing hw");
447 cs->ops->freecshw(cs);
452 case 2: /* error in initcshw */
453 /* Deregister from LL */
454 make_invalid(cs, VALID_ID);
455 gig_dbg(DEBUG_INIT, "clearing iif");
456 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
459 case 1: /* error when regestering to LL */
460 gig_dbg(DEBUG_INIT, "clearing at_state");
461 clear_at_state(&cs->at_state);
462 dealloc_at_states(cs);
465 case 0: /* error in one call to initbcs */
466 for (i = 0; i < cs->channels; ++i) {
467 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
468 gigaset_freebcs(cs->bcs + i);
472 gig_dbg(DEBUG_INIT, "freeing inbuf");
475 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
477 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
481 EXPORT_SYMBOL_GPL(gigaset_freecs);
483 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
484 struct cardstate *cs, int cid)
488 INIT_LIST_HEAD(&at_state->list);
489 at_state->waiting = 0;
490 at_state->getstring = 0;
491 at_state->pending_commands = 0;
492 at_state->timer_expires = 0;
493 at_state->timer_active = 0;
494 atomic_set(&at_state->timer_index, 0);
495 atomic_set(&at_state->seq_index, 0);
496 at_state->ConState = 0;
497 for (i = 0; i < STR_NUM; ++i)
498 at_state->str_var[i] = NULL;
499 at_state->int_var[VAR_ZDLE] = 0;
500 at_state->int_var[VAR_ZCTP] = -1;
501 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
506 at_state->replystruct = cs->tabnocid;
508 at_state->replystruct = cs->tabcid;
512 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
513 struct cardstate *cs, int inputstate)
514 /* inbuf->read must be allocated before! */
516 atomic_set(&inbuf->head, 0);
517 atomic_set(&inbuf->tail, 0);
519 inbuf->bcs = bcs; /*base driver: NULL*/
520 inbuf->rcvbuf = NULL; //FIXME
521 inbuf->inputstate = inputstate;
524 /* Initialize the b-channel structure */
525 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
526 struct cardstate *cs, int channel)
530 bcs->tx_skb = NULL; //FIXME -> hw part
532 skb_queue_head_init(&bcs->squeue);
538 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
539 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
543 #ifdef CONFIG_GIGASET_DEBUG
547 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
548 bcs->fcs = PPP_INITFCS;
550 if (cs->ignoreframes) {
551 bcs->inputstate |= INS_skip_frame;
553 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
554 skb_reserve(bcs->skb, HW_HDR_LEN);
556 dev_warn(cs->dev, "could not allocate skb\n");
557 bcs->inputstate |= INS_skip_frame;
560 bcs->channel = channel;
566 bcs->ignore = cs->ignoreframes;
568 for (i = 0; i < AT_NUM; ++i)
569 bcs->commands[i] = NULL;
571 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
572 if (cs->ops->initbcshw(bcs))
575 gig_dbg(DEBUG_INIT, " failed");
577 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
579 dev_kfree_skb(bcs->skb);
585 * Allocate and initialize cardstate structure for Gigaset driver
586 * Calls hardware dependent gigaset_initcshw() function
587 * Calls B channel initialization function gigaset_initbcs() for each B channel
589 * drv hardware driver the device belongs to
590 * channels number of B channels supported by device
591 * onechannel !=0: B channel data and AT commands share one
592 * communication channel
593 * ==0: B channels have separate communication channels
594 * ignoreframes number of frames to ignore after setting up B channel
595 * cidmode !=0: start in CallID mode
596 * modulename name of driver module (used for I4L registration)
598 * pointer to cardstate structure
600 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
601 int onechannel, int ignoreframes,
602 int cidmode, const char *modulename)
604 struct cardstate *cs = NULL;
607 gig_dbg(DEBUG_INIT, "allocating cs");
611 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
612 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
615 gig_dbg(DEBUG_INIT, "allocating inbuf");
616 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
621 cs->channels = channels;
622 cs->onechannel = onechannel;
623 cs->ignoreframes = ignoreframes;
624 INIT_LIST_HEAD(&cs->temp_at_states);
625 atomic_set(&cs->running, 0);
626 init_timer(&cs->timer); /* clear next & prev */
627 spin_lock_init(&cs->ev_lock);
628 atomic_set(&cs->ev_tail, 0);
629 atomic_set(&cs->ev_head, 0);
630 init_MUTEX_LOCKED(&cs->sem);
631 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
633 atomic_set(&cs->commands_pending, 0);
639 atomic_set(&cs->cidmode, cidmode != 0);
641 //if(onechannel) { //FIXME
642 cs->tabnocid = gigaset_tab_nocid_m10x;
643 cs->tabcid = gigaset_tab_cid_m10x;
645 // cs->tabnocid = gigaset_tab_nocid;
646 // cs->tabcid = gigaset_tab_cid;
649 init_waitqueue_head(&cs->waitqueue);
652 atomic_set(&cs->mode, M_UNKNOWN);
653 atomic_set(&cs->mstate, MS_UNINITIALIZED);
655 for (i = 0; i < channels; ++i) {
656 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
657 if (!gigaset_initbcs(cs->bcs + i, cs, i))
663 gig_dbg(DEBUG_INIT, "setting up at_state");
664 spin_lock_init(&cs->lock);
665 gigaset_at_init(&cs->at_state, NULL, cs, 0);
669 gig_dbg(DEBUG_INIT, "setting up inbuf");
670 if (onechannel) { //FIXME distinction necessary?
671 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
673 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
675 atomic_set(&cs->connected, 0);
677 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
678 cs->cmdbuf = cs->lastcmdbuf = NULL;
679 spin_lock_init(&cs->cmdlock);
683 gig_dbg(DEBUG_INIT, "setting up iif");
684 if (!gigaset_register_to_LL(cs, modulename)) {
685 err("register_isdn failed");
689 make_valid(cs, VALID_ID);
691 gig_dbg(DEBUG_INIT, "setting up hw");
692 if (!cs->ops->initcshw(cs))
699 atomic_set(&cs->running, 1);
700 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
701 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
702 /* FIXME: can jiffies increase too much until the timer is added?
703 * Same problem(?) with mod_timer() in timer_tick(). */
704 add_timer(&cs->timer);
706 gig_dbg(DEBUG_INIT, "cs initialized");
712 gig_dbg(DEBUG_INIT, "failed");
716 EXPORT_SYMBOL_GPL(gigaset_initcs);
718 /* ReInitialize the b-channel structure */
719 /* e.g. called on hangup, disconnect */
720 void gigaset_bcs_reinit(struct bc_state *bcs)
723 struct cardstate *cs = bcs->cs;
726 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
729 spin_lock_irqsave(&cs->lock, flags);
730 clear_at_state(&bcs->at_state);
731 bcs->at_state.ConState = 0;
732 bcs->at_state.timer_active = 0;
733 bcs->at_state.timer_expires = 0;
734 bcs->at_state.cid = -1; /* No CID defined */
735 spin_unlock_irqrestore(&cs->lock, flags);
739 #ifdef CONFIG_GIGASET_DEBUG
743 bcs->fcs = PPP_INITFCS;
746 bcs->ignore = cs->ignoreframes;
748 bcs->inputstate |= INS_skip_frame;
751 cs->ops->reinitbcshw(bcs);
754 static void cleanup_cs(struct cardstate *cs)
756 struct cmdbuf_t *cb, *tcb;
760 spin_lock_irqsave(&cs->lock, flags);
762 atomic_set(&cs->mode, M_UNKNOWN);
763 atomic_set(&cs->mstate, MS_UNINITIALIZED);
765 clear_at_state(&cs->at_state);
766 dealloc_at_states(cs);
767 free_strings(&cs->at_state);
768 gigaset_at_init(&cs->at_state, NULL, cs, 0);
770 kfree(cs->inbuf->rcvbuf);
771 cs->inbuf->rcvbuf = NULL;
772 cs->inbuf->inputstate = INS_command;
773 atomic_set(&cs->inbuf->head, 0);
774 atomic_set(&cs->inbuf->tail, 0);
782 cs->cmdbuf = cs->lastcmdbuf = NULL;
788 atomic_set(&cs->commands_pending, 0);
791 spin_unlock_irqrestore(&cs->lock, flags);
793 for (i = 0; i < cs->channels; ++i) {
794 gigaset_freebcs(cs->bcs + i);
795 if (!gigaset_initbcs(cs->bcs + i, cs, i))
796 break; //FIXME error handling
800 cs->cmd_result = -ENODEV;
802 wake_up_interruptible(&cs->waitqueue);
807 int gigaset_start(struct cardstate *cs)
809 if (down_interruptible(&cs->sem))
812 atomic_set(&cs->connected, 1);
814 if (atomic_read(&cs->mstate) != MS_LOCKED) {
815 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
816 cs->ops->baud_rate(cs, B115200);
817 cs->ops->set_line_ctrl(cs, CS8);
818 cs->control_state = TIOCM_DTR|TIOCM_RTS;
820 //FIXME use some saved values?
825 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
827 //FIXME what should we do?
831 gig_dbg(DEBUG_CMD, "scheduling START");
832 gigaset_schedule_event(cs);
834 wait_event(cs->waitqueue, !cs->waiting);
836 /* set up device sysfs */
837 gigaset_init_dev_sysfs(cs);
846 EXPORT_SYMBOL_GPL(gigaset_start);
848 void gigaset_shutdown(struct cardstate *cs)
854 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
855 //FIXME what should we do?
859 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
860 gigaset_schedule_event(cs);
862 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
863 warn("%s: aborted", __func__);
867 if (atomic_read(&cs->mstate) != MS_LOCKED) {
869 //gigaset_baud_rate(cs, B115200);
870 //gigaset_set_line_ctrl(cs, CS8);
871 //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
872 //cs->control_state = 0;
874 //FIXME use some saved values?
882 EXPORT_SYMBOL_GPL(gigaset_shutdown);
884 void gigaset_stop(struct cardstate *cs)
888 /* clear device sysfs */
889 gigaset_free_dev_sysfs(cs);
891 atomic_set(&cs->connected, 0);
895 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
896 //FIXME what should we do?
900 gig_dbg(DEBUG_CMD, "scheduling STOP");
901 gigaset_schedule_event(cs);
903 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
904 warn("%s: aborted", __func__);
908 /* Tell the LL that the device is not available .. */
909 gigaset_i4l_cmd(cs, ISDN_STAT_STOP); // FIXME move to event layer?
916 EXPORT_SYMBOL_GPL(gigaset_stop);
918 static LIST_HEAD(drivers);
919 static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
921 struct cardstate *gigaset_get_cs_by_id(int id)
924 static struct cardstate *ret = NULL;
925 static struct cardstate *cs;
926 struct gigaset_driver *drv;
929 spin_lock_irqsave(&driver_lock, flags);
930 list_for_each_entry(drv, &drivers, list) {
931 spin_lock(&drv->lock);
932 for (i = 0; i < drv->minors; ++i) {
933 if (drv->flags[i] & VALID_ID) {
941 spin_unlock(&drv->lock);
945 spin_unlock_irqrestore(&driver_lock, flags);
949 void gigaset_debugdrivers(void)
952 static struct cardstate *cs;
953 struct gigaset_driver *drv;
956 spin_lock_irqsave(&driver_lock, flags);
957 list_for_each_entry(drv, &drivers, list) {
958 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
959 spin_lock(&drv->lock);
960 for (i = 0; i < drv->minors; ++i) {
961 gig_dbg(DEBUG_DRIVER, " index %u", i);
962 gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
965 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
966 gig_dbg(DEBUG_DRIVER, " minor_index %u",
968 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
969 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
971 spin_unlock(&drv->lock);
973 spin_unlock_irqrestore(&driver_lock, flags);
975 EXPORT_SYMBOL_GPL(gigaset_debugdrivers);
977 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
979 if (tty->index < 0 || tty->index >= tty->driver->num)
981 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
984 struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
987 static struct cardstate *ret = NULL;
988 struct gigaset_driver *drv;
991 spin_lock_irqsave(&driver_lock, flags);
992 list_for_each_entry(drv, &drivers, list) {
993 if (minor < drv->minor || minor >= drv->minor + drv->minors)
995 index = minor - drv->minor;
996 spin_lock(&drv->lock);
997 if (drv->flags[index] & VALID_MINOR)
998 ret = drv->cs + index;
999 spin_unlock(&drv->lock);
1003 spin_unlock_irqrestore(&driver_lock, flags);
1007 void gigaset_freedriver(struct gigaset_driver *drv)
1009 unsigned long flags;
1011 spin_lock_irqsave(&driver_lock, flags);
1012 list_del(&drv->list);
1013 spin_unlock_irqrestore(&driver_lock, flags);
1015 gigaset_if_freedriver(drv);
1016 module_put(drv->owner);
1022 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1024 /* gigaset_initdriver
1025 * Allocate and initialize gigaset_driver structure. Initialize interface.
1027 * minor First minor number
1028 * minors Number of minors this driver can handle
1029 * procname Name of the driver
1030 * devname Name of the device files (prefix without minor number)
1031 * devfsname Devfs name of the device files without %d
1033 * Pointer to the gigaset_driver structure on success, NULL on failure.
1035 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1036 const char *procname,
1037 const char *devname,
1038 const char *devfsname,
1039 const struct gigaset_ops *ops,
1040 struct module *owner)
1042 struct gigaset_driver *drv;
1043 unsigned long flags;
1046 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1049 if (!try_module_get(owner))
1055 drv->minors = minors;
1056 spin_lock_init(&drv->lock);
1060 INIT_LIST_HEAD(&drv->list);
1062 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1065 drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1069 for (i = 0; i < minors; ++i) {
1071 drv->cs[i].driver = drv;
1072 drv->cs[i].ops = drv->ops;
1073 drv->cs[i].minor_index = i;
1076 gigaset_if_initdriver(drv, procname, devname, devfsname);
1078 spin_lock_irqsave(&driver_lock, flags);
1079 list_add(&drv->list, &drivers);
1080 spin_unlock_irqrestore(&driver_lock, flags);
1091 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1093 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
1095 unsigned long flags;
1097 static struct cardstate *ret = NULL;
1099 spin_lock_irqsave(&drv->lock, flags);
1100 for (i = 0; i < drv->minors; ++i) {
1101 if (!(drv->flags[i] & VALID_MINOR)) {
1102 drv->flags[i] = VALID_MINOR;
1108 spin_unlock_irqrestore(&drv->lock, flags);
1112 static void free_cs(struct cardstate *cs)
1114 unsigned long flags;
1115 struct gigaset_driver *drv = cs->driver;
1116 spin_lock_irqsave(&drv->lock, flags);
1117 drv->flags[cs->minor_index] = 0;
1118 spin_unlock_irqrestore(&drv->lock, flags);
1121 static void make_valid(struct cardstate *cs, unsigned mask)
1123 unsigned long flags;
1124 struct gigaset_driver *drv = cs->driver;
1125 spin_lock_irqsave(&drv->lock, flags);
1126 drv->flags[cs->minor_index] |= mask;
1127 spin_unlock_irqrestore(&drv->lock, flags);
1130 static void make_invalid(struct cardstate *cs, unsigned mask)
1132 unsigned long flags;
1133 struct gigaset_driver *drv = cs->driver;
1134 spin_lock_irqsave(&drv->lock, flags);
1135 drv->flags[cs->minor_index] &= ~mask;
1136 spin_unlock_irqrestore(&drv->lock, flags);
1139 /* For drivers without fixed assignment device<->cardstate (usb) */
1140 struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1142 unsigned long flags;
1143 struct cardstate *cs = NULL;
1146 spin_lock_irqsave(&drv->lock, flags);
1149 for (i = 0; i < drv->minors; ++i) {
1150 if ((drv->flags[i] & VALID_MINOR) &&
1151 !(drv->flags[i] & ASSIGNED)) {
1152 drv->flags[i] |= ASSIGNED;
1158 spin_unlock_irqrestore(&drv->lock, flags);
1161 EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1163 void gigaset_unassign(struct cardstate *cs)
1165 unsigned long flags;
1166 unsigned *minor_flags;
1167 struct gigaset_driver *drv;
1172 spin_lock_irqsave(&drv->lock, flags);
1173 minor_flags = drv->flags + cs->minor_index;
1174 if (*minor_flags & VALID_MINOR)
1175 *minor_flags &= ~ASSIGNED;
1176 spin_unlock_irqrestore(&drv->lock, flags);
1178 EXPORT_SYMBOL_GPL(gigaset_unassign);
1180 void gigaset_blockdriver(struct gigaset_driver *drv)
1182 unsigned long flags;
1183 spin_lock_irqsave(&drv->lock, flags);
1185 spin_unlock_irqrestore(&drv->lock, flags);
1187 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1189 static int __init gigaset_init_module(void)
1191 /* in accordance with the principle of least astonishment,
1192 * setting the 'debug' parameter to 1 activates a sensible
1193 * set of default debug levels
1195 if (gigaset_debuglevel == 1)
1196 gigaset_debuglevel = DEBUG_DEFAULT;
1198 info(DRIVER_AUTHOR);
1203 static void __exit gigaset_exit_module(void)
1207 module_init(gigaset_init_module);
1208 module_exit(gigaset_exit_module);
1210 MODULE_AUTHOR(DRIVER_AUTHOR);
1211 MODULE_DESCRIPTION(DRIVER_DESC);
1213 MODULE_LICENSE("GPL");