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