2 * @file me1600_device.c
4 * @brief ME-1600 device class implementation.
5 * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * @author Guenter Gebhardt
7 * @author Krzysztof Gantzke (k.gantzke@meilhaus.de)
11 * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
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.
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.
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.
36 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/slab.h>
44 #include "meinternal.h"
48 #include "mesubdevice.h"
49 #include "me1600_device.h"
51 static void me1600_set_registry(me1600_device_t
*subdevice
, uint32_t reg_base
);
52 static void me1600_destructor(struct me_device
*device
);
55 * @brief Global variable.
56 * This is working queue for runing a separate atask that will be responsible for work status (start, stop, timeouts).
58 static struct workqueue_struct
*me1600_workqueue
;
60 me_device_t
*me1600_pci_constructor(struct pci_dev
*pci_device
)
63 me1600_device_t
*me1600_device
;
64 me_subdevice_t
*subdevice
;
65 unsigned int chip_idx
;
68 PDEBUG("executed.\n");
70 // Allocate structure for device instance.
71 me1600_device
= kmalloc(sizeof(me1600_device_t
), GFP_KERNEL
);
74 PERROR("Cannot get memory for device instance.\n");
78 memset(me1600_device
, 0, sizeof(me1600_device_t
));
80 // Initialize base class structure.
81 err
= me_device_pci_init((me_device_t
*) me1600_device
, pci_device
);
85 PERROR("Cannot initialize device base class.\n");
88 // Initialize spin lock .
89 spin_lock_init(&me1600_device
->config_regs_lock
);
90 spin_lock_init(&me1600_device
->ao_shadows_lock
);
92 // Get the number of analog output subdevices.
94 me1600_versions_get_device_index(me1600_device
->base
.info
.pci
.
97 // Create shadow instance.
98 me1600_device
->ao_regs_shadows
.count
=
99 me1600_versions
[chip_idx
].ao_chips
;
100 me1600_device
->ao_regs_shadows
.registry
=
101 kmalloc(me1600_versions
[chip_idx
].ao_chips
* sizeof(unsigned long),
103 me1600_set_registry(me1600_device
,
104 me1600_device
->base
.info
.pci
.reg_bases
[2]);
105 me1600_device
->ao_regs_shadows
.shadow
=
106 kmalloc(me1600_versions
[chip_idx
].ao_chips
* sizeof(uint16_t),
108 me1600_device
->ao_regs_shadows
.mirror
=
109 kmalloc(me1600_versions
[chip_idx
].ao_chips
* sizeof(uint16_t),
112 // Create subdevice instances.
113 for (i
= 0; i
< me1600_versions
[chip_idx
].ao_chips
; i
++) {
115 (me_subdevice_t
*) me1600_ao_constructor(me1600_device
->
130 me_device_deinit((me_device_t
*) me1600_device
);
131 kfree(me1600_device
);
132 PERROR("Cannot get memory for subdevice.\n");
136 me_slist_add_subdevice_tail(&me1600_device
->base
.slist
,
140 // Overwrite base class methods.
141 me1600_device
->base
.me_device_destructor
= me1600_destructor
;
143 return (me_device_t
*) me1600_device
;
145 EXPORT_SYMBOL(me1600_pci_constructor
);
147 static void me1600_destructor(struct me_device
*device
)
149 me1600_device_t
*me1600_device
= (me1600_device_t
*) device
;
150 PDEBUG("executed.\n");
152 // Destroy shadow instance.
153 kfree(me1600_device
->ao_regs_shadows
.registry
);
154 kfree(me1600_device
->ao_regs_shadows
.shadow
);
155 kfree(me1600_device
->ao_regs_shadows
.mirror
);
157 me_device_deinit((me_device_t
*) me1600_device
);
158 kfree(me1600_device
);
161 static void me1600_set_registry(me1600_device_t
*subdevice
, uint32_t reg_base
)
162 { // Create shadow structure.
163 if (subdevice
->ao_regs_shadows
.count
>= 1) {
164 subdevice
->ao_regs_shadows
.registry
[0] =
165 (unsigned long)(reg_base
+ ME1600_CHANNEL_0_REG
);
167 if (subdevice
->ao_regs_shadows
.count
>= 2) {
168 subdevice
->ao_regs_shadows
.registry
[1] =
169 (unsigned long)(reg_base
+ ME1600_CHANNEL_1_REG
);
171 if (subdevice
->ao_regs_shadows
.count
>= 3) {
172 subdevice
->ao_regs_shadows
.registry
[2] =
173 (unsigned long)(reg_base
+ ME1600_CHANNEL_2_REG
);
175 if (subdevice
->ao_regs_shadows
.count
>= 4) {
176 subdevice
->ao_regs_shadows
.registry
[3] =
177 (unsigned long)(reg_base
+ ME1600_CHANNEL_3_REG
);
179 if (subdevice
->ao_regs_shadows
.count
>= 5) {
180 subdevice
->ao_regs_shadows
.registry
[4] =
181 (unsigned long)(reg_base
+ ME1600_CHANNEL_4_REG
);
183 if (subdevice
->ao_regs_shadows
.count
>= 6) {
184 subdevice
->ao_regs_shadows
.registry
[5] =
185 (unsigned long)(reg_base
+ ME1600_CHANNEL_5_REG
);
187 if (subdevice
->ao_regs_shadows
.count
>= 7) {
188 subdevice
->ao_regs_shadows
.registry
[6] =
189 (unsigned long)(reg_base
+ ME1600_CHANNEL_6_REG
);
191 if (subdevice
->ao_regs_shadows
.count
>= 8) {
192 subdevice
->ao_regs_shadows
.registry
[7] =
193 (unsigned long)(reg_base
+ ME1600_CHANNEL_7_REG
);
195 if (subdevice
->ao_regs_shadows
.count
>= 9) {
196 subdevice
->ao_regs_shadows
.registry
[8] =
197 (unsigned long)(reg_base
+ ME1600_CHANNEL_8_REG
);
199 if (subdevice
->ao_regs_shadows
.count
>= 10) {
200 subdevice
->ao_regs_shadows
.registry
[9] =
201 (unsigned long)(reg_base
+ ME1600_CHANNEL_9_REG
);
203 if (subdevice
->ao_regs_shadows
.count
>= 11) {
204 subdevice
->ao_regs_shadows
.registry
[10] =
205 (unsigned long)(reg_base
+ ME1600_CHANNEL_10_REG
);
207 if (subdevice
->ao_regs_shadows
.count
>= 12) {
208 subdevice
->ao_regs_shadows
.registry
[11] =
209 (unsigned long)(reg_base
+ ME1600_CHANNEL_11_REG
);
211 if (subdevice
->ao_regs_shadows
.count
>= 13) {
212 subdevice
->ao_regs_shadows
.registry
[12] =
213 (unsigned long)(reg_base
+ ME1600_CHANNEL_12_REG
);
215 if (subdevice
->ao_regs_shadows
.count
>= 14) {
216 subdevice
->ao_regs_shadows
.registry
[13] =
217 (unsigned long)(reg_base
+ ME1600_CHANNEL_13_REG
);
219 if (subdevice
->ao_regs_shadows
.count
>= 15) {
220 subdevice
->ao_regs_shadows
.registry
[14] =
221 (unsigned long)(reg_base
+ ME1600_CHANNEL_14_REG
);
223 if (subdevice
->ao_regs_shadows
.count
>= 16) {
224 subdevice
->ao_regs_shadows
.registry
[15] =
225 (unsigned long)(reg_base
+ ME1600_CHANNEL_15_REG
);
227 if (subdevice
->ao_regs_shadows
.count
> 16) {
228 PERROR("More than 16 outputs! (%d)\n",
229 subdevice
->ao_regs_shadows
.count
);
233 // Init and exit of module.
235 static int __init
me1600_init(void)
237 PDEBUG("executed\n.");
239 me1600_workqueue
= create_singlethread_workqueue("me1600");
243 static void __exit
me1600_exit(void)
245 PDEBUG("executed\n.");
247 flush_workqueue(me1600_workqueue
);
248 destroy_workqueue(me1600_workqueue
);
251 module_init(me1600_init
);
252 module_exit(me1600_exit
);
254 // Administrative stuff for modinfo.
256 ("Guenter Gebhardt <g.gebhardt@meilhaus.de> & Krzysztof Gantzke <k.gantzke@meilhaus.de>");
257 MODULE_DESCRIPTION("Device Driver Module for ME-1600 Device");
258 MODULE_SUPPORTED_DEVICE("Meilhaus ME-1600 Devices");
259 MODULE_LICENSE("GPL");