common.c

来自「LINUX 2.6.17.4的源码」· C语言 代码 · 共 1,234 行 · 第 1/2 页

C
1,234
字号
/* * Stuff used by all variants of the driver * * Copyright (c) 2001 by Stefan Eilers, *                       Hansjoerg Lipp <hjlipp@web.de>, *                       Tilman Schmidt <tilman@imap.cc>. * * ===================================================================== *	This program is free software; you can redistribute it and/or *	modify it under the terms of the GNU General Public License as *	published by the Free Software Foundation; either version 2 of *	the License, or (at your option) any later version. * ===================================================================== */#include "gigaset.h"#include <linux/ctype.h>#include <linux/module.h>#include <linux/moduleparam.h>/* Version Information */#define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"#define DRIVER_DESC "Driver for Gigaset 307x"/* Module parameters */int gigaset_debuglevel = DEBUG_DEFAULT;EXPORT_SYMBOL_GPL(gigaset_debuglevel);module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);MODULE_PARM_DESC(debug, "debug level");/* driver state flags */#define VALID_MINOR	0x01#define VALID_ID	0x02#define ASSIGNED	0x04/* bitwise byte inversion table */__u8 gigaset_invtab[256] = {	0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,	0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,	0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,	0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,	0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,	0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,	0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,	0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,	0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,	0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,	0x0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evel level, const unsigned char *msg,			size_t len, const unsigned char *buf){	unsigned char outbuf[80];	unsigned char c;	size_t space = sizeof outbuf - 1;	unsigned char *out = outbuf;	size_t numin = len;	while (numin--) {		c = *buf++;		if (c == '~' || c == '^' || c == '\\') {			if (!space--)				break;			*out++ = '\\';		}		if (c & 0x80) {			if (!space--)				break;			*out++ = '~';			c ^= 0x80;		}		if (c < 0x20 || c == 0x7f) {			if (!space--)				break;			*out++ = '^';			c ^= 0x40;		}		if (!space--)			break;		*out++ = c;	}	*out = 0;	gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);}EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);static int setflags(struct cardstate *cs, unsigned flags, unsigned delay){	int r;	r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);	cs->control_state = flags;	if (r < 0)		return r;	if (delay) {		set_current_state(TASK_INTERRUPTIBLE);		schedule_timeout(delay * HZ / 1000);	}	return 0;}int gigaset_enterconfigmode(struct cardstate *cs){	int i, r;	cs->control_state = TIOCM_RTS; //FIXME	r = setflags(cs, TIOCM_DTR, 200);	if (r < 0)		goto error;	r = setflags(cs, 0, 200);	if (r < 0)		goto error;	for (i = 0; i < 5; ++i) {		r = setflags(cs, TIOCM_RTS, 100);		if (r < 0)			goto error;		r = setflags(cs, 0, 100);		if (r < 0)			goto error;	}	r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);	if (r < 0)		goto error;	return 0;error:	dev_err(cs->dev, "error %d on setuartbits\n", -r);	cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?	cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);	return -1; //r}static int test_timeout(struct at_state_t *at_state){	if (!at_state->timer_expires)		return 0;	if (--at_state->timer_expires) {		gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",			at_state, at_state->timer_expires);		return 0;	}	if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,			       at_state->timer_index, NULL)) {		//FIXME what should we do?	}	return 1;}static void timer_tick(unsigned long data){	struct cardstate *cs = (struct cardstate *) data;	unsigned long flags;	unsigned channel;	struct at_state_t *at_state;	int timeout = 0;	spin_lock_irqsave(&cs->lock, flags);	for (channel = 0; channel < cs->channels; ++channel)		if (test_timeout(&cs->bcs[channel].at_state))			timeout = 1;	if (test_timeout(&cs->at_state))		timeout = 1;	list_for_each_entry(at_state, &cs->temp_at_states, list)		if (test_timeout(at_state))			timeout = 1;	if (cs->running) {		mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));		if (timeout) {			gig_dbg(DEBUG_CMD, "scheduling timeout");			tasklet_schedule(&cs->event_tasklet);		}	}	spin_unlock_irqrestore(&cs->lock, flags);}int gigaset_get_channel(struct bc_state *bcs){	unsigned long flags;	spin_lock_irqsave(&bcs->cs->lock, flags);	if (bcs->use_count) {		gig_dbg(DEBUG_ANY, "could not allocate channel %d",			bcs->channel);		spin_unlock_irqrestore(&bcs->cs->lock, flags);		return 0;	}	++bcs->use_count;	bcs->busy = 1;	gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);	spin_unlock_irqrestore(&bcs->cs->lock, flags);	return 1;}void gigaset_free_channel(struct bc_state *bcs){	unsigned long flags;	spin_lock_irqsave(&bcs->cs->lock, flags);	if (!bcs->busy) {		gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);		spin_unlock_irqrestore(&bcs->cs->lock, flags);		return;	}	--bcs->use_count;	bcs->busy = 0;	gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);	spin_unlock_irqrestore(&bcs->cs->lock, flags);}int gigaset_get_channels(struct cardstate *cs){	unsigned long flags;	int i;	spin_lock_irqsave(&cs->lock, flags);	for (i = 0; i < cs->channels; ++i)		if (cs->bcs[i].use_count) {			spin_unlock_irqrestore(&cs->lock, flags);			gig_dbg(DEBUG_ANY, "could not allocate all channels");			return 0;		}	for (i = 0; i < cs->channels; ++i)		++cs->bcs[i].use_count;	spin_unlock_irqrestore(&cs->lock, flags);	gig_dbg(DEBUG_ANY, "allocated all channels");	return 1;}void gigaset_free_channels(struct cardstate *cs){	unsigned long flags;	int i;	gig_dbg(DEBUG_ANY, "unblocking all channels");	spin_lock_irqsave(&cs->lock, flags);	for (i = 0; i < cs->channels; ++i)		--cs->bcs[i].use_count;	spin_unlock_irqrestore(&cs->lock, flags);}void gigaset_block_channels(struct cardstate *cs){	unsigned long flags;	int i;	gig_dbg(DEBUG_ANY, "blocking all channels");	spin_lock_irqsave(&cs->lock, flags);	for (i = 0; i < cs->channels; ++i)		++cs->bcs[i].use_count;	spin_unlock_irqrestore(&cs->lock, flags);}static void clear_events(struct cardstate *cs){	struct event_t *ev;	unsigned head, tail;	unsigned long flags;	spin_lock_irqsave(&cs->ev_lock, flags);	head = cs->ev_head;	tail = cs->ev_tail;	while (tail != head) {		ev = cs->events + head;		kfree(ev->ptr);		head = (head + 1) % MAX_EVENTS;	}	cs->ev_head = tail;	spin_unlock_irqrestore(&cs->ev_lock, flags);}struct event_t *gigaset_add_event(struct cardstate *cs,				  struct at_state_t *at_state, int type,				  void *ptr, int parameter, void *arg){	unsigned long flags;	unsigned next, tail;	struct event_t *event = NULL;	spin_lock_irqsave(&cs->ev_lock, flags);	tail = cs->ev_tail;	next = (tail + 1) % MAX_EVENTS;	if (unlikely(next == cs->ev_head))		err("event queue full");	else {		event = cs->events + tail;		event->type = type;		event->at_state = at_state;		event->cid = -1;		event->ptr = ptr;		event->arg = arg;		event->parameter = parameter;		cs->ev_tail = next;	}	spin_unlock_irqrestore(&cs->ev_lock, flags);	return event;}EXPORT_SYMBOL_GPL(gigaset_add_event);static void free_strings(struct at_state_t *at_state){	int i;	for (i = 0; i < STR_NUM; ++i) {		kfree(at_state->str_var[i]);		at_state->str_var[i] = NULL;	}}static void clear_at_state(struct at_state_t *at_state){	free_strings(at_state);}static void dealloc_at_states(struct cardstate *cs){	struct at_state_t *cur, *next;	list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {		list_del(&cur->list);		free_strings(cur);		kfree(cur);	}}static void gigaset_freebcs(struct bc_state *bcs){	int i;	gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);	if (!bcs->cs->ops->freebcshw(bcs)) {		gig_dbg(DEBUG_INIT, "failed");	}	gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);	clear_at_state(&bcs->at_state);	gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);	if (bcs->skb)		dev_kfree_skb(bcs->skb);	for (i = 0; i < AT_NUM; ++i) {		kfree(bcs->commands[i]);		bcs->commands[i] = NULL;	}}static struct cardstate *alloc_cs(struct gigaset_driver *drv){	unsigned long flags;	unsigned i;	static struct cardstate *ret = NULL;	spin_lock_irqsave(&drv->lock, flags);	for (i = 0; i < drv->minors; ++i) {		if (!(drv->flags[i] & VALID_MINOR)) {			drv->flags[i] = VALID_MINOR;			ret = drv->cs + i;		}		if (ret)			break;	}	spin_unlock_irqrestore(&drv->lock, flags);	return ret;}static void free_cs(struct cardstate *cs){	unsigned long flags;	struct gigaset_driver *drv = cs->driver;	spin_lock_irqsave(&drv->lock, flags);	drv->flags[cs->minor_index] = 0;	spin_unlock_irqrestore(&drv->lock, flags);}static void make_valid(struct cardstate *cs, unsigned mask){	unsigned long flags;	struct gigaset_driver *drv = cs->driver;	spin_lock_irqsave(&drv->lock, flags);	drv->flags[cs->minor_index] |= mask;	spin_unlock_irqrestore(&drv->lock, flags);}static void make_invalid(struct cardstate *cs, unsigned mask){	unsigned long flags;	struct gigaset_driver *drv = cs->driver;	spin_lock_irqsave(&drv->lock, flags);	drv->flags[cs->minor_index] &= ~mask;	spin_unlock_irqrestore(&drv->lock, flags);}void gigaset_freecs(struct cardstate *cs){	int i;	unsigned long flags;	if (!cs)		return;	mutex_lock(&cs->mutex);	if (!cs->bcs)		goto f_cs;	if (!cs->inbuf)		goto f_bcs;	spin_lock_irqsave(&cs->lock, flags);	cs->running = 0;	spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are						     not rescheduled below */	tasklet_kill(&cs->event_tasklet);	del_timer_sync(&cs->timer);	switch (cs->cs_init) {	default:		gigaset_if_free(cs);		gig_dbg(DEBUG_INIT, "clearing hw");		cs->ops->freecshw(cs);		//FIXME cmdbuf		/* fall through */	case 2: /* error in initcshw */		/* Deregister from LL */		make_invalid(cs, VALID_ID);		gig_dbg(DEBUG_INIT, "clearing iif");		gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);		/* fall through */	case 1: /* error when regestering to LL */		gig_dbg(DEBUG_INIT, "clearing at_state");		clear_at_state(&cs->at_state);		dealloc_at_states(cs);		/* fall through */	case 0: /* error in one call to initbcs */		for (i = 0; i < cs->channels; ++i) {			gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);			gigaset_freebcs(cs->bcs + i);		}		clear_events(cs);		gig_dbg(DEBUG_INIT, "freeing inbuf");		kfree(cs->inbuf);	}f_bcs:	gig_dbg(DEBUG_INIT, "freeing bcs[]");	kfree(cs->bcs);f_cs:	gig_dbg(DEBUG_INIT, "freeing cs");	mutex_unlock(&cs->mutex);	free_cs(cs);}EXPORT_SYMBOL_GPL(gigaset_freecs);void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,		     struct cardstate *cs, int cid){	int i;	INIT_LIST_HEAD(&at_state->list);	at_state->waiting = 0;	at_state->getstring = 0;	at_state->pending_commands = 0;	at_state->timer_expires = 0;	at_state->timer_active = 0;	at_state->timer_index = 0;	at_state->seq_index = 0;	at_state->ConState = 0;	for (i = 0; i < STR_NUM; ++i)		at_state->str_var[i] = NULL;	at_state->int_var[VAR_ZDLE] = 0;	at_state->int_var[VAR_ZCTP] = -1;	at_state->int_var[VAR_ZSAU] = ZSAU_NULL;	at_state->cs = cs;	at_state->bcs = bcs;	at_state->cid = cid;	if (!cid)		at_state->replystruct = cs->tabnocid;	else		at_state->replystruct = cs->tabcid;}static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,			       struct cardstate *cs, int inputstate)/* inbuf->read must be allocated before! */{	atomic_set(&inbuf->head, 0);	atomic_set(&inbuf->tail, 0);	inbuf->cs = cs;	inbuf->bcs = bcs; /*base driver: NULL*/	inbuf->rcvbuf = NULL; //FIXME	inbuf->inputstate = inputstate;}/* append received bytes to inbuf */int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,		       unsigned numbytes){	unsigned n, head, tail, bytesleft;	gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);	if (!numbytes)		return 0;	bytesleft = numbytes;	tail = atomic_read(&inbuf->tail);	head = atomic_read(&inbuf->head);	gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);	while (bytesleft) {		if (head > tail)			n = head - 1 - tail;		else if (head == 0)			n = (RBUFSIZE-1) - tail;		else			n = RBUFSIZE - tail;		if (!n) {			dev_err(inbuf->cs->dev,				"buffer overflow (%u bytes lost)", bytesleft);			break;		}		if (n > bytesleft)			n = bytesleft;		memcpy(inbuf->data + tail, src, n);		bytesleft -= n;		tail = (tail + n) % RBUFSIZE;		src += n;	}	gig_dbg(DEBUG_INTR, "setting tail to %u", tail);	atomic_set(&inbuf->tail, tail);	return numbytes != bytesleft;}EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);/* Initialize the b-channel structure */static struct bc_state *gigaset_initbcs(struct bc_state *bcs,					struct cardstate *cs, int channel){	int i;	bcs->tx_skb = NULL; //FIXME -> hw part	skb_queue_head_init(&bcs->squeue);	bcs->corrupted = 0;	bcs->trans_down = 0;	bcs->trans_up = 0;	gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);	gigaset_at_init(&bcs->at_state, bcs, cs, -1);	bcs->rcvbytes = 0;#ifdef CONFIG_GIGASET_DEBUG	bcs->emptycount = 0;#endif	gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);	bcs->fcs = PPP_INITFCS;	bcs->inputstate = 0;	if (cs->ignoreframes) {		bcs->inputstate |= INS_skip_frame;		bcs->skb = NULL;	} else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)		skb_reserve(bcs->skb, HW_HDR_LEN);	else {		dev_warn(cs->dev, "could not allocate skb\n");		bcs->inputstate |= INS_skip_frame;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?