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