]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/isdn/gigaset/common.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-eoan-kernel.git] / drivers / isdn / gigaset / common.c
1 /*
2 * Stuff used by all variants of the driver
3 *
4 * Copyright (c) 2001 by Stefan Eilers,
5 * Hansjoerg Lipp <hjlipp@web.de>,
6 * Tilman Schmidt <tilman@imap.cc>.
7 *
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 * =====================================================================
14 */
15
16 #include "gigaset.h"
17 #include <linux/ctype.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/slab.h>
21
22 /* Version Information */
23 #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
24 #define DRIVER_DESC "Driver for Gigaset 307x"
25
26 #ifdef CONFIG_GIGASET_DEBUG
27 #define DRIVER_DESC_DEBUG " (debug build)"
28 #else
29 #define DRIVER_DESC_DEBUG ""
30 #endif
31
32 /* Module parameters */
33 int gigaset_debuglevel;
34 EXPORT_SYMBOL_GPL(gigaset_debuglevel);
35 module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
36 MODULE_PARM_DESC(debug, "debug level");
37
38 /* driver state flags */
39 #define VALID_MINOR 0x01
40 #define VALID_ID 0x02
41
42 /**
43 * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging
44 * @level: debugging level.
45 * @msg: message prefix.
46 * @len: number of bytes to dump.
47 * @buf: data to dump.
48 *
49 * If the current debugging level includes one of the bits set in @level,
50 * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio,
51 * prefixed by the text @msg.
52 */
53 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
54 size_t len, const unsigned char *buf)
55 {
56 unsigned char outbuf[80];
57 unsigned char c;
58 size_t space = sizeof outbuf - 1;
59 unsigned char *out = outbuf;
60 size_t numin = len;
61
62 while (numin--) {
63 c = *buf++;
64 if (c == '~' || c == '^' || c == '\\') {
65 if (!space--)
66 break;
67 *out++ = '\\';
68 }
69 if (c & 0x80) {
70 if (!space--)
71 break;
72 *out++ = '~';
73 c ^= 0x80;
74 }
75 if (c < 0x20 || c == 0x7f) {
76 if (!space--)
77 break;
78 *out++ = '^';
79 c ^= 0x40;
80 }
81 if (!space--)
82 break;
83 *out++ = c;
84 }
85 *out = 0;
86
87 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
88 }
89 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
90
91 static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
92 {
93 int r;
94
95 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
96 cs->control_state = flags;
97 if (r < 0)
98 return r;
99
100 if (delay) {
101 set_current_state(TASK_INTERRUPTIBLE);
102 schedule_timeout(delay * HZ / 1000);
103 }
104
105 return 0;
106 }
107
108 int gigaset_enterconfigmode(struct cardstate *cs)
109 {
110 int i, r;
111
112 cs->control_state = TIOCM_RTS;
113
114 r = setflags(cs, TIOCM_DTR, 200);
115 if (r < 0)
116 goto error;
117 r = setflags(cs, 0, 200);
118 if (r < 0)
119 goto error;
120 for (i = 0; i < 5; ++i) {
121 r = setflags(cs, TIOCM_RTS, 100);
122 if (r < 0)
123 goto error;
124 r = setflags(cs, 0, 100);
125 if (r < 0)
126 goto error;
127 }
128 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
129 if (r < 0)
130 goto error;
131
132 return 0;
133
134 error:
135 dev_err(cs->dev, "error %d on setuartbits\n", -r);
136 cs->control_state = TIOCM_RTS|TIOCM_DTR;
137 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
138
139 return -1;
140 }
141
142 static int test_timeout(struct at_state_t *at_state)
143 {
144 if (!at_state->timer_expires)
145 return 0;
146
147 if (--at_state->timer_expires) {
148 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
149 at_state, at_state->timer_expires);
150 return 0;
151 }
152
153 gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
154 at_state->timer_index, NULL);
155 return 1;
156 }
157
158 static void timer_tick(unsigned long data)
159 {
160 struct cardstate *cs = (struct cardstate *) data;
161 unsigned long flags;
162 unsigned channel;
163 struct at_state_t *at_state;
164 int timeout = 0;
165
166 spin_lock_irqsave(&cs->lock, flags);
167
168 for (channel = 0; channel < cs->channels; ++channel)
169 if (test_timeout(&cs->bcs[channel].at_state))
170 timeout = 1;
171
172 if (test_timeout(&cs->at_state))
173 timeout = 1;
174
175 list_for_each_entry(at_state, &cs->temp_at_states, list)
176 if (test_timeout(at_state))
177 timeout = 1;
178
179 if (cs->running) {
180 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
181 if (timeout) {
182 gig_dbg(DEBUG_EVENT, "scheduling timeout");
183 tasklet_schedule(&cs->event_tasklet);
184 }
185 }
186
187 spin_unlock_irqrestore(&cs->lock, flags);
188 }
189
190 int gigaset_get_channel(struct bc_state *bcs)
191 {
192 unsigned long flags;
193
194 spin_lock_irqsave(&bcs->cs->lock, flags);
195 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
196 gig_dbg(DEBUG_CHANNEL, "could not allocate channel %d",
197 bcs->channel);
198 spin_unlock_irqrestore(&bcs->cs->lock, flags);
199 return 0;
200 }
201 ++bcs->use_count;
202 bcs->busy = 1;
203 gig_dbg(DEBUG_CHANNEL, "allocated channel %d", bcs->channel);
204 spin_unlock_irqrestore(&bcs->cs->lock, flags);
205 return 1;
206 }
207
208 struct bc_state *gigaset_get_free_channel(struct cardstate *cs)
209 {
210 unsigned long flags;
211 int i;
212
213 spin_lock_irqsave(&cs->lock, flags);
214 if (!try_module_get(cs->driver->owner)) {
215 gig_dbg(DEBUG_CHANNEL,
216 "could not get module for allocating channel");
217 spin_unlock_irqrestore(&cs->lock, flags);
218 return NULL;
219 }
220 for (i = 0; i < cs->channels; ++i)
221 if (!cs->bcs[i].use_count) {
222 ++cs->bcs[i].use_count;
223 cs->bcs[i].busy = 1;
224 spin_unlock_irqrestore(&cs->lock, flags);
225 gig_dbg(DEBUG_CHANNEL, "allocated channel %d", i);
226 return cs->bcs + i;
227 }
228 module_put(cs->driver->owner);
229 spin_unlock_irqrestore(&cs->lock, flags);
230 gig_dbg(DEBUG_CHANNEL, "no free channel");
231 return NULL;
232 }
233
234 void gigaset_free_channel(struct bc_state *bcs)
235 {
236 unsigned long flags;
237
238 spin_lock_irqsave(&bcs->cs->lock, flags);
239 if (!bcs->busy) {
240 gig_dbg(DEBUG_CHANNEL, "could not free channel %d",
241 bcs->channel);
242 spin_unlock_irqrestore(&bcs->cs->lock, flags);
243 return;
244 }
245 --bcs->use_count;
246 bcs->busy = 0;
247 module_put(bcs->cs->driver->owner);
248 gig_dbg(DEBUG_CHANNEL, "freed channel %d", bcs->channel);
249 spin_unlock_irqrestore(&bcs->cs->lock, flags);
250 }
251
252 int gigaset_get_channels(struct cardstate *cs)
253 {
254 unsigned long flags;
255 int i;
256
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_CHANNEL,
262 "could not allocate all channels");
263 return 0;
264 }
265 for (i = 0; i < cs->channels; ++i)
266 ++cs->bcs[i].use_count;
267 spin_unlock_irqrestore(&cs->lock, flags);
268
269 gig_dbg(DEBUG_CHANNEL, "allocated all channels");
270
271 return 1;
272 }
273
274 void gigaset_free_channels(struct cardstate *cs)
275 {
276 unsigned long flags;
277 int i;
278
279 gig_dbg(DEBUG_CHANNEL, "unblocking all channels");
280 spin_lock_irqsave(&cs->lock, flags);
281 for (i = 0; i < cs->channels; ++i)
282 --cs->bcs[i].use_count;
283 spin_unlock_irqrestore(&cs->lock, flags);
284 }
285
286 void gigaset_block_channels(struct cardstate *cs)
287 {
288 unsigned long flags;
289 int i;
290
291 gig_dbg(DEBUG_CHANNEL, "blocking all channels");
292 spin_lock_irqsave(&cs->lock, flags);
293 for (i = 0; i < cs->channels; ++i)
294 ++cs->bcs[i].use_count;
295 spin_unlock_irqrestore(&cs->lock, flags);
296 }
297
298 static void clear_events(struct cardstate *cs)
299 {
300 struct event_t *ev;
301 unsigned head, tail;
302 unsigned long flags;
303
304 spin_lock_irqsave(&cs->ev_lock, flags);
305
306 head = cs->ev_head;
307 tail = cs->ev_tail;
308
309 while (tail != head) {
310 ev = cs->events + head;
311 kfree(ev->ptr);
312 head = (head + 1) % MAX_EVENTS;
313 }
314
315 cs->ev_head = tail;
316
317 spin_unlock_irqrestore(&cs->ev_lock, flags);
318 }
319
320 /**
321 * gigaset_add_event() - add event to device event queue
322 * @cs: device descriptor structure.
323 * @at_state: connection state structure.
324 * @type: event type.
325 * @ptr: pointer parameter for event.
326 * @parameter: integer parameter for event.
327 * @arg: pointer parameter for event.
328 *
329 * Allocate an event queue entry from the device's event queue, and set it up
330 * with the parameters given.
331 *
332 * Return value: added event
333 */
334 struct event_t *gigaset_add_event(struct cardstate *cs,
335 struct at_state_t *at_state, int type,
336 void *ptr, int parameter, void *arg)
337 {
338 unsigned long flags;
339 unsigned next, tail;
340 struct event_t *event = NULL;
341
342 gig_dbg(DEBUG_EVENT, "queueing event %d", type);
343
344 spin_lock_irqsave(&cs->ev_lock, flags);
345
346 tail = cs->ev_tail;
347 next = (tail + 1) % MAX_EVENTS;
348 if (unlikely(next == cs->ev_head))
349 dev_err(cs->dev, "event queue full\n");
350 else {
351 event = cs->events + tail;
352 event->type = type;
353 event->at_state = at_state;
354 event->cid = -1;
355 event->ptr = ptr;
356 event->arg = arg;
357 event->parameter = parameter;
358 cs->ev_tail = next;
359 }
360
361 spin_unlock_irqrestore(&cs->ev_lock, flags);
362
363 return event;
364 }
365 EXPORT_SYMBOL_GPL(gigaset_add_event);
366
367 static void free_strings(struct at_state_t *at_state)
368 {
369 int i;
370
371 for (i = 0; i < STR_NUM; ++i) {
372 kfree(at_state->str_var[i]);
373 at_state->str_var[i] = NULL;
374 }
375 }
376
377 static void clear_at_state(struct at_state_t *at_state)
378 {
379 free_strings(at_state);
380 }
381
382 static void dealloc_at_states(struct cardstate *cs)
383 {
384 struct at_state_t *cur, *next;
385
386 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
387 list_del(&cur->list);
388 free_strings(cur);
389 kfree(cur);
390 }
391 }
392
393 static void gigaset_freebcs(struct bc_state *bcs)
394 {
395 int i;
396
397 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
398 if (!bcs->cs->ops->freebcshw(bcs))
399 gig_dbg(DEBUG_INIT, "failed");
400
401 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
402 clear_at_state(&bcs->at_state);
403 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
404 dev_kfree_skb(bcs->skb);
405 bcs->skb = NULL;
406
407 for (i = 0; i < AT_NUM; ++i) {
408 kfree(bcs->commands[i]);
409 bcs->commands[i] = NULL;
410 }
411 }
412
413 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
414 {
415 unsigned long flags;
416 unsigned i;
417 struct cardstate *cs;
418 struct cardstate *ret = NULL;
419
420 spin_lock_irqsave(&drv->lock, flags);
421 if (drv->blocked)
422 goto exit;
423 for (i = 0; i < drv->minors; ++i) {
424 cs = drv->cs + i;
425 if (!(cs->flags & VALID_MINOR)) {
426 cs->flags = VALID_MINOR;
427 ret = cs;
428 break;
429 }
430 }
431 exit:
432 spin_unlock_irqrestore(&drv->lock, flags);
433 return ret;
434 }
435
436 static void free_cs(struct cardstate *cs)
437 {
438 cs->flags = 0;
439 }
440
441 static void make_valid(struct cardstate *cs, unsigned mask)
442 {
443 unsigned long flags;
444 struct gigaset_driver *drv = cs->driver;
445 spin_lock_irqsave(&drv->lock, flags);
446 cs->flags |= mask;
447 spin_unlock_irqrestore(&drv->lock, flags);
448 }
449
450 static void make_invalid(struct cardstate *cs, unsigned mask)
451 {
452 unsigned long flags;
453 struct gigaset_driver *drv = cs->driver;
454 spin_lock_irqsave(&drv->lock, flags);
455 cs->flags &= ~mask;
456 spin_unlock_irqrestore(&drv->lock, flags);
457 }
458
459 /**
460 * gigaset_freecs() - free all associated ressources of a device
461 * @cs: device descriptor structure.
462 *
463 * Stops all tasklets and timers, unregisters the device from all
464 * subsystems it was registered to, deallocates the device structure
465 * @cs and all structures referenced from it.
466 * Operations on the device should be stopped before calling this.
467 */
468 void gigaset_freecs(struct cardstate *cs)
469 {
470 int i;
471 unsigned long flags;
472
473 if (!cs)
474 return;
475
476 mutex_lock(&cs->mutex);
477
478 if (!cs->bcs)
479 goto f_cs;
480 if (!cs->inbuf)
481 goto f_bcs;
482
483 spin_lock_irqsave(&cs->lock, flags);
484 cs->running = 0;
485 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
486 not rescheduled below */
487
488 tasklet_kill(&cs->event_tasklet);
489 del_timer_sync(&cs->timer);
490
491 switch (cs->cs_init) {
492 default:
493 /* clear B channel structures */
494 for (i = 0; i < cs->channels; ++i) {
495 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
496 gigaset_freebcs(cs->bcs + i);
497 }
498
499 /* clear device sysfs */
500 gigaset_free_dev_sysfs(cs);
501
502 gigaset_if_free(cs);
503
504 gig_dbg(DEBUG_INIT, "clearing hw");
505 cs->ops->freecshw(cs);
506
507 /* fall through */
508 case 2: /* error in initcshw */
509 /* Deregister from LL */
510 make_invalid(cs, VALID_ID);
511 gigaset_isdn_unregdev(cs);
512
513 /* fall through */
514 case 1: /* error when registering to LL */
515 gig_dbg(DEBUG_INIT, "clearing at_state");
516 clear_at_state(&cs->at_state);
517 dealloc_at_states(cs);
518
519 /* fall through */
520 case 0: /* error in basic setup */
521 clear_events(cs);
522 gig_dbg(DEBUG_INIT, "freeing inbuf");
523 kfree(cs->inbuf);
524 }
525 f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
526 kfree(cs->bcs);
527 f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
528 mutex_unlock(&cs->mutex);
529 free_cs(cs);
530 }
531 EXPORT_SYMBOL_GPL(gigaset_freecs);
532
533 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
534 struct cardstate *cs, int cid)
535 {
536 int i;
537
538 INIT_LIST_HEAD(&at_state->list);
539 at_state->waiting = 0;
540 at_state->getstring = 0;
541 at_state->pending_commands = 0;
542 at_state->timer_expires = 0;
543 at_state->timer_active = 0;
544 at_state->timer_index = 0;
545 at_state->seq_index = 0;
546 at_state->ConState = 0;
547 for (i = 0; i < STR_NUM; ++i)
548 at_state->str_var[i] = NULL;
549 at_state->int_var[VAR_ZDLE] = 0;
550 at_state->int_var[VAR_ZCTP] = -1;
551 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
552 at_state->cs = cs;
553 at_state->bcs = bcs;
554 at_state->cid = cid;
555 if (!cid)
556 at_state->replystruct = cs->tabnocid;
557 else
558 at_state->replystruct = cs->tabcid;
559 }
560
561
562 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
563 /* inbuf->read must be allocated before! */
564 {
565 inbuf->head = 0;
566 inbuf->tail = 0;
567 inbuf->cs = cs;
568 inbuf->inputstate = INS_command;
569 }
570
571 /**
572 * gigaset_fill_inbuf() - append received data to input buffer
573 * @inbuf: buffer structure.
574 * @src: received data.
575 * @numbytes: number of bytes received.
576 */
577 int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
578 unsigned numbytes)
579 {
580 unsigned n, head, tail, bytesleft;
581
582 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
583
584 if (!numbytes)
585 return 0;
586
587 bytesleft = numbytes;
588 tail = inbuf->tail;
589 head = inbuf->head;
590 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
591
592 while (bytesleft) {
593 if (head > tail)
594 n = head - 1 - tail;
595 else if (head == 0)
596 n = (RBUFSIZE-1) - tail;
597 else
598 n = RBUFSIZE - tail;
599 if (!n) {
600 dev_err(inbuf->cs->dev,
601 "buffer overflow (%u bytes lost)\n",
602 bytesleft);
603 break;
604 }
605 if (n > bytesleft)
606 n = bytesleft;
607 memcpy(inbuf->data + tail, src, n);
608 bytesleft -= n;
609 tail = (tail + n) % RBUFSIZE;
610 src += n;
611 }
612 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
613 inbuf->tail = tail;
614 return numbytes != bytesleft;
615 }
616 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
617
618 /* Initialize the b-channel structure */
619 static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
620 struct cardstate *cs, int channel)
621 {
622 int i;
623
624 bcs->tx_skb = NULL;
625
626 skb_queue_head_init(&bcs->squeue);
627
628 bcs->corrupted = 0;
629 bcs->trans_down = 0;
630 bcs->trans_up = 0;
631
632 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
633 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
634
635 #ifdef CONFIG_GIGASET_DEBUG
636 bcs->emptycount = 0;
637 #endif
638
639 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
640 bcs->fcs = PPP_INITFCS;
641 bcs->inputstate = 0;
642 if (cs->ignoreframes) {
643 bcs->skb = NULL;
644 } else {
645 bcs->skb = dev_alloc_skb(SBUFSIZE + cs->hw_hdr_len);
646 if (bcs->skb != NULL)
647 skb_reserve(bcs->skb, cs->hw_hdr_len);
648 else
649 pr_err("out of memory\n");
650 }
651
652 bcs->channel = channel;
653 bcs->cs = cs;
654
655 bcs->chstate = 0;
656 bcs->use_count = 1;
657 bcs->busy = 0;
658 bcs->ignore = cs->ignoreframes;
659
660 for (i = 0; i < AT_NUM; ++i)
661 bcs->commands[i] = NULL;
662
663 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
664 if (cs->ops->initbcshw(bcs))
665 return bcs;
666
667 gig_dbg(DEBUG_INIT, " failed");
668
669 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
670 dev_kfree_skb(bcs->skb);
671 bcs->skb = NULL;
672
673 return NULL;
674 }
675
676 /**
677 * gigaset_initcs() - initialize device structure
678 * @drv: hardware driver the device belongs to
679 * @channels: number of B channels supported by device
680 * @onechannel: !=0 if B channel data and AT commands share one
681 * communication channel (M10x),
682 * ==0 if B channels have separate communication channels (base)
683 * @ignoreframes: number of frames to ignore after setting up B channel
684 * @cidmode: !=0: start in CallID mode
685 * @modulename: name of driver module for LL registration
686 *
687 * Allocate and initialize cardstate structure for Gigaset driver
688 * Calls hardware dependent gigaset_initcshw() function
689 * Calls B channel initialization function gigaset_initbcs() for each B channel
690 *
691 * Return value:
692 * pointer to cardstate structure
693 */
694 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
695 int onechannel, int ignoreframes,
696 int cidmode, const char *modulename)
697 {
698 struct cardstate *cs;
699 unsigned long flags;
700 int i;
701
702 gig_dbg(DEBUG_INIT, "allocating cs");
703 cs = alloc_cs(drv);
704 if (!cs) {
705 pr_err("maximum number of devices exceeded\n");
706 return NULL;
707 }
708
709 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
710 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
711 if (!cs->bcs) {
712 pr_err("out of memory\n");
713 goto error;
714 }
715 gig_dbg(DEBUG_INIT, "allocating inbuf");
716 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
717 if (!cs->inbuf) {
718 pr_err("out of memory\n");
719 goto error;
720 }
721
722 cs->cs_init = 0;
723 cs->channels = channels;
724 cs->onechannel = onechannel;
725 cs->ignoreframes = ignoreframes;
726 INIT_LIST_HEAD(&cs->temp_at_states);
727 cs->running = 0;
728 init_timer(&cs->timer); /* clear next & prev */
729 spin_lock_init(&cs->ev_lock);
730 cs->ev_tail = 0;
731 cs->ev_head = 0;
732
733 tasklet_init(&cs->event_tasklet, gigaset_handle_event,
734 (unsigned long) cs);
735 cs->commands_pending = 0;
736 cs->cur_at_seq = 0;
737 cs->gotfwver = -1;
738 cs->open_count = 0;
739 cs->dev = NULL;
740 cs->tty = NULL;
741 cs->tty_dev = NULL;
742 cs->cidmode = cidmode != 0;
743 cs->tabnocid = gigaset_tab_nocid;
744 cs->tabcid = gigaset_tab_cid;
745
746 init_waitqueue_head(&cs->waitqueue);
747 cs->waiting = 0;
748
749 cs->mode = M_UNKNOWN;
750 cs->mstate = MS_UNINITIALIZED;
751
752 ++cs->cs_init;
753
754 gig_dbg(DEBUG_INIT, "setting up at_state");
755 spin_lock_init(&cs->lock);
756 gigaset_at_init(&cs->at_state, NULL, cs, 0);
757 cs->dle = 0;
758 cs->cbytes = 0;
759
760 gig_dbg(DEBUG_INIT, "setting up inbuf");
761 gigaset_inbuf_init(cs->inbuf, cs);
762
763 cs->connected = 0;
764 cs->isdn_up = 0;
765
766 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
767 cs->cmdbuf = cs->lastcmdbuf = NULL;
768 spin_lock_init(&cs->cmdlock);
769 cs->curlen = 0;
770 cs->cmdbytes = 0;
771
772 gig_dbg(DEBUG_INIT, "setting up iif");
773 if (!gigaset_isdn_regdev(cs, modulename)) {
774 pr_err("error registering ISDN device\n");
775 goto error;
776 }
777
778 make_valid(cs, VALID_ID);
779 ++cs->cs_init;
780 gig_dbg(DEBUG_INIT, "setting up hw");
781 if (!cs->ops->initcshw(cs))
782 goto error;
783
784 ++cs->cs_init;
785
786 /* set up character device */
787 gigaset_if_init(cs);
788
789 /* set up device sysfs */
790 gigaset_init_dev_sysfs(cs);
791
792 /* set up channel data structures */
793 for (i = 0; i < channels; ++i) {
794 gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
795 if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
796 pr_err("could not allocate channel %d data\n", i);
797 goto error;
798 }
799 }
800
801 spin_lock_irqsave(&cs->lock, flags);
802 cs->running = 1;
803 spin_unlock_irqrestore(&cs->lock, flags);
804 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
805 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
806 /* FIXME: can jiffies increase too much until the timer is added?
807 * Same problem(?) with mod_timer() in timer_tick(). */
808 add_timer(&cs->timer);
809
810 gig_dbg(DEBUG_INIT, "cs initialized");
811 return cs;
812
813 error:
814 gig_dbg(DEBUG_INIT, "failed");
815 gigaset_freecs(cs);
816 return NULL;
817 }
818 EXPORT_SYMBOL_GPL(gigaset_initcs);
819
820 /* ReInitialize the b-channel structure on hangup */
821 void gigaset_bcs_reinit(struct bc_state *bcs)
822 {
823 struct sk_buff *skb;
824 struct cardstate *cs = bcs->cs;
825 unsigned long flags;
826
827 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
828 dev_kfree_skb(skb);
829
830 spin_lock_irqsave(&cs->lock, flags);
831 clear_at_state(&bcs->at_state);
832 bcs->at_state.ConState = 0;
833 bcs->at_state.timer_active = 0;
834 bcs->at_state.timer_expires = 0;
835 bcs->at_state.cid = -1; /* No CID defined */
836 spin_unlock_irqrestore(&cs->lock, flags);
837
838 bcs->inputstate = 0;
839
840 #ifdef CONFIG_GIGASET_DEBUG
841 bcs->emptycount = 0;
842 #endif
843
844 bcs->fcs = PPP_INITFCS;
845 bcs->chstate = 0;
846
847 bcs->ignore = cs->ignoreframes;
848 if (bcs->ignore) {
849 dev_kfree_skb(bcs->skb);
850 bcs->skb = NULL;
851 }
852
853 cs->ops->reinitbcshw(bcs);
854 }
855
856 static void cleanup_cs(struct cardstate *cs)
857 {
858 struct cmdbuf_t *cb, *tcb;
859 int i;
860 unsigned long flags;
861
862 spin_lock_irqsave(&cs->lock, flags);
863
864 cs->mode = M_UNKNOWN;
865 cs->mstate = MS_UNINITIALIZED;
866
867 clear_at_state(&cs->at_state);
868 dealloc_at_states(cs);
869 free_strings(&cs->at_state);
870 gigaset_at_init(&cs->at_state, NULL, cs, 0);
871
872 cs->inbuf->inputstate = INS_command;
873 cs->inbuf->head = 0;
874 cs->inbuf->tail = 0;
875
876 cb = cs->cmdbuf;
877 while (cb) {
878 tcb = cb;
879 cb = cb->next;
880 kfree(tcb);
881 }
882 cs->cmdbuf = cs->lastcmdbuf = NULL;
883 cs->curlen = 0;
884 cs->cmdbytes = 0;
885 cs->gotfwver = -1;
886 cs->dle = 0;
887 cs->cur_at_seq = 0;
888 cs->commands_pending = 0;
889 cs->cbytes = 0;
890
891 spin_unlock_irqrestore(&cs->lock, flags);
892
893 for (i = 0; i < cs->channels; ++i) {
894 gigaset_freebcs(cs->bcs + i);
895 if (!gigaset_initbcs(cs->bcs + i, cs, i))
896 pr_err("could not allocate channel %d data\n", i);
897 }
898
899 if (cs->waiting) {
900 cs->cmd_result = -ENODEV;
901 cs->waiting = 0;
902 wake_up_interruptible(&cs->waitqueue);
903 }
904 }
905
906
907 /**
908 * gigaset_start() - start device operations
909 * @cs: device descriptor structure.
910 *
911 * Prepares the device for use by setting up communication parameters,
912 * scheduling an EV_START event to initiate device initialization, and
913 * waiting for completion of the initialization.
914 *
915 * Return value:
916 * 1 - success, 0 - error
917 */
918 int gigaset_start(struct cardstate *cs)
919 {
920 unsigned long flags;
921
922 if (mutex_lock_interruptible(&cs->mutex))
923 return 0;
924
925 spin_lock_irqsave(&cs->lock, flags);
926 cs->connected = 1;
927 spin_unlock_irqrestore(&cs->lock, flags);
928
929 if (cs->mstate != MS_LOCKED) {
930 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
931 cs->ops->baud_rate(cs, B115200);
932 cs->ops->set_line_ctrl(cs, CS8);
933 cs->control_state = TIOCM_DTR|TIOCM_RTS;
934 }
935
936 cs->waiting = 1;
937
938 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
939 cs->waiting = 0;
940 goto error;
941 }
942 gigaset_schedule_event(cs);
943
944 wait_event(cs->waitqueue, !cs->waiting);
945
946 mutex_unlock(&cs->mutex);
947 return 1;
948
949 error:
950 mutex_unlock(&cs->mutex);
951 return 0;
952 }
953 EXPORT_SYMBOL_GPL(gigaset_start);
954
955 /**
956 * gigaset_shutdown() - shut down device operations
957 * @cs: device descriptor structure.
958 *
959 * Deactivates the device by scheduling an EV_SHUTDOWN event and
960 * waiting for completion of the shutdown.
961 *
962 * Return value:
963 * 0 - success, -1 - error (no device associated)
964 */
965 int gigaset_shutdown(struct cardstate *cs)
966 {
967 mutex_lock(&cs->mutex);
968
969 if (!(cs->flags & VALID_MINOR)) {
970 mutex_unlock(&cs->mutex);
971 return -1;
972 }
973
974 cs->waiting = 1;
975
976 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL))
977 goto exit;
978 gigaset_schedule_event(cs);
979
980 wait_event(cs->waitqueue, !cs->waiting);
981
982 cleanup_cs(cs);
983
984 exit:
985 mutex_unlock(&cs->mutex);
986 return 0;
987 }
988 EXPORT_SYMBOL_GPL(gigaset_shutdown);
989
990 /**
991 * gigaset_stop() - stop device operations
992 * @cs: device descriptor structure.
993 *
994 * Stops operations on the device by scheduling an EV_STOP event and
995 * waiting for completion of the shutdown.
996 */
997 void gigaset_stop(struct cardstate *cs)
998 {
999 mutex_lock(&cs->mutex);
1000
1001 cs->waiting = 1;
1002
1003 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL))
1004 goto exit;
1005 gigaset_schedule_event(cs);
1006
1007 wait_event(cs->waitqueue, !cs->waiting);
1008
1009 cleanup_cs(cs);
1010
1011 exit:
1012 mutex_unlock(&cs->mutex);
1013 }
1014 EXPORT_SYMBOL_GPL(gigaset_stop);
1015
1016 static LIST_HEAD(drivers);
1017 static DEFINE_SPINLOCK(driver_lock);
1018
1019 struct cardstate *gigaset_get_cs_by_id(int id)
1020 {
1021 unsigned long flags;
1022 struct cardstate *ret = NULL;
1023 struct cardstate *cs;
1024 struct gigaset_driver *drv;
1025 unsigned i;
1026
1027 spin_lock_irqsave(&driver_lock, flags);
1028 list_for_each_entry(drv, &drivers, list) {
1029 spin_lock(&drv->lock);
1030 for (i = 0; i < drv->minors; ++i) {
1031 cs = drv->cs + i;
1032 if ((cs->flags & VALID_ID) && cs->myid == id) {
1033 ret = cs;
1034 break;
1035 }
1036 }
1037 spin_unlock(&drv->lock);
1038 if (ret)
1039 break;
1040 }
1041 spin_unlock_irqrestore(&driver_lock, flags);
1042 return ret;
1043 }
1044
1045 void gigaset_debugdrivers(void)
1046 {
1047 unsigned long flags;
1048 static struct cardstate *cs;
1049 struct gigaset_driver *drv;
1050 unsigned i;
1051
1052 spin_lock_irqsave(&driver_lock, flags);
1053 list_for_each_entry(drv, &drivers, list) {
1054 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
1055 spin_lock(&drv->lock);
1056 for (i = 0; i < drv->minors; ++i) {
1057 gig_dbg(DEBUG_DRIVER, " index %u", i);
1058 cs = drv->cs + i;
1059 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
1060 gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
1061 gig_dbg(DEBUG_DRIVER, " minor_index %u",
1062 cs->minor_index);
1063 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
1064 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
1065 }
1066 spin_unlock(&drv->lock);
1067 }
1068 spin_unlock_irqrestore(&driver_lock, flags);
1069 }
1070
1071 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1072 {
1073 unsigned long flags;
1074 struct cardstate *ret = NULL;
1075 struct gigaset_driver *drv;
1076 unsigned index;
1077
1078 spin_lock_irqsave(&driver_lock, flags);
1079 list_for_each_entry(drv, &drivers, list) {
1080 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1081 continue;
1082 index = minor - drv->minor;
1083 spin_lock(&drv->lock);
1084 if (drv->cs[index].flags & VALID_MINOR)
1085 ret = drv->cs + index;
1086 spin_unlock(&drv->lock);
1087 if (ret)
1088 break;
1089 }
1090 spin_unlock_irqrestore(&driver_lock, flags);
1091 return ret;
1092 }
1093
1094 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1095 {
1096 if (tty->index < 0 || tty->index >= tty->driver->num)
1097 return NULL;
1098 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1099 }
1100
1101 /**
1102 * gigaset_freedriver() - free all associated ressources of a driver
1103 * @drv: driver descriptor structure.
1104 *
1105 * Unregisters the driver from the system and deallocates the driver
1106 * structure @drv and all structures referenced from it.
1107 * All devices should be shut down before calling this.
1108 */
1109 void gigaset_freedriver(struct gigaset_driver *drv)
1110 {
1111 unsigned long flags;
1112
1113 spin_lock_irqsave(&driver_lock, flags);
1114 list_del(&drv->list);
1115 spin_unlock_irqrestore(&driver_lock, flags);
1116
1117 gigaset_if_freedriver(drv);
1118
1119 kfree(drv->cs);
1120 kfree(drv);
1121 }
1122 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1123
1124 /**
1125 * gigaset_initdriver() - initialize driver structure
1126 * @minor: First minor number
1127 * @minors: Number of minors this driver can handle
1128 * @procname: Name of the driver
1129 * @devname: Name of the device files (prefix without minor number)
1130 *
1131 * Allocate and initialize gigaset_driver structure. Initialize interface.
1132 *
1133 * Return value:
1134 * Pointer to the gigaset_driver structure on success, NULL on failure.
1135 */
1136 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1137 const char *procname,
1138 const char *devname,
1139 const struct gigaset_ops *ops,
1140 struct module *owner)
1141 {
1142 struct gigaset_driver *drv;
1143 unsigned long flags;
1144 unsigned i;
1145
1146 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1147 if (!drv)
1148 return NULL;
1149
1150 drv->have_tty = 0;
1151 drv->minor = minor;
1152 drv->minors = minors;
1153 spin_lock_init(&drv->lock);
1154 drv->blocked = 0;
1155 drv->ops = ops;
1156 drv->owner = owner;
1157 INIT_LIST_HEAD(&drv->list);
1158
1159 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1160 if (!drv->cs)
1161 goto error;
1162
1163 for (i = 0; i < minors; ++i) {
1164 drv->cs[i].flags = 0;
1165 drv->cs[i].driver = drv;
1166 drv->cs[i].ops = drv->ops;
1167 drv->cs[i].minor_index = i;
1168 mutex_init(&drv->cs[i].mutex);
1169 }
1170
1171 gigaset_if_initdriver(drv, procname, devname);
1172
1173 spin_lock_irqsave(&driver_lock, flags);
1174 list_add(&drv->list, &drivers);
1175 spin_unlock_irqrestore(&driver_lock, flags);
1176
1177 return drv;
1178
1179 error:
1180 kfree(drv->cs);
1181 kfree(drv);
1182 return NULL;
1183 }
1184 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1185
1186 /**
1187 * gigaset_blockdriver() - block driver
1188 * @drv: driver descriptor structure.
1189 *
1190 * Prevents the driver from attaching new devices, in preparation for
1191 * deregistration.
1192 */
1193 void gigaset_blockdriver(struct gigaset_driver *drv)
1194 {
1195 drv->blocked = 1;
1196 }
1197 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1198
1199 static int __init gigaset_init_module(void)
1200 {
1201 /* in accordance with the principle of least astonishment,
1202 * setting the 'debug' parameter to 1 activates a sensible
1203 * set of default debug levels
1204 */
1205 if (gigaset_debuglevel == 1)
1206 gigaset_debuglevel = DEBUG_DEFAULT;
1207
1208 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
1209 gigaset_isdn_regdrv();
1210 return 0;
1211 }
1212
1213 static void __exit gigaset_exit_module(void)
1214 {
1215 gigaset_isdn_unregdrv();
1216 }
1217
1218 module_init(gigaset_init_module);
1219 module_exit(gigaset_exit_module);
1220
1221 MODULE_AUTHOR(DRIVER_AUTHOR);
1222 MODULE_DESCRIPTION(DRIVER_DESC);
1223
1224 MODULE_LICENSE("GPL");