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>
41 #include <linux/version.h>
43 #include "medefines.h"
44 #include "meinternal.h"
49 #include "meplx_reg.h"
50 #include "me0900_reg.h"
51 #include "me0900_di.h"
61 static int me0900_di_io_reset_subdevice(struct me_subdevice
*subdevice
,
62 struct file
*filep
, int flags
)
64 PDEBUG("executed.\n");
67 PERROR("Invalid flag specified.\n");
68 return ME_ERRNO_INVALID_FLAGS
;
71 return ME_ERRNO_SUCCESS
;
74 static int me0900_di_io_single_config(me_subdevice_t
*subdevice
,
80 int trig_type
, int trig_edge
, int flags
)
82 me0900_di_subdevice_t
*instance
;
83 int err
= ME_ERRNO_SUCCESS
;
85 PDEBUG("executed.\n");
87 instance
= (me0900_di_subdevice_t
*) subdevice
;
91 spin_lock(&instance
->subdevice_lock
);
93 case ME_IO_SINGLE_CONFIG_NO_FLAGS
:
94 case ME_IO_SINGLE_TYPE_DIO_BYTE
:
96 if (single_config
== ME_SINGLE_CONFIG_DIO_INPUT
) {
98 PERROR("Invalid byte direction specified.\n");
99 err
= ME_ERRNO_INVALID_SINGLE_CONFIG
;
102 PERROR("Invalid byte number specified.\n");
103 err
= ME_ERRNO_INVALID_CHANNEL
;
108 PERROR("Invalid flags specified.\n");
109 err
= ME_ERRNO_INVALID_FLAGS
;
111 spin_unlock(&instance
->subdevice_lock
);
118 static int me0900_di_io_single_read(me_subdevice_t
*subdevice
,
121 int *value
, int time_out
, int flags
)
123 me0900_di_subdevice_t
*instance
;
124 int err
= ME_ERRNO_SUCCESS
;
126 PDEBUG("executed.\n");
128 instance
= (me0900_di_subdevice_t
*) subdevice
;
132 spin_lock(&instance
->subdevice_lock
);
134 case ME_IO_SINGLE_TYPE_DIO_BIT
:
135 if ((channel
>= 0) && (channel
< 8)) {
136 *value
= (~inb(instance
->port_reg
)) & (0x1 << channel
);
138 PERROR("Invalid bit number specified.\n");
139 err
= ME_ERRNO_INVALID_CHANNEL
;
143 case ME_IO_SINGLE_NO_FLAGS
:
144 case ME_IO_SINGLE_TYPE_DIO_BYTE
:
146 *value
= ~inb(instance
->port_reg
);
148 PERROR("Invalid byte number specified.\n");
149 err
= ME_ERRNO_INVALID_CHANNEL
;
154 PERROR("Invalid flags specified.\n");
155 err
= ME_ERRNO_INVALID_FLAGS
;
157 spin_unlock(&instance
->subdevice_lock
);
164 static int me0900_di_query_number_channels(me_subdevice_t
*subdevice
,
167 PDEBUG("executed.\n");
169 return ME_ERRNO_SUCCESS
;
172 static int me0900_di_query_subdevice_type(me_subdevice_t
*subdevice
,
173 int *type
, int *subtype
)
175 PDEBUG("executed.\n");
177 *subtype
= ME_SUBTYPE_SINGLE
;
178 return ME_ERRNO_SUCCESS
;
181 static int me0900_di_query_subdevice_caps(me_subdevice_t
*subdevice
, int *caps
)
183 PDEBUG("executed.\n");
185 return ME_ERRNO_SUCCESS
;
188 me0900_di_subdevice_t
*me0900_di_constructor(uint32_t reg_base
,
191 me0900_di_subdevice_t
*subdevice
;
194 PDEBUG("executed.\n");
196 /* Allocate memory for subdevice instance */
197 subdevice
= kmalloc(sizeof(me0900_di_subdevice_t
), GFP_KERNEL
);
200 PERROR("Cannot get memory for subdevice instance.\n");
204 memset(subdevice
, 0, sizeof(me0900_di_subdevice_t
));
206 /* Initialize subdevice base class */
207 err
= me_subdevice_init(&subdevice
->base
);
210 PERROR("Cannot initialize subdevice base class instance.\n");
214 // Initialize spin locks.
215 spin_lock_init(&subdevice
->subdevice_lock
);
217 /* Save the subdevice index. */
218 subdevice
->di_idx
= di_idx
;
220 /* Initialize registers */
222 subdevice
->ctrl_reg
= reg_base
+ ME0900_CTRL_REG
;
223 subdevice
->port_reg
= reg_base
+ ME0900_PORT_A_REG
;
225 subdevice
->ctrl_reg
= reg_base
+ ME0900_CTRL_REG
;
226 subdevice
->port_reg
= reg_base
+ ME0900_PORT_B_REG
;
228 #ifdef MEDEBUG_DEBUG_REG
229 subdevice
->reg_base
= reg_base
;
232 /* Overload base class methods. */
233 subdevice
->base
.me_subdevice_io_reset_subdevice
=
234 me0900_di_io_reset_subdevice
;
235 subdevice
->base
.me_subdevice_io_single_config
=
236 me0900_di_io_single_config
;
237 subdevice
->base
.me_subdevice_io_single_read
= me0900_di_io_single_read
;
238 subdevice
->base
.me_subdevice_query_number_channels
=
239 me0900_di_query_number_channels
;
240 subdevice
->base
.me_subdevice_query_subdevice_type
=
241 me0900_di_query_subdevice_type
;
242 subdevice
->base
.me_subdevice_query_subdevice_caps
=
243 me0900_di_query_subdevice_caps
;