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