1
2
3
4
5
6
7
8
9
10
11
12
13
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/smp_lock.h>
20#include <net/net_namespace.h>
21
22#include "hysdn_defs.h"
23
24static char *hysdn_procconf_revision = "$Revision: 1.8.6.4 $";
25
26#define INFO_OUT_LEN 80
27
28
29
30
31#define CONF_STATE_DETECT 0
32#define CONF_STATE_CONF 1
33#define CONF_STATE_POF 2
34#define CONF_LINE_LEN 255
35
36struct conf_writedata {
37 hysdn_card *card;
38 int buf_size;
39 int needed_size;
40 int state;
41 unsigned char conf_line[CONF_LINE_LEN];
42 unsigned short channel;
43 unsigned char *pof_buffer;
44};
45
46
47
48
49
50
51static int
52process_line(struct conf_writedata *cnf)
53{
54 unsigned char *cp = cnf->conf_line;
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 == '-') {
61 cp++;
62
63 if (*cp++ != 'c')
64 return (0);
65 i = 0;
66 while ((*cp <= '9') && (*cp >= '0'))
67 i = i * 10 + *cp++ - '0';
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);
72 }
73 cnf->channel = i & 0xFFFF;
74 return (0);
75 }
76 if (*cp == '*') {
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));
81 }
82 return (0);
83}
84
85
86
87
88
89
90
91
92static ssize_t
93hysdn_conf_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
94{
95 struct conf_writedata *cnf;
96 int i;
97 unsigned char ch, *cp;
98
99 if (!count)
100 return (0);
101
102 if (!(cnf = file->private_data))
103 return (-EFAULT);
104
105 if (cnf->state == CONF_STATE_DETECT) {
106 if (copy_from_user(&ch, buf, 1))
107 return (-EFAULT);
108
109 if (ch == 0x1A) {
110
111 if ((cnf->needed_size = pof_write_open(cnf->card, &cnf->pof_buffer)) <= 0)
112 return (cnf->needed_size);
113 cnf->buf_size = 0;
114 cnf->state = CONF_STATE_POF;
115 } else {
116
117 cnf->buf_size = 0;
118 cnf->state = CONF_STATE_CONF;
119 if (cnf->card->state != CARD_STATE_RUN)
120 return (-ERR_NOT_BOOTED);
121 cnf->conf_line[CONF_LINE_LEN - 1] = 0;
122 cnf->channel = 4098;
123 }
124 }
125 if (cnf->state == CONF_STATE_POF) {
126 i = cnf->needed_size - cnf->buf_size;
127 if (i <= 0)
128 return (-EINVAL);
129
130 if (i < count)
131 count = i;
132 if (copy_from_user(cnf->pof_buffer + cnf->buf_size, buf, count))
133 return (-EFAULT);
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);
138 if (cnf->needed_size <= 0) {
139 cnf->card->state = CARD_STATE_BOOTERR;
140 return (cnf->needed_size);
141 }
142 cnf->buf_size = 0;
143 }
144 }
145
146 else {
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;
154 if (i > 0) {
155
156
157 if (count > i)
158 count = i;
159 if (copy_from_user(cnf->conf_line + cnf->buf_size, buf, count))
160 return (-EFAULT);
161
162 i = count;
163 cp = cnf->conf_line + cnf->buf_size;
164 while (i) {
165
166 if ((*cp < ' ') && (*cp != 9))
167 break;
168 cp++;
169 i--;
170 }
171
172 if (i) {
173
174 *cp++ = 0;
175 count -= (i - 1);
176 while ((i) && (*cp < ' ') && (*cp != 9)) {
177 i--;
178 count++;
179 cp++;
180 }
181 cnf->buf_size = 0;
182 if ((i = process_line(cnf)) < 0)
183 count = i;
184 }
185
186 else {
187 cnf->buf_size += count;
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 }
194
195 }
196
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 }
203
204 return (count);
205}
206
207
208
209
210static ssize_t
211hysdn_conf_read(struct file *file, char __user *buf, size_t count, loff_t *off)
212{
213 char *cp;
214
215 if (!(file->f_mode & FMODE_READ))
216 return -EPERM;
217
218 if (!(cp = file->private_data))
219 return -EFAULT;
220
221 return simple_read_from_buffer(buf, count, off, cp, strlen(cp));
222}
223
224
225
226
227static int
228hysdn_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
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;
243 }
244 if (!card) {
245 unlock_kernel();
246 return (-ENODEV);
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",
250 filep->f_cred->fsuid, filep->f_cred->fsgid,
251 filep->f_mode);
252
253 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
254
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;
262 cnf->state = CONF_STATE_DETECT;
263 filep->private_data = cnf;
264
265 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
266
267
268 if (!(tmp = kmalloc(INFO_OUT_LEN * 2 + 2, GFP_KERNEL))) {
269 unlock_kernel();
270 return (-EFAULT);
271 }
272 filep->private_data = tmp;
273
274
275 sprintf(tmp, "id bus slot type irq iobase dp-mem b-chans fax-chans state device");
276 cp = tmp;
277 while (*cp)
278 cp++;
279 while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
280 *cp++ = ' ';
281 *cp++ = '\n';
282
283
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;
302 } else {
303 unlock_kernel();
304 return (-EPERM);
305 }
306 unlock_kernel();
307 return nonseekable_open(ino, filep);
308}
309
310
311
312
313static int
314hysdn_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
323 card = card_root;
324 while (card) {
325 pd = card->procconf;
326 if (pd == PDE(ino))
327 break;
328 card = card->next;
329 }
330 if (!card) {
331 unlock_kernel();
332 return (-ENODEV);
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",
336 filep->f_cred->fsuid, filep->f_cred->fsgid,
337 filep->f_mode);
338
339 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
340
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);
346 kfree(filep->private_data);
347
348 }
349 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
350
351
352 kfree(filep->private_data);
353 }
354 unlock_kernel();
355 return (retval);
356}
357
358
359
360
361static const struct file_operations conf_fops =
362{
363 .owner = THIS_MODULE,
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
373
374struct proc_dir_entry *hysdn_proc_entry = NULL;
375
376
377
378
379
380
381int
382hysdn_procconf_init(void)
383{
384 hysdn_card *card;
385 unsigned char conf_name[20];
386
387 hysdn_proc_entry = proc_mkdir(PROC_SUBDIR_NAME, init_net.proc_net);
388 if (!hysdn_proc_entry) {
389 printk(KERN_ERR "HYSDN: unable to create hysdn subdir\n");
390 return (-1);
391 }
392 card = card_root;
393 while (card) {
394
395 sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
396 if ((card->procconf = (void *) proc_create(conf_name,
397 S_IFREG | S_IRUGO | S_IWUSR,
398 hysdn_proc_entry,
399 &conf_fops)) != NULL) {
400 hysdn_proclog_init(card);
401 }
402 card = card->next;
403 }
404
405 printk(KERN_NOTICE "HYSDN: procfs Rev. %s initialised\n", hysdn_getrev(hysdn_procconf_revision));
406 return (0);
407}
408
409
410
411
412
413void
414hysdn_procconf_release(void)
415{
416 hysdn_card *card;
417 unsigned char conf_name[20];
418
419 card = card_root;
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);
427
428 card = card->next;
429 }
430
431 remove_proc_entry(PROC_SUBDIR_NAME, init_net.proc_net);
432}
433