1 /* $Id: hysdn_procconf.c,v 1.8.6.4 2001/09/23 22:24:54 kai Exp $
3 * Linux driver for HYSDN cards, /proc/net filesystem dir and conf functions.
5 * written by Werner Cornelius (werner@titro.de) for Hypercope GmbH
7 * Copyright 1999 by Werner Cornelius (werner@titro.de)
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
14 #include <linux/module.h>
15 #include <linux/version.h>
16 #include <linux/poll.h>
17 #include <linux/proc_fs.h>
18 #include <linux/pci.h>
19 #include <linux/smp_lock.h>
21 #include "hysdn_defs.h"
23 static char *hysdn_procconf_revision = "$Revision: 1.8.6.4 $";
25 #define INFO_OUT_LEN 80 /* length of info line including lf */
27 /********************************************************/
28 /* defines and data structure for conf write operations */
29 /********************************************************/
30 #define CONF_STATE_DETECT 0 /* waiting for detect */
31 #define CONF_STATE_CONF 1 /* writing config data */
32 #define CONF_STATE_POF 2 /* writing pof data */
33 #define CONF_LINE_LEN 255 /* 255 chars max */
35 struct conf_writedata {
36 hysdn_card *card; /* card the device is connected to */
37 int buf_size; /* actual number of bytes in the buffer */
38 int needed_size; /* needed size when reading pof */
39 int state; /* actual interface states from above constants */
40 uchar conf_line[CONF_LINE_LEN]; /* buffered conf line */
41 word channel; /* active channel number */
42 uchar *pof_buffer; /* buffer when writing pof */
45 /***********************************************************************/
46 /* process_line parses one config line and transfers it to the card if */
48 /* if the return value is negative an error occurred. */
49 /***********************************************************************/
51 process_line(struct conf_writedata *cnf)
53 uchar *cp = cnf->conf_line;
56 if (cnf->card->debug_flags & LOG_CNF_LINE)
57 hysdn_addlog(cnf->card, "conf line: %s", cp);
59 if (*cp == '-') { /* option */
60 cp++; /* point to option char */
63 return (0); /* option unknown or used */
64 i = 0; /* start value for channel */
65 while ((*cp <= '9') && (*cp >= '0'))
66 i = i * 10 + *cp++ - '0'; /* get decimal number */
68 if (cnf->card->debug_flags & LOG_CNF_MISC)
69 hysdn_addlog(cnf->card, "conf channel invalid %d", i);
70 return (-ERR_INV_CHAN); /* invalid channel */
72 cnf->channel = i & 0xFFFF; /* set new channel number */
73 return (0); /* success */
75 if (*cp == '*') { /* line to send */
76 if (cnf->card->debug_flags & LOG_CNF_DATA)
77 hysdn_addlog(cnf->card, "conf chan=%d %s", cnf->channel, cp);
78 return (hysdn_tx_cfgline(cnf->card, cnf->conf_line + 1,
79 cnf->channel)); /* send the line without * */
84 /***********************************/
85 /* conf file operations and tables */
86 /***********************************/
88 /****************************************************/
89 /* write conf file -> boot or send cfg line to card */
90 /****************************************************/
92 hysdn_conf_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
94 struct conf_writedata *cnf;
99 return (0); /* nothing to handle */
101 if (!(cnf = file->private_data))
102 return (-EFAULT); /* should never happen */
104 if (cnf->state == CONF_STATE_DETECT) { /* auto detect cnf or pof data */
105 if (copy_from_user(&ch, buf, 1)) /* get first char for detect */
109 /* we detected a pof file */
110 if ((cnf->needed_size = pof_write_open(cnf->card, &cnf->pof_buffer)) <= 0)
111 return (cnf->needed_size); /* an error occurred -> exit */
112 cnf->buf_size = 0; /* buffer is empty */
113 cnf->state = CONF_STATE_POF; /* new state */
115 /* conf data has been detected */
116 cnf->buf_size = 0; /* buffer is empty */
117 cnf->state = CONF_STATE_CONF; /* requested conf data write */
118 if (cnf->card->state != CARD_STATE_RUN)
119 return (-ERR_NOT_BOOTED);
120 cnf->conf_line[CONF_LINE_LEN - 1] = 0; /* limit string length */
121 cnf->channel = 4098; /* default channel for output */
123 } /* state was auto detect */
124 if (cnf->state == CONF_STATE_POF) { /* pof write active */
125 i = cnf->needed_size - cnf->buf_size; /* bytes still missing for write */
127 return (-EINVAL); /* size error handling pof */
130 count = i; /* limit requested number of bytes */
131 if (copy_from_user(cnf->pof_buffer + cnf->buf_size, buf, count))
132 return (-EFAULT); /* error while copying */
133 cnf->buf_size += count;
135 if (cnf->needed_size == cnf->buf_size) {
136 cnf->needed_size = pof_write_buffer(cnf->card, cnf->buf_size); /* write data */
137 if (cnf->needed_size <= 0) {
138 cnf->card->state = CARD_STATE_BOOTERR; /* show boot error */
139 return (cnf->needed_size); /* an error occurred */
141 cnf->buf_size = 0; /* buffer is empty again */
144 /* pof write active */
145 else { /* conf write active */
147 if (cnf->card->state != CARD_STATE_RUN) {
148 if (cnf->card->debug_flags & LOG_CNF_MISC)
149 hysdn_addlog(cnf->card, "cnf write denied -> not booted");
150 return (-ERR_NOT_BOOTED);
152 i = (CONF_LINE_LEN - 1) - cnf->buf_size; /* bytes available in buffer */
154 /* copy remaining bytes into buffer */
157 count = i; /* limit transfer */
158 if (copy_from_user(cnf->conf_line + cnf->buf_size, buf, count))
159 return (-EFAULT); /* error while copying */
161 i = count; /* number of chars in buffer */
162 cp = cnf->conf_line + cnf->buf_size;
164 /* search for end of line */
165 if ((*cp < ' ') && (*cp != 9))
166 break; /* end of line found */
169 } /* search for end of line */
172 /* delimiter found */
173 *cp++ = 0; /* string termination */
174 count -= (i - 1); /* subtract remaining bytes from count */
175 while ((i) && (*cp < ' ') && (*cp != 9)) {
176 i--; /* discard next char */
177 count++; /* mark as read */
178 cp++; /* next char */
180 cnf->buf_size = 0; /* buffer is empty after transfer */
181 if ((i = process_line(cnf)) < 0) /* handle the line */
182 count = i; /* return the error */
184 /* delimiter found */
186 cnf->buf_size += count; /* add chars to string */
187 if (cnf->buf_size >= CONF_LINE_LEN - 1) {
188 if (cnf->card->debug_flags & LOG_CNF_MISC)
189 hysdn_addlog(cnf->card, "cnf line too long %d chars pos %d", cnf->buf_size, count);
190 return (-ERR_CONF_LONG);
192 } /* not delimited */
195 /* copy remaining bytes into buffer */
197 if (cnf->card->debug_flags & LOG_CNF_MISC)
198 hysdn_addlog(cnf->card, "cnf line too long");
199 return (-ERR_CONF_LONG);
201 } /* conf write active */
204 } /* hysdn_conf_write */
206 /*******************************************/
207 /* read conf file -> output card info data */
208 /*******************************************/
210 hysdn_conf_read(struct file *file, char __user *buf, size_t count, loff_t * off)
215 if (file->f_mode & FMODE_READ) {
216 if (!(cp = file->private_data))
217 return (-EFAULT); /* should never happen */
218 i = strlen(cp); /* get total string length */
220 /* still bytes to transfer */
221 cp += *off; /* point to desired data offset */
222 i -= *off; /* remaining length */
224 i = count; /* limit length to transfer */
225 if (copy_to_user(buf, cp, i))
226 return (-EFAULT); /* copy error */
227 *off += i; /* adjust offset */
231 return (-EPERM); /* no permission to read */
234 } /* hysdn_conf_read */
240 hysdn_conf_open(struct inode *ino, struct file *filep)
243 struct proc_dir_entry *pd;
244 struct conf_writedata *cnf;
247 /* now search the addressed card */
254 card = card->next; /* search next entry */
258 return (-ENODEV); /* device is unknown/invalid */
260 if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
261 hysdn_addlog(card, "config open for uid=%d gid=%d mode=0x%x",
262 filep->f_uid, filep->f_gid, filep->f_mode);
264 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
265 /* write only access -> write boot file or conf line */
267 if (!(cnf = kmalloc(sizeof(struct conf_writedata), GFP_KERNEL))) {
272 cnf->buf_size = 0; /* nothing buffered */
273 cnf->state = CONF_STATE_DETECT; /* start auto detect */
274 filep->private_data = cnf;
276 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
277 /* read access -> output card info data */
279 if (!(tmp = (char *) kmalloc(INFO_OUT_LEN * 2 + 2, GFP_KERNEL))) {
281 return (-EFAULT); /* out of memory */
283 filep->private_data = tmp; /* start of string */
285 /* first output a headline */
286 sprintf(tmp, "id bus slot type irq iobase dp-mem b-chans fax-chans state device");
287 cp = tmp; /* start of string */
290 while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
294 /* and now the data */
295 sprintf(cp, "%d %3d %4d %4d %3d 0x%04x 0x%08lx %7d %9d %3d %s",
298 PCI_SLOT(card->devfn),
306 hysdn_net_getname(card));
309 while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
312 *cp = 0; /* end of string */
313 } else { /* simultaneous read/write access forbidden ! */
315 return (-EPERM); /* no permission this time */
318 return nonseekable_open(ino, filep);
319 } /* hysdn_conf_open */
321 /***************************/
322 /* close a config file. */
323 /***************************/
325 hysdn_conf_close(struct inode *ino, struct file *filep)
328 struct conf_writedata *cnf;
330 struct proc_dir_entry *pd;
333 /* search the addressed card */
339 card = card->next; /* search next entry */
343 return (-ENODEV); /* device is unknown/invalid */
345 if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
346 hysdn_addlog(card, "config close for uid=%d gid=%d mode=0x%x",
347 filep->f_uid, filep->f_gid, filep->f_mode);
349 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
350 /* write only access -> write boot file or conf line */
351 if (filep->private_data) {
352 cnf = filep->private_data;
354 if (cnf->state == CONF_STATE_POF)
355 retval = pof_write_close(cnf->card); /* close the pof write */
356 kfree(filep->private_data); /* free allocated memory for buffer */
358 } /* handle write private data */
359 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
360 /* read access -> output card info data */
362 if (filep->private_data)
363 kfree(filep->private_data); /* release memory */
367 } /* hysdn_conf_close */
369 /******************************************************/
370 /* table for conf filesystem functions defined above. */
371 /******************************************************/
372 static struct file_operations conf_fops =
375 .read = hysdn_conf_read,
376 .write = hysdn_conf_write,
377 .open = hysdn_conf_open,
378 .release = hysdn_conf_close,
381 /*****************************/
382 /* hysdn subdir in /proc/net */
383 /*****************************/
384 struct proc_dir_entry *hysdn_proc_entry = NULL;
386 /*******************************************************************************/
387 /* hysdn_procconf_init is called when the module is loaded and after the cards */
388 /* have been detected. The needed proc dir and card config files are created. */
389 /* The log init is called at last. */
390 /*******************************************************************************/
392 hysdn_procconf_init(void)
397 hysdn_proc_entry = proc_mkdir(PROC_SUBDIR_NAME, proc_net);
398 if (!hysdn_proc_entry) {
399 printk(KERN_ERR "HYSDN: unable to create hysdn subdir\n");
402 card = card_root; /* point to first card */
405 sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
406 if ((card->procconf = (void *) create_proc_entry(conf_name,
407 S_IFREG | S_IRUGO | S_IWUSR,
408 hysdn_proc_entry)) != NULL) {
409 ((struct proc_dir_entry *) card->procconf)->proc_fops = &conf_fops;
410 ((struct proc_dir_entry *) card->procconf)->owner = THIS_MODULE;
411 hysdn_proclog_init(card); /* init the log file entry */
413 card = card->next; /* next entry */
416 printk(KERN_NOTICE "HYSDN: procfs Rev. %s initialised\n", hysdn_getrev(hysdn_procconf_revision));
418 } /* hysdn_procconf_init */
420 /*************************************************************************************/
421 /* hysdn_procconf_release is called when the module is unloaded and before the cards */
422 /* resources are released. The module counter is assumed to be 0 ! */
423 /*************************************************************************************/
425 hysdn_procconf_release(void)
430 card = card_root; /* start with first card */
433 sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
435 remove_proc_entry(conf_name, hysdn_proc_entry);
437 hysdn_proclog_release(card); /* init the log file entry */
439 card = card->next; /* point to next card */
442 remove_proc_entry(PROC_SUBDIR_NAME, proc_net);