4 * @brief ME-9x digital input subdevice instance.
5 * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * @author Guenter Gebhardt
10 * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
12 * This file is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
39 #include <linux/types.h>
40 #include <linux/interrupt.h>
42 #include "medefines.h"
43 #include "meinternal.h"
48 #include "meplx_reg.h"
49 #include "me0900_reg.h"
50 #include "me0900_di.h"
60 static int me0900_di_io_reset_subdevice(struct me_subdevice *subdevice,
61 struct file *filep, int flags)
63 PDEBUG("executed.\n");
66 PERROR("Invalid flag specified.\n");
67 return ME_ERRNO_INVALID_FLAGS;
70 return ME_ERRNO_SUCCESS;
73 static int me0900_di_io_single_config(me_subdevice_t *subdevice,
79 int trig_type, int trig_edge, int flags)
81 me0900_di_subdevice_t *instance;
82 int err = ME_ERRNO_SUCCESS;
84 PDEBUG("executed.\n");
86 instance = (me0900_di_subdevice_t *) subdevice;
90 spin_lock(&instance->subdevice_lock);
92 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
93 case ME_IO_SINGLE_TYPE_DIO_BYTE:
95 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
97 PERROR("Invalid byte direction specified.\n");
98 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
101 PERROR("Invalid byte number specified.\n");
102 err = ME_ERRNO_INVALID_CHANNEL;
107 PERROR("Invalid flags specified.\n");
108 err = ME_ERRNO_INVALID_FLAGS;
110 spin_unlock(&instance->subdevice_lock);
117 static int me0900_di_io_single_read(me_subdevice_t *subdevice,
120 int *value, int time_out, int flags)
122 me0900_di_subdevice_t *instance;
123 int err = ME_ERRNO_SUCCESS;
125 PDEBUG("executed.\n");
127 instance = (me0900_di_subdevice_t *) subdevice;
131 spin_lock(&instance->subdevice_lock);
133 case ME_IO_SINGLE_TYPE_DIO_BIT:
134 if ((channel >= 0) && (channel < 8)) {
135 *value = (~inb(instance->port_reg)) & (0x1 << channel);
137 PERROR("Invalid bit number specified.\n");
138 err = ME_ERRNO_INVALID_CHANNEL;
142 case ME_IO_SINGLE_NO_FLAGS:
143 case ME_IO_SINGLE_TYPE_DIO_BYTE:
145 *value = ~inb(instance->port_reg);
147 PERROR("Invalid byte number specified.\n");
148 err = ME_ERRNO_INVALID_CHANNEL;
153 PERROR("Invalid flags specified.\n");
154 err = ME_ERRNO_INVALID_FLAGS;
156 spin_unlock(&instance->subdevice_lock);
163 static int me0900_di_query_number_channels(me_subdevice_t *subdevice,
166 PDEBUG("executed.\n");
168 return ME_ERRNO_SUCCESS;
171 static int me0900_di_query_subdevice_type(me_subdevice_t *subdevice,
172 int *type, int *subtype)
174 PDEBUG("executed.\n");
176 *subtype = ME_SUBTYPE_SINGLE;
177 return ME_ERRNO_SUCCESS;
180 static int me0900_di_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
182 PDEBUG("executed.\n");
184 return ME_ERRNO_SUCCESS;
187 me0900_di_subdevice_t *me0900_di_constructor(uint32_t reg_base,
190 me0900_di_subdevice_t *subdevice;
193 PDEBUG("executed.\n");
195 /* Allocate memory for subdevice instance */
196 subdevice = kmalloc(sizeof(me0900_di_subdevice_t), GFP_KERNEL);
199 PERROR("Cannot get memory for subdevice instance.\n");
203 memset(subdevice, 0, sizeof(me0900_di_subdevice_t));
205 /* Initialize subdevice base class */
206 err = me_subdevice_init(&subdevice->base);
209 PERROR("Cannot initialize subdevice base class instance.\n");
213 // Initialize spin locks.
214 spin_lock_init(&subdevice->subdevice_lock);
216 /* Save the subdevice index. */
217 subdevice->di_idx = di_idx;
219 /* Initialize registers */
221 subdevice->ctrl_reg = reg_base + ME0900_CTRL_REG;
222 subdevice->port_reg = reg_base + ME0900_PORT_A_REG;
224 subdevice->ctrl_reg = reg_base + ME0900_CTRL_REG;
225 subdevice->port_reg = reg_base + ME0900_PORT_B_REG;
227 #ifdef MEDEBUG_DEBUG_REG
228 subdevice->reg_base = reg_base;
231 /* Overload base class methods. */
232 subdevice->base.me_subdevice_io_reset_subdevice =
233 me0900_di_io_reset_subdevice;
234 subdevice->base.me_subdevice_io_single_config =
235 me0900_di_io_single_config;
236 subdevice->base.me_subdevice_io_single_read = me0900_di_io_single_read;
237 subdevice->base.me_subdevice_query_number_channels =
238 me0900_di_query_number_channels;
239 subdevice->base.me_subdevice_query_subdevice_type =
240 me0900_di_query_subdevice_type;
241 subdevice->base.me_subdevice_query_subdevice_caps =
242 me0900_di_query_subdevice_caps;