blob: 33dcd8d72b7cb72ced39b702a0e486400c4fcd52 [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
Tilman Schmidt20387242009-10-06 12:18:41 +000025#ifdef CONFIG_GIGASET_DEBUG
26#define DRIVER_DESC_DEBUG " (debug build)"
27#else
28#define DRIVER_DESC_DEBUG ""
29#endif
30
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080031/* Module parameters */
32int gigaset_debuglevel = DEBUG_DEFAULT;
33EXPORT_SYMBOL_GPL(gigaset_debuglevel);
34module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
35MODULE_PARM_DESC(debug, "debug level");
36
Tilman Schmidt70440cf2006-04-10 22:55:14 -070037/* driver state flags */
Tilman Schmidt784d5852006-04-10 22:55:04 -070038#define VALID_MINOR 0x01
39#define VALID_ID 0x02
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080040
Tilman Schmidt1cec9722009-10-06 12:19:01 +000041/**
42 * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging
43 * @level: debugging level.
44 * @msg: message prefix.
45 * @len: number of bytes to dump.
46 * @buf: data to dump.
47 *
48 * If the current debugging level includes one of the bits set in @level,
49 * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio,
50 * prefixed by the text @msg.
51 */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080052void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
Tilman Schmidt01371502006-04-10 22:55:11 -070053 size_t len, const unsigned char *buf)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080054{
55 unsigned char outbuf[80];
Tilman Schmidt784d5852006-04-10 22:55:04 -070056 unsigned char c;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080057 size_t space = sizeof outbuf - 1;
58 unsigned char *out = outbuf;
Tilman Schmidt01371502006-04-10 22:55:11 -070059 size_t numin = len;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080060
Tilman Schmidt01371502006-04-10 22:55:11 -070061 while (numin--) {
Tilman Schmidt784d5852006-04-10 22:55:04 -070062 c = *buf++;
63 if (c == '~' || c == '^' || c == '\\') {
Tilman Schmidt01371502006-04-10 22:55:11 -070064 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -070065 break;
66 *out++ = '\\';
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080067 }
Tilman Schmidt784d5852006-04-10 22:55:04 -070068 if (c & 0x80) {
Tilman Schmidt01371502006-04-10 22:55:11 -070069 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -070070 break;
71 *out++ = '~';
72 c ^= 0x80;
73 }
74 if (c < 0x20 || c == 0x7f) {
Tilman Schmidt01371502006-04-10 22:55:11 -070075 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -070076 break;
77 *out++ = '^';
78 c ^= 0x40;
79 }
Tilman Schmidt01371502006-04-10 22:55:11 -070080 if (!space--)
Tilman Schmidt784d5852006-04-10 22:55:04 -070081 break;
82 *out++ = c;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080083 }
84 *out = 0;
85
Tilman Schmidt784d5852006-04-10 22:55:04 -070086 gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -080087}
88EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
89
90static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
91{
92 int r;
93
94 r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
95 cs->control_state = flags;
96 if (r < 0)
97 return r;
98
99 if (delay) {
100 set_current_state(TASK_INTERRUPTIBLE);
101 schedule_timeout(delay * HZ / 1000);
102 }
103
104 return 0;
105}
106
107int gigaset_enterconfigmode(struct cardstate *cs)
108{
109 int i, r;
110
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800111 cs->control_state = TIOCM_RTS; //FIXME
112
113 r = setflags(cs, TIOCM_DTR, 200);
114 if (r < 0)
115 goto error;
116 r = setflags(cs, 0, 200);
117 if (r < 0)
118 goto error;
119 for (i = 0; i < 5; ++i) {
120 r = setflags(cs, TIOCM_RTS, 100);
121 if (r < 0)
122 goto error;
123 r = setflags(cs, 0, 100);
124 if (r < 0)
125 goto error;
126 }
127 r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
128 if (r < 0)
129 goto error;
130
131 return 0;
132
133error:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700134 dev_err(cs->dev, "error %d on setuartbits\n", -r);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800135 cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
136 cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
137
138 return -1; //r
139}
140
141static int test_timeout(struct at_state_t *at_state)
142{
143 if (!at_state->timer_expires)
144 return 0;
145
146 if (--at_state->timer_expires) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700147 gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
148 at_state, at_state->timer_expires);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800149 return 0;
150 }
151
152 if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700153 at_state->timer_index, NULL)) {
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800154 //FIXME what should we do?
155 }
156
157 return 1;
158}
159
160static void timer_tick(unsigned long data)
161{
162 struct cardstate *cs = (struct cardstate *) data;
163 unsigned long flags;
164 unsigned channel;
165 struct at_state_t *at_state;
166 int timeout = 0;
167
168 spin_lock_irqsave(&cs->lock, flags);
169
170 for (channel = 0; channel < cs->channels; ++channel)
171 if (test_timeout(&cs->bcs[channel].at_state))
172 timeout = 1;
173
174 if (test_timeout(&cs->at_state))
175 timeout = 1;
176
177 list_for_each_entry(at_state, &cs->temp_at_states, list)
178 if (test_timeout(at_state))
179 timeout = 1;
180
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700181 if (cs->running) {
Tilman Schmidtec81b5e62006-04-10 22:55:03 -0700182 mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800183 if (timeout) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700184 gig_dbg(DEBUG_CMD, "scheduling timeout");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800185 tasklet_schedule(&cs->event_tasklet);
186 }
187 }
188
189 spin_unlock_irqrestore(&cs->lock, flags);
190}
191
192int gigaset_get_channel(struct bc_state *bcs)
193{
194 unsigned long flags;
195
196 spin_lock_irqsave(&bcs->cs->lock, flags);
Tilman Schmidte468c042008-02-06 01:38:29 -0800197 if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700198 gig_dbg(DEBUG_ANY, "could not allocate channel %d",
199 bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800200 spin_unlock_irqrestore(&bcs->cs->lock, flags);
201 return 0;
202 }
203 ++bcs->use_count;
204 bcs->busy = 1;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700205 gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800206 spin_unlock_irqrestore(&bcs->cs->lock, flags);
207 return 1;
208}
209
210void gigaset_free_channel(struct bc_state *bcs)
211{
212 unsigned long flags;
213
214 spin_lock_irqsave(&bcs->cs->lock, flags);
215 if (!bcs->busy) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700216 gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800217 spin_unlock_irqrestore(&bcs->cs->lock, flags);
218 return;
219 }
220 --bcs->use_count;
221 bcs->busy = 0;
Tilman Schmidte468c042008-02-06 01:38:29 -0800222 module_put(bcs->cs->driver->owner);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700223 gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800224 spin_unlock_irqrestore(&bcs->cs->lock, flags);
225}
226
227int gigaset_get_channels(struct cardstate *cs)
228{
229 unsigned long flags;
230 int i;
231
232 spin_lock_irqsave(&cs->lock, flags);
233 for (i = 0; i < cs->channels; ++i)
234 if (cs->bcs[i].use_count) {
235 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700236 gig_dbg(DEBUG_ANY, "could not allocate all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800237 return 0;
238 }
239 for (i = 0; i < cs->channels; ++i)
240 ++cs->bcs[i].use_count;
241 spin_unlock_irqrestore(&cs->lock, flags);
242
Tilman Schmidt784d5852006-04-10 22:55:04 -0700243 gig_dbg(DEBUG_ANY, "allocated all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800244
245 return 1;
246}
247
248void gigaset_free_channels(struct cardstate *cs)
249{
250 unsigned long flags;
251 int i;
252
Tilman Schmidt784d5852006-04-10 22:55:04 -0700253 gig_dbg(DEBUG_ANY, "unblocking all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800254 spin_lock_irqsave(&cs->lock, flags);
255 for (i = 0; i < cs->channels; ++i)
256 --cs->bcs[i].use_count;
257 spin_unlock_irqrestore(&cs->lock, flags);
258}
259
260void gigaset_block_channels(struct cardstate *cs)
261{
262 unsigned long flags;
263 int i;
264
Tilman Schmidt784d5852006-04-10 22:55:04 -0700265 gig_dbg(DEBUG_ANY, "blocking all channels");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800266 spin_lock_irqsave(&cs->lock, flags);
267 for (i = 0; i < cs->channels; ++i)
268 ++cs->bcs[i].use_count;
269 spin_unlock_irqrestore(&cs->lock, flags);
270}
271
272static void clear_events(struct cardstate *cs)
273{
274 struct event_t *ev;
275 unsigned head, tail;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700276 unsigned long flags;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800277
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700278 spin_lock_irqsave(&cs->ev_lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800279
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700280 head = cs->ev_head;
281 tail = cs->ev_tail;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800282
283 while (tail != head) {
284 ev = cs->events + head;
285 kfree(ev->ptr);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800286 head = (head + 1) % MAX_EVENTS;
287 }
288
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700289 cs->ev_head = tail;
290
291 spin_unlock_irqrestore(&cs->ev_lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800292}
293
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000294/**
295 * gigaset_add_event() - add event to device event queue
296 * @cs: device descriptor structure.
297 * @at_state: connection state structure.
298 * @type: event type.
299 * @ptr: pointer parameter for event.
300 * @parameter: integer parameter for event.
301 * @arg: pointer parameter for event.
302 *
303 * Allocate an event queue entry from the device's event queue, and set it up
304 * with the parameters given.
305 *
306 * Return value: added event
307 */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800308struct event_t *gigaset_add_event(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700309 struct at_state_t *at_state, int type,
310 void *ptr, int parameter, void *arg)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800311{
312 unsigned long flags;
313 unsigned next, tail;
314 struct event_t *event = NULL;
315
316 spin_lock_irqsave(&cs->ev_lock, flags);
317
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700318 tail = cs->ev_tail;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800319 next = (tail + 1) % MAX_EVENTS;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700320 if (unlikely(next == cs->ev_head))
Tilman Schmidt50027792008-07-23 21:28:27 -0700321 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800322 else {
323 event = cs->events + tail;
324 event->type = type;
325 event->at_state = at_state;
326 event->cid = -1;
327 event->ptr = ptr;
328 event->arg = arg;
329 event->parameter = parameter;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700330 cs->ev_tail = next;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800331 }
332
333 spin_unlock_irqrestore(&cs->ev_lock, flags);
334
335 return event;
336}
337EXPORT_SYMBOL_GPL(gigaset_add_event);
338
339static void free_strings(struct at_state_t *at_state)
340{
341 int i;
342
343 for (i = 0; i < STR_NUM; ++i) {
344 kfree(at_state->str_var[i]);
345 at_state->str_var[i] = NULL;
346 }
347}
348
349static void clear_at_state(struct at_state_t *at_state)
350{
351 free_strings(at_state);
352}
353
354static void dealloc_at_states(struct cardstate *cs)
355{
356 struct at_state_t *cur, *next;
357
358 list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
359 list_del(&cur->list);
360 free_strings(cur);
361 kfree(cur);
362 }
363}
364
365static void gigaset_freebcs(struct bc_state *bcs)
366{
367 int i;
368
Tilman Schmidt784d5852006-04-10 22:55:04 -0700369 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800370 if (!bcs->cs->ops->freebcshw(bcs)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700371 gig_dbg(DEBUG_INIT, "failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800372 }
373
Tilman Schmidt784d5852006-04-10 22:55:04 -0700374 gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800375 clear_at_state(&bcs->at_state);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700376 gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800377
378 if (bcs->skb)
379 dev_kfree_skb(bcs->skb);
380 for (i = 0; i < AT_NUM; ++i) {
381 kfree(bcs->commands[i]);
382 bcs->commands[i] = NULL;
383 }
384}
385
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700386static struct cardstate *alloc_cs(struct gigaset_driver *drv)
387{
388 unsigned long flags;
389 unsigned i;
Tilman Schmidte468c042008-02-06 01:38:29 -0800390 struct cardstate *cs;
Tilman Schmidte702ff02007-01-26 00:56:56 -0800391 struct cardstate *ret = NULL;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700392
393 spin_lock_irqsave(&drv->lock, flags);
Tilman Schmidte468c042008-02-06 01:38:29 -0800394 if (drv->blocked)
395 goto exit;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700396 for (i = 0; i < drv->minors; ++i) {
Tilman Schmidte468c042008-02-06 01:38:29 -0800397 cs = drv->cs + i;
398 if (!(cs->flags & VALID_MINOR)) {
399 cs->flags = VALID_MINOR;
400 ret = cs;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700401 break;
Tilman Schmidte702ff02007-01-26 00:56:56 -0800402 }
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700403 }
Tilman Schmidte468c042008-02-06 01:38:29 -0800404exit:
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700405 spin_unlock_irqrestore(&drv->lock, flags);
406 return ret;
407}
408
409static void free_cs(struct cardstate *cs)
410{
Tilman Schmidte468c042008-02-06 01:38:29 -0800411 cs->flags = 0;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700412}
413
414static void make_valid(struct cardstate *cs, unsigned mask)
415{
416 unsigned long flags;
417 struct gigaset_driver *drv = cs->driver;
418 spin_lock_irqsave(&drv->lock, flags);
Tilman Schmidte468c042008-02-06 01:38:29 -0800419 cs->flags |= mask;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700420 spin_unlock_irqrestore(&drv->lock, flags);
421}
422
423static void make_invalid(struct cardstate *cs, unsigned mask)
424{
425 unsigned long flags;
426 struct gigaset_driver *drv = cs->driver;
427 spin_lock_irqsave(&drv->lock, flags);
Tilman Schmidte468c042008-02-06 01:38:29 -0800428 cs->flags &= ~mask;
Tilman Schmidt70440cf2006-04-10 22:55:14 -0700429 spin_unlock_irqrestore(&drv->lock, flags);
430}
431
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000432/**
433 * gigaset_freecs() - free all associated ressources of a device
434 * @cs: device descriptor structure.
435 *
436 * Stops all tasklets and timers, unregisters the device from all
437 * subsystems it was registered to, deallocates the device structure
438 * @cs and all structures referenced from it.
439 * Operations on the device should be stopped before calling this.
440 */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800441void gigaset_freecs(struct cardstate *cs)
442{
443 int i;
444 unsigned long flags;
445
446 if (!cs)
447 return;
448
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700449 mutex_lock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800450
451 if (!cs->bcs)
452 goto f_cs;
453 if (!cs->inbuf)
454 goto f_bcs;
455
456 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700457 cs->running = 0;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700458 spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
459 not rescheduled below */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800460
461 tasklet_kill(&cs->event_tasklet);
462 del_timer_sync(&cs->timer);
463
464 switch (cs->cs_init) {
465 default:
Hansjoerg Lipp3dda4e32006-04-22 18:43:00 +0200466 /* clear device sysfs */
467 gigaset_free_dev_sysfs(cs);
468
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800469 gigaset_if_free(cs);
470
Tilman Schmidt784d5852006-04-10 22:55:04 -0700471 gig_dbg(DEBUG_INIT, "clearing hw");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800472 cs->ops->freecshw(cs);
473
474 //FIXME cmdbuf
475
476 /* fall through */
477 case 2: /* error in initcshw */
478 /* Deregister from LL */
479 make_invalid(cs, VALID_ID);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700480 gig_dbg(DEBUG_INIT, "clearing iif");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800481 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
482
483 /* fall through */
484 case 1: /* error when regestering to LL */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700485 gig_dbg(DEBUG_INIT, "clearing at_state");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800486 clear_at_state(&cs->at_state);
487 dealloc_at_states(cs);
488
489 /* fall through */
490 case 0: /* error in one call to initbcs */
491 for (i = 0; i < cs->channels; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700492 gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800493 gigaset_freebcs(cs->bcs + i);
494 }
495
496 clear_events(cs);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700497 gig_dbg(DEBUG_INIT, "freeing inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800498 kfree(cs->inbuf);
499 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700500f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800501 kfree(cs->bcs);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700502f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700503 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800504 free_cs(cs);
505}
506EXPORT_SYMBOL_GPL(gigaset_freecs);
507
508void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700509 struct cardstate *cs, int cid)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800510{
511 int i;
512
513 INIT_LIST_HEAD(&at_state->list);
514 at_state->waiting = 0;
515 at_state->getstring = 0;
516 at_state->pending_commands = 0;
517 at_state->timer_expires = 0;
518 at_state->timer_active = 0;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700519 at_state->timer_index = 0;
520 at_state->seq_index = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800521 at_state->ConState = 0;
522 for (i = 0; i < STR_NUM; ++i)
523 at_state->str_var[i] = NULL;
524 at_state->int_var[VAR_ZDLE] = 0;
525 at_state->int_var[VAR_ZCTP] = -1;
526 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
527 at_state->cs = cs;
528 at_state->bcs = bcs;
529 at_state->cid = cid;
530 if (!cid)
531 at_state->replystruct = cs->tabnocid;
532 else
533 at_state->replystruct = cs->tabcid;
534}
535
536
537static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700538 struct cardstate *cs, int inputstate)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800539/* inbuf->read must be allocated before! */
540{
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800541 inbuf->head = 0;
542 inbuf->tail = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800543 inbuf->cs = cs;
544 inbuf->bcs = bcs; /*base driver: NULL*/
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800545 inbuf->rcvbuf = NULL;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800546 inbuf->inputstate = inputstate;
547}
548
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000549/**
550 * gigaset_fill_inbuf() - append received data to input buffer
551 * @inbuf: buffer structure.
552 * @src: received data.
553 * @numbytes: number of bytes received.
554 */
Tilman Schmidt714e8232006-04-10 22:55:09 -0700555int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
556 unsigned numbytes)
557{
558 unsigned n, head, tail, bytesleft;
559
560 gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
561
562 if (!numbytes)
563 return 0;
564
565 bytesleft = numbytes;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800566 tail = inbuf->tail;
567 head = inbuf->head;
Tilman Schmidt714e8232006-04-10 22:55:09 -0700568 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
569
570 while (bytesleft) {
571 if (head > tail)
572 n = head - 1 - tail;
573 else if (head == 0)
574 n = (RBUFSIZE-1) - tail;
575 else
576 n = RBUFSIZE - tail;
577 if (!n) {
578 dev_err(inbuf->cs->dev,
Joe Perches898eb712007-10-18 03:06:30 -0700579 "buffer overflow (%u bytes lost)\n",
580 bytesleft);
Tilman Schmidt714e8232006-04-10 22:55:09 -0700581 break;
582 }
583 if (n > bytesleft)
584 n = bytesleft;
585 memcpy(inbuf->data + tail, src, n);
586 bytesleft -= n;
587 tail = (tail + n) % RBUFSIZE;
588 src += n;
589 }
590 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800591 inbuf->tail = tail;
Tilman Schmidt714e8232006-04-10 22:55:09 -0700592 return numbytes != bytesleft;
593}
594EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
595
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800596/* Initialize the b-channel structure */
597static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700598 struct cardstate *cs, int channel)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800599{
600 int i;
601
602 bcs->tx_skb = NULL; //FIXME -> hw part
603
604 skb_queue_head_init(&bcs->squeue);
605
606 bcs->corrupted = 0;
607 bcs->trans_down = 0;
608 bcs->trans_up = 0;
609
Tilman Schmidt784d5852006-04-10 22:55:04 -0700610 gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800611 gigaset_at_init(&bcs->at_state, bcs, cs, -1);
612
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800613#ifdef CONFIG_GIGASET_DEBUG
614 bcs->emptycount = 0;
615#endif
616
Tilman Schmidt784d5852006-04-10 22:55:04 -0700617 gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800618 bcs->fcs = PPP_INITFCS;
619 bcs->inputstate = 0;
620 if (cs->ignoreframes) {
621 bcs->inputstate |= INS_skip_frame;
622 bcs->skb = NULL;
623 } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
624 skb_reserve(bcs->skb, HW_HDR_LEN);
625 else {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800626 pr_err("out of memory\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800627 bcs->inputstate |= INS_skip_frame;
628 }
629
630 bcs->channel = channel;
631 bcs->cs = cs;
632
633 bcs->chstate = 0;
634 bcs->use_count = 1;
635 bcs->busy = 0;
636 bcs->ignore = cs->ignoreframes;
637
638 for (i = 0; i < AT_NUM; ++i)
639 bcs->commands[i] = NULL;
640
Tilman Schmidt784d5852006-04-10 22:55:04 -0700641 gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800642 if (cs->ops->initbcshw(bcs))
643 return bcs;
644
Tilman Schmidt784d5852006-04-10 22:55:04 -0700645 gig_dbg(DEBUG_INIT, " failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800646
Tilman Schmidt784d5852006-04-10 22:55:04 -0700647 gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800648 if (bcs->skb)
649 dev_kfree_skb(bcs->skb);
650
651 return NULL;
652}
653
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000654/**
655 * gigaset_initcs() - initialize device structure
656 * @drv: hardware driver the device belongs to
657 * @channels: number of B channels supported by device
658 * @onechannel: !=0 if B channel data and AT commands share one
659 * communication channel (M10x),
660 * ==0 if B channels have separate communication channels (base)
661 * @ignoreframes: number of frames to ignore after setting up B channel
662 * @cidmode: !=0: start in CallID mode
663 * @modulename: name of driver module for LL registration
664 *
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800665 * Allocate and initialize cardstate structure for Gigaset driver
666 * Calls hardware dependent gigaset_initcshw() function
667 * Calls B channel initialization function gigaset_initbcs() for each B channel
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000668 *
669 * Return value:
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800670 * pointer to cardstate structure
671 */
672struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
673 int onechannel, int ignoreframes,
674 int cidmode, const char *modulename)
675{
676 struct cardstate *cs = NULL;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700677 unsigned long flags;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800678 int i;
679
Tilman Schmidt784d5852006-04-10 22:55:04 -0700680 gig_dbg(DEBUG_INIT, "allocating cs");
Tilman Schmidte702ff02007-01-26 00:56:56 -0800681 if (!(cs = alloc_cs(drv))) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800682 pr_err("maximum number of devices exceeded\n");
Tilman Schmidte702ff02007-01-26 00:56:56 -0800683 return NULL;
684 }
Tilman Schmidte702ff02007-01-26 00:56:56 -0800685
Tilman Schmidt784d5852006-04-10 22:55:04 -0700686 gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800687 cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
Tilman Schmidte702ff02007-01-26 00:56:56 -0800688 if (!cs->bcs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800689 pr_err("out of memory\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800690 goto error;
Tilman Schmidte702ff02007-01-26 00:56:56 -0800691 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700692 gig_dbg(DEBUG_INIT, "allocating inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800693 cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
Tilman Schmidte702ff02007-01-26 00:56:56 -0800694 if (!cs->inbuf) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800695 pr_err("out of memory\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800696 goto error;
Tilman Schmidte702ff02007-01-26 00:56:56 -0800697 }
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800698
699 cs->cs_init = 0;
700 cs->channels = channels;
701 cs->onechannel = onechannel;
702 cs->ignoreframes = ignoreframes;
703 INIT_LIST_HEAD(&cs->temp_at_states);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700704 cs->running = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800705 init_timer(&cs->timer); /* clear next & prev */
706 spin_lock_init(&cs->ev_lock);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700707 cs->ev_tail = 0;
708 cs->ev_head = 0;
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700709
Tilman Schmidt917f5082006-04-10 22:55:00 -0700710 tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
711 (unsigned long) cs);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800712 cs->commands_pending = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800713 cs->cur_at_seq = 0;
714 cs->gotfwver = -1;
715 cs->open_count = 0;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700716 cs->dev = NULL;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800717 cs->tty = NULL;
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -0700718 cs->tty_dev = NULL;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700719 cs->cidmode = cidmode != 0;
Tilman Schmidt528efc62009-05-13 12:44:17 +0000720 cs->tabnocid = gigaset_tab_nocid;
721 cs->tabcid = gigaset_tab_cid;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800722
723 init_waitqueue_head(&cs->waitqueue);
724 cs->waiting = 0;
725
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800726 cs->mode = M_UNKNOWN;
727 cs->mstate = MS_UNINITIALIZED;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800728
729 for (i = 0; i < channels; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700730 gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
Tilman Schmidte702ff02007-01-26 00:56:56 -0800731 if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800732 pr_err("could not allocate channel %d data\n", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800733 goto error;
Tilman Schmidte702ff02007-01-26 00:56:56 -0800734 }
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800735 }
736
737 ++cs->cs_init;
738
Tilman Schmidt784d5852006-04-10 22:55:04 -0700739 gig_dbg(DEBUG_INIT, "setting up at_state");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800740 spin_lock_init(&cs->lock);
741 gigaset_at_init(&cs->at_state, NULL, cs, 0);
742 cs->dle = 0;
743 cs->cbytes = 0;
744
Tilman Schmidt784d5852006-04-10 22:55:04 -0700745 gig_dbg(DEBUG_INIT, "setting up inbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800746 if (onechannel) { //FIXME distinction necessary?
747 gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
748 } else
749 gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
750
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700751 cs->connected = 0;
752 cs->isdn_up = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800753
Tilman Schmidt784d5852006-04-10 22:55:04 -0700754 gig_dbg(DEBUG_INIT, "setting up cmdbuf");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800755 cs->cmdbuf = cs->lastcmdbuf = NULL;
756 spin_lock_init(&cs->cmdlock);
757 cs->curlen = 0;
758 cs->cmdbytes = 0;
759
Tilman Schmidt784d5852006-04-10 22:55:04 -0700760 gig_dbg(DEBUG_INIT, "setting up iif");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800761 if (!gigaset_register_to_LL(cs, modulename)) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800762 pr_err("error registering ISDN device\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800763 goto error;
764 }
765
766 make_valid(cs, VALID_ID);
767 ++cs->cs_init;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700768 gig_dbg(DEBUG_INIT, "setting up hw");
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800769 if (!cs->ops->initcshw(cs))
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800770 goto error;
771
772 ++cs->cs_init;
773
Tilman Schmidt7435f502007-02-12 00:52:24 -0800774 /* set up character device */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800775 gigaset_if_init(cs);
776
Hansjoerg Lipp3dda4e32006-04-22 18:43:00 +0200777 /* set up device sysfs */
778 gigaset_init_dev_sysfs(cs);
779
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700780 spin_lock_irqsave(&cs->lock, flags);
781 cs->running = 1;
782 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtec81b5e62006-04-10 22:55:03 -0700783 setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
784 cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800785 /* FIXME: can jiffies increase too much until the timer is added?
786 * Same problem(?) with mod_timer() in timer_tick(). */
787 add_timer(&cs->timer);
788
Tilman Schmidt784d5852006-04-10 22:55:04 -0700789 gig_dbg(DEBUG_INIT, "cs initialized");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800790 return cs;
791
Tilman Schmidte702ff02007-01-26 00:56:56 -0800792error:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700793 gig_dbg(DEBUG_INIT, "failed");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800794 gigaset_freecs(cs);
795 return NULL;
796}
797EXPORT_SYMBOL_GPL(gigaset_initcs);
798
Tilman Schmidt73a88812006-04-22 02:35:30 -0700799/* ReInitialize the b-channel structure on hangup */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800800void gigaset_bcs_reinit(struct bc_state *bcs)
801{
802 struct sk_buff *skb;
803 struct cardstate *cs = bcs->cs;
804 unsigned long flags;
805
806 while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
807 dev_kfree_skb(skb);
808
Tilman Schmidt917f5082006-04-10 22:55:00 -0700809 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800810 clear_at_state(&bcs->at_state);
811 bcs->at_state.ConState = 0;
812 bcs->at_state.timer_active = 0;
813 bcs->at_state.timer_expires = 0;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700814 bcs->at_state.cid = -1; /* No CID defined */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800815 spin_unlock_irqrestore(&cs->lock, flags);
816
817 bcs->inputstate = 0;
818
819#ifdef CONFIG_GIGASET_DEBUG
820 bcs->emptycount = 0;
821#endif
822
823 bcs->fcs = PPP_INITFCS;
824 bcs->chstate = 0;
825
826 bcs->ignore = cs->ignoreframes;
827 if (bcs->ignore)
828 bcs->inputstate |= INS_skip_frame;
829
830
831 cs->ops->reinitbcshw(bcs);
832}
833
834static void cleanup_cs(struct cardstate *cs)
835{
836 struct cmdbuf_t *cb, *tcb;
837 int i;
838 unsigned long flags;
839
840 spin_lock_irqsave(&cs->lock, flags);
841
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800842 cs->mode = M_UNKNOWN;
843 cs->mstate = MS_UNINITIALIZED;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800844
845 clear_at_state(&cs->at_state);
846 dealloc_at_states(cs);
847 free_strings(&cs->at_state);
848 gigaset_at_init(&cs->at_state, NULL, cs, 0);
849
850 kfree(cs->inbuf->rcvbuf);
851 cs->inbuf->rcvbuf = NULL;
852 cs->inbuf->inputstate = INS_command;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800853 cs->inbuf->head = 0;
854 cs->inbuf->tail = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800855
856 cb = cs->cmdbuf;
857 while (cb) {
858 tcb = cb;
859 cb = cb->next;
860 kfree(tcb);
861 }
862 cs->cmdbuf = cs->lastcmdbuf = NULL;
863 cs->curlen = 0;
864 cs->cmdbytes = 0;
865 cs->gotfwver = -1;
866 cs->dle = 0;
867 cs->cur_at_seq = 0;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800868 cs->commands_pending = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800869 cs->cbytes = 0;
870
871 spin_unlock_irqrestore(&cs->lock, flags);
872
873 for (i = 0; i < cs->channels; ++i) {
874 gigaset_freebcs(cs->bcs + i);
875 if (!gigaset_initbcs(cs->bcs + i, cs, i))
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800876 pr_err("could not allocate channel %d data\n", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800877 }
878
879 if (cs->waiting) {
880 cs->cmd_result = -ENODEV;
881 cs->waiting = 0;
882 wake_up_interruptible(&cs->waitqueue);
883 }
884}
885
886
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000887/**
888 * gigaset_start() - start device operations
889 * @cs: device descriptor structure.
890 *
891 * Prepares the device for use by setting up communication parameters,
892 * scheduling an EV_START event to initiate device initialization, and
893 * waiting for completion of the initialization.
894 *
895 * Return value:
896 * 1 - success, 0 - error
897 */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800898int gigaset_start(struct cardstate *cs)
899{
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700900 unsigned long flags;
901
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700902 if (mutex_lock_interruptible(&cs->mutex))
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800903 return 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800904
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700905 spin_lock_irqsave(&cs->lock, flags);
906 cs->connected = 1;
907 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800908
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800909 if (cs->mstate != MS_LOCKED) {
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800910 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
911 cs->ops->baud_rate(cs, B115200);
912 cs->ops->set_line_ctrl(cs, CS8);
913 cs->control_state = TIOCM_DTR|TIOCM_RTS;
914 } else {
915 //FIXME use some saved values?
916 }
917
918 cs->waiting = 1;
919
920 if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
921 cs->waiting = 0;
922 //FIXME what should we do?
923 goto error;
924 }
925
Tilman Schmidt784d5852006-04-10 22:55:04 -0700926 gig_dbg(DEBUG_CMD, "scheduling START");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800927 gigaset_schedule_event(cs);
928
929 wait_event(cs->waitqueue, !cs->waiting);
930
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700931 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800932 return 1;
933
934error:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700935 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800936 return 0;
937}
938EXPORT_SYMBOL_GPL(gigaset_start);
939
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000940/**
941 * gigaset_shutdown() - shut down device operations
942 * @cs: device descriptor structure.
943 *
944 * Deactivates the device by scheduling an EV_SHUTDOWN event and
945 * waiting for completion of the shutdown.
946 *
947 * Return value:
948 * 0 - success, -1 - error (no device associated)
Tilman Schmidte468c042008-02-06 01:38:29 -0800949 */
950int gigaset_shutdown(struct cardstate *cs)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800951{
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700952 mutex_lock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800953
Tilman Schmidt5d49c1012008-03-07 19:47:08 +0100954 if (!(cs->flags & VALID_MINOR)) {
955 mutex_unlock(&cs->mutex);
Tilman Schmidte468c042008-02-06 01:38:29 -0800956 return -1;
Tilman Schmidt5d49c1012008-03-07 19:47:08 +0100957 }
Tilman Schmidte468c042008-02-06 01:38:29 -0800958
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800959 cs->waiting = 1;
960
961 if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
962 //FIXME what should we do?
963 goto exit;
964 }
965
Tilman Schmidt784d5852006-04-10 22:55:04 -0700966 gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800967 gigaset_schedule_event(cs);
968
Tilman Schmidt2869b232007-02-12 00:52:34 -0800969 wait_event(cs->waitqueue, !cs->waiting);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800970
971 cleanup_cs(cs);
972
973exit:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700974 mutex_unlock(&cs->mutex);
Tilman Schmidte468c042008-02-06 01:38:29 -0800975 return 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800976}
977EXPORT_SYMBOL_GPL(gigaset_shutdown);
978
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000979/**
980 * gigaset_stop() - stop device operations
981 * @cs: device descriptor structure.
982 *
983 * Stops operations on the device by scheduling an EV_STOP event and
984 * waiting for completion of the shutdown.
985 */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800986void gigaset_stop(struct cardstate *cs)
987{
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700988 mutex_lock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800989
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800990 cs->waiting = 1;
991
992 if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
993 //FIXME what should we do?
994 goto exit;
995 }
996
Tilman Schmidt784d5852006-04-10 22:55:04 -0700997 gig_dbg(DEBUG_CMD, "scheduling STOP");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -0800998 gigaset_schedule_event(cs);
999
Tilman Schmidt2869b232007-02-12 00:52:34 -08001000 wait_event(cs->waitqueue, !cs->waiting);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001001
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001002 cleanup_cs(cs);
1003
1004exit:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -07001005 mutex_unlock(&cs->mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001006}
1007EXPORT_SYMBOL_GPL(gigaset_stop);
1008
1009static LIST_HEAD(drivers);
Ingo Molnar34af9462006-06-27 02:53:55 -07001010static DEFINE_SPINLOCK(driver_lock);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001011
1012struct cardstate *gigaset_get_cs_by_id(int id)
1013{
1014 unsigned long flags;
Tilman Schmidt35dc8452007-03-29 01:20:34 -07001015 struct cardstate *ret = NULL;
1016 struct cardstate *cs;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001017 struct gigaset_driver *drv;
1018 unsigned i;
1019
1020 spin_lock_irqsave(&driver_lock, flags);
1021 list_for_each_entry(drv, &drivers, list) {
1022 spin_lock(&drv->lock);
1023 for (i = 0; i < drv->minors; ++i) {
Tilman Schmidte468c042008-02-06 01:38:29 -08001024 cs = drv->cs + i;
1025 if ((cs->flags & VALID_ID) && cs->myid == id) {
1026 ret = cs;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001027 break;
Tilman Schmidte468c042008-02-06 01:38:29 -08001028 }
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001029 }
1030 spin_unlock(&drv->lock);
1031 if (ret)
1032 break;
1033 }
1034 spin_unlock_irqrestore(&driver_lock, flags);
1035 return ret;
1036}
1037
1038void gigaset_debugdrivers(void)
1039{
1040 unsigned long flags;
1041 static struct cardstate *cs;
1042 struct gigaset_driver *drv;
1043 unsigned i;
1044
1045 spin_lock_irqsave(&driver_lock, flags);
1046 list_for_each_entry(drv, &drivers, list) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001047 gig_dbg(DEBUG_DRIVER, "driver %p", drv);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001048 spin_lock(&drv->lock);
1049 for (i = 0; i < drv->minors; ++i) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001050 gig_dbg(DEBUG_DRIVER, " index %u", i);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001051 cs = drv->cs + i;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001052 gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
Tilman Schmidte468c042008-02-06 01:38:29 -08001053 gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001054 gig_dbg(DEBUG_DRIVER, " minor_index %u",
1055 cs->minor_index);
1056 gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
1057 gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001058 }
1059 spin_unlock(&drv->lock);
1060 }
1061 spin_unlock_irqrestore(&driver_lock, flags);
1062}
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001063
Adrian Bunk8ca445d2006-04-10 22:55:18 -07001064static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001065{
1066 unsigned long flags;
Tilman Schmidt35dc8452007-03-29 01:20:34 -07001067 struct cardstate *ret = NULL;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001068 struct gigaset_driver *drv;
1069 unsigned index;
1070
1071 spin_lock_irqsave(&driver_lock, flags);
1072 list_for_each_entry(drv, &drivers, list) {
1073 if (minor < drv->minor || minor >= drv->minor + drv->minors)
1074 continue;
1075 index = minor - drv->minor;
1076 spin_lock(&drv->lock);
Tilman Schmidte468c042008-02-06 01:38:29 -08001077 if (drv->cs[index].flags & VALID_MINOR)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001078 ret = drv->cs + index;
1079 spin_unlock(&drv->lock);
1080 if (ret)
1081 break;
1082 }
1083 spin_unlock_irqrestore(&driver_lock, flags);
1084 return ret;
1085}
1086
Adrian Bunk8ca445d2006-04-10 22:55:18 -07001087struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1088{
1089 if (tty->index < 0 || tty->index >= tty->driver->num)
1090 return NULL;
1091 return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1092}
1093
Tilman Schmidt1cec9722009-10-06 12:19:01 +00001094/**
1095 * gigaset_freedriver() - free all associated ressources of a driver
1096 * @drv: driver descriptor structure.
1097 *
1098 * Unregisters the driver from the system and deallocates the driver
1099 * structure @drv and all structures referenced from it.
1100 * All devices should be shut down before calling this.
1101 */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001102void gigaset_freedriver(struct gigaset_driver *drv)
1103{
1104 unsigned long flags;
1105
1106 spin_lock_irqsave(&driver_lock, flags);
1107 list_del(&drv->list);
1108 spin_unlock_irqrestore(&driver_lock, flags);
1109
1110 gigaset_if_freedriver(drv);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001111
1112 kfree(drv->cs);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001113 kfree(drv);
1114}
1115EXPORT_SYMBOL_GPL(gigaset_freedriver);
1116
Tilman Schmidt1cec9722009-10-06 12:19:01 +00001117/**
1118 * gigaset_initdriver() - initialize driver structure
1119 * @minor: First minor number
1120 * @minors: Number of minors this driver can handle
1121 * @procname: Name of the driver
1122 * @devname: Name of the device files (prefix without minor number)
1123 *
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001124 * Allocate and initialize gigaset_driver structure. Initialize interface.
Tilman Schmidt1cec9722009-10-06 12:19:01 +00001125 *
1126 * Return value:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001127 * Pointer to the gigaset_driver structure on success, NULL on failure.
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001128 */
1129struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001130 const char *procname,
1131 const char *devname,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001132 const struct gigaset_ops *ops,
1133 struct module *owner)
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001134{
1135 struct gigaset_driver *drv;
1136 unsigned long flags;
1137 unsigned i;
1138
1139 drv = kmalloc(sizeof *drv, GFP_KERNEL);
1140 if (!drv)
1141 return NULL;
Adrian Bunkf4675c72006-04-10 22:55:19 -07001142
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001143 drv->have_tty = 0;
1144 drv->minor = minor;
1145 drv->minors = minors;
1146 spin_lock_init(&drv->lock);
1147 drv->blocked = 0;
1148 drv->ops = ops;
1149 drv->owner = owner;
1150 INIT_LIST_HEAD(&drv->list);
1151
1152 drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1153 if (!drv->cs)
Tilman Schmidte702ff02007-01-26 00:56:56 -08001154 goto error;
Adrian Bunkf4675c72006-04-10 22:55:19 -07001155
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001156 for (i = 0; i < minors; ++i) {
Tilman Schmidte468c042008-02-06 01:38:29 -08001157 drv->cs[i].flags = 0;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001158 drv->cs[i].driver = drv;
1159 drv->cs[i].ops = drv->ops;
1160 drv->cs[i].minor_index = i;
Tilman Schmidt5d49c1012008-03-07 19:47:08 +01001161 mutex_init(&drv->cs[i].mutex);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001162 }
1163
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -07001164 gigaset_if_initdriver(drv, procname, devname);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001165
1166 spin_lock_irqsave(&driver_lock, flags);
1167 list_add(&drv->list, &drivers);
1168 spin_unlock_irqrestore(&driver_lock, flags);
1169
1170 return drv;
1171
Tilman Schmidte702ff02007-01-26 00:56:56 -08001172error:
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001173 kfree(drv->cs);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001174 kfree(drv);
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001175 return NULL;
1176}
1177EXPORT_SYMBOL_GPL(gigaset_initdriver);
1178
Tilman Schmidt1cec9722009-10-06 12:19:01 +00001179/**
1180 * gigaset_blockdriver() - block driver
1181 * @drv: driver descriptor structure.
1182 *
1183 * Prevents the driver from attaching new devices, in preparation for
1184 * deregistration.
1185 */
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001186void gigaset_blockdriver(struct gigaset_driver *drv)
1187{
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001188 drv->blocked = 1;
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001189}
1190EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1191
1192static int __init gigaset_init_module(void)
1193{
1194 /* in accordance with the principle of least astonishment,
1195 * setting the 'debug' parameter to 1 activates a sensible
1196 * set of default debug levels
1197 */
1198 if (gigaset_debuglevel == 1)
1199 gigaset_debuglevel = DEBUG_DEFAULT;
1200
Tilman Schmidt20387242009-10-06 12:18:41 +00001201 pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
Hansjoerg Lipp6fd5ea62006-03-26 01:38:29 -08001202 return 0;
1203}
1204
1205static void __exit gigaset_exit_module(void)
1206{
1207}
1208
1209module_init(gigaset_init_module);
1210module_exit(gigaset_exit_module);
1211
1212MODULE_AUTHOR(DRIVER_AUTHOR);
1213MODULE_DESCRIPTION(DRIVER_DESC);
1214
1215MODULE_LICENSE("GPL");