added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / staging / meilhaus / me1600_device.c
blob3bc2cb1dc869fc159444439c048f2d7b0437074b
1 /**
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)
8 */
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.
28 #ifndef __KERNEL__
29 # define __KERNEL__
30 #endif
32 #ifndef MODULE
33 # define MODULE
34 #endif
36 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/slab.h>
41 #include "meids.h"
42 #include "meerror.h"
43 #include "mecommon.h"
44 #include "meinternal.h"
46 #include "medebug.h"
47 #include "medevice.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);
54 /**
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)
62 int err;
63 me1600_device_t *me1600_device;
64 me_subdevice_t *subdevice;
65 unsigned int chip_idx;
66 int i;
68 PDEBUG("executed.\n");
70 // Allocate structure for device instance.
71 me1600_device = kmalloc(sizeof(me1600_device_t), GFP_KERNEL);
73 if (!me1600_device) {
74 PERROR("Cannot get memory for device instance.\n");
75 return NULL;
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);
83 if (err) {
84 kfree(me1600_device);
85 PERROR("Cannot initialize device base class.\n");
86 return NULL;
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.
93 chip_idx =
94 me1600_versions_get_device_index(me1600_device->base.info.pci.
95 device_id);
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),
102 GFP_KERNEL);
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),
107 GFP_KERNEL);
108 me1600_device->ao_regs_shadows.mirror =
109 kmalloc(me1600_versions[chip_idx].ao_chips * sizeof(uint16_t),
110 GFP_KERNEL);
112 // Create subdevice instances.
113 for (i = 0; i < me1600_versions[chip_idx].ao_chips; i++) {
114 subdevice =
115 (me_subdevice_t *) me1600_ao_constructor(me1600_device->
116 base.info.pci.
117 reg_bases[2], i,
118 ((me1600_versions
119 [chip_idx].curr >
120 i) ? 1 : 0),
121 &me1600_device->
122 config_regs_lock,
123 &me1600_device->
124 ao_shadows_lock,
125 &me1600_device->
126 ao_regs_shadows,
127 me1600_workqueue);
129 if (!subdevice) {
130 me_device_deinit((me_device_t *) me1600_device);
131 kfree(me1600_device);
132 PERROR("Cannot get memory for subdevice.\n");
133 return NULL;
136 me_slist_add_subdevice_tail(&me1600_device->base.slist,
137 subdevice);
140 // Overwrite base class methods.
141 me1600_device->base.me_device_destructor = me1600_destructor;
143 return (me_device_t *) me1600_device;
146 static void me1600_destructor(struct me_device *device)
148 me1600_device_t *me1600_device = (me1600_device_t *) device;
149 PDEBUG("executed.\n");
151 // Destroy shadow instance.
152 kfree(me1600_device->ao_regs_shadows.registry);
153 kfree(me1600_device->ao_regs_shadows.shadow);
154 kfree(me1600_device->ao_regs_shadows.mirror);
156 me_device_deinit((me_device_t *) me1600_device);
157 kfree(me1600_device);
160 static void me1600_set_registry(me1600_device_t * subdevice, uint32_t reg_base)
161 { // Create shadow structure.
162 if (subdevice->ao_regs_shadows.count >= 1) {
163 subdevice->ao_regs_shadows.registry[0] =
164 (unsigned long)(reg_base + ME1600_CHANNEL_0_REG);
166 if (subdevice->ao_regs_shadows.count >= 2) {
167 subdevice->ao_regs_shadows.registry[1] =
168 (unsigned long)(reg_base + ME1600_CHANNEL_1_REG);
170 if (subdevice->ao_regs_shadows.count >= 3) {
171 subdevice->ao_regs_shadows.registry[2] =
172 (unsigned long)(reg_base + ME1600_CHANNEL_2_REG);
174 if (subdevice->ao_regs_shadows.count >= 4) {
175 subdevice->ao_regs_shadows.registry[3] =
176 (unsigned long)(reg_base + ME1600_CHANNEL_3_REG);
178 if (subdevice->ao_regs_shadows.count >= 5) {
179 subdevice->ao_regs_shadows.registry[4] =
180 (unsigned long)(reg_base + ME1600_CHANNEL_4_REG);
182 if (subdevice->ao_regs_shadows.count >= 6) {
183 subdevice->ao_regs_shadows.registry[5] =
184 (unsigned long)(reg_base + ME1600_CHANNEL_5_REG);
186 if (subdevice->ao_regs_shadows.count >= 7) {
187 subdevice->ao_regs_shadows.registry[6] =
188 (unsigned long)(reg_base + ME1600_CHANNEL_6_REG);
190 if (subdevice->ao_regs_shadows.count >= 8) {
191 subdevice->ao_regs_shadows.registry[7] =
192 (unsigned long)(reg_base + ME1600_CHANNEL_7_REG);
194 if (subdevice->ao_regs_shadows.count >= 9) {
195 subdevice->ao_regs_shadows.registry[8] =
196 (unsigned long)(reg_base + ME1600_CHANNEL_8_REG);
198 if (subdevice->ao_regs_shadows.count >= 10) {
199 subdevice->ao_regs_shadows.registry[9] =
200 (unsigned long)(reg_base + ME1600_CHANNEL_9_REG);
202 if (subdevice->ao_regs_shadows.count >= 11) {
203 subdevice->ao_regs_shadows.registry[10] =
204 (unsigned long)(reg_base + ME1600_CHANNEL_10_REG);
206 if (subdevice->ao_regs_shadows.count >= 12) {
207 subdevice->ao_regs_shadows.registry[11] =
208 (unsigned long)(reg_base + ME1600_CHANNEL_11_REG);
210 if (subdevice->ao_regs_shadows.count >= 13) {
211 subdevice->ao_regs_shadows.registry[12] =
212 (unsigned long)(reg_base + ME1600_CHANNEL_12_REG);
214 if (subdevice->ao_regs_shadows.count >= 14) {
215 subdevice->ao_regs_shadows.registry[13] =
216 (unsigned long)(reg_base + ME1600_CHANNEL_13_REG);
218 if (subdevice->ao_regs_shadows.count >= 15) {
219 subdevice->ao_regs_shadows.registry[14] =
220 (unsigned long)(reg_base + ME1600_CHANNEL_14_REG);
222 if (subdevice->ao_regs_shadows.count >= 16) {
223 subdevice->ao_regs_shadows.registry[15] =
224 (unsigned long)(reg_base + ME1600_CHANNEL_15_REG);
226 if (subdevice->ao_regs_shadows.count > 16) {
227 PERROR("More than 16 outputs! (%d)\n",
228 subdevice->ao_regs_shadows.count);
232 // Init and exit of module.
234 static int __init me1600_init(void)
236 PDEBUG("executed\n.");
238 me1600_workqueue = create_singlethread_workqueue("me1600");
239 return 0;
242 static void __exit me1600_exit(void)
244 PDEBUG("executed\n.");
246 flush_workqueue(me1600_workqueue);
247 destroy_workqueue(me1600_workqueue);
250 module_init(me1600_init);
251 module_exit(me1600_exit);
253 // Administrative stuff for modinfo.
254 MODULE_AUTHOR
255 ("Guenter Gebhardt <g.gebhardt@meilhaus.de> & Krzysztof Gantzke <k.gantzke@meilhaus.de>");
256 MODULE_DESCRIPTION("Device Driver Module for ME-1600 Device");
257 MODULE_SUPPORTED_DEVICE("Meilhaus ME-1600 Devices");
258 MODULE_LICENSE("GPL");
260 // Export the constructor.
261 EXPORT_SYMBOL(me1600_pci_constructor);