blob: 5155c5b07a064834245500c4d7aeed6f3f33679d [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
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080035static 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
Tilman Schmidt784d5852006-04-10 22:55:04 -070040#define VALID_MINOR 0x01
41#define VALID_ID 0x02
42#define ASSIGNED 0x04
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080043
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,
Tilman Schmidt784d5852006-04-10 22:55:04 -070082 size_t len, const unsigned char *buf, int from_user)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080083{
84 unsigned char outbuf[80];
85 unsigned char inbuf[80 - 1];
Tilman Schmidt784d5852006-04-10 22:55:04 -070086 unsigned char c;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080087 size_t numin;
88 const unsigned char *in;
89 size_t space = sizeof outbuf - 1;
90 unsigned char *out = outbuf;
91
92 if (!from_user) {
93 in = buf;
94 numin = len;
95 } else {
96 numin = len < sizeof inbuf ? len : sizeof inbuf;
97 in = inbuf;
Tilman Schmidt917f5082006-04-10 22:55:00 -070098 if (copy_from_user(inbuf, (const unsigned char __user *) buf,
99 numin)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700100 gig_dbg(level, "%s (%u bytes) - copy_from_user failed",
101 msg, (unsigned) len);
102 return;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800103 }
104 }
105
Tilman Schmidt784d5852006-04-10 22:55:04 -0700106 while (numin-- > 0) {
107 c = *buf++;
108 if (c == '~' || c == '^' || c == '\\') {
109 if (space-- <= 0)
110 break;
111 *out++ = '\\';
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800112 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700113 if (c & 0x80) {
114 if (space-- <= 0)
115 break;
116 *out++ = '~';
117 c ^= 0x80;
118 }
119 if (c < 0x20 || c == 0x7f) {
120 if (space-- <= 0)
121 break;
122 *out++ = '^';
123 c ^= 0x40;
124 }
125 if (space-- <= 0)
126 break;
127 *out++ = c;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800128 }
129 *out = 0;
130
Tilman Schmidt784d5852006-04-10 22:55:04 -0700131 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800132}
133EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
134
135static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
136{
137 int r;
138
139 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
140 cs->control_state = flags;
141 if (r < 0)
142 return r;
143
144 if (delay) {
145 set_current_state(TASK_INTERRUPTIBLE);
146 schedule_timeout(delay * HZ / 1000);
147 }
148
149 return 0;
150}
151
152int gigaset_enterconfigmode(struct cardstate *cs)
153{
154 int i, r;
155
156 if (!atomic_read(&cs->connected)) {
157 err("not connected!");
158 return -1;
159 }
160
161 cs->control_state = TIOCM_RTS; //FIXME
162
163 r = setflags(cs, TIOCM_DTR, 200);
164 if (r < 0)
165 goto error;
166 r = setflags(cs, 0, 200);
167 if (r < 0)
168 goto error;
169 for (i = 0; i < 5; ++i) {
170 r = setflags(cs, TIOCM_RTS, 100);
171 if (r < 0)
172 goto error;
173 r = setflags(cs, 0, 100);
174 if (r < 0)
175 goto error;
176 }
177 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
178 if (r < 0)
179 goto error;
180
181 return 0;
182
183error:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700184 dev_err(cs->dev, "error %d on setuartbits\n", -r);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800185 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
186 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
187
188 return -1; //r
189}
190
191static int test_timeout(struct at_state_t *at_state)
192{
193 if (!at_state->timer_expires)
194 return 0;
195
196 if (--at_state->timer_expires) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700197 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
198 at_state, at_state->timer_expires);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800199 return 0;
200 }
201
202 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700203 atomic_read(&at_state->timer_index), NULL)) {
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800204 //FIXME what should we do?
205 }
206
207 return 1;
208}
209
210static void timer_tick(unsigned long data)
211{
212 struct cardstate *cs = (struct cardstate *) data;
213 unsigned long flags;
214 unsigned channel;
215 struct at_state_t *at_state;
216 int timeout = 0;
217
218 spin_lock_irqsave(&cs->lock, flags);
219
220 for (channel = 0; channel < cs->channels; ++channel)
221 if (test_timeout(&cs->bcs[channel].at_state))
222 timeout = 1;
223
224 if (test_timeout(&cs->at_state))
225 timeout = 1;
226
227 list_for_each_entry(at_state, &cs->temp_at_states, list)
228 if (test_timeout(at_state))
229 timeout = 1;
230
231 if (atomic_read(&cs->running)) {
Tilman Schmidtec81b5e2006-04-10 22:55:03 -0700232 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800233 if (timeout) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700234 gig_dbg(DEBUG_CMD, "scheduling timeout");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800235 tasklet_schedule(&cs->event_tasklet);
236 }
237 }
238
239 spin_unlock_irqrestore(&cs->lock, flags);
240}
241
242int gigaset_get_channel(struct bc_state *bcs)
243{
244 unsigned long flags;
245
246 spin_lock_irqsave(&bcs->cs->lock, flags);
247 if (bcs->use_count) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700248 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
249 bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800250 spin_unlock_irqrestore(&bcs->cs->lock, flags);
251 return 0;
252 }
253 ++bcs->use_count;
254 bcs->busy = 1;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700255 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800256 spin_unlock_irqrestore(&bcs->cs->lock, flags);
257 return 1;
258}
259
260void gigaset_free_channel(struct bc_state *bcs)
261{
262 unsigned long flags;
263
264 spin_lock_irqsave(&bcs->cs->lock, flags);
265 if (!bcs->busy) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700266 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800267 spin_unlock_irqrestore(&bcs->cs->lock, flags);
268 return;
269 }
270 --bcs->use_count;
271 bcs->busy = 0;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700272 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800273 spin_unlock_irqrestore(&bcs->cs->lock, flags);
274}
275
276int gigaset_get_channels(struct cardstate *cs)
277{
278 unsigned long flags;
279 int i;
280
281 spin_lock_irqsave(&cs->lock, flags);
282 for (i = 0; i < cs->channels; ++i)
283 if (cs->bcs[i].use_count) {
284 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700285 gig_dbg(DEBUG_ANY, "could not allocate all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800286 return 0;
287 }
288 for (i = 0; i < cs->channels; ++i)
289 ++cs->bcs[i].use_count;
290 spin_unlock_irqrestore(&cs->lock, flags);
291
Tilman Schmidt784d5852006-04-10 22:55:04 -0700292 gig_dbg(DEBUG_ANY, "allocated all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800293
294 return 1;
295}
296
297void gigaset_free_channels(struct cardstate *cs)
298{
299 unsigned long flags;
300 int i;
301
Tilman Schmidt784d5852006-04-10 22:55:04 -0700302 gig_dbg(DEBUG_ANY, "unblocking all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800303 spin_lock_irqsave(&cs->lock, flags);
304 for (i = 0; i < cs->channels; ++i)
305 --cs->bcs[i].use_count;
306 spin_unlock_irqrestore(&cs->lock, flags);
307}
308
309void gigaset_block_channels(struct cardstate *cs)
310{
311 unsigned long flags;
312 int i;
313
Tilman Schmidt784d5852006-04-10 22:55:04 -0700314 gig_dbg(DEBUG_ANY, "blocking all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800315 spin_lock_irqsave(&cs->lock, flags);
316 for (i = 0; i < cs->channels; ++i)
317 ++cs->bcs[i].use_count;
318 spin_unlock_irqrestore(&cs->lock, flags);
319}
320
321static void clear_events(struct cardstate *cs)
322{
323 struct event_t *ev;
324 unsigned head, tail;
325
326 /* no locking needed (no reader/writer allowed) */
327
328 head = atomic_read(&cs->ev_head);
329 tail = atomic_read(&cs->ev_tail);
330
331 while (tail != head) {
332 ev = cs->events + head;
333 kfree(ev->ptr);
334
335 head = (head + 1) % MAX_EVENTS;
336 }
337
338 atomic_set(&cs->ev_head, tail);
339}
340
341struct event_t *gigaset_add_event(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700342 struct at_state_t *at_state, int type,
343 void *ptr, int parameter, void *arg)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800344{
345 unsigned long flags;
346 unsigned next, tail;
347 struct event_t *event = NULL;
348
349 spin_lock_irqsave(&cs->ev_lock, flags);
350
351 tail = atomic_read(&cs->ev_tail);
352 next = (tail + 1) % MAX_EVENTS;
353 if (unlikely(next == atomic_read(&cs->ev_head)))
354 err("event queue full");
355 else {
356 event = cs->events + tail;
357 event->type = type;
358 event->at_state = at_state;
359 event->cid = -1;
360 event->ptr = ptr;
361 event->arg = arg;
362 event->parameter = parameter;
363 atomic_set(&cs->ev_tail, next);
364 }
365
366 spin_unlock_irqrestore(&cs->ev_lock, flags);
367
368 return event;
369}
370EXPORT_SYMBOL_GPL(gigaset_add_event);
371
372static void free_strings(struct at_state_t *at_state)
373{
374 int i;
375
376 for (i = 0; i < STR_NUM; ++i) {
377 kfree(at_state->str_var[i]);
378 at_state->str_var[i] = NULL;
379 }
380}
381
382static void clear_at_state(struct at_state_t *at_state)
383{
384 free_strings(at_state);
385}
386
387static void dealloc_at_states(struct cardstate *cs)
388{
389 struct at_state_t *cur, *next;
390
391 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
392 list_del(&cur->list);
393 free_strings(cur);
394 kfree(cur);
395 }
396}
397
398static void gigaset_freebcs(struct bc_state *bcs)
399{
400 int i;
401
Tilman Schmidt784d5852006-04-10 22:55:04 -0700402 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800403 if (!bcs->cs->ops->freebcshw(bcs)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700404 gig_dbg(DEBUG_INIT, "failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800405 }
406
Tilman Schmidt784d5852006-04-10 22:55:04 -0700407 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800408 clear_at_state(&bcs->at_state);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700409 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800410
411 if (bcs->skb)
412 dev_kfree_skb(bcs->skb);
413 for (i = 0; i < AT_NUM; ++i) {
414 kfree(bcs->commands[i]);
415 bcs->commands[i] = NULL;
416 }
417}
418
419void gigaset_freecs(struct cardstate *cs)
420{
421 int i;
422 unsigned long flags;
423
424 if (!cs)
425 return;
426
427 down(&cs->sem);
428
429 if (!cs->bcs)
430 goto f_cs;
431 if (!cs->inbuf)
432 goto f_bcs;
433
434 spin_lock_irqsave(&cs->lock, flags);
435 atomic_set(&cs->running, 0);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700436 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
437 not rescheduled below */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800438
439 tasklet_kill(&cs->event_tasklet);
440 del_timer_sync(&cs->timer);
441
442 switch (cs->cs_init) {
443 default:
444 gigaset_if_free(cs);
445
Tilman Schmidt784d5852006-04-10 22:55:04 -0700446 gig_dbg(DEBUG_INIT, "clearing hw");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800447 cs->ops->freecshw(cs);
448
449 //FIXME cmdbuf
450
451 /* fall through */
452 case 2: /* error in initcshw */
453 /* Deregister from LL */
454 make_invalid(cs, VALID_ID);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700455 gig_dbg(DEBUG_INIT, "clearing iif");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800456 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
457
458 /* fall through */
459 case 1: /* error when regestering to LL */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700460 gig_dbg(DEBUG_INIT, "clearing at_state");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800461 clear_at_state(&cs->at_state);
462 dealloc_at_states(cs);
463
464 /* fall through */
465 case 0: /* error in one call to initbcs */
466 for (i = 0; i < cs->channels; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700467 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800468 gigaset_freebcs(cs->bcs + i);
469 }
470
471 clear_events(cs);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700472 gig_dbg(DEBUG_INIT, "freeing inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800473 kfree(cs->inbuf);
474 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700475f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800476 kfree(cs->bcs);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700477f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800478 up(&cs->sem);
479 free_cs(cs);
480}
481EXPORT_SYMBOL_GPL(gigaset_freecs);
482
483void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700484 struct cardstate *cs, int cid)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800485{
486 int i;
487
488 INIT_LIST_HEAD(&at_state->list);
489 at_state->waiting = 0;
490 at_state->getstring = 0;
491 at_state->pending_commands = 0;
492 at_state->timer_expires = 0;
493 at_state->timer_active = 0;
494 atomic_set(&at_state->timer_index, 0);
495 atomic_set(&at_state->seq_index, 0);
496 at_state->ConState = 0;
497 for (i = 0; i < STR_NUM; ++i)
498 at_state->str_var[i] = NULL;
499 at_state->int_var[VAR_ZDLE] = 0;
500 at_state->int_var[VAR_ZCTP] = -1;
501 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
502 at_state->cs = cs;
503 at_state->bcs = bcs;
504 at_state->cid = cid;
505 if (!cid)
506 at_state->replystruct = cs->tabnocid;
507 else
508 at_state->replystruct = cs->tabcid;
509}
510
511
512static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700513 struct cardstate *cs, int inputstate)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800514/* inbuf->read must be allocated before! */
515{
516 atomic_set(&inbuf->head, 0);
517 atomic_set(&inbuf->tail, 0);
518 inbuf->cs = cs;
519 inbuf->bcs = bcs; /*base driver: NULL*/
520 inbuf->rcvbuf = NULL; //FIXME
521 inbuf->inputstate = inputstate;
522}
523
Tilman Schmidt714e8232006-04-10 22:55:09 -0700524/* append received bytes to inbuf */
525int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
526 unsigned numbytes)
527{
528 unsigned n, head, tail, bytesleft;
529
530 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
531
532 if (!numbytes)
533 return 0;
534
535 bytesleft = numbytes;
536 tail = atomic_read(&inbuf->tail);
537 head = atomic_read(&inbuf->head);
538 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
539
540 while (bytesleft) {
541 if (head > tail)
542 n = head - 1 - tail;
543 else if (head == 0)
544 n = (RBUFSIZE-1) - tail;
545 else
546 n = RBUFSIZE - tail;
547 if (!n) {
548 dev_err(inbuf->cs->dev,
549 "buffer overflow (%u bytes lost)", bytesleft);
550 break;
551 }
552 if (n > bytesleft)
553 n = bytesleft;
554 memcpy(inbuf->data + tail, src, n);
555 bytesleft -= n;
556 tail = (tail + n) % RBUFSIZE;
557 src += n;
558 }
559 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
560 atomic_set(&inbuf->tail, tail);
561 return numbytes != bytesleft;
562}
563EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
564
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800565/* Initialize the b-channel structure */
566static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700567 struct cardstate *cs, int channel)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800568{
569 int i;
570
571 bcs->tx_skb = NULL; //FIXME -> hw part
572
573 skb_queue_head_init(&bcs->squeue);
574
575 bcs->corrupted = 0;
576 bcs->trans_down = 0;
577 bcs->trans_up = 0;
578
Tilman Schmidt784d5852006-04-10 22:55:04 -0700579 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800580 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
581
582 bcs->rcvbytes = 0;
583
584#ifdef CONFIG_GIGASET_DEBUG
585 bcs->emptycount = 0;
586#endif
587
Tilman Schmidt784d5852006-04-10 22:55:04 -0700588 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800589 bcs->fcs = PPP_INITFCS;
590 bcs->inputstate = 0;
591 if (cs->ignoreframes) {
592 bcs->inputstate |= INS_skip_frame;
593 bcs->skb = NULL;
594 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
595 skb_reserve(bcs->skb, HW_HDR_LEN);
596 else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700597 dev_warn(cs->dev, "could not allocate skb\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800598 bcs->inputstate |= INS_skip_frame;
599 }
600
601 bcs->channel = channel;
602 bcs->cs = cs;
603
604 bcs->chstate = 0;
605 bcs->use_count = 1;
606 bcs->busy = 0;
607 bcs->ignore = cs->ignoreframes;
608
609 for (i = 0; i < AT_NUM; ++i)
610 bcs->commands[i] = NULL;
611
Tilman Schmidt784d5852006-04-10 22:55:04 -0700612 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800613 if (cs->ops->initbcshw(bcs))
614 return bcs;
615
Tilman Schmidt784d5852006-04-10 22:55:04 -0700616 gig_dbg(DEBUG_INIT, " failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800617
Tilman Schmidt784d5852006-04-10 22:55:04 -0700618 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800619 if (bcs->skb)
620 dev_kfree_skb(bcs->skb);
621
622 return NULL;
623}
624
625/* gigaset_initcs
626 * Allocate and initialize cardstate structure for Gigaset driver
627 * Calls hardware dependent gigaset_initcshw() function
628 * Calls B channel initialization function gigaset_initbcs() for each B channel
629 * parameters:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700630 * drv hardware driver the device belongs to
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800631 * channels number of B channels supported by device
Tilman Schmidt917f5082006-04-10 22:55:00 -0700632 * onechannel !=0: B channel data and AT commands share one
633 * communication channel
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800634 * ==0: B channels have separate communication channels
635 * ignoreframes number of frames to ignore after setting up B channel
636 * cidmode !=0: start in CallID mode
637 * modulename name of driver module (used for I4L registration)
638 * return value:
639 * pointer to cardstate structure
640 */
641struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
642 int onechannel, int ignoreframes,
643 int cidmode, const char *modulename)
644{
645 struct cardstate *cs = NULL;
646 int i;
647
Tilman Schmidt784d5852006-04-10 22:55:04 -0700648 gig_dbg(DEBUG_INIT, "allocating cs");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800649 cs = alloc_cs(drv);
650 if (!cs)
651 goto error;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700652 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800653 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
654 if (!cs->bcs)
655 goto error;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700656 gig_dbg(DEBUG_INIT, "allocating inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800657 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
658 if (!cs->inbuf)
659 goto error;
660
661 cs->cs_init = 0;
662 cs->channels = channels;
663 cs->onechannel = onechannel;
664 cs->ignoreframes = ignoreframes;
665 INIT_LIST_HEAD(&cs->temp_at_states);
666 atomic_set(&cs->running, 0);
667 init_timer(&cs->timer); /* clear next & prev */
668 spin_lock_init(&cs->ev_lock);
669 atomic_set(&cs->ev_tail, 0);
670 atomic_set(&cs->ev_head, 0);
671 init_MUTEX_LOCKED(&cs->sem);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700672 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
673 (unsigned long) cs);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800674 atomic_set(&cs->commands_pending, 0);
675 cs->cur_at_seq = 0;
676 cs->gotfwver = -1;
677 cs->open_count = 0;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700678 cs->dev = NULL;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800679 cs->tty = NULL;
680 atomic_set(&cs->cidmode, cidmode != 0);
681
682 //if(onechannel) { //FIXME
683 cs->tabnocid = gigaset_tab_nocid_m10x;
684 cs->tabcid = gigaset_tab_cid_m10x;
685 //} else {
686 // cs->tabnocid = gigaset_tab_nocid;
687 // cs->tabcid = gigaset_tab_cid;
688 //}
689
690 init_waitqueue_head(&cs->waitqueue);
691 cs->waiting = 0;
692
693 atomic_set(&cs->mode, M_UNKNOWN);
694 atomic_set(&cs->mstate, MS_UNINITIALIZED);
695
696 for (i = 0; i < channels; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700697 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800698 if (!gigaset_initbcs(cs->bcs + i, cs, i))
699 goto error;
700 }
701
702 ++cs->cs_init;
703
Tilman Schmidt784d5852006-04-10 22:55:04 -0700704 gig_dbg(DEBUG_INIT, "setting up at_state");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800705 spin_lock_init(&cs->lock);
706 gigaset_at_init(&cs->at_state, NULL, cs, 0);
707 cs->dle = 0;
708 cs->cbytes = 0;
709
Tilman Schmidt784d5852006-04-10 22:55:04 -0700710 gig_dbg(DEBUG_INIT, "setting up inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800711 if (onechannel) { //FIXME distinction necessary?
712 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
713 } else
714 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
715
716 atomic_set(&cs->connected, 0);
717
Tilman Schmidt784d5852006-04-10 22:55:04 -0700718 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800719 cs->cmdbuf = cs->lastcmdbuf = NULL;
720 spin_lock_init(&cs->cmdlock);
721 cs->curlen = 0;
722 cs->cmdbytes = 0;
723
Tilman Schmidt784d5852006-04-10 22:55:04 -0700724 gig_dbg(DEBUG_INIT, "setting up iif");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800725 if (!gigaset_register_to_LL(cs, modulename)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700726 err("register_isdn failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800727 goto error;
728 }
729
730 make_valid(cs, VALID_ID);
731 ++cs->cs_init;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700732 gig_dbg(DEBUG_INIT, "setting up hw");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800733 if (!cs->ops->initcshw(cs))
734 goto error;
735
736 ++cs->cs_init;
737
738 gigaset_if_init(cs);
739
740 atomic_set(&cs->running, 1);
Tilman Schmidtec81b5e2006-04-10 22:55:03 -0700741 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
742 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800743 /* FIXME: can jiffies increase too much until the timer is added?
744 * Same problem(?) with mod_timer() in timer_tick(). */
745 add_timer(&cs->timer);
746
Tilman Schmidt784d5852006-04-10 22:55:04 -0700747 gig_dbg(DEBUG_INIT, "cs initialized");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800748 up(&cs->sem);
749 return cs;
750
751error: if (cs)
752 up(&cs->sem);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700753 gig_dbg(DEBUG_INIT, "failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800754 gigaset_freecs(cs);
755 return NULL;
756}
757EXPORT_SYMBOL_GPL(gigaset_initcs);
758
Tilman Schmidt917f5082006-04-10 22:55:00 -0700759/* ReInitialize the b-channel structure */
760/* e.g. called on hangup, disconnect */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800761void gigaset_bcs_reinit(struct bc_state *bcs)
762{
763 struct sk_buff *skb;
764 struct cardstate *cs = bcs->cs;
765 unsigned long flags;
766
767 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
768 dev_kfree_skb(skb);
769
Tilman Schmidt917f5082006-04-10 22:55:00 -0700770 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800771 clear_at_state(&bcs->at_state);
772 bcs->at_state.ConState = 0;
773 bcs->at_state.timer_active = 0;
774 bcs->at_state.timer_expires = 0;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700775 bcs->at_state.cid = -1; /* No CID defined */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800776 spin_unlock_irqrestore(&cs->lock, flags);
777
778 bcs->inputstate = 0;
779
780#ifdef CONFIG_GIGASET_DEBUG
781 bcs->emptycount = 0;
782#endif
783
784 bcs->fcs = PPP_INITFCS;
785 bcs->chstate = 0;
786
787 bcs->ignore = cs->ignoreframes;
788 if (bcs->ignore)
789 bcs->inputstate |= INS_skip_frame;
790
791
792 cs->ops->reinitbcshw(bcs);
793}
794
795static void cleanup_cs(struct cardstate *cs)
796{
797 struct cmdbuf_t *cb, *tcb;
798 int i;
799 unsigned long flags;
800
801 spin_lock_irqsave(&cs->lock, flags);
802
803 atomic_set(&cs->mode, M_UNKNOWN);
804 atomic_set(&cs->mstate, MS_UNINITIALIZED);
805
806 clear_at_state(&cs->at_state);
807 dealloc_at_states(cs);
808 free_strings(&cs->at_state);
809 gigaset_at_init(&cs->at_state, NULL, cs, 0);
810
811 kfree(cs->inbuf->rcvbuf);
812 cs->inbuf->rcvbuf = NULL;
813 cs->inbuf->inputstate = INS_command;
814 atomic_set(&cs->inbuf->head, 0);
815 atomic_set(&cs->inbuf->tail, 0);
816
817 cb = cs->cmdbuf;
818 while (cb) {
819 tcb = cb;
820 cb = cb->next;
821 kfree(tcb);
822 }
823 cs->cmdbuf = cs->lastcmdbuf = NULL;
824 cs->curlen = 0;
825 cs->cmdbytes = 0;
826 cs->gotfwver = -1;
827 cs->dle = 0;
828 cs->cur_at_seq = 0;
829 atomic_set(&cs->commands_pending, 0);
830 cs->cbytes = 0;
831
832 spin_unlock_irqrestore(&cs->lock, flags);
833
834 for (i = 0; i < cs->channels; ++i) {
835 gigaset_freebcs(cs->bcs + i);
836 if (!gigaset_initbcs(cs->bcs + i, cs, i))
837 break; //FIXME error handling
838 }
839
840 if (cs->waiting) {
841 cs->cmd_result = -ENODEV;
842 cs->waiting = 0;
843 wake_up_interruptible(&cs->waitqueue);
844 }
845}
846
847
848int gigaset_start(struct cardstate *cs)
849{
850 if (down_interruptible(&cs->sem))
851 return 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800852
853 atomic_set(&cs->connected, 1);
854
855 if (atomic_read(&cs->mstate) != MS_LOCKED) {
856 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
857 cs->ops->baud_rate(cs, B115200);
858 cs->ops->set_line_ctrl(cs, CS8);
859 cs->control_state = TIOCM_DTR|TIOCM_RTS;
860 } else {
861 //FIXME use some saved values?
862 }
863
864 cs->waiting = 1;
865
866 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
867 cs->waiting = 0;
868 //FIXME what should we do?
869 goto error;
870 }
871
Tilman Schmidt784d5852006-04-10 22:55:04 -0700872 gig_dbg(DEBUG_CMD, "scheduling START");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800873 gigaset_schedule_event(cs);
874
875 wait_event(cs->waitqueue, !cs->waiting);
876
Tilman Schmidtb1d47462006-04-10 22:55:07 -0700877 /* set up device sysfs */
878 gigaset_init_dev_sysfs(cs);
879
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800880 up(&cs->sem);
881 return 1;
882
883error:
884 up(&cs->sem);
885 return 0;
886}
887EXPORT_SYMBOL_GPL(gigaset_start);
888
889void gigaset_shutdown(struct cardstate *cs)
890{
891 down(&cs->sem);
892
893 cs->waiting = 1;
894
895 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
896 //FIXME what should we do?
897 goto exit;
898 }
899
Tilman Schmidt784d5852006-04-10 22:55:04 -0700900 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800901 gigaset_schedule_event(cs);
902
903 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700904 warn("%s: aborted", __func__);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800905 //FIXME
906 }
907
908 if (atomic_read(&cs->mstate) != MS_LOCKED) {
909 //FIXME?
910 //gigaset_baud_rate(cs, B115200);
911 //gigaset_set_line_ctrl(cs, CS8);
912 //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
913 //cs->control_state = 0;
914 } else {
915 //FIXME use some saved values?
916 }
917
918 cleanup_cs(cs);
919
920exit:
921 up(&cs->sem);
922}
923EXPORT_SYMBOL_GPL(gigaset_shutdown);
924
925void gigaset_stop(struct cardstate *cs)
926{
927 down(&cs->sem);
928
Tilman Schmidtb1d47462006-04-10 22:55:07 -0700929 /* clear device sysfs */
930 gigaset_free_dev_sysfs(cs);
931
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800932 atomic_set(&cs->connected, 0);
933
934 cs->waiting = 1;
935
936 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
937 //FIXME what should we do?
938 goto exit;
939 }
940
Tilman Schmidt784d5852006-04-10 22:55:04 -0700941 gig_dbg(DEBUG_CMD, "scheduling STOP");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800942 gigaset_schedule_event(cs);
943
944 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700945 warn("%s: aborted", __func__);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800946 //FIXME
947 }
948
949 /* Tell the LL that the device is not available .. */
950 gigaset_i4l_cmd(cs, ISDN_STAT_STOP); // FIXME move to event layer?
951
952 cleanup_cs(cs);
953
954exit:
955 up(&cs->sem);
956}
957EXPORT_SYMBOL_GPL(gigaset_stop);
958
959static LIST_HEAD(drivers);
960static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
961
962struct cardstate *gigaset_get_cs_by_id(int id)
963{
964 unsigned long flags;
965 static struct cardstate *ret = NULL;
966 static struct cardstate *cs;
967 struct gigaset_driver *drv;
968 unsigned i;
969
970 spin_lock_irqsave(&driver_lock, flags);
971 list_for_each_entry(drv, &drivers, list) {
972 spin_lock(&drv->lock);
973 for (i = 0; i < drv->minors; ++i) {
974 if (drv->flags[i] & VALID_ID) {
975 cs = drv->cs + i;
976 if (cs->myid == id)
977 ret = cs;
978 }
979 if (ret)
980 break;
981 }
982 spin_unlock(&drv->lock);
983 if (ret)
984 break;
985 }
986 spin_unlock_irqrestore(&driver_lock, flags);
987 return ret;
988}
989
990void gigaset_debugdrivers(void)
991{
992 unsigned long flags;
993 static struct cardstate *cs;
994 struct gigaset_driver *drv;
995 unsigned i;
996
997 spin_lock_irqsave(&driver_lock, flags);
998 list_for_each_entry(drv, &drivers, list) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700999 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001000 spin_lock(&drv->lock);
1001 for (i = 0; i < drv->minors; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001002 gig_dbg(DEBUG_DRIVER, " index %u", i);
1003 gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
1004 drv->flags[i]);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001005 cs = drv->cs + i;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001006 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
1007 gig_dbg(DEBUG_DRIVER, " minor_index %u",
1008 cs->minor_index);
1009 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
1010 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001011 }
1012 spin_unlock(&drv->lock);
1013 }
1014 spin_unlock_irqrestore(&driver_lock, flags);
1015}
1016EXPORT_SYMBOL_GPL(gigaset_debugdrivers);
1017
1018struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1019{
1020 if (tty->index < 0 || tty->index >= tty->driver->num)
1021 return NULL;
1022 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1023}
1024
1025struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
1026{
1027 unsigned long flags;
1028 static struct cardstate *ret = NULL;
1029 struct gigaset_driver *drv;
1030 unsigned index;
1031
1032 spin_lock_irqsave(&driver_lock, flags);
1033 list_for_each_entry(drv, &drivers, list) {
1034 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1035 continue;
1036 index = minor - drv->minor;
1037 spin_lock(&drv->lock);
1038 if (drv->flags[index] & VALID_MINOR)
1039 ret = drv->cs + index;
1040 spin_unlock(&drv->lock);
1041 if (ret)
1042 break;
1043 }
1044 spin_unlock_irqrestore(&driver_lock, flags);
1045 return ret;
1046}
1047
1048void gigaset_freedriver(struct gigaset_driver *drv)
1049{
1050 unsigned long flags;
1051
1052 spin_lock_irqsave(&driver_lock, flags);
1053 list_del(&drv->list);
1054 spin_unlock_irqrestore(&driver_lock, flags);
1055
1056 gigaset_if_freedriver(drv);
1057 module_put(drv->owner);
1058
1059 kfree(drv->cs);
1060 kfree(drv->flags);
1061 kfree(drv);
1062}
1063EXPORT_SYMBOL_GPL(gigaset_freedriver);
1064
1065/* gigaset_initdriver
1066 * Allocate and initialize gigaset_driver structure. Initialize interface.
1067 * parameters:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001068 * minor First minor number
1069 * minors Number of minors this driver can handle
1070 * procname Name of the driver
1071 * devname Name of the device files (prefix without minor number)
1072 * devfsname Devfs name of the device files without %d
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001073 * return value:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001074 * Pointer to the gigaset_driver structure on success, NULL on failure.
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001075 */
1076struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001077 const char *procname,
1078 const char *devname,
1079 const char *devfsname,
1080 const struct gigaset_ops *ops,
1081 struct module *owner)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001082{
1083 struct gigaset_driver *drv;
1084 unsigned long flags;
1085 unsigned i;
1086
1087 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1088 if (!drv)
1089 return NULL;
1090 if (!try_module_get(owner))
1091 return NULL;
1092
1093 drv->cs = NULL;
1094 drv->have_tty = 0;
1095 drv->minor = minor;
1096 drv->minors = minors;
1097 spin_lock_init(&drv->lock);
1098 drv->blocked = 0;
1099 drv->ops = ops;
1100 drv->owner = owner;
1101 INIT_LIST_HEAD(&drv->list);
1102
1103 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1104 if (!drv->cs)
1105 goto out1;
1106 drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1107 if (!drv->flags)
1108 goto out2;
1109
1110 for (i = 0; i < minors; ++i) {
1111 drv->flags[i] = 0;
1112 drv->cs[i].driver = drv;
1113 drv->cs[i].ops = drv->ops;
1114 drv->cs[i].minor_index = i;
1115 }
1116
1117 gigaset_if_initdriver(drv, procname, devname, devfsname);
1118
1119 spin_lock_irqsave(&driver_lock, flags);
1120 list_add(&drv->list, &drivers);
1121 spin_unlock_irqrestore(&driver_lock, flags);
1122
1123 return drv;
1124
1125out2:
1126 kfree(drv->cs);
1127out1:
1128 kfree(drv);
1129 module_put(owner);
1130 return NULL;
1131}
1132EXPORT_SYMBOL_GPL(gigaset_initdriver);
1133
1134static struct cardstate *alloc_cs(struct gigaset_driver *drv)
1135{
1136 unsigned long flags;
1137 unsigned i;
1138 static struct cardstate *ret = NULL;
1139
1140 spin_lock_irqsave(&drv->lock, flags);
1141 for (i = 0; i < drv->minors; ++i) {
1142 if (!(drv->flags[i] & VALID_MINOR)) {
1143 drv->flags[i] = VALID_MINOR;
1144 ret = drv->cs + i;
1145 }
1146 if (ret)
1147 break;
1148 }
1149 spin_unlock_irqrestore(&drv->lock, flags);
1150 return ret;
1151}
1152
1153static void free_cs(struct cardstate *cs)
1154{
1155 unsigned long flags;
1156 struct gigaset_driver *drv = cs->driver;
1157 spin_lock_irqsave(&drv->lock, flags);
1158 drv->flags[cs->minor_index] = 0;
1159 spin_unlock_irqrestore(&drv->lock, flags);
1160}
1161
1162static void make_valid(struct cardstate *cs, unsigned mask)
1163{
1164 unsigned long flags;
1165 struct gigaset_driver *drv = cs->driver;
1166 spin_lock_irqsave(&drv->lock, flags);
1167 drv->flags[cs->minor_index] |= mask;
1168 spin_unlock_irqrestore(&drv->lock, flags);
1169}
1170
1171static void make_invalid(struct cardstate *cs, unsigned mask)
1172{
1173 unsigned long flags;
1174 struct gigaset_driver *drv = cs->driver;
1175 spin_lock_irqsave(&drv->lock, flags);
1176 drv->flags[cs->minor_index] &= ~mask;
1177 spin_unlock_irqrestore(&drv->lock, flags);
1178}
1179
1180/* For drivers without fixed assignment device<->cardstate (usb) */
1181struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1182{
1183 unsigned long flags;
1184 struct cardstate *cs = NULL;
1185 unsigned i;
1186
1187 spin_lock_irqsave(&drv->lock, flags);
1188 if (drv->blocked)
1189 goto exit;
1190 for (i = 0; i < drv->minors; ++i) {
1191 if ((drv->flags[i] & VALID_MINOR) &&
1192 !(drv->flags[i] & ASSIGNED)) {
1193 drv->flags[i] |= ASSIGNED;
1194 cs = drv->cs + i;
1195 break;
1196 }
1197 }
1198exit:
1199 spin_unlock_irqrestore(&drv->lock, flags);
1200 return cs;
1201}
1202EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1203
1204void gigaset_unassign(struct cardstate *cs)
1205{
1206 unsigned long flags;
1207 unsigned *minor_flags;
1208 struct gigaset_driver *drv;
1209
1210 if (!cs)
1211 return;
1212 drv = cs->driver;
1213 spin_lock_irqsave(&drv->lock, flags);
1214 minor_flags = drv->flags + cs->minor_index;
1215 if (*minor_flags & VALID_MINOR)
1216 *minor_flags &= ~ASSIGNED;
1217 spin_unlock_irqrestore(&drv->lock, flags);
1218}
1219EXPORT_SYMBOL_GPL(gigaset_unassign);
1220
1221void gigaset_blockdriver(struct gigaset_driver *drv)
1222{
1223 unsigned long flags;
1224 spin_lock_irqsave(&drv->lock, flags);
1225 drv->blocked = 1;
1226 spin_unlock_irqrestore(&drv->lock, flags);
1227}
1228EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1229
1230static int __init gigaset_init_module(void)
1231{
1232 /* in accordance with the principle of least astonishment,
1233 * setting the 'debug' parameter to 1 activates a sensible
1234 * set of default debug levels
1235 */
1236 if (gigaset_debuglevel == 1)
1237 gigaset_debuglevel = DEBUG_DEFAULT;
1238
1239 info(DRIVER_AUTHOR);
1240 info(DRIVER_DESC);
1241 return 0;
1242}
1243
1244static void __exit gigaset_exit_module(void)
1245{
1246}
1247
1248module_init(gigaset_init_module);
1249module_exit(gigaset_exit_module);
1250
1251MODULE_AUTHOR(DRIVER_AUTHOR);
1252MODULE_DESCRIPTION(DRIVER_DESC);
1253
1254MODULE_LICENSE("GPL");