1/*
2 * Stuff used by all variants of the driver
3 *
4 * Copyright (c) 2001 by Stefan Eilers,
5 *                       Hansjoerg Lipp <hjlipp@web.de>,
6 *                       Tilman Schmidt <tilman@imap.cc>.
7 *
8 * =====================================================================
9 *	This program is free software; you can redistribute it and/or
10 *	modify it under the terms of the GNU General Public License as
11 *	published by the Free Software Foundation; either version 2 of
12 *	the License, or (at your option) any later version.
13 * =====================================================================
14 */
15
16#include "gigaset.h"
17#include <linux/ctype.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20
21/* Version Information */
22#define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
23#define DRIVER_DESC "Driver for Gigaset 307x"
24
25/* Module parameters */
26int gigaset_debuglevel = DEBUG_DEFAULT;
27EXPORT_SYMBOL_GPL(gigaset_debuglevel);
28module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
29MODULE_PARM_DESC(debug, "debug level");
30
31/* driver state flags */
32#define VALID_MINOR	0x01
33#define VALID_ID	0x02
34#define ASSIGNED	0x04
35
36void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
37			size_t len, const unsigned char *buf)
38{
39	unsigned char outbuf[80];
40	unsigned char c;
41	size_t space = sizeof outbuf - 1;
42	unsigned char *out = outbuf;
43	size_t numin = len;
44
45	while (numin--) {
46		c = *buf++;
47		if (c == '~' || c == '^' || c == '\\') {
48			if (!space--)
49				break;
50			*out++ = '\\';
51		}
52		if (c & 0x80) {
53			if (!space--)
54				break;
55			*out++ = '~';
56			c ^= 0x80;
57		}
58		if (c < 0x20 || c == 0x7f) {
59			if (!space--)
60				break;
61			*out++ = '^';
62			c ^= 0x40;
63		}
64		if (!space--)
65			break;
66		*out++ = c;
67	}
68	*out = 0;
69
70	gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
71}
72EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
73
74static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
75{
76	int r;
77
78	r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
79	cs->control_state = flags;
80	if (r < 0)
81		return r;
82
83	if (delay) {
84		set_current_state(TASK_INTERRUPTIBLE);
85		schedule_timeout(delay * HZ / 1000);
86	}
87
88	return 0;
89}
90
91int gigaset_enterconfigmode(struct cardstate *cs)
92{
93	int i, r;
94
95	cs->control_state = TIOCM_RTS;
96
97	r = setflags(cs, TIOCM_DTR, 200);
98	if (r < 0)
99		goto error;
100	r = setflags(cs, 0, 200);
101	if (r < 0)
102		goto error;
103	for (i = 0; i < 5; ++i) {
104		r = setflags(cs, TIOCM_RTS, 100);
105		if (r < 0)
106			goto error;
107		r = setflags(cs, 0, 100);
108		if (r < 0)
109			goto error;
110	}
111	r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
112	if (r < 0)
113		goto error;
114
115	return 0;
116
117error:
118	dev_err(cs->dev, "error %d on setuartbits\n", -r);
119	cs->control_state = TIOCM_RTS|TIOCM_DTR;
120	cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
121
122	return -1; //r
123}
124
125static int test_timeout(struct at_state_t *at_state)
126{
127	if (!at_state->timer_expires)
128		return 0;
129
130	if (--at_state->timer_expires) {
131		gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
132			at_state, at_state->timer_expires);
133		return 0;
134	}
135
136	if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
137			       at_state->timer_index, NULL)) {
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
164	if (cs->running) {
165		mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
166		if (timeout) {
167			gig_dbg(DEBUG_CMD, "scheduling timeout");
168			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);
180	if (bcs->use_count) {
181		gig_dbg(DEBUG_ANY, "could not allocate channel %d",
182			bcs->channel);
183		spin_unlock_irqrestore(&bcs->cs->lock, flags);
184		return 0;
185	}
186	++bcs->use_count;
187	bcs->busy = 1;
188	gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
189	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) {
199		gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
200		spin_unlock_irqrestore(&bcs->cs->lock, flags);
201		return;
202	}
203	--bcs->use_count;
204	bcs->busy = 0;
205	gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
206	spin_unlock_irqrestore(&bcs->cs->lock, flags);
207}
208
209int gigaset_get_channels(struct cardstate *cs)
210{
211	unsigned long flags;
212	int i;
213
214	spin_lock_irqsave(&cs->lock, flags);
215	for (i = 0; i < cs->channels; ++i)
216		if (cs->bcs[i].use_count) {
217			spin_unlock_irqrestore(&cs->lock, flags);
218			gig_dbg(DEBUG_ANY, "could not allocate all channels");
219			return 0;
220		}
221	for (i = 0; i < cs->channels; ++i)
222		++cs->bcs[i].use_count;
223	spin_unlock_irqrestore(&cs->lock, flags);
224
225	gig_dbg(DEBUG_ANY, "allocated all channels");
226
227	return 1;
228}
229
230void gigaset_free_channels(struct cardstate *cs)
231{
232	unsigned long flags;
233	int i;
234
235	gig_dbg(DEBUG_ANY, "unblocking all channels");
236	spin_lock_irqsave(&cs->lock, flags);
237	for (i = 0; i < cs->channels; ++i)
238		--cs->bcs[i].use_count;
239	spin_unlock_irqrestore(&cs->lock, flags);
240}
241
242void gigaset_block_channels(struct cardstate *cs)
243{
244	unsigned long flags;
245	int i;
246
247	gig_dbg(DEBUG_ANY, "blocking all channels");
248	spin_lock_irqsave(&cs->lock, flags);
249	for (i = 0; i < cs->channels; ++i)
250		++cs->bcs[i].use_count;
251	spin_unlock_irqrestore(&cs->lock, flags);
252}
253
254static void clear_events(struct cardstate *cs)
255{
256	struct event_t *ev;
257	unsigned head, tail;
258	unsigned long flags;
259
260	spin_lock_irqsave(&cs->ev_lock, flags);
261
262	head = cs->ev_head;
263	tail = cs->ev_tail;
264
265	while (tail != head) {
266		ev = cs->events + head;
267		kfree(ev->ptr);
268		head = (head + 1) % MAX_EVENTS;
269	}
270
271	cs->ev_head = tail;
272
273	spin_unlock_irqrestore(&cs->ev_lock, flags);
274}
275
276struct event_t *gigaset_add_event(struct cardstate *cs,
277				  struct at_state_t *at_state, int type,
278				  void *ptr, int parameter, void *arg)
279{
280	unsigned long flags;
281	unsigned next, tail;
282	struct event_t *event = NULL;
283
284	spin_lock_irqsave(&cs->ev_lock, flags);
285
286	tail = cs->ev_tail;
287	next = (tail + 1) % MAX_EVENTS;
288	if (unlikely(next == cs->ev_head))
289		err("event queue full");
290	else {
291		event = cs->events + tail;
292		event->type = type;
293		event->at_state = at_state;
294		event->cid = -1;
295		event->ptr = ptr;
296		event->arg = arg;
297		event->parameter = parameter;
298		cs->ev_tail = next;
299	}
300
301	spin_unlock_irqrestore(&cs->ev_lock, flags);
302
303	return event;
304}
305EXPORT_SYMBOL_GPL(gigaset_add_event);
306
307static void free_strings(struct at_state_t *at_state)
308{
309	int i;
310
311	for (i = 0; i < STR_NUM; ++i) {
312		kfree(at_state->str_var[i]);
313		at_state->str_var[i] = NULL;
314	}
315}
316
317static void clear_at_state(struct at_state_t *at_state)
318{
319	free_strings(at_state);
320}
321
322static void dealloc_at_states(struct cardstate *cs)
323{
324	struct at_state_t *cur, *next;
325
326	list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
327		list_del(&cur->list);
328		free_strings(cur);
329		kfree(cur);
330	}
331}
332
333static void gigaset_freebcs(struct bc_state *bcs)
334{
335	int i;
336
337	gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
338	if (!bcs->cs->ops->freebcshw(bcs)) {
339		gig_dbg(DEBUG_INIT, "failed");
340	}
341
342	gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
343	clear_at_state(&bcs->at_state);
344	gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
345
346	if (bcs->skb)
347		dev_kfree_skb(bcs->skb);
348	for (i = 0; i < AT_NUM; ++i) {
349		kfree(bcs->commands[i]);
350		bcs->commands[i] = NULL;
351	}
352}
353
354static struct cardstate *alloc_cs(struct gigaset_driver *drv)
355{
356	unsigned long flags;
357	unsigned i;
358	struct cardstate *ret = NULL;
359
360	spin_lock_irqsave(&drv->lock, flags);
361	for (i = 0; i < drv->minors; ++i) {
362		if (!(drv->flags[i] & VALID_MINOR)) {
363			if (try_module_get(drv->owner)) {
364				drv->flags[i] = VALID_MINOR;
365				ret = drv->cs + i;
366			}
367			break;
368		}
369	}
370	spin_unlock_irqrestore(&drv->lock, flags);
371	return ret;
372}
373
374static void free_cs(struct cardstate *cs)
375{
376	unsigned long flags;
377	struct gigaset_driver *drv = cs->driver;
378	spin_lock_irqsave(&drv->lock, flags);
379	if (drv->flags[cs->minor_index] & VALID_MINOR)
380		module_put(drv->owner);
381	drv->flags[cs->minor_index] = 0;
382	spin_unlock_irqrestore(&drv->lock, flags);
383}
384
385static void make_valid(struct cardstate *cs, unsigned mask)
386{
387	unsigned long flags;
388	struct gigaset_driver *drv = cs->driver;
389	spin_lock_irqsave(&drv->lock, flags);
390	drv->flags[cs->minor_index] |= mask;
391	spin_unlock_irqrestore(&drv->lock, flags);
392}
393
394static void make_invalid(struct cardstate *cs, unsigned mask)
395{
396	unsigned long flags;
397	struct gigaset_driver *drv = cs->driver;
398	spin_lock_irqsave(&drv->lock, flags);
399	drv->flags[cs->minor_index] &= ~mask;
400	spin_unlock_irqrestore(&drv->lock, flags);
401}
402
403void gigaset_freecs(struct cardstate *cs)
404{
405	int i;
406	unsigned long flags;
407
408	if (!cs)
409		return;
410
411	mutex_lock(&cs->mutex);
412
413	if (!cs->bcs)
414		goto f_cs;
415	if (!cs->inbuf)
416		goto f_bcs;
417
418	spin_lock_irqsave(&cs->lock, flags);
419	cs->running = 0;
420	spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
421						     not rescheduled below */
422
423	tasklet_kill(&cs->event_tasklet);
424	del_timer_sync(&cs->timer);
425
426	switch (cs->cs_init) {
427	default:
428		/* clear device sysfs */
429		gigaset_free_dev_sysfs(cs);
430
431		gigaset_if_free(cs);
432
433		gig_dbg(DEBUG_INIT, "clearing hw");
434		cs->ops->freecshw(cs);
435
436
437		/* fall through */
438	case 2: /* error in initcshw */
439		/* Deregister from LL */
440		make_invalid(cs, VALID_ID);
441		gig_dbg(DEBUG_INIT, "clearing iif");
442		gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
443
444		/* fall through */
445	case 1: /* error when regestering to LL */
446		gig_dbg(DEBUG_INIT, "clearing at_state");
447		clear_at_state(&cs->at_state);
448		dealloc_at_states(cs);
449
450		/* fall through */
451	case 0: /* error in one call to initbcs */
452		for (i = 0; i < cs->channels; ++i) {
453			gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
454			gigaset_freebcs(cs->bcs + i);
455		}
456
457		clear_events(cs);
458		gig_dbg(DEBUG_INIT, "freeing inbuf");
459		kfree(cs->inbuf);
460	}
461f_bcs:	gig_dbg(DEBUG_INIT, "freeing bcs[]");
462	kfree(cs->bcs);
463f_cs:	gig_dbg(DEBUG_INIT, "freeing cs");
464	mutex_unlock(&cs->mutex);
465	free_cs(cs);
466}
467EXPORT_SYMBOL_GPL(gigaset_freecs);
468
469void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
470		     struct cardstate *cs, int cid)
471{
472	int i;
473
474	INIT_LIST_HEAD(&at_state->list);
475	at_state->waiting = 0;
476	at_state->getstring = 0;
477	at_state->pending_commands = 0;
478	at_state->timer_expires = 0;
479	at_state->timer_active = 0;
480	at_state->timer_index = 0;
481	at_state->seq_index = 0;
482	at_state->ConState = 0;
483	for (i = 0; i < STR_NUM; ++i)
484		at_state->str_var[i] = NULL;
485	at_state->int_var[VAR_ZDLE] = 0;
486	at_state->int_var[VAR_ZCTP] = -1;
487	at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
488	at_state->cs = cs;
489	at_state->bcs = bcs;
490	at_state->cid = cid;
491	if (!cid)
492		at_state->replystruct = cs->tabnocid;
493	else
494		at_state->replystruct = cs->tabcid;
495}
496
497
498static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
499			       struct cardstate *cs, int inputstate)
500/* inbuf->read must be allocated before! */
501{
502	atomic_set(&inbuf->head, 0);
503	atomic_set(&inbuf->tail, 0);
504	inbuf->cs = cs;
505	inbuf->bcs = bcs; /*base driver: NULL*/
506	inbuf->rcvbuf = NULL;
507	inbuf->inputstate = inputstate;
508}
509
510/* append received bytes to inbuf */
511int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
512		       unsigned numbytes)
513{
514	unsigned n, head, tail, bytesleft;
515
516	gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
517
518	if (!numbytes)
519		return 0;
520
521	bytesleft = numbytes;
522	tail = atomic_read(&inbuf->tail);
523	head = atomic_read(&inbuf->head);
524	gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
525
526	while (bytesleft) {
527		if (head > tail)
528			n = head - 1 - tail;
529		else if (head == 0)
530			n = (RBUFSIZE-1) - tail;
531		else
532			n = RBUFSIZE - tail;
533		if (!n) {
534			dev_err(inbuf->cs->dev,
535				"buffer overflow (%u bytes lost)", bytesleft);
536			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);
546	atomic_set(&inbuf->tail, tail);
547	return numbytes != bytesleft;
548}
549EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
550
551/* Initialize the b-channel structure */
552static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
553					struct cardstate *cs, int channel)
554{
555	int i;
556
557	bcs->tx_skb = NULL;
558
559	skb_queue_head_init(&bcs->squeue);
560
561	bcs->corrupted = 0;
562	bcs->trans_down = 0;
563	bcs->trans_up = 0;
564
565	gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
566	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
574	gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
575	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 {
583		warn("could not allocate skb");
584		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
598	gig_dbg(DEBUG_INIT, "  setting up bcs[%d]->hw", channel);
599	if (cs->ops->initbcshw(bcs))
600		return bcs;
601
602	gig_dbg(DEBUG_INIT, "  failed");
603
604	gig_dbg(DEBUG_INIT, "  freeing bcs[%d]->skb", channel);
605	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:
616 *	drv		hardware driver the device belongs to
617 *	channels	number of B channels supported by device
618 *	onechannel	!=0: B channel data and AT commands share one
619 *			     communication channel
620 *			==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;
632	unsigned long flags;
633	int i;
634
635	gig_dbg(DEBUG_INIT, "allocating cs");
636	if (!(cs = alloc_cs(drv))) {
637		err("maximum number of devices exceeded");
638		return NULL;
639	}
640	mutex_init(&cs->mutex);
641
642	gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
643	cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
644	if (!cs->bcs) {
645		err("out of memory");
646		goto error;
647	}
648	gig_dbg(DEBUG_INIT, "allocating inbuf");
649	cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
650	if (!cs->inbuf) {
651		err("out of memory");
652		goto error;
653	}
654
655	cs->cs_init = 0;
656	cs->channels = channels;
657	cs->onechannel = onechannel;
658	cs->ignoreframes = ignoreframes;
659	INIT_LIST_HEAD(&cs->temp_at_states);
660	cs->running = 0;
661	init_timer(&cs->timer); /* clear next & prev */
662	spin_lock_init(&cs->ev_lock);
663	cs->ev_tail = 0;
664	cs->ev_head = 0;
665
666	tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
667		     (unsigned long) cs);
668	atomic_set(&cs->commands_pending, 0);
669	cs->cur_at_seq = 0;
670	cs->gotfwver = -1;
671	cs->open_count = 0;
672	cs->dev = NULL;
673	cs->tty = NULL;
674	cs->tty_dev = NULL;
675	cs->cidmode = cidmode != 0;
676
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
687	atomic_set(&cs->mode, M_UNKNOWN);
688	atomic_set(&cs->mstate, MS_UNINITIALIZED);
689
690	for (i = 0; i < channels; ++i) {
691		gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
692		if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
693			err("could not allocate channel %d data", i);
694			goto error;
695		}
696	}
697
698	++cs->cs_init;
699
700	gig_dbg(DEBUG_INIT, "setting up at_state");
701	spin_lock_init(&cs->lock);
702	gigaset_at_init(&cs->at_state, NULL, cs, 0);
703	cs->dle = 0;
704	cs->cbytes = 0;
705
706	gig_dbg(DEBUG_INIT, "setting up inbuf");
707	if (onechannel) {
708		gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
709	} else
710		gigaset_inbuf_init(cs->inbuf, NULL,    cs, INS_command);
711
712	cs->connected = 0;
713	cs->isdn_up = 0;
714
715	gig_dbg(DEBUG_INIT, "setting up cmdbuf");
716	cs->cmdbuf = cs->lastcmdbuf = NULL;
717	spin_lock_init(&cs->cmdlock);
718	cs->curlen = 0;
719	cs->cmdbytes = 0;
720
721	gig_dbg(DEBUG_INIT, "setting up iif");
722	if (!gigaset_register_to_LL(cs, modulename)) {
723		err("register_isdn failed");
724		goto error;
725	}
726
727	make_valid(cs, VALID_ID);
728	++cs->cs_init;
729	gig_dbg(DEBUG_INIT, "setting up hw");
730	if (!cs->ops->initcshw(cs)) {
731		err("could not allocate device specific data");
732		goto error;
733	}
734
735	++cs->cs_init;
736
737	/* set up character device */
738	gigaset_if_init(cs);
739
740	/* set up device sysfs */
741	gigaset_init_dev_sysfs(cs);
742
743	spin_lock_irqsave(&cs->lock, flags);
744	cs->running = 1;
745	spin_unlock_irqrestore(&cs->lock, flags);
746	setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
747	cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
748	add_timer(&cs->timer);
749
750	gig_dbg(DEBUG_INIT, "cs initialized");
751	return cs;
752
753error:
754	gig_dbg(DEBUG_INIT, "failed");
755	gigaset_freecs(cs);
756	return NULL;
757}
758EXPORT_SYMBOL_GPL(gigaset_initcs);
759
760/* ReInitialize the b-channel structure on hangup */
761void 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
770	spin_lock_irqsave(&cs->lock, flags);
771	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;
775	bcs->at_state.cid = -1;			/* No CID defined */
776	spin_unlock_irqrestore(&cs->lock, flags);
777
778	bcs->inputstate = 0;
779
780#ifdef CONFIG_GIGASET_DEBUG
781	bcs->emptycount = 0;
782#endif
783
784	bcs->fcs = PPP_INITFCS;
785	bcs->chstate = 0;
786
787	bcs->ignore = cs->ignoreframes;
788	if (bcs->ignore)
789		bcs->inputstate |= INS_skip_frame;
790
791
792	cs->ops->reinitbcshw(bcs);
793}
794
795static void cleanup_cs(struct cardstate *cs)
796{
797	struct cmdbuf_t *cb, *tcb;
798	int i;
799	unsigned long flags;
800
801	spin_lock_irqsave(&cs->lock, flags);
802
803	atomic_set(&cs->mode, M_UNKNOWN);
804	atomic_set(&cs->mstate, MS_UNINITIALIZED);
805
806	clear_at_state(&cs->at_state);
807	dealloc_at_states(cs);
808	free_strings(&cs->at_state);
809	gigaset_at_init(&cs->at_state, NULL, cs, 0);
810
811	kfree(cs->inbuf->rcvbuf);
812	cs->inbuf->rcvbuf = NULL;
813	cs->inbuf->inputstate = INS_command;
814	atomic_set(&cs->inbuf->head, 0);
815	atomic_set(&cs->inbuf->tail, 0);
816
817	cb = cs->cmdbuf;
818	while (cb) {
819		tcb = cb;
820		cb = cb->next;
821		kfree(tcb);
822	}
823	cs->cmdbuf = cs->lastcmdbuf = NULL;
824	cs->curlen = 0;
825	cs->cmdbytes = 0;
826	cs->gotfwver = -1;
827	cs->dle = 0;
828	cs->cur_at_seq = 0;
829	atomic_set(&cs->commands_pending, 0);
830	cs->cbytes = 0;
831
832	spin_unlock_irqrestore(&cs->lock, flags);
833
834	for (i = 0; i < cs->channels; ++i) {
835		gigaset_freebcs(cs->bcs + i);
836		if (!gigaset_initbcs(cs->bcs + i, cs, i))
837			break;
838	}
839
840	if (cs->waiting) {
841		cs->cmd_result = -ENODEV;
842		cs->waiting = 0;
843		wake_up_interruptible(&cs->waitqueue);
844	}
845}
846
847
848int gigaset_start(struct cardstate *cs)
849{
850	unsigned long flags;
851
852	if (mutex_lock_interruptible(&cs->mutex))
853		return 0;
854
855	spin_lock_irqsave(&cs->lock, flags);
856	cs->connected = 1;
857	spin_unlock_irqrestore(&cs->lock, flags);
858
859	if (atomic_read(&cs->mstate) != MS_LOCKED) {
860		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	}
866
867	cs->waiting = 1;
868
869	if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
870		cs->waiting = 0;
871		goto error;
872	}
873
874	gig_dbg(DEBUG_CMD, "scheduling START");
875	gigaset_schedule_event(cs);
876
877	wait_event(cs->waitqueue, !cs->waiting);
878
879	mutex_unlock(&cs->mutex);
880	return 1;
881
882error:
883	mutex_unlock(&cs->mutex);
884	return 0;
885}
886EXPORT_SYMBOL_GPL(gigaset_start);
887
888void gigaset_shutdown(struct cardstate *cs)
889{
890	mutex_lock(&cs->mutex);
891
892	cs->waiting = 1;
893
894	if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
895		goto exit;
896	}
897
898	gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
899	gigaset_schedule_event(cs);
900
901	wait_event(cs->waitqueue, !cs->waiting);
902
903	cleanup_cs(cs);
904
905exit:
906	mutex_unlock(&cs->mutex);
907}
908EXPORT_SYMBOL_GPL(gigaset_shutdown);
909
910void gigaset_stop(struct cardstate *cs)
911{
912	mutex_lock(&cs->mutex);
913
914	cs->waiting = 1;
915
916	if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
917		goto exit;
918	}
919
920	gig_dbg(DEBUG_CMD, "scheduling STOP");
921	gigaset_schedule_event(cs);
922
923	wait_event(cs->waitqueue, !cs->waiting);
924
925	cleanup_cs(cs);
926
927exit:
928	mutex_unlock(&cs->mutex);
929}
930EXPORT_SYMBOL_GPL(gigaset_stop);
931
932static LIST_HEAD(drivers);
933static DEFINE_SPINLOCK(driver_lock);
934
935struct cardstate *gigaset_get_cs_by_id(int id)
936{
937	unsigned long flags;
938	struct cardstate *ret = NULL;
939	struct cardstate *cs;
940	struct gigaset_driver *drv;
941	unsigned i;
942
943	spin_lock_irqsave(&driver_lock, flags);
944	list_for_each_entry(drv, &drivers, list) {
945		spin_lock(&drv->lock);
946		for (i = 0; i < drv->minors; ++i) {
947			if (drv->flags[i] & VALID_ID) {
948				cs = drv->cs + i;
949				if (cs->myid == id)
950					ret = cs;
951			}
952			if (ret)
953				break;
954		}
955		spin_unlock(&drv->lock);
956		if (ret)
957			break;
958	}
959	spin_unlock_irqrestore(&driver_lock, flags);
960	return ret;
961}
962
963void gigaset_debugdrivers(void)
964{
965	unsigned long flags;
966	static struct cardstate *cs;
967	struct gigaset_driver *drv;
968	unsigned i;
969
970	spin_lock_irqsave(&driver_lock, flags);
971	list_for_each_entry(drv, &drivers, list) {
972		gig_dbg(DEBUG_DRIVER, "driver %p", drv);
973		spin_lock(&drv->lock);
974		for (i = 0; i < drv->minors; ++i) {
975			gig_dbg(DEBUG_DRIVER, "  index %u", i);
976			gig_dbg(DEBUG_DRIVER, "    flags 0x%02x",
977				drv->flags[i]);
978			cs = drv->cs + i;
979			gig_dbg(DEBUG_DRIVER, "    cardstate %p", cs);
980			gig_dbg(DEBUG_DRIVER, "    minor_index %u",
981				cs->minor_index);
982			gig_dbg(DEBUG_DRIVER, "    driver %p", cs->driver);
983			gig_dbg(DEBUG_DRIVER, "    i4l id %d", cs->myid);
984		}
985		spin_unlock(&drv->lock);
986	}
987	spin_unlock_irqrestore(&driver_lock, flags);
988}
989
990static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
991{
992	unsigned long flags;
993	struct cardstate *ret = NULL;
994	struct gigaset_driver *drv;
995	unsigned index;
996
997	spin_lock_irqsave(&driver_lock, flags);
998	list_for_each_entry(drv, &drivers, list) {
999		if (minor < drv->minor || minor >= drv->minor + drv->minors)
1000			continue;
1001		index = minor - drv->minor;
1002		spin_lock(&drv->lock);
1003		if (drv->flags[index] & VALID_MINOR)
1004			ret = drv->cs + index;
1005		spin_unlock(&drv->lock);
1006		if (ret)
1007			break;
1008	}
1009	spin_unlock_irqrestore(&driver_lock, flags);
1010	return ret;
1011}
1012
1013struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
1014{
1015	if (tty->index < 0 || tty->index >= tty->driver->num)
1016		return NULL;
1017	return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
1018}
1019
1020void gigaset_freedriver(struct gigaset_driver *drv)
1021{
1022	unsigned long flags;
1023
1024	spin_lock_irqsave(&driver_lock, flags);
1025	list_del(&drv->list);
1026	spin_unlock_irqrestore(&driver_lock, flags);
1027
1028	gigaset_if_freedriver(drv);
1029
1030	kfree(drv->cs);
1031	kfree(drv->flags);
1032	kfree(drv);
1033}
1034EXPORT_SYMBOL_GPL(gigaset_freedriver);
1035
1036/* gigaset_initdriver
1037 * Allocate and initialize gigaset_driver structure. Initialize interface.
1038 * parameters:
1039 *	minor		First minor number
1040 *	minors		Number of minors this driver can handle
1041 *	procname	Name of the driver
1042 *	devname		Name of the device files (prefix without minor number)
1043 * return value:
1044 *	Pointer to the gigaset_driver structure on success, NULL on failure.
1045 */
1046struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
1047					  const char *procname,
1048					  const char *devname,
1049					  const struct gigaset_ops *ops,
1050					  struct module *owner)
1051{
1052	struct gigaset_driver *drv;
1053	unsigned long flags;
1054	unsigned i;
1055
1056	drv = kmalloc(sizeof *drv, GFP_KERNEL);
1057	if (!drv)
1058		return NULL;
1059
1060	drv->have_tty = 0;
1061	drv->minor = minor;
1062	drv->minors = minors;
1063	spin_lock_init(&drv->lock);
1064	drv->blocked = 0;
1065	drv->ops = ops;
1066	drv->owner = owner;
1067	INIT_LIST_HEAD(&drv->list);
1068
1069	drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
1070	if (!drv->cs)
1071		goto error;
1072
1073	drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
1074	if (!drv->flags)
1075		goto error;
1076
1077	for (i = 0; i < minors; ++i) {
1078		drv->flags[i] = 0;
1079		drv->cs[i].driver = drv;
1080		drv->cs[i].ops = drv->ops;
1081		drv->cs[i].minor_index = i;
1082	}
1083
1084	gigaset_if_initdriver(drv, procname, devname);
1085
1086	spin_lock_irqsave(&driver_lock, flags);
1087	list_add(&drv->list, &drivers);
1088	spin_unlock_irqrestore(&driver_lock, flags);
1089
1090	return drv;
1091
1092error:
1093	kfree(drv->cs);
1094	kfree(drv);
1095	return NULL;
1096}
1097EXPORT_SYMBOL_GPL(gigaset_initdriver);
1098
1099/* For drivers without fixed assignment device<->cardstate (usb) */
1100struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
1101{
1102	unsigned long flags;
1103	struct cardstate *cs = NULL;
1104	unsigned i;
1105
1106	spin_lock_irqsave(&drv->lock, flags);
1107	if (drv->blocked)
1108		goto exit;
1109	for (i = 0; i < drv->minors; ++i) {
1110		if ((drv->flags[i] & VALID_MINOR) &&
1111		    !(drv->flags[i] & ASSIGNED)) {
1112			drv->flags[i] |= ASSIGNED;
1113			cs = drv->cs + i;
1114			break;
1115		}
1116	}
1117exit:
1118	spin_unlock_irqrestore(&drv->lock, flags);
1119	return cs;
1120}
1121EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
1122
1123void gigaset_unassign(struct cardstate *cs)
1124{
1125	unsigned long flags;
1126	unsigned *minor_flags;
1127	struct gigaset_driver *drv;
1128
1129	if (!cs)
1130		return;
1131	drv = cs->driver;
1132	spin_lock_irqsave(&drv->lock, flags);
1133	minor_flags = drv->flags + cs->minor_index;
1134	if (*minor_flags & VALID_MINOR)
1135		*minor_flags &= ~ASSIGNED;
1136	spin_unlock_irqrestore(&drv->lock, flags);
1137}
1138EXPORT_SYMBOL_GPL(gigaset_unassign);
1139
1140void gigaset_blockdriver(struct gigaset_driver *drv)
1141{
1142	unsigned long flags;
1143	spin_lock_irqsave(&drv->lock, flags);
1144	drv->blocked = 1;
1145	spin_unlock_irqrestore(&drv->lock, flags);
1146}
1147EXPORT_SYMBOL_GPL(gigaset_blockdriver);
1148
1149static int __init gigaset_init_module(void)
1150{
1151	/* in accordance with the principle of least astonishment,
1152	 * setting the 'debug' parameter to 1 activates a sensible
1153	 * set of default debug levels
1154	 */
1155	if (gigaset_debuglevel == 1)
1156		gigaset_debuglevel = DEBUG_DEFAULT;
1157
1158	info(DRIVER_AUTHOR);
1159	info(DRIVER_DESC);
1160	return 0;
1161}
1162
1163static void __exit gigaset_exit_module(void)
1164{
1165}
1166
1167module_init(gigaset_init_module);
1168module_exit(gigaset_exit_module);
1169
1170MODULE_AUTHOR(DRIVER_AUTHOR);
1171MODULE_DESCRIPTION(DRIVER_DESC);
1172
1173MODULE_LICENSE("GPL");
1174