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 /* 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 /* driver state flags */
32 #define VALID_MINOR 0x01
36 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
37 size_t len, const unsigned char *buf)
39 unsigned char outbuf[80];
41 size_t space = sizeof outbuf - 1;
42 unsigned char *out = outbuf;
47 if (c == '~' || c == '^' || c == '\\') {
58 if (c < 0x20 || c == 0x7f) {
70 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
72 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
74 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
78 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
79 cs->control_state = flags;
84 set_current_state(TASK_INTERRUPTIBLE);
85 schedule_timeout(delay * HZ / 1000);
91 int gigaset_enterconfigmode(struct cardstate *cs)
95 cs->control_state = TIOCM_RTS; //FIXME
97 r = setflags(cs, TIOCM_DTR, 200);
100 r = setflags(cs, 0, 200);
103 for (i = 0; i < 5; ++i) {
104 r = setflags(cs, TIOCM_RTS, 100);
107 r = setflags(cs, 0, 100);
111 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
118 dev_err(cs->dev, "error %d on setuartbits\n", -r);
119 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
120 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
125 static int test_timeout(struct at_state_t *at_state)
127 if (!at_state->timer_expires)
130 if (--at_state->timer_expires) {
131 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
132 at_state, at_state->timer_expires);
136 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
137 at_state->timer_index, NULL)) {
138 //FIXME what should we do?
144 static void timer_tick(unsigned long data)
146 struct cardstate *cs = (struct cardstate *) data;
149 struct at_state_t *at_state;
152 spin_lock_irqsave(&cs->lock, flags);
154 for (channel = 0; channel < cs->channels; ++channel)
155 if (test_timeout(&cs->bcs[channel].at_state))
158 if (test_timeout(&cs->at_state))
161 list_for_each_entry(at_state, &cs->temp_at_states, list)
162 if (test_timeout(at_state))
166 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
168 gig_dbg(DEBUG_CMD, "scheduling timeout");
169 tasklet_schedule(&cs->event_tasklet);
173 spin_unlock_irqrestore(&cs->lock, flags);
176 int gigaset_get_channel(struct bc_state *bcs)
180 spin_lock_irqsave(&bcs->cs->lock, flags);
181 if (bcs->use_count) {
182 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
184 spin_unlock_irqrestore(&bcs->cs->lock, flags);
189 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
190 spin_unlock_irqrestore(&bcs->cs->lock, flags);
194 void gigaset_free_channel(struct bc_state *bcs)
198 spin_lock_irqsave(&bcs->cs->lock, flags);
200 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
201 spin_unlock_irqrestore(&bcs->cs->lock, flags);
206 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
207 spin_unlock_irqrestore(&bcs->cs->lock, flags);
210 int gigaset_get_channels(struct cardstate *cs)
215 spin_lock_irqsave(&cs->lock, flags);
216 for (i = 0; i < cs->channels; ++i)
217 if (cs->bcs[i].use_count) {
218 spin_unlock_irqrestore(&cs->lock, flags);
219 gig_dbg(DEBUG_ANY, "could not allocate all channels");
222 for (i = 0; i < cs->channels; ++i)
223 ++cs->bcs[i].use_count;
224 spin_unlock_irqrestore(&cs->lock, flags);
226 gig_dbg(DEBUG_ANY, "allocated all channels");
231 void gigaset_free_channels(struct cardstate *cs)
236 gig_dbg(DEBUG_ANY, "unblocking all channels");
237 spin_lock_irqsave(&cs->lock, flags);
238 for (i = 0; i < cs->channels; ++i)
239 --cs->bcs[i].use_count;
240 spin_unlock_irqrestore(&cs->lock, flags);
243 void gigaset_block_channels(struct cardstate *cs)
248 gig_dbg(DEBUG_ANY, "blocking all channels");
249 spin_lock_irqsave(&cs->lock, flags);
250 for (i = 0; i < cs->channels; ++i)
251 ++cs->bcs[i].use_count;
252 spin_unlock_irqrestore(&cs->lock, flags);
255 static void clear_events(struct cardstate *cs)
261 spin_lock_irqsave(&cs->ev_lock, flags);
266 while (tail != head) {
267 ev = cs->events + head;
269 head = (head + 1) % MAX_EVENTS;
274 spin_unlock_irqrestore(&cs->ev_lock, flags);
277 struct event_t *gigaset_add_event(struct cardstate *cs,
278 struct at_state_t *at_state, int type,
279 void *ptr, int parameter, void *arg)
283 struct event_t *event = NULL;
285 spin_lock_irqsave(&cs->ev_lock, flags);
288 next = (tail + 1) % MAX_EVENTS;
289 if (unlikely(next == cs->ev_head))
290 err("event queue full");
292 event = cs->events + tail;
294 event->at_state = at_state;
298 event->parameter = parameter;
302 spin_unlock_irqrestore(&cs->ev_lock, flags);
306 EXPORT_SYMBOL_GPL(gigaset_add_event);
308 static void free_strings(struct at_state_t *at_state)
312 for (i = 0; i < STR_NUM; ++i) {
313 kfree(at_state->str_var[i]);
314 at_state->str_var[i] = NULL;
318 static void clear_at_state(struct at_state_t *at_state)
320 free_strings(at_state);
323 static void dealloc_at_states(struct cardstate *cs)
325 struct at_state_t *cur, *next;
327 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
328 list_del(&cur->list);
334 static void gigaset_freebcs(struct bc_state *bcs)
338 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
339 if (!bcs->cs->ops->freebcshw(bcs)) {
340 gig_dbg(DEBUG_INIT, "failed");
343 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
344 clear_at_state(&bcs->at_state);
345 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
348 dev_kfree_skb(bcs->skb);
349 for (i = 0; i < AT_NUM; ++i) {
350 kfree(bcs->commands[i]);
351 bcs->commands[i] = NULL;
355 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
359 struct cardstate *ret = NULL;
361 spin_lock_irqsave(&drv->lock, flags);
362 for (i = 0; i < drv->minors; ++i) {
363 if (!(drv->flags[i] & VALID_MINOR)) {
364 if (try_module_get(drv->owner)) {
365 drv->flags[i] = VALID_MINOR;
371 spin_unlock_irqrestore(&drv->lock, flags);
375 static void free_cs(struct cardstate *cs)
378 struct gigaset_driver *drv = cs->driver;
379 spin_lock_irqsave(&drv->lock, flags);
380 if (drv->flags[cs->minor_index] & VALID_MINOR)
381 module_put(drv->owner);
382 drv->flags[cs->minor_index] = 0;
383 spin_unlock_irqrestore(&drv->lock, flags);
386 static void make_valid(struct cardstate *cs, unsigned mask)
389 struct gigaset_driver *drv = cs->driver;
390 spin_lock_irqsave(&drv->lock, flags);
391 drv->flags[cs->minor_index] |= mask;
392 spin_unlock_irqrestore(&drv->lock, flags);
395 static void make_invalid(struct cardstate *cs, unsigned mask)
398 struct gigaset_driver *drv = cs->driver;
399 spin_lock_irqsave(&drv->lock, flags);
400 drv->flags[cs->minor_index] &= ~mask;
401 spin_unlock_irqrestore(&drv->lock, flags);
404 void gigaset_freecs(struct cardstate *cs)
412 mutex_lock(&cs->mutex);
419 spin_lock_irqsave(&cs->lock, flags);
421 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
422 not rescheduled below */
424 tasklet_kill(&cs->event_tasklet);
425 del_timer_sync(&cs->timer);
427 switch (cs->cs_init) {
429 /* clear device sysfs */
430 gigaset_free_dev_sysfs(cs);
434 gig_dbg(DEBUG_INIT, "clearing hw");
435 cs->ops->freecshw(cs);
440 case 2: /* error in initcshw */
441 /* Deregister from LL */
442 make_invalid(cs, VALID_ID);
443 gig_dbg(DEBUG_INIT, "clearing iif");
444 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
447 case 1: /* error when regestering to LL */
448 gig_dbg(DEBUG_INIT, "clearing at_state");
449 clear_at_state(&cs->at_state);
450 dealloc_at_states(cs);
453 case 0: /* error in one call to initbcs */
454 for (i = 0; i < cs->channels; ++i) {
455 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
456 gigaset_freebcs(cs->bcs + i);
460 gig_dbg(DEBUG_INIT, "freeing inbuf");
463 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
465 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
466 mutex_unlock(&cs->mutex);
469 EXPORT_SYMBOL_GPL(gigaset_freecs);
471 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
472 struct cardstate *cs, int cid)
476 INIT_LIST_HEAD(&at_state->list);
477 at_state->waiting = 0;
478 at_state->getstring = 0;
479 at_state->pending_commands = 0;
480 at_state->timer_expires = 0;
481 at_state->timer_active = 0;
482 at_state->timer_index = 0;
483 at_state->seq_index = 0;
484 at_state->ConState = 0;
485 for (i = 0; i < STR_NUM; ++i)
486 at_state->str_var[i] = NULL;
487 at_state->int_var[VAR_ZDLE] = 0;
488 at_state->int_var[VAR_ZCTP] = -1;
489 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
494 at_state->replystruct = cs->tabnocid;
496 at_state->replystruct = cs->tabcid;
500 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
501 struct cardstate *cs, int inputstate)
502 /* inbuf->read must be allocated before! */
504 atomic_set(&inbuf->head, 0);
505 atomic_set(&inbuf->tail, 0);
507 inbuf->bcs = bcs; /*base driver: NULL*/
508 inbuf->rcvbuf = NULL; //FIXME
509 inbuf->inputstate = inputstate;
512 /* append received bytes to inbuf */
513 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
516 unsigned n, head, tail, bytesleft;
518 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
523 bytesleft = numbytes;
524 tail = atomic_read(&inbuf->tail);
525 head = atomic_read(&inbuf->head);
526 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
532 n = (RBUFSIZE-1) - tail;
536 dev_err(inbuf->cs->dev,
537 "buffer overflow (%u bytes lost)", bytesleft);
542 memcpy(inbuf->data + tail, src, n);
544 tail = (tail + n) % RBUFSIZE;
547 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
548 atomic_set(&inbuf->tail, tail);
549 return numbytes != bytesleft;
551 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
553 /* Initialize the b-channel structure */
554 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
555 struct cardstate *cs, int channel)
559 bcs->tx_skb = NULL; //FIXME -> hw part
561 skb_queue_head_init(&bcs->squeue);
567 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
568 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
572 #ifdef CONFIG_GIGASET_DEBUG
576 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
577 bcs->fcs = PPP_INITFCS;
579 if (cs->ignoreframes) {
580 bcs->inputstate |= INS_skip_frame;
582 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
583 skb_reserve(bcs->skb, HW_HDR_LEN);
585 warn("could not allocate skb");
586 bcs->inputstate |= INS_skip_frame;
589 bcs->channel = channel;
595 bcs->ignore = cs->ignoreframes;
597 for (i = 0; i < AT_NUM; ++i)
598 bcs->commands[i] = NULL;
600 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
601 if (cs->ops->initbcshw(bcs))
604 gig_dbg(DEBUG_INIT, " failed");
606 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
608 dev_kfree_skb(bcs->skb);
614 * Allocate and initialize cardstate structure for Gigaset driver
615 * Calls hardware dependent gigaset_initcshw() function
616 * Calls B channel initialization function gigaset_initbcs() for each B channel
618 * drv hardware driver the device belongs to
619 * channels number of B channels supported by device
620 * onechannel !=0: B channel data and AT commands share one
621 * communication channel
622 * ==0: B channels have separate communication channels
623 * ignoreframes number of frames to ignore after setting up B channel
624 * cidmode !=0: start in CallID mode
625 * modulename name of driver module (used for I4L registration)
627 * pointer to cardstate structure
629 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
630 int onechannel, int ignoreframes,
631 int cidmode, const char *modulename)
633 struct cardstate *cs = NULL;
637 gig_dbg(DEBUG_INIT, "allocating cs");
638 if (!(cs = alloc_cs(drv))) {
639 err("maximum number of devices exceeded");
642 mutex_init(&cs->mutex);
644 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
645 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
647 err("out of memory");
650 gig_dbg(DEBUG_INIT, "allocating inbuf");
651 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
653 err("out of memory");
658 cs->channels = channels;
659 cs->onechannel = onechannel;
660 cs->ignoreframes = ignoreframes;
661 INIT_LIST_HEAD(&cs->temp_at_states);
663 init_timer(&cs->timer); /* clear next & prev */
664 spin_lock_init(&cs->ev_lock);
668 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
670 atomic_set(&cs->commands_pending, 0);
677 cs->cidmode = cidmode != 0;
679 //if(onechannel) { //FIXME
680 cs->tabnocid = gigaset_tab_nocid_m10x;
681 cs->tabcid = gigaset_tab_cid_m10x;
683 // cs->tabnocid = gigaset_tab_nocid;
684 // cs->tabcid = gigaset_tab_cid;
687 init_waitqueue_head(&cs->waitqueue);
690 atomic_set(&cs->mode, M_UNKNOWN);
691 atomic_set(&cs->mstate, MS_UNINITIALIZED);
693 for (i = 0; i < channels; ++i) {
694 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
695 if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
696 err("could not allocate channel %d data", i);
703 gig_dbg(DEBUG_INIT, "setting up at_state");
704 spin_lock_init(&cs->lock);
705 gigaset_at_init(&cs->at_state, NULL, cs, 0);
709 gig_dbg(DEBUG_INIT, "setting up inbuf");
710 if (onechannel) { //FIXME distinction necessary?
711 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
713 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
718 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
719 cs->cmdbuf = cs->lastcmdbuf = NULL;
720 spin_lock_init(&cs->cmdlock);
724 gig_dbg(DEBUG_INIT, "setting up iif");
725 if (!gigaset_register_to_LL(cs, modulename)) {
726 err("register_isdn failed");
730 make_valid(cs, VALID_ID);
732 gig_dbg(DEBUG_INIT, "setting up hw");
733 if (!cs->ops->initcshw(cs)) {
734 err("could not allocate device specific data");
740 /* set up character device */
743 /* set up device sysfs */
744 gigaset_init_dev_sysfs(cs);
746 spin_lock_irqsave(&cs->lock, flags);
748 spin_unlock_irqrestore(&cs->lock, flags);
749 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
750 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
751 /* FIXME: can jiffies increase too much until the timer is added?
752 * Same problem(?) with mod_timer() in timer_tick(). */
753 add_timer(&cs->timer);
755 gig_dbg(DEBUG_INIT, "cs initialized");
759 gig_dbg(DEBUG_INIT, "failed");
763 EXPORT_SYMBOL_GPL(gigaset_initcs);
765 /* ReInitialize the b-channel structure on hangup */
766 void gigaset_bcs_reinit(struct bc_state *bcs)
769 struct cardstate *cs = bcs->cs;
772 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
775 spin_lock_irqsave(&cs->lock, flags);
776 clear_at_state(&bcs->at_state);
777 bcs->at_state.ConState = 0;
778 bcs->at_state.timer_active = 0;
779 bcs->at_state.timer_expires = 0;
780 bcs->at_state.cid = -1; /* No CID defined */
781 spin_unlock_irqrestore(&cs->lock, flags);
785 #ifdef CONFIG_GIGASET_DEBUG
789 bcs->fcs = PPP_INITFCS;
792 bcs->ignore = cs->ignoreframes;
794 bcs->inputstate |= INS_skip_frame;
797 cs->ops->reinitbcshw(bcs);
800 static void cleanup_cs(struct cardstate *cs)
802 struct cmdbuf_t *cb, *tcb;
806 spin_lock_irqsave(&cs->lock, flags);
808 atomic_set(&cs->mode, M_UNKNOWN);
809 atomic_set(&cs->mstate, MS_UNINITIALIZED);
811 clear_at_state(&cs->at_state);
812 dealloc_at_states(cs);
813 free_strings(&cs->at_state);
814 gigaset_at_init(&cs->at_state, NULL, cs, 0);
816 kfree(cs->inbuf->rcvbuf);
817 cs->inbuf->rcvbuf = NULL;
818 cs->inbuf->inputstate = INS_command;
819 atomic_set(&cs->inbuf->head, 0);
820 atomic_set(&cs->inbuf->tail, 0);
828 cs->cmdbuf = cs->lastcmdbuf = NULL;
834 atomic_set(&cs->commands_pending, 0);
837 spin_unlock_irqrestore(&cs->lock, flags);
839 for (i = 0; i < cs->channels; ++i) {
840 gigaset_freebcs(cs->bcs + i);
841 if (!gigaset_initbcs(cs->bcs + i, cs, i))
842 break; //FIXME error handling
846 cs->cmd_result = -ENODEV;
848 wake_up_interruptible(&cs->waitqueue);
853 int gigaset_start(struct cardstate *cs)
857 if (mutex_lock_interruptible(&cs->mutex))
860 spin_lock_irqsave(&cs->lock, flags);
862 spin_unlock_irqrestore(&cs->lock, flags);
864 if (atomic_read(&cs->mstate) != MS_LOCKED) {
865 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
866 cs->ops->baud_rate(cs, B115200);
867 cs->ops->set_line_ctrl(cs, CS8);
868 cs->control_state = TIOCM_DTR|TIOCM_RTS;
870 //FIXME use some saved values?
875 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
877 //FIXME what should we do?
881 gig_dbg(DEBUG_CMD, "scheduling START");
882 gigaset_schedule_event(cs);
884 wait_event(cs->waitqueue, !cs->waiting);
886 mutex_unlock(&cs->mutex);
890 mutex_unlock(&cs->mutex);
893 EXPORT_SYMBOL_GPL(gigaset_start);
895 void gigaset_shutdown(struct cardstate *cs)
897 mutex_lock(&cs->mutex);
901 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
902 //FIXME what should we do?
906 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
907 gigaset_schedule_event(cs);
909 wait_event(cs->waitqueue, !cs->waiting);
914 mutex_unlock(&cs->mutex);
916 EXPORT_SYMBOL_GPL(gigaset_shutdown);
918 void gigaset_stop(struct cardstate *cs)
920 mutex_lock(&cs->mutex);
924 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
925 //FIXME what should we do?
929 gig_dbg(DEBUG_CMD, "scheduling STOP");
930 gigaset_schedule_event(cs);
932 wait_event(cs->waitqueue, !cs->waiting);
937 mutex_unlock(&cs->mutex);
939 EXPORT_SYMBOL_GPL(gigaset_stop);
941 static LIST_HEAD(drivers);
942 static DEFINE_SPINLOCK(driver_lock);
944 struct cardstate *gigaset_get_cs_by_id(int id)
947 static struct cardstate *ret = NULL;
948 static struct cardstate *cs;
949 struct gigaset_driver *drv;
952 spin_lock_irqsave(&driver_lock, flags);
953 list_for_each_entry(drv, &drivers, list) {
954 spin_lock(&drv->lock);
955 for (i = 0; i < drv->minors; ++i) {
956 if (drv->flags[i] & VALID_ID) {
964 spin_unlock(&drv->lock);
968 spin_unlock_irqrestore(&driver_lock, flags);
972 void gigaset_debugdrivers(void)
975 static struct cardstate *cs;
976 struct gigaset_driver *drv;
979 spin_lock_irqsave(&driver_lock, flags);
980 list_for_each_entry(drv, &drivers, list) {
981 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
982 spin_lock(&drv->lock);
983 for (i = 0; i < drv->minors; ++i) {
984 gig_dbg(DEBUG_DRIVER, " index %u", i);
985 gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
988 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
989 gig_dbg(DEBUG_DRIVER, " minor_index %u",
991 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
992 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
994 spin_unlock(&drv->lock);
996 spin_unlock_irqrestore(&driver_lock, flags);
999 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1001 unsigned long flags;
1002 static struct cardstate *ret = NULL;
1003 struct gigaset_driver *drv;
1006 spin_lock_irqsave(&driver_lock, flags);
1007 list_for_each_entry(drv, &drivers, list) {
1008 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1010 index = minor - drv->minor;
1011 spin_lock(&drv->lock);
1012 if (drv->flags[index] & VALID_MINOR)
1013 ret = drv->cs + index;
1014 spin_unlock(&drv->lock);
1018 spin_unlock_irqrestore(&driver_lock, flags);
1022 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1024 if (tty->index < 0 || tty->index >= tty->driver->num)
1026 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1029 void gigaset_freedriver(struct gigaset_driver *drv)
1031 unsigned long flags;
1033 spin_lock_irqsave(&driver_lock, flags);
1034 list_del(&drv->list);
1035 spin_unlock_irqrestore(&driver_lock, flags);
1037 gigaset_if_freedriver(drv);
1043 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1045 /* gigaset_initdriver
1046 * Allocate and initialize gigaset_driver structure. Initialize interface.
1048 * minor First minor number
1049 * minors Number of minors this driver can handle
1050 * procname Name of the driver
1051 * devname Name of the device files (prefix without minor number)
1053 * Pointer to the gigaset_driver structure on success, NULL on failure.
1055 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1056 const char *procname,
1057 const char *devname,
1058 const struct gigaset_ops *ops,
1059 struct module *owner)
1061 struct gigaset_driver *drv;
1062 unsigned long flags;
1065 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1071 drv->minors = minors;
1072 spin_lock_init(&drv->lock);
1076 INIT_LIST_HEAD(&drv->list);
1078 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1082 drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1086 for (i = 0; i < minors; ++i) {
1088 drv->cs[i].driver = drv;
1089 drv->cs[i].ops = drv->ops;
1090 drv->cs[i].minor_index = i;
1093 gigaset_if_initdriver(drv, procname, devname);
1095 spin_lock_irqsave(&driver_lock, flags);
1096 list_add(&drv->list, &drivers);
1097 spin_unlock_irqrestore(&driver_lock, flags);
1106 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1108 /* For drivers without fixed assignment device<->cardstate (usb) */
1109 struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1111 unsigned long flags;
1112 struct cardstate *cs = NULL;
1115 spin_lock_irqsave(&drv->lock, flags);
1118 for (i = 0; i < drv->minors; ++i) {
1119 if ((drv->flags[i] & VALID_MINOR) &&
1120 !(drv->flags[i] & ASSIGNED)) {
1121 drv->flags[i] |= ASSIGNED;
1127 spin_unlock_irqrestore(&drv->lock, flags);
1130 EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1132 void gigaset_unassign(struct cardstate *cs)
1134 unsigned long flags;
1135 unsigned *minor_flags;
1136 struct gigaset_driver *drv;
1141 spin_lock_irqsave(&drv->lock, flags);
1142 minor_flags = drv->flags + cs->minor_index;
1143 if (*minor_flags & VALID_MINOR)
1144 *minor_flags &= ~ASSIGNED;
1145 spin_unlock_irqrestore(&drv->lock, flags);
1147 EXPORT_SYMBOL_GPL(gigaset_unassign);
1149 void gigaset_blockdriver(struct gigaset_driver *drv)
1151 unsigned long flags;
1152 spin_lock_irqsave(&drv->lock, flags);
1154 spin_unlock_irqrestore(&drv->lock, flags);
1156 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1158 static int __init gigaset_init_module(void)
1160 /* in accordance with the principle of least astonishment,
1161 * setting the 'debug' parameter to 1 activates a sensible
1162 * set of default debug levels
1164 if (gigaset_debuglevel == 1)
1165 gigaset_debuglevel = DEBUG_DEFAULT;
1167 info(DRIVER_AUTHOR);
1172 static void __exit gigaset_exit_module(void)
1176 module_init(gigaset_init_module);
1177 module_exit(gigaset_exit_module);
1179 MODULE_AUTHOR(DRIVER_AUTHOR);
1180 MODULE_DESCRIPTION(DRIVER_DESC);
1182 MODULE_LICENSE("GPL");