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 if (!gigaset_add_event(at_state
->cs
, at_state
, EV_TIMEOUT
, NULL
,
153 at_state
->timer_index
, NULL
))
154 dev_err(at_state
->cs
->dev
, "%s: out of memory\n",
159 static void timer_tick(unsigned long data
)
161 struct cardstate
*cs
= (struct cardstate
*) data
;
164 struct at_state_t
*at_state
;
167 spin_lock_irqsave(&cs
->lock
, flags
);
169 for (channel
= 0; channel
< cs
->channels
; ++channel
)
170 if (test_timeout(&cs
->bcs
[channel
].at_state
))
173 if (test_timeout(&cs
->at_state
))
176 list_for_each_entry(at_state
, &cs
->temp_at_states
, list
)
177 if (test_timeout(at_state
))
181 mod_timer(&cs
->timer
, jiffies
+ msecs_to_jiffies(GIG_TICK
));
183 gig_dbg(DEBUG_CMD
, "scheduling timeout");
184 tasklet_schedule(&cs
->event_tasklet
);
188 spin_unlock_irqrestore(&cs
->lock
, flags
);
191 int gigaset_get_channel(struct bc_state
*bcs
)
195 spin_lock_irqsave(&bcs
->cs
->lock
, flags
);
196 if (bcs
->use_count
|| !try_module_get(bcs
->cs
->driver
->owner
)) {
197 gig_dbg(DEBUG_ANY
, "could not allocate channel %d",
199 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
204 gig_dbg(DEBUG_ANY
, "allocated channel %d", bcs
->channel
);
205 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
209 struct bc_state
*gigaset_get_free_channel(struct cardstate
*cs
)
214 spin_lock_irqsave(&cs
->lock
, flags
);
215 if (!try_module_get(cs
->driver
->owner
)) {
217 "could not get module for allocating channel");
218 spin_unlock_irqrestore(&cs
->lock
, flags
);
221 for (i
= 0; i
< cs
->channels
; ++i
)
222 if (!cs
->bcs
[i
].use_count
) {
223 ++cs
->bcs
[i
].use_count
;
225 spin_unlock_irqrestore(&cs
->lock
, flags
);
226 gig_dbg(DEBUG_ANY
, "allocated channel %d", i
);
229 module_put(cs
->driver
->owner
);
230 spin_unlock_irqrestore(&cs
->lock
, flags
);
231 gig_dbg(DEBUG_ANY
, "no free channel");
235 void gigaset_free_channel(struct bc_state
*bcs
)
239 spin_lock_irqsave(&bcs
->cs
->lock
, flags
);
241 gig_dbg(DEBUG_ANY
, "could not free channel %d", bcs
->channel
);
242 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
247 module_put(bcs
->cs
->driver
->owner
);
248 gig_dbg(DEBUG_ANY
, "freed channel %d", bcs
->channel
);
249 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
252 int gigaset_get_channels(struct cardstate
*cs
)
257 spin_lock_irqsave(&cs
->lock
, flags
);
258 for (i
= 0; i
< cs
->channels
; ++i
)
259 if (cs
->bcs
[i
].use_count
) {
260 spin_unlock_irqrestore(&cs
->lock
, flags
);
261 gig_dbg(DEBUG_ANY
, "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_ANY
, "allocated all channels");
273 void gigaset_free_channels(struct cardstate
*cs
)
278 gig_dbg(DEBUG_ANY
, "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_ANY
, "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 spin_lock_irqsave(&cs
->ev_lock
, flags
);
344 next
= (tail
+ 1) % MAX_EVENTS
;
345 if (unlikely(next
== cs
->ev_head
))
346 dev_err(cs
->dev
, "event queue full\n");
348 event
= cs
->events
+ tail
;
350 event
->at_state
= at_state
;
354 event
->parameter
= parameter
;
358 spin_unlock_irqrestore(&cs
->ev_lock
, flags
);
362 EXPORT_SYMBOL_GPL(gigaset_add_event
);
364 static void free_strings(struct at_state_t
*at_state
)
368 for (i
= 0; i
< STR_NUM
; ++i
) {
369 kfree(at_state
->str_var
[i
]);
370 at_state
->str_var
[i
] = NULL
;
374 static void clear_at_state(struct at_state_t
*at_state
)
376 free_strings(at_state
);
379 static void dealloc_at_states(struct cardstate
*cs
)
381 struct at_state_t
*cur
, *next
;
383 list_for_each_entry_safe(cur
, next
, &cs
->temp_at_states
, list
) {
384 list_del(&cur
->list
);
390 static void gigaset_freebcs(struct bc_state
*bcs
)
394 gig_dbg(DEBUG_INIT
, "freeing bcs[%d]->hw", bcs
->channel
);
395 if (!bcs
->cs
->ops
->freebcshw(bcs
))
396 gig_dbg(DEBUG_INIT
, "failed");
398 gig_dbg(DEBUG_INIT
, "clearing bcs[%d]->at_state", bcs
->channel
);
399 clear_at_state(&bcs
->at_state
);
400 gig_dbg(DEBUG_INIT
, "freeing bcs[%d]->skb", bcs
->channel
);
401 dev_kfree_skb(bcs
->skb
);
404 for (i
= 0; i
< AT_NUM
; ++i
) {
405 kfree(bcs
->commands
[i
]);
406 bcs
->commands
[i
] = NULL
;
410 static struct cardstate
*alloc_cs(struct gigaset_driver
*drv
)
414 struct cardstate
*cs
;
415 struct cardstate
*ret
= NULL
;
417 spin_lock_irqsave(&drv
->lock
, flags
);
420 for (i
= 0; i
< drv
->minors
; ++i
) {
422 if (!(cs
->flags
& VALID_MINOR
)) {
423 cs
->flags
= VALID_MINOR
;
429 spin_unlock_irqrestore(&drv
->lock
, flags
);
433 static void free_cs(struct cardstate
*cs
)
438 static void make_valid(struct cardstate
*cs
, unsigned mask
)
441 struct gigaset_driver
*drv
= cs
->driver
;
442 spin_lock_irqsave(&drv
->lock
, flags
);
444 spin_unlock_irqrestore(&drv
->lock
, flags
);
447 static void make_invalid(struct cardstate
*cs
, unsigned mask
)
450 struct gigaset_driver
*drv
= cs
->driver
;
451 spin_lock_irqsave(&drv
->lock
, flags
);
453 spin_unlock_irqrestore(&drv
->lock
, flags
);
457 * gigaset_freecs() - free all associated ressources of a device
458 * @cs: device descriptor structure.
460 * Stops all tasklets and timers, unregisters the device from all
461 * subsystems it was registered to, deallocates the device structure
462 * @cs and all structures referenced from it.
463 * Operations on the device should be stopped before calling this.
465 void gigaset_freecs(struct cardstate
*cs
)
473 mutex_lock(&cs
->mutex
);
480 spin_lock_irqsave(&cs
->lock
, flags
);
482 spin_unlock_irqrestore(&cs
->lock
, flags
); /* event handler and timer are
483 not rescheduled below */
485 tasklet_kill(&cs
->event_tasklet
);
486 del_timer_sync(&cs
->timer
);
488 switch (cs
->cs_init
) {
490 /* clear B channel structures */
491 for (i
= 0; i
< cs
->channels
; ++i
) {
492 gig_dbg(DEBUG_INIT
, "clearing bcs[%d]", i
);
493 gigaset_freebcs(cs
->bcs
+ i
);
496 /* clear device sysfs */
497 gigaset_free_dev_sysfs(cs
);
501 gig_dbg(DEBUG_INIT
, "clearing hw");
502 cs
->ops
->freecshw(cs
);
505 case 2: /* error in initcshw */
506 /* Deregister from LL */
507 make_invalid(cs
, VALID_ID
);
508 gigaset_isdn_unregister(cs
);
511 case 1: /* error when registering to LL */
512 gig_dbg(DEBUG_INIT
, "clearing at_state");
513 clear_at_state(&cs
->at_state
);
514 dealloc_at_states(cs
);
517 case 0: /* error in basic setup */
519 gig_dbg(DEBUG_INIT
, "freeing inbuf");
522 f_bcs
: gig_dbg(DEBUG_INIT
, "freeing bcs[]");
524 f_cs
: gig_dbg(DEBUG_INIT
, "freeing cs");
525 mutex_unlock(&cs
->mutex
);
528 EXPORT_SYMBOL_GPL(gigaset_freecs
);
530 void gigaset_at_init(struct at_state_t
*at_state
, struct bc_state
*bcs
,
531 struct cardstate
*cs
, int cid
)
535 INIT_LIST_HEAD(&at_state
->list
);
536 at_state
->waiting
= 0;
537 at_state
->getstring
= 0;
538 at_state
->pending_commands
= 0;
539 at_state
->timer_expires
= 0;
540 at_state
->timer_active
= 0;
541 at_state
->timer_index
= 0;
542 at_state
->seq_index
= 0;
543 at_state
->ConState
= 0;
544 for (i
= 0; i
< STR_NUM
; ++i
)
545 at_state
->str_var
[i
] = NULL
;
546 at_state
->int_var
[VAR_ZDLE
] = 0;
547 at_state
->int_var
[VAR_ZCTP
] = -1;
548 at_state
->int_var
[VAR_ZSAU
] = ZSAU_NULL
;
553 at_state
->replystruct
= cs
->tabnocid
;
555 at_state
->replystruct
= cs
->tabcid
;
559 static void gigaset_inbuf_init(struct inbuf_t
*inbuf
, struct cardstate
*cs
)
560 /* inbuf->read must be allocated before! */
565 inbuf
->inputstate
= INS_command
;
569 * gigaset_fill_inbuf() - append received data to input buffer
570 * @inbuf: buffer structure.
571 * @src: received data.
572 * @numbytes: number of bytes received.
574 int gigaset_fill_inbuf(struct inbuf_t
*inbuf
, const unsigned char *src
,
577 unsigned n
, head
, tail
, bytesleft
;
579 gig_dbg(DEBUG_INTR
, "received %u bytes", numbytes
);
584 bytesleft
= numbytes
;
587 gig_dbg(DEBUG_INTR
, "buffer state: %u -> %u", head
, tail
);
593 n
= (RBUFSIZE
-1) - tail
;
597 dev_err(inbuf
->cs
->dev
,
598 "buffer overflow (%u bytes lost)\n",
604 memcpy(inbuf
->data
+ tail
, src
, n
);
606 tail
= (tail
+ n
) % RBUFSIZE
;
609 gig_dbg(DEBUG_INTR
, "setting tail to %u", tail
);
611 return numbytes
!= bytesleft
;
613 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf
);
615 /* Initialize the b-channel structure */
616 static struct bc_state
*gigaset_initbcs(struct bc_state
*bcs
,
617 struct cardstate
*cs
, int channel
)
623 skb_queue_head_init(&bcs
->squeue
);
629 gig_dbg(DEBUG_INIT
, "setting up bcs[%d]->at_state", channel
);
630 gigaset_at_init(&bcs
->at_state
, bcs
, cs
, -1);
632 #ifdef CONFIG_GIGASET_DEBUG
636 gig_dbg(DEBUG_INIT
, "allocating bcs[%d]->skb", channel
);
637 bcs
->fcs
= PPP_INITFCS
;
639 if (cs
->ignoreframes
) {
642 bcs
->skb
= dev_alloc_skb(SBUFSIZE
+ cs
->hw_hdr_len
);
643 if (bcs
->skb
!= NULL
)
644 skb_reserve(bcs
->skb
, cs
->hw_hdr_len
);
646 pr_err("out of memory\n");
649 bcs
->channel
= channel
;
655 bcs
->ignore
= cs
->ignoreframes
;
657 for (i
= 0; i
< AT_NUM
; ++i
)
658 bcs
->commands
[i
] = NULL
;
660 gig_dbg(DEBUG_INIT
, " setting up bcs[%d]->hw", channel
);
661 if (cs
->ops
->initbcshw(bcs
))
664 gig_dbg(DEBUG_INIT
, " failed");
666 gig_dbg(DEBUG_INIT
, " freeing bcs[%d]->skb", channel
);
667 dev_kfree_skb(bcs
->skb
);
674 * gigaset_initcs() - initialize device structure
675 * @drv: hardware driver the device belongs to
676 * @channels: number of B channels supported by device
677 * @onechannel: !=0 if B channel data and AT commands share one
678 * communication channel (M10x),
679 * ==0 if B channels have separate communication channels (base)
680 * @ignoreframes: number of frames to ignore after setting up B channel
681 * @cidmode: !=0: start in CallID mode
682 * @modulename: name of driver module for LL registration
684 * Allocate and initialize cardstate structure for Gigaset driver
685 * Calls hardware dependent gigaset_initcshw() function
686 * Calls B channel initialization function gigaset_initbcs() for each B channel
689 * pointer to cardstate structure
691 struct cardstate
*gigaset_initcs(struct gigaset_driver
*drv
, int channels
,
692 int onechannel
, int ignoreframes
,
693 int cidmode
, const char *modulename
)
695 struct cardstate
*cs
;
699 gig_dbg(DEBUG_INIT
, "allocating cs");
702 pr_err("maximum number of devices exceeded\n");
706 gig_dbg(DEBUG_INIT
, "allocating bcs[0..%d]", channels
- 1);
707 cs
->bcs
= kmalloc(channels
* sizeof(struct bc_state
), GFP_KERNEL
);
709 pr_err("out of memory\n");
712 gig_dbg(DEBUG_INIT
, "allocating inbuf");
713 cs
->inbuf
= kmalloc(sizeof(struct inbuf_t
), GFP_KERNEL
);
715 pr_err("out of memory\n");
720 cs
->channels
= channels
;
721 cs
->onechannel
= onechannel
;
722 cs
->ignoreframes
= ignoreframes
;
723 INIT_LIST_HEAD(&cs
->temp_at_states
);
725 init_timer(&cs
->timer
); /* clear next & prev */
726 spin_lock_init(&cs
->ev_lock
);
730 tasklet_init(&cs
->event_tasklet
, gigaset_handle_event
,
732 cs
->commands_pending
= 0;
739 cs
->cidmode
= cidmode
!= 0;
740 cs
->tabnocid
= gigaset_tab_nocid
;
741 cs
->tabcid
= gigaset_tab_cid
;
743 init_waitqueue_head(&cs
->waitqueue
);
746 cs
->mode
= M_UNKNOWN
;
747 cs
->mstate
= MS_UNINITIALIZED
;
751 gig_dbg(DEBUG_INIT
, "setting up at_state");
752 spin_lock_init(&cs
->lock
);
753 gigaset_at_init(&cs
->at_state
, NULL
, cs
, 0);
757 gig_dbg(DEBUG_INIT
, "setting up inbuf");
758 gigaset_inbuf_init(cs
->inbuf
, cs
);
763 gig_dbg(DEBUG_INIT
, "setting up cmdbuf");
764 cs
->cmdbuf
= cs
->lastcmdbuf
= NULL
;
765 spin_lock_init(&cs
->cmdlock
);
769 gig_dbg(DEBUG_INIT
, "setting up iif");
770 if (!gigaset_isdn_register(cs
, modulename
)) {
771 pr_err("error registering ISDN device\n");
775 make_valid(cs
, VALID_ID
);
777 gig_dbg(DEBUG_INIT
, "setting up hw");
778 if (!cs
->ops
->initcshw(cs
))
783 /* set up character device */
786 /* set up device sysfs */
787 gigaset_init_dev_sysfs(cs
);
789 /* set up channel data structures */
790 for (i
= 0; i
< channels
; ++i
) {
791 gig_dbg(DEBUG_INIT
, "setting up bcs[%d]", i
);
792 if (!gigaset_initbcs(cs
->bcs
+ i
, cs
, i
)) {
793 pr_err("could not allocate channel %d data\n", i
);
798 spin_lock_irqsave(&cs
->lock
, flags
);
800 spin_unlock_irqrestore(&cs
->lock
, flags
);
801 setup_timer(&cs
->timer
, timer_tick
, (unsigned long) cs
);
802 cs
->timer
.expires
= jiffies
+ msecs_to_jiffies(GIG_TICK
);
803 /* FIXME: can jiffies increase too much until the timer is added?
804 * Same problem(?) with mod_timer() in timer_tick(). */
805 add_timer(&cs
->timer
);
807 gig_dbg(DEBUG_INIT
, "cs initialized");
811 gig_dbg(DEBUG_INIT
, "failed");
815 EXPORT_SYMBOL_GPL(gigaset_initcs
);
817 /* ReInitialize the b-channel structure on hangup */
818 void gigaset_bcs_reinit(struct bc_state
*bcs
)
821 struct cardstate
*cs
= bcs
->cs
;
824 while ((skb
= skb_dequeue(&bcs
->squeue
)) != NULL
)
827 spin_lock_irqsave(&cs
->lock
, flags
);
828 clear_at_state(&bcs
->at_state
);
829 bcs
->at_state
.ConState
= 0;
830 bcs
->at_state
.timer_active
= 0;
831 bcs
->at_state
.timer_expires
= 0;
832 bcs
->at_state
.cid
= -1; /* No CID defined */
833 spin_unlock_irqrestore(&cs
->lock
, flags
);
837 #ifdef CONFIG_GIGASET_DEBUG
841 bcs
->fcs
= PPP_INITFCS
;
844 bcs
->ignore
= cs
->ignoreframes
;
846 dev_kfree_skb(bcs
->skb
);
850 cs
->ops
->reinitbcshw(bcs
);
853 static void cleanup_cs(struct cardstate
*cs
)
855 struct cmdbuf_t
*cb
, *tcb
;
859 spin_lock_irqsave(&cs
->lock
, flags
);
861 cs
->mode
= M_UNKNOWN
;
862 cs
->mstate
= MS_UNINITIALIZED
;
864 clear_at_state(&cs
->at_state
);
865 dealloc_at_states(cs
);
866 free_strings(&cs
->at_state
);
867 gigaset_at_init(&cs
->at_state
, NULL
, cs
, 0);
869 cs
->inbuf
->inputstate
= INS_command
;
879 cs
->cmdbuf
= cs
->lastcmdbuf
= NULL
;
885 cs
->commands_pending
= 0;
888 spin_unlock_irqrestore(&cs
->lock
, flags
);
890 for (i
= 0; i
< cs
->channels
; ++i
) {
891 gigaset_freebcs(cs
->bcs
+ i
);
892 if (!gigaset_initbcs(cs
->bcs
+ i
, cs
, i
))
893 pr_err("could not allocate channel %d data\n", i
);
897 cs
->cmd_result
= -ENODEV
;
899 wake_up_interruptible(&cs
->waitqueue
);
905 * gigaset_start() - start device operations
906 * @cs: device descriptor structure.
908 * Prepares the device for use by setting up communication parameters,
909 * scheduling an EV_START event to initiate device initialization, and
910 * waiting for completion of the initialization.
913 * 1 - success, 0 - error
915 int gigaset_start(struct cardstate
*cs
)
919 if (mutex_lock_interruptible(&cs
->mutex
))
922 spin_lock_irqsave(&cs
->lock
, flags
);
924 spin_unlock_irqrestore(&cs
->lock
, flags
);
926 if (cs
->mstate
!= MS_LOCKED
) {
927 cs
->ops
->set_modem_ctrl(cs
, 0, TIOCM_DTR
|TIOCM_RTS
);
928 cs
->ops
->baud_rate(cs
, B115200
);
929 cs
->ops
->set_line_ctrl(cs
, CS8
);
930 cs
->control_state
= TIOCM_DTR
|TIOCM_RTS
;
935 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_START
, NULL
, 0, NULL
)) {
937 dev_err(cs
->dev
, "%s: out of memory\n", __func__
);
941 gig_dbg(DEBUG_CMD
, "scheduling START");
942 gigaset_schedule_event(cs
);
944 wait_event(cs
->waitqueue
, !cs
->waiting
);
946 mutex_unlock(&cs
->mutex
);
950 mutex_unlock(&cs
->mutex
);
953 EXPORT_SYMBOL_GPL(gigaset_start
);
956 * gigaset_shutdown() - shut down device operations
957 * @cs: device descriptor structure.
959 * Deactivates the device by scheduling an EV_SHUTDOWN event and
960 * waiting for completion of the shutdown.
963 * 0 - success, -1 - error (no device associated)
965 int gigaset_shutdown(struct cardstate
*cs
)
967 mutex_lock(&cs
->mutex
);
969 if (!(cs
->flags
& VALID_MINOR
)) {
970 mutex_unlock(&cs
->mutex
);
976 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_SHUTDOWN
, NULL
, 0, NULL
)) {
977 dev_err(cs
->dev
, "%s: out of memory\n", __func__
);
981 gig_dbg(DEBUG_CMD
, "scheduling SHUTDOWN");
982 gigaset_schedule_event(cs
);
984 wait_event(cs
->waitqueue
, !cs
->waiting
);
989 mutex_unlock(&cs
->mutex
);
992 EXPORT_SYMBOL_GPL(gigaset_shutdown
);
995 * gigaset_stop() - stop device operations
996 * @cs: device descriptor structure.
998 * Stops operations on the device by scheduling an EV_STOP event and
999 * waiting for completion of the shutdown.
1001 void gigaset_stop(struct cardstate
*cs
)
1003 mutex_lock(&cs
->mutex
);
1007 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_STOP
, NULL
, 0, NULL
)) {
1008 dev_err(cs
->dev
, "%s: out of memory\n", __func__
);
1012 gig_dbg(DEBUG_CMD
, "scheduling STOP");
1013 gigaset_schedule_event(cs
);
1015 wait_event(cs
->waitqueue
, !cs
->waiting
);
1020 mutex_unlock(&cs
->mutex
);
1022 EXPORT_SYMBOL_GPL(gigaset_stop
);
1024 static LIST_HEAD(drivers
);
1025 static DEFINE_SPINLOCK(driver_lock
);
1027 struct cardstate
*gigaset_get_cs_by_id(int id
)
1029 unsigned long flags
;
1030 struct cardstate
*ret
= NULL
;
1031 struct cardstate
*cs
;
1032 struct gigaset_driver
*drv
;
1035 spin_lock_irqsave(&driver_lock
, flags
);
1036 list_for_each_entry(drv
, &drivers
, list
) {
1037 spin_lock(&drv
->lock
);
1038 for (i
= 0; i
< drv
->minors
; ++i
) {
1040 if ((cs
->flags
& VALID_ID
) && cs
->myid
== id
) {
1045 spin_unlock(&drv
->lock
);
1049 spin_unlock_irqrestore(&driver_lock
, flags
);
1053 void gigaset_debugdrivers(void)
1055 unsigned long flags
;
1056 static struct cardstate
*cs
;
1057 struct gigaset_driver
*drv
;
1060 spin_lock_irqsave(&driver_lock
, flags
);
1061 list_for_each_entry(drv
, &drivers
, list
) {
1062 gig_dbg(DEBUG_DRIVER
, "driver %p", drv
);
1063 spin_lock(&drv
->lock
);
1064 for (i
= 0; i
< drv
->minors
; ++i
) {
1065 gig_dbg(DEBUG_DRIVER
, " index %u", i
);
1067 gig_dbg(DEBUG_DRIVER
, " cardstate %p", cs
);
1068 gig_dbg(DEBUG_DRIVER
, " flags 0x%02x", cs
->flags
);
1069 gig_dbg(DEBUG_DRIVER
, " minor_index %u",
1071 gig_dbg(DEBUG_DRIVER
, " driver %p", cs
->driver
);
1072 gig_dbg(DEBUG_DRIVER
, " i4l id %d", cs
->myid
);
1074 spin_unlock(&drv
->lock
);
1076 spin_unlock_irqrestore(&driver_lock
, flags
);
1079 static struct cardstate
*gigaset_get_cs_by_minor(unsigned minor
)
1081 unsigned long flags
;
1082 struct cardstate
*ret
= NULL
;
1083 struct gigaset_driver
*drv
;
1086 spin_lock_irqsave(&driver_lock
, flags
);
1087 list_for_each_entry(drv
, &drivers
, list
) {
1088 if (minor
< drv
->minor
|| minor
>= drv
->minor
+ drv
->minors
)
1090 index
= minor
- drv
->minor
;
1091 spin_lock(&drv
->lock
);
1092 if (drv
->cs
[index
].flags
& VALID_MINOR
)
1093 ret
= drv
->cs
+ index
;
1094 spin_unlock(&drv
->lock
);
1098 spin_unlock_irqrestore(&driver_lock
, flags
);
1102 struct cardstate
*gigaset_get_cs_by_tty(struct tty_struct
*tty
)
1104 if (tty
->index
< 0 || tty
->index
>= tty
->driver
->num
)
1106 return gigaset_get_cs_by_minor(tty
->index
+ tty
->driver
->minor_start
);
1110 * gigaset_freedriver() - free all associated ressources of a driver
1111 * @drv: driver descriptor structure.
1113 * Unregisters the driver from the system and deallocates the driver
1114 * structure @drv and all structures referenced from it.
1115 * All devices should be shut down before calling this.
1117 void gigaset_freedriver(struct gigaset_driver
*drv
)
1119 unsigned long flags
;
1121 spin_lock_irqsave(&driver_lock
, flags
);
1122 list_del(&drv
->list
);
1123 spin_unlock_irqrestore(&driver_lock
, flags
);
1125 gigaset_if_freedriver(drv
);
1130 EXPORT_SYMBOL_GPL(gigaset_freedriver
);
1133 * gigaset_initdriver() - initialize driver structure
1134 * @minor: First minor number
1135 * @minors: Number of minors this driver can handle
1136 * @procname: Name of the driver
1137 * @devname: Name of the device files (prefix without minor number)
1139 * Allocate and initialize gigaset_driver structure. Initialize interface.
1142 * Pointer to the gigaset_driver structure on success, NULL on failure.
1144 struct gigaset_driver
*gigaset_initdriver(unsigned minor
, unsigned minors
,
1145 const char *procname
,
1146 const char *devname
,
1147 const struct gigaset_ops
*ops
,
1148 struct module
*owner
)
1150 struct gigaset_driver
*drv
;
1151 unsigned long flags
;
1154 drv
= kmalloc(sizeof *drv
, GFP_KERNEL
);
1160 drv
->minors
= minors
;
1161 spin_lock_init(&drv
->lock
);
1165 INIT_LIST_HEAD(&drv
->list
);
1167 drv
->cs
= kmalloc(minors
* sizeof *drv
->cs
, GFP_KERNEL
);
1171 for (i
= 0; i
< minors
; ++i
) {
1172 drv
->cs
[i
].flags
= 0;
1173 drv
->cs
[i
].driver
= drv
;
1174 drv
->cs
[i
].ops
= drv
->ops
;
1175 drv
->cs
[i
].minor_index
= i
;
1176 mutex_init(&drv
->cs
[i
].mutex
);
1179 gigaset_if_initdriver(drv
, procname
, devname
);
1181 spin_lock_irqsave(&driver_lock
, flags
);
1182 list_add(&drv
->list
, &drivers
);
1183 spin_unlock_irqrestore(&driver_lock
, flags
);
1192 EXPORT_SYMBOL_GPL(gigaset_initdriver
);
1195 * gigaset_blockdriver() - block driver
1196 * @drv: driver descriptor structure.
1198 * Prevents the driver from attaching new devices, in preparation for
1201 void gigaset_blockdriver(struct gigaset_driver
*drv
)
1205 EXPORT_SYMBOL_GPL(gigaset_blockdriver
);
1207 static int __init
gigaset_init_module(void)
1209 /* in accordance with the principle of least astonishment,
1210 * setting the 'debug' parameter to 1 activates a sensible
1211 * set of default debug levels
1213 if (gigaset_debuglevel
== 1)
1214 gigaset_debuglevel
= DEBUG_DEFAULT
;
1216 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG
"\n");
1220 static void __exit
gigaset_exit_module(void)
1224 module_init(gigaset_init_module
);
1225 module_exit(gigaset_exit_module
);
1227 MODULE_AUTHOR(DRIVER_AUTHOR
);
1228 MODULE_DESCRIPTION(DRIVER_DESC
);
1230 MODULE_LICENSE("GPL");