proc: use seq_puts()/seq_putc() where possible
[linux-2.6/next.git] / drivers / isdn / hysdn / hysdn_procconf.c
blob96b3e39c3356b0ff08dbfc38577b66b982cba701
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/cred.h>
15 #include <linux/module.h>
16 #include <linux/poll.h>
17 #include <linux/proc_fs.h>
18 #include <linux/pci.h>
19 #include <linux/slab.h>
20 #include <linux/mutex.h>
21 #include <net/net_namespace.h>
23 #include "hysdn_defs.h"
25 static DEFINE_MUTEX(hysdn_conf_mutex);
26 static char *hysdn_procconf_revision = "$Revision: 1.8.6.4 $";
28 #define INFO_OUT_LEN 80 /* length of info line including lf */
30 /********************************************************/
31 /* defines and data structure for conf write operations */
32 /********************************************************/
33 #define CONF_STATE_DETECT 0 /* waiting for detect */
34 #define CONF_STATE_CONF 1 /* writing config data */
35 #define CONF_STATE_POF 2 /* writing pof data */
36 #define CONF_LINE_LEN 255 /* 255 chars max */
38 struct conf_writedata {
39 hysdn_card *card; /* card the device is connected to */
40 int buf_size; /* actual number of bytes in the buffer */
41 int needed_size; /* needed size when reading pof */
42 int state; /* actual interface states from above constants */
43 unsigned char conf_line[CONF_LINE_LEN]; /* buffered conf line */
44 unsigned short channel; /* active channel number */
45 unsigned char *pof_buffer; /* buffer when writing pof */
48 /***********************************************************************/
49 /* process_line parses one config line and transfers it to the card if */
50 /* necessary. */
51 /* if the return value is negative an error occurred. */
52 /***********************************************************************/
53 static int
54 process_line(struct conf_writedata *cnf)
56 unsigned char *cp = cnf->conf_line;
57 int i;
59 if (cnf->card->debug_flags & LOG_CNF_LINE)
60 hysdn_addlog(cnf->card, "conf line: %s", cp);
62 if (*cp == '-') { /* option */
63 cp++; /* point to option char */
65 if (*cp++ != 'c')
66 return (0); /* option unknown or used */
67 i = 0; /* start value for channel */
68 while ((*cp <= '9') && (*cp >= '0'))
69 i = i * 10 + *cp++ - '0'; /* get decimal number */
70 if (i > 65535) {
71 if (cnf->card->debug_flags & LOG_CNF_MISC)
72 hysdn_addlog(cnf->card, "conf channel invalid %d", i);
73 return (-ERR_INV_CHAN); /* invalid channel */
75 cnf->channel = i & 0xFFFF; /* set new channel number */
76 return (0); /* success */
77 } /* option */
78 if (*cp == '*') { /* line to send */
79 if (cnf->card->debug_flags & LOG_CNF_DATA)
80 hysdn_addlog(cnf->card, "conf chan=%d %s", cnf->channel, cp);
81 return (hysdn_tx_cfgline(cnf->card, cnf->conf_line + 1,
82 cnf->channel)); /* send the line without * */
83 } /* line to send */
84 return (0);
85 } /* process_line */
87 /***********************************/
88 /* conf file operations and tables */
89 /***********************************/
91 /****************************************************/
92 /* write conf file -> boot or send cfg line to card */
93 /****************************************************/
94 static ssize_t
95 hysdn_conf_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
97 struct conf_writedata *cnf;
98 int i;
99 unsigned char ch, *cp;
101 if (!count)
102 return (0); /* nothing to handle */
104 if (!(cnf = file->private_data))
105 return (-EFAULT); /* should never happen */
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);
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 */
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 */
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;
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 */
144 cnf->buf_size = 0; /* buffer is empty again */
147 /* pof write active */
148 else { /* conf write active */
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);
155 i = (CONF_LINE_LEN - 1) - cnf->buf_size; /* bytes available in buffer */
156 if (i > 0) {
157 /* copy remaining bytes into buffer */
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 */
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 */
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 */
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 */
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);
195 } /* not delimited */
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);
204 } /* conf write active */
206 return (count);
207 } /* hysdn_conf_write */
209 /*******************************************/
210 /* read conf file -> output card info data */
211 /*******************************************/
212 static ssize_t
213 hysdn_conf_read(struct file *file, char __user *buf, size_t count, loff_t *off)
215 char *cp;
217 if (!(file->f_mode & FMODE_READ))
218 return -EPERM; /* no permission to read */
220 if (!(cp = file->private_data))
221 return -EFAULT; /* should never happen */
223 return simple_read_from_buffer(buf, count, off, cp, strlen(cp));
224 } /* hysdn_conf_read */
226 /******************/
227 /* open conf file */
228 /******************/
229 static int
230 hysdn_conf_open(struct inode *ino, struct file *filep)
232 hysdn_card *card;
233 struct proc_dir_entry *pd;
234 struct conf_writedata *cnf;
235 char *cp, *tmp;
237 /* now search the addressed card */
238 mutex_lock(&hysdn_conf_mutex);
239 card = card_root;
240 while (card) {
241 pd = card->procconf;
242 if (pd == PDE(ino))
243 break;
244 card = card->next; /* search next entry */
246 if (!card) {
247 mutex_unlock(&hysdn_conf_mutex);
248 return (-ENODEV); /* device is unknown/invalid */
250 if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
251 hysdn_addlog(card, "config open for uid=%d gid=%d mode=0x%x",
252 filep->f_cred->fsuid, filep->f_cred->fsgid,
253 filep->f_mode);
255 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
256 /* write only access -> write boot file or conf line */
258 if (!(cnf = kmalloc(sizeof(struct conf_writedata), GFP_KERNEL))) {
259 mutex_unlock(&hysdn_conf_mutex);
260 return (-EFAULT);
262 cnf->card = card;
263 cnf->buf_size = 0; /* nothing buffered */
264 cnf->state = CONF_STATE_DETECT; /* start auto detect */
265 filep->private_data = cnf;
267 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
268 /* read access -> output card info data */
270 if (!(tmp = kmalloc(INFO_OUT_LEN * 2 + 2, GFP_KERNEL))) {
271 mutex_unlock(&hysdn_conf_mutex);
272 return (-EFAULT); /* out of memory */
274 filep->private_data = tmp; /* start of string */
276 /* first output a headline */
277 sprintf(tmp, "id bus slot type irq iobase dp-mem b-chans fax-chans state device");
278 cp = tmp; /* start of string */
279 while (*cp)
280 cp++;
281 while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
282 *cp++ = ' ';
283 *cp++ = '\n';
285 /* and now the data */
286 sprintf(cp, "%d %3d %4d %4d %3d 0x%04x 0x%08lx %7d %9d %3d %s",
287 card->myid,
288 card->bus,
289 PCI_SLOT(card->devfn),
290 card->brdtype,
291 card->irq,
292 card->iobase,
293 card->membase,
294 card->bchans,
295 card->faxchans,
296 card->state,
297 hysdn_net_getname(card));
298 while (*cp)
299 cp++;
300 while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
301 *cp++ = ' ';
302 *cp++ = '\n';
303 *cp = 0; /* end of string */
304 } else { /* simultaneous read/write access forbidden ! */
305 mutex_unlock(&hysdn_conf_mutex);
306 return (-EPERM); /* no permission this time */
308 mutex_unlock(&hysdn_conf_mutex);
309 return nonseekable_open(ino, filep);
310 } /* hysdn_conf_open */
312 /***************************/
313 /* close a config file. */
314 /***************************/
315 static int
316 hysdn_conf_close(struct inode *ino, struct file *filep)
318 hysdn_card *card;
319 struct conf_writedata *cnf;
320 int retval = 0;
321 struct proc_dir_entry *pd;
323 mutex_lock(&hysdn_conf_mutex);
324 /* search the addressed card */
325 card = card_root;
326 while (card) {
327 pd = card->procconf;
328 if (pd == PDE(ino))
329 break;
330 card = card->next; /* search next entry */
332 if (!card) {
333 mutex_unlock(&hysdn_conf_mutex);
334 return (-ENODEV); /* device is unknown/invalid */
336 if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
337 hysdn_addlog(card, "config close for uid=%d gid=%d mode=0x%x",
338 filep->f_cred->fsuid, filep->f_cred->fsgid,
339 filep->f_mode);
341 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
342 /* write only access -> write boot file or conf line */
343 if (filep->private_data) {
344 cnf = filep->private_data;
346 if (cnf->state == CONF_STATE_POF)
347 retval = pof_write_close(cnf->card); /* close the pof write */
348 kfree(filep->private_data); /* free allocated memory for buffer */
350 } /* handle write private data */
351 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
352 /* read access -> output card info data */
354 kfree(filep->private_data); /* release memory */
356 mutex_unlock(&hysdn_conf_mutex);
357 return (retval);
358 } /* hysdn_conf_close */
360 /******************************************************/
361 /* table for conf filesystem functions defined above. */
362 /******************************************************/
363 static const struct file_operations conf_fops =
365 .owner = THIS_MODULE,
366 .llseek = no_llseek,
367 .read = hysdn_conf_read,
368 .write = hysdn_conf_write,
369 .open = hysdn_conf_open,
370 .release = hysdn_conf_close,
373 /*****************************/
374 /* hysdn subdir in /proc/net */
375 /*****************************/
376 struct proc_dir_entry *hysdn_proc_entry = NULL;
378 /*******************************************************************************/
379 /* hysdn_procconf_init is called when the module is loaded and after the cards */
380 /* have been detected. The needed proc dir and card config files are created. */
381 /* The log init is called at last. */
382 /*******************************************************************************/
384 hysdn_procconf_init(void)
386 hysdn_card *card;
387 unsigned char conf_name[20];
389 hysdn_proc_entry = proc_mkdir(PROC_SUBDIR_NAME, init_net.proc_net);
390 if (!hysdn_proc_entry) {
391 printk(KERN_ERR "HYSDN: unable to create hysdn subdir\n");
392 return (-1);
394 card = card_root; /* point to first card */
395 while (card) {
397 sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
398 if ((card->procconf = (void *) proc_create(conf_name,
399 S_IFREG | S_IRUGO | S_IWUSR,
400 hysdn_proc_entry,
401 &conf_fops)) != NULL) {
402 hysdn_proclog_init(card); /* init the log file entry */
404 card = card->next; /* next entry */
407 printk(KERN_NOTICE "HYSDN: procfs Rev. %s initialised\n", hysdn_getrev(hysdn_procconf_revision));
408 return (0);
409 } /* hysdn_procconf_init */
411 /*************************************************************************************/
412 /* hysdn_procconf_release is called when the module is unloaded and before the cards */
413 /* resources are released. The module counter is assumed to be 0 ! */
414 /*************************************************************************************/
415 void
416 hysdn_procconf_release(void)
418 hysdn_card *card;
419 unsigned char conf_name[20];
421 card = card_root; /* start with first card */
422 while (card) {
424 sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
425 if (card->procconf)
426 remove_proc_entry(conf_name, hysdn_proc_entry);
428 hysdn_proclog_release(card); /* init the log file entry */
430 card = card->next; /* point to next card */
433 remove_proc_entry(PROC_SUBDIR_NAME, init_net.proc_net);