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