2 * Stuff used by all variants of the driver
4 * Copyright (c) 2001 by Stefan Eilers,
5 * Hansjoerg Lipp <hjlipp@web.de>,
6 * Tilman Schmidt <tilman@imap.cc>.
8 * =====================================================================
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 * =====================================================================
17 #include <linux/ctype.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
21 /* Version Information */
22 #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
23 #define DRIVER_DESC "Driver for Gigaset 307x"
25 /* Module parameters */
26 int gigaset_debuglevel = DEBUG_DEFAULT;
27 EXPORT_SYMBOL_GPL(gigaset_debuglevel);
28 module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
29 MODULE_PARM_DESC(debug, "debug level");
31 /* driver state flags */
32 #define VALID_MINOR 0x01
36 /* bitwise byte inversion table */
37 __u8 gigaset_invtab[256] = {
38 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
39 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
40 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
41 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
42 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
43 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
44 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
45 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
46 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
47 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
48 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
49 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
50 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
51 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
52 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
53 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
54 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
55 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
56 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
57 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
58 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
59 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
60 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
61 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
62 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
63 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
64 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
65 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
66 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
67 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
68 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
69 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
71 EXPORT_SYMBOL_GPL(gigaset_invtab);
73 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
74 size_t len, const unsigned char *buf)
76 unsigned char outbuf[80];
78 size_t space = sizeof outbuf - 1;
79 unsigned char *out = outbuf;
84 if (c == '~' || c == '^' || c == '\\') {
95 if (c < 0x20 || c == 0x7f) {
107 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
109 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
111 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
115 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
116 cs->control_state = flags;
121 set_current_state(TASK_INTERRUPTIBLE);
122 schedule_timeout(delay * HZ / 1000);
128 int gigaset_enterconfigmode(struct cardstate *cs)
132 cs->control_state = TIOCM_RTS; //FIXME
134 r = setflags(cs, TIOCM_DTR, 200);
137 r = setflags(cs, 0, 200);
140 for (i = 0; i < 5; ++i) {
141 r = setflags(cs, TIOCM_RTS, 100);
144 r = setflags(cs, 0, 100);
148 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
155 dev_err(cs->dev, "error %d on setuartbits\n", -r);
156 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
157 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
162 static int test_timeout(struct at_state_t *at_state)
164 if (!at_state->timer_expires)
167 if (--at_state->timer_expires) {
168 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
169 at_state, at_state->timer_expires);
173 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
174 at_state->timer_index, NULL)) {
175 //FIXME what should we do?
181 static void timer_tick(unsigned long data)
183 struct cardstate *cs = (struct cardstate *) data;
186 struct at_state_t *at_state;
189 spin_lock_irqsave(&cs->lock, flags);
191 for (channel = 0; channel < cs->channels; ++channel)
192 if (test_timeout(&cs->bcs[channel].at_state))
195 if (test_timeout(&cs->at_state))
198 list_for_each_entry(at_state, &cs->temp_at_states, list)
199 if (test_timeout(at_state))
203 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
205 gig_dbg(DEBUG_CMD, "scheduling timeout");
206 tasklet_schedule(&cs->event_tasklet);
210 spin_unlock_irqrestore(&cs->lock, flags);
213 int gigaset_get_channel(struct bc_state *bcs)
217 spin_lock_irqsave(&bcs->cs->lock, flags);
218 if (bcs->use_count) {
219 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
221 spin_unlock_irqrestore(&bcs->cs->lock, flags);
226 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
227 spin_unlock_irqrestore(&bcs->cs->lock, flags);
231 void gigaset_free_channel(struct bc_state *bcs)
235 spin_lock_irqsave(&bcs->cs->lock, flags);
237 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
238 spin_unlock_irqrestore(&bcs->cs->lock, flags);
243 gig_dbg(DEBUG_ANY, "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_ANY, "could not allocate all channels");
259 for (i = 0; i < cs->channels; ++i)
260 ++cs->bcs[i].use_count;
261 spin_unlock_irqrestore(&cs->lock, flags);
263 gig_dbg(DEBUG_ANY, "allocated all channels");
268 void gigaset_free_channels(struct cardstate *cs)
273 gig_dbg(DEBUG_ANY, "unblocking all channels");
274 spin_lock_irqsave(&cs->lock, flags);
275 for (i = 0; i < cs->channels; ++i)
276 --cs->bcs[i].use_count;
277 spin_unlock_irqrestore(&cs->lock, flags);
280 void gigaset_block_channels(struct cardstate *cs)
285 gig_dbg(DEBUG_ANY, "blocking all channels");
286 spin_lock_irqsave(&cs->lock, flags);
287 for (i = 0; i < cs->channels; ++i)
288 ++cs->bcs[i].use_count;
289 spin_unlock_irqrestore(&cs->lock, flags);
292 static void clear_events(struct cardstate *cs)
298 spin_lock_irqsave(&cs->ev_lock, flags);
303 while (tail != head) {
304 ev = cs->events + head;
306 head = (head + 1) % MAX_EVENTS;
311 spin_unlock_irqrestore(&cs->ev_lock, flags);
314 struct event_t *gigaset_add_event(struct cardstate *cs,
315 struct at_state_t *at_state, int type,
316 void *ptr, int parameter, void *arg)
320 struct event_t *event = NULL;
322 spin_lock_irqsave(&cs->ev_lock, flags);
325 next = (tail + 1) % MAX_EVENTS;
326 if (unlikely(next == cs->ev_head))
327 err("event queue full");
329 event = cs->events + tail;
331 event->at_state = at_state;
335 event->parameter = parameter;
339 spin_unlock_irqrestore(&cs->ev_lock, flags);
343 EXPORT_SYMBOL_GPL(gigaset_add_event);
345 static void free_strings(struct at_state_t *at_state)
349 for (i = 0; i < STR_NUM; ++i) {
350 kfree(at_state->str_var[i]);
351 at_state->str_var[i] = NULL;
355 static void clear_at_state(struct at_state_t *at_state)
357 free_strings(at_state);
360 static void dealloc_at_states(struct cardstate *cs)
362 struct at_state_t *cur, *next;
364 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
365 list_del(&cur->list);
371 static void gigaset_freebcs(struct bc_state *bcs)
375 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
376 if (!bcs->cs->ops->freebcshw(bcs)) {
377 gig_dbg(DEBUG_INIT, "failed");
380 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
381 clear_at_state(&bcs->at_state);
382 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
385 dev_kfree_skb(bcs->skb);
386 for (i = 0; i < AT_NUM; ++i) {
387 kfree(bcs->commands[i]);
388 bcs->commands[i] = NULL;
392 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
396 static struct cardstate *ret = NULL;
398 spin_lock_irqsave(&drv->lock, flags);
399 for (i = 0; i < drv->minors; ++i) {
400 if (!(drv->flags[i] & VALID_MINOR)) {
401 drv->flags[i] = VALID_MINOR;
407 spin_unlock_irqrestore(&drv->lock, flags);
411 static void free_cs(struct cardstate *cs)
414 struct gigaset_driver *drv = cs->driver;
415 spin_lock_irqsave(&drv->lock, flags);
416 drv->flags[cs->minor_index] = 0;
417 spin_unlock_irqrestore(&drv->lock, flags);
420 static void make_valid(struct cardstate *cs, unsigned mask)
423 struct gigaset_driver *drv = cs->driver;
424 spin_lock_irqsave(&drv->lock, flags);
425 drv->flags[cs->minor_index] |= mask;
426 spin_unlock_irqrestore(&drv->lock, flags);
429 static void make_invalid(struct cardstate *cs, unsigned mask)
432 struct gigaset_driver *drv = cs->driver;
433 spin_lock_irqsave(&drv->lock, flags);
434 drv->flags[cs->minor_index] &= ~mask;
435 spin_unlock_irqrestore(&drv->lock, flags);
438 void gigaset_freecs(struct cardstate *cs)
446 mutex_lock(&cs->mutex);
453 spin_lock_irqsave(&cs->lock, flags);
455 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
456 not rescheduled below */
458 tasklet_kill(&cs->event_tasklet);
459 del_timer_sync(&cs->timer);
461 switch (cs->cs_init) {
463 /* clear device sysfs */
464 gigaset_free_dev_sysfs(cs);
468 gig_dbg(DEBUG_INIT, "clearing hw");
469 cs->ops->freecshw(cs);
474 case 2: /* error in initcshw */
475 /* Deregister from LL */
476 make_invalid(cs, VALID_ID);
477 gig_dbg(DEBUG_INIT, "clearing iif");
478 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
481 case 1: /* error when regestering to LL */
482 gig_dbg(DEBUG_INIT, "clearing at_state");
483 clear_at_state(&cs->at_state);
484 dealloc_at_states(cs);
487 case 0: /* error in one call to initbcs */
488 for (i = 0; i < cs->channels; ++i) {
489 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
490 gigaset_freebcs(cs->bcs + i);
494 gig_dbg(DEBUG_INIT, "freeing inbuf");
497 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
499 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
500 mutex_unlock(&cs->mutex);
503 EXPORT_SYMBOL_GPL(gigaset_freecs);
505 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
506 struct cardstate *cs, int cid)
510 INIT_LIST_HEAD(&at_state->list);
511 at_state->waiting = 0;
512 at_state->getstring = 0;
513 at_state->pending_commands = 0;
514 at_state->timer_expires = 0;
515 at_state->timer_active = 0;
516 at_state->timer_index = 0;
517 at_state->seq_index = 0;
518 at_state->ConState = 0;
519 for (i = 0; i < STR_NUM; ++i)
520 at_state->str_var[i] = NULL;
521 at_state->int_var[VAR_ZDLE] = 0;
522 at_state->int_var[VAR_ZCTP] = -1;
523 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
528 at_state->replystruct = cs->tabnocid;
530 at_state->replystruct = cs->tabcid;
534 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
535 struct cardstate *cs, int inputstate)
536 /* inbuf->read must be allocated before! */
538 atomic_set(&inbuf->head, 0);
539 atomic_set(&inbuf->tail, 0);
541 inbuf->bcs = bcs; /*base driver: NULL*/
542 inbuf->rcvbuf = NULL; //FIXME
543 inbuf->inputstate = inputstate;
546 /* append received bytes to inbuf */
547 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
550 unsigned n, head, tail, bytesleft;
552 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
557 bytesleft = numbytes;
558 tail = atomic_read(&inbuf->tail);
559 head = atomic_read(&inbuf->head);
560 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
566 n = (RBUFSIZE-1) - tail;
570 dev_err(inbuf->cs->dev,
571 "buffer overflow (%u bytes lost)", bytesleft);
576 memcpy(inbuf->data + tail, src, n);
578 tail = (tail + n) % RBUFSIZE;
581 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
582 atomic_set(&inbuf->tail, tail);
583 return numbytes != bytesleft;
585 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
587 /* Initialize the b-channel structure */
588 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
589 struct cardstate *cs, int channel)
593 bcs->tx_skb = NULL; //FIXME -> hw part
595 skb_queue_head_init(&bcs->squeue);
601 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
602 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
606 #ifdef CONFIG_GIGASET_DEBUG
610 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
611 bcs->fcs = PPP_INITFCS;
613 if (cs->ignoreframes) {
614 bcs->inputstate |= INS_skip_frame;
616 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
617 skb_reserve(bcs->skb, HW_HDR_LEN);
619 dev_warn(cs->dev, "could not allocate skb\n");
620 bcs->inputstate |= INS_skip_frame;
623 bcs->channel = channel;
629 bcs->ignore = cs->ignoreframes;
631 for (i = 0; i < AT_NUM; ++i)
632 bcs->commands[i] = NULL;
634 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
635 if (cs->ops->initbcshw(bcs))
638 gig_dbg(DEBUG_INIT, " failed");
640 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
642 dev_kfree_skb(bcs->skb);
648 * Allocate and initialize cardstate structure for Gigaset driver
649 * Calls hardware dependent gigaset_initcshw() function
650 * Calls B channel initialization function gigaset_initbcs() for each B channel
652 * drv hardware driver the device belongs to
653 * channels number of B channels supported by device
654 * onechannel !=0: B channel data and AT commands share one
655 * communication channel
656 * ==0: B channels have separate communication channels
657 * ignoreframes number of frames to ignore after setting up B channel
658 * cidmode !=0: start in CallID mode
659 * modulename name of driver module (used for I4L registration)
661 * pointer to cardstate structure
663 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
664 int onechannel, int ignoreframes,
665 int cidmode, const char *modulename)
667 struct cardstate *cs = NULL;
671 gig_dbg(DEBUG_INIT, "allocating cs");
675 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
676 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
679 gig_dbg(DEBUG_INIT, "allocating inbuf");
680 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
685 cs->channels = channels;
686 cs->onechannel = onechannel;
687 cs->ignoreframes = ignoreframes;
688 INIT_LIST_HEAD(&cs->temp_at_states);
690 init_timer(&cs->timer); /* clear next & prev */
691 spin_lock_init(&cs->ev_lock);
694 mutex_init(&cs->mutex);
695 mutex_lock(&cs->mutex);
697 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
699 atomic_set(&cs->commands_pending, 0);
706 cs->cidmode = cidmode != 0;
708 //if(onechannel) { //FIXME
709 cs->tabnocid = gigaset_tab_nocid_m10x;
710 cs->tabcid = gigaset_tab_cid_m10x;
712 // cs->tabnocid = gigaset_tab_nocid;
713 // cs->tabcid = gigaset_tab_cid;
716 init_waitqueue_head(&cs->waitqueue);
719 atomic_set(&cs->mode, M_UNKNOWN);
720 atomic_set(&cs->mstate, MS_UNINITIALIZED);
722 for (i = 0; i < channels; ++i) {
723 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
724 if (!gigaset_initbcs(cs->bcs + i, cs, i))
730 gig_dbg(DEBUG_INIT, "setting up at_state");
731 spin_lock_init(&cs->lock);
732 gigaset_at_init(&cs->at_state, NULL, cs, 0);
736 gig_dbg(DEBUG_INIT, "setting up inbuf");
737 if (onechannel) { //FIXME distinction necessary?
738 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
740 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
745 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
746 cs->cmdbuf = cs->lastcmdbuf = NULL;
747 spin_lock_init(&cs->cmdlock);
751 gig_dbg(DEBUG_INIT, "setting up iif");
752 if (!gigaset_register_to_LL(cs, modulename)) {
753 err("register_isdn failed");
757 make_valid(cs, VALID_ID);
759 gig_dbg(DEBUG_INIT, "setting up hw");
760 if (!cs->ops->initcshw(cs))
767 /* set up device sysfs */
768 gigaset_init_dev_sysfs(cs);
770 spin_lock_irqsave(&cs->lock, flags);
772 spin_unlock_irqrestore(&cs->lock, flags);
773 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
774 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
775 /* FIXME: can jiffies increase too much until the timer is added?
776 * Same problem(?) with mod_timer() in timer_tick(). */
777 add_timer(&cs->timer);
779 gig_dbg(DEBUG_INIT, "cs initialized");
780 mutex_unlock(&cs->mutex);
784 mutex_unlock(&cs->mutex);
785 gig_dbg(DEBUG_INIT, "failed");
789 EXPORT_SYMBOL_GPL(gigaset_initcs);
791 /* ReInitialize the b-channel structure on hangup */
792 void gigaset_bcs_reinit(struct bc_state *bcs)
795 struct cardstate *cs = bcs->cs;
798 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
801 spin_lock_irqsave(&cs->lock, flags);
802 clear_at_state(&bcs->at_state);
803 bcs->at_state.ConState = 0;
804 bcs->at_state.timer_active = 0;
805 bcs->at_state.timer_expires = 0;
806 bcs->at_state.cid = -1; /* No CID defined */
807 spin_unlock_irqrestore(&cs->lock, flags);
811 #ifdef CONFIG_GIGASET_DEBUG
815 bcs->fcs = PPP_INITFCS;
818 bcs->ignore = cs->ignoreframes;
820 bcs->inputstate |= INS_skip_frame;
823 cs->ops->reinitbcshw(bcs);
826 static void cleanup_cs(struct cardstate *cs)
828 struct cmdbuf_t *cb, *tcb;
832 spin_lock_irqsave(&cs->lock, flags);
834 atomic_set(&cs->mode, M_UNKNOWN);
835 atomic_set(&cs->mstate, MS_UNINITIALIZED);
837 clear_at_state(&cs->at_state);
838 dealloc_at_states(cs);
839 free_strings(&cs->at_state);
840 gigaset_at_init(&cs->at_state, NULL, cs, 0);
842 kfree(cs->inbuf->rcvbuf);
843 cs->inbuf->rcvbuf = NULL;
844 cs->inbuf->inputstate = INS_command;
845 atomic_set(&cs->inbuf->head, 0);
846 atomic_set(&cs->inbuf->tail, 0);
854 cs->cmdbuf = cs->lastcmdbuf = NULL;
860 atomic_set(&cs->commands_pending, 0);
863 spin_unlock_irqrestore(&cs->lock, flags);
865 for (i = 0; i < cs->channels; ++i) {
866 gigaset_freebcs(cs->bcs + i);
867 if (!gigaset_initbcs(cs->bcs + i, cs, i))
868 break; //FIXME error handling
872 cs->cmd_result = -ENODEV;
874 wake_up_interruptible(&cs->waitqueue);
879 int gigaset_start(struct cardstate *cs)
883 if (mutex_lock_interruptible(&cs->mutex))
886 spin_lock_irqsave(&cs->lock, flags);
888 spin_unlock_irqrestore(&cs->lock, flags);
890 if (atomic_read(&cs->mstate) != MS_LOCKED) {
891 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
892 cs->ops->baud_rate(cs, B115200);
893 cs->ops->set_line_ctrl(cs, CS8);
894 cs->control_state = TIOCM_DTR|TIOCM_RTS;
896 //FIXME use some saved values?
901 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
903 //FIXME what should we do?
907 gig_dbg(DEBUG_CMD, "scheduling START");
908 gigaset_schedule_event(cs);
910 wait_event(cs->waitqueue, !cs->waiting);
912 mutex_unlock(&cs->mutex);
916 mutex_unlock(&cs->mutex);
919 EXPORT_SYMBOL_GPL(gigaset_start);
921 void gigaset_shutdown(struct cardstate *cs)
923 mutex_lock(&cs->mutex);
927 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
928 //FIXME what should we do?
932 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
933 gigaset_schedule_event(cs);
935 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
936 warn("%s: aborted", __func__);
940 if (atomic_read(&cs->mstate) != MS_LOCKED) {
942 //gigaset_baud_rate(cs, B115200);
943 //gigaset_set_line_ctrl(cs, CS8);
944 //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
945 //cs->control_state = 0;
947 //FIXME use some saved values?
953 mutex_unlock(&cs->mutex);
955 EXPORT_SYMBOL_GPL(gigaset_shutdown);
957 void gigaset_stop(struct cardstate *cs)
959 mutex_lock(&cs->mutex);
963 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
964 //FIXME what should we do?
968 gig_dbg(DEBUG_CMD, "scheduling STOP");
969 gigaset_schedule_event(cs);
971 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
972 warn("%s: aborted", __func__);
979 mutex_unlock(&cs->mutex);
981 EXPORT_SYMBOL_GPL(gigaset_stop);
983 static LIST_HEAD(drivers);
984 static DEFINE_SPINLOCK(driver_lock);
986 struct cardstate *gigaset_get_cs_by_id(int id)
989 static struct cardstate *ret = NULL;
990 static struct cardstate *cs;
991 struct gigaset_driver *drv;
994 spin_lock_irqsave(&driver_lock, flags);
995 list_for_each_entry(drv, &drivers, list) {
996 spin_lock(&drv->lock);
997 for (i = 0; i < drv->minors; ++i) {
998 if (drv->flags[i] & VALID_ID) {
1006 spin_unlock(&drv->lock);
1010 spin_unlock_irqrestore(&driver_lock, flags);
1014 void gigaset_debugdrivers(void)
1016 unsigned long flags;
1017 static struct cardstate *cs;
1018 struct gigaset_driver *drv;
1021 spin_lock_irqsave(&driver_lock, flags);
1022 list_for_each_entry(drv, &drivers, list) {
1023 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
1024 spin_lock(&drv->lock);
1025 for (i = 0; i < drv->minors; ++i) {
1026 gig_dbg(DEBUG_DRIVER, " index %u", i);
1027 gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
1030 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
1031 gig_dbg(DEBUG_DRIVER, " minor_index %u",
1033 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
1034 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
1036 spin_unlock(&drv->lock);
1038 spin_unlock_irqrestore(&driver_lock, flags);
1041 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1043 unsigned long flags;
1044 static struct cardstate *ret = NULL;
1045 struct gigaset_driver *drv;
1048 spin_lock_irqsave(&driver_lock, flags);
1049 list_for_each_entry(drv, &drivers, list) {
1050 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1052 index = minor - drv->minor;
1053 spin_lock(&drv->lock);
1054 if (drv->flags[index] & VALID_MINOR)
1055 ret = drv->cs + index;
1056 spin_unlock(&drv->lock);
1060 spin_unlock_irqrestore(&driver_lock, flags);
1064 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1066 if (tty->index < 0 || tty->index >= tty->driver->num)
1068 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1071 void gigaset_freedriver(struct gigaset_driver *drv)
1073 unsigned long flags;
1075 spin_lock_irqsave(&driver_lock, flags);
1076 list_del(&drv->list);
1077 spin_unlock_irqrestore(&driver_lock, flags);
1079 gigaset_if_freedriver(drv);
1080 module_put(drv->owner);
1086 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1088 /* gigaset_initdriver
1089 * Allocate and initialize gigaset_driver structure. Initialize interface.
1091 * minor First minor number
1092 * minors Number of minors this driver can handle
1093 * procname Name of the driver
1094 * devname Name of the device files (prefix without minor number)
1096 * Pointer to the gigaset_driver structure on success, NULL on failure.
1098 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1099 const char *procname,
1100 const char *devname,
1101 const struct gigaset_ops *ops,
1102 struct module *owner)
1104 struct gigaset_driver *drv;
1105 unsigned long flags;
1108 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1112 if (!try_module_get(owner))
1118 drv->minors = minors;
1119 spin_lock_init(&drv->lock);
1123 INIT_LIST_HEAD(&drv->list);
1125 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1129 drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1133 for (i = 0; i < minors; ++i) {
1135 drv->cs[i].driver = drv;
1136 drv->cs[i].ops = drv->ops;
1137 drv->cs[i].minor_index = i;
1140 gigaset_if_initdriver(drv, procname, devname);
1142 spin_lock_irqsave(&driver_lock, flags);
1143 list_add(&drv->list, &drivers);
1144 spin_unlock_irqrestore(&driver_lock, flags);
1156 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1158 /* For drivers without fixed assignment device<->cardstate (usb) */
1159 struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1161 unsigned long flags;
1162 struct cardstate *cs = NULL;
1165 spin_lock_irqsave(&drv->lock, flags);
1168 for (i = 0; i < drv->minors; ++i) {
1169 if ((drv->flags[i] & VALID_MINOR) &&
1170 !(drv->flags[i] & ASSIGNED)) {
1171 drv->flags[i] |= ASSIGNED;
1177 spin_unlock_irqrestore(&drv->lock, flags);
1180 EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1182 void gigaset_unassign(struct cardstate *cs)
1184 unsigned long flags;
1185 unsigned *minor_flags;
1186 struct gigaset_driver *drv;
1191 spin_lock_irqsave(&drv->lock, flags);
1192 minor_flags = drv->flags + cs->minor_index;
1193 if (*minor_flags & VALID_MINOR)
1194 *minor_flags &= ~ASSIGNED;
1195 spin_unlock_irqrestore(&drv->lock, flags);
1197 EXPORT_SYMBOL_GPL(gigaset_unassign);
1199 void gigaset_blockdriver(struct gigaset_driver *drv)
1201 unsigned long flags;
1202 spin_lock_irqsave(&drv->lock, flags);
1204 spin_unlock_irqrestore(&drv->lock, flags);
1206 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1208 static int __init gigaset_init_module(void)
1210 /* in accordance with the principle of least astonishment,
1211 * setting the 'debug' parameter to 1 activates a sensible
1212 * set of default debug levels
1214 if (gigaset_debuglevel == 1)
1215 gigaset_debuglevel = DEBUG_DEFAULT;
1217 info(DRIVER_AUTHOR);
1222 static void __exit gigaset_exit_module(void)
1226 module_init(gigaset_init_module);
1227 module_exit(gigaset_exit_module);
1229 MODULE_AUTHOR(DRIVER_AUTHOR);
1230 MODULE_DESCRIPTION(DRIVER_DESC);
1232 MODULE_LICENSE("GPL");