8f9f4912de3243e4b118c09c4a346a006dc73981
[safe/jmp/linux-2.6] / 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/poll.h>
16 #include <linux/proc_fs.h>
17 #include <linux/pci.h>
18 #include <linux/smp_lock.h>
19 #include <net/net_namespace.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         unsigned char conf_line[CONF_LINE_LEN]; /* buffered conf line */
41         unsigned short channel;         /* active channel number */
42         unsigned char *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         unsigned char *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 __user *buf, size_t count, loff_t * off)
93 {
94         struct conf_writedata *cnf;
95         int i;
96         unsigned char ch, *cp;
97
98         if (!count)
99                 return (0);     /* nothing to handle */
100
101         if (!(cnf = file->private_data))
102                 return (-EFAULT);       /* should never happen */
103
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 */
106                         return (-EFAULT);
107
108                 if (ch == 0x1A) {
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 */
114                 } else {
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 */
122                 }
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 */
126                 if (i <= 0)
127                         return (-EINVAL);       /* size error handling pof */
128
129                 if (i < count)
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;
134
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 */
140                         }
141                         cnf->buf_size = 0;      /* buffer is empty again */
142                 }
143         }
144         /* pof write active */
145         else {                  /* conf write active */
146
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);
151                 }
152                 i = (CONF_LINE_LEN - 1) - cnf->buf_size;        /* bytes available in buffer */
153                 if (i > 0) {
154                         /* copy remaining bytes into buffer */
155
156                         if (count > i)
157                                 count = i;      /* limit transfer */
158                         if (copy_from_user(cnf->conf_line + cnf->buf_size, buf, count))
159                                 return (-EFAULT);       /* error while copying */
160
161                         i = count;      /* number of chars in buffer */
162                         cp = cnf->conf_line + cnf->buf_size;
163                         while (i) {
164                                 /* search for end of line */
165                                 if ((*cp < ' ') && (*cp != 9))
166                                         break;  /* end of line found */
167                                 cp++;
168                                 i--;
169                         }       /* search for end of line */
170
171                         if (i) {
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 */
179                                 }
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 */
183                         }
184                         /* delimiter found */
185                         else {
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);
191                                 }
192                         }       /* not delimited */
193
194                 }
195                 /* copy remaining bytes into buffer */
196                 else {
197                         if (cnf->card->debug_flags & LOG_CNF_MISC)
198                                 hysdn_addlog(cnf->card, "cnf line too long");
199                         return (-ERR_CONF_LONG);
200                 }
201         }                       /* conf write active */
202
203         return (count);
204 }                               /* hysdn_conf_write */
205
206 /*******************************************/
207 /* read conf file -> output card info data */
208 /*******************************************/
209 static ssize_t
210 hysdn_conf_read(struct file *file, char __user *buf, size_t count, loff_t *off)
211 {
212         char *cp;
213
214         if (!(file->f_mode & FMODE_READ))
215                 return -EPERM;  /* no permission to read */
216
217         if (!(cp = file->private_data))
218                 return -EFAULT; /* should never happen */
219
220         return simple_read_from_buffer(buf, count, off, cp, strlen(cp));
221 }                               /* hysdn_conf_read */
222
223 /******************/
224 /* open conf file */
225 /******************/
226 static int
227 hysdn_conf_open(struct inode *ino, struct file *filep)
228 {
229         hysdn_card *card;
230         struct proc_dir_entry *pd;
231         struct conf_writedata *cnf;
232         char *cp, *tmp;
233
234         /* now search the addressed card */
235         lock_kernel();
236         card = card_root;
237         while (card) {
238                 pd = card->procconf;
239                 if (pd == PDE(ino))
240                         break;
241                 card = card->next;      /* search next entry */
242         }
243         if (!card) {
244                 unlock_kernel();
245                 return (-ENODEV);       /* device is unknown/invalid */
246         }
247         if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
248                 hysdn_addlog(card, "config open for uid=%d gid=%d mode=0x%x",
249                              filep->f_cred->fsuid, filep->f_cred->fsgid,
250                              filep->f_mode);
251
252         if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
253                 /* write only access -> write boot file or conf line */
254
255                 if (!(cnf = kmalloc(sizeof(struct conf_writedata), GFP_KERNEL))) {
256                         unlock_kernel();
257                         return (-EFAULT);
258                 }
259                 cnf->card = card;
260                 cnf->buf_size = 0;      /* nothing buffered */
261                 cnf->state = CONF_STATE_DETECT;         /* start auto detect */
262                 filep->private_data = cnf;
263
264         } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
265                 /* read access -> output card info data */
266
267                 if (!(tmp = kmalloc(INFO_OUT_LEN * 2 + 2, GFP_KERNEL))) {
268                         unlock_kernel();
269                         return (-EFAULT);       /* out of memory */
270                 }
271                 filep->private_data = tmp;      /* start of string */
272
273                 /* first output a headline */
274                 sprintf(tmp, "id bus slot type irq iobase dp-mem     b-chans fax-chans state device");
275                 cp = tmp;       /* start of string */
276                 while (*cp)
277                         cp++;
278                 while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
279                         *cp++ = ' ';
280                 *cp++ = '\n';
281
282                 /* and now the data */
283                 sprintf(cp, "%d  %3d %4d %4d %3d 0x%04x 0x%08lx %7d %9d %3d   %s",
284                         card->myid,
285                         card->bus,
286                         PCI_SLOT(card->devfn),
287                         card->brdtype,
288                         card->irq,
289                         card->iobase,
290                         card->membase,
291                         card->bchans,
292                         card->faxchans,
293                         card->state,
294                         hysdn_net_getname(card));
295                 while (*cp)
296                         cp++;
297                 while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
298                         *cp++ = ' ';
299                 *cp++ = '\n';
300                 *cp = 0;        /* end of string */
301         } else {                /* simultaneous read/write access forbidden ! */
302                 unlock_kernel();
303                 return (-EPERM);        /* no permission this time */
304         }
305         unlock_kernel();
306         return nonseekable_open(ino, filep);
307 }                               /* hysdn_conf_open */
308
309 /***************************/
310 /* close a config file.    */
311 /***************************/
312 static int
313 hysdn_conf_close(struct inode *ino, struct file *filep)
314 {
315         hysdn_card *card;
316         struct conf_writedata *cnf;
317         int retval = 0;
318         struct proc_dir_entry *pd;
319
320         lock_kernel();
321         /* search the addressed card */
322         card = card_root;
323         while (card) {
324                 pd = card->procconf;
325                 if (pd == PDE(ino))
326                         break;
327                 card = card->next;      /* search next entry */
328         }
329         if (!card) {
330                 unlock_kernel();
331                 return (-ENODEV);       /* device is unknown/invalid */
332         }
333         if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
334                 hysdn_addlog(card, "config close for uid=%d gid=%d mode=0x%x",
335                              filep->f_cred->fsuid, filep->f_cred->fsgid,
336                              filep->f_mode);
337
338         if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
339                 /* write only access -> write boot file or conf line */
340                 if (filep->private_data) {
341                         cnf = filep->private_data;
342
343                         if (cnf->state == CONF_STATE_POF)
344                                 retval = pof_write_close(cnf->card);    /* close the pof write */
345                         kfree(filep->private_data);     /* free allocated memory for buffer */
346
347                 }               /* handle write private data */
348         } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
349                 /* read access -> output card info data */
350
351                 kfree(filep->private_data);     /* release memory */
352         }
353         unlock_kernel();
354         return (retval);
355 }                               /* hysdn_conf_close */
356
357 /******************************************************/
358 /* table for conf filesystem functions defined above. */
359 /******************************************************/
360 static const struct file_operations conf_fops =
361 {
362         .owner          = THIS_MODULE,
363         .llseek         = no_llseek,
364         .read           = hysdn_conf_read,
365         .write          = hysdn_conf_write,
366         .open           = hysdn_conf_open,
367         .release        = hysdn_conf_close,                                       
368 };
369
370 /*****************************/
371 /* hysdn subdir in /proc/net */
372 /*****************************/
373 struct proc_dir_entry *hysdn_proc_entry = NULL;
374
375 /*******************************************************************************/
376 /* hysdn_procconf_init is called when the module is loaded and after the cards */
377 /* have been detected. The needed proc dir and card config files are created.  */
378 /* The log init is called at last.                                             */
379 /*******************************************************************************/
380 int
381 hysdn_procconf_init(void)
382 {
383         hysdn_card *card;
384         unsigned char conf_name[20];
385
386         hysdn_proc_entry = proc_mkdir(PROC_SUBDIR_NAME, init_net.proc_net);
387         if (!hysdn_proc_entry) {
388                 printk(KERN_ERR "HYSDN: unable to create hysdn subdir\n");
389                 return (-1);
390         }
391         card = card_root;       /* point to first card */
392         while (card) {
393
394                 sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
395                 if ((card->procconf = (void *) proc_create(conf_name,
396                                                 S_IFREG | S_IRUGO | S_IWUSR,
397                                                 hysdn_proc_entry,
398                                                 &conf_fops)) != NULL) {
399                         hysdn_proclog_init(card);       /* init the log file entry */
400                 }
401                 card = card->next;      /* next entry */
402         }
403
404         printk(KERN_NOTICE "HYSDN: procfs Rev. %s initialised\n", hysdn_getrev(hysdn_procconf_revision));
405         return (0);
406 }                               /* hysdn_procconf_init */
407
408 /*************************************************************************************/
409 /* hysdn_procconf_release is called when the module is unloaded and before the cards */
410 /* resources are released. The module counter is assumed to be 0 !                   */
411 /*************************************************************************************/
412 void
413 hysdn_procconf_release(void)
414 {
415         hysdn_card *card;
416         unsigned char conf_name[20];
417
418         card = card_root;       /* start with first card */
419         while (card) {
420
421                 sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
422                 if (card->procconf)
423                         remove_proc_entry(conf_name, hysdn_proc_entry);
424
425                 hysdn_proclog_release(card);    /* init the log file entry */
426
427                 card = card->next;      /* point to next card */
428         }
429
430         remove_proc_entry(PROC_SUBDIR_NAME, init_net.proc_net);
431 }