]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - drivers/isdn/gigaset/common.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-focal-kernel.git] / drivers / isdn / gigaset / common.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Stuff used by all variants of the driver
4 *
5 * Copyright (c) 2001 by Stefan Eilers,
6 * Hansjoerg Lipp <hjlipp@web.de>,
7 * Tilman Schmidt <tilman@imap.cc>.
8 *
9 * =====================================================================
10 * =====================================================================
11 */
12
13 #include "gigaset.h"
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16
17 /* Version Information */
18 #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
19 #define DRIVER_DESC "Driver for Gigaset 307x"
20
21 #ifdef CONFIG_GIGASET_DEBUG
22 #define DRIVER_DESC_DEBUG " (debug build)"
23 #else
24 #define DRIVER_DESC_DEBUG ""
25 #endif
26
27 /* Module parameters */
28 int gigaset_debuglevel;
29 EXPORT_SYMBOL_GPL(gigaset_debuglevel);
30 module_param_named(debug, gigaset_debuglevel, int, S_IRUGO | S_IWUSR);
31 MODULE_PARM_DESC(debug, "debug level");
32
33 /* driver state flags */
34 #define VALID_MINOR 0x01
35 #define VALID_ID 0x02
36
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 */
48 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
49 size_t len, const unsigned char *buf)
50 {
51 unsigned char outbuf[80];
52 unsigned char c;
53 size_t space = sizeof outbuf - 1;
54 unsigned char *out = outbuf;
55 size_t numin = len;
56
57 while (numin--) {
58 c = *buf++;
59 if (c == '~' || c == '^' || c == '\\') {
60 if (!space--)
61 break;
62 *out++ = '\\';
63 }
64 if (c & 0x80) {
65 if (!space--)
66 break;
67 *out++ = '~';
68 c ^= 0x80;
69 }
70 if (c < 0x20 || c == 0x7f) {
71 if (!space--)
72 break;
73 *out++ = '^';
74 c ^= 0x40;
75 }
76 if (!space--)
77 break;
78 *out++ = c;
79 }
80 *out = 0;
81
82 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
83 }
84 EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
85
86 static 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
103 int gigaset_enterconfigmode(struct cardstate *cs)
104 {
105 int i, r;
106
107 cs->control_state = TIOCM_RTS;
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 }
123 r = setflags(cs, TIOCM_RTS | TIOCM_DTR, 800);
124 if (r < 0)
125 goto error;
126
127 return 0;
128
129 error:
130 dev_err(cs->dev, "error %d on setuartbits\n", -r);
131 cs->control_state = TIOCM_RTS | TIOCM_DTR;
132 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS | TIOCM_DTR);
133
134 return -1;
135 }
136
137 static 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) {
143 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
144 at_state, at_state->timer_expires);
145 return 0;
146 }
147
148 gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
149 at_state->timer_index, NULL);
150 return 1;
151 }
152
153 static void timer_tick(struct timer_list *t)
154 {
155 struct cardstate *cs = from_timer(cs, t, timer);
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
174 if (cs->running) {
175 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
176 if (timeout) {
177 gig_dbg(DEBUG_EVENT, "scheduling timeout");
178 tasklet_schedule(&cs->event_tasklet);
179 }
180 }
181
182 spin_unlock_irqrestore(&cs->lock, flags);
183 }
184
185 int gigaset_get_channel(struct bc_state *bcs)
186 {
187 unsigned long flags;
188
189 spin_lock_irqsave(&bcs->cs->lock, flags);
190 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
191 gig_dbg(DEBUG_CHANNEL, "could not allocate channel %d",
192 bcs->channel);
193 spin_unlock_irqrestore(&bcs->cs->lock, flags);
194 return -EBUSY;
195 }
196 ++bcs->use_count;
197 bcs->busy = 1;
198 gig_dbg(DEBUG_CHANNEL, "allocated channel %d", bcs->channel);
199 spin_unlock_irqrestore(&bcs->cs->lock, flags);
200 return 0;
201 }
202
203 struct 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)) {
210 gig_dbg(DEBUG_CHANNEL,
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);
220 gig_dbg(DEBUG_CHANNEL, "allocated channel %d", i);
221 return cs->bcs + i;
222 }
223 module_put(cs->driver->owner);
224 spin_unlock_irqrestore(&cs->lock, flags);
225 gig_dbg(DEBUG_CHANNEL, "no free channel");
226 return NULL;
227 }
228
229 void 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) {
235 gig_dbg(DEBUG_CHANNEL, "could not free channel %d",
236 bcs->channel);
237 spin_unlock_irqrestore(&bcs->cs->lock, flags);
238 return;
239 }
240 --bcs->use_count;
241 bcs->busy = 0;
242 module_put(bcs->cs->driver->owner);
243 gig_dbg(DEBUG_CHANNEL, "freed channel %d", bcs->channel);
244 spin_unlock_irqrestore(&bcs->cs->lock, flags);
245 }
246
247 int 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);
256 gig_dbg(DEBUG_CHANNEL,
257 "could not allocate all channels");
258 return -EBUSY;
259 }
260 for (i = 0; i < cs->channels; ++i)
261 ++cs->bcs[i].use_count;
262 spin_unlock_irqrestore(&cs->lock, flags);
263
264 gig_dbg(DEBUG_CHANNEL, "allocated all channels");
265
266 return 0;
267 }
268
269 void gigaset_free_channels(struct cardstate *cs)
270 {
271 unsigned long flags;
272 int i;
273
274 gig_dbg(DEBUG_CHANNEL, "unblocking all channels");
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
281 void gigaset_block_channels(struct cardstate *cs)
282 {
283 unsigned long flags;
284 int i;
285
286 gig_dbg(DEBUG_CHANNEL, "blocking all channels");
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
293 static void clear_events(struct cardstate *cs)
294 {
295 struct event_t *ev;
296 unsigned head, tail;
297 unsigned long flags;
298
299 spin_lock_irqsave(&cs->ev_lock, flags);
300
301 head = cs->ev_head;
302 tail = cs->ev_tail;
303
304 while (tail != head) {
305 ev = cs->events + head;
306 kfree(ev->ptr);
307 head = (head + 1) % MAX_EVENTS;
308 }
309
310 cs->ev_head = tail;
311
312 spin_unlock_irqrestore(&cs->ev_lock, flags);
313 }
314
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 */
329 struct event_t *gigaset_add_event(struct cardstate *cs,
330 struct at_state_t *at_state, int type,
331 void *ptr, int parameter, void *arg)
332 {
333 unsigned long flags;
334 unsigned next, tail;
335 struct event_t *event = NULL;
336
337 gig_dbg(DEBUG_EVENT, "queueing event %d", type);
338
339 spin_lock_irqsave(&cs->ev_lock, flags);
340
341 tail = cs->ev_tail;
342 next = (tail + 1) % MAX_EVENTS;
343 if (unlikely(next == cs->ev_head))
344 dev_err(cs->dev, "event queue full\n");
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;
353 cs->ev_tail = next;
354 }
355
356 spin_unlock_irqrestore(&cs->ev_lock, flags);
357
358 return event;
359 }
360 EXPORT_SYMBOL_GPL(gigaset_add_event);
361
362 static void clear_at_state(struct at_state_t *at_state)
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
372 static void dealloc_temp_at_states(struct cardstate *cs)
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);
378 clear_at_state(cur);
379 kfree(cur);
380 }
381 }
382
383 static void gigaset_freebcs(struct bc_state *bcs)
384 {
385 int i;
386
387 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
388 bcs->cs->ops->freebcshw(bcs);
389
390 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
391 clear_at_state(&bcs->at_state);
392 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
393 dev_kfree_skb(bcs->rx_skb);
394 bcs->rx_skb = NULL;
395
396 for (i = 0; i < AT_NUM; ++i) {
397 kfree(bcs->commands[i]);
398 bcs->commands[i] = NULL;
399 }
400 }
401
402 static struct cardstate *alloc_cs(struct gigaset_driver *drv)
403 {
404 unsigned long flags;
405 unsigned i;
406 struct cardstate *cs;
407 struct cardstate *ret = NULL;
408
409 spin_lock_irqsave(&drv->lock, flags);
410 if (drv->blocked)
411 goto exit;
412 for (i = 0; i < drv->minors; ++i) {
413 cs = drv->cs + i;
414 if (!(cs->flags & VALID_MINOR)) {
415 cs->flags = VALID_MINOR;
416 ret = cs;
417 break;
418 }
419 }
420 exit:
421 spin_unlock_irqrestore(&drv->lock, flags);
422 return ret;
423 }
424
425 static void free_cs(struct cardstate *cs)
426 {
427 cs->flags = 0;
428 }
429
430 static 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);
435 cs->flags |= mask;
436 spin_unlock_irqrestore(&drv->lock, flags);
437 }
438
439 static 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);
444 cs->flags &= ~mask;
445 spin_unlock_irqrestore(&drv->lock, flags);
446 }
447
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 */
457 void gigaset_freecs(struct cardstate *cs)
458 {
459 int i;
460 unsigned long flags;
461
462 if (!cs)
463 return;
464
465 mutex_lock(&cs->mutex);
466
467 spin_lock_irqsave(&cs->lock, flags);
468 cs->running = 0;
469 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
470 not rescheduled below */
471
472 tasklet_kill(&cs->event_tasklet);
473 del_timer_sync(&cs->timer);
474
475 switch (cs->cs_init) {
476 default:
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
483 /* clear device sysfs */
484 gigaset_free_dev_sysfs(cs);
485
486 gigaset_if_free(cs);
487
488 gig_dbg(DEBUG_INIT, "clearing hw");
489 cs->ops->freecshw(cs);
490
491 /* fall through */
492 case 2: /* error in initcshw */
493 /* Deregister from LL */
494 make_invalid(cs, VALID_ID);
495 gigaset_isdn_unregdev(cs);
496
497 /* fall through */
498 case 1: /* error when registering to LL */
499 gig_dbg(DEBUG_INIT, "clearing at_state");
500 clear_at_state(&cs->at_state);
501 dealloc_temp_at_states(cs);
502 clear_events(cs);
503 tty_port_destroy(&cs->port);
504
505 /* fall through */
506 case 0: /* error in basic setup */
507 gig_dbg(DEBUG_INIT, "freeing inbuf");
508 kfree(cs->inbuf);
509 kfree(cs->bcs);
510 }
511
512 mutex_unlock(&cs->mutex);
513 free_cs(cs);
514 }
515 EXPORT_SYMBOL_GPL(gigaset_freecs);
516
517 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
518 struct cardstate *cs, int cid)
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;
528 at_state->timer_index = 0;
529 at_state->seq_index = 0;
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
546 static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
547 /* inbuf->read must be allocated before! */
548 {
549 inbuf->head = 0;
550 inbuf->tail = 0;
551 inbuf->cs = cs;
552 inbuf->inputstate = INS_command;
553 }
554
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.
560 *
561 * Return value: !=0 if some data was appended
562 */
563 int 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;
574 tail = inbuf->tail;
575 head = inbuf->head;
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)
582 n = (RBUFSIZE - 1) - tail;
583 else
584 n = RBUFSIZE - tail;
585 if (!n) {
586 dev_err(inbuf->cs->dev,
587 "buffer overflow (%u bytes lost)\n",
588 bytesleft);
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);
599 inbuf->tail = tail;
600 return numbytes != bytesleft;
601 }
602 EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
603
604 /* Initialize the b-channel structure */
605 static int gigaset_initbcs(struct bc_state *bcs, struct cardstate *cs,
606 int channel)
607 {
608 int i;
609
610 bcs->tx_skb = NULL;
611
612 skb_queue_head_init(&bcs->squeue);
613
614 bcs->corrupted = 0;
615 bcs->trans_down = 0;
616 bcs->trans_up = 0;
617
618 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
619 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
620
621 #ifdef CONFIG_GIGASET_DEBUG
622 bcs->emptycount = 0;
623 #endif
624
625 bcs->rx_bufsize = 0;
626 bcs->rx_skb = NULL;
627 bcs->rx_fcs = PPP_INITFCS;
628 bcs->inputstate = 0;
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
640 spin_lock_init(&bcs->aplock);
641 bcs->ap = NULL;
642 bcs->apconnstate = 0;
643
644 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
645 return cs->ops->initbcshw(bcs);
646 }
647
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 *
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
662 *
663 * Return value:
664 * pointer to cardstate structure
665 */
666 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
667 int onechannel, int ignoreframes,
668 int cidmode, const char *modulename)
669 {
670 struct cardstate *cs;
671 unsigned long flags;
672 int i;
673
674 gig_dbg(DEBUG_INIT, "allocating cs");
675 cs = alloc_cs(drv);
676 if (!cs) {
677 pr_err("maximum number of devices exceeded\n");
678 return NULL;
679 }
680
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);
686 cs->running = 0;
687 timer_setup(&cs->timer, timer_tick, 0);
688 spin_lock_init(&cs->ev_lock);
689 cs->ev_tail = 0;
690 cs->ev_head = 0;
691
692 tasklet_init(&cs->event_tasklet, gigaset_handle_event,
693 (unsigned long) cs);
694 tty_port_init(&cs->port);
695 cs->commands_pending = 0;
696 cs->cur_at_seq = 0;
697 cs->gotfwver = -1;
698 cs->dev = NULL;
699 cs->tty_dev = NULL;
700 cs->cidmode = cidmode != 0;
701 cs->tabnocid = gigaset_tab_nocid;
702 cs->tabcid = gigaset_tab_cid;
703
704 init_waitqueue_head(&cs->waitqueue);
705 cs->waiting = 0;
706
707 cs->mode = M_UNKNOWN;
708 cs->mstate = MS_UNINITIALIZED;
709
710 cs->bcs = kmalloc_array(channels, sizeof(struct bc_state), GFP_KERNEL);
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 }
716 ++cs->cs_init;
717
718 gig_dbg(DEBUG_INIT, "setting up at_state");
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
724 gig_dbg(DEBUG_INIT, "setting up inbuf");
725 gigaset_inbuf_init(cs->inbuf, cs);
726
727 cs->connected = 0;
728 cs->isdn_up = 0;
729
730 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
731 cs->cmdbuf = cs->lastcmdbuf = NULL;
732 spin_lock_init(&cs->cmdlock);
733 cs->curlen = 0;
734 cs->cmdbytes = 0;
735
736 gig_dbg(DEBUG_INIT, "setting up iif");
737 if (gigaset_isdn_regdev(cs, modulename) < 0) {
738 pr_err("error registering ISDN device\n");
739 goto error;
740 }
741
742 make_valid(cs, VALID_ID);
743 ++cs->cs_init;
744 gig_dbg(DEBUG_INIT, "setting up hw");
745 if (cs->ops->initcshw(cs) < 0)
746 goto error;
747
748 ++cs->cs_init;
749
750 /* set up character device */
751 gigaset_if_init(cs);
752
753 /* set up device sysfs */
754 gigaset_init_dev_sysfs(cs);
755
756 /* set up channel data structures */
757 for (i = 0; i < channels; ++i) {
758 gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
759 if (gigaset_initbcs(cs->bcs + i, cs, i) < 0) {
760 pr_err("could not allocate channel %d data\n", i);
761 goto error;
762 }
763 }
764
765 spin_lock_irqsave(&cs->lock, flags);
766 cs->running = 1;
767 spin_unlock_irqrestore(&cs->lock, flags);
768 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
769 add_timer(&cs->timer);
770
771 gig_dbg(DEBUG_INIT, "cs initialized");
772 return cs;
773
774 error:
775 gig_dbg(DEBUG_INIT, "failed");
776 gigaset_freecs(cs);
777 return NULL;
778 }
779 EXPORT_SYMBOL_GPL(gigaset_initcs);
780
781 /* ReInitialize the b-channel structure on hangup */
782 void 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
791 spin_lock_irqsave(&cs->lock, flags);
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;
796 bcs->at_state.cid = -1; /* No CID defined */
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
805 bcs->rx_fcs = PPP_INITFCS;
806 bcs->chstate = 0;
807
808 bcs->ignore = cs->ignoreframes;
809 dev_kfree_skb(bcs->rx_skb);
810 bcs->rx_skb = NULL;
811
812 cs->ops->reinitbcshw(bcs);
813 }
814
815 static 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
823 cs->mode = M_UNKNOWN;
824 cs->mstate = MS_UNINITIALIZED;
825
826 clear_at_state(&cs->at_state);
827 dealloc_temp_at_states(cs);
828 gigaset_at_init(&cs->at_state, NULL, cs, 0);
829
830 cs->inbuf->inputstate = INS_command;
831 cs->inbuf->head = 0;
832 cs->inbuf->tail = 0;
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;
846 cs->commands_pending = 0;
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);
853 if (gigaset_initbcs(cs->bcs + i, cs, i) < 0)
854 pr_err("could not allocate channel %d data\n", i);
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
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:
874 * 0 on success, error code < 0 on failure
875 */
876 int gigaset_start(struct cardstate *cs)
877 {
878 unsigned long flags;
879
880 if (mutex_lock_interruptible(&cs->mutex))
881 return -EBUSY;
882
883 spin_lock_irqsave(&cs->lock, flags);
884 cs->connected = 1;
885 spin_unlock_irqrestore(&cs->lock, flags);
886
887 if (cs->mstate != MS_LOCKED) {
888 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS);
889 cs->ops->baud_rate(cs, B115200);
890 cs->ops->set_line_ctrl(cs, CS8);
891 cs->control_state = TIOCM_DTR | TIOCM_RTS;
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;
898 goto error;
899 }
900 gigaset_schedule_event(cs);
901
902 wait_event(cs->waitqueue, !cs->waiting);
903
904 mutex_unlock(&cs->mutex);
905 return 0;
906
907 error:
908 mutex_unlock(&cs->mutex);
909 return -ENOMEM;
910 }
911 EXPORT_SYMBOL_GPL(gigaset_start);
912
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:
921 * 0 - success, -ENODEV - error (no device associated)
922 */
923 int gigaset_shutdown(struct cardstate *cs)
924 {
925 mutex_lock(&cs->mutex);
926
927 if (!(cs->flags & VALID_MINOR)) {
928 mutex_unlock(&cs->mutex);
929 return -ENODEV;
930 }
931
932 cs->waiting = 1;
933
934 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL))
935 goto exit;
936 gigaset_schedule_event(cs);
937
938 wait_event(cs->waitqueue, !cs->waiting);
939
940 cleanup_cs(cs);
941
942 exit:
943 mutex_unlock(&cs->mutex);
944 return 0;
945 }
946 EXPORT_SYMBOL_GPL(gigaset_shutdown);
947
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 */
955 void gigaset_stop(struct cardstate *cs)
956 {
957 mutex_lock(&cs->mutex);
958
959 cs->waiting = 1;
960
961 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL))
962 goto exit;
963 gigaset_schedule_event(cs);
964
965 wait_event(cs->waitqueue, !cs->waiting);
966
967 cleanup_cs(cs);
968
969 exit:
970 mutex_unlock(&cs->mutex);
971 }
972 EXPORT_SYMBOL_GPL(gigaset_stop);
973
974 static LIST_HEAD(drivers);
975 static DEFINE_SPINLOCK(driver_lock);
976
977 struct cardstate *gigaset_get_cs_by_id(int id)
978 {
979 unsigned long flags;
980 struct cardstate *ret = NULL;
981 struct cardstate *cs;
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) {
989 cs = drv->cs + i;
990 if ((cs->flags & VALID_ID) && cs->myid == id) {
991 ret = cs;
992 break;
993 }
994 }
995 spin_unlock(&drv->lock);
996 if (ret)
997 break;
998 }
999 spin_unlock_irqrestore(&driver_lock, flags);
1000 return ret;
1001 }
1002
1003 static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1004 {
1005 unsigned long flags;
1006 struct cardstate *ret = NULL;
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);
1016 if (drv->cs[index].flags & VALID_MINOR)
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
1026 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1027 {
1028 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1029 }
1030
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 */
1039 void 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);
1048
1049 kfree(drv->cs);
1050 kfree(drv);
1051 }
1052 EXPORT_SYMBOL_GPL(gigaset_freedriver);
1053
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 *
1061 * Allocate and initialize gigaset_driver structure. Initialize interface.
1062 *
1063 * Return value:
1064 * Pointer to the gigaset_driver structure on success, NULL on failure.
1065 */
1066 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1067 const char *procname,
1068 const char *devname,
1069 const struct gigaset_ops *ops,
1070 struct module *owner)
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;
1079
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
1089 drv->cs = kmalloc_array(minors, sizeof(*drv->cs), GFP_KERNEL);
1090 if (!drv->cs)
1091 goto error;
1092
1093 for (i = 0; i < minors; ++i) {
1094 drv->cs[i].flags = 0;
1095 drv->cs[i].driver = drv;
1096 drv->cs[i].ops = drv->ops;
1097 drv->cs[i].minor_index = i;
1098 mutex_init(&drv->cs[i].mutex);
1099 }
1100
1101 gigaset_if_initdriver(drv, procname, devname);
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
1109 error:
1110 kfree(drv);
1111 return NULL;
1112 }
1113 EXPORT_SYMBOL_GPL(gigaset_initdriver);
1114
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 */
1122 void gigaset_blockdriver(struct gigaset_driver *drv)
1123 {
1124 drv->blocked = 1;
1125 }
1126 EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1127
1128 static 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
1137 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
1138 gigaset_isdn_regdrv();
1139 return 0;
1140 }
1141
1142 static void __exit gigaset_exit_module(void)
1143 {
1144 gigaset_isdn_unregdrv();
1145 }
1146
1147 module_init(gigaset_init_module);
1148 module_exit(gigaset_exit_module);
1149
1150 MODULE_AUTHOR(DRIVER_AUTHOR);
1151 MODULE_DESCRIPTION(DRIVER_DESC);
1152
1153 MODULE_LICENSE("GPL");