staging: comedi: remove the comed_alloc_subdevices "allocation failed" messages
[deliverable/linux.git] / drivers / staging / comedi / drivers / 8255.c
1 /*
2 comedi/drivers/8255.c
3 Driver for 8255
4
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1998 David A. Schleef <ds@schleef.org>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23 /*
24 Driver: 8255
25 Description: generic 8255 support
26 Devices: [standard] 8255 (8255)
27 Author: ds
28 Status: works
29 Updated: Fri, 7 Jun 2002 12:56:45 -0700
30
31 The classic in digital I/O. The 8255 appears in Comedi as a single
32 digital I/O subdevice with 24 channels. The channel 0 corresponds
33 to the 8255's port A, bit 0; channel 23 corresponds to port C, bit
34 7. Direction configuration is done in blocks, with channels 0-7,
35 8-15, 16-19, and 20-23 making up the 4 blocks. The only 8255 mode
36 supported is mode 0.
37
38 You should enable compilation this driver if you plan to use a board
39 that has an 8255 chip. For multifunction boards, the main driver will
40 configure the 8255 subdevice automatically.
41
42 This driver also works independently with ISA and PCI cards that
43 directly map the 8255 registers to I/O ports, including cards with
44 multiple 8255 chips. To configure the driver for such a card, the
45 option list should be a list of the I/O port bases for each of the
46 8255 chips. For example,
47
48 comedi_config /dev/comedi0 8255 0x200,0x204,0x208,0x20c
49
50 Note that most PCI 8255 boards do NOT work with this driver, and
51 need a separate driver as a wrapper. For those that do work, the
52 I/O port base address can be found in the output of 'lspci -v'.
53
54 */
55
56 /*
57 This file contains an exported subdevice for driving an 8255.
58
59 To use this subdevice as part of another driver, you need to
60 set up the subdevice in the attach function of the driver by
61 calling:
62
63 subdev_8255_init(device, subdevice, callback_function, arg)
64
65 device and subdevice are pointers to the device and subdevice
66 structures. callback_function will be called to provide the
67 low-level input/output to the device, i.e., actual register
68 access. callback_function will be called with the value of arg
69 as the last parameter. If the 8255 device is mapped as 4
70 consecutive I/O ports, you can use NULL for callback_function
71 and the I/O port base for arg, and an internal function will
72 handle the register access.
73
74 In addition, if the main driver handles interrupts, you can
75 enable commands on the subdevice by calling subdev_8255_init_irq()
76 instead. Then, when you get an interrupt that is likely to be
77 from the 8255, you should call subdev_8255_interrupt(), which
78 will copy the latched value to a Comedi buffer.
79 */
80
81 #include "../comedidev.h"
82
83 #include <linux/ioport.h>
84 #include <linux/slab.h>
85 #include "8255.h"
86
87 #define _8255_SIZE 4
88
89 #define _8255_DATA 0
90 #define _8255_CR 3
91
92 #define CR_C_LO_IO 0x01
93 #define CR_B_IO 0x02
94 #define CR_B_MODE 0x04
95 #define CR_C_HI_IO 0x08
96 #define CR_A_IO 0x10
97 #define CR_A_MODE(a) ((a)<<5)
98 #define CR_CW 0x80
99
100 struct subdev_8255_struct {
101 unsigned long cb_arg;
102 int (*cb_func) (int, int, int, unsigned long);
103 int have_irq;
104 };
105
106 #define CALLBACK_ARG (((struct subdev_8255_struct *)s->private)->cb_arg)
107 #define CALLBACK_FUNC (((struct subdev_8255_struct *)s->private)->cb_func)
108 #define subdevpriv ((struct subdev_8255_struct *)s->private)
109
110 void subdev_8255_interrupt(struct comedi_device *dev,
111 struct comedi_subdevice *s)
112 {
113 short d;
114
115 d = CALLBACK_FUNC(0, _8255_DATA, 0, CALLBACK_ARG);
116 d |= (CALLBACK_FUNC(0, _8255_DATA + 1, 0, CALLBACK_ARG) << 8);
117
118 comedi_buf_put(s->async, d);
119 s->async->events |= COMEDI_CB_EOS;
120
121 comedi_event(dev, s);
122 }
123 EXPORT_SYMBOL(subdev_8255_interrupt);
124
125 static int subdev_8255_cb(int dir, int port, int data, unsigned long arg)
126 {
127 unsigned long iobase = arg;
128
129 if (dir) {
130 outb(data, iobase + port);
131 return 0;
132 } else {
133 return inb(iobase + port);
134 }
135 }
136
137 static int subdev_8255_insn(struct comedi_device *dev,
138 struct comedi_subdevice *s,
139 struct comedi_insn *insn, unsigned int *data)
140 {
141 if (data[0]) {
142 s->state &= ~data[0];
143 s->state |= (data[0] & data[1]);
144
145 if (data[0] & 0xff)
146 CALLBACK_FUNC(1, _8255_DATA, s->state & 0xff,
147 CALLBACK_ARG);
148 if (data[0] & 0xff00)
149 CALLBACK_FUNC(1, _8255_DATA + 1, (s->state >> 8) & 0xff,
150 CALLBACK_ARG);
151 if (data[0] & 0xff0000)
152 CALLBACK_FUNC(1, _8255_DATA + 2,
153 (s->state >> 16) & 0xff, CALLBACK_ARG);
154 }
155
156 data[1] = CALLBACK_FUNC(0, _8255_DATA, 0, CALLBACK_ARG);
157 data[1] |= (CALLBACK_FUNC(0, _8255_DATA + 1, 0, CALLBACK_ARG) << 8);
158 data[1] |= (CALLBACK_FUNC(0, _8255_DATA + 2, 0, CALLBACK_ARG) << 16);
159
160 return 2;
161 }
162
163 static void do_config(struct comedi_device *dev, struct comedi_subdevice *s)
164 {
165 int config;
166
167 config = CR_CW;
168 /* 1 in io_bits indicates output, 1 in config indicates input */
169 if (!(s->io_bits & 0x0000ff))
170 config |= CR_A_IO;
171 if (!(s->io_bits & 0x00ff00))
172 config |= CR_B_IO;
173 if (!(s->io_bits & 0x0f0000))
174 config |= CR_C_LO_IO;
175 if (!(s->io_bits & 0xf00000))
176 config |= CR_C_HI_IO;
177 CALLBACK_FUNC(1, _8255_CR, config, CALLBACK_ARG);
178 }
179
180 static int subdev_8255_insn_config(struct comedi_device *dev,
181 struct comedi_subdevice *s,
182 struct comedi_insn *insn, unsigned int *data)
183 {
184 unsigned int mask;
185 unsigned int bits;
186
187 mask = 1 << CR_CHAN(insn->chanspec);
188 if (mask & 0x0000ff)
189 bits = 0x0000ff;
190 else if (mask & 0x00ff00)
191 bits = 0x00ff00;
192 else if (mask & 0x0f0000)
193 bits = 0x0f0000;
194 else
195 bits = 0xf00000;
196
197 switch (data[0]) {
198 case INSN_CONFIG_DIO_INPUT:
199 s->io_bits &= ~bits;
200 break;
201 case INSN_CONFIG_DIO_OUTPUT:
202 s->io_bits |= bits;
203 break;
204 case INSN_CONFIG_DIO_QUERY:
205 data[1] = (s->io_bits & bits) ? COMEDI_OUTPUT : COMEDI_INPUT;
206 return insn->n;
207 break;
208 default:
209 return -EINVAL;
210 }
211
212 do_config(dev, s);
213
214 return 1;
215 }
216
217 static int subdev_8255_cmdtest(struct comedi_device *dev,
218 struct comedi_subdevice *s,
219 struct comedi_cmd *cmd)
220 {
221 int err = 0;
222 unsigned int tmp;
223
224 /* step 1 */
225
226 tmp = cmd->start_src;
227 cmd->start_src &= TRIG_NOW;
228 if (!cmd->start_src || tmp != cmd->start_src)
229 err++;
230
231 tmp = cmd->scan_begin_src;
232 cmd->scan_begin_src &= TRIG_EXT;
233 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
234 err++;
235
236 tmp = cmd->convert_src;
237 cmd->convert_src &= TRIG_FOLLOW;
238 if (!cmd->convert_src || tmp != cmd->convert_src)
239 err++;
240
241 tmp = cmd->scan_end_src;
242 cmd->scan_end_src &= TRIG_COUNT;
243 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
244 err++;
245
246 tmp = cmd->stop_src;
247 cmd->stop_src &= TRIG_NONE;
248 if (!cmd->stop_src || tmp != cmd->stop_src)
249 err++;
250
251 if (err)
252 return 1;
253
254 /* step 2 */
255
256 if (err)
257 return 2;
258
259 /* step 3 */
260
261 if (cmd->start_arg != 0) {
262 cmd->start_arg = 0;
263 err++;
264 }
265 if (cmd->scan_begin_arg != 0) {
266 cmd->scan_begin_arg = 0;
267 err++;
268 }
269 if (cmd->convert_arg != 0) {
270 cmd->convert_arg = 0;
271 err++;
272 }
273 if (cmd->scan_end_arg != 1) {
274 cmd->scan_end_arg = 1;
275 err++;
276 }
277 if (cmd->stop_arg != 0) {
278 cmd->stop_arg = 0;
279 err++;
280 }
281
282 if (err)
283 return 3;
284
285 /* step 4 */
286
287 if (err)
288 return 4;
289
290 return 0;
291 }
292
293 static int subdev_8255_cmd(struct comedi_device *dev,
294 struct comedi_subdevice *s)
295 {
296 /* FIXME */
297
298 return 0;
299 }
300
301 static int subdev_8255_cancel(struct comedi_device *dev,
302 struct comedi_subdevice *s)
303 {
304 /* FIXME */
305
306 return 0;
307 }
308
309 int subdev_8255_init(struct comedi_device *dev, struct comedi_subdevice *s,
310 int (*cb) (int, int, int, unsigned long),
311 unsigned long arg)
312 {
313 s->type = COMEDI_SUBD_DIO;
314 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
315 s->n_chan = 24;
316 s->range_table = &range_digital;
317 s->maxdata = 1;
318
319 s->private = kmalloc(sizeof(struct subdev_8255_struct), GFP_KERNEL);
320 if (!s->private)
321 return -ENOMEM;
322
323 CALLBACK_ARG = arg;
324 if (cb == NULL)
325 CALLBACK_FUNC = subdev_8255_cb;
326 else
327 CALLBACK_FUNC = cb;
328 s->insn_bits = subdev_8255_insn;
329 s->insn_config = subdev_8255_insn_config;
330
331 s->state = 0;
332 s->io_bits = 0;
333 do_config(dev, s);
334
335 return 0;
336 }
337 EXPORT_SYMBOL(subdev_8255_init);
338
339 int subdev_8255_init_irq(struct comedi_device *dev, struct comedi_subdevice *s,
340 int (*cb) (int, int, int, unsigned long),
341 unsigned long arg)
342 {
343 int ret;
344
345 ret = subdev_8255_init(dev, s, cb, arg);
346 if (ret < 0)
347 return ret;
348
349 s->do_cmdtest = subdev_8255_cmdtest;
350 s->do_cmd = subdev_8255_cmd;
351 s->cancel = subdev_8255_cancel;
352
353 subdevpriv->have_irq = 1;
354
355 return 0;
356 }
357 EXPORT_SYMBOL(subdev_8255_init_irq);
358
359 void subdev_8255_cleanup(struct comedi_device *dev, struct comedi_subdevice *s)
360 {
361 kfree(s->private);
362 }
363 EXPORT_SYMBOL(subdev_8255_cleanup);
364
365 /*
366
367 Start of the 8255 standalone device
368
369 */
370
371 static int dev_8255_attach(struct comedi_device *dev,
372 struct comedi_devconfig *it)
373 {
374 int ret;
375 unsigned long iobase;
376 int i;
377
378 dev->board_name = "8255";
379
380 for (i = 0; i < COMEDI_NDEVCONFOPTS; i++) {
381 iobase = it->options[i];
382 if (!iobase)
383 break;
384 }
385 if (i == 0) {
386 printk(KERN_WARNING
387 "comedi%d: 8255: no devices specified\n", dev->minor);
388 return -EINVAL;
389 }
390
391 ret = comedi_alloc_subdevices(dev, i);
392 if (ret < 0)
393 return ret;
394
395 printk(KERN_INFO "comedi%d: 8255:", dev->minor);
396
397 for (i = 0; i < dev->n_subdevices; i++) {
398 iobase = it->options[i];
399
400 printk(" 0x%04lx", iobase);
401 if (!request_region(iobase, _8255_SIZE, "8255")) {
402 printk(" (I/O port conflict)");
403
404 dev->subdevices[i].type = COMEDI_SUBD_UNUSED;
405 } else {
406 subdev_8255_init(dev, dev->subdevices + i, NULL,
407 iobase);
408 }
409 }
410
411 printk("\n");
412
413 return 0;
414 }
415
416 static void dev_8255_detach(struct comedi_device *dev)
417 {
418 int i;
419 unsigned long iobase;
420 struct comedi_subdevice *s;
421
422 for (i = 0; i < dev->n_subdevices; i++) {
423 s = dev->subdevices + i;
424 if (s->type != COMEDI_SUBD_UNUSED) {
425 iobase = CALLBACK_ARG;
426 release_region(iobase, _8255_SIZE);
427 }
428 subdev_8255_cleanup(dev, s);
429 }
430 }
431
432 static struct comedi_driver dev_8255_driver = {
433 .driver_name = "8255",
434 .module = THIS_MODULE,
435 .attach = dev_8255_attach,
436 .detach = dev_8255_detach,
437 };
438 module_comedi_driver(dev_8255_driver);
439
440 MODULE_AUTHOR("Comedi http://www.comedi.org");
441 MODULE_DESCRIPTION("Comedi low-level driver");
442 MODULE_LICENSE("GPL");
This page took 0.055032 seconds and 5 git commands to generate.