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