1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Stuff used by all variants of the driver
5 * Copyright (c) 2001 by Stefan Eilers,
6 * Hansjoerg Lipp <hjlipp@web.de>,
7 * Tilman Schmidt <tilman@imap.cc>.
9 * =====================================================================
10 * =====================================================================
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
17 /* Version Information */
18 #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
19 #define DRIVER_DESC "Driver for Gigaset 307x"
21 #ifdef CONFIG_GIGASET_DEBUG
22 #define DRIVER_DESC_DEBUG " (debug build)"
24 #define DRIVER_DESC_DEBUG ""
27 /* Module parameters */
28 int gigaset_debuglevel
;
29 EXPORT_SYMBOL_GPL(gigaset_debuglevel
);
30 module_param_named(debug
, gigaset_debuglevel
, int, S_IRUGO
| S_IWUSR
);
31 MODULE_PARM_DESC(debug
, "debug level");
33 /* driver state flags */
34 #define VALID_MINOR 0x01
38 * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging
39 * @level: debugging level.
40 * @msg: message prefix.
41 * @len: number of bytes to dump.
44 * If the current debugging level includes one of the bits set in @level,
45 * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio,
46 * prefixed by the text @msg.
48 void gigaset_dbg_buffer(enum debuglevel level
, const unsigned char *msg
,
49 size_t len
, const unsigned char *buf
)
51 unsigned char outbuf
[80];
53 size_t space
= sizeof outbuf
- 1;
54 unsigned char *out
= outbuf
;
59 if (c
== '~' || c
== '^' || c
== '\\') {
70 if (c
< 0x20 || c
== 0x7f) {
82 gig_dbg(level
, "%s (%u bytes): %s", msg
, (unsigned) len
, outbuf
);
84 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer
);
86 static int setflags(struct cardstate
*cs
, unsigned flags
, unsigned delay
)
90 r
= cs
->ops
->set_modem_ctrl(cs
, cs
->control_state
, flags
);
91 cs
->control_state
= flags
;
96 set_current_state(TASK_INTERRUPTIBLE
);
97 schedule_timeout(delay
* HZ
/ 1000);
103 int gigaset_enterconfigmode(struct cardstate
*cs
)
107 cs
->control_state
= TIOCM_RTS
;
109 r
= setflags(cs
, TIOCM_DTR
, 200);
112 r
= setflags(cs
, 0, 200);
115 for (i
= 0; i
< 5; ++i
) {
116 r
= setflags(cs
, TIOCM_RTS
, 100);
119 r
= setflags(cs
, 0, 100);
123 r
= setflags(cs
, TIOCM_RTS
| TIOCM_DTR
, 800);
130 dev_err(cs
->dev
, "error %d on setuartbits\n", -r
);
131 cs
->control_state
= TIOCM_RTS
| TIOCM_DTR
;
132 cs
->ops
->set_modem_ctrl(cs
, 0, TIOCM_RTS
| TIOCM_DTR
);
137 static int test_timeout(struct at_state_t
*at_state
)
139 if (!at_state
->timer_expires
)
142 if (--at_state
->timer_expires
) {
143 gig_dbg(DEBUG_MCMD
, "decreased timer of %p to %lu",
144 at_state
, at_state
->timer_expires
);
148 gigaset_add_event(at_state
->cs
, at_state
, EV_TIMEOUT
, NULL
,
149 at_state
->timer_index
, NULL
);
153 static void timer_tick(struct timer_list
*t
)
155 struct cardstate
*cs
= from_timer(cs
, t
, timer
);
158 struct at_state_t
*at_state
;
161 spin_lock_irqsave(&cs
->lock
, flags
);
163 for (channel
= 0; channel
< cs
->channels
; ++channel
)
164 if (test_timeout(&cs
->bcs
[channel
].at_state
))
167 if (test_timeout(&cs
->at_state
))
170 list_for_each_entry(at_state
, &cs
->temp_at_states
, list
)
171 if (test_timeout(at_state
))
175 mod_timer(&cs
->timer
, jiffies
+ msecs_to_jiffies(GIG_TICK
));
177 gig_dbg(DEBUG_EVENT
, "scheduling timeout");
178 tasklet_schedule(&cs
->event_tasklet
);
182 spin_unlock_irqrestore(&cs
->lock
, flags
);
185 int gigaset_get_channel(struct bc_state
*bcs
)
189 spin_lock_irqsave(&bcs
->cs
->lock
, flags
);
190 if (bcs
->use_count
|| !try_module_get(bcs
->cs
->driver
->owner
)) {
191 gig_dbg(DEBUG_CHANNEL
, "could not allocate channel %d",
193 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
198 gig_dbg(DEBUG_CHANNEL
, "allocated channel %d", bcs
->channel
);
199 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
203 struct bc_state
*gigaset_get_free_channel(struct cardstate
*cs
)
208 spin_lock_irqsave(&cs
->lock
, flags
);
209 if (!try_module_get(cs
->driver
->owner
)) {
210 gig_dbg(DEBUG_CHANNEL
,
211 "could not get module for allocating channel");
212 spin_unlock_irqrestore(&cs
->lock
, flags
);
215 for (i
= 0; i
< cs
->channels
; ++i
)
216 if (!cs
->bcs
[i
].use_count
) {
217 ++cs
->bcs
[i
].use_count
;
219 spin_unlock_irqrestore(&cs
->lock
, flags
);
220 gig_dbg(DEBUG_CHANNEL
, "allocated channel %d", i
);
223 module_put(cs
->driver
->owner
);
224 spin_unlock_irqrestore(&cs
->lock
, flags
);
225 gig_dbg(DEBUG_CHANNEL
, "no free channel");
229 void gigaset_free_channel(struct bc_state
*bcs
)
233 spin_lock_irqsave(&bcs
->cs
->lock
, flags
);
235 gig_dbg(DEBUG_CHANNEL
, "could not free channel %d",
237 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
242 module_put(bcs
->cs
->driver
->owner
);
243 gig_dbg(DEBUG_CHANNEL
, "freed channel %d", bcs
->channel
);
244 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
247 int gigaset_get_channels(struct cardstate
*cs
)
252 spin_lock_irqsave(&cs
->lock
, flags
);
253 for (i
= 0; i
< cs
->channels
; ++i
)
254 if (cs
->bcs
[i
].use_count
) {
255 spin_unlock_irqrestore(&cs
->lock
, flags
);
256 gig_dbg(DEBUG_CHANNEL
,
257 "could not allocate all channels");
260 for (i
= 0; i
< cs
->channels
; ++i
)
261 ++cs
->bcs
[i
].use_count
;
262 spin_unlock_irqrestore(&cs
->lock
, flags
);
264 gig_dbg(DEBUG_CHANNEL
, "allocated all channels");
269 void gigaset_free_channels(struct cardstate
*cs
)
274 gig_dbg(DEBUG_CHANNEL
, "unblocking all channels");
275 spin_lock_irqsave(&cs
->lock
, flags
);
276 for (i
= 0; i
< cs
->channels
; ++i
)
277 --cs
->bcs
[i
].use_count
;
278 spin_unlock_irqrestore(&cs
->lock
, flags
);
281 void gigaset_block_channels(struct cardstate
*cs
)
286 gig_dbg(DEBUG_CHANNEL
, "blocking all channels");
287 spin_lock_irqsave(&cs
->lock
, flags
);
288 for (i
= 0; i
< cs
->channels
; ++i
)
289 ++cs
->bcs
[i
].use_count
;
290 spin_unlock_irqrestore(&cs
->lock
, flags
);
293 static void clear_events(struct cardstate
*cs
)
299 spin_lock_irqsave(&cs
->ev_lock
, flags
);
304 while (tail
!= head
) {
305 ev
= cs
->events
+ head
;
307 head
= (head
+ 1) % MAX_EVENTS
;
312 spin_unlock_irqrestore(&cs
->ev_lock
, flags
);
316 * gigaset_add_event() - add event to device event queue
317 * @cs: device descriptor structure.
318 * @at_state: connection state structure.
320 * @ptr: pointer parameter for event.
321 * @parameter: integer parameter for event.
322 * @arg: pointer parameter for event.
324 * Allocate an event queue entry from the device's event queue, and set it up
325 * with the parameters given.
327 * Return value: added event
329 struct event_t
*gigaset_add_event(struct cardstate
*cs
,
330 struct at_state_t
*at_state
, int type
,
331 void *ptr
, int parameter
, void *arg
)
335 struct event_t
*event
= NULL
;
337 gig_dbg(DEBUG_EVENT
, "queueing event %d", type
);
339 spin_lock_irqsave(&cs
->ev_lock
, flags
);
342 next
= (tail
+ 1) % MAX_EVENTS
;
343 if (unlikely(next
== cs
->ev_head
))
344 dev_err(cs
->dev
, "event queue full\n");
346 event
= cs
->events
+ tail
;
348 event
->at_state
= at_state
;
352 event
->parameter
= parameter
;
356 spin_unlock_irqrestore(&cs
->ev_lock
, flags
);
360 EXPORT_SYMBOL_GPL(gigaset_add_event
);
362 static void clear_at_state(struct at_state_t
*at_state
)
366 for (i
= 0; i
< STR_NUM
; ++i
) {
367 kfree(at_state
->str_var
[i
]);
368 at_state
->str_var
[i
] = NULL
;
372 static void dealloc_temp_at_states(struct cardstate
*cs
)
374 struct at_state_t
*cur
, *next
;
376 list_for_each_entry_safe(cur
, next
, &cs
->temp_at_states
, list
) {
377 list_del(&cur
->list
);
383 static void gigaset_freebcs(struct bc_state
*bcs
)
387 gig_dbg(DEBUG_INIT
, "freeing bcs[%d]->hw", bcs
->channel
);
388 bcs
->cs
->ops
->freebcshw(bcs
);
390 gig_dbg(DEBUG_INIT
, "clearing bcs[%d]->at_state", bcs
->channel
);
391 clear_at_state(&bcs
->at_state
);
392 gig_dbg(DEBUG_INIT
, "freeing bcs[%d]->skb", bcs
->channel
);
393 dev_kfree_skb(bcs
->rx_skb
);
396 for (i
= 0; i
< AT_NUM
; ++i
) {
397 kfree(bcs
->commands
[i
]);
398 bcs
->commands
[i
] = NULL
;
402 static struct cardstate
*alloc_cs(struct gigaset_driver
*drv
)
406 struct cardstate
*cs
;
407 struct cardstate
*ret
= NULL
;
409 spin_lock_irqsave(&drv
->lock
, flags
);
412 for (i
= 0; i
< drv
->minors
; ++i
) {
414 if (!(cs
->flags
& VALID_MINOR
)) {
415 cs
->flags
= VALID_MINOR
;
421 spin_unlock_irqrestore(&drv
->lock
, flags
);
425 static void free_cs(struct cardstate
*cs
)
430 static void make_valid(struct cardstate
*cs
, unsigned mask
)
433 struct gigaset_driver
*drv
= cs
->driver
;
434 spin_lock_irqsave(&drv
->lock
, flags
);
436 spin_unlock_irqrestore(&drv
->lock
, flags
);
439 static void make_invalid(struct cardstate
*cs
, unsigned mask
)
442 struct gigaset_driver
*drv
= cs
->driver
;
443 spin_lock_irqsave(&drv
->lock
, flags
);
445 spin_unlock_irqrestore(&drv
->lock
, flags
);
449 * gigaset_freecs() - free all associated ressources of a device
450 * @cs: device descriptor structure.
452 * Stops all tasklets and timers, unregisters the device from all
453 * subsystems it was registered to, deallocates the device structure
454 * @cs and all structures referenced from it.
455 * Operations on the device should be stopped before calling this.
457 void gigaset_freecs(struct cardstate
*cs
)
465 mutex_lock(&cs
->mutex
);
467 spin_lock_irqsave(&cs
->lock
, flags
);
469 spin_unlock_irqrestore(&cs
->lock
, flags
); /* event handler and timer are
470 not rescheduled below */
472 tasklet_kill(&cs
->event_tasklet
);
473 del_timer_sync(&cs
->timer
);
475 switch (cs
->cs_init
) {
477 /* clear B channel structures */
478 for (i
= 0; i
< cs
->channels
; ++i
) {
479 gig_dbg(DEBUG_INIT
, "clearing bcs[%d]", i
);
480 gigaset_freebcs(cs
->bcs
+ i
);
483 /* clear device sysfs */
484 gigaset_free_dev_sysfs(cs
);
488 gig_dbg(DEBUG_INIT
, "clearing hw");
489 cs
->ops
->freecshw(cs
);
492 case 2: /* error in initcshw */
493 /* Deregister from LL */
494 make_invalid(cs
, VALID_ID
);
495 gigaset_isdn_unregdev(cs
);
498 case 1: /* error when registering to LL */
499 gig_dbg(DEBUG_INIT
, "clearing at_state");
500 clear_at_state(&cs
->at_state
);
501 dealloc_temp_at_states(cs
);
503 tty_port_destroy(&cs
->port
);
506 case 0: /* error in basic setup */
507 gig_dbg(DEBUG_INIT
, "freeing inbuf");
512 mutex_unlock(&cs
->mutex
);
515 EXPORT_SYMBOL_GPL(gigaset_freecs
);
517 void gigaset_at_init(struct at_state_t
*at_state
, struct bc_state
*bcs
,
518 struct cardstate
*cs
, int cid
)
522 INIT_LIST_HEAD(&at_state
->list
);
523 at_state
->waiting
= 0;
524 at_state
->getstring
= 0;
525 at_state
->pending_commands
= 0;
526 at_state
->timer_expires
= 0;
527 at_state
->timer_active
= 0;
528 at_state
->timer_index
= 0;
529 at_state
->seq_index
= 0;
530 at_state
->ConState
= 0;
531 for (i
= 0; i
< STR_NUM
; ++i
)
532 at_state
->str_var
[i
] = NULL
;
533 at_state
->int_var
[VAR_ZDLE
] = 0;
534 at_state
->int_var
[VAR_ZCTP
] = -1;
535 at_state
->int_var
[VAR_ZSAU
] = ZSAU_NULL
;
540 at_state
->replystruct
= cs
->tabnocid
;
542 at_state
->replystruct
= cs
->tabcid
;
546 static void gigaset_inbuf_init(struct inbuf_t
*inbuf
, struct cardstate
*cs
)
547 /* inbuf->read must be allocated before! */
552 inbuf
->inputstate
= INS_command
;
556 * gigaset_fill_inbuf() - append received data to input buffer
557 * @inbuf: buffer structure.
558 * @src: received data.
559 * @numbytes: number of bytes received.
561 * Return value: !=0 if some data was appended
563 int gigaset_fill_inbuf(struct inbuf_t
*inbuf
, const unsigned char *src
,
566 unsigned n
, head
, tail
, bytesleft
;
568 gig_dbg(DEBUG_INTR
, "received %u bytes", numbytes
);
573 bytesleft
= numbytes
;
576 gig_dbg(DEBUG_INTR
, "buffer state: %u -> %u", head
, tail
);
582 n
= (RBUFSIZE
- 1) - tail
;
586 dev_err(inbuf
->cs
->dev
,
587 "buffer overflow (%u bytes lost)\n",
593 memcpy(inbuf
->data
+ tail
, src
, n
);
595 tail
= (tail
+ n
) % RBUFSIZE
;
598 gig_dbg(DEBUG_INTR
, "setting tail to %u", tail
);
600 return numbytes
!= bytesleft
;
602 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf
);
604 /* Initialize the b-channel structure */
605 static int gigaset_initbcs(struct bc_state
*bcs
, struct cardstate
*cs
,
612 skb_queue_head_init(&bcs
->squeue
);
618 gig_dbg(DEBUG_INIT
, "setting up bcs[%d]->at_state", channel
);
619 gigaset_at_init(&bcs
->at_state
, bcs
, cs
, -1);
621 #ifdef CONFIG_GIGASET_DEBUG
627 bcs
->rx_fcs
= PPP_INITFCS
;
629 bcs
->channel
= channel
;
635 bcs
->ignore
= cs
->ignoreframes
;
637 for (i
= 0; i
< AT_NUM
; ++i
)
638 bcs
->commands
[i
] = NULL
;
640 spin_lock_init(&bcs
->aplock
);
642 bcs
->apconnstate
= 0;
644 gig_dbg(DEBUG_INIT
, " setting up bcs[%d]->hw", channel
);
645 return cs
->ops
->initbcshw(bcs
);
649 * gigaset_initcs() - initialize device structure
650 * @drv: hardware driver the device belongs to
651 * @channels: number of B channels supported by device
652 * @onechannel: !=0 if B channel data and AT commands share one
653 * communication channel (M10x),
654 * ==0 if B channels have separate communication channels (base)
655 * @ignoreframes: number of frames to ignore after setting up B channel
656 * @cidmode: !=0: start in CallID mode
657 * @modulename: name of driver module for LL registration
659 * Allocate and initialize cardstate structure for Gigaset driver
660 * Calls hardware dependent gigaset_initcshw() function
661 * Calls B channel initialization function gigaset_initbcs() for each B channel
664 * pointer to cardstate structure
666 struct cardstate
*gigaset_initcs(struct gigaset_driver
*drv
, int channels
,
667 int onechannel
, int ignoreframes
,
668 int cidmode
, const char *modulename
)
670 struct cardstate
*cs
;
674 gig_dbg(DEBUG_INIT
, "allocating cs");
677 pr_err("maximum number of devices exceeded\n");
682 cs
->channels
= channels
;
683 cs
->onechannel
= onechannel
;
684 cs
->ignoreframes
= ignoreframes
;
685 INIT_LIST_HEAD(&cs
->temp_at_states
);
687 timer_setup(&cs
->timer
, timer_tick
, 0);
688 spin_lock_init(&cs
->ev_lock
);
692 tasklet_init(&cs
->event_tasklet
, gigaset_handle_event
,
694 tty_port_init(&cs
->port
);
695 cs
->commands_pending
= 0;
700 cs
->cidmode
= cidmode
!= 0;
701 cs
->tabnocid
= gigaset_tab_nocid
;
702 cs
->tabcid
= gigaset_tab_cid
;
704 init_waitqueue_head(&cs
->waitqueue
);
707 cs
->mode
= M_UNKNOWN
;
708 cs
->mstate
= MS_UNINITIALIZED
;
710 cs
->bcs
= kmalloc_array(channels
, sizeof(struct bc_state
), GFP_KERNEL
);
711 cs
->inbuf
= kmalloc(sizeof(struct inbuf_t
), GFP_KERNEL
);
712 if (!cs
->bcs
|| !cs
->inbuf
) {
713 pr_err("out of memory\n");
718 gig_dbg(DEBUG_INIT
, "setting up at_state");
719 spin_lock_init(&cs
->lock
);
720 gigaset_at_init(&cs
->at_state
, NULL
, cs
, 0);
724 gig_dbg(DEBUG_INIT
, "setting up inbuf");
725 gigaset_inbuf_init(cs
->inbuf
, cs
);
730 gig_dbg(DEBUG_INIT
, "setting up cmdbuf");
731 cs
->cmdbuf
= cs
->lastcmdbuf
= NULL
;
732 spin_lock_init(&cs
->cmdlock
);
736 gig_dbg(DEBUG_INIT
, "setting up iif");
737 if (gigaset_isdn_regdev(cs
, modulename
) < 0) {
738 pr_err("error registering ISDN device\n");
742 make_valid(cs
, VALID_ID
);
744 gig_dbg(DEBUG_INIT
, "setting up hw");
745 if (cs
->ops
->initcshw(cs
) < 0)
750 /* set up character device */
753 /* set up device sysfs */
754 gigaset_init_dev_sysfs(cs
);
756 /* set up channel data structures */
757 for (i
= 0; i
< channels
; ++i
) {
758 gig_dbg(DEBUG_INIT
, "setting up bcs[%d]", i
);
759 if (gigaset_initbcs(cs
->bcs
+ i
, cs
, i
) < 0) {
760 pr_err("could not allocate channel %d data\n", i
);
765 spin_lock_irqsave(&cs
->lock
, flags
);
767 spin_unlock_irqrestore(&cs
->lock
, flags
);
768 cs
->timer
.expires
= jiffies
+ msecs_to_jiffies(GIG_TICK
);
769 add_timer(&cs
->timer
);
771 gig_dbg(DEBUG_INIT
, "cs initialized");
775 gig_dbg(DEBUG_INIT
, "failed");
779 EXPORT_SYMBOL_GPL(gigaset_initcs
);
781 /* ReInitialize the b-channel structure on hangup */
782 void gigaset_bcs_reinit(struct bc_state
*bcs
)
785 struct cardstate
*cs
= bcs
->cs
;
788 while ((skb
= skb_dequeue(&bcs
->squeue
)) != NULL
)
791 spin_lock_irqsave(&cs
->lock
, flags
);
792 clear_at_state(&bcs
->at_state
);
793 bcs
->at_state
.ConState
= 0;
794 bcs
->at_state
.timer_active
= 0;
795 bcs
->at_state
.timer_expires
= 0;
796 bcs
->at_state
.cid
= -1; /* No CID defined */
797 spin_unlock_irqrestore(&cs
->lock
, flags
);
801 #ifdef CONFIG_GIGASET_DEBUG
805 bcs
->rx_fcs
= PPP_INITFCS
;
808 bcs
->ignore
= cs
->ignoreframes
;
809 dev_kfree_skb(bcs
->rx_skb
);
812 cs
->ops
->reinitbcshw(bcs
);
815 static void cleanup_cs(struct cardstate
*cs
)
817 struct cmdbuf_t
*cb
, *tcb
;
821 spin_lock_irqsave(&cs
->lock
, flags
);
823 cs
->mode
= M_UNKNOWN
;
824 cs
->mstate
= MS_UNINITIALIZED
;
826 clear_at_state(&cs
->at_state
);
827 dealloc_temp_at_states(cs
);
828 gigaset_at_init(&cs
->at_state
, NULL
, cs
, 0);
830 cs
->inbuf
->inputstate
= INS_command
;
840 cs
->cmdbuf
= cs
->lastcmdbuf
= NULL
;
846 cs
->commands_pending
= 0;
849 spin_unlock_irqrestore(&cs
->lock
, flags
);
851 for (i
= 0; i
< cs
->channels
; ++i
) {
852 gigaset_freebcs(cs
->bcs
+ i
);
853 if (gigaset_initbcs(cs
->bcs
+ i
, cs
, i
) < 0)
854 pr_err("could not allocate channel %d data\n", i
);
858 cs
->cmd_result
= -ENODEV
;
860 wake_up_interruptible(&cs
->waitqueue
);
866 * gigaset_start() - start device operations
867 * @cs: device descriptor structure.
869 * Prepares the device for use by setting up communication parameters,
870 * scheduling an EV_START event to initiate device initialization, and
871 * waiting for completion of the initialization.
874 * 0 on success, error code < 0 on failure
876 int gigaset_start(struct cardstate
*cs
)
880 if (mutex_lock_interruptible(&cs
->mutex
))
883 spin_lock_irqsave(&cs
->lock
, flags
);
885 spin_unlock_irqrestore(&cs
->lock
, flags
);
887 if (cs
->mstate
!= MS_LOCKED
) {
888 cs
->ops
->set_modem_ctrl(cs
, 0, TIOCM_DTR
| TIOCM_RTS
);
889 cs
->ops
->baud_rate(cs
, B115200
);
890 cs
->ops
->set_line_ctrl(cs
, CS8
);
891 cs
->control_state
= TIOCM_DTR
| TIOCM_RTS
;
896 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_START
, NULL
, 0, NULL
)) {
900 gigaset_schedule_event(cs
);
902 wait_event(cs
->waitqueue
, !cs
->waiting
);
904 mutex_unlock(&cs
->mutex
);
908 mutex_unlock(&cs
->mutex
);
911 EXPORT_SYMBOL_GPL(gigaset_start
);
914 * gigaset_shutdown() - shut down device operations
915 * @cs: device descriptor structure.
917 * Deactivates the device by scheduling an EV_SHUTDOWN event and
918 * waiting for completion of the shutdown.
921 * 0 - success, -ENODEV - error (no device associated)
923 int gigaset_shutdown(struct cardstate
*cs
)
925 mutex_lock(&cs
->mutex
);
927 if (!(cs
->flags
& VALID_MINOR
)) {
928 mutex_unlock(&cs
->mutex
);
934 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_SHUTDOWN
, NULL
, 0, NULL
))
936 gigaset_schedule_event(cs
);
938 wait_event(cs
->waitqueue
, !cs
->waiting
);
943 mutex_unlock(&cs
->mutex
);
946 EXPORT_SYMBOL_GPL(gigaset_shutdown
);
949 * gigaset_stop() - stop device operations
950 * @cs: device descriptor structure.
952 * Stops operations on the device by scheduling an EV_STOP event and
953 * waiting for completion of the shutdown.
955 void gigaset_stop(struct cardstate
*cs
)
957 mutex_lock(&cs
->mutex
);
961 if (!gigaset_add_event(cs
, &cs
->at_state
, EV_STOP
, NULL
, 0, NULL
))
963 gigaset_schedule_event(cs
);
965 wait_event(cs
->waitqueue
, !cs
->waiting
);
970 mutex_unlock(&cs
->mutex
);
972 EXPORT_SYMBOL_GPL(gigaset_stop
);
974 static LIST_HEAD(drivers
);
975 static DEFINE_SPINLOCK(driver_lock
);
977 struct cardstate
*gigaset_get_cs_by_id(int id
)
980 struct cardstate
*ret
= NULL
;
981 struct cardstate
*cs
;
982 struct gigaset_driver
*drv
;
985 spin_lock_irqsave(&driver_lock
, flags
);
986 list_for_each_entry(drv
, &drivers
, list
) {
987 spin_lock(&drv
->lock
);
988 for (i
= 0; i
< drv
->minors
; ++i
) {
990 if ((cs
->flags
& VALID_ID
) && cs
->myid
== id
) {
995 spin_unlock(&drv
->lock
);
999 spin_unlock_irqrestore(&driver_lock
, flags
);
1003 static struct cardstate
*gigaset_get_cs_by_minor(unsigned minor
)
1005 unsigned long flags
;
1006 struct cardstate
*ret
= NULL
;
1007 struct gigaset_driver
*drv
;
1010 spin_lock_irqsave(&driver_lock
, flags
);
1011 list_for_each_entry(drv
, &drivers
, list
) {
1012 if (minor
< drv
->minor
|| minor
>= drv
->minor
+ drv
->minors
)
1014 index
= minor
- drv
->minor
;
1015 spin_lock(&drv
->lock
);
1016 if (drv
->cs
[index
].flags
& VALID_MINOR
)
1017 ret
= drv
->cs
+ index
;
1018 spin_unlock(&drv
->lock
);
1022 spin_unlock_irqrestore(&driver_lock
, flags
);
1026 struct cardstate
*gigaset_get_cs_by_tty(struct tty_struct
*tty
)
1028 return gigaset_get_cs_by_minor(tty
->index
+ tty
->driver
->minor_start
);
1032 * gigaset_freedriver() - free all associated ressources of a driver
1033 * @drv: driver descriptor structure.
1035 * Unregisters the driver from the system and deallocates the driver
1036 * structure @drv and all structures referenced from it.
1037 * All devices should be shut down before calling this.
1039 void gigaset_freedriver(struct gigaset_driver
*drv
)
1041 unsigned long flags
;
1043 spin_lock_irqsave(&driver_lock
, flags
);
1044 list_del(&drv
->list
);
1045 spin_unlock_irqrestore(&driver_lock
, flags
);
1047 gigaset_if_freedriver(drv
);
1052 EXPORT_SYMBOL_GPL(gigaset_freedriver
);
1055 * gigaset_initdriver() - initialize driver structure
1056 * @minor: First minor number
1057 * @minors: Number of minors this driver can handle
1058 * @procname: Name of the driver
1059 * @devname: Name of the device files (prefix without minor number)
1061 * Allocate and initialize gigaset_driver structure. Initialize interface.
1064 * Pointer to the gigaset_driver structure on success, NULL on failure.
1066 struct gigaset_driver
*gigaset_initdriver(unsigned minor
, unsigned minors
,
1067 const char *procname
,
1068 const char *devname
,
1069 const struct gigaset_ops
*ops
,
1070 struct module
*owner
)
1072 struct gigaset_driver
*drv
;
1073 unsigned long flags
;
1076 drv
= kmalloc(sizeof *drv
, GFP_KERNEL
);
1082 drv
->minors
= minors
;
1083 spin_lock_init(&drv
->lock
);
1087 INIT_LIST_HEAD(&drv
->list
);
1089 drv
->cs
= kmalloc_array(minors
, sizeof(*drv
->cs
), GFP_KERNEL
);
1093 for (i
= 0; i
< minors
; ++i
) {
1094 drv
->cs
[i
].flags
= 0;
1095 drv
->cs
[i
].driver
= drv
;
1096 drv
->cs
[i
].ops
= drv
->ops
;
1097 drv
->cs
[i
].minor_index
= i
;
1098 mutex_init(&drv
->cs
[i
].mutex
);
1101 gigaset_if_initdriver(drv
, procname
, devname
);
1103 spin_lock_irqsave(&driver_lock
, flags
);
1104 list_add(&drv
->list
, &drivers
);
1105 spin_unlock_irqrestore(&driver_lock
, flags
);
1113 EXPORT_SYMBOL_GPL(gigaset_initdriver
);
1116 * gigaset_blockdriver() - block driver
1117 * @drv: driver descriptor structure.
1119 * Prevents the driver from attaching new devices, in preparation for
1122 void gigaset_blockdriver(struct gigaset_driver
*drv
)
1126 EXPORT_SYMBOL_GPL(gigaset_blockdriver
);
1128 static int __init
gigaset_init_module(void)
1130 /* in accordance with the principle of least astonishment,
1131 * setting the 'debug' parameter to 1 activates a sensible
1132 * set of default debug levels
1134 if (gigaset_debuglevel
== 1)
1135 gigaset_debuglevel
= DEBUG_DEFAULT
;
1137 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG
"\n");
1138 gigaset_isdn_regdrv();
1142 static void __exit
gigaset_exit_module(void)
1144 gigaset_isdn_unregdrv();
1147 module_init(gigaset_init_module
);
1148 module_exit(gigaset_exit_module
);
1150 MODULE_AUTHOR(DRIVER_AUTHOR
);
1151 MODULE_DESCRIPTION(DRIVER_DESC
);
1153 MODULE_LICENSE("GPL");