hysdn_procconf.c revision 733482e445ca4450cf41381b1c95e2b8c7145114
1/* $Id: hysdn_procconf.c,v 1.8.6.4 2001/09/23 22:24:54 kai Exp $
2 *
3 * Linux driver for HYSDN cards, /proc/net filesystem dir and conf functions.
4 *
5 * written by Werner Cornelius (werner@titro.de) for Hypercope GmbH
6 *
7 * Copyright 1999  by Werner Cornelius (werner@titro.de)
8 *
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/poll.h>
16#include <linux/proc_fs.h>
17#include <linux/pci.h>
18#include <linux/smp_lock.h>
19
20#include "hysdn_defs.h"
21
22static char *hysdn_procconf_revision = "$Revision: 1.8.6.4 $";
23
24#define INFO_OUT_LEN 80		/* length of info line including lf */
25
26/********************************************************/
27/* defines and data structure for conf write operations */
28/********************************************************/
29#define CONF_STATE_DETECT 0	/* waiting for detect */
30#define CONF_STATE_CONF   1	/* writing config data */
31#define CONF_STATE_POF    2	/* writing pof data */
32#define CONF_LINE_LEN   255	/* 255 chars max */
33
34struct conf_writedata {
35	hysdn_card *card;	/* card the device is connected to */
36	int buf_size;		/* actual number of bytes in the buffer */
37	int needed_size;	/* needed size when reading pof */
38	int state;		/* actual interface states from above constants */
39	uchar conf_line[CONF_LINE_LEN];		/* buffered conf line */
40	word channel;		/* active channel number */
41	uchar *pof_buffer;	/* buffer when writing pof */
42};
43
44/***********************************************************************/
45/* process_line parses one config line and transfers it to the card if */
46/* necessary.                                                          */
47/* if the return value is negative an error occurred.                   */
48/***********************************************************************/
49static int
50process_line(struct conf_writedata *cnf)
51{
52	uchar *cp = cnf->conf_line;
53	int i;
54
55	if (cnf->card->debug_flags & LOG_CNF_LINE)
56		hysdn_addlog(cnf->card, "conf line: %s", cp);
57
58	if (*cp == '-') {	/* option */
59		cp++;		/* point to option char */
60
61		if (*cp++ != 'c')
62			return (0);	/* option unknown or used */
63		i = 0;		/* start value for channel */
64		while ((*cp <= '9') && (*cp >= '0'))
65			i = i * 10 + *cp++ - '0';	/* get decimal number */
66		if (i > 65535) {
67			if (cnf->card->debug_flags & LOG_CNF_MISC)
68				hysdn_addlog(cnf->card, "conf channel invalid  %d", i);
69			return (-ERR_INV_CHAN);		/* invalid channel */
70		}
71		cnf->channel = i & 0xFFFF;	/* set new channel number */
72		return (0);	/* success */
73	}			/* option */
74	if (*cp == '*') {	/* line to send */
75		if (cnf->card->debug_flags & LOG_CNF_DATA)
76			hysdn_addlog(cnf->card, "conf chan=%d %s", cnf->channel, cp);
77		return (hysdn_tx_cfgline(cnf->card, cnf->conf_line + 1,
78					 cnf->channel));	/* send the line without * */
79	}			/* line to send */
80	return (0);
81}				/* process_line */
82
83/***********************************/
84/* conf file operations and tables */
85/***********************************/
86
87/****************************************************/
88/* write conf file -> boot or send cfg line to card */
89/****************************************************/
90static ssize_t
91hysdn_conf_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
92{
93	struct conf_writedata *cnf;
94	int i;
95	uchar ch, *cp;
96
97	if (!count)
98		return (0);	/* nothing to handle */
99
100	if (!(cnf = file->private_data))
101		return (-EFAULT);	/* should never happen */
102
103	if (cnf->state == CONF_STATE_DETECT) {	/* auto detect cnf or pof data */
104		if (copy_from_user(&ch, buf, 1))	/* get first char for detect */
105			return (-EFAULT);
106
107		if (ch == 0x1A) {
108			/* we detected a pof file */
109			if ((cnf->needed_size = pof_write_open(cnf->card, &cnf->pof_buffer)) <= 0)
110				return (cnf->needed_size);	/* an error occurred -> exit */
111			cnf->buf_size = 0;	/* buffer is empty */
112			cnf->state = CONF_STATE_POF;	/* new state */
113		} else {
114			/* conf data has been detected */
115			cnf->buf_size = 0;	/* buffer is empty */
116			cnf->state = CONF_STATE_CONF;	/* requested conf data write */
117			if (cnf->card->state != CARD_STATE_RUN)
118				return (-ERR_NOT_BOOTED);
119			cnf->conf_line[CONF_LINE_LEN - 1] = 0;	/* limit string length */
120			cnf->channel = 4098;	/* default channel for output */
121		}
122	}			/* state was auto detect */
123	if (cnf->state == CONF_STATE_POF) {	/* pof write active */
124		i = cnf->needed_size - cnf->buf_size;	/* bytes still missing for write */
125		if (i <= 0)
126			return (-EINVAL);	/* size error handling pof */
127
128		if (i < count)
129			count = i;	/* limit requested number of bytes */
130		if (copy_from_user(cnf->pof_buffer + cnf->buf_size, buf, count))
131			return (-EFAULT);	/* error while copying */
132		cnf->buf_size += count;
133
134		if (cnf->needed_size == cnf->buf_size) {
135			cnf->needed_size = pof_write_buffer(cnf->card, cnf->buf_size);	/* write data */
136			if (cnf->needed_size <= 0) {
137				cnf->card->state = CARD_STATE_BOOTERR;	/* show boot error */
138				return (cnf->needed_size);	/* an error occurred */
139			}
140			cnf->buf_size = 0;	/* buffer is empty again */
141		}
142	}
143	/* pof write active */
144	else {			/* conf write active */
145
146		if (cnf->card->state != CARD_STATE_RUN) {
147			if (cnf->card->debug_flags & LOG_CNF_MISC)
148				hysdn_addlog(cnf->card, "cnf write denied -> not booted");
149			return (-ERR_NOT_BOOTED);
150		}
151		i = (CONF_LINE_LEN - 1) - cnf->buf_size;	/* bytes available in buffer */
152		if (i > 0) {
153			/* copy remaining bytes into buffer */
154
155			if (count > i)
156				count = i;	/* limit transfer */
157			if (copy_from_user(cnf->conf_line + cnf->buf_size, buf, count))
158				return (-EFAULT);	/* error while copying */
159
160			i = count;	/* number of chars in buffer */
161			cp = cnf->conf_line + cnf->buf_size;
162			while (i) {
163				/* search for end of line */
164				if ((*cp < ' ') && (*cp != 9))
165					break;	/* end of line found */
166				cp++;
167				i--;
168			}	/* search for end of line */
169
170			if (i) {
171				/* delimiter found */
172				*cp++ = 0;	/* string termination */
173				count -= (i - 1);	/* subtract remaining bytes from count */
174				while ((i) && (*cp < ' ') && (*cp != 9)) {
175					i--;	/* discard next char */
176					count++;	/* mark as read */
177					cp++;	/* next char */
178				}
179				cnf->buf_size = 0;	/* buffer is empty after transfer */
180				if ((i = process_line(cnf)) < 0)	/* handle the line */
181					count = i;	/* return the error */
182			}
183			/* delimiter found */
184			else {
185				cnf->buf_size += count;		/* add chars to string */
186				if (cnf->buf_size >= CONF_LINE_LEN - 1) {
187					if (cnf->card->debug_flags & LOG_CNF_MISC)
188						hysdn_addlog(cnf->card, "cnf line too long %d chars pos %d", cnf->buf_size, count);
189					return (-ERR_CONF_LONG);
190				}
191			}	/* not delimited */
192
193		}
194		/* copy remaining bytes into buffer */
195		else {
196			if (cnf->card->debug_flags & LOG_CNF_MISC)
197				hysdn_addlog(cnf->card, "cnf line too long");
198			return (-ERR_CONF_LONG);
199		}
200	}			/* conf write active */
201
202	return (count);
203}				/* hysdn_conf_write */
204
205/*******************************************/
206/* read conf file -> output card info data */
207/*******************************************/
208static ssize_t
209hysdn_conf_read(struct file *file, char __user *buf, size_t count, loff_t * off)
210{
211	char *cp;
212	int i;
213
214	if (file->f_mode & FMODE_READ) {
215		if (!(cp = file->private_data))
216			return (-EFAULT);	/* should never happen */
217		i = strlen(cp);	/* get total string length */
218		if (*off < i) {
219			/* still bytes to transfer */
220			cp += *off;	/* point to desired data offset */
221			i -= *off;	/* remaining length */
222			if (i > count)
223				i = count;	/* limit length to transfer */
224			if (copy_to_user(buf, cp, i))
225				return (-EFAULT);	/* copy error */
226			*off += i;	/* adjust offset */
227		} else
228			return (0);
229	} else
230		return (-EPERM);	/* no permission to read */
231
232	return (i);
233}				/* hysdn_conf_read */
234
235/******************/
236/* open conf file */
237/******************/
238static int
239hysdn_conf_open(struct inode *ino, struct file *filep)
240{
241	hysdn_card *card;
242	struct proc_dir_entry *pd;
243	struct conf_writedata *cnf;
244	char *cp, *tmp;
245
246	/* now search the addressed card */
247	lock_kernel();
248	card = card_root;
249	while (card) {
250		pd = card->procconf;
251		if (pd == PDE(ino))
252			break;
253		card = card->next;	/* search next entry */
254	}
255	if (!card) {
256		unlock_kernel();
257		return (-ENODEV);	/* device is unknown/invalid */
258	}
259	if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
260		hysdn_addlog(card, "config open for uid=%d gid=%d mode=0x%x",
261			     filep->f_uid, filep->f_gid, filep->f_mode);
262
263	if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
264		/* write only access -> write boot file or conf line */
265
266		if (!(cnf = kmalloc(sizeof(struct conf_writedata), GFP_KERNEL))) {
267			unlock_kernel();
268			return (-EFAULT);
269		}
270		cnf->card = card;
271		cnf->buf_size = 0;	/* nothing buffered */
272		cnf->state = CONF_STATE_DETECT;		/* start auto detect */
273		filep->private_data = cnf;
274
275	} else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
276		/* read access -> output card info data */
277
278		if (!(tmp = (char *) kmalloc(INFO_OUT_LEN * 2 + 2, GFP_KERNEL))) {
279			unlock_kernel();
280			return (-EFAULT);	/* out of memory */
281		}
282		filep->private_data = tmp;	/* start of string */
283
284		/* first output a headline */
285		sprintf(tmp, "id bus slot type irq iobase dp-mem     b-chans fax-chans state device");
286		cp = tmp;	/* start of string */
287		while (*cp)
288			cp++;
289		while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
290			*cp++ = ' ';
291		*cp++ = '\n';
292
293		/* and now the data */
294		sprintf(cp, "%d  %3d %4d %4d %3d 0x%04x 0x%08lx %7d %9d %3d   %s",
295			card->myid,
296			card->bus,
297			PCI_SLOT(card->devfn),
298			card->brdtype,
299			card->irq,
300			card->iobase,
301			card->membase,
302			card->bchans,
303			card->faxchans,
304			card->state,
305			hysdn_net_getname(card));
306		while (*cp)
307			cp++;
308		while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
309			*cp++ = ' ';
310		*cp++ = '\n';
311		*cp = 0;	/* end of string */
312	} else {		/* simultaneous read/write access forbidden ! */
313		unlock_kernel();
314		return (-EPERM);	/* no permission this time */
315	}
316	unlock_kernel();
317	return nonseekable_open(ino, filep);
318}				/* hysdn_conf_open */
319
320/***************************/
321/* close a config file.    */
322/***************************/
323static int
324hysdn_conf_close(struct inode *ino, struct file *filep)
325{
326	hysdn_card *card;
327	struct conf_writedata *cnf;
328	int retval = 0;
329	struct proc_dir_entry *pd;
330
331	lock_kernel();
332	/* search the addressed card */
333	card = card_root;
334	while (card) {
335		pd = card->procconf;
336		if (pd == PDE(ino))
337			break;
338		card = card->next;	/* search next entry */
339	}
340	if (!card) {
341		unlock_kernel();
342		return (-ENODEV);	/* device is unknown/invalid */
343	}
344	if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
345		hysdn_addlog(card, "config close for uid=%d gid=%d mode=0x%x",
346			     filep->f_uid, filep->f_gid, filep->f_mode);
347
348	if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
349		/* write only access -> write boot file or conf line */
350		if (filep->private_data) {
351			cnf = filep->private_data;
352
353			if (cnf->state == CONF_STATE_POF)
354				retval = pof_write_close(cnf->card);	/* close the pof write */
355			kfree(filep->private_data);	/* free allocated memory for buffer */
356
357		}		/* handle write private data */
358	} else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
359		/* read access -> output card info data */
360
361		kfree(filep->private_data);	/* release memory */
362	}
363	unlock_kernel();
364	return (retval);
365}				/* hysdn_conf_close */
366
367/******************************************************/
368/* table for conf filesystem functions defined above. */
369/******************************************************/
370static struct file_operations conf_fops =
371{
372	.llseek         = no_llseek,
373	.read           = hysdn_conf_read,
374	.write          = hysdn_conf_write,
375	.open           = hysdn_conf_open,
376	.release        = hysdn_conf_close,
377};
378
379/*****************************/
380/* hysdn subdir in /proc/net */
381/*****************************/
382struct proc_dir_entry *hysdn_proc_entry = NULL;
383
384/*******************************************************************************/
385/* hysdn_procconf_init is called when the module is loaded and after the cards */
386/* have been detected. The needed proc dir and card config files are created.  */
387/* The log init is called at last.                                             */
388/*******************************************************************************/
389int
390hysdn_procconf_init(void)
391{
392	hysdn_card *card;
393	uchar conf_name[20];
394
395	hysdn_proc_entry = proc_mkdir(PROC_SUBDIR_NAME, proc_net);
396	if (!hysdn_proc_entry) {
397		printk(KERN_ERR "HYSDN: unable to create hysdn subdir\n");
398		return (-1);
399	}
400	card = card_root;	/* point to first card */
401	while (card) {
402
403		sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
404		if ((card->procconf = (void *) create_proc_entry(conf_name,
405					     S_IFREG | S_IRUGO | S_IWUSR,
406					    hysdn_proc_entry)) != NULL) {
407			((struct proc_dir_entry *) card->procconf)->proc_fops = &conf_fops;
408			((struct proc_dir_entry *) card->procconf)->owner = THIS_MODULE;
409			hysdn_proclog_init(card);	/* init the log file entry */
410		}
411		card = card->next;	/* next entry */
412	}
413
414	printk(KERN_NOTICE "HYSDN: procfs Rev. %s initialised\n", hysdn_getrev(hysdn_procconf_revision));
415	return (0);
416}				/* hysdn_procconf_init */
417
418/*************************************************************************************/
419/* hysdn_procconf_release is called when the module is unloaded and before the cards */
420/* resources are released. The module counter is assumed to be 0 !                   */
421/*************************************************************************************/
422void
423hysdn_procconf_release(void)
424{
425	hysdn_card *card;
426	uchar conf_name[20];
427
428	card = card_root;	/* start with first card */
429	while (card) {
430
431		sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
432		if (card->procconf)
433			remove_proc_entry(conf_name, hysdn_proc_entry);
434
435		hysdn_proclog_release(card);	/* init the log file entry */
436
437		card = card->next;	/* point to next card */
438	}
439
440	remove_proc_entry(PROC_SUBDIR_NAME, proc_net);
441}
442