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