blob: fb5cf703133fbfeae74536e98f68980cf839f5b9 [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
524/* Initialize the b-channel structure */
525static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700526 struct cardstate *cs, int channel)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800527{
528 int i;
529
530 bcs->tx_skb = NULL; //FIXME -> hw part
531
532 skb_queue_head_init(&bcs->squeue);
533
534 bcs->corrupted = 0;
535 bcs->trans_down = 0;
536 bcs->trans_up = 0;
537
Tilman Schmidt784d5852006-04-10 22:55:04 -0700538 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800539 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
540
541 bcs->rcvbytes = 0;
542
543#ifdef CONFIG_GIGASET_DEBUG
544 bcs->emptycount = 0;
545#endif
546
Tilman Schmidt784d5852006-04-10 22:55:04 -0700547 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800548 bcs->fcs = PPP_INITFCS;
549 bcs->inputstate = 0;
550 if (cs->ignoreframes) {
551 bcs->inputstate |= INS_skip_frame;
552 bcs->skb = NULL;
553 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
554 skb_reserve(bcs->skb, HW_HDR_LEN);
555 else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700556 dev_warn(cs->dev, "could not allocate skb\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800557 bcs->inputstate |= INS_skip_frame;
558 }
559
560 bcs->channel = channel;
561 bcs->cs = cs;
562
563 bcs->chstate = 0;
564 bcs->use_count = 1;
565 bcs->busy = 0;
566 bcs->ignore = cs->ignoreframes;
567
568 for (i = 0; i < AT_NUM; ++i)
569 bcs->commands[i] = NULL;
570
Tilman Schmidt784d5852006-04-10 22:55:04 -0700571 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800572 if (cs->ops->initbcshw(bcs))
573 return bcs;
574
Tilman Schmidt784d5852006-04-10 22:55:04 -0700575 gig_dbg(DEBUG_INIT, " failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800576
Tilman Schmidt784d5852006-04-10 22:55:04 -0700577 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800578 if (bcs->skb)
579 dev_kfree_skb(bcs->skb);
580
581 return NULL;
582}
583
584/* gigaset_initcs
585 * Allocate and initialize cardstate structure for Gigaset driver
586 * Calls hardware dependent gigaset_initcshw() function
587 * Calls B channel initialization function gigaset_initbcs() for each B channel
588 * parameters:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700589 * drv hardware driver the device belongs to
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800590 * channels number of B channels supported by device
Tilman Schmidt917f5082006-04-10 22:55:00 -0700591 * onechannel !=0: B channel data and AT commands share one
592 * communication channel
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800593 * ==0: B channels have separate communication channels
594 * ignoreframes number of frames to ignore after setting up B channel
595 * cidmode !=0: start in CallID mode
596 * modulename name of driver module (used for I4L registration)
597 * return value:
598 * pointer to cardstate structure
599 */
600struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
601 int onechannel, int ignoreframes,
602 int cidmode, const char *modulename)
603{
604 struct cardstate *cs = NULL;
605 int i;
606
Tilman Schmidt784d5852006-04-10 22:55:04 -0700607 gig_dbg(DEBUG_INIT, "allocating cs");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800608 cs = alloc_cs(drv);
609 if (!cs)
610 goto error;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700611 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800612 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
613 if (!cs->bcs)
614 goto error;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700615 gig_dbg(DEBUG_INIT, "allocating inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800616 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
617 if (!cs->inbuf)
618 goto error;
619
620 cs->cs_init = 0;
621 cs->channels = channels;
622 cs->onechannel = onechannel;
623 cs->ignoreframes = ignoreframes;
624 INIT_LIST_HEAD(&cs->temp_at_states);
625 atomic_set(&cs->running, 0);
626 init_timer(&cs->timer); /* clear next & prev */
627 spin_lock_init(&cs->ev_lock);
628 atomic_set(&cs->ev_tail, 0);
629 atomic_set(&cs->ev_head, 0);
630 init_MUTEX_LOCKED(&cs->sem);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700631 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
632 (unsigned long) cs);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800633 atomic_set(&cs->commands_pending, 0);
634 cs->cur_at_seq = 0;
635 cs->gotfwver = -1;
636 cs->open_count = 0;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700637 cs->dev = NULL;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800638 cs->tty = NULL;
639 atomic_set(&cs->cidmode, cidmode != 0);
640
641 //if(onechannel) { //FIXME
642 cs->tabnocid = gigaset_tab_nocid_m10x;
643 cs->tabcid = gigaset_tab_cid_m10x;
644 //} else {
645 // cs->tabnocid = gigaset_tab_nocid;
646 // cs->tabcid = gigaset_tab_cid;
647 //}
648
649 init_waitqueue_head(&cs->waitqueue);
650 cs->waiting = 0;
651
652 atomic_set(&cs->mode, M_UNKNOWN);
653 atomic_set(&cs->mstate, MS_UNINITIALIZED);
654
655 for (i = 0; i < channels; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700656 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800657 if (!gigaset_initbcs(cs->bcs + i, cs, i))
658 goto error;
659 }
660
661 ++cs->cs_init;
662
Tilman Schmidt784d5852006-04-10 22:55:04 -0700663 gig_dbg(DEBUG_INIT, "setting up at_state");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800664 spin_lock_init(&cs->lock);
665 gigaset_at_init(&cs->at_state, NULL, cs, 0);
666 cs->dle = 0;
667 cs->cbytes = 0;
668
Tilman Schmidt784d5852006-04-10 22:55:04 -0700669 gig_dbg(DEBUG_INIT, "setting up inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800670 if (onechannel) { //FIXME distinction necessary?
671 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
672 } else
673 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
674
675 atomic_set(&cs->connected, 0);
676
Tilman Schmidt784d5852006-04-10 22:55:04 -0700677 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800678 cs->cmdbuf = cs->lastcmdbuf = NULL;
679 spin_lock_init(&cs->cmdlock);
680 cs->curlen = 0;
681 cs->cmdbytes = 0;
682
Tilman Schmidt784d5852006-04-10 22:55:04 -0700683 gig_dbg(DEBUG_INIT, "setting up iif");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800684 if (!gigaset_register_to_LL(cs, modulename)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700685 err("register_isdn failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800686 goto error;
687 }
688
689 make_valid(cs, VALID_ID);
690 ++cs->cs_init;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700691 gig_dbg(DEBUG_INIT, "setting up hw");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800692 if (!cs->ops->initcshw(cs))
693 goto error;
694
695 ++cs->cs_init;
696
697 gigaset_if_init(cs);
698
699 atomic_set(&cs->running, 1);
Tilman Schmidtec81b5e2006-04-10 22:55:03 -0700700 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
701 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800702 /* FIXME: can jiffies increase too much until the timer is added?
703 * Same problem(?) with mod_timer() in timer_tick(). */
704 add_timer(&cs->timer);
705
Tilman Schmidt784d5852006-04-10 22:55:04 -0700706 gig_dbg(DEBUG_INIT, "cs initialized");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800707 up(&cs->sem);
708 return cs;
709
710error: if (cs)
711 up(&cs->sem);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700712 gig_dbg(DEBUG_INIT, "failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800713 gigaset_freecs(cs);
714 return NULL;
715}
716EXPORT_SYMBOL_GPL(gigaset_initcs);
717
Tilman Schmidt917f5082006-04-10 22:55:00 -0700718/* ReInitialize the b-channel structure */
719/* e.g. called on hangup, disconnect */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800720void gigaset_bcs_reinit(struct bc_state *bcs)
721{
722 struct sk_buff *skb;
723 struct cardstate *cs = bcs->cs;
724 unsigned long flags;
725
726 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
727 dev_kfree_skb(skb);
728
Tilman Schmidt917f5082006-04-10 22:55:00 -0700729 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800730 clear_at_state(&bcs->at_state);
731 bcs->at_state.ConState = 0;
732 bcs->at_state.timer_active = 0;
733 bcs->at_state.timer_expires = 0;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700734 bcs->at_state.cid = -1; /* No CID defined */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800735 spin_unlock_irqrestore(&cs->lock, flags);
736
737 bcs->inputstate = 0;
738
739#ifdef CONFIG_GIGASET_DEBUG
740 bcs->emptycount = 0;
741#endif
742
743 bcs->fcs = PPP_INITFCS;
744 bcs->chstate = 0;
745
746 bcs->ignore = cs->ignoreframes;
747 if (bcs->ignore)
748 bcs->inputstate |= INS_skip_frame;
749
750
751 cs->ops->reinitbcshw(bcs);
752}
753
754static void cleanup_cs(struct cardstate *cs)
755{
756 struct cmdbuf_t *cb, *tcb;
757 int i;
758 unsigned long flags;
759
760 spin_lock_irqsave(&cs->lock, flags);
761
762 atomic_set(&cs->mode, M_UNKNOWN);
763 atomic_set(&cs->mstate, MS_UNINITIALIZED);
764
765 clear_at_state(&cs->at_state);
766 dealloc_at_states(cs);
767 free_strings(&cs->at_state);
768 gigaset_at_init(&cs->at_state, NULL, cs, 0);
769
770 kfree(cs->inbuf->rcvbuf);
771 cs->inbuf->rcvbuf = NULL;
772 cs->inbuf->inputstate = INS_command;
773 atomic_set(&cs->inbuf->head, 0);
774 atomic_set(&cs->inbuf->tail, 0);
775
776 cb = cs->cmdbuf;
777 while (cb) {
778 tcb = cb;
779 cb = cb->next;
780 kfree(tcb);
781 }
782 cs->cmdbuf = cs->lastcmdbuf = NULL;
783 cs->curlen = 0;
784 cs->cmdbytes = 0;
785 cs->gotfwver = -1;
786 cs->dle = 0;
787 cs->cur_at_seq = 0;
788 atomic_set(&cs->commands_pending, 0);
789 cs->cbytes = 0;
790
791 spin_unlock_irqrestore(&cs->lock, flags);
792
793 for (i = 0; i < cs->channels; ++i) {
794 gigaset_freebcs(cs->bcs + i);
795 if (!gigaset_initbcs(cs->bcs + i, cs, i))
796 break; //FIXME error handling
797 }
798
799 if (cs->waiting) {
800 cs->cmd_result = -ENODEV;
801 cs->waiting = 0;
802 wake_up_interruptible(&cs->waitqueue);
803 }
804}
805
806
807int gigaset_start(struct cardstate *cs)
808{
809 if (down_interruptible(&cs->sem))
810 return 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800811
812 atomic_set(&cs->connected, 1);
813
814 if (atomic_read(&cs->mstate) != MS_LOCKED) {
815 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
816 cs->ops->baud_rate(cs, B115200);
817 cs->ops->set_line_ctrl(cs, CS8);
818 cs->control_state = TIOCM_DTR|TIOCM_RTS;
819 } else {
820 //FIXME use some saved values?
821 }
822
823 cs->waiting = 1;
824
825 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
826 cs->waiting = 0;
827 //FIXME what should we do?
828 goto error;
829 }
830
Tilman Schmidt784d5852006-04-10 22:55:04 -0700831 gig_dbg(DEBUG_CMD, "scheduling START");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800832 gigaset_schedule_event(cs);
833
834 wait_event(cs->waitqueue, !cs->waiting);
835
836 up(&cs->sem);
837 return 1;
838
839error:
840 up(&cs->sem);
841 return 0;
842}
843EXPORT_SYMBOL_GPL(gigaset_start);
844
845void gigaset_shutdown(struct cardstate *cs)
846{
847 down(&cs->sem);
848
849 cs->waiting = 1;
850
851 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
852 //FIXME what should we do?
853 goto exit;
854 }
855
Tilman Schmidt784d5852006-04-10 22:55:04 -0700856 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800857 gigaset_schedule_event(cs);
858
859 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700860 warn("%s: aborted", __func__);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800861 //FIXME
862 }
863
864 if (atomic_read(&cs->mstate) != MS_LOCKED) {
865 //FIXME?
866 //gigaset_baud_rate(cs, B115200);
867 //gigaset_set_line_ctrl(cs, CS8);
868 //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
869 //cs->control_state = 0;
870 } else {
871 //FIXME use some saved values?
872 }
873
874 cleanup_cs(cs);
875
876exit:
877 up(&cs->sem);
878}
879EXPORT_SYMBOL_GPL(gigaset_shutdown);
880
881void gigaset_stop(struct cardstate *cs)
882{
883 down(&cs->sem);
884
885 atomic_set(&cs->connected, 0);
886
887 cs->waiting = 1;
888
889 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
890 //FIXME what should we do?
891 goto exit;
892 }
893
Tilman Schmidt784d5852006-04-10 22:55:04 -0700894 gig_dbg(DEBUG_CMD, "scheduling STOP");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800895 gigaset_schedule_event(cs);
896
897 if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700898 warn("%s: aborted", __func__);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800899 //FIXME
900 }
901
902 /* Tell the LL that the device is not available .. */
903 gigaset_i4l_cmd(cs, ISDN_STAT_STOP); // FIXME move to event layer?
904
905 cleanup_cs(cs);
906
907exit:
908 up(&cs->sem);
909}
910EXPORT_SYMBOL_GPL(gigaset_stop);
911
912static LIST_HEAD(drivers);
913static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
914
915struct cardstate *gigaset_get_cs_by_id(int id)
916{
917 unsigned long flags;
918 static struct cardstate *ret = NULL;
919 static struct cardstate *cs;
920 struct gigaset_driver *drv;
921 unsigned i;
922
923 spin_lock_irqsave(&driver_lock, flags);
924 list_for_each_entry(drv, &drivers, list) {
925 spin_lock(&drv->lock);
926 for (i = 0; i < drv->minors; ++i) {
927 if (drv->flags[i] & VALID_ID) {
928 cs = drv->cs + i;
929 if (cs->myid == id)
930 ret = cs;
931 }
932 if (ret)
933 break;
934 }
935 spin_unlock(&drv->lock);
936 if (ret)
937 break;
938 }
939 spin_unlock_irqrestore(&driver_lock, flags);
940 return ret;
941}
942
943void gigaset_debugdrivers(void)
944{
945 unsigned long flags;
946 static struct cardstate *cs;
947 struct gigaset_driver *drv;
948 unsigned i;
949
950 spin_lock_irqsave(&driver_lock, flags);
951 list_for_each_entry(drv, &drivers, list) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700952 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800953 spin_lock(&drv->lock);
954 for (i = 0; i < drv->minors; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700955 gig_dbg(DEBUG_DRIVER, " index %u", i);
956 gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
957 drv->flags[i]);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800958 cs = drv->cs + i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700959 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
960 gig_dbg(DEBUG_DRIVER, " minor_index %u",
961 cs->minor_index);
962 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
963 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800964 }
965 spin_unlock(&drv->lock);
966 }
967 spin_unlock_irqrestore(&driver_lock, flags);
968}
969EXPORT_SYMBOL_GPL(gigaset_debugdrivers);
970
971struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
972{
973 if (tty->index < 0 || tty->index >= tty->driver->num)
974 return NULL;
975 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
976}
977
978struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
979{
980 unsigned long flags;
981 static struct cardstate *ret = NULL;
982 struct gigaset_driver *drv;
983 unsigned index;
984
985 spin_lock_irqsave(&driver_lock, flags);
986 list_for_each_entry(drv, &drivers, list) {
987 if (minor < drv->minor || minor >= drv->minor + drv->minors)
988 continue;
989 index = minor - drv->minor;
990 spin_lock(&drv->lock);
991 if (drv->flags[index] & VALID_MINOR)
992 ret = drv->cs + index;
993 spin_unlock(&drv->lock);
994 if (ret)
995 break;
996 }
997 spin_unlock_irqrestore(&driver_lock, flags);
998 return ret;
999}
1000
1001void gigaset_freedriver(struct gigaset_driver *drv)
1002{
1003 unsigned long flags;
1004
1005 spin_lock_irqsave(&driver_lock, flags);
1006 list_del(&drv->list);
1007 spin_unlock_irqrestore(&driver_lock, flags);
1008
1009 gigaset_if_freedriver(drv);
1010 module_put(drv->owner);
1011
1012 kfree(drv->cs);
1013 kfree(drv->flags);
1014 kfree(drv);
1015}
1016EXPORT_SYMBOL_GPL(gigaset_freedriver);
1017
1018/* gigaset_initdriver
1019 * Allocate and initialize gigaset_driver structure. Initialize interface.
1020 * parameters:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001021 * minor First minor number
1022 * minors Number of minors this driver can handle
1023 * procname Name of the driver
1024 * devname Name of the device files (prefix without minor number)
1025 * devfsname Devfs name of the device files without %d
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001026 * return value:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001027 * Pointer to the gigaset_driver structure on success, NULL on failure.
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001028 */
1029struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001030 const char *procname,
1031 const char *devname,
1032 const char *devfsname,
1033 const struct gigaset_ops *ops,
1034 struct module *owner)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001035{
1036 struct gigaset_driver *drv;
1037 unsigned long flags;
1038 unsigned i;
1039
1040 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1041 if (!drv)
1042 return NULL;
1043 if (!try_module_get(owner))
1044 return NULL;
1045
1046 drv->cs = NULL;
1047 drv->have_tty = 0;
1048 drv->minor = minor;
1049 drv->minors = minors;
1050 spin_lock_init(&drv->lock);
1051 drv->blocked = 0;
1052 drv->ops = ops;
1053 drv->owner = owner;
1054 INIT_LIST_HEAD(&drv->list);
1055
1056 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1057 if (!drv->cs)
1058 goto out1;
1059 drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1060 if (!drv->flags)
1061 goto out2;
1062
1063 for (i = 0; i < minors; ++i) {
1064 drv->flags[i] = 0;
1065 drv->cs[i].driver = drv;
1066 drv->cs[i].ops = drv->ops;
1067 drv->cs[i].minor_index = i;
1068 }
1069
1070 gigaset_if_initdriver(drv, procname, devname, devfsname);
1071
1072 spin_lock_irqsave(&driver_lock, flags);
1073 list_add(&drv->list, &drivers);
1074 spin_unlock_irqrestore(&driver_lock, flags);
1075
1076 return drv;
1077
1078out2:
1079 kfree(drv->cs);
1080out1:
1081 kfree(drv);
1082 module_put(owner);
1083 return NULL;
1084}
1085EXPORT_SYMBOL_GPL(gigaset_initdriver);
1086
1087static struct cardstate *alloc_cs(struct gigaset_driver *drv)
1088{
1089 unsigned long flags;
1090 unsigned i;
1091 static struct cardstate *ret = NULL;
1092
1093 spin_lock_irqsave(&drv->lock, flags);
1094 for (i = 0; i < drv->minors; ++i) {
1095 if (!(drv->flags[i] & VALID_MINOR)) {
1096 drv->flags[i] = VALID_MINOR;
1097 ret = drv->cs + i;
1098 }
1099 if (ret)
1100 break;
1101 }
1102 spin_unlock_irqrestore(&drv->lock, flags);
1103 return ret;
1104}
1105
1106static void free_cs(struct cardstate *cs)
1107{
1108 unsigned long flags;
1109 struct gigaset_driver *drv = cs->driver;
1110 spin_lock_irqsave(&drv->lock, flags);
1111 drv->flags[cs->minor_index] = 0;
1112 spin_unlock_irqrestore(&drv->lock, flags);
1113}
1114
1115static void make_valid(struct cardstate *cs, unsigned mask)
1116{
1117 unsigned long flags;
1118 struct gigaset_driver *drv = cs->driver;
1119 spin_lock_irqsave(&drv->lock, flags);
1120 drv->flags[cs->minor_index] |= mask;
1121 spin_unlock_irqrestore(&drv->lock, flags);
1122}
1123
1124static void make_invalid(struct cardstate *cs, unsigned mask)
1125{
1126 unsigned long flags;
1127 struct gigaset_driver *drv = cs->driver;
1128 spin_lock_irqsave(&drv->lock, flags);
1129 drv->flags[cs->minor_index] &= ~mask;
1130 spin_unlock_irqrestore(&drv->lock, flags);
1131}
1132
1133/* For drivers without fixed assignment device<->cardstate (usb) */
1134struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1135{
1136 unsigned long flags;
1137 struct cardstate *cs = NULL;
1138 unsigned i;
1139
1140 spin_lock_irqsave(&drv->lock, flags);
1141 if (drv->blocked)
1142 goto exit;
1143 for (i = 0; i < drv->minors; ++i) {
1144 if ((drv->flags[i] & VALID_MINOR) &&
1145 !(drv->flags[i] & ASSIGNED)) {
1146 drv->flags[i] |= ASSIGNED;
1147 cs = drv->cs + i;
1148 break;
1149 }
1150 }
1151exit:
1152 spin_unlock_irqrestore(&drv->lock, flags);
1153 return cs;
1154}
1155EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1156
1157void gigaset_unassign(struct cardstate *cs)
1158{
1159 unsigned long flags;
1160 unsigned *minor_flags;
1161 struct gigaset_driver *drv;
1162
1163 if (!cs)
1164 return;
1165 drv = cs->driver;
1166 spin_lock_irqsave(&drv->lock, flags);
1167 minor_flags = drv->flags + cs->minor_index;
1168 if (*minor_flags & VALID_MINOR)
1169 *minor_flags &= ~ASSIGNED;
1170 spin_unlock_irqrestore(&drv->lock, flags);
1171}
1172EXPORT_SYMBOL_GPL(gigaset_unassign);
1173
1174void gigaset_blockdriver(struct gigaset_driver *drv)
1175{
1176 unsigned long flags;
1177 spin_lock_irqsave(&drv->lock, flags);
1178 drv->blocked = 1;
1179 spin_unlock_irqrestore(&drv->lock, flags);
1180}
1181EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1182
1183static int __init gigaset_init_module(void)
1184{
1185 /* in accordance with the principle of least astonishment,
1186 * setting the 'debug' parameter to 1 activates a sensible
1187 * set of default debug levels
1188 */
1189 if (gigaset_debuglevel == 1)
1190 gigaset_debuglevel = DEBUG_DEFAULT;
1191
1192 info(DRIVER_AUTHOR);
1193 info(DRIVER_DESC);
1194 return 0;
1195}
1196
1197static void __exit gigaset_exit_module(void)
1198{
1199}
1200
1201module_init(gigaset_init_module);
1202module_exit(gigaset_exit_module);
1203
1204MODULE_AUTHOR(DRIVER_AUTHOR);
1205MODULE_DESCRIPTION(DRIVER_DESC);
1206
1207MODULE_LICENSE("GPL");