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