blob: c3c9804796cd1b30ca63315a51f3f48346cdf59f [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 Schmidt01371502006-04-10 22:55:11 -070082 size_t len, const unsigned char *buf)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080083{
84 unsigned char outbuf[80];
Tilman Schmidt784d5852006-04-10 22:55:04 -070085 unsigned char c;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080086 size_t space = sizeof outbuf - 1;
87 unsigned char *out = outbuf;
Tilman Schmidt01371502006-04-10 22:55:11 -070088 size_t numin = len;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080089
Tilman Schmidt01371502006-04-10 22:55:11 -070090 while (numin--) {
Tilman Schmidt784d5852006-04-10 22:55:04 -070091 c = *buf++;
92 if (c == '~' || c == '^' || c == '\\') {
Tilman Schmidt01371502006-04-10 22:55:11 -070093 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -070094 break;
95 *out++ = '\\';
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080096 }
Tilman Schmidt784d5852006-04-10 22:55:04 -070097 if (c & 0x80) {
Tilman Schmidt01371502006-04-10 22:55:11 -070098 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -070099 break;
100 *out++ = '~';
101 c ^= 0x80;
102 }
103 if (c < 0x20 || c == 0x7f) {
Tilman Schmidt01371502006-04-10 22:55:11 -0700104 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700105 break;
106 *out++ = '^';
107 c ^= 0x40;
108 }
Tilman Schmidt01371502006-04-10 22:55:11 -0700109 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700110 break;
111 *out++ = c;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800112 }
113 *out = 0;
114
Tilman Schmidt784d5852006-04-10 22:55:04 -0700115 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800116}
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:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700168 dev_err(cs->dev, "error %d on setuartbits\n", -r);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800169 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) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700181 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
182 at_state, at_state->timer_expires);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800183 return 0;
184 }
185
186 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700187 atomic_read(&at_state->timer_index), NULL)) {
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800188 //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)) {
Tilman Schmidtec81b5e62006-04-10 22:55:03 -0700216 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800217 if (timeout) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700218 gig_dbg(DEBUG_CMD, "scheduling timeout");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800219 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) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700232 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
233 bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800234 spin_unlock_irqrestore(&bcs->cs->lock, flags);
235 return 0;
236 }
237 ++bcs->use_count;
238 bcs->busy = 1;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700239 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800240 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) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700250 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800251 spin_unlock_irqrestore(&bcs->cs->lock, flags);
252 return;
253 }
254 --bcs->use_count;
255 bcs->busy = 0;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700256 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800257 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);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700269 gig_dbg(DEBUG_ANY, "could not allocate all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800270 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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700276 gig_dbg(DEBUG_ANY, "allocated all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800277
278 return 1;
279}
280
281void gigaset_free_channels(struct cardstate *cs)
282{
283 unsigned long flags;
284 int i;
285
Tilman Schmidt784d5852006-04-10 22:55:04 -0700286 gig_dbg(DEBUG_ANY, "unblocking all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800287 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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700298 gig_dbg(DEBUG_ANY, "blocking all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800299 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,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700326 struct at_state_t *at_state, int type,
327 void *ptr, int parameter, void *arg)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800328{
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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700386 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800387 if (!bcs->cs->ops->freebcshw(bcs)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700388 gig_dbg(DEBUG_INIT, "failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800389 }
390
Tilman Schmidt784d5852006-04-10 22:55:04 -0700391 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800392 clear_at_state(&bcs->at_state);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700393 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800394
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
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700411 mutex_lock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800412
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);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700420 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
421 not rescheduled below */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800422
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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700430 gig_dbg(DEBUG_INIT, "clearing hw");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800431 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);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700439 gig_dbg(DEBUG_INIT, "clearing iif");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800440 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
441
442 /* fall through */
443 case 1: /* error when regestering to LL */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700444 gig_dbg(DEBUG_INIT, "clearing at_state");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800445 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) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700451 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800452 gigaset_freebcs(cs->bcs + i);
453 }
454
455 clear_events(cs);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700456 gig_dbg(DEBUG_INIT, "freeing inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800457 kfree(cs->inbuf);
458 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700459f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800460 kfree(cs->bcs);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700461f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700462 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800463 free_cs(cs);
464}
465EXPORT_SYMBOL_GPL(gigaset_freecs);
466
467void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700468 struct cardstate *cs, int cid)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800469{
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,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700497 struct cardstate *cs, int inputstate)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800498/* 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
Tilman Schmidt714e8232006-04-10 22:55:09 -0700508/* 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
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800549/* Initialize the b-channel structure */
550static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700551 struct cardstate *cs, int channel)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800552{
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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700563 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800564 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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700572 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800573 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 {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700581 dev_warn(cs->dev, "could not allocate skb\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800582 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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700596 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800597 if (cs->ops->initbcshw(bcs))
598 return bcs;
599
Tilman Schmidt784d5852006-04-10 22:55:04 -0700600 gig_dbg(DEBUG_INIT, " failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800601
Tilman Schmidt784d5852006-04-10 22:55:04 -0700602 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800603 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:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700614 * drv hardware driver the device belongs to
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800615 * channels number of B channels supported by device
Tilman Schmidt917f5082006-04-10 22:55:00 -0700616 * onechannel !=0: B channel data and AT commands share one
617 * communication channel
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800618 * ==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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700632 gig_dbg(DEBUG_INIT, "allocating cs");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800633 cs = alloc_cs(drv);
634 if (!cs)
635 goto error;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700636 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800637 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
638 if (!cs->bcs)
639 goto error;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700640 gig_dbg(DEBUG_INIT, "allocating inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800641 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);
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700655 mutex_init(&cs->mutex);
656 mutex_lock(&cs->mutex);
657
Tilman Schmidt917f5082006-04-10 22:55:00 -0700658 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
659 (unsigned long) cs);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800660 atomic_set(&cs->commands_pending, 0);
661 cs->cur_at_seq = 0;
662 cs->gotfwver = -1;
663 cs->open_count = 0;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700664 cs->dev = NULL;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800665 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) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700683 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800684 if (!gigaset_initbcs(cs->bcs + i, cs, i))
685 goto error;
686 }
687
688 ++cs->cs_init;
689
Tilman Schmidt784d5852006-04-10 22:55:04 -0700690 gig_dbg(DEBUG_INIT, "setting up at_state");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800691 spin_lock_init(&cs->lock);
692 gigaset_at_init(&cs->at_state, NULL, cs, 0);
693 cs->dle = 0;
694 cs->cbytes = 0;
695
Tilman Schmidt784d5852006-04-10 22:55:04 -0700696 gig_dbg(DEBUG_INIT, "setting up inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800697 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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700704 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800705 cs->cmdbuf = cs->lastcmdbuf = NULL;
706 spin_lock_init(&cs->cmdlock);
707 cs->curlen = 0;
708 cs->cmdbytes = 0;
709
Tilman Schmidt784d5852006-04-10 22:55:04 -0700710 gig_dbg(DEBUG_INIT, "setting up iif");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800711 if (!gigaset_register_to_LL(cs, modulename)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700712 err("register_isdn failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800713 goto error;
714 }
715
716 make_valid(cs, VALID_ID);
717 ++cs->cs_init;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700718 gig_dbg(DEBUG_INIT, "setting up hw");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800719 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);
Tilman Schmidtec81b5e62006-04-10 22:55:03 -0700727 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
728 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800729 /* 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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700733 gig_dbg(DEBUG_INIT, "cs initialized");
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700734 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800735 return cs;
736
737error: if (cs)
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700738 mutex_unlock(&cs->mutex);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700739 gig_dbg(DEBUG_INIT, "failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800740 gigaset_freecs(cs);
741 return NULL;
742}
743EXPORT_SYMBOL_GPL(gigaset_initcs);
744
Tilman Schmidt917f5082006-04-10 22:55:00 -0700745/* ReInitialize the b-channel structure */
746/* e.g. called on hangup, disconnect */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800747void 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
Tilman Schmidt917f5082006-04-10 22:55:00 -0700756 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800757 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;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700761 bcs->at_state.cid = -1; /* No CID defined */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800762 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{
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700836 if (mutex_lock_interruptible(&cs->mutex))
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800837 return 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800838
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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700858 gig_dbg(DEBUG_CMD, "scheduling START");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800859 gigaset_schedule_event(cs);
860
861 wait_event(cs->waitqueue, !cs->waiting);
862
Tilman Schmidtb1d47462006-04-10 22:55:07 -0700863 /* set up device sysfs */
864 gigaset_init_dev_sysfs(cs);
865
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700866 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800867 return 1;
868
869error:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700870 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800871 return 0;
872}
873EXPORT_SYMBOL_GPL(gigaset_start);
874
875void gigaset_shutdown(struct cardstate *cs)
876{
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700877 mutex_lock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800878
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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700886 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800887 gigaset_schedule_event(cs);
888
889 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700890 warn("%s: aborted", __func__);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800891 //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:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700907 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800908}
909EXPORT_SYMBOL_GPL(gigaset_shutdown);
910
911void gigaset_stop(struct cardstate *cs)
912{
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700913 mutex_lock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800914
Tilman Schmidtb1d47462006-04-10 22:55:07 -0700915 /* clear device sysfs */
916 gigaset_free_dev_sysfs(cs);
917
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800918 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
Tilman Schmidt784d5852006-04-10 22:55:04 -0700927 gig_dbg(DEBUG_CMD, "scheduling STOP");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800928 gigaset_schedule_event(cs);
929
930 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700931 warn("%s: aborted", __func__);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800932 //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:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700941 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800942}
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) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700985 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800986 spin_lock(&drv->lock);
987 for (i = 0; i < drv->minors; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700988 gig_dbg(DEBUG_DRIVER, " index %u", i);
989 gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
990 drv->flags[i]);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800991 cs = drv->cs + i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700992 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);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800997 }
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:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001054 * 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
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001059 * return value:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001060 * Pointer to the gigaset_driver structure on success, NULL on failure.
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001061 */
1062struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001063 const char *procname,
1064 const char *devname,
1065 const char *devfsname,
1066 const struct gigaset_ops *ops,
1067 struct module *owner)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001068{
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");