Staging: meilhaus: some checkpatch.pl cleanup
[safe/jmp/linux-2.6] / 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 #include <linux/version.h>
51
52 #include "meids.h"
53 #include "meerror.h"
54 #include "mecommon.h"
55 #include "meinternal.h"
56
57 #include "medebug.h"
58
59 #include "me1400_device.h"
60 #include "me8254.h"
61 #include "me8254_reg.h"
62 #include "me8255.h"
63 #include "me1400_ext_irq.h"
64
65 me_device_t *me1400_pci_constructor(struct pci_dev *pci_device)
66 {
67         int err;
68         me1400_device_t *me1400_device;
69         me_subdevice_t *subdevice;
70         unsigned int version_idx;
71         unsigned int me8255_idx;
72         unsigned int dio_idx;
73         unsigned int me8254_idx;
74         unsigned int ctr_idx;
75         unsigned int ext_irq_idx;
76
77         PDEBUG("executed.\n");
78
79         // Allocate structure for device instance.
80         me1400_device = kmalloc(sizeof(me1400_device_t), GFP_KERNEL);
81
82         if (!me1400_device) {
83                 PERROR("Cannot get memory for 1400ate device instance.\n");
84                 return NULL;
85         }
86
87         memset(me1400_device, 0, sizeof(me1400_device_t));
88
89         // Initialize base class structure.
90         err = me_device_pci_init((me_device_t *) me1400_device, pci_device);
91
92         if (err) {
93                 kfree(me1400_device);
94                 PERROR("Cannot initialize device base class.\n");
95                 return NULL;
96         }
97
98         /* Check for ME1400 extension device. If detected we fake a ME-1400 D device id. */
99         if (me1400_device->base.info.pci.device_id ==
100             PCI_DEVICE_ID_MEILHAUS_ME140C) {
101                 uint8_t ctrl;
102                 ctrl =
103                     inb(me1400_device->base.info.pci.reg_bases[2] +
104                         ME1400D_CLK_SRC_2_REG);
105                 PDEBUG_REG("xxx_reg inb(0x%X+0x%X)=0x%x\n",
106                            me1400_device->base.info.pci.reg_bases[2],
107                            ME1400D_CLK_SRC_2_REG, ctrl);
108                 outb(ctrl | 0xF0,
109                      me1400_device->base.info.pci.reg_bases[2] +
110                      ME1400D_CLK_SRC_2_REG);
111                 PDEBUG_REG("xxx_reg outb(0x%X+0x%X)=0x%x\n",
112                            me1400_device->base.info.pci.reg_bases[2],
113                            ME1400D_CLK_SRC_2_REG, ctrl | 0xF0);
114                 ctrl =
115                     inb(me1400_device->base.info.pci.reg_bases[2] +
116                         ME1400D_CLK_SRC_2_REG);
117                 PDEBUG_REG("xxx_reg inb(0x%X+0x%X)=0x%x\n",
118                            me1400_device->base.info.pci.reg_bases[2],
119                            ME1400D_CLK_SRC_2_REG, ctrl);
120
121                 if ((ctrl & 0xF0) == 0xF0) {
122                         PINFO("ME1400 D detected.\n");
123                         me1400_device->base.info.pci.device_id =
124                             PCI_DEVICE_ID_MEILHAUS_ME140D;
125                 }
126         }
127
128         /* Initialize global stuff of digital i/o subdevices. */
129         for (me8255_idx = 0; me8255_idx < ME1400_MAX_8255; me8255_idx++) {
130                 me1400_device->dio_current_mode[me8255_idx] = 0;
131                 spin_lock_init(&me1400_device->dio_ctrl_reg_lock[me8255_idx]);
132         }
133
134         /* Initialize global stuff of counter subdevices. */
135         spin_lock_init(&me1400_device->clk_src_reg_lock);
136
137         for (me8254_idx = 0; me8254_idx < ME1400_MAX_8254; me8254_idx++)
138                 spin_lock_init(&me1400_device->ctr_ctrl_reg_lock[me8254_idx]);
139
140         /* Get the index in the device version information table. */
141         version_idx =
142             me1400_versions_get_device_index(me1400_device->base.info.pci.
143                                              device_id);
144
145         /* Generate DIO subdevice instances. */
146         for (me8255_idx = 0;
147              me8255_idx < me1400_versions[version_idx].dio_chips;
148              me8255_idx++) {
149                 for (dio_idx = 0; dio_idx < 3; dio_idx++) {
150                         subdevice =
151                             (me_subdevice_t *)
152                             me8255_constructor(me1400_versions[version_idx].
153                                                device_id,
154                                                me1400_device->base.info.pci.
155                                                reg_bases[2], me8255_idx,
156                                                dio_idx,
157                                                &me1400_device->
158                                                dio_current_mode[me8255_idx],
159                                                &me1400_device->
160                                                dio_ctrl_reg_lock[me8255_idx]);
161
162                         if (!subdevice) {
163                                 me_device_deinit((me_device_t *) me1400_device);
164                                 kfree(me1400_device);
165                                 PERROR("Cannot get memory for subdevice.\n");
166                                 return NULL;
167                         }
168
169                         me_slist_add_subdevice_tail(&me1400_device->base.slist,
170                                                     subdevice);
171                 }
172         }
173
174         /* Generate counter subdevice instances. */
175         for (me8254_idx = 0;
176              me8254_idx < me1400_versions[version_idx].ctr_chips;
177              me8254_idx++) {
178                 for (ctr_idx = 0; ctr_idx < 3; ctr_idx++) {
179                         subdevice =
180                             (me_subdevice_t *)
181                             me8254_constructor(me1400_device->base.info.pci.
182                                                device_id,
183                                                me1400_device->base.info.pci.
184                                                reg_bases[2], me8254_idx,
185                                                ctr_idx,
186                                                &me1400_device->
187                                                ctr_ctrl_reg_lock[me8254_idx],
188                                                &me1400_device->
189                                                clk_src_reg_lock);
190
191                         if (!subdevice) {
192                                 me_device_deinit((me_device_t *) me1400_device);
193                                 kfree(me1400_device);
194                                 PERROR("Cannot get memory for subdevice.\n");
195                                 return NULL;
196                         }
197
198                         me_slist_add_subdevice_tail(&me1400_device->base.slist,
199                                                     subdevice);
200                 }
201         }
202
203         /* Generate external interrupt subdevice instances. */
204         for (ext_irq_idx = 0;
205              ext_irq_idx < me1400_versions[version_idx].ext_irq_subdevices;
206              ext_irq_idx++) {
207                 subdevice =
208                     (me_subdevice_t *)
209                     me1400_ext_irq_constructor(me1400_device->base.info.pci.
210                                                device_id,
211                                                me1400_device->base.info.pci.
212                                                reg_bases[1],
213                                                me1400_device->base.info.pci.
214                                                reg_bases[2],
215                                                &me1400_device->clk_src_reg_lock,
216                                                me1400_device->base.irq);
217
218                 if (!subdevice) {
219                         me_device_deinit((me_device_t *) me1400_device);
220                         kfree(me1400_device);
221                         PERROR("Cannot get memory for subdevice.\n");
222                         return NULL;
223                 }
224
225                 me_slist_add_subdevice_tail(&me1400_device->base.slist,
226                                             subdevice);
227         }
228
229         return (me_device_t *) me1400_device;
230 }
231 EXPORT_SYMBOL(me1400_pci_constructor);
232
233 // Init and exit of module.
234
235 static int __init me1400_init(void)
236 {
237         PDEBUG("executed.\n");
238         return 0;
239 }
240
241 static void __exit me1400_exit(void)
242 {
243         PDEBUG("executed.\n");
244 }
245
246 module_init(me1400_init);
247 module_exit(me1400_exit);
248
249 // Administrative stuff for modinfo.
250 MODULE_AUTHOR
251     ("Guenter Gebhardt <g.gebhardt@meilhaus.de> & Krzysztof Gantzke <k.gantzke@meilhaus.de>");
252 MODULE_DESCRIPTION("Device Driver Module for Meilhaus ME-14xx devices");
253 MODULE_SUPPORTED_DEVICE("Meilhaus ME-14xx MIO devices");
254 MODULE_LICENSE("GPL");