usb_gigaset: code cleanup
[deliverable/linux.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
HL
31/* Module parameters */
32int gigaset_debuglevel = DEBUG_DEFAULT;
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
6fd5ea63
HL
111 cs->control_state = TIOCM_RTS; //FIXME
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);
6fd5ea63
HL
135 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
136 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
137
138 return -1; //r
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,
69049cc8 153 at_state->timer_index, NULL)) {
6fd5ea63
HL
154 //FIXME what should we do?
155 }
156
157 return 1;
158}
159
160static void timer_tick(unsigned long data)
161{
162 struct cardstate *cs = (struct cardstate *) data;
163 unsigned long flags;
164 unsigned channel;
165 struct at_state_t *at_state;
166 int timeout = 0;
167
168 spin_lock_irqsave(&cs->lock, flags);
169
170 for (channel = 0; channel < cs->channels; ++channel)
171 if (test_timeout(&cs->bcs[channel].at_state))
172 timeout = 1;
173
174 if (test_timeout(&cs->at_state))
175 timeout = 1;
176
177 list_for_each_entry(at_state, &cs->temp_at_states, list)
178 if (test_timeout(at_state))
179 timeout = 1;
180
69049cc8 181 if (cs->running) {
ec81b5e6 182 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
6fd5ea63 183 if (timeout) {
784d5858 184 gig_dbg(DEBUG_CMD, "scheduling timeout");
6fd5ea63
HL
185 tasklet_schedule(&cs->event_tasklet);
186 }
187 }
188
189 spin_unlock_irqrestore(&cs->lock, flags);
190}
191
192int gigaset_get_channel(struct bc_state *bcs)
193{
194 unsigned long flags;
195
196 spin_lock_irqsave(&bcs->cs->lock, flags);
e468c048 197 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
784d5858
TS
198 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
199 bcs->channel);
6fd5ea63
HL
200 spin_unlock_irqrestore(&bcs->cs->lock, flags);
201 return 0;
202 }
203 ++bcs->use_count;
204 bcs->busy = 1;
784d5858 205 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
6fd5ea63
HL
206 spin_unlock_irqrestore(&bcs->cs->lock, flags);
207 return 1;
208}
209
7bb5fdc2
TS
210struct bc_state *gigaset_get_free_channel(struct cardstate *cs)
211{
212 unsigned long flags;
213 int i;
214
215 spin_lock_irqsave(&cs->lock, flags);
216 if (!try_module_get(cs->driver->owner)) {
217 gig_dbg(DEBUG_ANY,
218 "could not get module for allocating channel");
219 spin_unlock_irqrestore(&cs->lock, flags);
220 return NULL;
221 }
222 for (i = 0; i < cs->channels; ++i)
223 if (!cs->bcs[i].use_count) {
224 ++cs->bcs[i].use_count;
225 cs->bcs[i].busy = 1;
226 spin_unlock_irqrestore(&cs->lock, flags);
227 gig_dbg(DEBUG_ANY, "allocated channel %d", i);
228 return cs->bcs + i;
229 }
230 module_put(cs->driver->owner);
231 spin_unlock_irqrestore(&cs->lock, flags);
232 gig_dbg(DEBUG_ANY, "no free channel");
233 return NULL;
234}
235
6fd5ea63
HL
236void gigaset_free_channel(struct bc_state *bcs)
237{
238 unsigned long flags;
239
240 spin_lock_irqsave(&bcs->cs->lock, flags);
241 if (!bcs->busy) {
784d5858 242 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
6fd5ea63
HL
243 spin_unlock_irqrestore(&bcs->cs->lock, flags);
244 return;
245 }
246 --bcs->use_count;
247 bcs->busy = 0;
e468c048 248 module_put(bcs->cs->driver->owner);
784d5858 249 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
6fd5ea63
HL
250 spin_unlock_irqrestore(&bcs->cs->lock, flags);
251}
252
253int gigaset_get_channels(struct cardstate *cs)
254{
255 unsigned long flags;
256 int i;
257
258 spin_lock_irqsave(&cs->lock, flags);
259 for (i = 0; i < cs->channels; ++i)
260 if (cs->bcs[i].use_count) {
261 spin_unlock_irqrestore(&cs->lock, flags);
784d5858 262 gig_dbg(DEBUG_ANY, "could not allocate all channels");
6fd5ea63
HL
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
784d5858 269 gig_dbg(DEBUG_ANY, "allocated all channels");
6fd5ea63
HL
270
271 return 1;
272}
273
274void gigaset_free_channels(struct cardstate *cs)
275{
276 unsigned long flags;
277 int i;
278
784d5858 279 gig_dbg(DEBUG_ANY, "unblocking all channels");
6fd5ea63
HL
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
286void gigaset_block_channels(struct cardstate *cs)
287{
288 unsigned long flags;
289 int i;
290
784d5858 291 gig_dbg(DEBUG_ANY, "blocking all channels");
6fd5ea63
HL
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
298static void clear_events(struct cardstate *cs)
299{
300 struct event_t *ev;
301 unsigned head, tail;
69049cc8 302 unsigned long flags;
6fd5ea63 303
69049cc8 304 spin_lock_irqsave(&cs->ev_lock, flags);
6fd5ea63 305
69049cc8
TS
306 head = cs->ev_head;
307 tail = cs->ev_tail;
6fd5ea63
HL
308
309 while (tail != head) {
310 ev = cs->events + head;
311 kfree(ev->ptr);
6fd5ea63
HL
312 head = (head + 1) % MAX_EVENTS;
313 }
314
69049cc8
TS
315 cs->ev_head = tail;
316
317 spin_unlock_irqrestore(&cs->ev_lock, flags);
6fd5ea63
HL
318}
319
1cec9727
TS
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 */
6fd5ea63 334struct event_t *gigaset_add_event(struct cardstate *cs,
784d5858
TS
335 struct at_state_t *at_state, int type,
336 void *ptr, int parameter, void *arg)
6fd5ea63
HL
337{
338 unsigned long flags;
339 unsigned next, tail;
340 struct event_t *event = NULL;
341
342 spin_lock_irqsave(&cs->ev_lock, flags);
343
69049cc8 344 tail = cs->ev_tail;
6fd5ea63 345 next = (tail + 1) % MAX_EVENTS;
69049cc8 346 if (unlikely(next == cs->ev_head))
5002779d 347 dev_err(cs->dev, "event queue full\n");
6fd5ea63
HL
348 else {
349 event = cs->events + tail;
350 event->type = type;
351 event->at_state = at_state;
352 event->cid = -1;
353 event->ptr = ptr;
354 event->arg = arg;
355 event->parameter = parameter;
69049cc8 356 cs->ev_tail = next;
6fd5ea63
HL
357 }
358
359 spin_unlock_irqrestore(&cs->ev_lock, flags);
360
361 return event;
362}
363EXPORT_SYMBOL_GPL(gigaset_add_event);
364
365static void free_strings(struct at_state_t *at_state)
366{
367 int i;
368
369 for (i = 0; i < STR_NUM; ++i) {
370 kfree(at_state->str_var[i]);
371 at_state->str_var[i] = NULL;
372 }
373}
374
375static void clear_at_state(struct at_state_t *at_state)
376{
377 free_strings(at_state);
378}
379
380static void dealloc_at_states(struct cardstate *cs)
381{
382 struct at_state_t *cur, *next;
383
384 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
385 list_del(&cur->list);
386 free_strings(cur);
387 kfree(cur);
388 }
389}
390
391static void gigaset_freebcs(struct bc_state *bcs)
392{
393 int i;
394
784d5858 395 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
6fd5ea63 396 if (!bcs->cs->ops->freebcshw(bcs)) {
784d5858 397 gig_dbg(DEBUG_INIT, "failed");
6fd5ea63
HL
398 }
399
784d5858 400 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
6fd5ea63 401 clear_at_state(&bcs->at_state);
784d5858 402 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
2032e2c2
TS
403 dev_kfree_skb(bcs->skb);
404 bcs->skb = NULL;
6fd5ea63 405
6fd5ea63
HL
406 for (i = 0; i < AT_NUM; ++i) {
407 kfree(bcs->commands[i]);
408 bcs->commands[i] = NULL;
409 }
410}
411
70440cf2
TS
412static struct cardstate *alloc_cs(struct gigaset_driver *drv)
413{
414 unsigned long flags;
415 unsigned i;
e468c048 416 struct cardstate *cs;
e702ff0b 417 struct cardstate *ret = NULL;
70440cf2
TS
418
419 spin_lock_irqsave(&drv->lock, flags);
e468c048
TS
420 if (drv->blocked)
421 goto exit;
70440cf2 422 for (i = 0; i < drv->minors; ++i) {
e468c048
TS
423 cs = drv->cs + i;
424 if (!(cs->flags & VALID_MINOR)) {
425 cs->flags = VALID_MINOR;
426 ret = cs;
70440cf2 427 break;
e702ff0b 428 }
70440cf2 429 }
e468c048 430exit:
70440cf2
TS
431 spin_unlock_irqrestore(&drv->lock, flags);
432 return ret;
433}
434
435static void free_cs(struct cardstate *cs)
436{
e468c048 437 cs->flags = 0;
70440cf2
TS
438}
439
440static void make_valid(struct cardstate *cs, unsigned mask)
441{
442 unsigned long flags;
443 struct gigaset_driver *drv = cs->driver;
444 spin_lock_irqsave(&drv->lock, flags);
e468c048 445 cs->flags |= mask;
70440cf2
TS
446 spin_unlock_irqrestore(&drv->lock, flags);
447}
448
449static void make_invalid(struct cardstate *cs, unsigned mask)
450{
451 unsigned long flags;
452 struct gigaset_driver *drv = cs->driver;
453 spin_lock_irqsave(&drv->lock, flags);
e468c048 454 cs->flags &= ~mask;
70440cf2
TS
455 spin_unlock_irqrestore(&drv->lock, flags);
456}
457
1cec9727
TS
458/**
459 * gigaset_freecs() - free all associated ressources of a device
460 * @cs: device descriptor structure.
461 *
462 * Stops all tasklets and timers, unregisters the device from all
463 * subsystems it was registered to, deallocates the device structure
464 * @cs and all structures referenced from it.
465 * Operations on the device should be stopped before calling this.
466 */
6fd5ea63
HL
467void gigaset_freecs(struct cardstate *cs)
468{
469 int i;
470 unsigned long flags;
471
472 if (!cs)
473 return;
474
abfd1dc7 475 mutex_lock(&cs->mutex);
6fd5ea63
HL
476
477 if (!cs->bcs)
478 goto f_cs;
479 if (!cs->inbuf)
480 goto f_bcs;
481
482 spin_lock_irqsave(&cs->lock, flags);
69049cc8 483 cs->running = 0;
917f5085
TS
484 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
485 not rescheduled below */
6fd5ea63
HL
486
487 tasklet_kill(&cs->event_tasklet);
488 del_timer_sync(&cs->timer);
489
490 switch (cs->cs_init) {
491 default:
088ec0cc
TS
492 /* clear B channel structures */
493 for (i = 0; i < cs->channels; ++i) {
494 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
495 gigaset_freebcs(cs->bcs + i);
496 }
497
3dda4e37
HL
498 /* clear device sysfs */
499 gigaset_free_dev_sysfs(cs);
500
6fd5ea63
HL
501 gigaset_if_free(cs);
502
784d5858 503 gig_dbg(DEBUG_INIT, "clearing hw");
6fd5ea63
HL
504 cs->ops->freecshw(cs);
505
506 //FIXME cmdbuf
507
508 /* fall through */
509 case 2: /* error in initcshw */
510 /* Deregister from LL */
511 make_invalid(cs, VALID_ID);
088ec0cc 512 gigaset_isdn_unregister(cs);
6fd5ea63
HL
513
514 /* fall through */
088ec0cc 515 case 1: /* error when registering to LL */
784d5858 516 gig_dbg(DEBUG_INIT, "clearing at_state");
6fd5ea63
HL
517 clear_at_state(&cs->at_state);
518 dealloc_at_states(cs);
519
520 /* fall through */
088ec0cc 521 case 0: /* error in basic setup */
6fd5ea63 522 clear_events(cs);
784d5858 523 gig_dbg(DEBUG_INIT, "freeing inbuf");
6fd5ea63
HL
524 kfree(cs->inbuf);
525 }
784d5858 526f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
6fd5ea63 527 kfree(cs->bcs);
784d5858 528f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
abfd1dc7 529 mutex_unlock(&cs->mutex);
6fd5ea63
HL
530 free_cs(cs);
531}
532EXPORT_SYMBOL_GPL(gigaset_freecs);
533
534void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
784d5858 535 struct cardstate *cs, int cid)
6fd5ea63
HL
536{
537 int i;
538
539 INIT_LIST_HEAD(&at_state->list);
540 at_state->waiting = 0;
541 at_state->getstring = 0;
542 at_state->pending_commands = 0;
543 at_state->timer_expires = 0;
544 at_state->timer_active = 0;
69049cc8
TS
545 at_state->timer_index = 0;
546 at_state->seq_index = 0;
6fd5ea63
HL
547 at_state->ConState = 0;
548 for (i = 0; i < STR_NUM; ++i)
549 at_state->str_var[i] = NULL;
550 at_state->int_var[VAR_ZDLE] = 0;
551 at_state->int_var[VAR_ZCTP] = -1;
552 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
553 at_state->cs = cs;
554 at_state->bcs = bcs;
555 at_state->cid = cid;
556 if (!cid)
557 at_state->replystruct = cs->tabnocid;
558 else
559 at_state->replystruct = cs->tabcid;
560}
561
562
2032e2c2 563static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
6fd5ea63
HL
564/* inbuf->read must be allocated before! */
565{
9d4bee2b
TS
566 inbuf->head = 0;
567 inbuf->tail = 0;
6fd5ea63 568 inbuf->cs = cs;
2032e2c2 569 inbuf->inputstate = INS_command;
6fd5ea63
HL
570}
571
1cec9727
TS
572/**
573 * gigaset_fill_inbuf() - append received data to input buffer
574 * @inbuf: buffer structure.
575 * @src: received data.
576 * @numbytes: number of bytes received.
577 */
714e8236
TS
578int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
579 unsigned numbytes)
580{
581 unsigned n, head, tail, bytesleft;
582
583 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
584
585 if (!numbytes)
586 return 0;
587
588 bytesleft = numbytes;
9d4bee2b
TS
589 tail = inbuf->tail;
590 head = inbuf->head;
714e8236
TS
591 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
592
593 while (bytesleft) {
594 if (head > tail)
595 n = head - 1 - tail;
596 else if (head == 0)
597 n = (RBUFSIZE-1) - tail;
598 else
599 n = RBUFSIZE - tail;
600 if (!n) {
601 dev_err(inbuf->cs->dev,
898eb71c
JP
602 "buffer overflow (%u bytes lost)\n",
603 bytesleft);
714e8236
TS
604 break;
605 }
606 if (n > bytesleft)
607 n = bytesleft;
608 memcpy(inbuf->data + tail, src, n);
609 bytesleft -= n;
610 tail = (tail + n) % RBUFSIZE;
611 src += n;
612 }
613 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
9d4bee2b 614 inbuf->tail = tail;
714e8236
TS
615 return numbytes != bytesleft;
616}
617EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
618
6fd5ea63
HL
619/* Initialize the b-channel structure */
620static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
784d5858 621 struct cardstate *cs, int channel)
6fd5ea63
HL
622{
623 int i;
624
625 bcs->tx_skb = NULL; //FIXME -> hw part
626
627 skb_queue_head_init(&bcs->squeue);
628
629 bcs->corrupted = 0;
630 bcs->trans_down = 0;
631 bcs->trans_up = 0;
632
784d5858 633 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
6fd5ea63
HL
634 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
635
6fd5ea63
HL
636#ifdef CONFIG_GIGASET_DEBUG
637 bcs->emptycount = 0;
638#endif
639
784d5858 640 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
6fd5ea63
HL
641 bcs->fcs = PPP_INITFCS;
642 bcs->inputstate = 0;
643 if (cs->ignoreframes) {
6fd5ea63 644 bcs->skb = NULL;
088ec0cc
TS
645 } else {
646 bcs->skb = dev_alloc_skb(SBUFSIZE + cs->hw_hdr_len);
647 if (bcs->skb != NULL)
648 skb_reserve(bcs->skb, cs->hw_hdr_len);
2032e2c2 649 else
088ec0cc 650 pr_err("out of memory\n");
6fd5ea63
HL
651 }
652
653 bcs->channel = channel;
654 bcs->cs = cs;
655
656 bcs->chstate = 0;
657 bcs->use_count = 1;
658 bcs->busy = 0;
659 bcs->ignore = cs->ignoreframes;
660
661 for (i = 0; i < AT_NUM; ++i)
662 bcs->commands[i] = NULL;
663
784d5858 664 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
6fd5ea63
HL
665 if (cs->ops->initbcshw(bcs))
666 return bcs;
667
784d5858 668 gig_dbg(DEBUG_INIT, " failed");
6fd5ea63 669
784d5858 670 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
2032e2c2
TS
671 dev_kfree_skb(bcs->skb);
672 bcs->skb = NULL;
6fd5ea63
HL
673
674 return NULL;
675}
676
1cec9727
TS
677/**
678 * gigaset_initcs() - initialize device structure
679 * @drv: hardware driver the device belongs to
680 * @channels: number of B channels supported by device
681 * @onechannel: !=0 if B channel data and AT commands share one
682 * communication channel (M10x),
683 * ==0 if B channels have separate communication channels (base)
684 * @ignoreframes: number of frames to ignore after setting up B channel
685 * @cidmode: !=0: start in CallID mode
686 * @modulename: name of driver module for LL registration
687 *
6fd5ea63
HL
688 * Allocate and initialize cardstate structure for Gigaset driver
689 * Calls hardware dependent gigaset_initcshw() function
690 * Calls B channel initialization function gigaset_initbcs() for each B channel
1cec9727
TS
691 *
692 * Return value:
6fd5ea63
HL
693 * pointer to cardstate structure
694 */
695struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
696 int onechannel, int ignoreframes,
697 int cidmode, const char *modulename)
698{
699 struct cardstate *cs = NULL;
69049cc8 700 unsigned long flags;
6fd5ea63
HL
701 int i;
702
784d5858 703 gig_dbg(DEBUG_INIT, "allocating cs");
e702ff0b 704 if (!(cs = alloc_cs(drv))) {
c8770dca 705 pr_err("maximum number of devices exceeded\n");
e702ff0b
TS
706 return NULL;
707 }
e702ff0b 708
784d5858 709 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
6fd5ea63 710 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
e702ff0b 711 if (!cs->bcs) {
c8770dca 712 pr_err("out of memory\n");
6fd5ea63 713 goto error;
e702ff0b 714 }
784d5858 715 gig_dbg(DEBUG_INIT, "allocating inbuf");
6fd5ea63 716 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
e702ff0b 717 if (!cs->inbuf) {
c8770dca 718 pr_err("out of memory\n");
6fd5ea63 719 goto error;
e702ff0b 720 }
6fd5ea63
HL
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);
69049cc8 727 cs->running = 0;
6fd5ea63
HL
728 init_timer(&cs->timer); /* clear next & prev */
729 spin_lock_init(&cs->ev_lock);
69049cc8
TS
730 cs->ev_tail = 0;
731 cs->ev_head = 0;
abfd1dc7 732
917f5085
TS
733 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
734 (unsigned long) cs);
9d4bee2b 735 cs->commands_pending = 0;
6fd5ea63
HL
736 cs->cur_at_seq = 0;
737 cs->gotfwver = -1;
738 cs->open_count = 0;
784d5858 739 cs->dev = NULL;
6fd5ea63 740 cs->tty = NULL;
01107d34 741 cs->tty_dev = NULL;
69049cc8 742 cs->cidmode = cidmode != 0;
528efc6a
TS
743 cs->tabnocid = gigaset_tab_nocid;
744 cs->tabcid = gigaset_tab_cid;
6fd5ea63
HL
745
746 init_waitqueue_head(&cs->waitqueue);
747 cs->waiting = 0;
748
9d4bee2b
TS
749 cs->mode = M_UNKNOWN;
750 cs->mstate = MS_UNINITIALIZED;
6fd5ea63 751
6fd5ea63
HL
752 ++cs->cs_init;
753
784d5858 754 gig_dbg(DEBUG_INIT, "setting up at_state");
6fd5ea63
HL
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
784d5858 760 gig_dbg(DEBUG_INIT, "setting up inbuf");
2032e2c2 761 gigaset_inbuf_init(cs->inbuf, cs);
6fd5ea63 762
69049cc8
TS
763 cs->connected = 0;
764 cs->isdn_up = 0;
6fd5ea63 765
784d5858 766 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
6fd5ea63
HL
767 cs->cmdbuf = cs->lastcmdbuf = NULL;
768 spin_lock_init(&cs->cmdlock);
769 cs->curlen = 0;
770 cs->cmdbytes = 0;
771
784d5858 772 gig_dbg(DEBUG_INIT, "setting up iif");
088ec0cc 773 if (!gigaset_isdn_register(cs, modulename)) {
c8770dca 774 pr_err("error registering ISDN device\n");
6fd5ea63
HL
775 goto error;
776 }
777
778 make_valid(cs, VALID_ID);
779 ++cs->cs_init;
784d5858 780 gig_dbg(DEBUG_INIT, "setting up hw");
c8770dca 781 if (!cs->ops->initcshw(cs))
6fd5ea63
HL
782 goto error;
783
784 ++cs->cs_init;
785
7435f50e 786 /* set up character device */
6fd5ea63
HL
787 gigaset_if_init(cs);
788
3dda4e37
HL
789 /* set up device sysfs */
790 gigaset_init_dev_sysfs(cs);
791
088ec0cc
TS
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
69049cc8
TS
801 spin_lock_irqsave(&cs->lock, flags);
802 cs->running = 1;
803 spin_unlock_irqrestore(&cs->lock, flags);
ec81b5e6
TS
804 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
805 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
6fd5ea63
HL
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
784d5858 810 gig_dbg(DEBUG_INIT, "cs initialized");
6fd5ea63
HL
811 return cs;
812
e702ff0b 813error:
784d5858 814 gig_dbg(DEBUG_INIT, "failed");
6fd5ea63
HL
815 gigaset_freecs(cs);
816 return NULL;
817}
818EXPORT_SYMBOL_GPL(gigaset_initcs);
819
73a88814 820/* ReInitialize the b-channel structure on hangup */
6fd5ea63
HL
821void 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
917f5085 830 spin_lock_irqsave(&cs->lock, flags);
6fd5ea63
HL
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;
784d5858 835 bcs->at_state.cid = -1; /* No CID defined */
6fd5ea63
HL
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;
2032e2c2
TS
848 if (bcs->ignore) {
849 dev_kfree_skb(bcs->skb);
850 bcs->skb = NULL;
851 }
6fd5ea63
HL
852
853 cs->ops->reinitbcshw(bcs);
854}
855
856static 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
9d4bee2b
TS
864 cs->mode = M_UNKNOWN;
865 cs->mstate = MS_UNINITIALIZED;
6fd5ea63
HL
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
6fd5ea63 872 cs->inbuf->inputstate = INS_command;
9d4bee2b
TS
873 cs->inbuf->head = 0;
874 cs->inbuf->tail = 0;
6fd5ea63
HL
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;
9d4bee2b 888 cs->commands_pending = 0;
6fd5ea63
HL
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))
c8770dca 896 pr_err("could not allocate channel %d data\n", i);
6fd5ea63
HL
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
1cec9727
TS
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 */
6fd5ea63
HL
918int gigaset_start(struct cardstate *cs)
919{
69049cc8
TS
920 unsigned long flags;
921
abfd1dc7 922 if (mutex_lock_interruptible(&cs->mutex))
6fd5ea63 923 return 0;
6fd5ea63 924
69049cc8
TS
925 spin_lock_irqsave(&cs->lock, flags);
926 cs->connected = 1;
927 spin_unlock_irqrestore(&cs->lock, flags);
6fd5ea63 928
9d4bee2b 929 if (cs->mstate != MS_LOCKED) {
6fd5ea63
HL
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 } else {
935 //FIXME use some saved values?
936 }
937
938 cs->waiting = 1;
939
940 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
941 cs->waiting = 0;
942 //FIXME what should we do?
943 goto error;
944 }
945
784d5858 946 gig_dbg(DEBUG_CMD, "scheduling START");
6fd5ea63
HL
947 gigaset_schedule_event(cs);
948
949 wait_event(cs->waitqueue, !cs->waiting);
950
abfd1dc7 951 mutex_unlock(&cs->mutex);
6fd5ea63
HL
952 return 1;
953
954error:
abfd1dc7 955 mutex_unlock(&cs->mutex);
6fd5ea63
HL
956 return 0;
957}
958EXPORT_SYMBOL_GPL(gigaset_start);
959
1cec9727
TS
960/**
961 * gigaset_shutdown() - shut down device operations
962 * @cs: device descriptor structure.
963 *
964 * Deactivates the device by scheduling an EV_SHUTDOWN event and
965 * waiting for completion of the shutdown.
966 *
967 * Return value:
968 * 0 - success, -1 - error (no device associated)
e468c048
TS
969 */
970int gigaset_shutdown(struct cardstate *cs)
6fd5ea63 971{
abfd1dc7 972 mutex_lock(&cs->mutex);
6fd5ea63 973
5d49c101
TS
974 if (!(cs->flags & VALID_MINOR)) {
975 mutex_unlock(&cs->mutex);
e468c048 976 return -1;
5d49c101 977 }
e468c048 978
6fd5ea63
HL
979 cs->waiting = 1;
980
981 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
982 //FIXME what should we do?
983 goto exit;
984 }
985
784d5858 986 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
6fd5ea63
HL
987 gigaset_schedule_event(cs);
988
2869b23e 989 wait_event(cs->waitqueue, !cs->waiting);
6fd5ea63
HL
990
991 cleanup_cs(cs);
992
993exit:
abfd1dc7 994 mutex_unlock(&cs->mutex);
e468c048 995 return 0;
6fd5ea63
HL
996}
997EXPORT_SYMBOL_GPL(gigaset_shutdown);
998
1cec9727
TS
999/**
1000 * gigaset_stop() - stop device operations
1001 * @cs: device descriptor structure.
1002 *
1003 * Stops operations on the device by scheduling an EV_STOP event and
1004 * waiting for completion of the shutdown.
1005 */
6fd5ea63
HL
1006void gigaset_stop(struct cardstate *cs)
1007{
abfd1dc7 1008 mutex_lock(&cs->mutex);
6fd5ea63 1009
6fd5ea63
HL
1010 cs->waiting = 1;
1011
1012 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
1013 //FIXME what should we do?
1014 goto exit;
1015 }
1016
784d5858 1017 gig_dbg(DEBUG_CMD, "scheduling STOP");
6fd5ea63
HL
1018 gigaset_schedule_event(cs);
1019
2869b23e 1020 wait_event(cs->waitqueue, !cs->waiting);
6fd5ea63 1021
6fd5ea63
HL
1022 cleanup_cs(cs);
1023
1024exit:
abfd1dc7 1025 mutex_unlock(&cs->mutex);
6fd5ea63
HL
1026}
1027EXPORT_SYMBOL_GPL(gigaset_stop);
1028
1029static LIST_HEAD(drivers);
34af946a 1030static DEFINE_SPINLOCK(driver_lock);
6fd5ea63
HL
1031
1032struct cardstate *gigaset_get_cs_by_id(int id)
1033{
1034 unsigned long flags;
35dc8457
TS
1035 struct cardstate *ret = NULL;
1036 struct cardstate *cs;
6fd5ea63
HL
1037 struct gigaset_driver *drv;
1038 unsigned i;
1039
1040 spin_lock_irqsave(&driver_lock, flags);
1041 list_for_each_entry(drv, &drivers, list) {
1042 spin_lock(&drv->lock);
1043 for (i = 0; i < drv->minors; ++i) {
e468c048
TS
1044 cs = drv->cs + i;
1045 if ((cs->flags & VALID_ID) && cs->myid == id) {
1046 ret = cs;
6fd5ea63 1047 break;
e468c048 1048 }
6fd5ea63
HL
1049 }
1050 spin_unlock(&drv->lock);
1051 if (ret)
1052 break;
1053 }
1054 spin_unlock_irqrestore(&driver_lock, flags);
1055 return ret;
1056}
1057
1058void gigaset_debugdrivers(void)
1059{
1060 unsigned long flags;
1061 static struct cardstate *cs;
1062 struct gigaset_driver *drv;
1063 unsigned i;
1064
1065 spin_lock_irqsave(&driver_lock, flags);
1066 list_for_each_entry(drv, &drivers, list) {
784d5858 1067 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
6fd5ea63
HL
1068 spin_lock(&drv->lock);
1069 for (i = 0; i < drv->minors; ++i) {
784d5858 1070 gig_dbg(DEBUG_DRIVER, " index %u", i);
6fd5ea63 1071 cs = drv->cs + i;
784d5858 1072 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
e468c048 1073 gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
784d5858
TS
1074 gig_dbg(DEBUG_DRIVER, " minor_index %u",
1075 cs->minor_index);
1076 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
1077 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
6fd5ea63
HL
1078 }
1079 spin_unlock(&drv->lock);
1080 }
1081 spin_unlock_irqrestore(&driver_lock, flags);
1082}
6fd5ea63 1083
8ca445df 1084static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
6fd5ea63
HL
1085{
1086 unsigned long flags;
35dc8457 1087 struct cardstate *ret = NULL;
6fd5ea63
HL
1088 struct gigaset_driver *drv;
1089 unsigned index;
1090
1091 spin_lock_irqsave(&driver_lock, flags);
1092 list_for_each_entry(drv, &drivers, list) {
1093 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1094 continue;
1095 index = minor - drv->minor;
1096 spin_lock(&drv->lock);
e468c048 1097 if (drv->cs[index].flags & VALID_MINOR)
6fd5ea63
HL
1098 ret = drv->cs + index;
1099 spin_unlock(&drv->lock);
1100 if (ret)
1101 break;
1102 }
1103 spin_unlock_irqrestore(&driver_lock, flags);
1104 return ret;
1105}
1106
8ca445df
AB
1107struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1108{
1109 if (tty->index < 0 || tty->index >= tty->driver->num)
1110 return NULL;
1111 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1112}
1113
1cec9727
TS
1114/**
1115 * gigaset_freedriver() - free all associated ressources of a driver
1116 * @drv: driver descriptor structure.
1117 *
1118 * Unregisters the driver from the system and deallocates the driver
1119 * structure @drv and all structures referenced from it.
1120 * All devices should be shut down before calling this.
1121 */
6fd5ea63
HL
1122void gigaset_freedriver(struct gigaset_driver *drv)
1123{
1124 unsigned long flags;
1125
1126 spin_lock_irqsave(&driver_lock, flags);
1127 list_del(&drv->list);
1128 spin_unlock_irqrestore(&driver_lock, flags);
1129
1130 gigaset_if_freedriver(drv);
6fd5ea63
HL
1131
1132 kfree(drv->cs);
6fd5ea63
HL
1133 kfree(drv);
1134}
1135EXPORT_SYMBOL_GPL(gigaset_freedriver);
1136
1cec9727
TS
1137/**
1138 * gigaset_initdriver() - initialize driver structure
1139 * @minor: First minor number
1140 * @minors: Number of minors this driver can handle
1141 * @procname: Name of the driver
1142 * @devname: Name of the device files (prefix without minor number)
1143 *
6fd5ea63 1144 * Allocate and initialize gigaset_driver structure. Initialize interface.
1cec9727
TS
1145 *
1146 * Return value:
784d5858 1147 * Pointer to the gigaset_driver structure on success, NULL on failure.
6fd5ea63
HL
1148 */
1149struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
784d5858
TS
1150 const char *procname,
1151 const char *devname,
784d5858
TS
1152 const struct gigaset_ops *ops,
1153 struct module *owner)
6fd5ea63
HL
1154{
1155 struct gigaset_driver *drv;
1156 unsigned long flags;
1157 unsigned i;
1158
1159 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1160 if (!drv)
1161 return NULL;
f4675c70 1162
6fd5ea63
HL
1163 drv->have_tty = 0;
1164 drv->minor = minor;
1165 drv->minors = minors;
1166 spin_lock_init(&drv->lock);
1167 drv->blocked = 0;
1168 drv->ops = ops;
1169 drv->owner = owner;
1170 INIT_LIST_HEAD(&drv->list);
1171
1172 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1173 if (!drv->cs)
e702ff0b 1174 goto error;
f4675c70 1175
6fd5ea63 1176 for (i = 0; i < minors; ++i) {
e468c048 1177 drv->cs[i].flags = 0;
6fd5ea63
HL
1178 drv->cs[i].driver = drv;
1179 drv->cs[i].ops = drv->ops;
1180 drv->cs[i].minor_index = i;
5d49c101 1181 mutex_init(&drv->cs[i].mutex);
6fd5ea63
HL
1182 }
1183
f4eaa370 1184 gigaset_if_initdriver(drv, procname, devname);
6fd5ea63
HL
1185
1186 spin_lock_irqsave(&driver_lock, flags);
1187 list_add(&drv->list, &drivers);
1188 spin_unlock_irqrestore(&driver_lock, flags);
1189
1190 return drv;
1191
e702ff0b 1192error:
6fd5ea63 1193 kfree(drv->cs);
6fd5ea63 1194 kfree(drv);
6fd5ea63
HL
1195 return NULL;
1196}
1197EXPORT_SYMBOL_GPL(gigaset_initdriver);
1198
1cec9727
TS
1199/**
1200 * gigaset_blockdriver() - block driver
1201 * @drv: driver descriptor structure.
1202 *
1203 * Prevents the driver from attaching new devices, in preparation for
1204 * deregistration.
1205 */
6fd5ea63
HL
1206void gigaset_blockdriver(struct gigaset_driver *drv)
1207{
6fd5ea63 1208 drv->blocked = 1;
6fd5ea63
HL
1209}
1210EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1211
1212static int __init gigaset_init_module(void)
1213{
1214 /* in accordance with the principle of least astonishment,
1215 * setting the 'debug' parameter to 1 activates a sensible
1216 * set of default debug levels
1217 */
1218 if (gigaset_debuglevel == 1)
1219 gigaset_debuglevel = DEBUG_DEFAULT;
1220
2038724c 1221 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
6fd5ea63
HL
1222 return 0;
1223}
1224
1225static void __exit gigaset_exit_module(void)
1226{
1227}
1228
1229module_init(gigaset_init_module);
1230module_exit(gigaset_exit_module);
1231
1232MODULE_AUTHOR(DRIVER_AUTHOR);
1233MODULE_DESCRIPTION(DRIVER_DESC);
1234
1235MODULE_LICENSE("GPL");
This page took 0.454297 seconds and 5 git commands to generate.