ni_daq_700.c revision f25bd6bfdf9bccd8164c151b027f9c14ec07960b
1/*
2 *     comedi/drivers/ni_daq_700.c
3 *     Driver for DAQCard-700 DIO only
4 *     copied from 8255
5 *
6 *     COMEDI - Linux Control and Measurement Device Interface
7 *     Copyright (C) 1998 David A. Schleef <ds@schleef.org>
8 *
9 *     This program is free software; you can redistribute it and/or modify
10 *     it under the terms of the GNU General Public License as published by
11 *     the Free Software Foundation; either version 2 of the License, or
12 *     (at your option) any later version.
13 *
14 *     This program is distributed in the hope that it will be useful,
15 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *     GNU General Public License for more details.
18 *
19 *     You should have received a copy of the GNU General Public License
20 *     along with this program; if not, write to the Free Software
21 *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25/*
26Driver: ni_daq_700
27Description: National Instruments PCMCIA DAQCard-700 DIO only
28Author: Fred Brooks <nsaspook@nsaspook.com>,
29  based on ni_daq_dio24 by Daniel Vecino Castel <dvecino@able.es>
30Devices: [National Instruments] PCMCIA DAQ-Card-700 (ni_daq_700)
31Status: works
32Updated: Thu, 21 Feb 2008 12:07:20 +0000
33
34The daqcard-700 appears in Comedi as a single digital I/O subdevice with
3516 channels.  The channel 0 corresponds to the daqcard-700's output
36port, bit 0; channel 8 corresponds to the input port, bit 0.
37
38Direction configuration: channels 0-7 output, 8-15 input (8225 device
39emu as port A output, port B input, port C N/A).
40
41IRQ is assigned but not used.
42*/
43
44#include <linux/interrupt.h>
45#include <linux/slab.h>
46#include "../comedidev.h"
47
48#include <linux/ioport.h>
49
50#include <pcmcia/cs_types.h>
51#include <pcmcia/cs.h>
52#include <pcmcia/cistpl.h>
53#include <pcmcia/cisreg.h>
54#include <pcmcia/ds.h>
55
56static struct pcmcia_device *pcmcia_cur_dev = NULL;
57
58#define DIO700_SIZE 8		/*  size of io region used by board */
59
60static int dio700_attach(struct comedi_device *dev,
61			 struct comedi_devconfig *it);
62static int dio700_detach(struct comedi_device *dev);
63
64enum dio700_bustype { pcmcia_bustype };
65
66struct dio700_board {
67	const char *name;
68	int device_id;		/*  device id for pcmcia board */
69	enum dio700_bustype bustype;	/*  PCMCIA */
70	int have_dio;		/*  have daqcard-700 dio */
71	/*  function pointers so we can use inb/outb or readb/writeb */
72	/*  as appropriate */
73	unsigned int (*read_byte) (unsigned int address);
74	void (*write_byte) (unsigned int byte, unsigned int address);
75};
76
77static const struct dio700_board dio700_boards[] = {
78	{
79	 .name = "daqcard-700",
80	  /*  0x10b is manufacturer id, 0x4743 is device id */
81	 .device_id = 0x4743,
82	 .bustype = pcmcia_bustype,
83	 .have_dio = 1,
84	 },
85	{
86	 .name = "ni_daq_700",
87	  /*  0x10b is manufacturer id, 0x4743 is device id */
88	 .device_id = 0x4743,
89	 .bustype = pcmcia_bustype,
90	 .have_dio = 1,
91	 },
92};
93
94/*
95 * Useful for shorthand access to the particular board structure
96 */
97#define thisboard ((const struct dio700_board *)dev->board_ptr)
98
99struct dio700_private {
100
101	int data;		/* number of data points left to be taken */
102};
103
104#define devpriv ((struct dio700_private *)dev->private)
105
106static struct comedi_driver driver_dio700 = {
107	.driver_name = "ni_daq_700",
108	.module = THIS_MODULE,
109	.attach = dio700_attach,
110	.detach = dio700_detach,
111	.num_names = ARRAY_SIZE(dio700_boards),
112	.board_name = &dio700_boards[0].name,
113	.offset = sizeof(struct dio700_board),
114};
115
116/*	the real driver routines	*/
117
118#define _700_SIZE 8
119
120#define _700_DATA 0
121
122#define DIO_W		0x04
123#define DIO_R		0x05
124
125struct subdev_700_struct {
126	unsigned long cb_arg;
127	int (*cb_func) (int, int, int, unsigned long);
128	int have_irq;
129};
130
131#define CALLBACK_ARG	(((struct subdev_700_struct *)s->private)->cb_arg)
132#define CALLBACK_FUNC	(((struct subdev_700_struct *)s->private)->cb_func)
133#define subdevpriv	((struct subdev_700_struct *)s->private)
134
135static void do_config(struct comedi_device *dev, struct comedi_subdevice *s);
136
137void subdev_700_interrupt(struct comedi_device *dev, struct comedi_subdevice *s)
138{
139	short d;
140
141	d = CALLBACK_FUNC(0, _700_DATA, 0, CALLBACK_ARG);
142
143	comedi_buf_put(s->async, d);
144	s->async->events |= COMEDI_CB_EOS;
145
146	comedi_event(dev, s);
147}
148EXPORT_SYMBOL(subdev_700_interrupt);
149
150static int subdev_700_cb(int dir, int port, int data, unsigned long arg)
151{
152	/* port is always A for output and B for input (8255 emu) */
153	unsigned long iobase = arg;
154
155	if (dir) {
156		outb(data, iobase + DIO_W);
157		return 0;
158	} else {
159		return inb(iobase + DIO_R);
160	}
161}
162
163static int subdev_700_insn(struct comedi_device *dev,
164			   struct comedi_subdevice *s, struct comedi_insn *insn,
165			   unsigned int *data)
166{
167	if (data[0]) {
168		s->state &= ~data[0];
169		s->state |= (data[0] & data[1]);
170
171		if (data[0] & 0xff)
172			CALLBACK_FUNC(1, _700_DATA, s->state & 0xff,
173				      CALLBACK_ARG);
174	}
175
176	data[1] = s->state & 0xff;
177	data[1] |= CALLBACK_FUNC(0, _700_DATA, 0, CALLBACK_ARG) << 8;
178
179	return 2;
180}
181
182static int subdev_700_insn_config(struct comedi_device *dev,
183				  struct comedi_subdevice *s,
184				  struct comedi_insn *insn, unsigned int *data)
185{
186
187	switch (data[0]) {
188	case INSN_CONFIG_DIO_INPUT:
189		break;
190	case INSN_CONFIG_DIO_OUTPUT:
191		break;
192	case INSN_CONFIG_DIO_QUERY:
193		data[1] =
194		    (s->
195		     io_bits & (1 << CR_CHAN(insn->chanspec))) ? COMEDI_OUTPUT :
196		    COMEDI_INPUT;
197		return insn->n;
198		break;
199	default:
200		return -EINVAL;
201	}
202
203	return 1;
204}
205
206static void do_config(struct comedi_device *dev, struct comedi_subdevice *s)
207{				/* use powerup defaults */
208	return;
209}
210
211static int subdev_700_cmdtest(struct comedi_device *dev,
212			      struct comedi_subdevice *s,
213			      struct comedi_cmd *cmd)
214{
215	int err = 0;
216	unsigned int tmp;
217
218	/* step 1 */
219
220	tmp = cmd->start_src;
221	cmd->start_src &= TRIG_NOW;
222	if (!cmd->start_src || tmp != cmd->start_src)
223		err++;
224
225	tmp = cmd->scan_begin_src;
226	cmd->scan_begin_src &= TRIG_EXT;
227	if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
228		err++;
229
230	tmp = cmd->convert_src;
231	cmd->convert_src &= TRIG_FOLLOW;
232	if (!cmd->convert_src || tmp != cmd->convert_src)
233		err++;
234
235	tmp = cmd->scan_end_src;
236	cmd->scan_end_src &= TRIG_COUNT;
237	if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
238		err++;
239
240	tmp = cmd->stop_src;
241	cmd->stop_src &= TRIG_NONE;
242	if (!cmd->stop_src || tmp != cmd->stop_src)
243		err++;
244
245	if (err)
246		return 1;
247
248	/* step 2 */
249
250	if (err)
251		return 2;
252
253	/* step 3 */
254
255	if (cmd->start_arg != 0) {
256		cmd->start_arg = 0;
257		err++;
258	}
259	if (cmd->scan_begin_arg != 0) {
260		cmd->scan_begin_arg = 0;
261		err++;
262	}
263	if (cmd->convert_arg != 0) {
264		cmd->convert_arg = 0;
265		err++;
266	}
267	if (cmd->scan_end_arg != 1) {
268		cmd->scan_end_arg = 1;
269		err++;
270	}
271	if (cmd->stop_arg != 0) {
272		cmd->stop_arg = 0;
273		err++;
274	}
275
276	if (err)
277		return 3;
278
279	/* step 4 */
280
281	if (err)
282		return 4;
283
284	return 0;
285}
286
287static int subdev_700_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
288{
289	/* FIXME */
290
291	return 0;
292}
293
294static int subdev_700_cancel(struct comedi_device *dev,
295			     struct comedi_subdevice *s)
296{
297	/* FIXME */
298
299	return 0;
300}
301
302int subdev_700_init(struct comedi_device *dev, struct comedi_subdevice *s,
303		    int (*cb) (int, int, int, unsigned long), unsigned long arg)
304{
305	s->type = COMEDI_SUBD_DIO;
306	s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
307	s->n_chan = 16;
308	s->range_table = &range_digital;
309	s->maxdata = 1;
310
311	s->private = kmalloc(sizeof(struct subdev_700_struct), GFP_KERNEL);
312	if (!s->private)
313		return -ENOMEM;
314
315	CALLBACK_ARG = arg;
316	if (cb == NULL)
317		CALLBACK_FUNC = subdev_700_cb;
318	 else
319		CALLBACK_FUNC = cb;
320
321	s->insn_bits = subdev_700_insn;
322	s->insn_config = subdev_700_insn_config;
323
324	s->state = 0;
325	s->io_bits = 0x00ff;
326	do_config(dev, s);
327
328	return 0;
329}
330EXPORT_SYMBOL(subdev_700_init);
331
332int subdev_700_init_irq(struct comedi_device *dev, struct comedi_subdevice *s,
333			int (*cb) (int, int, int, unsigned long),
334			unsigned long arg)
335{
336	int ret;
337
338	ret = subdev_700_init(dev, s, cb, arg);
339	if (ret < 0)
340		return ret;
341
342	s->do_cmdtest = subdev_700_cmdtest;
343	s->do_cmd = subdev_700_cmd;
344	s->cancel = subdev_700_cancel;
345
346	subdevpriv->have_irq = 1;
347
348	return 0;
349}
350EXPORT_SYMBOL(subdev_700_init_irq);
351
352void subdev_700_cleanup(struct comedi_device *dev, struct comedi_subdevice *s)
353{
354	if (s->private)
355		if (subdevpriv->have_irq)
356
357			kfree(s->private);
358}
359EXPORT_SYMBOL(subdev_700_cleanup);
360
361static int dio700_attach(struct comedi_device *dev, struct comedi_devconfig *it)
362{
363	struct comedi_subdevice *s;
364	unsigned long iobase = 0;
365#ifdef incomplete
366	unsigned int irq = 0;
367#endif
368	struct pcmcia_device *link;
369
370	/* allocate and initialize dev->private */
371	if (alloc_private(dev, sizeof(struct dio700_private)) < 0)
372		return -ENOMEM;
373
374	/*  get base address, irq etc. based on bustype */
375	switch (thisboard->bustype) {
376	case pcmcia_bustype:
377		link = pcmcia_cur_dev;	/* XXX hack */
378		if (!link)
379			return -EIO;
380		iobase = link->io.BasePort1;
381#ifdef incomplete
382		irq = link->irq;
383#endif
384		break;
385	default:
386		printk("bug! couldn't determine board type\n");
387		return -EINVAL;
388		break;
389	}
390	printk("comedi%d: ni_daq_700: %s, io 0x%lx", dev->minor,
391	       thisboard->name, iobase);
392#ifdef incomplete
393	if (irq)
394		printk(", irq %u", irq);
395
396#endif
397
398	printk("\n");
399
400	if (iobase == 0) {
401		printk("io base address is zero!\n");
402		return -EINVAL;
403	}
404
405	dev->iobase = iobase;
406
407#ifdef incomplete
408	/* grab our IRQ */
409	dev->irq = irq;
410#endif
411
412	dev->board_name = thisboard->name;
413
414	if (alloc_subdevices(dev, 1) < 0)
415		return -ENOMEM;
416
417	/* DAQCard-700 dio */
418	s = dev->subdevices + 0;
419	subdev_700_init(dev, s, NULL, dev->iobase);
420
421	return 0;
422};
423
424static int dio700_detach(struct comedi_device *dev)
425{
426	printk("comedi%d: ni_daq_700: cs-remove\n", dev->minor);
427
428	if (dev->subdevices)
429		subdev_700_cleanup(dev, dev->subdevices + 0);
430
431	if (thisboard->bustype != pcmcia_bustype && dev->iobase)
432		release_region(dev->iobase, DIO700_SIZE);
433	if (dev->irq)
434		free_irq(dev->irq, dev);
435
436	return 0;
437};
438
439/* PCMCIA crap -- watch your words, please! */
440
441static void dio700_config(struct pcmcia_device *link);
442static void dio700_release(struct pcmcia_device *link);
443static int dio700_cs_suspend(struct pcmcia_device *p_dev);
444static int dio700_cs_resume(struct pcmcia_device *p_dev);
445
446/*
447   The attach() and detach() entry points are used to create and destroy
448   "instances" of the driver, where each instance represents everything
449   needed to manage one actual PCMCIA card.
450*/
451
452static int dio700_cs_attach(struct pcmcia_device *);
453static void dio700_cs_detach(struct pcmcia_device *);
454
455/*
456   You'll also need to prototype all the functions that will actually
457   be used to talk to your device.  See 'memory_cs' for a good example
458   of a fully self-sufficient driver; the other drivers rely more or
459   less on other parts of the kernel.
460*/
461
462/*
463   The dev_info variable is the "key" that is used to match up this
464   device driver with appropriate cards, through the card configuration
465   database.
466*/
467
468static const dev_info_t dev_info = "ni_daq_700";
469
470struct local_info_t {
471	struct pcmcia_device *link;
472	int stop;
473	struct bus_operations *bus;
474};
475
476/*======================================================================
477
478    dio700_cs_attach() creates an "instance" of the driver, allocating
479    local data structures for one device.  The device is registered
480    with Card Services.
481
482    The dev_link structure is initialized, but we don't actually
483    configure the card at this point -- we wait until we receive a
484    card insertion event.
485
486======================================================================*/
487
488static int dio700_cs_attach(struct pcmcia_device *link)
489{
490	struct local_info_t *local;
491
492	printk(KERN_INFO "ni_daq_700:  cs-attach\n");
493
494	dev_dbg(&link->dev, "dio700_cs_attach()\n");
495
496	/* Allocate space for private device-specific data */
497	local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
498	if (!local)
499		return -ENOMEM;
500	local->link = link;
501	link->priv = local;
502
503	/*
504	   General socket configuration defaults can go here.  In this
505	   client, we assume very little, and rely on the CIS for almost
506	   everything.  In most clients, many details (i.e., number, sizes,
507	   and attributes of IO windows) are fixed by the nature of the
508	   device, and can be hard-wired here.
509	 */
510	link->conf.Attributes = 0;
511	link->conf.IntType = INT_MEMORY_AND_IO;
512
513	pcmcia_cur_dev = link;
514
515	dio700_config(link);
516
517	return 0;
518}				/* dio700_cs_attach */
519
520/*======================================================================
521
522    This deletes a driver "instance".  The device is de-registered
523    with Card Services.  If it has been released, all local data
524    structures are freed.  Otherwise, the structures will be freed
525    when the device is released.
526
527======================================================================*/
528
529static void dio700_cs_detach(struct pcmcia_device *link)
530{
531
532	printk(KERN_INFO "ni_daq_700: cs-detach!\n");
533
534	dev_dbg(&link->dev, "dio700_cs_detach\n");
535
536	((struct local_info_t *)link->priv)->stop = 1;
537	dio700_release(link);
538
539	/* This points to the parent struct local_info_t struct */
540	kfree(link->priv);
541
542}				/* dio700_cs_detach */
543
544/*======================================================================
545
546    dio700_config() is scheduled to run after a CARD_INSERTION event
547    is received, to configure the PCMCIA socket, and to make the
548    device available to the system.
549
550======================================================================*/
551
552static int dio700_pcmcia_config_loop(struct pcmcia_device *p_dev,
553				cistpl_cftable_entry_t *cfg,
554				cistpl_cftable_entry_t *dflt,
555				unsigned int vcc,
556				void *priv_data)
557{
558	win_req_t *req = priv_data;
559	memreq_t map;
560
561	if (cfg->index == 0)
562		return -ENODEV;
563
564	/* Does this card need audio output? */
565	if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
566		p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
567		p_dev->conf.Status = CCSR_AUDIO_ENA;
568	}
569
570	/* Do we need to allocate an interrupt? */
571	p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
572
573	/* IO window settings */
574	p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0;
575	if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
576		cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
577		p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
578		if (!(io->flags & CISTPL_IO_8BIT))
579			p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
580		if (!(io->flags & CISTPL_IO_16BIT))
581			p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
582		p_dev->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
583		p_dev->io.BasePort1 = io->win[0].base;
584		p_dev->io.NumPorts1 = io->win[0].len;
585		if (io->nwin > 1) {
586			p_dev->io.Attributes2 = p_dev->io.Attributes1;
587			p_dev->io.BasePort2 = io->win[1].base;
588			p_dev->io.NumPorts2 = io->win[1].len;
589		}
590		/* This reserves IO space but doesn't actually enable it */
591		if (pcmcia_request_io(p_dev, &p_dev->io) != 0)
592			return -ENODEV;
593	}
594
595	if ((cfg->mem.nwin > 0) || (dflt->mem.nwin > 0)) {
596		cistpl_mem_t *mem =
597			(cfg->mem.nwin) ? &cfg->mem : &dflt->mem;
598		req->Attributes = WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM;
599		req->Attributes |= WIN_ENABLE;
600		req->Base = mem->win[0].host_addr;
601		req->Size = mem->win[0].len;
602		if (req->Size < 0x1000)
603			req->Size = 0x1000;
604		req->AccessSpeed = 0;
605		if (pcmcia_request_window(p_dev, req, &p_dev->win))
606			return -ENODEV;
607		map.Page = 0;
608		map.CardOffset = mem->win[0].card_addr;
609		if (pcmcia_map_mem_page(p_dev, p_dev->win, &map))
610			return -ENODEV;
611	}
612	/* If we got this far, we're cool! */
613	return 0;
614}
615
616static void dio700_config(struct pcmcia_device *link)
617{
618	win_req_t req;
619	int ret;
620
621	printk(KERN_INFO "ni_daq_700:  cs-config\n");
622
623	dev_dbg(&link->dev, "dio700_config\n");
624
625	ret = pcmcia_loop_config(link, dio700_pcmcia_config_loop, &req);
626	if (ret) {
627		dev_warn(&link->dev, "no configuration found\n");
628		goto failed;
629	}
630
631	if (!link->irq)
632		goto failed;
633
634	/*
635	   This actually configures the PCMCIA socket -- setting up
636	   the I/O windows and the interrupt mapping, and putting the
637	   card and host interface into "Memory and IO" mode.
638	 */
639	ret = pcmcia_request_configuration(link, &link->conf);
640	if (ret != 0)
641		goto failed;
642
643	/* Finally, report what we've done */
644	dev_info(&link->dev, "index 0x%02x", link->conf.ConfigIndex);
645	if (link->conf.Attributes & CONF_ENABLE_IRQ)
646		printk(", irq %d", link->irq);
647	if (link->io.NumPorts1)
648		printk(", io 0x%04x-0x%04x", link->io.BasePort1,
649		       link->io.BasePort1 + link->io.NumPorts1 - 1);
650	if (link->io.NumPorts2)
651		printk(" & 0x%04x-0x%04x", link->io.BasePort2,
652		       link->io.BasePort2 + link->io.NumPorts2 - 1);
653	if (link->win)
654		printk(", mem 0x%06lx-0x%06lx", req.Base,
655		       req.Base + req.Size - 1);
656	printk("\n");
657
658	return;
659
660failed:
661	printk(KERN_INFO "ni_daq_700 cs failed");
662	dio700_release(link);
663
664}				/* dio700_config */
665
666static void dio700_release(struct pcmcia_device *link)
667{
668	dev_dbg(&link->dev, "dio700_release\n");
669
670	pcmcia_disable_device(link);
671}				/* dio700_release */
672
673/*======================================================================
674
675    The card status event handler.  Mostly, this schedules other
676    stuff to run after an event is received.
677
678    When a CARD_REMOVAL event is received, we immediately set a
679    private flag to block future accesses to this device.  All the
680    functions that actually access the device should check this flag
681    to make sure the card is still present.
682
683======================================================================*/
684
685static int dio700_cs_suspend(struct pcmcia_device *link)
686{
687	struct local_info_t *local = link->priv;
688
689	/* Mark the device as stopped, to block IO until later */
690	local->stop = 1;
691	return 0;
692}				/* dio700_cs_suspend */
693
694static int dio700_cs_resume(struct pcmcia_device *link)
695{
696	struct local_info_t *local = link->priv;
697
698	local->stop = 0;
699	return 0;
700}				/* dio700_cs_resume */
701
702/*====================================================================*/
703
704static struct pcmcia_device_id dio700_cs_ids[] = {
705	/* N.B. These IDs should match those in dio700_boards */
706	PCMCIA_DEVICE_MANF_CARD(0x010b, 0x4743),	/* daqcard-700 */
707	PCMCIA_DEVICE_NULL
708};
709
710
711MODULE_DEVICE_TABLE(pcmcia, dio700_cs_ids);
712MODULE_AUTHOR("Fred Brooks <nsaspook@nsaspook.com>");
713MODULE_DESCRIPTION("Comedi driver for National Instruments "
714		   "PCMCIA DAQCard-700 DIO");
715MODULE_LICENSE("GPL");
716
717struct pcmcia_driver dio700_cs_driver = {
718	.probe = dio700_cs_attach,
719	.remove = dio700_cs_detach,
720	.suspend = dio700_cs_suspend,
721	.resume = dio700_cs_resume,
722	.id_table = dio700_cs_ids,
723	.owner = THIS_MODULE,
724	.drv = {
725		.name = dev_info,
726		},
727};
728
729static int __init init_dio700_cs(void)
730{
731	pcmcia_register_driver(&dio700_cs_driver);
732	return 0;
733}
734
735static void __exit exit_dio700_cs(void)
736{
737	pr_debug("ni_daq_700: unloading\n");
738	pcmcia_unregister_driver(&dio700_cs_driver);
739}
740
741int __init init_module(void)
742{
743	int ret;
744
745	ret = init_dio700_cs();
746	if (ret < 0)
747		return ret;
748
749	return comedi_driver_register(&driver_dio700);
750}
751
752void __exit cleanup_module(void)
753{
754	exit_dio700_cs();
755	comedi_driver_unregister(&driver_dio700);
756}
757