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