blob: 1163d316f58c1941299a9da2c4cdd6f4b05ef4f4 [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)) {
222 mod_timer(&cs->timer, jiffies + GIG_TICK);
223 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);
688 cs->timer.data = (unsigned long) cs;
689 cs->timer.function = timer_tick;
690 cs->timer.expires = jiffies + GIG_TICK;
691 /* FIXME: can jiffies increase too much until the timer is added?
692 * Same problem(?) with mod_timer() in timer_tick(). */
693 add_timer(&cs->timer);
694
695 dbg(DEBUG_INIT, "cs initialized!");
696 up(&cs->sem);
697 return cs;
698
699error: if (cs)
700 up(&cs->sem);
701 dbg(DEBUG_INIT, "failed");
702 gigaset_freecs(cs);
703 return NULL;
704}
705EXPORT_SYMBOL_GPL(gigaset_initcs);
706
Tilman Schmidt917f5082006-04-10 22:55:00 -0700707/* ReInitialize the b-channel structure */
708/* e.g. called on hangup, disconnect */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800709void gigaset_bcs_reinit(struct bc_state *bcs)
710{
711 struct sk_buff *skb;
712 struct cardstate *cs = bcs->cs;
713 unsigned long flags;
714
715 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
716 dev_kfree_skb(skb);
717
Tilman Schmidt917f5082006-04-10 22:55:00 -0700718 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800719 clear_at_state(&bcs->at_state);
720 bcs->at_state.ConState = 0;
721 bcs->at_state.timer_active = 0;
722 bcs->at_state.timer_expires = 0;
723 bcs->at_state.cid = -1; /* No CID defined */
724 spin_unlock_irqrestore(&cs->lock, flags);
725
726 bcs->inputstate = 0;
727
728#ifdef CONFIG_GIGASET_DEBUG
729 bcs->emptycount = 0;
730#endif
731
732 bcs->fcs = PPP_INITFCS;
733 bcs->chstate = 0;
734
735 bcs->ignore = cs->ignoreframes;
736 if (bcs->ignore)
737 bcs->inputstate |= INS_skip_frame;
738
739
740 cs->ops->reinitbcshw(bcs);
741}
742
743static void cleanup_cs(struct cardstate *cs)
744{
745 struct cmdbuf_t *cb, *tcb;
746 int i;
747 unsigned long flags;
748
749 spin_lock_irqsave(&cs->lock, flags);
750
751 atomic_set(&cs->mode, M_UNKNOWN);
752 atomic_set(&cs->mstate, MS_UNINITIALIZED);
753
754 clear_at_state(&cs->at_state);
755 dealloc_at_states(cs);
756 free_strings(&cs->at_state);
757 gigaset_at_init(&cs->at_state, NULL, cs, 0);
758
759 kfree(cs->inbuf->rcvbuf);
760 cs->inbuf->rcvbuf = NULL;
761 cs->inbuf->inputstate = INS_command;
762 atomic_set(&cs->inbuf->head, 0);
763 atomic_set(&cs->inbuf->tail, 0);
764
765 cb = cs->cmdbuf;
766 while (cb) {
767 tcb = cb;
768 cb = cb->next;
769 kfree(tcb);
770 }
771 cs->cmdbuf = cs->lastcmdbuf = NULL;
772 cs->curlen = 0;
773 cs->cmdbytes = 0;
774 cs->gotfwver = -1;
775 cs->dle = 0;
776 cs->cur_at_seq = 0;
777 atomic_set(&cs->commands_pending, 0);
778 cs->cbytes = 0;
779
780 spin_unlock_irqrestore(&cs->lock, flags);
781
782 for (i = 0; i < cs->channels; ++i) {
783 gigaset_freebcs(cs->bcs + i);
784 if (!gigaset_initbcs(cs->bcs + i, cs, i))
785 break; //FIXME error handling
786 }
787
788 if (cs->waiting) {
789 cs->cmd_result = -ENODEV;
790 cs->waiting = 0;
791 wake_up_interruptible(&cs->waitqueue);
792 }
793}
794
795
796int gigaset_start(struct cardstate *cs)
797{
798 if (down_interruptible(&cs->sem))
799 return 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800800
801 atomic_set(&cs->connected, 1);
802
803 if (atomic_read(&cs->mstate) != MS_LOCKED) {
804 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
805 cs->ops->baud_rate(cs, B115200);
806 cs->ops->set_line_ctrl(cs, CS8);
807 cs->control_state = TIOCM_DTR|TIOCM_RTS;
808 } else {
809 //FIXME use some saved values?
810 }
811
812 cs->waiting = 1;
813
814 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
815 cs->waiting = 0;
816 //FIXME what should we do?
817 goto error;
818 }
819
820 dbg(DEBUG_CMD, "scheduling START");
821 gigaset_schedule_event(cs);
822
823 wait_event(cs->waitqueue, !cs->waiting);
824
825 up(&cs->sem);
826 return 1;
827
828error:
829 up(&cs->sem);
830 return 0;
831}
832EXPORT_SYMBOL_GPL(gigaset_start);
833
834void gigaset_shutdown(struct cardstate *cs)
835{
836 down(&cs->sem);
837
838 cs->waiting = 1;
839
840 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
841 //FIXME what should we do?
842 goto exit;
843 }
844
845 dbg(DEBUG_CMD, "scheduling SHUTDOWN");
846 gigaset_schedule_event(cs);
847
848 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
849 warn("aborted");
850 //FIXME
851 }
852
853 if (atomic_read(&cs->mstate) != MS_LOCKED) {
854 //FIXME?
855 //gigaset_baud_rate(cs, B115200);
856 //gigaset_set_line_ctrl(cs, CS8);
857 //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
858 //cs->control_state = 0;
859 } else {
860 //FIXME use some saved values?
861 }
862
863 cleanup_cs(cs);
864
865exit:
866 up(&cs->sem);
867}
868EXPORT_SYMBOL_GPL(gigaset_shutdown);
869
870void gigaset_stop(struct cardstate *cs)
871{
872 down(&cs->sem);
873
874 atomic_set(&cs->connected, 0);
875
876 cs->waiting = 1;
877
878 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
879 //FIXME what should we do?
880 goto exit;
881 }
882
883 dbg(DEBUG_CMD, "scheduling STOP");
884 gigaset_schedule_event(cs);
885
886 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
887 warn("aborted");
888 //FIXME
889 }
890
891 /* Tell the LL that the device is not available .. */
892 gigaset_i4l_cmd(cs, ISDN_STAT_STOP); // FIXME move to event layer?
893
894 cleanup_cs(cs);
895
896exit:
897 up(&cs->sem);
898}
899EXPORT_SYMBOL_GPL(gigaset_stop);
900
901static LIST_HEAD(drivers);
902static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
903
904struct cardstate *gigaset_get_cs_by_id(int id)
905{
906 unsigned long flags;
907 static struct cardstate *ret = NULL;
908 static struct cardstate *cs;
909 struct gigaset_driver *drv;
910 unsigned i;
911
912 spin_lock_irqsave(&driver_lock, flags);
913 list_for_each_entry(drv, &drivers, list) {
914 spin_lock(&drv->lock);
915 for (i = 0; i < drv->minors; ++i) {
916 if (drv->flags[i] & VALID_ID) {
917 cs = drv->cs + i;
918 if (cs->myid == id)
919 ret = cs;
920 }
921 if (ret)
922 break;
923 }
924 spin_unlock(&drv->lock);
925 if (ret)
926 break;
927 }
928 spin_unlock_irqrestore(&driver_lock, flags);
929 return ret;
930}
931
932void gigaset_debugdrivers(void)
933{
934 unsigned long flags;
935 static struct cardstate *cs;
936 struct gigaset_driver *drv;
937 unsigned i;
938
939 spin_lock_irqsave(&driver_lock, flags);
940 list_for_each_entry(drv, &drivers, list) {
941 dbg(DEBUG_DRIVER, "driver %p", drv);
942 spin_lock(&drv->lock);
943 for (i = 0; i < drv->minors; ++i) {
944 dbg(DEBUG_DRIVER, " index %u", i);
945 dbg(DEBUG_DRIVER, " flags 0x%02x", drv->flags[i]);
946 cs = drv->cs + i;
947 dbg(DEBUG_DRIVER, " cardstate %p", cs);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700948 dbg(DEBUG_DRIVER, " minor_index %u",
949 cs->minor_index);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800950 dbg(DEBUG_DRIVER, " driver %p", cs->driver);
951 dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
952 }
953 spin_unlock(&drv->lock);
954 }
955 spin_unlock_irqrestore(&driver_lock, flags);
956}
957EXPORT_SYMBOL_GPL(gigaset_debugdrivers);
958
959struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
960{
961 if (tty->index < 0 || tty->index >= tty->driver->num)
962 return NULL;
963 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
964}
965
966struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
967{
968 unsigned long flags;
969 static struct cardstate *ret = NULL;
970 struct gigaset_driver *drv;
971 unsigned index;
972
973 spin_lock_irqsave(&driver_lock, flags);
974 list_for_each_entry(drv, &drivers, list) {
975 if (minor < drv->minor || minor >= drv->minor + drv->minors)
976 continue;
977 index = minor - drv->minor;
978 spin_lock(&drv->lock);
979 if (drv->flags[index] & VALID_MINOR)
980 ret = drv->cs + index;
981 spin_unlock(&drv->lock);
982 if (ret)
983 break;
984 }
985 spin_unlock_irqrestore(&driver_lock, flags);
986 return ret;
987}
988
989void gigaset_freedriver(struct gigaset_driver *drv)
990{
991 unsigned long flags;
992
993 spin_lock_irqsave(&driver_lock, flags);
994 list_del(&drv->list);
995 spin_unlock_irqrestore(&driver_lock, flags);
996
997 gigaset_if_freedriver(drv);
998 module_put(drv->owner);
999
1000 kfree(drv->cs);
1001 kfree(drv->flags);
1002 kfree(drv);
1003}
1004EXPORT_SYMBOL_GPL(gigaset_freedriver);
1005
1006/* gigaset_initdriver
1007 * Allocate and initialize gigaset_driver structure. Initialize interface.
1008 * parameters:
1009 * minor First minor number
1010 * minors Number of minors this driver can handle
Tilman Schmidt917f5082006-04-10 22:55:00 -07001011 * procname Name of the driver
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001012 * devname Name of the device files (prefix without minor number)
1013 * devfsname Devfs name of the device files without %d
1014 * return value:
1015 * Pointer to the gigaset_driver structure on success, NULL on failure.
1016 */
1017struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1018 const char *procname,
1019 const char *devname,
1020 const char *devfsname,
1021 const struct gigaset_ops *ops,
1022 struct module *owner)
1023{
1024 struct gigaset_driver *drv;
1025 unsigned long flags;
1026 unsigned i;
1027
1028 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1029 if (!drv)
1030 return NULL;
1031 if (!try_module_get(owner))
1032 return NULL;
1033
1034 drv->cs = NULL;
1035 drv->have_tty = 0;
1036 drv->minor = minor;
1037 drv->minors = minors;
1038 spin_lock_init(&drv->lock);
1039 drv->blocked = 0;
1040 drv->ops = ops;
1041 drv->owner = owner;
1042 INIT_LIST_HEAD(&drv->list);
1043
1044 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1045 if (!drv->cs)
1046 goto out1;
1047 drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1048 if (!drv->flags)
1049 goto out2;
1050
1051 for (i = 0; i < minors; ++i) {
1052 drv->flags[i] = 0;
1053 drv->cs[i].driver = drv;
1054 drv->cs[i].ops = drv->ops;
1055 drv->cs[i].minor_index = i;
1056 }
1057
1058 gigaset_if_initdriver(drv, procname, devname, devfsname);
1059
1060 spin_lock_irqsave(&driver_lock, flags);
1061 list_add(&drv->list, &drivers);
1062 spin_unlock_irqrestore(&driver_lock, flags);
1063
1064 return drv;
1065
1066out2:
1067 kfree(drv->cs);
1068out1:
1069 kfree(drv);
1070 module_put(owner);
1071 return NULL;
1072}
1073EXPORT_SYMBOL_GPL(gigaset_initdriver);
1074
1075static struct cardstate *alloc_cs(struct gigaset_driver *drv)
1076{
1077 unsigned long flags;
1078 unsigned i;
1079 static struct cardstate *ret = NULL;
1080
1081 spin_lock_irqsave(&drv->lock, flags);
1082 for (i = 0; i < drv->minors; ++i) {
1083 if (!(drv->flags[i] & VALID_MINOR)) {
1084 drv->flags[i] = VALID_MINOR;
1085 ret = drv->cs + i;
1086 }
1087 if (ret)
1088 break;
1089 }
1090 spin_unlock_irqrestore(&drv->lock, flags);
1091 return ret;
1092}
1093
1094static void free_cs(struct cardstate *cs)
1095{
1096 unsigned long flags;
1097 struct gigaset_driver *drv = cs->driver;
1098 spin_lock_irqsave(&drv->lock, flags);
1099 drv->flags[cs->minor_index] = 0;
1100 spin_unlock_irqrestore(&drv->lock, flags);
1101}
1102
1103static void make_valid(struct cardstate *cs, unsigned mask)
1104{
1105 unsigned long flags;
1106 struct gigaset_driver *drv = cs->driver;
1107 spin_lock_irqsave(&drv->lock, flags);
1108 drv->flags[cs->minor_index] |= mask;
1109 spin_unlock_irqrestore(&drv->lock, flags);
1110}
1111
1112static void make_invalid(struct cardstate *cs, unsigned mask)
1113{
1114 unsigned long flags;
1115 struct gigaset_driver *drv = cs->driver;
1116 spin_lock_irqsave(&drv->lock, flags);
1117 drv->flags[cs->minor_index] &= ~mask;
1118 spin_unlock_irqrestore(&drv->lock, flags);
1119}
1120
1121/* For drivers without fixed assignment device<->cardstate (usb) */
1122struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1123{
1124 unsigned long flags;
1125 struct cardstate *cs = NULL;
1126 unsigned i;
1127
1128 spin_lock_irqsave(&drv->lock, flags);
1129 if (drv->blocked)
1130 goto exit;
1131 for (i = 0; i < drv->minors; ++i) {
1132 if ((drv->flags[i] & VALID_MINOR) &&
1133 !(drv->flags[i] & ASSIGNED)) {
1134 drv->flags[i] |= ASSIGNED;
1135 cs = drv->cs + i;
1136 break;
1137 }
1138 }
1139exit:
1140 spin_unlock_irqrestore(&drv->lock, flags);
1141 return cs;
1142}
1143EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1144
1145void gigaset_unassign(struct cardstate *cs)
1146{
1147 unsigned long flags;
1148 unsigned *minor_flags;
1149 struct gigaset_driver *drv;
1150
1151 if (!cs)
1152 return;
1153 drv = cs->driver;
1154 spin_lock_irqsave(&drv->lock, flags);
1155 minor_flags = drv->flags + cs->minor_index;
1156 if (*minor_flags & VALID_MINOR)
1157 *minor_flags &= ~ASSIGNED;
1158 spin_unlock_irqrestore(&drv->lock, flags);
1159}
1160EXPORT_SYMBOL_GPL(gigaset_unassign);
1161
1162void gigaset_blockdriver(struct gigaset_driver *drv)
1163{
1164 unsigned long flags;
1165 spin_lock_irqsave(&drv->lock, flags);
1166 drv->blocked = 1;
1167 spin_unlock_irqrestore(&drv->lock, flags);
1168}
1169EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1170
1171static int __init gigaset_init_module(void)
1172{
1173 /* in accordance with the principle of least astonishment,
1174 * setting the 'debug' parameter to 1 activates a sensible
1175 * set of default debug levels
1176 */
1177 if (gigaset_debuglevel == 1)
1178 gigaset_debuglevel = DEBUG_DEFAULT;
1179
1180 info(DRIVER_AUTHOR);
1181 info(DRIVER_DESC);
1182 return 0;
1183}
1184
1185static void __exit gigaset_exit_module(void)
1186{
1187}
1188
1189module_init(gigaset_init_module);
1190module_exit(gigaset_exit_module);
1191
1192MODULE_AUTHOR(DRIVER_AUTHOR);
1193MODULE_DESCRIPTION(DRIVER_DESC);
1194
1195MODULE_LICENSE("GPL");