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