2 * Stuff used by all variants of the driver
4 * Copyright (c) 2001 by Stefan Eilers,
5 * Hansjoerg Lipp <hjlipp@web.de>,
6 * Tilman Schmidt <tilman@imap.cc>.
8 * =====================================================================
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 * =====================================================================
17 #include <linux/ctype.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/slab.h>
22 /* Version Information */
23 #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
24 #define DRIVER_DESC "Driver for Gigaset 307x"
26 #ifdef CONFIG_GIGASET_DEBUG
27 #define DRIVER_DESC_DEBUG " (debug build)"
29 #define DRIVER_DESC_DEBUG ""
32 /* Module parameters */
33 int gigaset_debuglevel;
34 EXPORT_SYMBOL_GPL(gigaset_debuglevel);
35 module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
36 MODULE_PARM_DESC(debug, "debug level");
38 /* driver state flags */
39 #define VALID_MINOR 0x01
43 * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging
44 * @level: debugging level.
45 * @msg: message prefix.
46 * @len: number of bytes to dump.
49 * If the current debugging level includes one of the bits set in @level,
50 * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio,
51 * prefixed by the text @msg.
53 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
54 size_t len, const unsigned char *buf)
56 unsigned char outbuf[80];
58 size_t space = sizeof outbuf - 1;
59 unsigned char *out = outbuf;
64 if (c == '~' || c == '^' || c == '\\') {
75 if (c < 0x20 || c == 0x7f) {
87 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
89 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
91 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
95 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
96 cs->control_state = flags;
101 set_current_state(TASK_INTERRUPTIBLE);
102 schedule_timeout(delay * HZ / 1000);
108 int gigaset_enterconfigmode(struct cardstate *cs)
112 cs->control_state = TIOCM_RTS;
114 r = setflags(cs, TIOCM_DTR, 200);
117 r = setflags(cs, 0, 200);
120 for (i = 0; i < 5; ++i) {
121 r = setflags(cs, TIOCM_RTS, 100);
124 r = setflags(cs, 0, 100);
128 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
135 dev_err(cs->dev, "error %d on setuartbits\n", -r);
136 cs->control_state = TIOCM_RTS|TIOCM_DTR;
137 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
142 static int test_timeout(struct at_state_t *at_state)
144 if (!at_state->timer_expires)
147 if (--at_state->timer_expires) {
148 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
149 at_state, at_state->timer_expires);
153 gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
154 at_state->timer_index, NULL);
158 static void timer_tick(unsigned long data)
160 struct cardstate *cs = (struct cardstate *) data;
163 struct at_state_t *at_state;
166 spin_lock_irqsave(&cs->lock, flags);
168 for (channel = 0; channel < cs->channels; ++channel)
169 if (test_timeout(&cs->bcs[channel].at_state))
172 if (test_timeout(&cs->at_state))
175 list_for_each_entry(at_state, &cs->temp_at_states, list)
176 if (test_timeout(at_state))
180 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
182 gig_dbg(DEBUG_EVENT, "scheduling timeout");
183 tasklet_schedule(&cs->event_tasklet);
187 spin_unlock_irqrestore(&cs->lock, flags);
190 int gigaset_get_channel(struct bc_state *bcs)
194 spin_lock_irqsave(&bcs->cs->lock, flags);
195 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
196 gig_dbg(DEBUG_CHANNEL, "could not allocate channel %d",
198 spin_unlock_irqrestore(&bcs->cs->lock, flags);
203 gig_dbg(DEBUG_CHANNEL, "allocated channel %d", bcs->channel);
204 spin_unlock_irqrestore(&bcs->cs->lock, flags);
208 struct bc_state *gigaset_get_free_channel(struct cardstate *cs)
213 spin_lock_irqsave(&cs->lock, flags);
214 if (!try_module_get(cs->driver->owner)) {
215 gig_dbg(DEBUG_CHANNEL,
216 "could not get module for allocating channel");
217 spin_unlock_irqrestore(&cs->lock, flags);
220 for (i = 0; i < cs->channels; ++i)
221 if (!cs->bcs[i].use_count) {
222 ++cs->bcs[i].use_count;
224 spin_unlock_irqrestore(&cs->lock, flags);
225 gig_dbg(DEBUG_CHANNEL, "allocated channel %d", i);
228 module_put(cs->driver->owner);
229 spin_unlock_irqrestore(&cs->lock, flags);
230 gig_dbg(DEBUG_CHANNEL, "no free channel");
234 void gigaset_free_channel(struct bc_state *bcs)
238 spin_lock_irqsave(&bcs->cs->lock, flags);
240 gig_dbg(DEBUG_CHANNEL, "could not free channel %d",
242 spin_unlock_irqrestore(&bcs->cs->lock, flags);
247 module_put(bcs->cs->driver->owner);
248 gig_dbg(DEBUG_CHANNEL, "freed channel %d", bcs->channel);
249 spin_unlock_irqrestore(&bcs->cs->lock, flags);
252 int gigaset_get_channels(struct cardstate *cs)
257 spin_lock_irqsave(&cs->lock, flags);
258 for (i = 0; i < cs->channels; ++i)
259 if (cs->bcs[i].use_count) {
260 spin_unlock_irqrestore(&cs->lock, flags);
261 gig_dbg(DEBUG_CHANNEL,
262 "could not allocate all channels");
265 for (i = 0; i < cs->channels; ++i)
266 ++cs->bcs[i].use_count;
267 spin_unlock_irqrestore(&cs->lock, flags);
269 gig_dbg(DEBUG_CHANNEL, "allocated all channels");
274 void gigaset_free_channels(struct cardstate *cs)
279 gig_dbg(DEBUG_CHANNEL, "unblocking all channels");
280 spin_lock_irqsave(&cs->lock, flags);
281 for (i = 0; i < cs->channels; ++i)
282 --cs->bcs[i].use_count;
283 spin_unlock_irqrestore(&cs->lock, flags);
286 void gigaset_block_channels(struct cardstate *cs)
291 gig_dbg(DEBUG_CHANNEL, "blocking 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 static void clear_events(struct cardstate *cs)
304 spin_lock_irqsave(&cs->ev_lock, flags);
309 while (tail != head) {
310 ev = cs->events + head;
312 head = (head + 1) % MAX_EVENTS;
317 spin_unlock_irqrestore(&cs->ev_lock, flags);
321 * gigaset_add_event() - add event to device event queue
322 * @cs: device descriptor structure.
323 * @at_state: connection state structure.
325 * @ptr: pointer parameter for event.
326 * @parameter: integer parameter for event.
327 * @arg: pointer parameter for event.
329 * Allocate an event queue entry from the device's event queue, and set it up
330 * with the parameters given.
332 * Return value: added event
334 struct event_t *gigaset_add_event(struct cardstate *cs,
335 struct at_state_t *at_state, int type,
336 void *ptr, int parameter, void *arg)
340 struct event_t *event = NULL;
342 gig_dbg(DEBUG_EVENT, "queueing event %d", type);
344 spin_lock_irqsave(&cs->ev_lock, flags);
347 next = (tail + 1) % MAX_EVENTS;
348 if (unlikely(next == cs->ev_head))
349 dev_err(cs->dev, "event queue full\n");
351 event = cs->events + tail;
353 event->at_state = at_state;
357 event->parameter = parameter;
361 spin_unlock_irqrestore(&cs->ev_lock, flags);
365 EXPORT_SYMBOL_GPL(gigaset_add_event);
367 static void free_strings(struct at_state_t *at_state)
371 for (i = 0; i < STR_NUM; ++i) {
372 kfree(at_state->str_var[i]);
373 at_state->str_var[i] = NULL;
377 static void clear_at_state(struct at_state_t *at_state)
379 free_strings(at_state);
382 static void dealloc_at_states(struct cardstate *cs)
384 struct at_state_t *cur, *next;
386 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
387 list_del(&cur->list);
393 static void gigaset_freebcs(struct bc_state *bcs)
397 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
398 if (!bcs->cs->ops->freebcshw(bcs))
399 gig_dbg(DEBUG_INIT, "failed");
401 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
402 clear_at_state(&bcs->at_state);
403 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
404 dev_kfree_skb(bcs->skb);
407 for (i = 0; i < AT_NUM; ++i) {
408 kfree(bcs->commands[i]);
409 bcs->commands[i] = NULL;
413 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
417 struct cardstate *cs;
418 struct cardstate *ret = NULL;
420 spin_lock_irqsave(&drv->lock, flags);
423 for (i = 0; i < drv->minors; ++i) {
425 if (!(cs->flags & VALID_MINOR)) {
426 cs->flags = VALID_MINOR;
432 spin_unlock_irqrestore(&drv->lock, flags);
436 static void free_cs(struct cardstate *cs)
441 static void make_valid(struct cardstate *cs, unsigned mask)
444 struct gigaset_driver *drv = cs->driver;
445 spin_lock_irqsave(&drv->lock, flags);
447 spin_unlock_irqrestore(&drv->lock, flags);
450 static void make_invalid(struct cardstate *cs, unsigned mask)
453 struct gigaset_driver *drv = cs->driver;
454 spin_lock_irqsave(&drv->lock, flags);
456 spin_unlock_irqrestore(&drv->lock, flags);
460 * gigaset_freecs() - free all associated ressources of a device
461 * @cs: device descriptor structure.
463 * Stops all tasklets and timers, unregisters the device from all
464 * subsystems it was registered to, deallocates the device structure
465 * @cs and all structures referenced from it.
466 * Operations on the device should be stopped before calling this.
468 void gigaset_freecs(struct cardstate *cs)
476 mutex_lock(&cs->mutex);
483 spin_lock_irqsave(&cs->lock, flags);
485 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
486 not rescheduled below */
488 tasklet_kill(&cs->event_tasklet);
489 del_timer_sync(&cs->timer);
491 switch (cs->cs_init) {
493 /* clear B channel structures */
494 for (i = 0; i < cs->channels; ++i) {
495 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
496 gigaset_freebcs(cs->bcs + i);
499 /* clear device sysfs */
500 gigaset_free_dev_sysfs(cs);
504 gig_dbg(DEBUG_INIT, "clearing hw");
505 cs->ops->freecshw(cs);
508 case 2: /* error in initcshw */
509 /* Deregister from LL */
510 make_invalid(cs, VALID_ID);
511 gigaset_isdn_unregdev(cs);
514 case 1: /* error when registering to LL */
515 gig_dbg(DEBUG_INIT, "clearing at_state");
516 clear_at_state(&cs->at_state);
517 dealloc_at_states(cs);
520 case 0: /* error in basic setup */
522 gig_dbg(DEBUG_INIT, "freeing inbuf");
525 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
527 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
528 mutex_unlock(&cs->mutex);
531 EXPORT_SYMBOL_GPL(gigaset_freecs);
533 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
534 struct cardstate *cs, int cid)
538 INIT_LIST_HEAD(&at_state->list);
539 at_state->waiting = 0;
540 at_state->getstring = 0;
541 at_state->pending_commands = 0;
542 at_state->timer_expires = 0;
543 at_state->timer_active = 0;
544 at_state->timer_index = 0;
545 at_state->seq_index = 0;
546 at_state->ConState = 0;
547 for (i = 0; i < STR_NUM; ++i)
548 at_state->str_var[i] = NULL;
549 at_state->int_var[VAR_ZDLE] = 0;
550 at_state->int_var[VAR_ZCTP] = -1;
551 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
556 at_state->replystruct = cs->tabnocid;
558 at_state->replystruct = cs->tabcid;
562 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
563 /* inbuf->read must be allocated before! */
568 inbuf->inputstate = INS_command;
572 * gigaset_fill_inbuf() - append received data to input buffer
573 * @inbuf: buffer structure.
574 * @src: received data.
575 * @numbytes: number of bytes received.
577 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
580 unsigned n, head, tail, bytesleft;
582 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
587 bytesleft = numbytes;
590 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
596 n = (RBUFSIZE-1) - tail;
600 dev_err(inbuf->cs->dev,
601 "buffer overflow (%u bytes lost)\n",
607 memcpy(inbuf->data + tail, src, n);
609 tail = (tail + n) % RBUFSIZE;
612 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
614 return numbytes != bytesleft;
616 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
618 /* Initialize the b-channel structure */
619 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
620 struct cardstate *cs, int channel)
626 skb_queue_head_init(&bcs->squeue);
632 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
633 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
635 #ifdef CONFIG_GIGASET_DEBUG
639 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
640 bcs->fcs = PPP_INITFCS;
642 if (cs->ignoreframes) {
645 bcs->skb = dev_alloc_skb(SBUFSIZE + cs->hw_hdr_len);
646 if (bcs->skb != NULL)
647 skb_reserve(bcs->skb, cs->hw_hdr_len);
649 pr_err("out of memory\n");
652 bcs->channel = channel;
658 bcs->ignore = cs->ignoreframes;
660 for (i = 0; i < AT_NUM; ++i)
661 bcs->commands[i] = NULL;
663 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
664 if (cs->ops->initbcshw(bcs))
667 gig_dbg(DEBUG_INIT, " failed");
669 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
670 dev_kfree_skb(bcs->skb);
677 * gigaset_initcs() - initialize device structure
678 * @drv: hardware driver the device belongs to
679 * @channels: number of B channels supported by device
680 * @onechannel: !=0 if B channel data and AT commands share one
681 * communication channel (M10x),
682 * ==0 if B channels have separate communication channels (base)
683 * @ignoreframes: number of frames to ignore after setting up B channel
684 * @cidmode: !=0: start in CallID mode
685 * @modulename: name of driver module for LL registration
687 * Allocate and initialize cardstate structure for Gigaset driver
688 * Calls hardware dependent gigaset_initcshw() function
689 * Calls B channel initialization function gigaset_initbcs() for each B channel
692 * pointer to cardstate structure
694 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
695 int onechannel, int ignoreframes,
696 int cidmode, const char *modulename)
698 struct cardstate *cs;
702 gig_dbg(DEBUG_INIT, "allocating cs");
705 pr_err("maximum number of devices exceeded\n");
709 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
710 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
712 pr_err("out of memory\n");
715 gig_dbg(DEBUG_INIT, "allocating inbuf");
716 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
718 pr_err("out of memory\n");
723 cs->channels = channels;
724 cs->onechannel = onechannel;
725 cs->ignoreframes = ignoreframes;
726 INIT_LIST_HEAD(&cs->temp_at_states);
728 init_timer(&cs->timer); /* clear next & prev */
729 spin_lock_init(&cs->ev_lock);
733 tasklet_init(&cs->event_tasklet, gigaset_handle_event,
735 cs->commands_pending = 0;
742 cs->cidmode = cidmode != 0;
743 cs->tabnocid = gigaset_tab_nocid;
744 cs->tabcid = gigaset_tab_cid;
746 init_waitqueue_head(&cs->waitqueue);
749 cs->mode = M_UNKNOWN;
750 cs->mstate = MS_UNINITIALIZED;
754 gig_dbg(DEBUG_INIT, "setting up at_state");
755 spin_lock_init(&cs->lock);
756 gigaset_at_init(&cs->at_state, NULL, cs, 0);
760 gig_dbg(DEBUG_INIT, "setting up inbuf");
761 gigaset_inbuf_init(cs->inbuf, cs);
766 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
767 cs->cmdbuf = cs->lastcmdbuf = NULL;
768 spin_lock_init(&cs->cmdlock);
772 gig_dbg(DEBUG_INIT, "setting up iif");
773 if (!gigaset_isdn_regdev(cs, modulename)) {
774 pr_err("error registering ISDN device\n");
778 make_valid(cs, VALID_ID);
780 gig_dbg(DEBUG_INIT, "setting up hw");
781 if (!cs->ops->initcshw(cs))
786 /* set up character device */
789 /* set up device sysfs */
790 gigaset_init_dev_sysfs(cs);
792 /* set up channel data structures */
793 for (i = 0; i < channels; ++i) {
794 gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
795 if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
796 pr_err("could not allocate channel %d data\n", i);
801 spin_lock_irqsave(&cs->lock, flags);
803 spin_unlock_irqrestore(&cs->lock, flags);
804 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
805 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
806 /* FIXME: can jiffies increase too much until the timer is added?
807 * Same problem(?) with mod_timer() in timer_tick(). */
808 add_timer(&cs->timer);
810 gig_dbg(DEBUG_INIT, "cs initialized");
814 gig_dbg(DEBUG_INIT, "failed");
818 EXPORT_SYMBOL_GPL(gigaset_initcs);
820 /* ReInitialize the b-channel structure on hangup */
821 void gigaset_bcs_reinit(struct bc_state *bcs)
824 struct cardstate *cs = bcs->cs;
827 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
830 spin_lock_irqsave(&cs->lock, flags);
831 clear_at_state(&bcs->at_state);
832 bcs->at_state.ConState = 0;
833 bcs->at_state.timer_active = 0;
834 bcs->at_state.timer_expires = 0;
835 bcs->at_state.cid = -1; /* No CID defined */
836 spin_unlock_irqrestore(&cs->lock, flags);
840 #ifdef CONFIG_GIGASET_DEBUG
844 bcs->fcs = PPP_INITFCS;
847 bcs->ignore = cs->ignoreframes;
849 dev_kfree_skb(bcs->skb);
853 cs->ops->reinitbcshw(bcs);
856 static void cleanup_cs(struct cardstate *cs)
858 struct cmdbuf_t *cb, *tcb;
862 spin_lock_irqsave(&cs->lock, flags);
864 cs->mode = M_UNKNOWN;
865 cs->mstate = MS_UNINITIALIZED;
867 clear_at_state(&cs->at_state);
868 dealloc_at_states(cs);
869 free_strings(&cs->at_state);
870 gigaset_at_init(&cs->at_state, NULL, cs, 0);
872 cs->inbuf->inputstate = INS_command;
882 cs->cmdbuf = cs->lastcmdbuf = NULL;
888 cs->commands_pending = 0;
891 spin_unlock_irqrestore(&cs->lock, flags);
893 for (i = 0; i < cs->channels; ++i) {
894 gigaset_freebcs(cs->bcs + i);
895 if (!gigaset_initbcs(cs->bcs + i, cs, i))
896 pr_err("could not allocate channel %d data\n", i);
900 cs->cmd_result = -ENODEV;
902 wake_up_interruptible(&cs->waitqueue);
908 * gigaset_start() - start device operations
909 * @cs: device descriptor structure.
911 * Prepares the device for use by setting up communication parameters,
912 * scheduling an EV_START event to initiate device initialization, and
913 * waiting for completion of the initialization.
916 * 1 - success, 0 - error
918 int gigaset_start(struct cardstate *cs)
922 if (mutex_lock_interruptible(&cs->mutex))
925 spin_lock_irqsave(&cs->lock, flags);
927 spin_unlock_irqrestore(&cs->lock, flags);
929 if (cs->mstate != MS_LOCKED) {
930 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
931 cs->ops->baud_rate(cs, B115200);
932 cs->ops->set_line_ctrl(cs, CS8);
933 cs->control_state = TIOCM_DTR|TIOCM_RTS;
938 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
942 gigaset_schedule_event(cs);
944 wait_event(cs->waitqueue, !cs->waiting);
946 mutex_unlock(&cs->mutex);
950 mutex_unlock(&cs->mutex);
953 EXPORT_SYMBOL_GPL(gigaset_start);
956 * gigaset_shutdown() - shut down device operations
957 * @cs: device descriptor structure.
959 * Deactivates the device by scheduling an EV_SHUTDOWN event and
960 * waiting for completion of the shutdown.
963 * 0 - success, -1 - error (no device associated)
965 int gigaset_shutdown(struct cardstate *cs)
967 mutex_lock(&cs->mutex);
969 if (!(cs->flags & VALID_MINOR)) {
970 mutex_unlock(&cs->mutex);
976 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL))
978 gigaset_schedule_event(cs);
980 wait_event(cs->waitqueue, !cs->waiting);
985 mutex_unlock(&cs->mutex);
988 EXPORT_SYMBOL_GPL(gigaset_shutdown);
991 * gigaset_stop() - stop device operations
992 * @cs: device descriptor structure.
994 * Stops operations on the device by scheduling an EV_STOP event and
995 * waiting for completion of the shutdown.
997 void gigaset_stop(struct cardstate *cs)
999 mutex_lock(&cs->mutex);
1003 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL))
1005 gigaset_schedule_event(cs);
1007 wait_event(cs->waitqueue, !cs->waiting);
1012 mutex_unlock(&cs->mutex);
1014 EXPORT_SYMBOL_GPL(gigaset_stop);
1016 static LIST_HEAD(drivers);
1017 static DEFINE_SPINLOCK(driver_lock);
1019 struct cardstate *gigaset_get_cs_by_id(int id)
1021 unsigned long flags;
1022 struct cardstate *ret = NULL;
1023 struct cardstate *cs;
1024 struct gigaset_driver *drv;
1027 spin_lock_irqsave(&driver_lock, flags);
1028 list_for_each_entry(drv, &drivers, list) {
1029 spin_lock(&drv->lock);
1030 for (i = 0; i < drv->minors; ++i) {
1032 if ((cs->flags & VALID_ID) && cs->myid == id) {
1037 spin_unlock(&drv->lock);
1041 spin_unlock_irqrestore(&driver_lock, flags);
1045 void gigaset_debugdrivers(void)
1047 unsigned long flags;
1048 static struct cardstate *cs;
1049 struct gigaset_driver *drv;
1052 spin_lock_irqsave(&driver_lock, flags);
1053 list_for_each_entry(drv, &drivers, list) {
1054 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
1055 spin_lock(&drv->lock);
1056 for (i = 0; i < drv->minors; ++i) {
1057 gig_dbg(DEBUG_DRIVER, " index %u", i);
1059 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
1060 gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
1061 gig_dbg(DEBUG_DRIVER, " minor_index %u",
1063 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
1064 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
1066 spin_unlock(&drv->lock);
1068 spin_unlock_irqrestore(&driver_lock, flags);
1071 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1073 unsigned long flags;
1074 struct cardstate *ret = NULL;
1075 struct gigaset_driver *drv;
1078 spin_lock_irqsave(&driver_lock, flags);
1079 list_for_each_entry(drv, &drivers, list) {
1080 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1082 index = minor - drv->minor;
1083 spin_lock(&drv->lock);
1084 if (drv->cs[index].flags & VALID_MINOR)
1085 ret = drv->cs + index;
1086 spin_unlock(&drv->lock);
1090 spin_unlock_irqrestore(&driver_lock, flags);
1094 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1096 if (tty->index < 0 || tty->index >= tty->driver->num)
1098 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1102 * gigaset_freedriver() - free all associated ressources of a driver
1103 * @drv: driver descriptor structure.
1105 * Unregisters the driver from the system and deallocates the driver
1106 * structure @drv and all structures referenced from it.
1107 * All devices should be shut down before calling this.
1109 void gigaset_freedriver(struct gigaset_driver *drv)
1111 unsigned long flags;
1113 spin_lock_irqsave(&driver_lock, flags);
1114 list_del(&drv->list);
1115 spin_unlock_irqrestore(&driver_lock, flags);
1117 gigaset_if_freedriver(drv);
1122 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1125 * gigaset_initdriver() - initialize driver structure
1126 * @minor: First minor number
1127 * @minors: Number of minors this driver can handle
1128 * @procname: Name of the driver
1129 * @devname: Name of the device files (prefix without minor number)
1131 * Allocate and initialize gigaset_driver structure. Initialize interface.
1134 * Pointer to the gigaset_driver structure on success, NULL on failure.
1136 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1137 const char *procname,
1138 const char *devname,
1139 const struct gigaset_ops *ops,
1140 struct module *owner)
1142 struct gigaset_driver *drv;
1143 unsigned long flags;
1146 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1152 drv->minors = minors;
1153 spin_lock_init(&drv->lock);
1157 INIT_LIST_HEAD(&drv->list);
1159 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1163 for (i = 0; i < minors; ++i) {
1164 drv->cs[i].flags = 0;
1165 drv->cs[i].driver = drv;
1166 drv->cs[i].ops = drv->ops;
1167 drv->cs[i].minor_index = i;
1168 mutex_init(&drv->cs[i].mutex);
1171 gigaset_if_initdriver(drv, procname, devname);
1173 spin_lock_irqsave(&driver_lock, flags);
1174 list_add(&drv->list, &drivers);
1175 spin_unlock_irqrestore(&driver_lock, flags);
1184 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1187 * gigaset_blockdriver() - block driver
1188 * @drv: driver descriptor structure.
1190 * Prevents the driver from attaching new devices, in preparation for
1193 void gigaset_blockdriver(struct gigaset_driver *drv)
1197 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1199 static int __init gigaset_init_module(void)
1201 /* in accordance with the principle of least astonishment,
1202 * setting the 'debug' parameter to 1 activates a sensible
1203 * set of default debug levels
1205 if (gigaset_debuglevel == 1)
1206 gigaset_debuglevel = DEBUG_DEFAULT;
1208 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
1209 gigaset_isdn_regdrv();
1213 static void __exit gigaset_exit_module(void)
1215 gigaset_isdn_unregdrv();
1218 module_init(gigaset_init_module);
1219 module_exit(gigaset_exit_module);
1221 MODULE_AUTHOR(DRIVER_AUTHOR);
1222 MODULE_DESCRIPTION(DRIVER_DESC);
1224 MODULE_LICENSE("GPL");