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
;
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
;
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
;
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 gigaset_add_event(at_state
->cs
, at_state
, EV_TIMEOUT
, NULL
,
153 at_state
->timer_index
, NULL
);
157 static void timer_tick(unsigned long data
)
159 struct cardstate
*cs
= (struct cardstate
*) data
;
162 struct at_state_t
*at_state
;
165 spin_lock_irqsave(&cs
->lock
, flags
);
167 for (channel
= 0; channel
< cs
->channels
; ++channel
)
168 if (test_timeout(&cs
->bcs
[channel
].at_state
))
171 if (test_timeout(&cs
->at_state
))
174 list_for_each_entry(at_state
, &cs
->temp_at_states
, list
)
175 if (test_timeout(at_state
))
179 mod_timer(&cs
->timer
, jiffies
+ msecs_to_jiffies(GIG_TICK
));
181 gig_dbg(DEBUG_EVENT
, "scheduling timeout");
182 tasklet_schedule(&cs
->event_tasklet
);
186 spin_unlock_irqrestore(&cs
->lock
, flags
);
189 int gigaset_get_channel(struct bc_state
*bcs
)
193 spin_lock_irqsave(&bcs
->cs
->lock
, flags
);
194 if (bcs
->use_count
|| !try_module_get(bcs
->cs
->driver
->owner
)) {
195 gig_dbg(DEBUG_CHANNEL
, "could not allocate channel %d",
197 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
202 gig_dbg(DEBUG_CHANNEL
, "allocated channel %d", bcs
->channel
);
203 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
207 struct bc_state
*gigaset_get_free_channel(struct cardstate
*cs
)
212 spin_lock_irqsave(&cs
->lock
, flags
);
213 if (!try_module_get(cs
->driver
->owner
)) {
214 gig_dbg(DEBUG_CHANNEL
,
215 "could not get module for allocating channel");
216 spin_unlock_irqrestore(&cs
->lock
, flags
);
219 for (i
= 0; i
< cs
->channels
; ++i
)
220 if (!cs
->bcs
[i
].use_count
) {
221 ++cs
->bcs
[i
].use_count
;
223 spin_unlock_irqrestore(&cs
->lock
, flags
);
224 gig_dbg(DEBUG_CHANNEL
, "allocated channel %d", i
);
227 module_put(cs
->driver
->owner
);
228 spin_unlock_irqrestore(&cs
->lock
, flags
);
229 gig_dbg(DEBUG_CHANNEL
, "no free channel");
233 void gigaset_free_channel(struct bc_state
*bcs
)
237 spin_lock_irqsave(&bcs
->cs
->lock
, flags
);
239 gig_dbg(DEBUG_CHANNEL
, "could not free channel %d",
241 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
246 module_put(bcs
->cs
->driver
->owner
);
247 gig_dbg(DEBUG_CHANNEL
, "freed channel %d", bcs
->channel
);
248 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
251 int gigaset_get_channels(struct cardstate
*cs
)
256 spin_lock_irqsave(&cs
->lock
, flags
);
257 for (i
= 0; i
< cs
->channels
; ++i
)
258 if (cs
->bcs
[i
].use_count
) {
259 spin_unlock_irqrestore(&cs
->lock
, flags
);
260 gig_dbg(DEBUG_CHANNEL
,
261 "could not allocate all channels");
264 for (i
= 0; i
< cs
->channels
; ++i
)
265 ++cs
->bcs
[i
].use_count
;
266 spin_unlock_irqrestore(&cs
->lock
, flags
);
268 gig_dbg(DEBUG_CHANNEL
, "allocated all channels");
273 void gigaset_free_channels(struct cardstate
*cs
)
278 gig_dbg(DEBUG_CHANNEL
, "unblocking all channels");
279 spin_lock_irqsave(&cs
->lock
, flags
);
280 for (i
= 0; i
< cs
->channels
; ++i
)
281 --cs
->bcs
[i
].use_count
;
282 spin_unlock_irqrestore(&cs
->lock
, flags
);
285 void gigaset_block_channels(struct cardstate
*cs
)
290 gig_dbg(DEBUG_CHANNEL
, "blocking all channels");
291 spin_lock_irqsave(&cs
->lock
, flags
);
292 for (i
= 0; i
< cs
->channels
; ++i
)
293 ++cs
->bcs
[i
].use_count
;
294 spin_unlock_irqrestore(&cs
->lock
, flags
);
297 static void clear_events(struct cardstate
*cs
)
303 spin_lock_irqsave(&cs
->ev_lock
, flags
);
308 while (tail
!= head
) {
309 ev
= cs
->events
+ head
;
311 head
= (head
+ 1) % MAX_EVENTS
;
316 spin_unlock_irqrestore(&cs
->ev_lock
, flags
);
320 * gigaset_add_event() - add event to device event queue
321 * @cs: device descriptor structure.
322 * @at_state: connection state structure.
324 * @ptr: pointer parameter for event.
325 * @parameter: integer parameter for event.
326 * @arg: pointer parameter for event.
328 * Allocate an event queue entry from the device's event queue, and set it up
329 * with the parameters given.
331 * Return value: added event
333 struct event_t
*gigaset_add_event(struct cardstate
*cs
,
334 struct at_state_t
*at_state
, int type
,
335 void *ptr
, int parameter
, void *arg
)
339 struct event_t
*event
= NULL
;
341 gig_dbg(DEBUG_EVENT
, "queueing event %d", type
);
343 spin_lock_irqsave(&cs
->ev_lock
, flags
);
346 next
= (tail
+ 1) % MAX_EVENTS
;
347 if (unlikely(next
== cs
->ev_head
))
348 dev_err(cs
->dev
, "event queue full\n");
350 event
= cs
->events
+ tail
;
352 event
->at_state
= at_state
;
356 event
->parameter
= parameter
;
360 spin_unlock_irqrestore(&cs
->ev_lock
, flags
);
364 EXPORT_SYMBOL_GPL(gigaset_add_event
);
366 static void free_strings(struct at_state_t
*at_state
)
370 for (i
= 0; i
< STR_NUM
; ++i
) {
371 kfree(at_state
->str_var
[i
]);
372 at_state
->str_var
[i
] = NULL
;
376 static void clear_at_state(struct at_state_t
*at_state
)
378 free_strings(at_state
);
381 static void dealloc_at_states(struct cardstate
*cs
)
383 struct at_state_t
*cur
, *next
;
385 list_for_each_entry_safe(cur
, next
, &cs
->temp_at_states
, list
) {
386 list_del(&cur
->list
);
392 static void gigaset_freebcs(struct bc_state
*bcs
)
396 gig_dbg(DEBUG_INIT
, "freeing bcs[%d]->hw", bcs
->channel
);
397 if (!bcs
->cs
->ops
->freebcshw(bcs
))
398 gig_dbg(DEBUG_INIT
, "failed");
400 gig_dbg(DEBUG_INIT
, "clearing bcs[%d]->at_state", bcs
->channel
);
401 clear_at_state(&bcs
->at_state
);
402 gig_dbg(DEBUG_INIT
, "freeing bcs[%d]->skb", bcs
->channel
);
403 dev_kfree_skb(bcs
->skb
);
406 for (i
= 0; i
< AT_NUM
; ++i
) {
407 kfree(bcs
->commands
[i
]);
408 bcs
->commands
[i
] = NULL
;
412 static struct cardstate
*alloc_cs(struct gigaset_driver
*drv
)
416 struct cardstate
*cs
;
417 struct cardstate
*ret
= NULL
;
419 spin_lock_irqsave(&drv
->lock
, flags
);
422 for (i
= 0; i
< drv
->minors
; ++i
) {
424 if (!(cs
->flags
& VALID_MINOR
)) {
425 cs
->flags
= VALID_MINOR
;
431 spin_unlock_irqrestore(&drv
->lock
, flags
);
435 static void free_cs(struct cardstate
*cs
)
440 static void make_valid(struct cardstate
*cs
, unsigned mask
)
443 struct gigaset_driver
*drv
= cs
->driver
;
444 spin_lock_irqsave(&drv
->lock
, flags
);
446 spin_unlock_irqrestore(&drv
->lock
, flags
);
449 static void make_invalid(struct cardstate
*cs
, unsigned mask
)
452 struct gigaset_driver
*drv
= cs
->driver
;
453 spin_lock_irqsave(&drv
->lock
, flags
);
455 spin_unlock_irqrestore(&drv
->lock
, flags
);
459 * gigaset_freecs() - free all associated ressources of a device
460 * @cs: device descriptor structure.
462 * Stops all tasklets and timers, unregisters the device from all
463 * subsystems it was registered to, deallocates the device structure
464 * @cs and all structures referenced from it.
465 * Operations on the device should be stopped before calling this.
467 void gigaset_freecs(struct cardstate
*cs
)
475 mutex_lock(&cs
->mutex
);
482 spin_lock_irqsave(&cs
->lock
, flags
);
484 spin_unlock_irqrestore(&cs
->lock
, flags
); /* event handler and timer are
485 not rescheduled below */
487 tasklet_kill(&cs
->event_tasklet
);
488 del_timer_sync(&cs
->timer
);
490 switch (cs
->cs_init
) {
492 /* clear B channel structures */
493 for (i
= 0; i
< cs
->channels
; ++i
) {
494 gig_dbg(DEBUG_INIT
, "clearing bcs[%d]", i
);
495 gigaset_freebcs(cs
->bcs
+ i
);
498 /* clear device sysfs */
499 gigaset_free_dev_sysfs(cs
);
503 gig_dbg(DEBUG_INIT
, "clearing hw");
504 cs
->ops
->freecshw(cs
);
507 case 2: /* error in initcshw */
508 /* Deregister from LL */
509 make_invalid(cs
, VALID_ID
);
510 gigaset_isdn_unregdev(cs
);
513 case 1: /* error when registering to LL */
514 gig_dbg(DEBUG_INIT
, "clearing at_state");
515 clear_at_state(&cs
->at_state
);
516 dealloc_at_states(cs
);
519 case 0: /* error in basic setup */
521 gig_dbg(DEBUG_INIT
, "freeing inbuf");
524 f_bcs
: gig_dbg(DEBUG_INIT
, "freeing bcs[]");
526 f_cs
: gig_dbg(DEBUG_INIT
, "freeing cs");
527 mutex_unlock(&cs
->mutex
);
530 EXPORT_SYMBOL_GPL(gigaset_freecs
);
532 void gigaset_at_init(struct at_state_t
*at_state
, struct bc_state
*bcs
,
533 struct cardstate
*cs
, int cid
)
537 INIT_LIST_HEAD(&at_state
->list
);
538 at_state
->waiting
= 0;
539 at_state
->getstring
= 0;
540 at_state
->pending_commands
= 0;
541 at_state
->timer_expires
= 0;
542 at_state
->timer_active
= 0;
543 at_state
->timer_index
= 0;
544 at_state
->seq_index
= 0;
545 at_state
->ConState
= 0;
546 for (i
= 0; i
< STR_NUM
; ++i
)
547 at_state
->str_var
[i
] = NULL
;
548 at_state
->int_var
[VAR_ZDLE
] = 0;
549 at_state
->int_var
[VAR_ZCTP
] = -1;
550 at_state
->int_var
[VAR_ZSAU
] = ZSAU_NULL
;
555 at_state
->replystruct
= cs
->tabnocid
;
557 at_state
->replystruct
= cs
->tabcid
;
561 static void gigaset_inbuf_init(struct inbuf_t
*inbuf
, struct cardstate
*cs
)
562 /* inbuf->read must be allocated before! */
567 inbuf
->inputstate
= INS_command
;
571 * gigaset_fill_inbuf() - append received data to input buffer
572 * @inbuf: buffer structure.
573 * @src: received data.
574 * @numbytes: number of bytes received.
576 int gigaset_fill_inbuf(struct inbuf_t
*inbuf
, const unsigned char *src
,
579 unsigned n
, head
, tail
, bytesleft
;
581 gig_dbg(DEBUG_INTR
, "received %u bytes", numbytes
);
586 bytesleft
= numbytes
;
589 gig_dbg(DEBUG_INTR
, "buffer state: %u -> %u", head
, tail
);
595 n
= (RBUFSIZE
-1) - tail
;
599 dev_err(inbuf
->cs
->dev
,
600 "buffer overflow (%u bytes lost)\n",
606 memcpy(inbuf
->data
+ tail
, src
, n
);
608 tail
= (tail
+ n
) % RBUFSIZE
;
611 gig_dbg(DEBUG_INTR
, "setting tail to %u", tail
);
613 return numbytes
!= bytesleft
;
615 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf
);
617 /* Initialize the b-channel structure */
618 static struct bc_state
*gigaset_initbcs(struct bc_state
*bcs
,
619 struct cardstate
*cs
, int channel
)
625 skb_queue_head_init(&bcs
->squeue
);
631 gig_dbg(DEBUG_INIT
, "setting up bcs[%d]->at_state", channel
);
632 gigaset_at_init(&bcs
->at_state
, bcs
, cs
, -1);
634 #ifdef CONFIG_GIGASET_DEBUG
638 gig_dbg(DEBUG_INIT
, "allocating bcs[%d]->skb", channel
);
639 bcs
->fcs
= PPP_INITFCS
;
641 if (cs
->ignoreframes
) {
644 bcs
->skb
= dev_alloc_skb(SBUFSIZE
+ cs
->hw_hdr_len
);
645 if (bcs
->skb
!= NULL
)
646 skb_reserve(bcs
->skb
, cs
->hw_hdr_len
);
648 pr_err("out of memory\n");
651 bcs
->channel
= channel
;
657 bcs
->ignore
= cs
->ignoreframes
;
659 for (i
= 0; i
< AT_NUM
; ++i
)
660 bcs
->commands
[i
] = NULL
;
662 gig_dbg(DEBUG_INIT
, " setting up bcs[%d]->hw", channel
);
663 if (cs
->ops
->initbcshw(bcs
))
666 gig_dbg(DEBUG_INIT
, " failed");
668 gig_dbg(DEBUG_INIT
, " freeing bcs[%d]->skb", channel
);
669 dev_kfree_skb(bcs
->skb
);
676 * gigaset_initcs() - initialize device structure
677 * @drv: hardware driver the device belongs to
678 * @channels: number of B channels supported by device
679 * @onechannel: !=0 if B channel data and AT commands share one
680 * communication channel (M10x),
681 * ==0 if B channels have separate communication channels (base)
682 * @ignoreframes: number of frames to ignore after setting up B channel
683 * @cidmode: !=0: start in CallID mode
684 * @modulename: name of driver module for LL registration
686 * Allocate and initialize cardstate structure for Gigaset driver
687 * Calls hardware dependent gigaset_initcshw() function
688 * Calls B channel initialization function gigaset_initbcs() for each B channel
691 * pointer to cardstate structure
693 struct cardstate
*gigaset_initcs(struct gigaset_driver
*drv
, int channels
,
694 int onechannel
, int ignoreframes
,
695 int cidmode
, const char *modulename
)
697 struct cardstate
*cs
;
701 gig_dbg(DEBUG_INIT
, "allocating cs");
704 pr_err("maximum number of devices exceeded\n");
708 gig_dbg(DEBUG_INIT
, "allocating bcs[0..%d]", channels
- 1);
709 cs
->bcs
= kmalloc(channels
* sizeof(struct bc_state
), GFP_KERNEL
);
711 pr_err("out of memory\n");
714 gig_dbg(DEBUG_INIT
, "allocating inbuf");
715 cs
->inbuf
= kmalloc(sizeof(struct inbuf_t
), GFP_KERNEL
);
717 pr_err("out of memory\n");
722 cs
->channels
= channels
;
723 cs
->onechannel
= onechannel
;
724 cs
->ignoreframes
= ignoreframes
;
725 INIT_LIST_HEAD(&cs
->temp_at_states
);
727 init_timer(&cs
->timer
); /* clear next & prev */
728 spin_lock_init(&cs
->ev_lock
);
732 tasklet_init(&cs
->event_tasklet
, gigaset_handle_event
,
734 cs
->commands_pending
= 0;
741 cs
->cidmode
= cidmode
!= 0;
742 cs
->tabnocid
= gigaset_tab_nocid
;
743 cs
->tabcid
= gigaset_tab_cid
;
745 init_waitqueue_head(&cs
->waitqueue
);
748 cs
->mode
= M_UNKNOWN
;
749 cs
->mstate
= MS_UNINITIALIZED
;
753 gig_dbg(DEBUG_INIT
, "setting up at_state");
754 spin_lock_init(&cs
->lock
);
755 gigaset_at_init(&cs
->at_state
, NULL
, cs
, 0);
759 gig_dbg(DEBUG_INIT
, "setting up inbuf");
760 gigaset_inbuf_init(cs
->inbuf
, cs
);
765 gig_dbg(DEBUG_INIT
, "setting up cmdbuf");
766 cs
->cmdbuf
= cs
->lastcmdbuf
= NULL
;
767 spin_lock_init(&cs
->cmdlock
);
771 gig_dbg(DEBUG_INIT
, "setting up iif");
772 if (!gigaset_isdn_regdev(cs
, modulename
)) {
773 pr_err("error registering ISDN device\n");
777 make_valid(cs
, VALID_ID
);
779 gig_dbg(DEBUG_INIT
, "setting up hw");
780 if (!cs
->ops
->initcshw(cs
))
785 /* set up character device */
788 /* set up device sysfs */
789 gigaset_init_dev_sysfs(cs
);
791 /* set up channel data structures */
792 for (i
= 0; i
< channels
; ++i
) {
793 gig_dbg(DEBUG_INIT
, "setting up bcs[%d]", i
);
794 if (!gigaset_initbcs(cs
->bcs
+ i
, cs
, i
)) {
795 pr_err("could not allocate channel %d data\n", i
);
800 spin_lock_irqsave(&cs
->lock
, flags
);
802 spin_unlock_irqrestore(&cs
->lock
, flags
);
803 setup_timer(&cs
->timer
, timer_tick
, (unsigned long) cs
);
804 cs
->timer
.expires
= jiffies
+ msecs_to_jiffies(GIG_TICK
);
805 /* FIXME: can jiffies increase too much until the timer is added?
806 * Same problem(?) with mod_timer() in timer_tick(). */
807 add_timer(&cs
->timer
);
809 gig_dbg(DEBUG_INIT
, "cs initialized");
813 gig_dbg(DEBUG_INIT
, "failed");
817 EXPORT_SYMBOL_GPL(gigaset_initcs
);
819 /* ReInitialize the b-channel structure on hangup */
820 void gigaset_bcs_reinit(struct bc_state
*bcs
)
823 struct cardstate
*cs
= bcs
->cs
;
826 while ((skb
= skb_dequeue(&bcs
->squeue
)) != NULL
)
829 spin_lock_irqsave(&cs
->lock
, flags
);
830 clear_at_state(&bcs
->at_state
);
831 bcs
->at_state
.ConState
= 0;
832 bcs
->at_state
.timer_active
= 0;
833 bcs
->at_state
.timer_expires
= 0;
834 bcs
->at_state
.cid
= -1; /* No CID defined */
835 spin_unlock_irqrestore(&cs
->lock
, flags
);
839 #ifdef CONFIG_GIGASET_DEBUG
843 bcs
->fcs
= PPP_INITFCS
;
846 bcs
->ignore
= cs
->ignoreframes
;
848 dev_kfree_skb(bcs
->skb
);
852 cs
->ops
->reinitbcshw(bcs
);
855 static void cleanup_cs(struct cardstate
*cs
)
857 struct cmdbuf_t
*cb
, *tcb
;
861 spin_lock_irqsave(&cs
->lock
, flags
);
863 cs
->mode
= M_UNKNOWN
;
864 cs
->mstate
= MS_UNINITIALIZED
;
866 clear_at_state(&cs
->at_state
);
867 dealloc_at_states(cs
);
868 free_strings(&cs
->at_state
);
869 gigaset_at_init(&cs
->at_state
, NULL
, cs
, 0);
871 cs
->inbuf
->inputstate
= INS_command
;
881 cs
->cmdbuf
= cs
->lastcmdbuf
= NULL
;
887 cs
->commands_pending
= 0;
890 spin_unlock_irqrestore(&cs
->lock
, flags
);
892 for (i
= 0; i
< cs
->channels
; ++i
) {
893 gigaset_freebcs(cs
->bcs
+ i
);
894 if (!gigaset_initbcs(cs
->bcs
+ i
, cs
, i
))
895 pr_err("could not allocate channel %d data\n", i
);
899 cs
->cmd_result
= -ENODEV
;
901 wake_up_interruptible(&cs
->waitqueue
);
907 * gigaset_start() - start device operations
908 * @cs: device descriptor structure.
910 * Prepares the device for use by setting up communication parameters,
911 * scheduling an EV_START event to initiate device initialization, and
912 * waiting for completion of the initialization.
915 * 1 - success, 0 - error
917 int gigaset_start(struct cardstate
*cs
)
921 if (mutex_lock_interruptible(&cs
->mutex
))
924 spin_lock_irqsave(&cs
->lock
, flags
);
926 spin_unlock_irqrestore(&cs
->lock
, flags
);
928 if (cs
->mstate
!= MS_LOCKED
) {
929 cs
->ops
->set_modem_ctrl(cs
, 0, TIOCM_DTR
|TIOCM_RTS
);
930 cs
->ops
->baud_rate(cs
, B115200
);
931 cs
->ops
->set_line_ctrl(cs
, CS8
);
932 cs
->control_state
= TIOCM_DTR
|TIOCM_RTS
;
937 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_START
, NULL
, 0, NULL
)) {
941 gigaset_schedule_event(cs
);
943 wait_event(cs
->waitqueue
, !cs
->waiting
);
945 mutex_unlock(&cs
->mutex
);
949 mutex_unlock(&cs
->mutex
);
952 EXPORT_SYMBOL_GPL(gigaset_start
);
955 * gigaset_shutdown() - shut down device operations
956 * @cs: device descriptor structure.
958 * Deactivates the device by scheduling an EV_SHUTDOWN event and
959 * waiting for completion of the shutdown.
962 * 0 - success, -1 - error (no device associated)
964 int gigaset_shutdown(struct cardstate
*cs
)
966 mutex_lock(&cs
->mutex
);
968 if (!(cs
->flags
& VALID_MINOR
)) {
969 mutex_unlock(&cs
->mutex
);
975 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_SHUTDOWN
, NULL
, 0, NULL
))
977 gigaset_schedule_event(cs
);
979 wait_event(cs
->waitqueue
, !cs
->waiting
);
984 mutex_unlock(&cs
->mutex
);
987 EXPORT_SYMBOL_GPL(gigaset_shutdown
);
990 * gigaset_stop() - stop device operations
991 * @cs: device descriptor structure.
993 * Stops operations on the device by scheduling an EV_STOP event and
994 * waiting for completion of the shutdown.
996 void gigaset_stop(struct cardstate
*cs
)
998 mutex_lock(&cs
->mutex
);
1002 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_STOP
, NULL
, 0, NULL
))
1004 gigaset_schedule_event(cs
);
1006 wait_event(cs
->waitqueue
, !cs
->waiting
);
1011 mutex_unlock(&cs
->mutex
);
1013 EXPORT_SYMBOL_GPL(gigaset_stop
);
1015 static LIST_HEAD(drivers
);
1016 static DEFINE_SPINLOCK(driver_lock
);
1018 struct cardstate
*gigaset_get_cs_by_id(int id
)
1020 unsigned long flags
;
1021 struct cardstate
*ret
= NULL
;
1022 struct cardstate
*cs
;
1023 struct gigaset_driver
*drv
;
1026 spin_lock_irqsave(&driver_lock
, flags
);
1027 list_for_each_entry(drv
, &drivers
, list
) {
1028 spin_lock(&drv
->lock
);
1029 for (i
= 0; i
< drv
->minors
; ++i
) {
1031 if ((cs
->flags
& VALID_ID
) && cs
->myid
== id
) {
1036 spin_unlock(&drv
->lock
);
1040 spin_unlock_irqrestore(&driver_lock
, flags
);
1044 void gigaset_debugdrivers(void)
1046 unsigned long flags
;
1047 static struct cardstate
*cs
;
1048 struct gigaset_driver
*drv
;
1051 spin_lock_irqsave(&driver_lock
, flags
);
1052 list_for_each_entry(drv
, &drivers
, list
) {
1053 gig_dbg(DEBUG_DRIVER
, "driver %p", drv
);
1054 spin_lock(&drv
->lock
);
1055 for (i
= 0; i
< drv
->minors
; ++i
) {
1056 gig_dbg(DEBUG_DRIVER
, " index %u", i
);
1058 gig_dbg(DEBUG_DRIVER
, " cardstate %p", cs
);
1059 gig_dbg(DEBUG_DRIVER
, " flags 0x%02x", cs
->flags
);
1060 gig_dbg(DEBUG_DRIVER
, " minor_index %u",
1062 gig_dbg(DEBUG_DRIVER
, " driver %p", cs
->driver
);
1063 gig_dbg(DEBUG_DRIVER
, " i4l id %d", cs
->myid
);
1065 spin_unlock(&drv
->lock
);
1067 spin_unlock_irqrestore(&driver_lock
, flags
);
1070 static struct cardstate
*gigaset_get_cs_by_minor(unsigned minor
)
1072 unsigned long flags
;
1073 struct cardstate
*ret
= NULL
;
1074 struct gigaset_driver
*drv
;
1077 spin_lock_irqsave(&driver_lock
, flags
);
1078 list_for_each_entry(drv
, &drivers
, list
) {
1079 if (minor
< drv
->minor
|| minor
>= drv
->minor
+ drv
->minors
)
1081 index
= minor
- drv
->minor
;
1082 spin_lock(&drv
->lock
);
1083 if (drv
->cs
[index
].flags
& VALID_MINOR
)
1084 ret
= drv
->cs
+ index
;
1085 spin_unlock(&drv
->lock
);
1089 spin_unlock_irqrestore(&driver_lock
, flags
);
1093 struct cardstate
*gigaset_get_cs_by_tty(struct tty_struct
*tty
)
1095 if (tty
->index
< 0 || tty
->index
>= tty
->driver
->num
)
1097 return gigaset_get_cs_by_minor(tty
->index
+ tty
->driver
->minor_start
);
1101 * gigaset_freedriver() - free all associated ressources of a driver
1102 * @drv: driver descriptor structure.
1104 * Unregisters the driver from the system and deallocates the driver
1105 * structure @drv and all structures referenced from it.
1106 * All devices should be shut down before calling this.
1108 void gigaset_freedriver(struct gigaset_driver
*drv
)
1110 unsigned long flags
;
1112 spin_lock_irqsave(&driver_lock
, flags
);
1113 list_del(&drv
->list
);
1114 spin_unlock_irqrestore(&driver_lock
, flags
);
1116 gigaset_if_freedriver(drv
);
1121 EXPORT_SYMBOL_GPL(gigaset_freedriver
);
1124 * gigaset_initdriver() - initialize driver structure
1125 * @minor: First minor number
1126 * @minors: Number of minors this driver can handle
1127 * @procname: Name of the driver
1128 * @devname: Name of the device files (prefix without minor number)
1130 * Allocate and initialize gigaset_driver structure. Initialize interface.
1133 * Pointer to the gigaset_driver structure on success, NULL on failure.
1135 struct gigaset_driver
*gigaset_initdriver(unsigned minor
, unsigned minors
,
1136 const char *procname
,
1137 const char *devname
,
1138 const struct gigaset_ops
*ops
,
1139 struct module
*owner
)
1141 struct gigaset_driver
*drv
;
1142 unsigned long flags
;
1145 drv
= kmalloc(sizeof *drv
, GFP_KERNEL
);
1151 drv
->minors
= minors
;
1152 spin_lock_init(&drv
->lock
);
1156 INIT_LIST_HEAD(&drv
->list
);
1158 drv
->cs
= kmalloc(minors
* sizeof *drv
->cs
, GFP_KERNEL
);
1162 for (i
= 0; i
< minors
; ++i
) {
1163 drv
->cs
[i
].flags
= 0;
1164 drv
->cs
[i
].driver
= drv
;
1165 drv
->cs
[i
].ops
= drv
->ops
;
1166 drv
->cs
[i
].minor_index
= i
;
1167 mutex_init(&drv
->cs
[i
].mutex
);
1170 gigaset_if_initdriver(drv
, procname
, devname
);
1172 spin_lock_irqsave(&driver_lock
, flags
);
1173 list_add(&drv
->list
, &drivers
);
1174 spin_unlock_irqrestore(&driver_lock
, flags
);
1183 EXPORT_SYMBOL_GPL(gigaset_initdriver
);
1186 * gigaset_blockdriver() - block driver
1187 * @drv: driver descriptor structure.
1189 * Prevents the driver from attaching new devices, in preparation for
1192 void gigaset_blockdriver(struct gigaset_driver
*drv
)
1196 EXPORT_SYMBOL_GPL(gigaset_blockdriver
);
1198 static int __init
gigaset_init_module(void)
1200 /* in accordance with the principle of least astonishment,
1201 * setting the 'debug' parameter to 1 activates a sensible
1202 * set of default debug levels
1204 if (gigaset_debuglevel
== 1)
1205 gigaset_debuglevel
= DEBUG_DEFAULT
;
1207 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG
"\n");
1208 gigaset_isdn_regdrv();
1212 static void __exit
gigaset_exit_module(void)
1214 gigaset_isdn_unregdrv();
1217 module_init(gigaset_init_module
);
1218 module_exit(gigaset_exit_module
);
1220 MODULE_AUTHOR(DRIVER_AUTHOR
);
1221 MODULE_DESCRIPTION(DRIVER_DESC
);
1223 MODULE_LICENSE("GPL");