tracehook_notify_death: use task_detached() helper
[linux-2.6/mini2440.git] / drivers / staging / meilhaus / me1000_dio.c
blob87605a9108ae350d4fbbb858c170f8989361a138
1 /**
2 * @file me1000_dio.c
4 * @brief ME-1000 DIO subdevice instance.
5 * @note Copyright (C) 2006 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * @author Guenter Gebhardt
7 * @author Krzysztof Gantzke (k.gantzke@meilhaus.de)
8 */
11 * Copyright (C) 2006 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
33 * Includes
35 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <asm/io.h>
40 #include <linux/types.h>
42 #include "medefines.h"
43 #include "meinternal.h"
44 #include "meerror.h"
45 #include "medebug.h"
47 #include "me1000_dio_reg.h"
48 #include "me1000_dio.h"
51 * Defines
53 #define ME1000_DIO_MAGIC_NUMBER 0x1000 /**< The magic number of the class structure. */
56 * Functions
59 static int me1000_dio_io_reset_subdevice(struct me_subdevice *subdevice,
60 struct file *filep, int flags)
62 me1000_dio_subdevice_t *instance;
63 uint32_t tmp;
65 PDEBUG("executed.\n");
67 instance = (me1000_dio_subdevice_t *) subdevice;
69 if (flags) {
70 PERROR("Invalid flag specified.\n");
71 return ME_ERRNO_INVALID_FLAGS;
74 ME_SUBDEVICE_ENTER;
76 spin_lock(&instance->subdevice_lock);
77 spin_lock(instance->ctrl_reg_lock);
78 tmp = inl(instance->ctrl_reg);
79 tmp &= ~(0x1 << instance->dio_idx);
80 outl(tmp, instance->ctrl_reg);
81 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
82 instance->ctrl_reg - instance->reg_base, tmp);
83 spin_unlock(instance->ctrl_reg_lock);
85 outl(0x00000000, instance->port_reg);
86 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
87 instance->ctrl_reg - instance->reg_base, 0);
88 spin_unlock(&instance->subdevice_lock);
90 ME_SUBDEVICE_EXIT;
92 return ME_ERRNO_SUCCESS;
95 static int me1000_dio_io_single_config(struct me_subdevice *subdevice,
96 struct file *filep,
97 int channel,
98 int single_config,
99 int ref,
100 int trig_chan,
101 int trig_type, int trig_edge, int flags)
103 me1000_dio_subdevice_t *instance;
104 int err = ME_ERRNO_SUCCESS;
105 int ctrl;
106 int size =
107 flags & (ME_IO_SINGLE_CONFIG_DIO_BIT | ME_IO_SINGLE_CONFIG_DIO_BYTE
108 | ME_IO_SINGLE_CONFIG_DIO_WORD |
109 ME_IO_SINGLE_CONFIG_DIO_DWORD);
111 PDEBUG("executed.\n");
113 instance = (me1000_dio_subdevice_t *) subdevice;
115 ME_SUBDEVICE_ENTER;
117 spin_lock(&instance->subdevice_lock);
118 spin_lock(instance->ctrl_reg_lock);
119 ctrl = inl(instance->ctrl_reg);
121 switch (size) {
122 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
123 case ME_IO_SINGLE_CONFIG_DIO_DWORD:
124 if (channel == 0) {
125 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
126 ctrl &= ~(0x1 << instance->dio_idx);
127 } else if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
128 ctrl |= 0x1 << instance->dio_idx;
129 } else {
130 PERROR("Invalid port direction.\n");
131 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
133 } else {
134 PERROR("Invalid channel number.\n");
135 err = ME_ERRNO_INVALID_CHANNEL;
137 break;
139 default:
140 PERROR("Invalid flags.\n");
141 err = ME_ERRNO_INVALID_FLAGS;
144 if (!err) {
145 outl(ctrl, instance->ctrl_reg);
146 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
147 instance->reg_base,
148 instance->ctrl_reg - instance->reg_base, ctrl);
150 spin_unlock(instance->ctrl_reg_lock);
151 spin_unlock(&instance->subdevice_lock);
153 ME_SUBDEVICE_EXIT;
155 return err;
158 static int me1000_dio_io_single_read(struct me_subdevice *subdevice,
159 struct file *filep,
160 int channel,
161 int *value, int time_out, int flags)
163 me1000_dio_subdevice_t *instance;
164 int err = ME_ERRNO_SUCCESS;
166 PDEBUG("executed.\n");
168 instance = (me1000_dio_subdevice_t *) subdevice;
170 ME_SUBDEVICE_ENTER;
172 spin_lock(&instance->subdevice_lock);
173 switch (flags) {
174 case ME_IO_SINGLE_TYPE_DIO_BIT:
175 if ((channel >= 0) && (channel < 32)) {
176 *value = inl(instance->port_reg) & (0x1 << channel);
177 } else {
178 PERROR("Invalid bit number.\n");
179 err = ME_ERRNO_INVALID_CHANNEL;
181 break;
183 case ME_IO_SINGLE_TYPE_DIO_BYTE:
184 if ((channel >= 0) && (channel < 4)) {
185 *value =
186 (inl(instance->port_reg) >> (channel * 8)) & 0xFF;
187 } else {
188 PERROR("Invalid byte number.\n");
189 err = ME_ERRNO_INVALID_CHANNEL;
191 break;
193 case ME_IO_SINGLE_TYPE_DIO_WORD:
194 if ((channel >= 0) && (channel < 2)) {
195 *value =
196 (inl(instance->port_reg) >> (channel * 16)) &
197 0xFFFF;
198 } else {
199 PERROR("Invalid word number.\n");
200 err = ME_ERRNO_INVALID_CHANNEL;
202 break;
204 case ME_IO_SINGLE_NO_FLAGS:
205 case ME_IO_SINGLE_TYPE_DIO_DWORD:
206 if (channel == 0) {
207 *value = inl(instance->port_reg);
208 } else {
209 PERROR("Invalid dword number.\n");
210 err = ME_ERRNO_INVALID_CHANNEL;
212 break;
214 default:
215 PERROR("Invalid flags specified.\n");
216 err = ME_ERRNO_INVALID_FLAGS;
218 spin_unlock(&instance->subdevice_lock);
220 ME_SUBDEVICE_EXIT;
222 return err;
225 static int me1000_dio_io_single_write(struct me_subdevice *subdevice,
226 struct file *filep,
227 int channel,
228 int value, int time_out, int flags)
230 me1000_dio_subdevice_t *instance;
231 int err = ME_ERRNO_SUCCESS;
232 uint32_t config;
233 uint32_t state;
235 PDEBUG("executed.\n");
237 instance = (me1000_dio_subdevice_t *) subdevice;
239 ME_SUBDEVICE_ENTER spin_lock(&instance->subdevice_lock);
240 spin_lock(instance->ctrl_reg_lock);
241 config = inl(instance->ctrl_reg) & (0x1 << instance->dio_idx);
242 switch (flags) {
243 case ME_IO_SINGLE_TYPE_DIO_BIT:
244 if ((channel >= 0) && (channel < 32)) {
245 if (config) {
246 state = inl(instance->port_reg);
247 state =
248 value ? (state | (0x1 << channel)) : (state
250 ~(0x1
252 channel));
253 outl(state, instance->port_reg);
254 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
255 instance->reg_base,
256 instance->port_reg -
257 instance->reg_base, state);
258 } else {
259 PERROR("Port is not in output mode.\n");
260 err = ME_ERRNO_PREVIOUS_CONFIG;
262 } else {
263 PERROR("Invalid bit number.\n");
264 err = ME_ERRNO_INVALID_CHANNEL;
266 break;
268 case ME_IO_SINGLE_TYPE_DIO_BYTE:
269 if ((channel >= 0) && (channel < 4)) {
270 if (config) {
271 state = inl(instance->port_reg);
272 state &= ~(0xFF << (channel * 8));
273 state |= (value & 0xFF) << (channel * 8);
274 outl(state, instance->port_reg);
275 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
276 instance->reg_base,
277 instance->port_reg -
278 instance->reg_base, state);
279 } else {
280 PERROR("Port is not in output mode.\n");
281 err = ME_ERRNO_PREVIOUS_CONFIG;
283 } else {
284 PERROR("Invalid byte number.\n");
285 err = ME_ERRNO_INVALID_CHANNEL;
287 break;
289 case ME_IO_SINGLE_TYPE_DIO_WORD:
290 if ((channel >= 0) && (channel < 2)) {
291 if (config) {
292 state = inl(instance->port_reg);
293 state &= ~(0xFFFF << (channel * 16));
294 state |= (value & 0xFFFF) << (channel * 16);
295 outl(state, instance->port_reg);
296 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
297 instance->reg_base,
298 instance->port_reg -
299 instance->reg_base, state);
300 } else {
301 PERROR("Port is not in output mode.\n");
302 err = ME_ERRNO_PREVIOUS_CONFIG;
304 } else {
305 PERROR("Invalid word number.\n");
306 err = ME_ERRNO_INVALID_CHANNEL;
308 break;
310 case ME_IO_SINGLE_NO_FLAGS:
311 case ME_IO_SINGLE_TYPE_DIO_DWORD:
312 if (channel == 0) {
313 if (config) {
314 outl(value, instance->port_reg);
315 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
316 instance->reg_base,
317 instance->port_reg -
318 instance->reg_base, value);
319 } else {
320 PERROR("Port is not in output mode.\n");
321 err = ME_ERRNO_PREVIOUS_CONFIG;
323 } else {
324 PERROR("Invalid dword number.\n");
325 err = ME_ERRNO_INVALID_CHANNEL;
327 break;
329 default:
330 PERROR("Invalid flags specified.\n");
331 err = ME_ERRNO_INVALID_FLAGS;
333 spin_unlock(instance->ctrl_reg_lock);
334 spin_unlock(&instance->subdevice_lock);
336 ME_SUBDEVICE_EXIT;
338 return err;
341 static int me1000_dio_query_number_channels(struct me_subdevice *subdevice,
342 int *number)
344 PDEBUG("executed.\n");
345 *number = ME1000_DIO_NUMBER_CHANNELS;
346 return ME_ERRNO_SUCCESS;
349 static int me1000_dio_query_subdevice_type(struct me_subdevice *subdevice,
350 int *type, int *subtype)
352 PDEBUG("executed.\n");
353 *type = ME_TYPE_DIO;
354 *subtype = ME_SUBTYPE_SINGLE;
355 return ME_ERRNO_SUCCESS;
358 static int me1000_dio_query_subdevice_caps(struct me_subdevice *subdevice,
359 int *caps)
361 me1000_dio_subdevice_t *instance;
363 PDEBUG("executed.\n");
365 instance = (me1000_dio_subdevice_t *) subdevice;
367 *caps = ME_CAPS_DIO_DIR_DWORD;
369 return ME_ERRNO_SUCCESS;
372 me1000_dio_subdevice_t *me1000_dio_constructor(uint32_t reg_base,
373 unsigned int dio_idx,
374 spinlock_t * ctrl_reg_lock)
376 me1000_dio_subdevice_t *subdevice;
377 int err;
379 PDEBUG("executed.\n");
381 /* Allocate memory for subdevice instance */
382 subdevice = kmalloc(sizeof(me1000_dio_subdevice_t), GFP_KERNEL);
384 if (!subdevice) {
385 PERROR("Cannot get memory for ME-1000 DIO instance.\n");
386 return NULL;
389 memset(subdevice, 0, sizeof(me1000_dio_subdevice_t));
391 /* Check if counter index is out of range */
393 if (dio_idx >= ME1000_DIO_NUMBER_PORTS) {
394 PERROR("DIO index is out of range.\n");
395 kfree(subdevice);
396 return NULL;
399 /* Initialize subdevice base class */
400 err = me_subdevice_init(&subdevice->base);
402 if (err) {
403 PERROR("Cannot initialize subdevice base class instance.\n");
404 kfree(subdevice);
405 return NULL;
407 // Initialize spin locks.
408 spin_lock_init(&subdevice->subdevice_lock);
409 subdevice->ctrl_reg_lock = ctrl_reg_lock;
411 /* Save the DIO index */
412 subdevice->dio_idx = dio_idx;
414 /* Initialize registers. */
415 #ifdef MEDEBUG_DEBUG_REG
416 subdevice->reg_base = reg_base;
417 #endif
418 subdevice->ctrl_reg = reg_base + ME1000_PORT_MODE;
419 subdevice->port_reg =
420 reg_base + ME1000_PORT + (dio_idx * ME1000_PORT_STEP);
422 /* Override base class methods. */
423 subdevice->base.me_subdevice_io_reset_subdevice =
424 me1000_dio_io_reset_subdevice;
425 subdevice->base.me_subdevice_io_single_config =
426 me1000_dio_io_single_config;
427 subdevice->base.me_subdevice_io_single_read = me1000_dio_io_single_read;
428 subdevice->base.me_subdevice_io_single_write =
429 me1000_dio_io_single_write;
430 subdevice->base.me_subdevice_query_number_channels =
431 me1000_dio_query_number_channels;
432 subdevice->base.me_subdevice_query_subdevice_type =
433 me1000_dio_query_subdevice_type;
434 subdevice->base.me_subdevice_query_subdevice_caps =
435 me1000_dio_query_subdevice_caps;
437 return subdevice;