blob: 0048ce98bfa8d789dd2262db49bf672ec75f64b1 [file] [log] [blame]
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001/*
2 * Stuff used by all variants of the driver
3 *
Tilman Schmidt70440cf2006-04-10 22:55:14 -07004 * Copyright (c) 2001 by Stefan Eilers,
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08005 * 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 */
Tilman Schmidt70440cf2006-04-10 22:55:14 -070022#define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080023#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
Tilman Schmidt70440cf2006-04-10 22:55:14 -070031/* driver state flags */
Tilman Schmidt784d5852006-04-10 22:55:04 -070032#define VALID_MINOR 0x01
33#define VALID_ID 0x02
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080034
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080035void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
Tilman Schmidt01371502006-04-10 22:55:11 -070036 size_t len, const unsigned char *buf)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080037{
38 unsigned char outbuf[80];
Tilman Schmidt784d5852006-04-10 22:55:04 -070039 unsigned char c;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080040 size_t space = sizeof outbuf - 1;
41 unsigned char *out = outbuf;
Tilman Schmidt01371502006-04-10 22:55:11 -070042 size_t numin = len;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080043
Tilman Schmidt01371502006-04-10 22:55:11 -070044 while (numin--) {
Tilman Schmidt784d5852006-04-10 22:55:04 -070045 c = *buf++;
46 if (c == '~' || c == '^' || c == '\\') {
Tilman Schmidt01371502006-04-10 22:55:11 -070047 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -070048 break;
49 *out++ = '\\';
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080050 }
Tilman Schmidt784d5852006-04-10 22:55:04 -070051 if (c & 0x80) {
Tilman Schmidt01371502006-04-10 22:55:11 -070052 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -070053 break;
54 *out++ = '~';
55 c ^= 0x80;
56 }
57 if (c < 0x20 || c == 0x7f) {
Tilman Schmidt01371502006-04-10 22:55:11 -070058 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -070059 break;
60 *out++ = '^';
61 c ^= 0x40;
62 }
Tilman Schmidt01371502006-04-10 22:55:11 -070063 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -070064 break;
65 *out++ = c;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080066 }
67 *out = 0;
68
Tilman Schmidt784d5852006-04-10 22:55:04 -070069 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080070}
71EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
72
73static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
74{
75 int r;
76
77 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
78 cs->control_state = flags;
79 if (r < 0)
80 return r;
81
82 if (delay) {
83 set_current_state(TASK_INTERRUPTIBLE);
84 schedule_timeout(delay * HZ / 1000);
85 }
86
87 return 0;
88}
89
90int gigaset_enterconfigmode(struct cardstate *cs)
91{
92 int i, r;
93
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080094 cs->control_state = TIOCM_RTS; //FIXME
95
96 r = setflags(cs, TIOCM_DTR, 200);
97 if (r < 0)
98 goto error;
99 r = setflags(cs, 0, 200);
100 if (r < 0)
101 goto error;
102 for (i = 0; i < 5; ++i) {
103 r = setflags(cs, TIOCM_RTS, 100);
104 if (r < 0)
105 goto error;
106 r = setflags(cs, 0, 100);
107 if (r < 0)
108 goto error;
109 }
110 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
111 if (r < 0)
112 goto error;
113
114 return 0;
115
116error:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700117 dev_err(cs->dev, "error %d on setuartbits\n", -r);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800118 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
119 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
120
121 return -1; //r
122}
123
124static int test_timeout(struct at_state_t *at_state)
125{
126 if (!at_state->timer_expires)
127 return 0;
128
129 if (--at_state->timer_expires) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700130 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
131 at_state, at_state->timer_expires);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800132 return 0;
133 }
134
135 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700136 at_state->timer_index, NULL)) {
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800137 //FIXME what should we do?
138 }
139
140 return 1;
141}
142
143static void timer_tick(unsigned long data)
144{
145 struct cardstate *cs = (struct cardstate *) data;
146 unsigned long flags;
147 unsigned channel;
148 struct at_state_t *at_state;
149 int timeout = 0;
150
151 spin_lock_irqsave(&cs->lock, flags);
152
153 for (channel = 0; channel < cs->channels; ++channel)
154 if (test_timeout(&cs->bcs[channel].at_state))
155 timeout = 1;
156
157 if (test_timeout(&cs->at_state))
158 timeout = 1;
159
160 list_for_each_entry(at_state, &cs->temp_at_states, list)
161 if (test_timeout(at_state))
162 timeout = 1;
163
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700164 if (cs->running) {
Tilman Schmidtec81b5e62006-04-10 22:55:03 -0700165 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800166 if (timeout) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700167 gig_dbg(DEBUG_CMD, "scheduling timeout");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800168 tasklet_schedule(&cs->event_tasklet);
169 }
170 }
171
172 spin_unlock_irqrestore(&cs->lock, flags);
173}
174
175int gigaset_get_channel(struct bc_state *bcs)
176{
177 unsigned long flags;
178
179 spin_lock_irqsave(&bcs->cs->lock, flags);
Tilman Schmidte468c042008-02-06 01:38:29 -0800180 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700181 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
182 bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800183 spin_unlock_irqrestore(&bcs->cs->lock, flags);
184 return 0;
185 }
186 ++bcs->use_count;
187 bcs->busy = 1;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700188 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800189 spin_unlock_irqrestore(&bcs->cs->lock, flags);
190 return 1;
191}
192
193void gigaset_free_channel(struct bc_state *bcs)
194{
195 unsigned long flags;
196
197 spin_lock_irqsave(&bcs->cs->lock, flags);
198 if (!bcs->busy) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700199 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800200 spin_unlock_irqrestore(&bcs->cs->lock, flags);
201 return;
202 }
203 --bcs->use_count;
204 bcs->busy = 0;
Tilman Schmidte468c042008-02-06 01:38:29 -0800205 module_put(bcs->cs->driver->owner);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700206 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800207 spin_unlock_irqrestore(&bcs->cs->lock, flags);
208}
209
210int gigaset_get_channels(struct cardstate *cs)
211{
212 unsigned long flags;
213 int i;
214
215 spin_lock_irqsave(&cs->lock, flags);
216 for (i = 0; i < cs->channels; ++i)
217 if (cs->bcs[i].use_count) {
218 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700219 gig_dbg(DEBUG_ANY, "could not allocate all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800220 return 0;
221 }
222 for (i = 0; i < cs->channels; ++i)
223 ++cs->bcs[i].use_count;
224 spin_unlock_irqrestore(&cs->lock, flags);
225
Tilman Schmidt784d5852006-04-10 22:55:04 -0700226 gig_dbg(DEBUG_ANY, "allocated all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800227
228 return 1;
229}
230
231void gigaset_free_channels(struct cardstate *cs)
232{
233 unsigned long flags;
234 int i;
235
Tilman Schmidt784d5852006-04-10 22:55:04 -0700236 gig_dbg(DEBUG_ANY, "unblocking all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800237 spin_lock_irqsave(&cs->lock, flags);
238 for (i = 0; i < cs->channels; ++i)
239 --cs->bcs[i].use_count;
240 spin_unlock_irqrestore(&cs->lock, flags);
241}
242
243void gigaset_block_channels(struct cardstate *cs)
244{
245 unsigned long flags;
246 int i;
247
Tilman Schmidt784d5852006-04-10 22:55:04 -0700248 gig_dbg(DEBUG_ANY, "blocking all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800249 spin_lock_irqsave(&cs->lock, flags);
250 for (i = 0; i < cs->channels; ++i)
251 ++cs->bcs[i].use_count;
252 spin_unlock_irqrestore(&cs->lock, flags);
253}
254
255static void clear_events(struct cardstate *cs)
256{
257 struct event_t *ev;
258 unsigned head, tail;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700259 unsigned long flags;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800260
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700261 spin_lock_irqsave(&cs->ev_lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800262
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700263 head = cs->ev_head;
264 tail = cs->ev_tail;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800265
266 while (tail != head) {
267 ev = cs->events + head;
268 kfree(ev->ptr);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800269 head = (head + 1) % MAX_EVENTS;
270 }
271
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700272 cs->ev_head = tail;
273
274 spin_unlock_irqrestore(&cs->ev_lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800275}
276
277struct event_t *gigaset_add_event(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700278 struct at_state_t *at_state, int type,
279 void *ptr, int parameter, void *arg)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800280{
281 unsigned long flags;
282 unsigned next, tail;
283 struct event_t *event = NULL;
284
285 spin_lock_irqsave(&cs->ev_lock, flags);
286
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700287 tail = cs->ev_tail;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800288 next = (tail + 1) % MAX_EVENTS;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700289 if (unlikely(next == cs->ev_head))
Tilman Schmidt50027792008-07-23 21:28:27 -0700290 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800291 else {
292 event = cs->events + tail;
293 event->type = type;
294 event->at_state = at_state;
295 event->cid = -1;
296 event->ptr = ptr;
297 event->arg = arg;
298 event->parameter = parameter;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700299 cs->ev_tail = next;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800300 }
301
302 spin_unlock_irqrestore(&cs->ev_lock, flags);
303
304 return event;
305}
306EXPORT_SYMBOL_GPL(gigaset_add_event);
307
308static void free_strings(struct at_state_t *at_state)
309{
310 int i;
311
312 for (i = 0; i < STR_NUM; ++i) {
313 kfree(at_state->str_var[i]);
314 at_state->str_var[i] = NULL;
315 }
316}
317
318static void clear_at_state(struct at_state_t *at_state)
319{
320 free_strings(at_state);
321}
322
323static void dealloc_at_states(struct cardstate *cs)
324{
325 struct at_state_t *cur, *next;
326
327 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
328 list_del(&cur->list);
329 free_strings(cur);
330 kfree(cur);
331 }
332}
333
334static void gigaset_freebcs(struct bc_state *bcs)
335{
336 int i;
337
Tilman Schmidt784d5852006-04-10 22:55:04 -0700338 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800339 if (!bcs->cs->ops->freebcshw(bcs)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700340 gig_dbg(DEBUG_INIT, "failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800341 }
342
Tilman Schmidt784d5852006-04-10 22:55:04 -0700343 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800344 clear_at_state(&bcs->at_state);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700345 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800346
347 if (bcs->skb)
348 dev_kfree_skb(bcs->skb);
349 for (i = 0; i < AT_NUM; ++i) {
350 kfree(bcs->commands[i]);
351 bcs->commands[i] = NULL;
352 }
353}
354
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700355static struct cardstate *alloc_cs(struct gigaset_driver *drv)
356{
357 unsigned long flags;
358 unsigned i;
Tilman Schmidte468c042008-02-06 01:38:29 -0800359 struct cardstate *cs;
Tilman Schmidte702ff02007-01-26 00:56:56 -0800360 struct cardstate *ret = NULL;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700361
362 spin_lock_irqsave(&drv->lock, flags);
Tilman Schmidte468c042008-02-06 01:38:29 -0800363 if (drv->blocked)
364 goto exit;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700365 for (i = 0; i < drv->minors; ++i) {
Tilman Schmidte468c042008-02-06 01:38:29 -0800366 cs = drv->cs + i;
367 if (!(cs->flags & VALID_MINOR)) {
368 cs->flags = VALID_MINOR;
369 ret = cs;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700370 break;
Tilman Schmidte702ff02007-01-26 00:56:56 -0800371 }
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700372 }
Tilman Schmidte468c042008-02-06 01:38:29 -0800373exit:
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700374 spin_unlock_irqrestore(&drv->lock, flags);
375 return ret;
376}
377
378static void free_cs(struct cardstate *cs)
379{
Tilman Schmidte468c042008-02-06 01:38:29 -0800380 cs->flags = 0;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700381}
382
383static void make_valid(struct cardstate *cs, unsigned mask)
384{
385 unsigned long flags;
386 struct gigaset_driver *drv = cs->driver;
387 spin_lock_irqsave(&drv->lock, flags);
Tilman Schmidte468c042008-02-06 01:38:29 -0800388 cs->flags |= mask;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700389 spin_unlock_irqrestore(&drv->lock, flags);
390}
391
392static void make_invalid(struct cardstate *cs, unsigned mask)
393{
394 unsigned long flags;
395 struct gigaset_driver *drv = cs->driver;
396 spin_lock_irqsave(&drv->lock, flags);
Tilman Schmidte468c042008-02-06 01:38:29 -0800397 cs->flags &= ~mask;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700398 spin_unlock_irqrestore(&drv->lock, flags);
399}
400
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800401void gigaset_freecs(struct cardstate *cs)
402{
403 int i;
404 unsigned long flags;
405
406 if (!cs)
407 return;
408
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700409 mutex_lock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800410
411 if (!cs->bcs)
412 goto f_cs;
413 if (!cs->inbuf)
414 goto f_bcs;
415
416 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700417 cs->running = 0;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700418 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
419 not rescheduled below */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800420
421 tasklet_kill(&cs->event_tasklet);
422 del_timer_sync(&cs->timer);
423
424 switch (cs->cs_init) {
425 default:
Hansjoerg Lipp3dda4e32006-04-22 18:43:00 +0200426 /* clear device sysfs */
427 gigaset_free_dev_sysfs(cs);
428
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800429 gigaset_if_free(cs);
430
Tilman Schmidt784d5852006-04-10 22:55:04 -0700431 gig_dbg(DEBUG_INIT, "clearing hw");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800432 cs->ops->freecshw(cs);
433
434 //FIXME cmdbuf
435
436 /* fall through */
437 case 2: /* error in initcshw */
438 /* Deregister from LL */
439 make_invalid(cs, VALID_ID);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700440 gig_dbg(DEBUG_INIT, "clearing iif");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800441 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
442
443 /* fall through */
444 case 1: /* error when regestering to LL */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700445 gig_dbg(DEBUG_INIT, "clearing at_state");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800446 clear_at_state(&cs->at_state);
447 dealloc_at_states(cs);
448
449 /* fall through */
450 case 0: /* error in one call to initbcs */
451 for (i = 0; i < cs->channels; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700452 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800453 gigaset_freebcs(cs->bcs + i);
454 }
455
456 clear_events(cs);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700457 gig_dbg(DEBUG_INIT, "freeing inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800458 kfree(cs->inbuf);
459 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700460f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800461 kfree(cs->bcs);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700462f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700463 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800464 free_cs(cs);
465}
466EXPORT_SYMBOL_GPL(gigaset_freecs);
467
468void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700469 struct cardstate *cs, int cid)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800470{
471 int i;
472
473 INIT_LIST_HEAD(&at_state->list);
474 at_state->waiting = 0;
475 at_state->getstring = 0;
476 at_state->pending_commands = 0;
477 at_state->timer_expires = 0;
478 at_state->timer_active = 0;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700479 at_state->timer_index = 0;
480 at_state->seq_index = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800481 at_state->ConState = 0;
482 for (i = 0; i < STR_NUM; ++i)
483 at_state->str_var[i] = NULL;
484 at_state->int_var[VAR_ZDLE] = 0;
485 at_state->int_var[VAR_ZCTP] = -1;
486 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
487 at_state->cs = cs;
488 at_state->bcs = bcs;
489 at_state->cid = cid;
490 if (!cid)
491 at_state->replystruct = cs->tabnocid;
492 else
493 at_state->replystruct = cs->tabcid;
494}
495
496
497static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700498 struct cardstate *cs, int inputstate)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800499/* inbuf->read must be allocated before! */
500{
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800501 inbuf->head = 0;
502 inbuf->tail = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800503 inbuf->cs = cs;
504 inbuf->bcs = bcs; /*base driver: NULL*/
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800505 inbuf->rcvbuf = NULL;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800506 inbuf->inputstate = inputstate;
507}
508
Tilman Schmidt714e8232006-04-10 22:55:09 -0700509/* append received bytes to inbuf */
510int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
511 unsigned numbytes)
512{
513 unsigned n, head, tail, bytesleft;
514
515 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
516
517 if (!numbytes)
518 return 0;
519
520 bytesleft = numbytes;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800521 tail = inbuf->tail;
522 head = inbuf->head;
Tilman Schmidt714e8232006-04-10 22:55:09 -0700523 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
524
525 while (bytesleft) {
526 if (head > tail)
527 n = head - 1 - tail;
528 else if (head == 0)
529 n = (RBUFSIZE-1) - tail;
530 else
531 n = RBUFSIZE - tail;
532 if (!n) {
533 dev_err(inbuf->cs->dev,
Joe Perches898eb712007-10-18 03:06:30 -0700534 "buffer overflow (%u bytes lost)\n",
535 bytesleft);
Tilman Schmidt714e8232006-04-10 22:55:09 -0700536 break;
537 }
538 if (n > bytesleft)
539 n = bytesleft;
540 memcpy(inbuf->data + tail, src, n);
541 bytesleft -= n;
542 tail = (tail + n) % RBUFSIZE;
543 src += n;
544 }
545 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800546 inbuf->tail = tail;
Tilman Schmidt714e8232006-04-10 22:55:09 -0700547 return numbytes != bytesleft;
548}
549EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
550
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800551/* Initialize the b-channel structure */
552static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700553 struct cardstate *cs, int channel)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800554{
555 int i;
556
557 bcs->tx_skb = NULL; //FIXME -> hw part
558
559 skb_queue_head_init(&bcs->squeue);
560
561 bcs->corrupted = 0;
562 bcs->trans_down = 0;
563 bcs->trans_up = 0;
564
Tilman Schmidt784d5852006-04-10 22:55:04 -0700565 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800566 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
567
568 bcs->rcvbytes = 0;
569
570#ifdef CONFIG_GIGASET_DEBUG
571 bcs->emptycount = 0;
572#endif
573
Tilman Schmidt784d5852006-04-10 22:55:04 -0700574 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800575 bcs->fcs = PPP_INITFCS;
576 bcs->inputstate = 0;
577 if (cs->ignoreframes) {
578 bcs->inputstate |= INS_skip_frame;
579 bcs->skb = NULL;
580 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
581 skb_reserve(bcs->skb, HW_HDR_LEN);
582 else {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800583 pr_err("out of memory\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800584 bcs->inputstate |= INS_skip_frame;
585 }
586
587 bcs->channel = channel;
588 bcs->cs = cs;
589
590 bcs->chstate = 0;
591 bcs->use_count = 1;
592 bcs->busy = 0;
593 bcs->ignore = cs->ignoreframes;
594
595 for (i = 0; i < AT_NUM; ++i)
596 bcs->commands[i] = NULL;
597
Tilman Schmidt784d5852006-04-10 22:55:04 -0700598 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800599 if (cs->ops->initbcshw(bcs))
600 return bcs;
601
Tilman Schmidt784d5852006-04-10 22:55:04 -0700602 gig_dbg(DEBUG_INIT, " failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800603
Tilman Schmidt784d5852006-04-10 22:55:04 -0700604 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800605 if (bcs->skb)
606 dev_kfree_skb(bcs->skb);
607
608 return NULL;
609}
610
611/* gigaset_initcs
612 * Allocate and initialize cardstate structure for Gigaset driver
613 * Calls hardware dependent gigaset_initcshw() function
614 * Calls B channel initialization function gigaset_initbcs() for each B channel
615 * parameters:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700616 * drv hardware driver the device belongs to
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800617 * channels number of B channels supported by device
Tilman Schmidt917f5082006-04-10 22:55:00 -0700618 * onechannel !=0: B channel data and AT commands share one
619 * communication channel
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800620 * ==0: B channels have separate communication channels
621 * ignoreframes number of frames to ignore after setting up B channel
622 * cidmode !=0: start in CallID mode
623 * modulename name of driver module (used for I4L registration)
624 * return value:
625 * pointer to cardstate structure
626 */
627struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
628 int onechannel, int ignoreframes,
629 int cidmode, const char *modulename)
630{
631 struct cardstate *cs = NULL;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700632 unsigned long flags;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800633 int i;
634
Tilman Schmidt784d5852006-04-10 22:55:04 -0700635 gig_dbg(DEBUG_INIT, "allocating cs");
Tilman Schmidte702ff02007-01-26 00:56:56 -0800636 if (!(cs = alloc_cs(drv))) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800637 pr_err("maximum number of devices exceeded\n");
Tilman Schmidte702ff02007-01-26 00:56:56 -0800638 return NULL;
639 }
Tilman Schmidte702ff02007-01-26 00:56:56 -0800640
Tilman Schmidt784d5852006-04-10 22:55:04 -0700641 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800642 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
Tilman Schmidte702ff02007-01-26 00:56:56 -0800643 if (!cs->bcs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800644 pr_err("out of memory\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800645 goto error;
Tilman Schmidte702ff02007-01-26 00:56:56 -0800646 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700647 gig_dbg(DEBUG_INIT, "allocating inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800648 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
Tilman Schmidte702ff02007-01-26 00:56:56 -0800649 if (!cs->inbuf) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800650 pr_err("out of memory\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800651 goto error;
Tilman Schmidte702ff02007-01-26 00:56:56 -0800652 }
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800653
654 cs->cs_init = 0;
655 cs->channels = channels;
656 cs->onechannel = onechannel;
657 cs->ignoreframes = ignoreframes;
658 INIT_LIST_HEAD(&cs->temp_at_states);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700659 cs->running = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800660 init_timer(&cs->timer); /* clear next & prev */
661 spin_lock_init(&cs->ev_lock);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700662 cs->ev_tail = 0;
663 cs->ev_head = 0;
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700664
Tilman Schmidt917f5082006-04-10 22:55:00 -0700665 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
666 (unsigned long) cs);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800667 cs->commands_pending = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800668 cs->cur_at_seq = 0;
669 cs->gotfwver = -1;
670 cs->open_count = 0;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700671 cs->dev = NULL;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800672 cs->tty = NULL;
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -0700673 cs->tty_dev = NULL;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700674 cs->cidmode = cidmode != 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800675
676 //if(onechannel) { //FIXME
677 cs->tabnocid = gigaset_tab_nocid_m10x;
678 cs->tabcid = gigaset_tab_cid_m10x;
679 //} else {
680 // cs->tabnocid = gigaset_tab_nocid;
681 // cs->tabcid = gigaset_tab_cid;
682 //}
683
684 init_waitqueue_head(&cs->waitqueue);
685 cs->waiting = 0;
686
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800687 cs->mode = M_UNKNOWN;
688 cs->mstate = MS_UNINITIALIZED;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800689
690 for (i = 0; i < channels; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700691 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
Tilman Schmidte702ff02007-01-26 00:56:56 -0800692 if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800693 pr_err("could not allocate channel %d data\n", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800694 goto error;
Tilman Schmidte702ff02007-01-26 00:56:56 -0800695 }
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800696 }
697
698 ++cs->cs_init;
699
Tilman Schmidt784d5852006-04-10 22:55:04 -0700700 gig_dbg(DEBUG_INIT, "setting up at_state");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800701 spin_lock_init(&cs->lock);
702 gigaset_at_init(&cs->at_state, NULL, cs, 0);
703 cs->dle = 0;
704 cs->cbytes = 0;
705
Tilman Schmidt784d5852006-04-10 22:55:04 -0700706 gig_dbg(DEBUG_INIT, "setting up inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800707 if (onechannel) { //FIXME distinction necessary?
708 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
709 } else
710 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
711
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700712 cs->connected = 0;
713 cs->isdn_up = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800714
Tilman Schmidt784d5852006-04-10 22:55:04 -0700715 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800716 cs->cmdbuf = cs->lastcmdbuf = NULL;
717 spin_lock_init(&cs->cmdlock);
718 cs->curlen = 0;
719 cs->cmdbytes = 0;
720
Tilman Schmidt784d5852006-04-10 22:55:04 -0700721 gig_dbg(DEBUG_INIT, "setting up iif");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800722 if (!gigaset_register_to_LL(cs, modulename)) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800723 pr_err("error registering ISDN device\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800724 goto error;
725 }
726
727 make_valid(cs, VALID_ID);
728 ++cs->cs_init;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700729 gig_dbg(DEBUG_INIT, "setting up hw");
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800730 if (!cs->ops->initcshw(cs))
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800731 goto error;
732
733 ++cs->cs_init;
734
Tilman Schmidt7435f502007-02-12 00:52:24 -0800735 /* set up character device */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800736 gigaset_if_init(cs);
737
Hansjoerg Lipp3dda4e32006-04-22 18:43:00 +0200738 /* set up device sysfs */
739 gigaset_init_dev_sysfs(cs);
740
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700741 spin_lock_irqsave(&cs->lock, flags);
742 cs->running = 1;
743 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtec81b5e62006-04-10 22:55:03 -0700744 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
745 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800746 /* FIXME: can jiffies increase too much until the timer is added?
747 * Same problem(?) with mod_timer() in timer_tick(). */
748 add_timer(&cs->timer);
749
Tilman Schmidt784d5852006-04-10 22:55:04 -0700750 gig_dbg(DEBUG_INIT, "cs initialized");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800751 return cs;
752
Tilman Schmidte702ff02007-01-26 00:56:56 -0800753error:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700754 gig_dbg(DEBUG_INIT, "failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800755 gigaset_freecs(cs);
756 return NULL;
757}
758EXPORT_SYMBOL_GPL(gigaset_initcs);
759
Tilman Schmidt73a88812006-04-22 02:35:30 -0700760/* ReInitialize the b-channel structure on hangup */
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
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800803 cs->mode = M_UNKNOWN;
804 cs->mstate = MS_UNINITIALIZED;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800805
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;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800814 cs->inbuf->head = 0;
815 cs->inbuf->tail = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800816
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;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800829 cs->commands_pending = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800830 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))
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800837 pr_err("could not allocate channel %d data\n", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800838 }
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{
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700850 unsigned long flags;
851
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700852 if (mutex_lock_interruptible(&cs->mutex))
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800853 return 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800854
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700855 spin_lock_irqsave(&cs->lock, flags);
856 cs->connected = 1;
857 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800858
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800859 if (cs->mstate != MS_LOCKED) {
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800860 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
861 cs->ops->baud_rate(cs, B115200);
862 cs->ops->set_line_ctrl(cs, CS8);
863 cs->control_state = TIOCM_DTR|TIOCM_RTS;
864 } else {
865 //FIXME use some saved values?
866 }
867
868 cs->waiting = 1;
869
870 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
871 cs->waiting = 0;
872 //FIXME what should we do?
873 goto error;
874 }
875
Tilman Schmidt784d5852006-04-10 22:55:04 -0700876 gig_dbg(DEBUG_CMD, "scheduling START");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800877 gigaset_schedule_event(cs);
878
879 wait_event(cs->waitqueue, !cs->waiting);
880
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700881 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800882 return 1;
883
884error:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700885 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800886 return 0;
887}
888EXPORT_SYMBOL_GPL(gigaset_start);
889
Tilman Schmidte468c042008-02-06 01:38:29 -0800890/* gigaset_shutdown
891 * check if a device is associated to the cardstate structure and stop it
892 * return value: 0 if ok, -1 if no device was associated
893 */
894int gigaset_shutdown(struct cardstate *cs)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800895{
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700896 mutex_lock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800897
Tilman Schmidt5d49c1012008-03-07 19:47:08 +0100898 if (!(cs->flags & VALID_MINOR)) {
899 mutex_unlock(&cs->mutex);
Tilman Schmidte468c042008-02-06 01:38:29 -0800900 return -1;
Tilman Schmidt5d49c1012008-03-07 19:47:08 +0100901 }
Tilman Schmidte468c042008-02-06 01:38:29 -0800902
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800903 cs->waiting = 1;
904
905 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
906 //FIXME what should we do?
907 goto exit;
908 }
909
Tilman Schmidt784d5852006-04-10 22:55:04 -0700910 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800911 gigaset_schedule_event(cs);
912
Tilman Schmidt2869b232007-02-12 00:52:34 -0800913 wait_event(cs->waitqueue, !cs->waiting);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800914
915 cleanup_cs(cs);
916
917exit:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700918 mutex_unlock(&cs->mutex);
Tilman Schmidte468c042008-02-06 01:38:29 -0800919 return 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800920}
921EXPORT_SYMBOL_GPL(gigaset_shutdown);
922
923void gigaset_stop(struct cardstate *cs)
924{
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700925 mutex_lock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800926
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800927 cs->waiting = 1;
928
929 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
930 //FIXME what should we do?
931 goto exit;
932 }
933
Tilman Schmidt784d5852006-04-10 22:55:04 -0700934 gig_dbg(DEBUG_CMD, "scheduling STOP");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800935 gigaset_schedule_event(cs);
936
Tilman Schmidt2869b232007-02-12 00:52:34 -0800937 wait_event(cs->waitqueue, !cs->waiting);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800938
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800939 cleanup_cs(cs);
940
941exit:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700942 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800943}
944EXPORT_SYMBOL_GPL(gigaset_stop);
945
946static LIST_HEAD(drivers);
Ingo Molnar34af9462006-06-27 02:53:55 -0700947static DEFINE_SPINLOCK(driver_lock);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800948
949struct cardstate *gigaset_get_cs_by_id(int id)
950{
951 unsigned long flags;
Tilman Schmidt35dc8452007-03-29 01:20:34 -0700952 struct cardstate *ret = NULL;
953 struct cardstate *cs;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800954 struct gigaset_driver *drv;
955 unsigned i;
956
957 spin_lock_irqsave(&driver_lock, flags);
958 list_for_each_entry(drv, &drivers, list) {
959 spin_lock(&drv->lock);
960 for (i = 0; i < drv->minors; ++i) {
Tilman Schmidte468c042008-02-06 01:38:29 -0800961 cs = drv->cs + i;
962 if ((cs->flags & VALID_ID) && cs->myid == id) {
963 ret = cs;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800964 break;
Tilman Schmidte468c042008-02-06 01:38:29 -0800965 }
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800966 }
967 spin_unlock(&drv->lock);
968 if (ret)
969 break;
970 }
971 spin_unlock_irqrestore(&driver_lock, flags);
972 return ret;
973}
974
975void gigaset_debugdrivers(void)
976{
977 unsigned long flags;
978 static struct cardstate *cs;
979 struct gigaset_driver *drv;
980 unsigned i;
981
982 spin_lock_irqsave(&driver_lock, flags);
983 list_for_each_entry(drv, &drivers, list) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700984 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800985 spin_lock(&drv->lock);
986 for (i = 0; i < drv->minors; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700987 gig_dbg(DEBUG_DRIVER, " index %u", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800988 cs = drv->cs + i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700989 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
Tilman Schmidte468c042008-02-06 01:38:29 -0800990 gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700991 gig_dbg(DEBUG_DRIVER, " minor_index %u",
992 cs->minor_index);
993 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
994 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800995 }
996 spin_unlock(&drv->lock);
997 }
998 spin_unlock_irqrestore(&driver_lock, flags);
999}
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001000
Adrian Bunk8ca445d2006-04-10 22:55:18 -07001001static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001002{
1003 unsigned long flags;
Tilman Schmidt35dc8452007-03-29 01:20:34 -07001004 struct cardstate *ret = NULL;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001005 struct gigaset_driver *drv;
1006 unsigned index;
1007
1008 spin_lock_irqsave(&driver_lock, flags);
1009 list_for_each_entry(drv, &drivers, list) {
1010 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1011 continue;
1012 index = minor - drv->minor;
1013 spin_lock(&drv->lock);
Tilman Schmidte468c042008-02-06 01:38:29 -08001014 if (drv->cs[index].flags & VALID_MINOR)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001015 ret = drv->cs + index;
1016 spin_unlock(&drv->lock);
1017 if (ret)
1018 break;
1019 }
1020 spin_unlock_irqrestore(&driver_lock, flags);
1021 return ret;
1022}
1023
Adrian Bunk8ca445d2006-04-10 22:55:18 -07001024struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1025{
1026 if (tty->index < 0 || tty->index >= tty->driver->num)
1027 return NULL;
1028 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1029}
1030
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001031void gigaset_freedriver(struct gigaset_driver *drv)
1032{
1033 unsigned long flags;
1034
1035 spin_lock_irqsave(&driver_lock, flags);
1036 list_del(&drv->list);
1037 spin_unlock_irqrestore(&driver_lock, flags);
1038
1039 gigaset_if_freedriver(drv);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001040
1041 kfree(drv->cs);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001042 kfree(drv);
1043}
1044EXPORT_SYMBOL_GPL(gigaset_freedriver);
1045
1046/* gigaset_initdriver
1047 * Allocate and initialize gigaset_driver structure. Initialize interface.
1048 * parameters:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001049 * minor First minor number
1050 * minors Number of minors this driver can handle
1051 * procname Name of the driver
1052 * devname Name of the device files (prefix without minor number)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001053 * return value:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001054 * Pointer to the gigaset_driver structure on success, NULL on failure.
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001055 */
1056struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001057 const char *procname,
1058 const char *devname,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001059 const struct gigaset_ops *ops,
1060 struct module *owner)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001061{
1062 struct gigaset_driver *drv;
1063 unsigned long flags;
1064 unsigned i;
1065
1066 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1067 if (!drv)
1068 return NULL;
Adrian Bunkf4675c72006-04-10 22:55:19 -07001069
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001070 drv->have_tty = 0;
1071 drv->minor = minor;
1072 drv->minors = minors;
1073 spin_lock_init(&drv->lock);
1074 drv->blocked = 0;
1075 drv->ops = ops;
1076 drv->owner = owner;
1077 INIT_LIST_HEAD(&drv->list);
1078
1079 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1080 if (!drv->cs)
Tilman Schmidte702ff02007-01-26 00:56:56 -08001081 goto error;
Adrian Bunkf4675c72006-04-10 22:55:19 -07001082
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001083 for (i = 0; i < minors; ++i) {
Tilman Schmidte468c042008-02-06 01:38:29 -08001084 drv->cs[i].flags = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001085 drv->cs[i].driver = drv;
1086 drv->cs[i].ops = drv->ops;
1087 drv->cs[i].minor_index = i;
Tilman Schmidt5d49c1012008-03-07 19:47:08 +01001088 mutex_init(&drv->cs[i].mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001089 }
1090
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -07001091 gigaset_if_initdriver(drv, procname, devname);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001092
1093 spin_lock_irqsave(&driver_lock, flags);
1094 list_add(&drv->list, &drivers);
1095 spin_unlock_irqrestore(&driver_lock, flags);
1096
1097 return drv;
1098
Tilman Schmidte702ff02007-01-26 00:56:56 -08001099error:
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001100 kfree(drv->cs);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001101 kfree(drv);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001102 return NULL;
1103}
1104EXPORT_SYMBOL_GPL(gigaset_initdriver);
1105
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001106void gigaset_blockdriver(struct gigaset_driver *drv)
1107{
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001108 drv->blocked = 1;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001109}
1110EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1111
1112static int __init gigaset_init_module(void)
1113{
1114 /* in accordance with the principle of least astonishment,
1115 * setting the 'debug' parameter to 1 activates a sensible
1116 * set of default debug levels
1117 */
1118 if (gigaset_debuglevel == 1)
1119 gigaset_debuglevel = DEBUG_DEFAULT;
1120
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08001121 pr_info(DRIVER_DESC "\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001122 return 0;
1123}
1124
1125static void __exit gigaset_exit_module(void)
1126{
1127}
1128
1129module_init(gigaset_init_module);
1130module_exit(gigaset_exit_module);
1131
1132MODULE_AUTHOR(DRIVER_AUTHOR);
1133MODULE_DESCRIPTION(DRIVER_DESC);
1134
1135MODULE_LICENSE("GPL");