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/slab.h>
20#include <linux/mutex.h>
21#include <net/net_namespace.h>
22
23#include "hysdn_defs.h"
24
25static DEFINE_MUTEX(hysdn_conf_mutex);
26
27#define INFO_OUT_LEN 80
28
29
30
31
32#define CONF_STATE_DETECT 0
33#define CONF_STATE_CONF 1
34#define CONF_STATE_POF 2
35#define CONF_LINE_LEN 255
36
37struct conf_writedata {
38 hysdn_card *card;
39 int buf_size;
40 int needed_size;
41 int state;
42 unsigned char conf_line[CONF_LINE_LEN];
43 unsigned short channel;
44 unsigned char *pof_buffer;
45};
46
47
48
49
50
51
52static int
53process_line(struct conf_writedata *cnf)
54{
55 unsigned char *cp = cnf->conf_line;
56 int i;
57
58 if (cnf->card->debug_flags & LOG_CNF_LINE)
59 hysdn_addlog(cnf->card, "conf line: %s", cp);
60
61 if (*cp == '-') {
62 cp++;
63
64 if (*cp++ != 'c')
65 return (0);
66 i = 0;
67 while ((*cp <= '9') && (*cp >= '0'))
68 i = i * 10 + *cp++ - '0';
69 if (i > 65535) {
70 if (cnf->card->debug_flags & LOG_CNF_MISC)
71 hysdn_addlog(cnf->card, "conf channel invalid %d", i);
72 return (-ERR_INV_CHAN);
73 }
74 cnf->channel = i & 0xFFFF;
75 return (0);
76 }
77 if (*cp == '*') {
78 if (cnf->card->debug_flags & LOG_CNF_DATA)
79 hysdn_addlog(cnf->card, "conf chan=%d %s", cnf->channel, cp);
80 return (hysdn_tx_cfgline(cnf->card, cnf->conf_line + 1,
81 cnf->channel));
82 }
83 return (0);
84}
85
86
87
88
89
90
91
92
93static ssize_t
94hysdn_conf_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
95{
96 struct conf_writedata *cnf;
97 int i;
98 unsigned char ch, *cp;
99
100 if (!count)
101 return (0);
102
103 if (!(cnf = file->private_data))
104 return (-EFAULT);
105
106 if (cnf->state == CONF_STATE_DETECT) {
107 if (copy_from_user(&ch, buf, 1))
108 return (-EFAULT);
109
110 if (ch == 0x1A) {
111
112 if ((cnf->needed_size = pof_write_open(cnf->card, &cnf->pof_buffer)) <= 0)
113 return (cnf->needed_size);
114 cnf->buf_size = 0;
115 cnf->state = CONF_STATE_POF;
116 } else {
117
118 cnf->buf_size = 0;
119 cnf->state = CONF_STATE_CONF;
120 if (cnf->card->state != CARD_STATE_RUN)
121 return (-ERR_NOT_BOOTED);
122 cnf->conf_line[CONF_LINE_LEN - 1] = 0;
123 cnf->channel = 4098;
124 }
125 }
126 if (cnf->state == CONF_STATE_POF) {
127 i = cnf->needed_size - cnf->buf_size;
128 if (i <= 0)
129 return (-EINVAL);
130
131 if (i < count)
132 count = i;
133 if (copy_from_user(cnf->pof_buffer + cnf->buf_size, buf, count))
134 return (-EFAULT);
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);
139 if (cnf->needed_size <= 0) {
140 cnf->card->state = CARD_STATE_BOOTERR;
141 return (cnf->needed_size);
142 }
143 cnf->buf_size = 0;
144 }
145 }
146
147 else {
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;
155 if (i > 0) {
156
157
158 if (count > i)
159 count = i;
160 if (copy_from_user(cnf->conf_line + cnf->buf_size, buf, count))
161 return (-EFAULT);
162
163 i = count;
164 cp = cnf->conf_line + cnf->buf_size;
165 while (i) {
166
167 if ((*cp < ' ') && (*cp != 9))
168 break;
169 cp++;
170 i--;
171 }
172
173 if (i) {
174
175 *cp++ = 0;
176 count -= (i - 1);
177 while ((i) && (*cp < ' ') && (*cp != 9)) {
178 i--;
179 count++;
180 cp++;
181 }
182 cnf->buf_size = 0;
183 if ((i = process_line(cnf)) < 0)
184 count = i;
185 }
186
187 else {
188 cnf->buf_size += count;
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 }
195
196 }
197
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 }
204
205 return (count);
206}
207
208
209
210
211static ssize_t
212hysdn_conf_read(struct file *file, char __user *buf, size_t count, loff_t *off)
213{
214 char *cp;
215
216 if (!(file->f_mode & FMODE_READ))
217 return -EPERM;
218
219 if (!(cp = file->private_data))
220 return -EFAULT;
221
222 return simple_read_from_buffer(buf, count, off, cp, strlen(cp));
223}
224
225
226
227
228static int
229hysdn_conf_open(struct inode *ino, struct file *filep)
230{
231 hysdn_card *card;
232 struct proc_dir_entry *pd;
233 struct conf_writedata *cnf;
234 char *cp, *tmp;
235
236
237 mutex_lock(&hysdn_conf_mutex);
238 card = card_root;
239 while (card) {
240 pd = card->procconf;
241 if (pd == PDE(ino))
242 break;
243 card = card->next;
244 }
245 if (!card) {
246 mutex_unlock(&hysdn_conf_mutex);
247 return (-ENODEV);
248 }
249 if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
250 hysdn_addlog(card, "config open for uid=%d gid=%d mode=0x%x",
251 filep->f_cred->fsuid, filep->f_cred->fsgid,
252 filep->f_mode);
253
254 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
255
256
257 if (!(cnf = kmalloc(sizeof(struct conf_writedata), GFP_KERNEL))) {
258 mutex_unlock(&hysdn_conf_mutex);
259 return (-EFAULT);
260 }
261 cnf->card = card;
262 cnf->buf_size = 0;
263 cnf->state = CONF_STATE_DETECT;
264 filep->private_data = cnf;
265
266 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
267
268
269 if (!(tmp = kmalloc(INFO_OUT_LEN * 2 + 2, GFP_KERNEL))) {
270 mutex_unlock(&hysdn_conf_mutex);
271 return (-EFAULT);
272 }
273 filep->private_data = tmp;
274
275
276 sprintf(tmp, "id bus slot type irq iobase dp-mem b-chans fax-chans state device");
277 cp = tmp;
278 while (*cp)
279 cp++;
280 while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
281 *cp++ = ' ';
282 *cp++ = '\n';
283
284
285 sprintf(cp, "%d %3d %4d %4d %3d 0x%04x 0x%08lx %7d %9d %3d %s",
286 card->myid,
287 card->bus,
288 PCI_SLOT(card->devfn),
289 card->brdtype,
290 card->irq,
291 card->iobase,
292 card->membase,
293 card->bchans,
294 card->faxchans,
295 card->state,
296 hysdn_net_getname(card));
297 while (*cp)
298 cp++;
299 while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
300 *cp++ = ' ';
301 *cp++ = '\n';
302 *cp = 0;
303 } else {
304 mutex_unlock(&hysdn_conf_mutex);
305 return (-EPERM);
306 }
307 mutex_unlock(&hysdn_conf_mutex);
308 return nonseekable_open(ino, filep);
309}
310
311
312
313
314static int
315hysdn_conf_close(struct inode *ino, struct file *filep)
316{
317 hysdn_card *card;
318 struct conf_writedata *cnf;
319 int retval = 0;
320 struct proc_dir_entry *pd;
321
322 mutex_lock(&hysdn_conf_mutex);
323
324 card = card_root;
325 while (card) {
326 pd = card->procconf;
327 if (pd == PDE(ino))
328 break;
329 card = card->next;
330 }
331 if (!card) {
332 mutex_unlock(&hysdn_conf_mutex);
333 return (-ENODEV);
334 }
335 if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
336 hysdn_addlog(card, "config close for uid=%d gid=%d mode=0x%x",
337 filep->f_cred->fsuid, filep->f_cred->fsgid,
338 filep->f_mode);
339
340 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
341
342 if (filep->private_data) {
343 cnf = filep->private_data;
344
345 if (cnf->state == CONF_STATE_POF)
346 retval = pof_write_close(cnf->card);
347 kfree(filep->private_data);
348
349 }
350 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
351
352
353 kfree(filep->private_data);
354 }
355 mutex_unlock(&hysdn_conf_mutex);
356 return (retval);
357}
358
359
360
361
362static const struct file_operations conf_fops =
363{
364 .owner = THIS_MODULE,
365 .llseek = no_llseek,
366 .read = hysdn_conf_read,
367 .write = hysdn_conf_write,
368 .open = hysdn_conf_open,
369 .release = hysdn_conf_close,
370};
371
372
373
374
375struct proc_dir_entry *hysdn_proc_entry = NULL;
376
377
378
379
380
381
382int
383hysdn_procconf_init(void)
384{
385 hysdn_card *card;
386 unsigned char conf_name[20];
387
388 hysdn_proc_entry = proc_mkdir(PROC_SUBDIR_NAME, init_net.proc_net);
389 if (!hysdn_proc_entry) {
390 printk(KERN_ERR "HYSDN: unable to create hysdn subdir\n");
391 return (-1);
392 }
393 card = card_root;
394 while (card) {
395
396 sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
397 if ((card->procconf = (void *) proc_create(conf_name,
398 S_IFREG | S_IRUGO | S_IWUSR,
399 hysdn_proc_entry,
400 &conf_fops)) != NULL) {
401 hysdn_proclog_init(card);
402 }
403 card = card->next;
404 }
405
406 printk(KERN_NOTICE "HYSDN: procfs initialised\n");
407 return (0);
408}
409
410
411
412
413
414void
415hysdn_procconf_release(void)
416{
417 hysdn_card *card;
418 unsigned char conf_name[20];
419
420 card = card_root;
421 while (card) {
422
423 sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
424 if (card->procconf)
425 remove_proc_entry(conf_name, hysdn_proc_entry);
426
427 hysdn_proclog_release(card);
428
429 card = card->next;
430 }
431
432 remove_proc_entry(PROC_SUBDIR_NAME, init_net.proc_net);
433}
434