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