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