blob: b8c448f58e36e1424ff4abbb41f78c293be8ab03 [file] [log] [blame]
David Kiliani3fedd142008-11-01 00:39:12 +01001/**
2 * @file me0900_di.c
3 *
4 * @brief ME-9x digital input subdevice instance.
5 * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * @author Guenter Gebhardt
7 */
8
9/*
10 * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
11 *
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.
16 *
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.
21 *
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.
25 */
26
27#ifndef __KERNEL__
28# define __KERNEL__
29#endif
30
31/*
32 * Includes
33 */
34#include <linux/module.h>
35
36#include <linux/slab.h>
37#include <linux/spinlock.h>
Alexander Beregalov451084d2009-03-16 19:45:13 +030038#include <linux/io.h>
David Kiliani3fedd142008-11-01 00:39:12 +010039#include <linux/types.h>
40#include <linux/interrupt.h>
David Kiliani3fedd142008-11-01 00:39:12 +010041
42#include "medefines.h"
43#include "meinternal.h"
44#include "meerror.h"
45
46#include "meids.h"
47#include "medebug.h"
48#include "meplx_reg.h"
49#include "me0900_reg.h"
50#include "me0900_di.h"
51
52/*
53 * Defines
54 */
55
56/*
57 * Functions
58 */
59
60static int me0900_di_io_reset_subdevice(struct me_subdevice *subdevice,
61 struct file *filep, int flags)
62{
63 PDEBUG("executed.\n");
64
65 if (flags) {
66 PERROR("Invalid flag specified.\n");
67 return ME_ERRNO_INVALID_FLAGS;
68 }
69
70 return ME_ERRNO_SUCCESS;
71}
72
Alexander Beregalov451084d2009-03-16 19:45:13 +030073static int me0900_di_io_single_config(me_subdevice_t *subdevice,
David Kiliani3fedd142008-11-01 00:39:12 +010074 struct file *filep,
75 int channel,
76 int single_config,
77 int ref,
78 int trig_chan,
79 int trig_type, int trig_edge, int flags)
80{
81 me0900_di_subdevice_t *instance;
82 int err = ME_ERRNO_SUCCESS;
83
84 PDEBUG("executed.\n");
85
86 instance = (me0900_di_subdevice_t *) subdevice;
87
88 ME_SUBDEVICE_ENTER;
89
90 spin_lock(&instance->subdevice_lock);
91 switch (flags) {
92 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
93 case ME_IO_SINGLE_TYPE_DIO_BYTE:
94 if (channel == 0) {
95 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
96 } else {
97 PERROR("Invalid byte direction specified.\n");
98 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
99 }
100 } else {
101 PERROR("Invalid byte number specified.\n");
102 err = ME_ERRNO_INVALID_CHANNEL;
103 }
104 break;
105
106 default:
107 PERROR("Invalid flags specified.\n");
108 err = ME_ERRNO_INVALID_FLAGS;
109 }
110 spin_unlock(&instance->subdevice_lock);
111
112 ME_SUBDEVICE_EXIT;
113
114 return err;
115}
116
Alexander Beregalov451084d2009-03-16 19:45:13 +0300117static int me0900_di_io_single_read(me_subdevice_t *subdevice,
David Kiliani3fedd142008-11-01 00:39:12 +0100118 struct file *filep,
119 int channel,
120 int *value, int time_out, int flags)
121{
122 me0900_di_subdevice_t *instance;
123 int err = ME_ERRNO_SUCCESS;
124
125 PDEBUG("executed.\n");
126
127 instance = (me0900_di_subdevice_t *) subdevice;
128
129 ME_SUBDEVICE_ENTER;
130
131 spin_lock(&instance->subdevice_lock);
132 switch (flags) {
133 case ME_IO_SINGLE_TYPE_DIO_BIT:
134 if ((channel >= 0) && (channel < 8)) {
135 *value = (~inb(instance->port_reg)) & (0x1 << channel);
136 } else {
137 PERROR("Invalid bit number specified.\n");
138 err = ME_ERRNO_INVALID_CHANNEL;
139 }
140 break;
141
142 case ME_IO_SINGLE_NO_FLAGS:
143 case ME_IO_SINGLE_TYPE_DIO_BYTE:
144 if (channel == 0) {
145 *value = ~inb(instance->port_reg);
146 } else {
147 PERROR("Invalid byte number specified.\n");
148 err = ME_ERRNO_INVALID_CHANNEL;
149 }
150 break;
151
152 default:
153 PERROR("Invalid flags specified.\n");
154 err = ME_ERRNO_INVALID_FLAGS;
155 }
156 spin_unlock(&instance->subdevice_lock);
157
158 ME_SUBDEVICE_EXIT;
159
160 return err;
161}
162
Alexander Beregalov451084d2009-03-16 19:45:13 +0300163static int me0900_di_query_number_channels(me_subdevice_t *subdevice,
David Kiliani3fedd142008-11-01 00:39:12 +0100164 int *number)
165{
166 PDEBUG("executed.\n");
167 *number = 8;
168 return ME_ERRNO_SUCCESS;
169}
170
Alexander Beregalov451084d2009-03-16 19:45:13 +0300171static int me0900_di_query_subdevice_type(me_subdevice_t *subdevice,
David Kiliani3fedd142008-11-01 00:39:12 +0100172 int *type, int *subtype)
173{
174 PDEBUG("executed.\n");
175 *type = ME_TYPE_DI;
176 *subtype = ME_SUBTYPE_SINGLE;
177 return ME_ERRNO_SUCCESS;
178}
179
Alexander Beregalov451084d2009-03-16 19:45:13 +0300180static int me0900_di_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
David Kiliani3fedd142008-11-01 00:39:12 +0100181{
182 PDEBUG("executed.\n");
183 *caps = 0;
184 return ME_ERRNO_SUCCESS;
185}
186
187me0900_di_subdevice_t *me0900_di_constructor(uint32_t reg_base,
188 unsigned int di_idx)
189{
190 me0900_di_subdevice_t *subdevice;
191 int err;
192
193 PDEBUG("executed.\n");
194
195 /* Allocate memory for subdevice instance */
196 subdevice = kmalloc(sizeof(me0900_di_subdevice_t), GFP_KERNEL);
197
198 if (!subdevice) {
199 PERROR("Cannot get memory for subdevice instance.\n");
200 return NULL;
201 }
202
203 memset(subdevice, 0, sizeof(me0900_di_subdevice_t));
204
205 /* Initialize subdevice base class */
206 err = me_subdevice_init(&subdevice->base);
207
208 if (err) {
209 PERROR("Cannot initialize subdevice base class instance.\n");
210 kfree(subdevice);
211 return NULL;
212 }
213 // Initialize spin locks.
214 spin_lock_init(&subdevice->subdevice_lock);
215
216 /* Save the subdevice index. */
217 subdevice->di_idx = di_idx;
218
219 /* Initialize registers */
220 if (di_idx == 0) {
221 subdevice->ctrl_reg = reg_base + ME0900_CTRL_REG;
222 subdevice->port_reg = reg_base + ME0900_PORT_A_REG;
223 } else {
224 subdevice->ctrl_reg = reg_base + ME0900_CTRL_REG;
225 subdevice->port_reg = reg_base + ME0900_PORT_B_REG;
226 }
227#ifdef MEDEBUG_DEBUG_REG
228 subdevice->reg_base = reg_base;
229#endif
230
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;
243
244 return subdevice;
245}