blob: a607837b9f6ccdaa99077712919f2724793956bd [file] [log] [blame]
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001/*
2 * Stuff used by all variants of the driver
3 *
4 * Copyright (c) 2001 by Stefan Eilers <Eilers.Stefan@epost.de>,
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 * =====================================================================
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080014 */
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 <Eilers.Stefan@epost.de>"
23#define DRIVER_DESC "Driver for Gigaset 307x"
24
25/* Module parameters */
26int gigaset_debuglevel = DEBUG_DEFAULT;
27EXPORT_SYMBOL_GPL(gigaset_debuglevel);
28module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
29MODULE_PARM_DESC(debug, "debug level");
30
31/*======================================================================
32 Prototypes of internal functions
33 */
34
35//static void gigaset_process_response(int resp_code, int parameter,
36// struct at_state_t *at_state,
37// unsigned char ** pstring);
38static struct cardstate *alloc_cs(struct gigaset_driver *drv);
39static void free_cs(struct cardstate *cs);
40static void make_valid(struct cardstate *cs, unsigned mask);
41static void make_invalid(struct cardstate *cs, unsigned mask);
42
43#define VALID_MINOR 0x01
44#define VALID_ID 0x02
45#define ASSIGNED 0x04
46
47/* bitwise byte inversion table */
48__u8 gigaset_invtab[256] = {
49 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
50 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
51 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
52 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
53 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
54 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
55 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
56 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
57 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
58 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
59 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
60 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
61 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
62 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
63 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
64 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
65 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
66 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
67 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
68 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
69 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
70 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
71 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
72 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
73 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
74 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
75 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
76 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
77 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
78 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
79 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
80 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
81};
82EXPORT_SYMBOL_GPL(gigaset_invtab);
83
84void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
85 size_t len, const unsigned char *buf, int from_user)
86{
87 unsigned char outbuf[80];
88 unsigned char inbuf[80 - 1];
89 size_t numin;
90 const unsigned char *in;
91 size_t space = sizeof outbuf - 1;
92 unsigned char *out = outbuf;
93
94 if (!from_user) {
95 in = buf;
96 numin = len;
97 } else {
98 numin = len < sizeof inbuf ? len : sizeof inbuf;
99 in = inbuf;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700100 if (copy_from_user(inbuf, (const unsigned char __user *) buf,
101 numin)) {
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800102 strncpy(inbuf, "<FAULT>", sizeof inbuf);
103 numin = sizeof "<FAULT>" - 1;
104 }
105 }
106
107 for (; numin && space; --numin, ++in) {
108 --space;
109 if (*in >= 32)
110 *out++ = *in;
111 else {
112 *out++ = '^';
113 if (space) {
114 *out++ = '@' + *in;
115 --space;
116 }
117 }
118 }
119 *out = 0;
120
121 dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
122}
123EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
124
125static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
126{
127 int r;
128
129 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
130 cs->control_state = flags;
131 if (r < 0)
132 return r;
133
134 if (delay) {
135 set_current_state(TASK_INTERRUPTIBLE);
136 schedule_timeout(delay * HZ / 1000);
137 }
138
139 return 0;
140}
141
142int gigaset_enterconfigmode(struct cardstate *cs)
143{
144 int i, r;
145
146 if (!atomic_read(&cs->connected)) {
147 err("not connected!");
148 return -1;
149 }
150
151 cs->control_state = TIOCM_RTS; //FIXME
152
153 r = setflags(cs, TIOCM_DTR, 200);
154 if (r < 0)
155 goto error;
156 r = setflags(cs, 0, 200);
157 if (r < 0)
158 goto error;
159 for (i = 0; i < 5; ++i) {
160 r = setflags(cs, TIOCM_RTS, 100);
161 if (r < 0)
162 goto error;
163 r = setflags(cs, 0, 100);
164 if (r < 0)
165 goto error;
166 }
167 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
168 if (r < 0)
169 goto error;
170
171 return 0;
172
173error:
174 err("error %d on setuartbits!\n", -r);
175 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
176 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
177
178 return -1; //r
179}
180
181static int test_timeout(struct at_state_t *at_state)
182{
183 if (!at_state->timer_expires)
184 return 0;
185
186 if (--at_state->timer_expires) {
187 dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
188 at_state, at_state->timer_expires);
189 return 0;
190 }
191
192 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
193 atomic_read(&at_state->timer_index), NULL)) {
194 //FIXME what should we do?
195 }
196
197 return 1;
198}
199
200static void timer_tick(unsigned long data)
201{
202 struct cardstate *cs = (struct cardstate *) data;
203 unsigned long flags;
204 unsigned channel;
205 struct at_state_t *at_state;
206 int timeout = 0;
207
208 spin_lock_irqsave(&cs->lock, flags);
209
210 for (channel = 0; channel < cs->channels; ++channel)
211 if (test_timeout(&cs->bcs[channel].at_state))
212 timeout = 1;
213
214 if (test_timeout(&cs->at_state))
215 timeout = 1;
216
217 list_for_each_entry(at_state, &cs->temp_at_states, list)
218 if (test_timeout(at_state))
219 timeout = 1;
220
221 if (atomic_read(&cs->running)) {
Tilman Schmidtec81b5e62006-04-10 22:55:03 -0700222 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800223 if (timeout) {
224 dbg(DEBUG_CMD, "scheduling timeout");
225 tasklet_schedule(&cs->event_tasklet);
226 }
227 }
228
229 spin_unlock_irqrestore(&cs->lock, flags);
230}
231
232int gigaset_get_channel(struct bc_state *bcs)
233{
234 unsigned long flags;
235
236 spin_lock_irqsave(&bcs->cs->lock, flags);
237 if (bcs->use_count) {
238 dbg(DEBUG_ANY, "could not allocate channel %d", bcs->channel);
239 spin_unlock_irqrestore(&bcs->cs->lock, flags);
240 return 0;
241 }
242 ++bcs->use_count;
243 bcs->busy = 1;
244 dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
245 spin_unlock_irqrestore(&bcs->cs->lock, flags);
246 return 1;
247}
248
249void gigaset_free_channel(struct bc_state *bcs)
250{
251 unsigned long flags;
252
253 spin_lock_irqsave(&bcs->cs->lock, flags);
254 if (!bcs->busy) {
255 dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
256 spin_unlock_irqrestore(&bcs->cs->lock, flags);
257 return;
258 }
259 --bcs->use_count;
260 bcs->busy = 0;
261 dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
262 spin_unlock_irqrestore(&bcs->cs->lock, flags);
263}
264
265int gigaset_get_channels(struct cardstate *cs)
266{
267 unsigned long flags;
268 int i;
269
270 spin_lock_irqsave(&cs->lock, flags);
271 for (i = 0; i < cs->channels; ++i)
272 if (cs->bcs[i].use_count) {
273 spin_unlock_irqrestore(&cs->lock, flags);
274 dbg(DEBUG_ANY, "could not allocated all channels");
275 return 0;
276 }
277 for (i = 0; i < cs->channels; ++i)
278 ++cs->bcs[i].use_count;
279 spin_unlock_irqrestore(&cs->lock, flags);
280
281 dbg(DEBUG_ANY, "allocated all channels");
282
283 return 1;
284}
285
286void gigaset_free_channels(struct cardstate *cs)
287{
288 unsigned long flags;
289 int i;
290
291 dbg(DEBUG_ANY, "unblocking all channels");
292 spin_lock_irqsave(&cs->lock, flags);
293 for (i = 0; i < cs->channels; ++i)
294 --cs->bcs[i].use_count;
295 spin_unlock_irqrestore(&cs->lock, flags);
296}
297
298void gigaset_block_channels(struct cardstate *cs)
299{
300 unsigned long flags;
301 int i;
302
303 dbg(DEBUG_ANY, "blocking all channels");
304 spin_lock_irqsave(&cs->lock, flags);
305 for (i = 0; i < cs->channels; ++i)
306 ++cs->bcs[i].use_count;
307 spin_unlock_irqrestore(&cs->lock, flags);
308}
309
310static void clear_events(struct cardstate *cs)
311{
312 struct event_t *ev;
313 unsigned head, tail;
314
315 /* no locking needed (no reader/writer allowed) */
316
317 head = atomic_read(&cs->ev_head);
318 tail = atomic_read(&cs->ev_tail);
319
320 while (tail != head) {
321 ev = cs->events + head;
322 kfree(ev->ptr);
323
324 head = (head + 1) % MAX_EVENTS;
325 }
326
327 atomic_set(&cs->ev_head, tail);
328}
329
330struct event_t *gigaset_add_event(struct cardstate *cs,
331 struct at_state_t *at_state, int type,
332 void *ptr, int parameter, void *arg)
333{
334 unsigned long flags;
335 unsigned next, tail;
336 struct event_t *event = NULL;
337
338 spin_lock_irqsave(&cs->ev_lock, flags);
339
340 tail = atomic_read(&cs->ev_tail);
341 next = (tail + 1) % MAX_EVENTS;
342 if (unlikely(next == atomic_read(&cs->ev_head)))
343 err("event queue full");
344 else {
345 event = cs->events + tail;
346 event->type = type;
347 event->at_state = at_state;
348 event->cid = -1;
349 event->ptr = ptr;
350 event->arg = arg;
351 event->parameter = parameter;
352 atomic_set(&cs->ev_tail, next);
353 }
354
355 spin_unlock_irqrestore(&cs->ev_lock, flags);
356
357 return event;
358}
359EXPORT_SYMBOL_GPL(gigaset_add_event);
360
361static void free_strings(struct at_state_t *at_state)
362{
363 int i;
364
365 for (i = 0; i < STR_NUM; ++i) {
366 kfree(at_state->str_var[i]);
367 at_state->str_var[i] = NULL;
368 }
369}
370
371static void clear_at_state(struct at_state_t *at_state)
372{
373 free_strings(at_state);
374}
375
376static void dealloc_at_states(struct cardstate *cs)
377{
378 struct at_state_t *cur, *next;
379
380 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
381 list_del(&cur->list);
382 free_strings(cur);
383 kfree(cur);
384 }
385}
386
387static void gigaset_freebcs(struct bc_state *bcs)
388{
389 int i;
390
391 dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
392 if (!bcs->cs->ops->freebcshw(bcs)) {
393 dbg(DEBUG_INIT, "failed");
394 }
395
396 dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
397 clear_at_state(&bcs->at_state);
398 dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
399
400 if (bcs->skb)
401 dev_kfree_skb(bcs->skb);
402 for (i = 0; i < AT_NUM; ++i) {
403 kfree(bcs->commands[i]);
404 bcs->commands[i] = NULL;
405 }
406}
407
408void gigaset_freecs(struct cardstate *cs)
409{
410 int i;
411 unsigned long flags;
412
413 if (!cs)
414 return;
415
416 down(&cs->sem);
417
418 if (!cs->bcs)
419 goto f_cs;
420 if (!cs->inbuf)
421 goto f_bcs;
422
423 spin_lock_irqsave(&cs->lock, flags);
424 atomic_set(&cs->running, 0);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700425 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
426 not rescheduled below */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800427
428 tasklet_kill(&cs->event_tasklet);
429 del_timer_sync(&cs->timer);
430
431 switch (cs->cs_init) {
432 default:
433 gigaset_if_free(cs);
434
435 dbg(DEBUG_INIT, "clearing hw");
436 cs->ops->freecshw(cs);
437
438 //FIXME cmdbuf
439
440 /* fall through */
441 case 2: /* error in initcshw */
442 /* Deregister from LL */
443 make_invalid(cs, VALID_ID);
444 dbg(DEBUG_INIT, "clearing iif");
445 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
446
447 /* fall through */
448 case 1: /* error when regestering to LL */
449 dbg(DEBUG_INIT, "clearing at_state");
450 clear_at_state(&cs->at_state);
451 dealloc_at_states(cs);
452
453 /* fall through */
454 case 0: /* error in one call to initbcs */
455 for (i = 0; i < cs->channels; ++i) {
456 dbg(DEBUG_INIT, "clearing bcs[%d]", i);
457 gigaset_freebcs(cs->bcs + i);
458 }
459
460 clear_events(cs);
461 dbg(DEBUG_INIT, "freeing inbuf");
462 kfree(cs->inbuf);
463 }
464f_bcs: dbg(DEBUG_INIT, "freeing bcs[]");
465 kfree(cs->bcs);
466f_cs: dbg(DEBUG_INIT, "freeing cs");
467 up(&cs->sem);
468 free_cs(cs);
469}
470EXPORT_SYMBOL_GPL(gigaset_freecs);
471
472void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
473 struct cardstate *cs, int cid)
474{
475 int i;
476
477 INIT_LIST_HEAD(&at_state->list);
478 at_state->waiting = 0;
479 at_state->getstring = 0;
480 at_state->pending_commands = 0;
481 at_state->timer_expires = 0;
482 at_state->timer_active = 0;
483 atomic_set(&at_state->timer_index, 0);
484 atomic_set(&at_state->seq_index, 0);
485 at_state->ConState = 0;
486 for (i = 0; i < STR_NUM; ++i)
487 at_state->str_var[i] = NULL;
488 at_state->int_var[VAR_ZDLE] = 0;
489 at_state->int_var[VAR_ZCTP] = -1;
490 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
491 at_state->cs = cs;
492 at_state->bcs = bcs;
493 at_state->cid = cid;
494 if (!cid)
495 at_state->replystruct = cs->tabnocid;
496 else
497 at_state->replystruct = cs->tabcid;
498}
499
500
501static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
502 struct cardstate *cs, int inputstate)
503/* inbuf->read must be allocated before! */
504{
505 atomic_set(&inbuf->head, 0);
506 atomic_set(&inbuf->tail, 0);
507 inbuf->cs = cs;
508 inbuf->bcs = bcs; /*base driver: NULL*/
509 inbuf->rcvbuf = NULL; //FIXME
510 inbuf->inputstate = inputstate;
511}
512
513/* Initialize the b-channel structure */
514static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
515 struct cardstate *cs, int channel)
516{
517 int i;
518
519 bcs->tx_skb = NULL; //FIXME -> hw part
520
521 skb_queue_head_init(&bcs->squeue);
522
523 bcs->corrupted = 0;
524 bcs->trans_down = 0;
525 bcs->trans_up = 0;
526
527 dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
528 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
529
530 bcs->rcvbytes = 0;
531
532#ifdef CONFIG_GIGASET_DEBUG
533 bcs->emptycount = 0;
534#endif
535
536 dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
537 bcs->fcs = PPP_INITFCS;
538 bcs->inputstate = 0;
539 if (cs->ignoreframes) {
540 bcs->inputstate |= INS_skip_frame;
541 bcs->skb = NULL;
542 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
543 skb_reserve(bcs->skb, HW_HDR_LEN);
544 else {
545 warn("could not allocate skb");
546 bcs->inputstate |= INS_skip_frame;
547 }
548
549 bcs->channel = channel;
550 bcs->cs = cs;
551
552 bcs->chstate = 0;
553 bcs->use_count = 1;
554 bcs->busy = 0;
555 bcs->ignore = cs->ignoreframes;
556
557 for (i = 0; i < AT_NUM; ++i)
558 bcs->commands[i] = NULL;
559
560 dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
561 if (cs->ops->initbcshw(bcs))
562 return bcs;
563
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800564 dbg(DEBUG_INIT, " failed");
565
566 dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
567 if (bcs->skb)
568 dev_kfree_skb(bcs->skb);
569
570 return NULL;
571}
572
573/* gigaset_initcs
574 * Allocate and initialize cardstate structure for Gigaset driver
575 * Calls hardware dependent gigaset_initcshw() function
576 * Calls B channel initialization function gigaset_initbcs() for each B channel
577 * parameters:
578 * drv hardware driver the device belongs to
579 * channels number of B channels supported by device
Tilman Schmidt917f5082006-04-10 22:55:00 -0700580 * onechannel !=0: B channel data and AT commands share one
581 * communication channel
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800582 * ==0: B channels have separate communication channels
583 * ignoreframes number of frames to ignore after setting up B channel
584 * cidmode !=0: start in CallID mode
585 * modulename name of driver module (used for I4L registration)
586 * return value:
587 * pointer to cardstate structure
588 */
589struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
590 int onechannel, int ignoreframes,
591 int cidmode, const char *modulename)
592{
593 struct cardstate *cs = NULL;
594 int i;
595
596 dbg(DEBUG_INIT, "allocating cs");
597 cs = alloc_cs(drv);
598 if (!cs)
599 goto error;
600 dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
601 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
602 if (!cs->bcs)
603 goto error;
604 dbg(DEBUG_INIT, "allocating inbuf");
605 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
606 if (!cs->inbuf)
607 goto error;
608
609 cs->cs_init = 0;
610 cs->channels = channels;
611 cs->onechannel = onechannel;
612 cs->ignoreframes = ignoreframes;
613 INIT_LIST_HEAD(&cs->temp_at_states);
614 atomic_set(&cs->running, 0);
615 init_timer(&cs->timer); /* clear next & prev */
616 spin_lock_init(&cs->ev_lock);
617 atomic_set(&cs->ev_tail, 0);
618 atomic_set(&cs->ev_head, 0);
619 init_MUTEX_LOCKED(&cs->sem);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700620 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
621 (unsigned long) cs);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800622 atomic_set(&cs->commands_pending, 0);
623 cs->cur_at_seq = 0;
624 cs->gotfwver = -1;
625 cs->open_count = 0;
626 cs->tty = NULL;
627 atomic_set(&cs->cidmode, cidmode != 0);
628
629 //if(onechannel) { //FIXME
630 cs->tabnocid = gigaset_tab_nocid_m10x;
631 cs->tabcid = gigaset_tab_cid_m10x;
632 //} else {
633 // cs->tabnocid = gigaset_tab_nocid;
634 // cs->tabcid = gigaset_tab_cid;
635 //}
636
637 init_waitqueue_head(&cs->waitqueue);
638 cs->waiting = 0;
639
640 atomic_set(&cs->mode, M_UNKNOWN);
641 atomic_set(&cs->mstate, MS_UNINITIALIZED);
642
643 for (i = 0; i < channels; ++i) {
644 dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
645 if (!gigaset_initbcs(cs->bcs + i, cs, i))
646 goto error;
647 }
648
649 ++cs->cs_init;
650
651 dbg(DEBUG_INIT, "setting up at_state");
652 spin_lock_init(&cs->lock);
653 gigaset_at_init(&cs->at_state, NULL, cs, 0);
654 cs->dle = 0;
655 cs->cbytes = 0;
656
657 dbg(DEBUG_INIT, "setting up inbuf");
658 if (onechannel) { //FIXME distinction necessary?
659 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
660 } else
661 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
662
663 atomic_set(&cs->connected, 0);
664
665 dbg(DEBUG_INIT, "setting up cmdbuf");
666 cs->cmdbuf = cs->lastcmdbuf = NULL;
667 spin_lock_init(&cs->cmdlock);
668 cs->curlen = 0;
669 cs->cmdbytes = 0;
670
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800671 dbg(DEBUG_INIT, "setting up iif");
672 if (!gigaset_register_to_LL(cs, modulename)) {
673 err("register_isdn=>error");
674 goto error;
675 }
676
677 make_valid(cs, VALID_ID);
678 ++cs->cs_init;
679 dbg(DEBUG_INIT, "setting up hw");
680 if (!cs->ops->initcshw(cs))
681 goto error;
682
683 ++cs->cs_init;
684
685 gigaset_if_init(cs);
686
687 atomic_set(&cs->running, 1);
Tilman Schmidtec81b5e62006-04-10 22:55:03 -0700688 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
689 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800690 /* FIXME: can jiffies increase too much until the timer is added?
691 * Same problem(?) with mod_timer() in timer_tick(). */
692 add_timer(&cs->timer);
693
694 dbg(DEBUG_INIT, "cs initialized!");
695 up(&cs->sem);
696 return cs;
697
698error: if (cs)
699 up(&cs->sem);
700 dbg(DEBUG_INIT, "failed");
701 gigaset_freecs(cs);
702 return NULL;
703}
704EXPORT_SYMBOL_GPL(gigaset_initcs);
705
Tilman Schmidt917f5082006-04-10 22:55:00 -0700706/* ReInitialize the b-channel structure */
707/* e.g. called on hangup, disconnect */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800708void gigaset_bcs_reinit(struct bc_state *bcs)
709{
710 struct sk_buff *skb;
711 struct cardstate *cs = bcs->cs;
712 unsigned long flags;
713
714 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
715 dev_kfree_skb(skb);
716
Tilman Schmidt917f5082006-04-10 22:55:00 -0700717 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800718 clear_at_state(&bcs->at_state);
719 bcs->at_state.ConState = 0;
720 bcs->at_state.timer_active = 0;
721 bcs->at_state.timer_expires = 0;
722 bcs->at_state.cid = -1; /* No CID defined */
723 spin_unlock_irqrestore(&cs->lock, flags);
724
725 bcs->inputstate = 0;
726
727#ifdef CONFIG_GIGASET_DEBUG
728 bcs->emptycount = 0;
729#endif
730
731 bcs->fcs = PPP_INITFCS;
732 bcs->chstate = 0;
733
734 bcs->ignore = cs->ignoreframes;
735 if (bcs->ignore)
736 bcs->inputstate |= INS_skip_frame;
737
738
739 cs->ops->reinitbcshw(bcs);
740}
741
742static void cleanup_cs(struct cardstate *cs)
743{
744 struct cmdbuf_t *cb, *tcb;
745 int i;
746 unsigned long flags;
747
748 spin_lock_irqsave(&cs->lock, flags);
749
750 atomic_set(&cs->mode, M_UNKNOWN);
751 atomic_set(&cs->mstate, MS_UNINITIALIZED);
752
753 clear_at_state(&cs->at_state);
754 dealloc_at_states(cs);
755 free_strings(&cs->at_state);
756 gigaset_at_init(&cs->at_state, NULL, cs, 0);
757
758 kfree(cs->inbuf->rcvbuf);
759 cs->inbuf->rcvbuf = NULL;
760 cs->inbuf->inputstate = INS_command;
761 atomic_set(&cs->inbuf->head, 0);
762 atomic_set(&cs->inbuf->tail, 0);
763
764 cb = cs->cmdbuf;
765 while (cb) {
766 tcb = cb;
767 cb = cb->next;
768 kfree(tcb);
769 }
770 cs->cmdbuf = cs->lastcmdbuf = NULL;
771 cs->curlen = 0;
772 cs->cmdbytes = 0;
773 cs->gotfwver = -1;
774 cs->dle = 0;
775 cs->cur_at_seq = 0;
776 atomic_set(&cs->commands_pending, 0);
777 cs->cbytes = 0;
778
779 spin_unlock_irqrestore(&cs->lock, flags);
780
781 for (i = 0; i < cs->channels; ++i) {
782 gigaset_freebcs(cs->bcs + i);
783 if (!gigaset_initbcs(cs->bcs + i, cs, i))
784 break; //FIXME error handling
785 }
786
787 if (cs->waiting) {
788 cs->cmd_result = -ENODEV;
789 cs->waiting = 0;
790 wake_up_interruptible(&cs->waitqueue);
791 }
792}
793
794
795int gigaset_start(struct cardstate *cs)
796{
797 if (down_interruptible(&cs->sem))
798 return 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800799
800 atomic_set(&cs->connected, 1);
801
802 if (atomic_read(&cs->mstate) != MS_LOCKED) {
803 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
804 cs->ops->baud_rate(cs, B115200);
805 cs->ops->set_line_ctrl(cs, CS8);
806 cs->control_state = TIOCM_DTR|TIOCM_RTS;
807 } else {
808 //FIXME use some saved values?
809 }
810
811 cs->waiting = 1;
812
813 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
814 cs->waiting = 0;
815 //FIXME what should we do?
816 goto error;
817 }
818
819 dbg(DEBUG_CMD, "scheduling START");
820 gigaset_schedule_event(cs);
821
822 wait_event(cs->waitqueue, !cs->waiting);
823
824 up(&cs->sem);
825 return 1;
826
827error:
828 up(&cs->sem);
829 return 0;
830}
831EXPORT_SYMBOL_GPL(gigaset_start);
832
833void gigaset_shutdown(struct cardstate *cs)
834{
835 down(&cs->sem);
836
837 cs->waiting = 1;
838
839 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
840 //FIXME what should we do?
841 goto exit;
842 }
843
844 dbg(DEBUG_CMD, "scheduling SHUTDOWN");
845 gigaset_schedule_event(cs);
846
847 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
848 warn("aborted");
849 //FIXME
850 }
851
852 if (atomic_read(&cs->mstate) != MS_LOCKED) {
853 //FIXME?
854 //gigaset_baud_rate(cs, B115200);
855 //gigaset_set_line_ctrl(cs, CS8);
856 //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
857 //cs->control_state = 0;
858 } else {
859 //FIXME use some saved values?
860 }
861
862 cleanup_cs(cs);
863
864exit:
865 up(&cs->sem);
866}
867EXPORT_SYMBOL_GPL(gigaset_shutdown);
868
869void gigaset_stop(struct cardstate *cs)
870{
871 down(&cs->sem);
872
873 atomic_set(&cs->connected, 0);
874
875 cs->waiting = 1;
876
877 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
878 //FIXME what should we do?
879 goto exit;
880 }
881
882 dbg(DEBUG_CMD, "scheduling STOP");
883 gigaset_schedule_event(cs);
884
885 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
886 warn("aborted");
887 //FIXME
888 }
889
890 /* Tell the LL that the device is not available .. */
891 gigaset_i4l_cmd(cs, ISDN_STAT_STOP); // FIXME move to event layer?
892
893 cleanup_cs(cs);
894
895exit:
896 up(&cs->sem);
897}
898EXPORT_SYMBOL_GPL(gigaset_stop);
899
900static LIST_HEAD(drivers);
901static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
902
903struct cardstate *gigaset_get_cs_by_id(int id)
904{
905 unsigned long flags;
906 static struct cardstate *ret = NULL;
907 static struct cardstate *cs;
908 struct gigaset_driver *drv;
909 unsigned i;
910
911 spin_lock_irqsave(&driver_lock, flags);
912 list_for_each_entry(drv, &drivers, list) {
913 spin_lock(&drv->lock);
914 for (i = 0; i < drv->minors; ++i) {
915 if (drv->flags[i] & VALID_ID) {
916 cs = drv->cs + i;
917 if (cs->myid == id)
918 ret = cs;
919 }
920 if (ret)
921 break;
922 }
923 spin_unlock(&drv->lock);
924 if (ret)
925 break;
926 }
927 spin_unlock_irqrestore(&driver_lock, flags);
928 return ret;
929}
930
931void gigaset_debugdrivers(void)
932{
933 unsigned long flags;
934 static struct cardstate *cs;
935 struct gigaset_driver *drv;
936 unsigned i;
937
938 spin_lock_irqsave(&driver_lock, flags);
939 list_for_each_entry(drv, &drivers, list) {
940 dbg(DEBUG_DRIVER, "driver %p", drv);
941 spin_lock(&drv->lock);
942 for (i = 0; i < drv->minors; ++i) {
943 dbg(DEBUG_DRIVER, " index %u", i);
944 dbg(DEBUG_DRIVER, " flags 0x%02x", drv->flags[i]);
945 cs = drv->cs + i;
946 dbg(DEBUG_DRIVER, " cardstate %p", cs);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700947 dbg(DEBUG_DRIVER, " minor_index %u",
948 cs->minor_index);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800949 dbg(DEBUG_DRIVER, " driver %p", cs->driver);
950 dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
951 }
952 spin_unlock(&drv->lock);
953 }
954 spin_unlock_irqrestore(&driver_lock, flags);
955}
956EXPORT_SYMBOL_GPL(gigaset_debugdrivers);
957
958struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
959{
960 if (tty->index < 0 || tty->index >= tty->driver->num)
961 return NULL;
962 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
963}
964
965struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
966{
967 unsigned long flags;
968 static struct cardstate *ret = NULL;
969 struct gigaset_driver *drv;
970 unsigned index;
971
972 spin_lock_irqsave(&driver_lock, flags);
973 list_for_each_entry(drv, &drivers, list) {
974 if (minor < drv->minor || minor >= drv->minor + drv->minors)
975 continue;
976 index = minor - drv->minor;
977 spin_lock(&drv->lock);
978 if (drv->flags[index] & VALID_MINOR)
979 ret = drv->cs + index;
980 spin_unlock(&drv->lock);
981 if (ret)
982 break;
983 }
984 spin_unlock_irqrestore(&driver_lock, flags);
985 return ret;
986}
987
988void gigaset_freedriver(struct gigaset_driver *drv)
989{
990 unsigned long flags;
991
992 spin_lock_irqsave(&driver_lock, flags);
993 list_del(&drv->list);
994 spin_unlock_irqrestore(&driver_lock, flags);
995
996 gigaset_if_freedriver(drv);
997 module_put(drv->owner);
998
999 kfree(drv->cs);
1000 kfree(drv->flags);
1001 kfree(drv);
1002}
1003EXPORT_SYMBOL_GPL(gigaset_freedriver);
1004
1005/* gigaset_initdriver
1006 * Allocate and initialize gigaset_driver structure. Initialize interface.
1007 * parameters:
1008 * minor First minor number
1009 * minors Number of minors this driver can handle
Tilman Schmidt917f5082006-04-10 22:55:00 -07001010 * procname Name of the driver
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001011 * devname Name of the device files (prefix without minor number)
1012 * devfsname Devfs name of the device files without %d
1013 * return value:
1014 * Pointer to the gigaset_driver structure on success, NULL on failure.
1015 */
1016struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1017 const char *procname,
1018 const char *devname,
1019 const char *devfsname,
1020 const struct gigaset_ops *ops,
1021 struct module *owner)
1022{
1023 struct gigaset_driver *drv;
1024 unsigned long flags;
1025 unsigned i;
1026
1027 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1028 if (!drv)
1029 return NULL;
1030 if (!try_module_get(owner))
1031 return NULL;
1032
1033 drv->cs = NULL;
1034 drv->have_tty = 0;
1035 drv->minor = minor;
1036 drv->minors = minors;
1037 spin_lock_init(&drv->lock);
1038 drv->blocked = 0;
1039 drv->ops = ops;
1040 drv->owner = owner;
1041 INIT_LIST_HEAD(&drv->list);
1042
1043 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1044 if (!drv->cs)
1045 goto out1;
1046 drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1047 if (!drv->flags)
1048 goto out2;
1049
1050 for (i = 0; i < minors; ++i) {
1051 drv->flags[i] = 0;
1052 drv->cs[i].driver = drv;
1053 drv->cs[i].ops = drv->ops;
1054 drv->cs[i].minor_index = i;
1055 }
1056
1057 gigaset_if_initdriver(drv, procname, devname, devfsname);
1058
1059 spin_lock_irqsave(&driver_lock, flags);
1060 list_add(&drv->list, &drivers);
1061 spin_unlock_irqrestore(&driver_lock, flags);
1062
1063 return drv;
1064
1065out2:
1066 kfree(drv->cs);
1067out1:
1068 kfree(drv);
1069 module_put(owner);
1070 return NULL;
1071}
1072EXPORT_SYMBOL_GPL(gigaset_initdriver);
1073
1074static struct cardstate *alloc_cs(struct gigaset_driver *drv)
1075{
1076 unsigned long flags;
1077 unsigned i;
1078 static struct cardstate *ret = NULL;
1079
1080 spin_lock_irqsave(&drv->lock, flags);
1081 for (i = 0; i < drv->minors; ++i) {
1082 if (!(drv->flags[i] & VALID_MINOR)) {
1083 drv->flags[i] = VALID_MINOR;
1084 ret = drv->cs + i;
1085 }
1086 if (ret)
1087 break;
1088 }
1089 spin_unlock_irqrestore(&drv->lock, flags);
1090 return ret;
1091}
1092
1093static void free_cs(struct cardstate *cs)
1094{
1095 unsigned long flags;
1096 struct gigaset_driver *drv = cs->driver;
1097 spin_lock_irqsave(&drv->lock, flags);
1098 drv->flags[cs->minor_index] = 0;
1099 spin_unlock_irqrestore(&drv->lock, flags);
1100}
1101
1102static void make_valid(struct cardstate *cs, unsigned mask)
1103{
1104 unsigned long flags;
1105 struct gigaset_driver *drv = cs->driver;
1106 spin_lock_irqsave(&drv->lock, flags);
1107 drv->flags[cs->minor_index] |= mask;
1108 spin_unlock_irqrestore(&drv->lock, flags);
1109}
1110
1111static void make_invalid(struct cardstate *cs, unsigned mask)
1112{
1113 unsigned long flags;
1114 struct gigaset_driver *drv = cs->driver;
1115 spin_lock_irqsave(&drv->lock, flags);
1116 drv->flags[cs->minor_index] &= ~mask;
1117 spin_unlock_irqrestore(&drv->lock, flags);
1118}
1119
1120/* For drivers without fixed assignment device<->cardstate (usb) */
1121struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1122{
1123 unsigned long flags;
1124 struct cardstate *cs = NULL;
1125 unsigned i;
1126
1127 spin_lock_irqsave(&drv->lock, flags);
1128 if (drv->blocked)
1129 goto exit;
1130 for (i = 0; i < drv->minors; ++i) {
1131 if ((drv->flags[i] & VALID_MINOR) &&
1132 !(drv->flags[i] & ASSIGNED)) {
1133 drv->flags[i] |= ASSIGNED;
1134 cs = drv->cs + i;
1135 break;
1136 }
1137 }
1138exit:
1139 spin_unlock_irqrestore(&drv->lock, flags);
1140 return cs;
1141}
1142EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1143
1144void gigaset_unassign(struct cardstate *cs)
1145{
1146 unsigned long flags;
1147 unsigned *minor_flags;
1148 struct gigaset_driver *drv;
1149
1150 if (!cs)
1151 return;
1152 drv = cs->driver;
1153 spin_lock_irqsave(&drv->lock, flags);
1154 minor_flags = drv->flags + cs->minor_index;
1155 if (*minor_flags & VALID_MINOR)
1156 *minor_flags &= ~ASSIGNED;
1157 spin_unlock_irqrestore(&drv->lock, flags);
1158}
1159EXPORT_SYMBOL_GPL(gigaset_unassign);
1160
1161void gigaset_blockdriver(struct gigaset_driver *drv)
1162{
1163 unsigned long flags;
1164 spin_lock_irqsave(&drv->lock, flags);
1165 drv->blocked = 1;
1166 spin_unlock_irqrestore(&drv->lock, flags);
1167}
1168EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1169
1170static int __init gigaset_init_module(void)
1171{
1172 /* in accordance with the principle of least astonishment,
1173 * setting the 'debug' parameter to 1 activates a sensible
1174 * set of default debug levels
1175 */
1176 if (gigaset_debuglevel == 1)
1177 gigaset_debuglevel = DEBUG_DEFAULT;
1178
1179 info(DRIVER_AUTHOR);
1180 info(DRIVER_DESC);
1181 return 0;
1182}
1183
1184static void __exit gigaset_exit_module(void)
1185{
1186}
1187
1188module_init(gigaset_init_module);
1189module_exit(gigaset_exit_module);
1190
1191MODULE_AUTHOR(DRIVER_AUTHOR);
1192MODULE_DESCRIPTION(DRIVER_DESC);
1193
1194MODULE_LICENSE("GPL");