ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 *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 *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 *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                 switch (buffer[0]) {
162                 case '0':
163                         IoAdapter->capi_cfg.cfg_1 &=
164                             ~DIVA_XDI_CAPI_CFG_1_GROUP_POPTIMIZATION_ON;
165                         break;
166                 case '1':
167                         IoAdapter->capi_cfg.cfg_1 |=
168                             DIVA_XDI_CAPI_CFG_1_GROUP_POPTIMIZATION_ON;
169                         break;
170                 default:
171                         return (-EINVAL);
172                 }
173                 return (count);
174         }
175         return (-EINVAL);
176 }
177
178 /*
179 ** write dynamic_l1_down
180 */
181 static int
182 write_d_l1_down(struct file *file, const char *buffer, unsigned long count,
183                 void *data)
184 {
185         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
186         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
187
188         if ((count == 1) || (count == 2)) {
189                 switch (buffer[0]) {
190                 case '0':
191                         IoAdapter->capi_cfg.cfg_1 &=
192                             ~DIVA_XDI_CAPI_CFG_1_DYNAMIC_L1_ON;
193                         break;
194                 case '1':
195                         IoAdapter->capi_cfg.cfg_1 |=
196                             DIVA_XDI_CAPI_CFG_1_DYNAMIC_L1_ON;
197                         break;
198                 default:
199                         return (-EINVAL);
200                 }
201                 return (count);
202         }
203         return (-EINVAL);
204 }
205
206
207 /*
208 ** read dynamic_l1_down 
209 */
210 static int
211 read_d_l1_down(char *page, char **start, off_t off, int count, int *eof,
212                void *data)
213 {
214         int len = 0;
215         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
216         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
217
218         len += sprintf(page + len, "%s\n",
219                        (IoAdapter->capi_cfg.
220                         cfg_1 & DIVA_XDI_CAPI_CFG_1_DYNAMIC_L1_ON) ? "1" :
221                        "0");
222
223         if (off + count >= len)
224                 *eof = 1;
225         if (len < off)
226                 return 0;
227         *start = page + off;
228         return ((count < len - off) ? count : len - off);
229 }
230
231 /*
232 ** read group_optimization
233 */
234 static int
235 read_grp_opt(char *page, char **start, off_t off, int count, int *eof,
236              void *data)
237 {
238         int len = 0;
239         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
240         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
241
242         len += sprintf(page + len, "%s\n",
243                        (IoAdapter->capi_cfg.
244                         cfg_1 & DIVA_XDI_CAPI_CFG_1_GROUP_POPTIMIZATION_ON)
245                        ? "1" : "0");
246
247         if (off + count >= len)
248                 *eof = 1;
249         if (len < off)
250                 return 0;
251         *start = page + off;
252         return ((count < len - off) ? count : len - off);
253 }
254
255 /*
256 ** info write
257 */
258 static int
259 info_write(struct file *file, const char *buffer, unsigned long count,
260            void *data)
261 {
262         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
263         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
264
265         /* this is for test purposes only */
266         if ((count > 4) && (!memcmp(buffer, "trap", 4))) {
267                 (*(IoAdapter->os_trap_nfy_Fnc)) (IoAdapter, IoAdapter->ANum);
268                 return (count);
269         }
270         return (-EINVAL);
271 }
272
273 /*
274 ** info read
275 */
276 static int
277 info_read(char *page, char **start, off_t off, int count, int *eof,
278           void *data)
279 {
280         int i = 0;
281         int len = 0;
282         char *p;
283         char tmpser[16];
284         diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) data;
285         PISDN_ADAPTER IoAdapter = IoAdapters[a->controller - 1];
286
287         len +=
288             sprintf(page + len, "Name        : %s\n",
289                     IoAdapter->Properties.Name);
290         len += sprintf(page + len, "DSP state   : %08x\n", a->dsp_mask);
291         len += sprintf(page + len, "Channels    : %02d\n",
292                        IoAdapter->Properties.Channels);
293         len += sprintf(page + len, "E. max/used : %03d/%03d\n",
294                        IoAdapter->e_max, IoAdapter->e_count);
295         diva_get_vserial_number(IoAdapter, tmpser);
296         len += sprintf(page + len, "Serial      : %s\n", tmpser);
297         len +=
298             sprintf(page + len, "IRQ         : %d\n",
299                     IoAdapter->irq_info.irq_nr);
300         len += sprintf(page + len, "CardIndex   : %d\n", a->CardIndex);
301         len += sprintf(page + len, "CardOrdinal : %d\n", a->CardOrdinal);
302         len += sprintf(page + len, "Controller  : %d\n", a->controller);
303         len += sprintf(page + len, "Bus-Type    : %s\n",
304                        (a->Bus ==
305                         DIVAS_XDI_ADAPTER_BUS_ISA) ? "ISA" : "PCI");
306         len += sprintf(page + len, "Port-Name   : %s\n", a->port_name);
307         if (a->Bus == DIVAS_XDI_ADAPTER_BUS_PCI) {
308                 len +=
309                     sprintf(page + len, "PCI-bus     : %d\n",
310                             a->resources.pci.bus);
311                 len +=
312                     sprintf(page + len, "PCI-func    : %d\n",
313                             a->resources.pci.func);
314                 for (i = 0; i < 8; i++) {
315                         if (a->resources.pci.bar[i]) {
316                                 len +=
317                                     sprintf(page + len,
318                                             "Mem / I/O %d : 0x%x / mapped : 0x%lx",
319                                             i, a->resources.pci.bar[i],
320                                             (unsigned long) a->resources.
321                                             pci.addr[i]);
322                                 if (a->resources.pci.length[i]) {
323                                         len +=
324                                             sprintf(page + len,
325                                                     " / length : %d",
326                                                     a->resources.pci.
327                                                     length[i]);
328                                 }
329                                 len += sprintf(page + len, "\n");
330                         }
331                 }
332         }
333         if ((!a->xdi_adapter.port) &&
334             ((!a->xdi_adapter.ram) ||
335             (!a->xdi_adapter.reset)
336              || (!a->xdi_adapter.cfg))) {
337                 if (!IoAdapter->irq_info.irq_nr) {
338                         p = "slave";
339                 } else {
340                         p = "out of service";
341                 }
342         } else if (a->xdi_adapter.trapped) {
343                 p = "trapped";
344         } else if (a->xdi_adapter.Initialized) {
345                 p = "active";
346         } else {
347                 p = "ready";
348         }
349         len += sprintf(page + len, "State       : %s\n", p);
350
351         if (off + count >= len)
352                 *eof = 1;
353         if (len < off)
354                 return 0;
355         *start = page + off;
356         return ((count < len - off) ? count : len - off);
357 }
358
359 /*
360 ** adapter proc init/de-init
361 */
362
363 /* --------------------------------------------------------------------------
364     Create adapter directory and files in proc file system
365    -------------------------------------------------------------------------- */
366 int create_adapter_proc(diva_os_xdi_adapter_t * a)
367 {
368         struct proc_dir_entry *de, *pe;
369         char tmp[16];
370
371         sprintf(tmp, "%s%d", adapter_dir_name, a->controller);
372         if (!(de = create_proc_entry(tmp, S_IFDIR, proc_net_eicon)))
373                 return (0);
374         a->proc_adapter_dir = (void *) de;
375
376         if (!(pe =
377              create_proc_entry(info_proc_name, S_IFREG | S_IRUGO | S_IWUSR, de)))
378                 return (0);
379         a->proc_info = (void *) pe;
380         pe->write_proc = info_write;
381         pe->read_proc = info_read;
382         pe->data = a;
383
384         if ((pe = create_proc_entry(grp_opt_proc_name,
385                                S_IFREG | S_IRUGO | S_IWUSR, de))) {
386                 a->proc_grp_opt = (void *) pe;
387                 pe->write_proc = write_grp_opt;
388                 pe->read_proc = read_grp_opt;
389                 pe->data = a;
390         }
391         if ((pe = create_proc_entry(d_l1_down_proc_name,
392                                S_IFREG | S_IRUGO | S_IWUSR, de))) {
393                 a->proc_d_l1_down = (void *) pe;
394                 pe->write_proc = write_d_l1_down;
395                 pe->read_proc = read_d_l1_down;
396                 pe->data = a;
397         }
398
399         DBG_TRC(("proc entry %s created", tmp));
400
401         return (1);
402 }
403
404 /* --------------------------------------------------------------------------
405     Remove adapter directory and files in proc file system
406    -------------------------------------------------------------------------- */
407 void remove_adapter_proc(diva_os_xdi_adapter_t * a)
408 {
409         char tmp[16];
410
411         if (a->proc_adapter_dir) {
412                 if (a->proc_d_l1_down) {
413                         remove_proc_entry(d_l1_down_proc_name,
414                                           (struct proc_dir_entry *) a->proc_adapter_dir);
415                 }
416                 if (a->proc_grp_opt) {
417                         remove_proc_entry(grp_opt_proc_name,
418                                           (struct proc_dir_entry *) a->proc_adapter_dir);
419                 }
420                 if (a->proc_info) {
421                         remove_proc_entry(info_proc_name,
422                                           (struct proc_dir_entry *) a->proc_adapter_dir);
423                 }
424                 sprintf(tmp, "%s%d", adapter_dir_name, a->controller);
425                 remove_proc_entry(tmp, proc_net_eicon);
426                 DBG_TRC(("proc entry %s%d removed", adapter_dir_name,
427                          a->controller));
428         }
429 }