Merge branch 'for-2.6.31' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / staging / meilhaus / me1400_device.c
1 /**
2  * @file me1400_device.c
3  *
4  * @brief ME-1400 device instance.
5  * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
6  * @author Guenter Gebhardt
7  * @author Krzysztof Gantzke    (k.gantzke@meilhaus.de)
8  */
9
10 /*
11  * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
12  *
13  * This file is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 /*
29  * User application could also include the kernel header files. But the
30  * real kernel functions are protected by #ifdef __KERNEL__.
31  */
32 #ifndef __KERNEL__
33 #  define __KERNEL__
34 #endif
35
36 /*
37  * This must be defined before module.h is included. Not needed, when
38  * it is a built in driver.
39  */
40 #ifndef MODULE
41 #  define MODULE
42 #endif
43
44 #include <linux/module.h>
45
46 #include <linux/pci.h>
47 #include <linux/slab.h>
48 #include <linux/sched.h>
49 #include <linux/interrupt.h>
50
51 #include "meids.h"
52 #include "meerror.h"
53 #include "mecommon.h"
54 #include "meinternal.h"
55
56 #include "medebug.h"
57
58 #include "me1400_device.h"
59 #include "me8254.h"
60 #include "me8254_reg.h"
61 #include "me8255.h"
62 #include "me1400_ext_irq.h"
63
64 me_device_t *me1400_pci_constructor(struct pci_dev *pci_device)
65 {
66         int err;
67         me1400_device_t *me1400_device;
68         me_subdevice_t *subdevice;
69         unsigned int version_idx;
70         unsigned int me8255_idx;
71         unsigned int dio_idx;
72         unsigned int me8254_idx;
73         unsigned int ctr_idx;
74         unsigned int ext_irq_idx;
75
76         PDEBUG("executed.\n");
77
78         // Allocate structure for device instance.
79         me1400_device = kmalloc(sizeof(me1400_device_t), GFP_KERNEL);
80
81         if (!me1400_device) {
82                 PERROR("Cannot get memory for 1400ate device instance.\n");
83                 return NULL;
84         }
85
86         memset(me1400_device, 0, sizeof(me1400_device_t));
87
88         // Initialize base class structure.
89         err = me_device_pci_init((me_device_t *) me1400_device, pci_device);
90
91         if (err) {
92                 kfree(me1400_device);
93                 PERROR("Cannot initialize device base class.\n");
94                 return NULL;
95         }
96
97         /* Check for ME1400 extension device. If detected we fake a ME-1400 D device id. */
98         if (me1400_device->base.info.pci.device_id ==
99             PCI_DEVICE_ID_MEILHAUS_ME140C) {
100                 uint8_t ctrl;
101                 ctrl =
102                     inb(me1400_device->base.info.pci.reg_bases[2] +
103                         ME1400D_CLK_SRC_2_REG);
104                 PDEBUG_REG("xxx_reg inb(0x%X+0x%X)=0x%x\n",
105                            me1400_device->base.info.pci.reg_bases[2],
106                            ME1400D_CLK_SRC_2_REG, ctrl);
107                 outb(ctrl | 0xF0,
108                      me1400_device->base.info.pci.reg_bases[2] +
109                      ME1400D_CLK_SRC_2_REG);
110                 PDEBUG_REG("xxx_reg outb(0x%X+0x%X)=0x%x\n",
111                            me1400_device->base.info.pci.reg_bases[2],
112                            ME1400D_CLK_SRC_2_REG, ctrl | 0xF0);
113                 ctrl =
114                     inb(me1400_device->base.info.pci.reg_bases[2] +
115                         ME1400D_CLK_SRC_2_REG);
116                 PDEBUG_REG("xxx_reg inb(0x%X+0x%X)=0x%x\n",
117                            me1400_device->base.info.pci.reg_bases[2],
118                            ME1400D_CLK_SRC_2_REG, ctrl);
119
120                 if ((ctrl & 0xF0) == 0xF0) {
121                         PINFO("ME1400 D detected.\n");
122                         me1400_device->base.info.pci.device_id =
123                             PCI_DEVICE_ID_MEILHAUS_ME140D;
124                 }
125         }
126
127         /* Initialize global stuff of digital i/o subdevices. */
128         for (me8255_idx = 0; me8255_idx < ME1400_MAX_8255; me8255_idx++) {
129                 me1400_device->dio_current_mode[me8255_idx] = 0;
130                 spin_lock_init(&me1400_device->dio_ctrl_reg_lock[me8255_idx]);
131         }
132
133         /* Initialize global stuff of counter subdevices. */
134         spin_lock_init(&me1400_device->clk_src_reg_lock);
135
136         for (me8254_idx = 0; me8254_idx < ME1400_MAX_8254; me8254_idx++)
137                 spin_lock_init(&me1400_device->ctr_ctrl_reg_lock[me8254_idx]);
138
139         /* Get the index in the device version information table. */
140         version_idx =
141             me1400_versions_get_device_index(me1400_device->base.info.pci.
142                                              device_id);
143
144         /* Generate DIO subdevice instances. */
145         for (me8255_idx = 0;
146              me8255_idx < me1400_versions[version_idx].dio_chips;
147              me8255_idx++) {
148                 for (dio_idx = 0; dio_idx < 3; dio_idx++) {
149                         subdevice =
150                             (me_subdevice_t *)
151                             me8255_constructor(me1400_versions[version_idx].
152                                                device_id,
153                                                me1400_device->base.info.pci.
154                                                reg_bases[2], me8255_idx,
155                                                dio_idx,
156                                                &me1400_device->
157                                                dio_current_mode[me8255_idx],
158                                                &me1400_device->
159                                                dio_ctrl_reg_lock[me8255_idx]);
160
161                         if (!subdevice) {
162                                 me_device_deinit((me_device_t *) me1400_device);
163                                 kfree(me1400_device);
164                                 PERROR("Cannot get memory for subdevice.\n");
165                                 return NULL;
166                         }
167
168                         me_slist_add_subdevice_tail(&me1400_device->base.slist,
169                                                     subdevice);
170                 }
171         }
172
173         /* Generate counter subdevice instances. */
174         for (me8254_idx = 0;
175              me8254_idx < me1400_versions[version_idx].ctr_chips;
176              me8254_idx++) {
177                 for (ctr_idx = 0; ctr_idx < 3; ctr_idx++) {
178                         subdevice =
179                             (me_subdevice_t *)
180                             me8254_constructor(me1400_device->base.info.pci.
181                                                device_id,
182                                                me1400_device->base.info.pci.
183                                                reg_bases[2], me8254_idx,
184                                                ctr_idx,
185                                                &me1400_device->
186                                                ctr_ctrl_reg_lock[me8254_idx],
187                                                &me1400_device->
188                                                clk_src_reg_lock);
189
190                         if (!subdevice) {
191                                 me_device_deinit((me_device_t *) me1400_device);
192                                 kfree(me1400_device);
193                                 PERROR("Cannot get memory for subdevice.\n");
194                                 return NULL;
195                         }
196
197                         me_slist_add_subdevice_tail(&me1400_device->base.slist,
198                                                     subdevice);
199                 }
200         }
201
202         /* Generate external interrupt subdevice instances. */
203         for (ext_irq_idx = 0;
204              ext_irq_idx < me1400_versions[version_idx].ext_irq_subdevices;
205              ext_irq_idx++) {
206                 subdevice =
207                     (me_subdevice_t *)
208                     me1400_ext_irq_constructor(me1400_device->base.info.pci.
209                                                device_id,
210                                                me1400_device->base.info.pci.
211                                                reg_bases[1],
212                                                me1400_device->base.info.pci.
213                                                reg_bases[2],
214                                                &me1400_device->clk_src_reg_lock,
215                                                me1400_device->base.irq);
216
217                 if (!subdevice) {
218                         me_device_deinit((me_device_t *) me1400_device);
219                         kfree(me1400_device);
220                         PERROR("Cannot get memory for subdevice.\n");
221                         return NULL;
222                 }
223
224                 me_slist_add_subdevice_tail(&me1400_device->base.slist,
225                                             subdevice);
226         }
227
228         return (me_device_t *) me1400_device;
229 }
230 EXPORT_SYMBOL(me1400_pci_constructor);
231
232 // Init and exit of module.
233
234 static int __init me1400_init(void)
235 {
236         PDEBUG("executed.\n");
237         return 0;
238 }
239
240 static void __exit me1400_exit(void)
241 {
242         PDEBUG("executed.\n");
243 }
244
245 module_init(me1400_init);
246 module_exit(me1400_exit);
247
248 // Administrative stuff for modinfo.
249 MODULE_AUTHOR
250     ("Guenter Gebhardt <g.gebhardt@meilhaus.de> & Krzysztof Gantzke <k.gantzke@meilhaus.de>");
251 MODULE_DESCRIPTION("Device Driver Module for Meilhaus ME-14xx devices");
252 MODULE_SUPPORTED_DEVICE("Meilhaus ME-14xx MIO devices");
253 MODULE_LICENSE("GPL");