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/module.h>
18 #include <linux/moduleparam.h>
20 /* Version Information */
21 #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
22 #define DRIVER_DESC "Driver for Gigaset 307x"
24 #ifdef CONFIG_GIGASET_DEBUG
25 #define DRIVER_DESC_DEBUG " (debug build)"
27 #define DRIVER_DESC_DEBUG ""
30 /* Module parameters */
31 int gigaset_debuglevel;
32 EXPORT_SYMBOL_GPL(gigaset_debuglevel);
33 module_param_named(debug, gigaset_debuglevel, int, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(debug, "debug level");
36 /* driver state flags */
37 #define VALID_MINOR 0x01
41 * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging
42 * @level: debugging level.
43 * @msg: message prefix.
44 * @len: number of bytes to dump.
47 * If the current debugging level includes one of the bits set in @level,
48 * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio,
49 * prefixed by the text @msg.
51 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
52 size_t len, const unsigned char *buf)
54 unsigned char outbuf[80];
56 size_t space = sizeof outbuf - 1;
57 unsigned char *out = outbuf;
62 if (c == '~' || c == '^' || c == '\\') {
73 if (c < 0x20 || c == 0x7f) {
85 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
87 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
89 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
93 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
94 cs->control_state = flags;
99 set_current_state(TASK_INTERRUPTIBLE);
100 schedule_timeout(delay * HZ / 1000);
106 int gigaset_enterconfigmode(struct cardstate *cs)
110 cs->control_state = TIOCM_RTS;
112 r = setflags(cs, TIOCM_DTR, 200);
115 r = setflags(cs, 0, 200);
118 for (i = 0; i < 5; ++i) {
119 r = setflags(cs, TIOCM_RTS, 100);
122 r = setflags(cs, 0, 100);
126 r = setflags(cs, TIOCM_RTS | TIOCM_DTR, 800);
133 dev_err(cs->dev, "error %d on setuartbits\n", -r);
134 cs->control_state = TIOCM_RTS | TIOCM_DTR;
135 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS | TIOCM_DTR);
140 static int test_timeout(struct at_state_t *at_state)
142 if (!at_state->timer_expires)
145 if (--at_state->timer_expires) {
146 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
147 at_state, at_state->timer_expires);
151 gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
152 at_state->timer_index, NULL);
156 static void timer_tick(unsigned long data)
158 struct cardstate *cs = (struct cardstate *) data;
161 struct at_state_t *at_state;
164 spin_lock_irqsave(&cs->lock, flags);
166 for (channel = 0; channel < cs->channels; ++channel)
167 if (test_timeout(&cs->bcs[channel].at_state))
170 if (test_timeout(&cs->at_state))
173 list_for_each_entry(at_state, &cs->temp_at_states, list)
174 if (test_timeout(at_state))
178 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
180 gig_dbg(DEBUG_EVENT, "scheduling timeout");
181 tasklet_schedule(&cs->event_tasklet);
185 spin_unlock_irqrestore(&cs->lock, flags);
188 int gigaset_get_channel(struct bc_state *bcs)
192 spin_lock_irqsave(&bcs->cs->lock, flags);
193 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
194 gig_dbg(DEBUG_CHANNEL, "could not allocate channel %d",
196 spin_unlock_irqrestore(&bcs->cs->lock, flags);
201 gig_dbg(DEBUG_CHANNEL, "allocated channel %d", bcs->channel);
202 spin_unlock_irqrestore(&bcs->cs->lock, flags);
206 struct bc_state *gigaset_get_free_channel(struct cardstate *cs)
211 spin_lock_irqsave(&cs->lock, flags);
212 if (!try_module_get(cs->driver->owner)) {
213 gig_dbg(DEBUG_CHANNEL,
214 "could not get module for allocating channel");
215 spin_unlock_irqrestore(&cs->lock, flags);
218 for (i = 0; i < cs->channels; ++i)
219 if (!cs->bcs[i].use_count) {
220 ++cs->bcs[i].use_count;
222 spin_unlock_irqrestore(&cs->lock, flags);
223 gig_dbg(DEBUG_CHANNEL, "allocated channel %d", i);
226 module_put(cs->driver->owner);
227 spin_unlock_irqrestore(&cs->lock, flags);
228 gig_dbg(DEBUG_CHANNEL, "no free channel");
232 void gigaset_free_channel(struct bc_state *bcs)
236 spin_lock_irqsave(&bcs->cs->lock, flags);
238 gig_dbg(DEBUG_CHANNEL, "could not free channel %d",
240 spin_unlock_irqrestore(&bcs->cs->lock, flags);
245 module_put(bcs->cs->driver->owner);
246 gig_dbg(DEBUG_CHANNEL, "freed channel %d", bcs->channel);
247 spin_unlock_irqrestore(&bcs->cs->lock, flags);
250 int gigaset_get_channels(struct cardstate *cs)
255 spin_lock_irqsave(&cs->lock, flags);
256 for (i = 0; i < cs->channels; ++i)
257 if (cs->bcs[i].use_count) {
258 spin_unlock_irqrestore(&cs->lock, flags);
259 gig_dbg(DEBUG_CHANNEL,
260 "could not allocate all channels");
263 for (i = 0; i < cs->channels; ++i)
264 ++cs->bcs[i].use_count;
265 spin_unlock_irqrestore(&cs->lock, flags);
267 gig_dbg(DEBUG_CHANNEL, "allocated all channels");
272 void gigaset_free_channels(struct cardstate *cs)
277 gig_dbg(DEBUG_CHANNEL, "unblocking all channels");
278 spin_lock_irqsave(&cs->lock, flags);
279 for (i = 0; i < cs->channels; ++i)
280 --cs->bcs[i].use_count;
281 spin_unlock_irqrestore(&cs->lock, flags);
284 void gigaset_block_channels(struct cardstate *cs)
289 gig_dbg(DEBUG_CHANNEL, "blocking all channels");
290 spin_lock_irqsave(&cs->lock, flags);
291 for (i = 0; i < cs->channels; ++i)
292 ++cs->bcs[i].use_count;
293 spin_unlock_irqrestore(&cs->lock, flags);
296 static void clear_events(struct cardstate *cs)
302 spin_lock_irqsave(&cs->ev_lock, flags);
307 while (tail != head) {
308 ev = cs->events + head;
310 head = (head + 1) % MAX_EVENTS;
315 spin_unlock_irqrestore(&cs->ev_lock, flags);
319 * gigaset_add_event() - add event to device event queue
320 * @cs: device descriptor structure.
321 * @at_state: connection state structure.
323 * @ptr: pointer parameter for event.
324 * @parameter: integer parameter for event.
325 * @arg: pointer parameter for event.
327 * Allocate an event queue entry from the device's event queue, and set it up
328 * with the parameters given.
330 * Return value: added event
332 struct event_t *gigaset_add_event(struct cardstate *cs,
333 struct at_state_t *at_state, int type,
334 void *ptr, int parameter, void *arg)
338 struct event_t *event = NULL;
340 gig_dbg(DEBUG_EVENT, "queueing event %d", type);
342 spin_lock_irqsave(&cs->ev_lock, flags);
345 next = (tail + 1) % MAX_EVENTS;
346 if (unlikely(next == cs->ev_head))
347 dev_err(cs->dev, "event queue full\n");
349 event = cs->events + tail;
351 event->at_state = at_state;
355 event->parameter = parameter;
359 spin_unlock_irqrestore(&cs->ev_lock, flags);
363 EXPORT_SYMBOL_GPL(gigaset_add_event);
365 static void clear_at_state(struct at_state_t *at_state)
369 for (i = 0; i < STR_NUM; ++i) {
370 kfree(at_state->str_var[i]);
371 at_state->str_var[i] = NULL;
375 static void dealloc_temp_at_states(struct cardstate *cs)
377 struct at_state_t *cur, *next;
379 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
380 list_del(&cur->list);
386 static void gigaset_freebcs(struct bc_state *bcs)
390 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
391 bcs->cs->ops->freebcshw(bcs);
393 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
394 clear_at_state(&bcs->at_state);
395 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
396 dev_kfree_skb(bcs->rx_skb);
399 for (i = 0; i < AT_NUM; ++i) {
400 kfree(bcs->commands[i]);
401 bcs->commands[i] = NULL;
405 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
409 struct cardstate *cs;
410 struct cardstate *ret = NULL;
412 spin_lock_irqsave(&drv->lock, flags);
415 for (i = 0; i < drv->minors; ++i) {
417 if (!(cs->flags & VALID_MINOR)) {
418 cs->flags = VALID_MINOR;
424 spin_unlock_irqrestore(&drv->lock, flags);
428 static void free_cs(struct cardstate *cs)
433 static void make_valid(struct cardstate *cs, unsigned mask)
436 struct gigaset_driver *drv = cs->driver;
437 spin_lock_irqsave(&drv->lock, flags);
439 spin_unlock_irqrestore(&drv->lock, flags);
442 static void make_invalid(struct cardstate *cs, unsigned mask)
445 struct gigaset_driver *drv = cs->driver;
446 spin_lock_irqsave(&drv->lock, flags);
448 spin_unlock_irqrestore(&drv->lock, flags);
452 * gigaset_freecs() - free all associated ressources of a device
453 * @cs: device descriptor structure.
455 * Stops all tasklets and timers, unregisters the device from all
456 * subsystems it was registered to, deallocates the device structure
457 * @cs and all structures referenced from it.
458 * Operations on the device should be stopped before calling this.
460 void gigaset_freecs(struct cardstate *cs)
468 mutex_lock(&cs->mutex);
475 spin_lock_irqsave(&cs->lock, flags);
477 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
478 not rescheduled below */
480 tasklet_kill(&cs->event_tasklet);
481 del_timer_sync(&cs->timer);
483 switch (cs->cs_init) {
485 /* clear B channel structures */
486 for (i = 0; i < cs->channels; ++i) {
487 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
488 gigaset_freebcs(cs->bcs + i);
491 /* clear device sysfs */
492 gigaset_free_dev_sysfs(cs);
496 gig_dbg(DEBUG_INIT, "clearing hw");
497 cs->ops->freecshw(cs);
500 case 2: /* error in initcshw */
501 /* Deregister from LL */
502 make_invalid(cs, VALID_ID);
503 gigaset_isdn_unregdev(cs);
506 case 1: /* error when registering to LL */
507 gig_dbg(DEBUG_INIT, "clearing at_state");
508 clear_at_state(&cs->at_state);
509 dealloc_temp_at_states(cs);
510 tty_port_destroy(&cs->port);
513 case 0: /* error in basic setup */
515 gig_dbg(DEBUG_INIT, "freeing inbuf");
518 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
520 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
521 mutex_unlock(&cs->mutex);
524 EXPORT_SYMBOL_GPL(gigaset_freecs);
526 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
527 struct cardstate *cs, int cid)
531 INIT_LIST_HEAD(&at_state->list);
532 at_state->waiting = 0;
533 at_state->getstring = 0;
534 at_state->pending_commands = 0;
535 at_state->timer_expires = 0;
536 at_state->timer_active = 0;
537 at_state->timer_index = 0;
538 at_state->seq_index = 0;
539 at_state->ConState = 0;
540 for (i = 0; i < STR_NUM; ++i)
541 at_state->str_var[i] = NULL;
542 at_state->int_var[VAR_ZDLE] = 0;
543 at_state->int_var[VAR_ZCTP] = -1;
544 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
549 at_state->replystruct = cs->tabnocid;
551 at_state->replystruct = cs->tabcid;
555 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
556 /* inbuf->read must be allocated before! */
561 inbuf->inputstate = INS_command;
565 * gigaset_fill_inbuf() - append received data to input buffer
566 * @inbuf: buffer structure.
567 * @src: received data.
568 * @numbytes: number of bytes received.
570 * Return value: !=0 if some data was appended
572 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
575 unsigned n, head, tail, bytesleft;
577 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
582 bytesleft = numbytes;
585 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
591 n = (RBUFSIZE - 1) - tail;
595 dev_err(inbuf->cs->dev,
596 "buffer overflow (%u bytes lost)\n",
602 memcpy(inbuf->data + tail, src, n);
604 tail = (tail + n) % RBUFSIZE;
607 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
609 return numbytes != bytesleft;
611 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
613 /* Initialize the b-channel structure */
614 static int gigaset_initbcs(struct bc_state *bcs, struct cardstate *cs,
621 skb_queue_head_init(&bcs->squeue);
627 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
628 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
630 #ifdef CONFIG_GIGASET_DEBUG
636 bcs->rx_fcs = PPP_INITFCS;
638 bcs->channel = channel;
644 bcs->ignore = cs->ignoreframes;
646 for (i = 0; i < AT_NUM; ++i)
647 bcs->commands[i] = NULL;
649 spin_lock_init(&bcs->aplock);
651 bcs->apconnstate = 0;
653 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
654 return cs->ops->initbcshw(bcs);
658 * gigaset_initcs() - initialize device structure
659 * @drv: hardware driver the device belongs to
660 * @channels: number of B channels supported by device
661 * @onechannel: !=0 if B channel data and AT commands share one
662 * communication channel (M10x),
663 * ==0 if B channels have separate communication channels (base)
664 * @ignoreframes: number of frames to ignore after setting up B channel
665 * @cidmode: !=0: start in CallID mode
666 * @modulename: name of driver module for LL registration
668 * Allocate and initialize cardstate structure for Gigaset driver
669 * Calls hardware dependent gigaset_initcshw() function
670 * Calls B channel initialization function gigaset_initbcs() for each B channel
673 * pointer to cardstate structure
675 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
676 int onechannel, int ignoreframes,
677 int cidmode, const char *modulename)
679 struct cardstate *cs;
683 gig_dbg(DEBUG_INIT, "allocating cs");
686 pr_err("maximum number of devices exceeded\n");
690 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
691 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
693 pr_err("out of memory\n");
696 gig_dbg(DEBUG_INIT, "allocating inbuf");
697 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
699 pr_err("out of memory\n");
704 cs->channels = channels;
705 cs->onechannel = onechannel;
706 cs->ignoreframes = ignoreframes;
707 INIT_LIST_HEAD(&cs->temp_at_states);
709 init_timer(&cs->timer); /* clear next & prev */
710 spin_lock_init(&cs->ev_lock);
714 tasklet_init(&cs->event_tasklet, gigaset_handle_event,
716 tty_port_init(&cs->port);
717 cs->commands_pending = 0;
722 cs->cidmode = cidmode != 0;
723 cs->tabnocid = gigaset_tab_nocid;
724 cs->tabcid = gigaset_tab_cid;
726 init_waitqueue_head(&cs->waitqueue);
729 cs->mode = M_UNKNOWN;
730 cs->mstate = MS_UNINITIALIZED;
734 gig_dbg(DEBUG_INIT, "setting up at_state");
735 spin_lock_init(&cs->lock);
736 gigaset_at_init(&cs->at_state, NULL, cs, 0);
740 gig_dbg(DEBUG_INIT, "setting up inbuf");
741 gigaset_inbuf_init(cs->inbuf, cs);
746 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
747 cs->cmdbuf = cs->lastcmdbuf = NULL;
748 spin_lock_init(&cs->cmdlock);
752 gig_dbg(DEBUG_INIT, "setting up iif");
753 if (gigaset_isdn_regdev(cs, modulename) < 0) {
754 pr_err("error registering ISDN device\n");
758 make_valid(cs, VALID_ID);
760 gig_dbg(DEBUG_INIT, "setting up hw");
761 if (cs->ops->initcshw(cs) < 0)
766 /* set up character device */
769 /* set up device sysfs */
770 gigaset_init_dev_sysfs(cs);
772 /* set up channel data structures */
773 for (i = 0; i < channels; ++i) {
774 gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
775 if (gigaset_initbcs(cs->bcs + i, cs, i) < 0) {
776 pr_err("could not allocate channel %d data\n", i);
781 spin_lock_irqsave(&cs->lock, flags);
783 spin_unlock_irqrestore(&cs->lock, flags);
784 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
785 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
786 add_timer(&cs->timer);
788 gig_dbg(DEBUG_INIT, "cs initialized");
792 gig_dbg(DEBUG_INIT, "failed");
796 EXPORT_SYMBOL_GPL(gigaset_initcs);
798 /* ReInitialize the b-channel structure on hangup */
799 void gigaset_bcs_reinit(struct bc_state *bcs)
802 struct cardstate *cs = bcs->cs;
805 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
808 spin_lock_irqsave(&cs->lock, flags);
809 clear_at_state(&bcs->at_state);
810 bcs->at_state.ConState = 0;
811 bcs->at_state.timer_active = 0;
812 bcs->at_state.timer_expires = 0;
813 bcs->at_state.cid = -1; /* No CID defined */
814 spin_unlock_irqrestore(&cs->lock, flags);
818 #ifdef CONFIG_GIGASET_DEBUG
822 bcs->rx_fcs = PPP_INITFCS;
825 bcs->ignore = cs->ignoreframes;
826 dev_kfree_skb(bcs->rx_skb);
829 cs->ops->reinitbcshw(bcs);
832 static void cleanup_cs(struct cardstate *cs)
834 struct cmdbuf_t *cb, *tcb;
838 spin_lock_irqsave(&cs->lock, flags);
840 cs->mode = M_UNKNOWN;
841 cs->mstate = MS_UNINITIALIZED;
843 clear_at_state(&cs->at_state);
844 dealloc_temp_at_states(cs);
845 gigaset_at_init(&cs->at_state, NULL, cs, 0);
847 cs->inbuf->inputstate = INS_command;
857 cs->cmdbuf = cs->lastcmdbuf = NULL;
863 cs->commands_pending = 0;
866 spin_unlock_irqrestore(&cs->lock, flags);
868 for (i = 0; i < cs->channels; ++i) {
869 gigaset_freebcs(cs->bcs + i);
870 if (gigaset_initbcs(cs->bcs + i, cs, i) < 0)
871 pr_err("could not allocate channel %d data\n", i);
875 cs->cmd_result = -ENODEV;
877 wake_up_interruptible(&cs->waitqueue);
883 * gigaset_start() - start device operations
884 * @cs: device descriptor structure.
886 * Prepares the device for use by setting up communication parameters,
887 * scheduling an EV_START event to initiate device initialization, and
888 * waiting for completion of the initialization.
891 * 0 on success, error code < 0 on failure
893 int gigaset_start(struct cardstate *cs)
897 if (mutex_lock_interruptible(&cs->mutex))
900 spin_lock_irqsave(&cs->lock, flags);
902 spin_unlock_irqrestore(&cs->lock, flags);
904 if (cs->mstate != MS_LOCKED) {
905 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS);
906 cs->ops->baud_rate(cs, B115200);
907 cs->ops->set_line_ctrl(cs, CS8);
908 cs->control_state = TIOCM_DTR | TIOCM_RTS;
913 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
917 gigaset_schedule_event(cs);
919 wait_event(cs->waitqueue, !cs->waiting);
921 mutex_unlock(&cs->mutex);
925 mutex_unlock(&cs->mutex);
928 EXPORT_SYMBOL_GPL(gigaset_start);
931 * gigaset_shutdown() - shut down device operations
932 * @cs: device descriptor structure.
934 * Deactivates the device by scheduling an EV_SHUTDOWN event and
935 * waiting for completion of the shutdown.
938 * 0 - success, -ENODEV - error (no device associated)
940 int gigaset_shutdown(struct cardstate *cs)
942 mutex_lock(&cs->mutex);
944 if (!(cs->flags & VALID_MINOR)) {
945 mutex_unlock(&cs->mutex);
951 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL))
953 gigaset_schedule_event(cs);
955 wait_event(cs->waitqueue, !cs->waiting);
960 mutex_unlock(&cs->mutex);
963 EXPORT_SYMBOL_GPL(gigaset_shutdown);
966 * gigaset_stop() - stop device operations
967 * @cs: device descriptor structure.
969 * Stops operations on the device by scheduling an EV_STOP event and
970 * waiting for completion of the shutdown.
972 void gigaset_stop(struct cardstate *cs)
974 mutex_lock(&cs->mutex);
978 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL))
980 gigaset_schedule_event(cs);
982 wait_event(cs->waitqueue, !cs->waiting);
987 mutex_unlock(&cs->mutex);
989 EXPORT_SYMBOL_GPL(gigaset_stop);
991 static LIST_HEAD(drivers);
992 static DEFINE_SPINLOCK(driver_lock);
994 struct cardstate *gigaset_get_cs_by_id(int id)
997 struct cardstate *ret = NULL;
998 struct cardstate *cs;
999 struct gigaset_driver *drv;
1002 spin_lock_irqsave(&driver_lock, flags);
1003 list_for_each_entry(drv, &drivers, list) {
1004 spin_lock(&drv->lock);
1005 for (i = 0; i < drv->minors; ++i) {
1007 if ((cs->flags & VALID_ID) && cs->myid == id) {
1012 spin_unlock(&drv->lock);
1016 spin_unlock_irqrestore(&driver_lock, flags);
1020 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1022 unsigned long flags;
1023 struct cardstate *ret = NULL;
1024 struct gigaset_driver *drv;
1027 spin_lock_irqsave(&driver_lock, flags);
1028 list_for_each_entry(drv, &drivers, list) {
1029 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1031 index = minor - drv->minor;
1032 spin_lock(&drv->lock);
1033 if (drv->cs[index].flags & VALID_MINOR)
1034 ret = drv->cs + index;
1035 spin_unlock(&drv->lock);
1039 spin_unlock_irqrestore(&driver_lock, flags);
1043 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1045 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1049 * gigaset_freedriver() - free all associated ressources of a driver
1050 * @drv: driver descriptor structure.
1052 * Unregisters the driver from the system and deallocates the driver
1053 * structure @drv and all structures referenced from it.
1054 * All devices should be shut down before calling this.
1056 void gigaset_freedriver(struct gigaset_driver *drv)
1058 unsigned long flags;
1060 spin_lock_irqsave(&driver_lock, flags);
1061 list_del(&drv->list);
1062 spin_unlock_irqrestore(&driver_lock, flags);
1064 gigaset_if_freedriver(drv);
1069 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1072 * gigaset_initdriver() - initialize driver structure
1073 * @minor: First minor number
1074 * @minors: Number of minors this driver can handle
1075 * @procname: Name of the driver
1076 * @devname: Name of the device files (prefix without minor number)
1078 * Allocate and initialize gigaset_driver structure. Initialize interface.
1081 * Pointer to the gigaset_driver structure on success, NULL on failure.
1083 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1084 const char *procname,
1085 const char *devname,
1086 const struct gigaset_ops *ops,
1087 struct module *owner)
1089 struct gigaset_driver *drv;
1090 unsigned long flags;
1093 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1099 drv->minors = minors;
1100 spin_lock_init(&drv->lock);
1104 INIT_LIST_HEAD(&drv->list);
1106 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1110 for (i = 0; i < minors; ++i) {
1111 drv->cs[i].flags = 0;
1112 drv->cs[i].driver = drv;
1113 drv->cs[i].ops = drv->ops;
1114 drv->cs[i].minor_index = i;
1115 mutex_init(&drv->cs[i].mutex);
1118 gigaset_if_initdriver(drv, procname, devname);
1120 spin_lock_irqsave(&driver_lock, flags);
1121 list_add(&drv->list, &drivers);
1122 spin_unlock_irqrestore(&driver_lock, flags);
1130 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1133 * gigaset_blockdriver() - block driver
1134 * @drv: driver descriptor structure.
1136 * Prevents the driver from attaching new devices, in preparation for
1139 void gigaset_blockdriver(struct gigaset_driver *drv)
1143 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1145 static int __init gigaset_init_module(void)
1147 /* in accordance with the principle of least astonishment,
1148 * setting the 'debug' parameter to 1 activates a sensible
1149 * set of default debug levels
1151 if (gigaset_debuglevel == 1)
1152 gigaset_debuglevel = DEBUG_DEFAULT;
1154 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
1155 gigaset_isdn_regdrv();
1159 static void __exit gigaset_exit_module(void)
1161 gigaset_isdn_unregdrv();
1164 module_init(gigaset_init_module);
1165 module_exit(gigaset_exit_module);
1167 MODULE_AUTHOR(DRIVER_AUTHOR);
1168 MODULE_DESCRIPTION(DRIVER_DESC);
1170 MODULE_LICENSE("GPL");