Merge to Fedora kernel-2.6.7-1.492
[linux-2.6.git] / drivers / isdn / hardware / eicon / divasproc.c
1 /* $Id: divasproc.c,v 1.19 2004/03/21 17:26:01 armin Exp $
2  *
3  * Low level driver for Eicon DIVA Server ISDN cards.
4  * /proc functions
5  *
6  * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
7  * Copyright 2000-2003 Cytronics & Melware (info@melware.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 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/poll.h>
17 #include <linux/proc_fs.h>
18 #include <linux/list.h>
19
20 #include "platform.h"
21 #include "debuglib.h"
22 #undef ID_MASK
23 #undef N_DATA
24 #include "pc.h"
25 #include "di_defs.h"
26 #include "divasync.h"
27 #include "di.h"
28 #include "io.h"
29 #include "xdi_msg.h"
30 #include "xdi_adapter.h"
31 #include "diva.h"
32 #include "diva_pci.h"
33
34
35 extern PISDN_ADAPTER IoAdapters[MAX_ADAPTER];
36 extern void divas_get_version(char *);
37 extern void diva_get_vserial_number(PISDN_ADAPTER IoAdapter, char *buf);
38
39 /*********************************************************
40  ** Functions for /proc interface / File operations
41  *********************************************************/
42
43 static char *divas_proc_name = "divas";
44 static char *adapter_dir_name = "adapter";
45 static char *info_proc_name = "info";
46 static char *grp_opt_proc_name = "group_optimization";
47 static char *d_l1_down_proc_name = "dynamic_l1_down";
48
49 /*
50 ** "divas" entry
51 */
52
53 extern struct proc_dir_entry *proc_net_eicon;
54 static struct proc_dir_entry *divas_proc_entry = NULL;
55
56 static ssize_t
57 divas_read(struct file *file, char __user *buf, size_t count, loff_t * off)
58 {
59         int len = 0;
60         int cadapter;
61         char tmpbuf[80];
62         char tmpser[16];
63
64         if (*off)
65                 return 0;
66         if (off != &file->f_pos)
67                 return -ESPIPE;
68
69         divas_get_version(tmpbuf);
70         if (copy_to_user(buf + len, &tmpbuf, strlen(tmpbuf)))
71                 return -EFAULT;
72         len += strlen(tmpbuf);
73
74         for (cadapter = 0; cadapter < MAX_ADAPTER; cadapter++) {
75                 if (IoAdapters[cadapter]) {
76                         diva_get_vserial_number(IoAdapters[cadapter],
77                                                 tmpser);
78                         sprintf(tmpbuf,
79                                 "%2d: %-30s Serial:%-10s IRQ:%2d\n",
80                                 cadapter + 1,
81                                 IoAdapters[cadapter]->Properties.Name,
82                                 tmpser,
83                                 IoAdapters[cadapter]->irq_info.irq_nr);
84                         if ((strlen(tmpbuf) + len) > count)
85                                 break;
86                         if (copy_to_user
87                             (buf + len, &tmpbuf,
88                              strlen(tmpbuf))) return -EFAULT;
89                         len += strlen(tmpbuf);
90                 }
91         }
92
93         *off += len;
94         return (len);
95 }
96
97 static ssize_t
98 divas_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
99 {
100         return (-ENODEV);
101 }
102
103 static unsigned int divas_poll(struct file *file, poll_table * wait)
104 {
105         return (POLLERR);
106 }
107
108 static int divas_open(struct inode *inode, struct file *file)
109 {
110         return (0);
111 }
112
113 static int divas_close(struct inode *inode, struct file *file)
114 {
115         return (0);
116 }
117
118 static struct file_operations divas_fops = {
119         .owner   = THIS_MODULE,
120         .llseek  = no_llseek,
121         .read    = divas_read,
122         .write   = divas_write,
123         .poll    = divas_poll,
124         .open    = divas_open,
125         .release = divas_close
126 };
127
128 int create_divas_proc(void)
129 {
130         divas_proc_entry = create_proc_entry(divas_proc_name,
131                                              S_IFREG | S_IRUGO,
132                                              proc_net_eicon);
133         if (!divas_proc_entry)
134                 return (0);
135
136         divas_proc_entry->proc_fops = &divas_fops;
137         divas_proc_entry->owner = THIS_MODULE;
138
139         return (1);
140 }
141
142 void remove_divas_proc(void)
143 {
144         if (divas_proc_entry) {
145                 remove_proc_entry(divas_proc_name, proc_net_eicon);
146                 divas_proc_entry = NULL;
147         }
148 }
149
150 /*
151 ** write group_optimization 
152 */
153 static int
154 write_grp_opt(struct file *file, const char __user *buffer, unsigned long count,
155               void *data)
156 {
157         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
158         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
159
160         if ((count == 1) || (count == 2)) {
161                 char c;
162                 if (get_user(c, buffer))
163                         return -EFAULT;
164                 switch (c) {
165                 case '0':
166                         IoAdapter->capi_cfg.cfg_1 &=
167                             ~DIVA_XDI_CAPI_CFG_1_GROUP_POPTIMIZATION_ON;
168                         break;
169                 case '1':
170                         IoAdapter->capi_cfg.cfg_1 |=
171                             DIVA_XDI_CAPI_CFG_1_GROUP_POPTIMIZATION_ON;
172                         break;
173                 default:
174                         return (-EINVAL);
175                 }
176                 return (count);
177         }
178         return (-EINVAL);
179 }
180
181 /*
182 ** write dynamic_l1_down
183 */
184 static int
185 write_d_l1_down(struct file *file, const char __user *buffer, unsigned long count,
186                 void *data)
187 {
188         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
189         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
190
191         if ((count == 1) || (count == 2)) {
192                 char c;
193                 if (get_user(c, buffer))
194                         return -EFAULT;
195                 switch (c) {
196                 case '0':
197                         IoAdapter->capi_cfg.cfg_1 &=
198                             ~DIVA_XDI_CAPI_CFG_1_DYNAMIC_L1_ON;
199                         break;
200                 case '1':
201                         IoAdapter->capi_cfg.cfg_1 |=
202                             DIVA_XDI_CAPI_CFG_1_DYNAMIC_L1_ON;
203                         break;
204                 default:
205                         return (-EINVAL);
206                 }
207                 return (count);
208         }
209         return (-EINVAL);
210 }
211
212
213 /*
214 ** read dynamic_l1_down 
215 */
216 static int
217 read_d_l1_down(char *page, char **start, off_t off, int count, int *eof,
218                void *data)
219 {
220         int len = 0;
221         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
222         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
223
224         len += sprintf(page + len, "%s\n",
225                        (IoAdapter->capi_cfg.
226                         cfg_1 & DIVA_XDI_CAPI_CFG_1_DYNAMIC_L1_ON) ? "1" :
227                        "0");
228
229         if (off + count >= len)
230                 *eof = 1;
231         if (len < off)
232                 return 0;
233         *start = page + off;
234         return ((count < len - off) ? count : len - off);
235 }
236
237 /*
238 ** read group_optimization
239 */
240 static int
241 read_grp_opt(char *page, char **start, off_t off, int count, int *eof,
242              void *data)
243 {
244         int len = 0;
245         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
246         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
247
248         len += sprintf(page + len, "%s\n",
249                        (IoAdapter->capi_cfg.
250                         cfg_1 & DIVA_XDI_CAPI_CFG_1_GROUP_POPTIMIZATION_ON)
251                        ? "1" : "0");
252
253         if (off + count >= len)
254                 *eof = 1;
255         if (len < off)
256                 return 0;
257         *start = page + off;
258         return ((count < len - off) ? count : len - off);
259 }
260
261 /*
262 ** info write
263 */
264 static int
265 info_write(struct file *file, const char __user *buffer, unsigned long count,
266            void *data)
267 {
268         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
269         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
270         char c[4];
271
272         if (count <= 4)
273                 return -EINVAL;
274
275         if (copy_from_user(c, buffer, 4))
276                 return -EFAULT;
277
278         /* this is for test purposes only */
279         if (!memcmp(c, "trap", 4)) {
280                 (*(IoAdapter->os_trap_nfy_Fnc)) (IoAdapter, IoAdapter->ANum);
281                 return (count);
282         }
283         return (-EINVAL);
284 }
285
286 /*
287 ** info read
288 */
289 static int
290 info_read(char *page, char **start, off_t off, int count, int *eof,
291           void *data)
292 {
293         int i = 0;
294         int len = 0;
295         char *p;
296         char tmpser[16];
297         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
298         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
299
300         len +=
301             sprintf(page + len, "Name        : %s\n",
302                     IoAdapter->Properties.Name);
303         len += sprintf(page + len, "DSP state   : %08x\n", a->dsp_mask);
304         len += sprintf(page + len, "Channels    : %02d\n",
305                        IoAdapter->Properties.Channels);
306         len += sprintf(page + len, "E. max/used : %03d/%03d\n",
307                        IoAdapter->e_max, IoAdapter->e_count);
308         diva_get_vserial_number(IoAdapter, tmpser);
309         len += sprintf(page + len, "Serial      : %s\n", tmpser);
310         len +=
311             sprintf(page + len, "IRQ         : %d\n",
312                     IoAdapter->irq_info.irq_nr);
313         len += sprintf(page + len, "CardIndex   : %d\n", a->CardIndex);
314         len += sprintf(page + len, "CardOrdinal : %d\n", a->CardOrdinal);
315         len += sprintf(page + len, "Controller  : %d\n", a->controller);
316         len += sprintf(page + len, "Bus-Type    : %s\n",
317                        (a->Bus ==
318                         DIVAS_XDI_ADAPTER_BUS_ISA) ? "ISA" : "PCI");
319         len += sprintf(page + len, "Port-Name   : %s\n", a->port_name);
320         if (a->Bus == DIVAS_XDI_ADAPTER_BUS_PCI) {
321                 len +=
322                     sprintf(page + len, "PCI-bus     : %d\n",
323                             a->resources.pci.bus);
324                 len +=
325                     sprintf(page + len, "PCI-func    : %d\n",
326                             a->resources.pci.func);
327                 for (i = 0; i < 8; i++) {
328                         if (a->resources.pci.bar[i]) {
329                                 len +=
330                                     sprintf(page + len,
331                                             "Mem / I/O %d : 0x%x / mapped : 0x%lx",
332                                             i, a->resources.pci.bar[i],
333                                             (unsigned long) a->resources.
334                                             pci.addr[i]);
335                                 if (a->resources.pci.length[i]) {
336                                         len +=
337                                             sprintf(page + len,
338                                                     " / length : %d",
339                                                     a->resources.pci.
340                                                     length[i]);
341                                 }
342                                 len += sprintf(page + len, "\n");
343                         }
344                 }
345         }
346         if ((!a->xdi_adapter.port) &&
347             ((!a->xdi_adapter.ram) ||
348             (!a->xdi_adapter.reset)
349              || (!a->xdi_adapter.cfg))) {
350                 if (!IoAdapter->irq_info.irq_nr) {
351                         p = "slave";
352                 } else {
353                         p = "out of service";
354                 }
355         } else if (a->xdi_adapter.trapped) {
356                 p = "trapped";
357         } else if (a->xdi_adapter.Initialized) {
358                 p = "active";
359         } else {
360                 p = "ready";
361         }
362         len += sprintf(page + len, "State       : %s\n", p);
363
364         if (off + count >= len)
365                 *eof = 1;
366         if (len < off)
367                 return 0;
368         *start = page + off;
369         return ((count < len - off) ? count : len - off);
370 }
371
372 /*
373 ** adapter proc init/de-init
374 */
375
376 /* --------------------------------------------------------------------------
377     Create adapter directory and files in proc file system
378    -------------------------------------------------------------------------- */
379 int create_adapter_proc(diva_os_xdi_adapter_t * a)
380 {
381         struct proc_dir_entry *de, *pe;
382         char tmp[16];
383
384         sprintf(tmp, "%s%d", adapter_dir_name, a->controller);
385         if (!(de = create_proc_entry(tmp, S_IFDIR, proc_net_eicon)))
386                 return (0);
387         a->proc_adapter_dir = (void *) de;
388
389         if (!(pe =
390              create_proc_entry(info_proc_name, S_IFREG | S_IRUGO | S_IWUSR, de)))
391                 return (0);
392         a->proc_info = (void *) pe;
393         pe->write_proc = info_write;
394         pe->read_proc = info_read;
395         pe->data = a;
396
397         if ((pe = create_proc_entry(grp_opt_proc_name,
398                                S_IFREG | S_IRUGO | S_IWUSR, de))) {
399                 a->proc_grp_opt = (void *) pe;
400                 pe->write_proc = write_grp_opt;
401                 pe->read_proc = read_grp_opt;
402                 pe->data = a;
403         }
404         if ((pe = create_proc_entry(d_l1_down_proc_name,
405                                S_IFREG | S_IRUGO | S_IWUSR, de))) {
406                 a->proc_d_l1_down = (void *) pe;
407                 pe->write_proc = write_d_l1_down;
408                 pe->read_proc = read_d_l1_down;
409                 pe->data = a;
410         }
411
412         DBG_TRC(("proc entry %s created", tmp));
413
414         return (1);
415 }
416
417 /* --------------------------------------------------------------------------
418     Remove adapter directory and files in proc file system
419    -------------------------------------------------------------------------- */
420 void remove_adapter_proc(diva_os_xdi_adapter_t * a)
421 {
422         char tmp[16];
423
424         if (a->proc_adapter_dir) {
425                 if (a->proc_d_l1_down) {
426                         remove_proc_entry(d_l1_down_proc_name,
427                                           (struct proc_dir_entry *) a->proc_adapter_dir);
428                 }
429                 if (a->proc_grp_opt) {
430                         remove_proc_entry(grp_opt_proc_name,
431                                           (struct proc_dir_entry *) a->proc_adapter_dir);
432                 }
433                 if (a->proc_info) {
434                         remove_proc_entry(info_proc_name,
435                                           (struct proc_dir_entry *) a->proc_adapter_dir);
436                 }
437                 sprintf(tmp, "%s%d", adapter_dir_name, a->controller);
438                 remove_proc_entry(tmp, proc_net_eicon);
439                 DBG_TRC(("proc entry %s%d removed", adapter_dir_name,
440                          a->controller));
441         }
442 }