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>
21 /* Version Information */
22 #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
23 #define DRIVER_DESC "Driver for Gigaset 307x"
25 #ifdef CONFIG_GIGASET_DEBUG
26 #define DRIVER_DESC_DEBUG " (debug build)"
28 #define DRIVER_DESC_DEBUG ""
31 /* Module parameters */
32 int gigaset_debuglevel = DEBUG_DEFAULT;
33 EXPORT_SYMBOL_GPL(gigaset_debuglevel);
34 module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
35 MODULE_PARM_DESC(debug, "debug level");
37 /* driver state flags */
38 #define VALID_MINOR 0x01
42 * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging
43 * @level: debugging level.
44 * @msg: message prefix.
45 * @len: number of bytes to dump.
48 * If the current debugging level includes one of the bits set in @level,
49 * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio,
50 * prefixed by the text @msg.
52 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
53 size_t len, const unsigned char *buf)
55 unsigned char outbuf[80];
57 size_t space = sizeof outbuf - 1;
58 unsigned char *out = outbuf;
63 if (c == '~' || c == '^' || c == '\\') {
74 if (c < 0x20 || c == 0x7f) {
86 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
88 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
90 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
94 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
95 cs->control_state = flags;
100 set_current_state(TASK_INTERRUPTIBLE);
101 schedule_timeout(delay * HZ / 1000);
107 int gigaset_enterconfigmode(struct cardstate *cs)
111 cs->control_state = TIOCM_RTS; //FIXME
113 r = setflags(cs, TIOCM_DTR, 200);
116 r = setflags(cs, 0, 200);
119 for (i = 0; i < 5; ++i) {
120 r = setflags(cs, TIOCM_RTS, 100);
123 r = setflags(cs, 0, 100);
127 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
134 dev_err(cs->dev, "error %d on setuartbits\n", -r);
135 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
136 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
141 static int test_timeout(struct at_state_t *at_state)
143 if (!at_state->timer_expires)
146 if (--at_state->timer_expires) {
147 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
148 at_state, at_state->timer_expires);
152 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
153 at_state->timer_index, NULL)) {
154 //FIXME what should we do?
160 static void timer_tick(unsigned long data)
162 struct cardstate *cs = (struct cardstate *) data;
165 struct at_state_t *at_state;
168 spin_lock_irqsave(&cs->lock, flags);
170 for (channel = 0; channel < cs->channels; ++channel)
171 if (test_timeout(&cs->bcs[channel].at_state))
174 if (test_timeout(&cs->at_state))
177 list_for_each_entry(at_state, &cs->temp_at_states, list)
178 if (test_timeout(at_state))
182 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
184 gig_dbg(DEBUG_CMD, "scheduling timeout");
185 tasklet_schedule(&cs->event_tasklet);
189 spin_unlock_irqrestore(&cs->lock, flags);
192 int gigaset_get_channel(struct bc_state *bcs)
196 spin_lock_irqsave(&bcs->cs->lock, flags);
197 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
198 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
200 spin_unlock_irqrestore(&bcs->cs->lock, flags);
205 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
206 spin_unlock_irqrestore(&bcs->cs->lock, flags);
210 void gigaset_free_channel(struct bc_state *bcs)
214 spin_lock_irqsave(&bcs->cs->lock, flags);
216 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
217 spin_unlock_irqrestore(&bcs->cs->lock, flags);
222 module_put(bcs->cs->driver->owner);
223 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
224 spin_unlock_irqrestore(&bcs->cs->lock, flags);
227 int gigaset_get_channels(struct cardstate *cs)
232 spin_lock_irqsave(&cs->lock, flags);
233 for (i = 0; i < cs->channels; ++i)
234 if (cs->bcs[i].use_count) {
235 spin_unlock_irqrestore(&cs->lock, flags);
236 gig_dbg(DEBUG_ANY, "could not allocate all channels");
239 for (i = 0; i < cs->channels; ++i)
240 ++cs->bcs[i].use_count;
241 spin_unlock_irqrestore(&cs->lock, flags);
243 gig_dbg(DEBUG_ANY, "allocated all channels");
248 void gigaset_free_channels(struct cardstate *cs)
253 gig_dbg(DEBUG_ANY, "unblocking all channels");
254 spin_lock_irqsave(&cs->lock, flags);
255 for (i = 0; i < cs->channels; ++i)
256 --cs->bcs[i].use_count;
257 spin_unlock_irqrestore(&cs->lock, flags);
260 void gigaset_block_channels(struct cardstate *cs)
265 gig_dbg(DEBUG_ANY, "blocking all channels");
266 spin_lock_irqsave(&cs->lock, flags);
267 for (i = 0; i < cs->channels; ++i)
268 ++cs->bcs[i].use_count;
269 spin_unlock_irqrestore(&cs->lock, flags);
272 static void clear_events(struct cardstate *cs)
278 spin_lock_irqsave(&cs->ev_lock, flags);
283 while (tail != head) {
284 ev = cs->events + head;
286 head = (head + 1) % MAX_EVENTS;
291 spin_unlock_irqrestore(&cs->ev_lock, flags);
295 * gigaset_add_event() - add event to device event queue
296 * @cs: device descriptor structure.
297 * @at_state: connection state structure.
299 * @ptr: pointer parameter for event.
300 * @parameter: integer parameter for event.
301 * @arg: pointer parameter for event.
303 * Allocate an event queue entry from the device's event queue, and set it up
304 * with the parameters given.
306 * Return value: added event
308 struct event_t *gigaset_add_event(struct cardstate *cs,
309 struct at_state_t *at_state, int type,
310 void *ptr, int parameter, void *arg)
314 struct event_t *event = NULL;
316 spin_lock_irqsave(&cs->ev_lock, flags);
319 next = (tail + 1) % MAX_EVENTS;
320 if (unlikely(next == cs->ev_head))
321 dev_err(cs->dev, "event queue full\n");
323 event = cs->events + tail;
325 event->at_state = at_state;
329 event->parameter = parameter;
333 spin_unlock_irqrestore(&cs->ev_lock, flags);
337 EXPORT_SYMBOL_GPL(gigaset_add_event);
339 static void free_strings(struct at_state_t *at_state)
343 for (i = 0; i < STR_NUM; ++i) {
344 kfree(at_state->str_var[i]);
345 at_state->str_var[i] = NULL;
349 static void clear_at_state(struct at_state_t *at_state)
351 free_strings(at_state);
354 static void dealloc_at_states(struct cardstate *cs)
356 struct at_state_t *cur, *next;
358 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
359 list_del(&cur->list);
365 static void gigaset_freebcs(struct bc_state *bcs)
369 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
370 if (!bcs->cs->ops->freebcshw(bcs)) {
371 gig_dbg(DEBUG_INIT, "failed");
374 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
375 clear_at_state(&bcs->at_state);
376 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
379 dev_kfree_skb(bcs->skb);
380 for (i = 0; i < AT_NUM; ++i) {
381 kfree(bcs->commands[i]);
382 bcs->commands[i] = NULL;
386 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
390 struct cardstate *cs;
391 struct cardstate *ret = NULL;
393 spin_lock_irqsave(&drv->lock, flags);
396 for (i = 0; i < drv->minors; ++i) {
398 if (!(cs->flags & VALID_MINOR)) {
399 cs->flags = VALID_MINOR;
405 spin_unlock_irqrestore(&drv->lock, flags);
409 static void free_cs(struct cardstate *cs)
414 static void make_valid(struct cardstate *cs, unsigned mask)
417 struct gigaset_driver *drv = cs->driver;
418 spin_lock_irqsave(&drv->lock, flags);
420 spin_unlock_irqrestore(&drv->lock, flags);
423 static void make_invalid(struct cardstate *cs, unsigned mask)
426 struct gigaset_driver *drv = cs->driver;
427 spin_lock_irqsave(&drv->lock, flags);
429 spin_unlock_irqrestore(&drv->lock, flags);
433 * gigaset_freecs() - free all associated ressources of a device
434 * @cs: device descriptor structure.
436 * Stops all tasklets and timers, unregisters the device from all
437 * subsystems it was registered to, deallocates the device structure
438 * @cs and all structures referenced from it.
439 * Operations on the device should be stopped before calling this.
441 void gigaset_freecs(struct cardstate *cs)
449 mutex_lock(&cs->mutex);
456 spin_lock_irqsave(&cs->lock, flags);
458 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
459 not rescheduled below */
461 tasklet_kill(&cs->event_tasklet);
462 del_timer_sync(&cs->timer);
464 switch (cs->cs_init) {
466 /* clear device sysfs */
467 gigaset_free_dev_sysfs(cs);
471 gig_dbg(DEBUG_INIT, "clearing hw");
472 cs->ops->freecshw(cs);
477 case 2: /* error in initcshw */
478 /* Deregister from LL */
479 make_invalid(cs, VALID_ID);
480 gig_dbg(DEBUG_INIT, "clearing iif");
481 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
484 case 1: /* error when regestering to LL */
485 gig_dbg(DEBUG_INIT, "clearing at_state");
486 clear_at_state(&cs->at_state);
487 dealloc_at_states(cs);
490 case 0: /* error in one call to initbcs */
491 for (i = 0; i < cs->channels; ++i) {
492 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
493 gigaset_freebcs(cs->bcs + i);
497 gig_dbg(DEBUG_INIT, "freeing inbuf");
500 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
502 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
503 mutex_unlock(&cs->mutex);
506 EXPORT_SYMBOL_GPL(gigaset_freecs);
508 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
509 struct cardstate *cs, int cid)
513 INIT_LIST_HEAD(&at_state->list);
514 at_state->waiting = 0;
515 at_state->getstring = 0;
516 at_state->pending_commands = 0;
517 at_state->timer_expires = 0;
518 at_state->timer_active = 0;
519 at_state->timer_index = 0;
520 at_state->seq_index = 0;
521 at_state->ConState = 0;
522 for (i = 0; i < STR_NUM; ++i)
523 at_state->str_var[i] = NULL;
524 at_state->int_var[VAR_ZDLE] = 0;
525 at_state->int_var[VAR_ZCTP] = -1;
526 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
531 at_state->replystruct = cs->tabnocid;
533 at_state->replystruct = cs->tabcid;
537 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
538 struct cardstate *cs, int inputstate)
539 /* inbuf->read must be allocated before! */
544 inbuf->bcs = bcs; /*base driver: NULL*/
545 inbuf->rcvbuf = NULL;
546 inbuf->inputstate = inputstate;
550 * gigaset_fill_inbuf() - append received data to input buffer
551 * @inbuf: buffer structure.
552 * @src: received data.
553 * @numbytes: number of bytes received.
555 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
558 unsigned n, head, tail, bytesleft;
560 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
565 bytesleft = numbytes;
568 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
574 n = (RBUFSIZE-1) - tail;
578 dev_err(inbuf->cs->dev,
579 "buffer overflow (%u bytes lost)\n",
585 memcpy(inbuf->data + tail, src, n);
587 tail = (tail + n) % RBUFSIZE;
590 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
592 return numbytes != bytesleft;
594 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
596 /* Initialize the b-channel structure */
597 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
598 struct cardstate *cs, int channel)
602 bcs->tx_skb = NULL; //FIXME -> hw part
604 skb_queue_head_init(&bcs->squeue);
610 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
611 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
613 #ifdef CONFIG_GIGASET_DEBUG
617 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
618 bcs->fcs = PPP_INITFCS;
620 if (cs->ignoreframes) {
621 bcs->inputstate |= INS_skip_frame;
623 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
624 skb_reserve(bcs->skb, HW_HDR_LEN);
626 pr_err("out of memory\n");
627 bcs->inputstate |= INS_skip_frame;
630 bcs->channel = channel;
636 bcs->ignore = cs->ignoreframes;
638 for (i = 0; i < AT_NUM; ++i)
639 bcs->commands[i] = NULL;
641 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
642 if (cs->ops->initbcshw(bcs))
645 gig_dbg(DEBUG_INIT, " failed");
647 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
649 dev_kfree_skb(bcs->skb);
655 * gigaset_initcs() - initialize device structure
656 * @drv: hardware driver the device belongs to
657 * @channels: number of B channels supported by device
658 * @onechannel: !=0 if B channel data and AT commands share one
659 * communication channel (M10x),
660 * ==0 if B channels have separate communication channels (base)
661 * @ignoreframes: number of frames to ignore after setting up B channel
662 * @cidmode: !=0: start in CallID mode
663 * @modulename: name of driver module for LL registration
665 * Allocate and initialize cardstate structure for Gigaset driver
666 * Calls hardware dependent gigaset_initcshw() function
667 * Calls B channel initialization function gigaset_initbcs() for each B channel
670 * pointer to cardstate structure
672 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
673 int onechannel, int ignoreframes,
674 int cidmode, const char *modulename)
676 struct cardstate *cs = NULL;
680 gig_dbg(DEBUG_INIT, "allocating cs");
681 if (!(cs = alloc_cs(drv))) {
682 pr_err("maximum number of devices exceeded\n");
686 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
687 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
689 pr_err("out of memory\n");
692 gig_dbg(DEBUG_INIT, "allocating inbuf");
693 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
695 pr_err("out of memory\n");
700 cs->channels = channels;
701 cs->onechannel = onechannel;
702 cs->ignoreframes = ignoreframes;
703 INIT_LIST_HEAD(&cs->temp_at_states);
705 init_timer(&cs->timer); /* clear next & prev */
706 spin_lock_init(&cs->ev_lock);
710 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
712 cs->commands_pending = 0;
719 cs->cidmode = cidmode != 0;
720 cs->tabnocid = gigaset_tab_nocid;
721 cs->tabcid = gigaset_tab_cid;
723 init_waitqueue_head(&cs->waitqueue);
726 cs->mode = M_UNKNOWN;
727 cs->mstate = MS_UNINITIALIZED;
729 for (i = 0; i < channels; ++i) {
730 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
731 if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
732 pr_err("could not allocate channel %d data\n", i);
739 gig_dbg(DEBUG_INIT, "setting up at_state");
740 spin_lock_init(&cs->lock);
741 gigaset_at_init(&cs->at_state, NULL, cs, 0);
745 gig_dbg(DEBUG_INIT, "setting up inbuf");
746 if (onechannel) { //FIXME distinction necessary?
747 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
749 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
754 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
755 cs->cmdbuf = cs->lastcmdbuf = NULL;
756 spin_lock_init(&cs->cmdlock);
760 gig_dbg(DEBUG_INIT, "setting up iif");
761 if (!gigaset_register_to_LL(cs, modulename)) {
762 pr_err("error registering ISDN device\n");
766 make_valid(cs, VALID_ID);
768 gig_dbg(DEBUG_INIT, "setting up hw");
769 if (!cs->ops->initcshw(cs))
774 /* set up character device */
777 /* set up device sysfs */
778 gigaset_init_dev_sysfs(cs);
780 spin_lock_irqsave(&cs->lock, flags);
782 spin_unlock_irqrestore(&cs->lock, flags);
783 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
784 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
785 /* FIXME: can jiffies increase too much until the timer is added?
786 * Same problem(?) with mod_timer() in timer_tick(). */
787 add_timer(&cs->timer);
789 gig_dbg(DEBUG_INIT, "cs initialized");
793 gig_dbg(DEBUG_INIT, "failed");
797 EXPORT_SYMBOL_GPL(gigaset_initcs);
799 /* ReInitialize the b-channel structure on hangup */
800 void gigaset_bcs_reinit(struct bc_state *bcs)
803 struct cardstate *cs = bcs->cs;
806 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
809 spin_lock_irqsave(&cs->lock, flags);
810 clear_at_state(&bcs->at_state);
811 bcs->at_state.ConState = 0;
812 bcs->at_state.timer_active = 0;
813 bcs->at_state.timer_expires = 0;
814 bcs->at_state.cid = -1; /* No CID defined */
815 spin_unlock_irqrestore(&cs->lock, flags);
819 #ifdef CONFIG_GIGASET_DEBUG
823 bcs->fcs = PPP_INITFCS;
826 bcs->ignore = cs->ignoreframes;
828 bcs->inputstate |= INS_skip_frame;
831 cs->ops->reinitbcshw(bcs);
834 static void cleanup_cs(struct cardstate *cs)
836 struct cmdbuf_t *cb, *tcb;
840 spin_lock_irqsave(&cs->lock, flags);
842 cs->mode = M_UNKNOWN;
843 cs->mstate = MS_UNINITIALIZED;
845 clear_at_state(&cs->at_state);
846 dealloc_at_states(cs);
847 free_strings(&cs->at_state);
848 gigaset_at_init(&cs->at_state, NULL, cs, 0);
850 kfree(cs->inbuf->rcvbuf);
851 cs->inbuf->rcvbuf = NULL;
852 cs->inbuf->inputstate = INS_command;
862 cs->cmdbuf = cs->lastcmdbuf = NULL;
868 cs->commands_pending = 0;
871 spin_unlock_irqrestore(&cs->lock, flags);
873 for (i = 0; i < cs->channels; ++i) {
874 gigaset_freebcs(cs->bcs + i);
875 if (!gigaset_initbcs(cs->bcs + i, cs, i))
876 pr_err("could not allocate channel %d data\n", i);
880 cs->cmd_result = -ENODEV;
882 wake_up_interruptible(&cs->waitqueue);
888 * gigaset_start() - start device operations
889 * @cs: device descriptor structure.
891 * Prepares the device for use by setting up communication parameters,
892 * scheduling an EV_START event to initiate device initialization, and
893 * waiting for completion of the initialization.
896 * 1 - success, 0 - error
898 int gigaset_start(struct cardstate *cs)
902 if (mutex_lock_interruptible(&cs->mutex))
905 spin_lock_irqsave(&cs->lock, flags);
907 spin_unlock_irqrestore(&cs->lock, flags);
909 if (cs->mstate != MS_LOCKED) {
910 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
911 cs->ops->baud_rate(cs, B115200);
912 cs->ops->set_line_ctrl(cs, CS8);
913 cs->control_state = TIOCM_DTR|TIOCM_RTS;
915 //FIXME use some saved values?
920 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
922 //FIXME what should we do?
926 gig_dbg(DEBUG_CMD, "scheduling START");
927 gigaset_schedule_event(cs);
929 wait_event(cs->waitqueue, !cs->waiting);
931 mutex_unlock(&cs->mutex);
935 mutex_unlock(&cs->mutex);
938 EXPORT_SYMBOL_GPL(gigaset_start);
941 * gigaset_shutdown() - shut down device operations
942 * @cs: device descriptor structure.
944 * Deactivates the device by scheduling an EV_SHUTDOWN event and
945 * waiting for completion of the shutdown.
948 * 0 - success, -1 - error (no device associated)
950 int gigaset_shutdown(struct cardstate *cs)
952 mutex_lock(&cs->mutex);
954 if (!(cs->flags & VALID_MINOR)) {
955 mutex_unlock(&cs->mutex);
961 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
962 //FIXME what should we do?
966 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
967 gigaset_schedule_event(cs);
969 wait_event(cs->waitqueue, !cs->waiting);
974 mutex_unlock(&cs->mutex);
977 EXPORT_SYMBOL_GPL(gigaset_shutdown);
980 * gigaset_stop() - stop device operations
981 * @cs: device descriptor structure.
983 * Stops operations on the device by scheduling an EV_STOP event and
984 * waiting for completion of the shutdown.
986 void gigaset_stop(struct cardstate *cs)
988 mutex_lock(&cs->mutex);
992 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
993 //FIXME what should we do?
997 gig_dbg(DEBUG_CMD, "scheduling STOP");
998 gigaset_schedule_event(cs);
1000 wait_event(cs->waitqueue, !cs->waiting);
1005 mutex_unlock(&cs->mutex);
1007 EXPORT_SYMBOL_GPL(gigaset_stop);
1009 static LIST_HEAD(drivers);
1010 static DEFINE_SPINLOCK(driver_lock);
1012 struct cardstate *gigaset_get_cs_by_id(int id)
1014 unsigned long flags;
1015 struct cardstate *ret = NULL;
1016 struct cardstate *cs;
1017 struct gigaset_driver *drv;
1020 spin_lock_irqsave(&driver_lock, flags);
1021 list_for_each_entry(drv, &drivers, list) {
1022 spin_lock(&drv->lock);
1023 for (i = 0; i < drv->minors; ++i) {
1025 if ((cs->flags & VALID_ID) && cs->myid == id) {
1030 spin_unlock(&drv->lock);
1034 spin_unlock_irqrestore(&driver_lock, flags);
1038 void gigaset_debugdrivers(void)
1040 unsigned long flags;
1041 static struct cardstate *cs;
1042 struct gigaset_driver *drv;
1045 spin_lock_irqsave(&driver_lock, flags);
1046 list_for_each_entry(drv, &drivers, list) {
1047 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
1048 spin_lock(&drv->lock);
1049 for (i = 0; i < drv->minors; ++i) {
1050 gig_dbg(DEBUG_DRIVER, " index %u", i);
1052 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
1053 gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
1054 gig_dbg(DEBUG_DRIVER, " minor_index %u",
1056 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
1057 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
1059 spin_unlock(&drv->lock);
1061 spin_unlock_irqrestore(&driver_lock, flags);
1064 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1066 unsigned long flags;
1067 struct cardstate *ret = NULL;
1068 struct gigaset_driver *drv;
1071 spin_lock_irqsave(&driver_lock, flags);
1072 list_for_each_entry(drv, &drivers, list) {
1073 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1075 index = minor - drv->minor;
1076 spin_lock(&drv->lock);
1077 if (drv->cs[index].flags & VALID_MINOR)
1078 ret = drv->cs + index;
1079 spin_unlock(&drv->lock);
1083 spin_unlock_irqrestore(&driver_lock, flags);
1087 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1089 if (tty->index < 0 || tty->index >= tty->driver->num)
1091 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1095 * gigaset_freedriver() - free all associated ressources of a driver
1096 * @drv: driver descriptor structure.
1098 * Unregisters the driver from the system and deallocates the driver
1099 * structure @drv and all structures referenced from it.
1100 * All devices should be shut down before calling this.
1102 void gigaset_freedriver(struct gigaset_driver *drv)
1104 unsigned long flags;
1106 spin_lock_irqsave(&driver_lock, flags);
1107 list_del(&drv->list);
1108 spin_unlock_irqrestore(&driver_lock, flags);
1110 gigaset_if_freedriver(drv);
1115 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1118 * gigaset_initdriver() - initialize driver structure
1119 * @minor: First minor number
1120 * @minors: Number of minors this driver can handle
1121 * @procname: Name of the driver
1122 * @devname: Name of the device files (prefix without minor number)
1124 * Allocate and initialize gigaset_driver structure. Initialize interface.
1127 * Pointer to the gigaset_driver structure on success, NULL on failure.
1129 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1130 const char *procname,
1131 const char *devname,
1132 const struct gigaset_ops *ops,
1133 struct module *owner)
1135 struct gigaset_driver *drv;
1136 unsigned long flags;
1139 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1145 drv->minors = minors;
1146 spin_lock_init(&drv->lock);
1150 INIT_LIST_HEAD(&drv->list);
1152 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1156 for (i = 0; i < minors; ++i) {
1157 drv->cs[i].flags = 0;
1158 drv->cs[i].driver = drv;
1159 drv->cs[i].ops = drv->ops;
1160 drv->cs[i].minor_index = i;
1161 mutex_init(&drv->cs[i].mutex);
1164 gigaset_if_initdriver(drv, procname, devname);
1166 spin_lock_irqsave(&driver_lock, flags);
1167 list_add(&drv->list, &drivers);
1168 spin_unlock_irqrestore(&driver_lock, flags);
1177 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1180 * gigaset_blockdriver() - block driver
1181 * @drv: driver descriptor structure.
1183 * Prevents the driver from attaching new devices, in preparation for
1186 void gigaset_blockdriver(struct gigaset_driver *drv)
1190 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1192 static int __init gigaset_init_module(void)
1194 /* in accordance with the principle of least astonishment,
1195 * setting the 'debug' parameter to 1 activates a sensible
1196 * set of default debug levels
1198 if (gigaset_debuglevel == 1)
1199 gigaset_debuglevel = DEBUG_DEFAULT;
1201 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
1205 static void __exit gigaset_exit_module(void)
1209 module_init(gigaset_init_module);
1210 module_exit(gigaset_exit_module);
1212 MODULE_AUTHOR(DRIVER_AUTHOR);
1213 MODULE_DESCRIPTION(DRIVER_DESC);
1215 MODULE_LICENSE("GPL");