x86/amd-iommu: Add function to complete a tlb flush
[linux/fpc-iii.git] / drivers / isdn / gigaset / common.c
blob33dcd8d72b7cb72ced39b702a0e486400c4fcd52
1 /*
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 * =====================================================================
16 #include "gigaset.h"
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)"
27 #else
28 #define DRIVER_DESC_DEBUG ""
29 #endif
31 /* Module parameters */
32 int gigaset_debuglevel = DEBUG_DEFAULT;
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
39 #define VALID_ID 0x02
41 /**
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.
46 * @buf: data 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];
56 unsigned char c;
57 size_t space = sizeof outbuf - 1;
58 unsigned char *out = outbuf;
59 size_t numin = len;
61 while (numin--) {
62 c = *buf++;
63 if (c == '~' || c == '^' || c == '\\') {
64 if (!space--)
65 break;
66 *out++ = '\\';
68 if (c & 0x80) {
69 if (!space--)
70 break;
71 *out++ = '~';
72 c ^= 0x80;
74 if (c < 0x20 || c == 0x7f) {
75 if (!space--)
76 break;
77 *out++ = '^';
78 c ^= 0x40;
80 if (!space--)
81 break;
82 *out++ = c;
84 *out = 0;
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)
92 int r;
94 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
95 cs->control_state = flags;
96 if (r < 0)
97 return r;
99 if (delay) {
100 set_current_state(TASK_INTERRUPTIBLE);
101 schedule_timeout(delay * HZ / 1000);
104 return 0;
107 int gigaset_enterconfigmode(struct cardstate *cs)
109 int i, r;
111 cs->control_state = TIOCM_RTS; //FIXME
113 r = setflags(cs, TIOCM_DTR, 200);
114 if (r < 0)
115 goto error;
116 r = setflags(cs, 0, 200);
117 if (r < 0)
118 goto error;
119 for (i = 0; i < 5; ++i) {
120 r = setflags(cs, TIOCM_RTS, 100);
121 if (r < 0)
122 goto error;
123 r = setflags(cs, 0, 100);
124 if (r < 0)
125 goto error;
127 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
128 if (r < 0)
129 goto error;
131 return 0;
133 error:
134 dev_err(cs->dev, "error %d on setuartbits\n", -r);
135 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
136 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
138 return -1; //r
141 static int test_timeout(struct at_state_t *at_state)
143 if (!at_state->timer_expires)
144 return 0;
146 if (--at_state->timer_expires) {
147 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
148 at_state, at_state->timer_expires);
149 return 0;
152 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
153 at_state->timer_index, NULL)) {
154 //FIXME what should we do?
157 return 1;
160 static void timer_tick(unsigned long data)
162 struct cardstate *cs = (struct cardstate *) data;
163 unsigned long flags;
164 unsigned channel;
165 struct at_state_t *at_state;
166 int timeout = 0;
168 spin_lock_irqsave(&cs->lock, flags);
170 for (channel = 0; channel < cs->channels; ++channel)
171 if (test_timeout(&cs->bcs[channel].at_state))
172 timeout = 1;
174 if (test_timeout(&cs->at_state))
175 timeout = 1;
177 list_for_each_entry(at_state, &cs->temp_at_states, list)
178 if (test_timeout(at_state))
179 timeout = 1;
181 if (cs->running) {
182 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
183 if (timeout) {
184 gig_dbg(DEBUG_CMD, "scheduling timeout");
185 tasklet_schedule(&cs->event_tasklet);
189 spin_unlock_irqrestore(&cs->lock, flags);
192 int gigaset_get_channel(struct bc_state *bcs)
194 unsigned long flags;
196 spin_lock_irqsave(&bcs->cs->lock, flags);
197 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
198 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
199 bcs->channel);
200 spin_unlock_irqrestore(&bcs->cs->lock, flags);
201 return 0;
203 ++bcs->use_count;
204 bcs->busy = 1;
205 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
206 spin_unlock_irqrestore(&bcs->cs->lock, flags);
207 return 1;
210 void gigaset_free_channel(struct bc_state *bcs)
212 unsigned long flags;
214 spin_lock_irqsave(&bcs->cs->lock, flags);
215 if (!bcs->busy) {
216 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
217 spin_unlock_irqrestore(&bcs->cs->lock, flags);
218 return;
220 --bcs->use_count;
221 bcs->busy = 0;
222 module_put(bcs->cs->driver->owner);
223 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
224 spin_unlock_irqrestore(&bcs->cs->lock, flags);
227 int gigaset_get_channels(struct cardstate *cs)
229 unsigned long flags;
230 int i;
232 spin_lock_irqsave(&cs->lock, flags);
233 for (i = 0; i < cs->channels; ++i)
234 if (cs->bcs[i].use_count) {
235 spin_unlock_irqrestore(&cs->lock, flags);
236 gig_dbg(DEBUG_ANY, "could not allocate all channels");
237 return 0;
239 for (i = 0; i < cs->channels; ++i)
240 ++cs->bcs[i].use_count;
241 spin_unlock_irqrestore(&cs->lock, flags);
243 gig_dbg(DEBUG_ANY, "allocated all channels");
245 return 1;
248 void gigaset_free_channels(struct cardstate *cs)
250 unsigned long flags;
251 int i;
253 gig_dbg(DEBUG_ANY, "unblocking all channels");
254 spin_lock_irqsave(&cs->lock, flags);
255 for (i = 0; i < cs->channels; ++i)
256 --cs->bcs[i].use_count;
257 spin_unlock_irqrestore(&cs->lock, flags);
260 void gigaset_block_channels(struct cardstate *cs)
262 unsigned long flags;
263 int i;
265 gig_dbg(DEBUG_ANY, "blocking all channels");
266 spin_lock_irqsave(&cs->lock, flags);
267 for (i = 0; i < cs->channels; ++i)
268 ++cs->bcs[i].use_count;
269 spin_unlock_irqrestore(&cs->lock, flags);
272 static void clear_events(struct cardstate *cs)
274 struct event_t *ev;
275 unsigned head, tail;
276 unsigned long flags;
278 spin_lock_irqsave(&cs->ev_lock, flags);
280 head = cs->ev_head;
281 tail = cs->ev_tail;
283 while (tail != head) {
284 ev = cs->events + head;
285 kfree(ev->ptr);
286 head = (head + 1) % MAX_EVENTS;
289 cs->ev_head = tail;
291 spin_unlock_irqrestore(&cs->ev_lock, flags);
295 * gigaset_add_event() - add event to device event queue
296 * @cs: device descriptor structure.
297 * @at_state: connection state structure.
298 * @type: event type.
299 * @ptr: pointer parameter for event.
300 * @parameter: integer parameter for event.
301 * @arg: pointer parameter for event.
303 * Allocate an event queue entry from the device's event queue, and set it up
304 * with the parameters given.
306 * Return value: added event
308 struct event_t *gigaset_add_event(struct cardstate *cs,
309 struct at_state_t *at_state, int type,
310 void *ptr, int parameter, void *arg)
312 unsigned long flags;
313 unsigned next, tail;
314 struct event_t *event = NULL;
316 spin_lock_irqsave(&cs->ev_lock, flags);
318 tail = cs->ev_tail;
319 next = (tail + 1) % MAX_EVENTS;
320 if (unlikely(next == cs->ev_head))
321 dev_err(cs->dev, "event queue full\n");
322 else {
323 event = cs->events + tail;
324 event->type = type;
325 event->at_state = at_state;
326 event->cid = -1;
327 event->ptr = ptr;
328 event->arg = arg;
329 event->parameter = parameter;
330 cs->ev_tail = next;
333 spin_unlock_irqrestore(&cs->ev_lock, flags);
335 return event;
337 EXPORT_SYMBOL_GPL(gigaset_add_event);
339 static void free_strings(struct at_state_t *at_state)
341 int i;
343 for (i = 0; i < STR_NUM; ++i) {
344 kfree(at_state->str_var[i]);
345 at_state->str_var[i] = NULL;
349 static void clear_at_state(struct at_state_t *at_state)
351 free_strings(at_state);
354 static void dealloc_at_states(struct cardstate *cs)
356 struct at_state_t *cur, *next;
358 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
359 list_del(&cur->list);
360 free_strings(cur);
361 kfree(cur);
365 static void gigaset_freebcs(struct bc_state *bcs)
367 int i;
369 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
370 if (!bcs->cs->ops->freebcshw(bcs)) {
371 gig_dbg(DEBUG_INIT, "failed");
374 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
375 clear_at_state(&bcs->at_state);
376 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
378 if (bcs->skb)
379 dev_kfree_skb(bcs->skb);
380 for (i = 0; i < AT_NUM; ++i) {
381 kfree(bcs->commands[i]);
382 bcs->commands[i] = NULL;
386 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
388 unsigned long flags;
389 unsigned i;
390 struct cardstate *cs;
391 struct cardstate *ret = NULL;
393 spin_lock_irqsave(&drv->lock, flags);
394 if (drv->blocked)
395 goto exit;
396 for (i = 0; i < drv->minors; ++i) {
397 cs = drv->cs + i;
398 if (!(cs->flags & VALID_MINOR)) {
399 cs->flags = VALID_MINOR;
400 ret = cs;
401 break;
404 exit:
405 spin_unlock_irqrestore(&drv->lock, flags);
406 return ret;
409 static void free_cs(struct cardstate *cs)
411 cs->flags = 0;
414 static void make_valid(struct cardstate *cs, unsigned mask)
416 unsigned long flags;
417 struct gigaset_driver *drv = cs->driver;
418 spin_lock_irqsave(&drv->lock, flags);
419 cs->flags |= mask;
420 spin_unlock_irqrestore(&drv->lock, flags);
423 static void make_invalid(struct cardstate *cs, unsigned mask)
425 unsigned long flags;
426 struct gigaset_driver *drv = cs->driver;
427 spin_lock_irqsave(&drv->lock, flags);
428 cs->flags &= ~mask;
429 spin_unlock_irqrestore(&drv->lock, flags);
433 * gigaset_freecs() - free all associated ressources of a device
434 * @cs: device descriptor structure.
436 * Stops all tasklets and timers, unregisters the device from all
437 * subsystems it was registered to, deallocates the device structure
438 * @cs and all structures referenced from it.
439 * Operations on the device should be stopped before calling this.
441 void gigaset_freecs(struct cardstate *cs)
443 int i;
444 unsigned long flags;
446 if (!cs)
447 return;
449 mutex_lock(&cs->mutex);
451 if (!cs->bcs)
452 goto f_cs;
453 if (!cs->inbuf)
454 goto f_bcs;
456 spin_lock_irqsave(&cs->lock, flags);
457 cs->running = 0;
458 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
459 not rescheduled below */
461 tasklet_kill(&cs->event_tasklet);
462 del_timer_sync(&cs->timer);
464 switch (cs->cs_init) {
465 default:
466 /* clear device sysfs */
467 gigaset_free_dev_sysfs(cs);
469 gigaset_if_free(cs);
471 gig_dbg(DEBUG_INIT, "clearing hw");
472 cs->ops->freecshw(cs);
474 //FIXME cmdbuf
476 /* fall through */
477 case 2: /* error in initcshw */
478 /* Deregister from LL */
479 make_invalid(cs, VALID_ID);
480 gig_dbg(DEBUG_INIT, "clearing iif");
481 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
483 /* fall through */
484 case 1: /* error when regestering to LL */
485 gig_dbg(DEBUG_INIT, "clearing at_state");
486 clear_at_state(&cs->at_state);
487 dealloc_at_states(cs);
489 /* fall through */
490 case 0: /* error in one call to initbcs */
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_events(cs);
497 gig_dbg(DEBUG_INIT, "freeing inbuf");
498 kfree(cs->inbuf);
500 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
501 kfree(cs->bcs);
502 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
503 mutex_unlock(&cs->mutex);
504 free_cs(cs);
506 EXPORT_SYMBOL_GPL(gigaset_freecs);
508 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
509 struct cardstate *cs, int cid)
511 int i;
513 INIT_LIST_HEAD(&at_state->list);
514 at_state->waiting = 0;
515 at_state->getstring = 0;
516 at_state->pending_commands = 0;
517 at_state->timer_expires = 0;
518 at_state->timer_active = 0;
519 at_state->timer_index = 0;
520 at_state->seq_index = 0;
521 at_state->ConState = 0;
522 for (i = 0; i < STR_NUM; ++i)
523 at_state->str_var[i] = NULL;
524 at_state->int_var[VAR_ZDLE] = 0;
525 at_state->int_var[VAR_ZCTP] = -1;
526 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
527 at_state->cs = cs;
528 at_state->bcs = bcs;
529 at_state->cid = cid;
530 if (!cid)
531 at_state->replystruct = cs->tabnocid;
532 else
533 at_state->replystruct = cs->tabcid;
537 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
538 struct cardstate *cs, int inputstate)
539 /* inbuf->read must be allocated before! */
541 inbuf->head = 0;
542 inbuf->tail = 0;
543 inbuf->cs = cs;
544 inbuf->bcs = bcs; /*base driver: NULL*/
545 inbuf->rcvbuf = NULL;
546 inbuf->inputstate = inputstate;
550 * gigaset_fill_inbuf() - append received data to input buffer
551 * @inbuf: buffer structure.
552 * @src: received data.
553 * @numbytes: number of bytes received.
555 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
556 unsigned numbytes)
558 unsigned n, head, tail, bytesleft;
560 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
562 if (!numbytes)
563 return 0;
565 bytesleft = numbytes;
566 tail = inbuf->tail;
567 head = inbuf->head;
568 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
570 while (bytesleft) {
571 if (head > tail)
572 n = head - 1 - tail;
573 else if (head == 0)
574 n = (RBUFSIZE-1) - tail;
575 else
576 n = RBUFSIZE - tail;
577 if (!n) {
578 dev_err(inbuf->cs->dev,
579 "buffer overflow (%u bytes lost)\n",
580 bytesleft);
581 break;
583 if (n > bytesleft)
584 n = bytesleft;
585 memcpy(inbuf->data + tail, src, n);
586 bytesleft -= n;
587 tail = (tail + n) % RBUFSIZE;
588 src += n;
590 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
591 inbuf->tail = tail;
592 return numbytes != bytesleft;
594 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
596 /* Initialize the b-channel structure */
597 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
598 struct cardstate *cs, int channel)
600 int i;
602 bcs->tx_skb = NULL; //FIXME -> hw part
604 skb_queue_head_init(&bcs->squeue);
606 bcs->corrupted = 0;
607 bcs->trans_down = 0;
608 bcs->trans_up = 0;
610 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
611 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
613 #ifdef CONFIG_GIGASET_DEBUG
614 bcs->emptycount = 0;
615 #endif
617 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
618 bcs->fcs = PPP_INITFCS;
619 bcs->inputstate = 0;
620 if (cs->ignoreframes) {
621 bcs->inputstate |= INS_skip_frame;
622 bcs->skb = NULL;
623 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
624 skb_reserve(bcs->skb, HW_HDR_LEN);
625 else {
626 pr_err("out of memory\n");
627 bcs->inputstate |= INS_skip_frame;
630 bcs->channel = channel;
631 bcs->cs = cs;
633 bcs->chstate = 0;
634 bcs->use_count = 1;
635 bcs->busy = 0;
636 bcs->ignore = cs->ignoreframes;
638 for (i = 0; i < AT_NUM; ++i)
639 bcs->commands[i] = NULL;
641 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
642 if (cs->ops->initbcshw(bcs))
643 return bcs;
645 gig_dbg(DEBUG_INIT, " failed");
647 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
648 if (bcs->skb)
649 dev_kfree_skb(bcs->skb);
651 return NULL;
655 * gigaset_initcs() - initialize device structure
656 * @drv: hardware driver the device belongs to
657 * @channels: number of B channels supported by device
658 * @onechannel: !=0 if B channel data and AT commands share one
659 * communication channel (M10x),
660 * ==0 if B channels have separate communication channels (base)
661 * @ignoreframes: number of frames to ignore after setting up B channel
662 * @cidmode: !=0: start in CallID mode
663 * @modulename: name of driver module for LL registration
665 * Allocate and initialize cardstate structure for Gigaset driver
666 * Calls hardware dependent gigaset_initcshw() function
667 * Calls B channel initialization function gigaset_initbcs() for each B channel
669 * Return value:
670 * pointer to cardstate structure
672 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
673 int onechannel, int ignoreframes,
674 int cidmode, const char *modulename)
676 struct cardstate *cs = NULL;
677 unsigned long flags;
678 int i;
680 gig_dbg(DEBUG_INIT, "allocating cs");
681 if (!(cs = alloc_cs(drv))) {
682 pr_err("maximum number of devices exceeded\n");
683 return NULL;
686 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
687 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
688 if (!cs->bcs) {
689 pr_err("out of memory\n");
690 goto error;
692 gig_dbg(DEBUG_INIT, "allocating inbuf");
693 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
694 if (!cs->inbuf) {
695 pr_err("out of memory\n");
696 goto error;
699 cs->cs_init = 0;
700 cs->channels = channels;
701 cs->onechannel = onechannel;
702 cs->ignoreframes = ignoreframes;
703 INIT_LIST_HEAD(&cs->temp_at_states);
704 cs->running = 0;
705 init_timer(&cs->timer); /* clear next & prev */
706 spin_lock_init(&cs->ev_lock);
707 cs->ev_tail = 0;
708 cs->ev_head = 0;
710 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
711 (unsigned long) cs);
712 cs->commands_pending = 0;
713 cs->cur_at_seq = 0;
714 cs->gotfwver = -1;
715 cs->open_count = 0;
716 cs->dev = NULL;
717 cs->tty = NULL;
718 cs->tty_dev = NULL;
719 cs->cidmode = cidmode != 0;
720 cs->tabnocid = gigaset_tab_nocid;
721 cs->tabcid = gigaset_tab_cid;
723 init_waitqueue_head(&cs->waitqueue);
724 cs->waiting = 0;
726 cs->mode = M_UNKNOWN;
727 cs->mstate = MS_UNINITIALIZED;
729 for (i = 0; i < channels; ++i) {
730 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
731 if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
732 pr_err("could not allocate channel %d data\n", i);
733 goto error;
737 ++cs->cs_init;
739 gig_dbg(DEBUG_INIT, "setting up at_state");
740 spin_lock_init(&cs->lock);
741 gigaset_at_init(&cs->at_state, NULL, cs, 0);
742 cs->dle = 0;
743 cs->cbytes = 0;
745 gig_dbg(DEBUG_INIT, "setting up inbuf");
746 if (onechannel) { //FIXME distinction necessary?
747 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
748 } else
749 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
751 cs->connected = 0;
752 cs->isdn_up = 0;
754 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
755 cs->cmdbuf = cs->lastcmdbuf = NULL;
756 spin_lock_init(&cs->cmdlock);
757 cs->curlen = 0;
758 cs->cmdbytes = 0;
760 gig_dbg(DEBUG_INIT, "setting up iif");
761 if (!gigaset_register_to_LL(cs, modulename)) {
762 pr_err("error registering ISDN device\n");
763 goto error;
766 make_valid(cs, VALID_ID);
767 ++cs->cs_init;
768 gig_dbg(DEBUG_INIT, "setting up hw");
769 if (!cs->ops->initcshw(cs))
770 goto error;
772 ++cs->cs_init;
774 /* set up character device */
775 gigaset_if_init(cs);
777 /* set up device sysfs */
778 gigaset_init_dev_sysfs(cs);
780 spin_lock_irqsave(&cs->lock, flags);
781 cs->running = 1;
782 spin_unlock_irqrestore(&cs->lock, flags);
783 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
784 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
785 /* FIXME: can jiffies increase too much until the timer is added?
786 * Same problem(?) with mod_timer() in timer_tick(). */
787 add_timer(&cs->timer);
789 gig_dbg(DEBUG_INIT, "cs initialized");
790 return cs;
792 error:
793 gig_dbg(DEBUG_INIT, "failed");
794 gigaset_freecs(cs);
795 return NULL;
797 EXPORT_SYMBOL_GPL(gigaset_initcs);
799 /* ReInitialize the b-channel structure on hangup */
800 void gigaset_bcs_reinit(struct bc_state *bcs)
802 struct sk_buff *skb;
803 struct cardstate *cs = bcs->cs;
804 unsigned long flags;
806 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
807 dev_kfree_skb(skb);
809 spin_lock_irqsave(&cs->lock, flags);
810 clear_at_state(&bcs->at_state);
811 bcs->at_state.ConState = 0;
812 bcs->at_state.timer_active = 0;
813 bcs->at_state.timer_expires = 0;
814 bcs->at_state.cid = -1; /* No CID defined */
815 spin_unlock_irqrestore(&cs->lock, flags);
817 bcs->inputstate = 0;
819 #ifdef CONFIG_GIGASET_DEBUG
820 bcs->emptycount = 0;
821 #endif
823 bcs->fcs = PPP_INITFCS;
824 bcs->chstate = 0;
826 bcs->ignore = cs->ignoreframes;
827 if (bcs->ignore)
828 bcs->inputstate |= INS_skip_frame;
831 cs->ops->reinitbcshw(bcs);
834 static void cleanup_cs(struct cardstate *cs)
836 struct cmdbuf_t *cb, *tcb;
837 int i;
838 unsigned long flags;
840 spin_lock_irqsave(&cs->lock, flags);
842 cs->mode = M_UNKNOWN;
843 cs->mstate = MS_UNINITIALIZED;
845 clear_at_state(&cs->at_state);
846 dealloc_at_states(cs);
847 free_strings(&cs->at_state);
848 gigaset_at_init(&cs->at_state, NULL, cs, 0);
850 kfree(cs->inbuf->rcvbuf);
851 cs->inbuf->rcvbuf = NULL;
852 cs->inbuf->inputstate = INS_command;
853 cs->inbuf->head = 0;
854 cs->inbuf->tail = 0;
856 cb = cs->cmdbuf;
857 while (cb) {
858 tcb = cb;
859 cb = cb->next;
860 kfree(tcb);
862 cs->cmdbuf = cs->lastcmdbuf = NULL;
863 cs->curlen = 0;
864 cs->cmdbytes = 0;
865 cs->gotfwver = -1;
866 cs->dle = 0;
867 cs->cur_at_seq = 0;
868 cs->commands_pending = 0;
869 cs->cbytes = 0;
871 spin_unlock_irqrestore(&cs->lock, flags);
873 for (i = 0; i < cs->channels; ++i) {
874 gigaset_freebcs(cs->bcs + i);
875 if (!gigaset_initbcs(cs->bcs + i, cs, i))
876 pr_err("could not allocate channel %d data\n", i);
879 if (cs->waiting) {
880 cs->cmd_result = -ENODEV;
881 cs->waiting = 0;
882 wake_up_interruptible(&cs->waitqueue);
888 * gigaset_start() - start device operations
889 * @cs: device descriptor structure.
891 * Prepares the device for use by setting up communication parameters,
892 * scheduling an EV_START event to initiate device initialization, and
893 * waiting for completion of the initialization.
895 * Return value:
896 * 1 - success, 0 - error
898 int gigaset_start(struct cardstate *cs)
900 unsigned long flags;
902 if (mutex_lock_interruptible(&cs->mutex))
903 return 0;
905 spin_lock_irqsave(&cs->lock, flags);
906 cs->connected = 1;
907 spin_unlock_irqrestore(&cs->lock, flags);
909 if (cs->mstate != MS_LOCKED) {
910 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
911 cs->ops->baud_rate(cs, B115200);
912 cs->ops->set_line_ctrl(cs, CS8);
913 cs->control_state = TIOCM_DTR|TIOCM_RTS;
914 } else {
915 //FIXME use some saved values?
918 cs->waiting = 1;
920 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
921 cs->waiting = 0;
922 //FIXME what should we do?
923 goto error;
926 gig_dbg(DEBUG_CMD, "scheduling START");
927 gigaset_schedule_event(cs);
929 wait_event(cs->waitqueue, !cs->waiting);
931 mutex_unlock(&cs->mutex);
932 return 1;
934 error:
935 mutex_unlock(&cs->mutex);
936 return 0;
938 EXPORT_SYMBOL_GPL(gigaset_start);
941 * gigaset_shutdown() - shut down device operations
942 * @cs: device descriptor structure.
944 * Deactivates the device by scheduling an EV_SHUTDOWN event and
945 * waiting for completion of the shutdown.
947 * Return value:
948 * 0 - success, -1 - error (no device associated)
950 int gigaset_shutdown(struct cardstate *cs)
952 mutex_lock(&cs->mutex);
954 if (!(cs->flags & VALID_MINOR)) {
955 mutex_unlock(&cs->mutex);
956 return -1;
959 cs->waiting = 1;
961 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
962 //FIXME what should we do?
963 goto exit;
966 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
967 gigaset_schedule_event(cs);
969 wait_event(cs->waitqueue, !cs->waiting);
971 cleanup_cs(cs);
973 exit:
974 mutex_unlock(&cs->mutex);
975 return 0;
977 EXPORT_SYMBOL_GPL(gigaset_shutdown);
980 * gigaset_stop() - stop device operations
981 * @cs: device descriptor structure.
983 * Stops operations on the device by scheduling an EV_STOP event and
984 * waiting for completion of the shutdown.
986 void gigaset_stop(struct cardstate *cs)
988 mutex_lock(&cs->mutex);
990 cs->waiting = 1;
992 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
993 //FIXME what should we do?
994 goto exit;
997 gig_dbg(DEBUG_CMD, "scheduling STOP");
998 gigaset_schedule_event(cs);
1000 wait_event(cs->waitqueue, !cs->waiting);
1002 cleanup_cs(cs);
1004 exit:
1005 mutex_unlock(&cs->mutex);
1007 EXPORT_SYMBOL_GPL(gigaset_stop);
1009 static LIST_HEAD(drivers);
1010 static DEFINE_SPINLOCK(driver_lock);
1012 struct cardstate *gigaset_get_cs_by_id(int id)
1014 unsigned long flags;
1015 struct cardstate *ret = NULL;
1016 struct cardstate *cs;
1017 struct gigaset_driver *drv;
1018 unsigned i;
1020 spin_lock_irqsave(&driver_lock, flags);
1021 list_for_each_entry(drv, &drivers, list) {
1022 spin_lock(&drv->lock);
1023 for (i = 0; i < drv->minors; ++i) {
1024 cs = drv->cs + i;
1025 if ((cs->flags & VALID_ID) && cs->myid == id) {
1026 ret = cs;
1027 break;
1030 spin_unlock(&drv->lock);
1031 if (ret)
1032 break;
1034 spin_unlock_irqrestore(&driver_lock, flags);
1035 return ret;
1038 void gigaset_debugdrivers(void)
1040 unsigned long flags;
1041 static struct cardstate *cs;
1042 struct gigaset_driver *drv;
1043 unsigned i;
1045 spin_lock_irqsave(&driver_lock, flags);
1046 list_for_each_entry(drv, &drivers, list) {
1047 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
1048 spin_lock(&drv->lock);
1049 for (i = 0; i < drv->minors; ++i) {
1050 gig_dbg(DEBUG_DRIVER, " index %u", i);
1051 cs = drv->cs + i;
1052 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
1053 gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
1054 gig_dbg(DEBUG_DRIVER, " minor_index %u",
1055 cs->minor_index);
1056 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
1057 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
1059 spin_unlock(&drv->lock);
1061 spin_unlock_irqrestore(&driver_lock, flags);
1064 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1066 unsigned long flags;
1067 struct cardstate *ret = NULL;
1068 struct gigaset_driver *drv;
1069 unsigned index;
1071 spin_lock_irqsave(&driver_lock, flags);
1072 list_for_each_entry(drv, &drivers, list) {
1073 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1074 continue;
1075 index = minor - drv->minor;
1076 spin_lock(&drv->lock);
1077 if (drv->cs[index].flags & VALID_MINOR)
1078 ret = drv->cs + index;
1079 spin_unlock(&drv->lock);
1080 if (ret)
1081 break;
1083 spin_unlock_irqrestore(&driver_lock, flags);
1084 return ret;
1087 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1089 if (tty->index < 0 || tty->index >= tty->driver->num)
1090 return NULL;
1091 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1095 * gigaset_freedriver() - free all associated ressources of a driver
1096 * @drv: driver descriptor structure.
1098 * Unregisters the driver from the system and deallocates the driver
1099 * structure @drv and all structures referenced from it.
1100 * All devices should be shut down before calling this.
1102 void gigaset_freedriver(struct gigaset_driver *drv)
1104 unsigned long flags;
1106 spin_lock_irqsave(&driver_lock, flags);
1107 list_del(&drv->list);
1108 spin_unlock_irqrestore(&driver_lock, flags);
1110 gigaset_if_freedriver(drv);
1112 kfree(drv->cs);
1113 kfree(drv);
1115 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1118 * gigaset_initdriver() - initialize driver structure
1119 * @minor: First minor number
1120 * @minors: Number of minors this driver can handle
1121 * @procname: Name of the driver
1122 * @devname: Name of the device files (prefix without minor number)
1124 * Allocate and initialize gigaset_driver structure. Initialize interface.
1126 * Return value:
1127 * Pointer to the gigaset_driver structure on success, NULL on failure.
1129 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1130 const char *procname,
1131 const char *devname,
1132 const struct gigaset_ops *ops,
1133 struct module *owner)
1135 struct gigaset_driver *drv;
1136 unsigned long flags;
1137 unsigned i;
1139 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1140 if (!drv)
1141 return NULL;
1143 drv->have_tty = 0;
1144 drv->minor = minor;
1145 drv->minors = minors;
1146 spin_lock_init(&drv->lock);
1147 drv->blocked = 0;
1148 drv->ops = ops;
1149 drv->owner = owner;
1150 INIT_LIST_HEAD(&drv->list);
1152 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1153 if (!drv->cs)
1154 goto error;
1156 for (i = 0; i < minors; ++i) {
1157 drv->cs[i].flags = 0;
1158 drv->cs[i].driver = drv;
1159 drv->cs[i].ops = drv->ops;
1160 drv->cs[i].minor_index = i;
1161 mutex_init(&drv->cs[i].mutex);
1164 gigaset_if_initdriver(drv, procname, devname);
1166 spin_lock_irqsave(&driver_lock, flags);
1167 list_add(&drv->list, &drivers);
1168 spin_unlock_irqrestore(&driver_lock, flags);
1170 return drv;
1172 error:
1173 kfree(drv->cs);
1174 kfree(drv);
1175 return NULL;
1177 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1180 * gigaset_blockdriver() - block driver
1181 * @drv: driver descriptor structure.
1183 * Prevents the driver from attaching new devices, in preparation for
1184 * deregistration.
1186 void gigaset_blockdriver(struct gigaset_driver *drv)
1188 drv->blocked = 1;
1190 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1192 static int __init gigaset_init_module(void)
1194 /* in accordance with the principle of least astonishment,
1195 * setting the 'debug' parameter to 1 activates a sensible
1196 * set of default debug levels
1198 if (gigaset_debuglevel == 1)
1199 gigaset_debuglevel = DEBUG_DEFAULT;
1201 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
1202 return 0;
1205 static void __exit gigaset_exit_module(void)
1209 module_init(gigaset_init_module);
1210 module_exit(gigaset_exit_module);
1212 MODULE_AUTHOR(DRIVER_AUTHOR);
1213 MODULE_DESCRIPTION(DRIVER_DESC);
1215 MODULE_LICENSE("GPL");