blob: 7ea64538e0556482f187f8b19b07bdd9ced246f8 [file] [log] [blame]
Fred Brooks1f9bbbe2009-02-19 10:05:01 -08001/*
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
Greg Kroah-Hartman25436dc2009-04-27 15:14:34 -070044#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080046#include "../comedidev.h"
47
48#include <linux/ioport.h>
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080049
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
Bill Pemberton2696fb52009-03-27 11:29:34 -040058#define DIO700_SIZE 8 /* size of io region used by board */
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080059
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053060static int dio700_attach(struct comedi_device *dev,
61 struct comedi_devconfig *it);
Bill Pembertonda91b262009-04-09 16:07:03 -040062static int dio700_detach(struct comedi_device *dev);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080063
64enum dio700_bustype { pcmcia_bustype };
65
Bill Pemberton4cab3742009-03-19 17:59:34 -040066struct dio700_board {
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080067 const char *name;
Bill Pemberton2696fb52009-03-27 11:29:34 -040068 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 */
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080073 unsigned int (*read_byte) (unsigned int address);
74 void (*write_byte) (unsigned int byte, unsigned int address);
Bill Pemberton4cab3742009-03-19 17:59:34 -040075};
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080076
Bill Pemberton4cab3742009-03-19 17:59:34 -040077static const struct dio700_board dio700_boards[] = {
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080078 {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053079 .name = "daqcard-700",
Graham M Howe9a16a922010-02-15 10:04:26 +000080 /* 0x10b is manufacturer id, 0x4743 is device id */
81 .device_id = 0x4743,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053082 .bustype = pcmcia_bustype,
83 .have_dio = 1,
84 },
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080085 {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053086 .name = "ni_daq_700",
Graham M Howe9a16a922010-02-15 10:04:26 +000087 /* 0x10b is manufacturer id, 0x4743 is device id */
88 .device_id = 0x4743,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053089 .bustype = pcmcia_bustype,
90 .have_dio = 1,
91 },
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080092};
93
94/*
95 * Useful for shorthand access to the particular board structure
96 */
Bill Pemberton4cab3742009-03-19 17:59:34 -040097#define thisboard ((const struct dio700_board *)dev->board_ptr)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -080098
Bill Pembertonadf32842009-03-16 22:16:23 -040099struct dio700_private {
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800100
Bill Pembertonadf32842009-03-16 22:16:23 -0400101 int data; /* number of data points left to be taken */
102};
103
Bill Pembertonadf32842009-03-16 22:16:23 -0400104#define devpriv ((struct dio700_private *)dev->private)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800105
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400106static struct comedi_driver driver_dio700 = {
Bill Pemberton68c3dbf2009-04-22 21:11:49 -0400107 .driver_name = "ni_daq_700",
108 .module = THIS_MODULE,
109 .attach = dio700_attach,
110 .detach = dio700_detach,
Bill Pemberton8629efa2009-04-23 15:54:56 -0400111 .num_names = ARRAY_SIZE(dio700_boards),
Bill Pemberton68c3dbf2009-04-22 21:11:49 -0400112 .board_name = &dio700_boards[0].name,
113 .offset = sizeof(struct dio700_board),
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800114};
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
Bill Pembertonda91b262009-04-09 16:07:03 -0400135static void do_config(struct comedi_device *dev, struct comedi_subdevice *s);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800136
Bill Pembertonda91b262009-04-09 16:07:03 -0400137void subdev_700_interrupt(struct comedi_device *dev, struct comedi_subdevice *s)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800138{
Bill Pemberton790c5542009-03-16 22:05:02 -0400139 short d;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800140
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}
148
149static int subdev_700_cb(int dir, int port, int data, unsigned long arg)
150{
151 /* port is always A for output and B for input (8255 emu) */
152 unsigned long iobase = arg;
153
154 if (dir) {
155 outb(data, iobase + DIO_W);
156 return 0;
157 } else {
158 return inb(iobase + DIO_R);
159 }
160}
161
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530162static int subdev_700_insn(struct comedi_device *dev,
163 struct comedi_subdevice *s, struct comedi_insn *insn,
164 unsigned int *data)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800165{
166 if (data[0]) {
167 s->state &= ~data[0];
168 s->state |= (data[0] & data[1]);
169
170 if (data[0] & 0xff)
171 CALLBACK_FUNC(1, _700_DATA, s->state & 0xff,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530172 CALLBACK_ARG);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800173 }
174
175 data[1] = s->state & 0xff;
176 data[1] |= CALLBACK_FUNC(0, _700_DATA, 0, CALLBACK_ARG) << 8;
177
178 return 2;
179}
180
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530181static int subdev_700_insn_config(struct comedi_device *dev,
182 struct comedi_subdevice *s,
183 struct comedi_insn *insn, unsigned int *data)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800184{
185
186 switch (data[0]) {
187 case INSN_CONFIG_DIO_INPUT:
188 break;
189 case INSN_CONFIG_DIO_OUTPUT:
190 break;
191 case INSN_CONFIG_DIO_QUERY:
192 data[1] =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530193 (s->
194 io_bits & (1 << CR_CHAN(insn->chanspec))) ? COMEDI_OUTPUT :
195 COMEDI_INPUT;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800196 return insn->n;
197 break;
198 default:
199 return -EINVAL;
200 }
201
202 return 1;
203}
204
Bill Pembertonda91b262009-04-09 16:07:03 -0400205static void do_config(struct comedi_device *dev, struct comedi_subdevice *s)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800206{ /* use powerup defaults */
207 return;
208}
209
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530210static int subdev_700_cmdtest(struct comedi_device *dev,
211 struct comedi_subdevice *s,
212 struct comedi_cmd *cmd)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800213{
214 int err = 0;
215 unsigned int tmp;
216
217 /* step 1 */
218
219 tmp = cmd->start_src;
220 cmd->start_src &= TRIG_NOW;
221 if (!cmd->start_src || tmp != cmd->start_src)
222 err++;
223
224 tmp = cmd->scan_begin_src;
225 cmd->scan_begin_src &= TRIG_EXT;
226 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
227 err++;
228
229 tmp = cmd->convert_src;
230 cmd->convert_src &= TRIG_FOLLOW;
231 if (!cmd->convert_src || tmp != cmd->convert_src)
232 err++;
233
234 tmp = cmd->scan_end_src;
235 cmd->scan_end_src &= TRIG_COUNT;
236 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
237 err++;
238
239 tmp = cmd->stop_src;
240 cmd->stop_src &= TRIG_NONE;
241 if (!cmd->stop_src || tmp != cmd->stop_src)
242 err++;
243
244 if (err)
245 return 1;
246
247 /* step 2 */
248
249 if (err)
250 return 2;
251
252 /* step 3 */
253
254 if (cmd->start_arg != 0) {
255 cmd->start_arg = 0;
256 err++;
257 }
258 if (cmd->scan_begin_arg != 0) {
259 cmd->scan_begin_arg = 0;
260 err++;
261 }
262 if (cmd->convert_arg != 0) {
263 cmd->convert_arg = 0;
264 err++;
265 }
266 if (cmd->scan_end_arg != 1) {
267 cmd->scan_end_arg = 1;
268 err++;
269 }
270 if (cmd->stop_arg != 0) {
271 cmd->stop_arg = 0;
272 err++;
273 }
274
275 if (err)
276 return 3;
277
278 /* step 4 */
279
280 if (err)
281 return 4;
282
283 return 0;
284}
285
Bill Pembertonda91b262009-04-09 16:07:03 -0400286static int subdev_700_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800287{
288 /* FIXME */
289
290 return 0;
291}
292
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530293static int subdev_700_cancel(struct comedi_device *dev,
294 struct comedi_subdevice *s)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800295{
296 /* FIXME */
297
298 return 0;
299}
300
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530301int subdev_700_init(struct comedi_device *dev, struct comedi_subdevice *s,
302 int (*cb) (int, int, int, unsigned long), unsigned long arg)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800303{
304 s->type = COMEDI_SUBD_DIO;
305 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
306 s->n_chan = 16;
307 s->range_table = &range_digital;
308 s->maxdata = 1;
309
310 s->private = kmalloc(sizeof(struct subdev_700_struct), GFP_KERNEL);
311 if (!s->private)
312 return -ENOMEM;
313
314 CALLBACK_ARG = arg;
Graham M Howe9a16a922010-02-15 10:04:26 +0000315 if (cb == NULL)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800316 CALLBACK_FUNC = subdev_700_cb;
Graham M Howe9a16a922010-02-15 10:04:26 +0000317 else
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800318 CALLBACK_FUNC = cb;
Graham M Howe9a16a922010-02-15 10:04:26 +0000319
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800320 s->insn_bits = subdev_700_insn;
321 s->insn_config = subdev_700_insn_config;
322
323 s->state = 0;
324 s->io_bits = 0x00ff;
325 do_config(dev, s);
326
327 return 0;
328}
329
Bill Pembertonda91b262009-04-09 16:07:03 -0400330int subdev_700_init_irq(struct comedi_device *dev, struct comedi_subdevice *s,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530331 int (*cb) (int, int, int, unsigned long),
332 unsigned long arg)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800333{
334 int ret;
335
336 ret = subdev_700_init(dev, s, cb, arg);
337 if (ret < 0)
338 return ret;
339
340 s->do_cmdtest = subdev_700_cmdtest;
341 s->do_cmd = subdev_700_cmd;
342 s->cancel = subdev_700_cancel;
343
344 subdevpriv->have_irq = 1;
345
346 return 0;
347}
348
Bill Pembertonda91b262009-04-09 16:07:03 -0400349void subdev_700_cleanup(struct comedi_device *dev, struct comedi_subdevice *s)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800350{
Graham M Howe9a16a922010-02-15 10:04:26 +0000351 if (s->private)
352 if (subdevpriv->have_irq)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800353
Graham M Howe9a16a922010-02-15 10:04:26 +0000354 kfree(s->private);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800355}
356
357EXPORT_SYMBOL(subdev_700_init);
358EXPORT_SYMBOL(subdev_700_init_irq);
359EXPORT_SYMBOL(subdev_700_cleanup);
360EXPORT_SYMBOL(subdev_700_interrupt);
361
Bill Pembertonda91b262009-04-09 16:07:03 -0400362static int dio700_attach(struct comedi_device *dev, struct comedi_devconfig *it)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800363{
Bill Pemberton34c43922009-03-16 22:05:14 -0400364 struct comedi_subdevice *s;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800365 unsigned long iobase = 0;
366#ifdef incomplete
367 unsigned int irq = 0;
368#endif
369 struct pcmcia_device *link;
370
371 /* allocate and initialize dev->private */
Bill Pembertonadf32842009-03-16 22:16:23 -0400372 if (alloc_private(dev, sizeof(struct dio700_private)) < 0)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800373 return -ENOMEM;
374
Bill Pemberton2696fb52009-03-27 11:29:34 -0400375 /* get base address, irq etc. based on bustype */
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800376 switch (thisboard->bustype) {
377 case pcmcia_bustype:
378 link = pcmcia_cur_dev; /* XXX hack */
379 if (!link)
380 return -EIO;
381 iobase = link->io.BasePort1;
382#ifdef incomplete
383 irq = link->irq.AssignedIRQ;
384#endif
385 break;
386 default:
387 printk("bug! couldn't determine board type\n");
388 return -EINVAL;
389 break;
390 }
391 printk("comedi%d: ni_daq_700: %s, io 0x%lx", dev->minor,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530392 thisboard->name, iobase);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800393#ifdef incomplete
Graham M Howe9a16a922010-02-15 10:04:26 +0000394 if (irq)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800395 printk(", irq %u", irq);
Graham M Howe9a16a922010-02-15 10:04:26 +0000396
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800397#endif
398
399 printk("\n");
400
401 if (iobase == 0) {
402 printk("io base address is zero!\n");
403 return -EINVAL;
404 }
405
406 dev->iobase = iobase;
407
408#ifdef incomplete
409 /* grab our IRQ */
410 dev->irq = irq;
411#endif
412
413 dev->board_name = thisboard->name;
414
415 if (alloc_subdevices(dev, 1) < 0)
416 return -ENOMEM;
417
418 /* DAQCard-700 dio */
419 s = dev->subdevices + 0;
420 subdev_700_init(dev, s, NULL, dev->iobase);
421
422 return 0;
423};
424
Bill Pembertonda91b262009-04-09 16:07:03 -0400425static int dio700_detach(struct comedi_device *dev)
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800426{
427 printk("comedi%d: ni_daq_700: cs-remove\n", dev->minor);
428
429 if (dev->subdevices)
430 subdev_700_cleanup(dev, dev->subdevices + 0);
431
432 if (thisboard->bustype != pcmcia_bustype && dev->iobase)
433 release_region(dev->iobase, DIO700_SIZE);
434 if (dev->irq)
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -0700435 free_irq(dev->irq, dev);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800436
437 return 0;
438};
439
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100440/* PCMCIA crap -- watch your words, please! */
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800441
442static void dio700_config(struct pcmcia_device *link);
443static void dio700_release(struct pcmcia_device *link);
444static int dio700_cs_suspend(struct pcmcia_device *p_dev);
445static int dio700_cs_resume(struct pcmcia_device *p_dev);
446
447/*
448 The attach() and detach() entry points are used to create and destroy
449 "instances" of the driver, where each instance represents everything
450 needed to manage one actual PCMCIA card.
451*/
452
453static int dio700_cs_attach(struct pcmcia_device *);
454static void dio700_cs_detach(struct pcmcia_device *);
455
456/*
457 You'll also need to prototype all the functions that will actually
458 be used to talk to your device. See 'memory_cs' for a good example
459 of a fully self-sufficient driver; the other drivers rely more or
460 less on other parts of the kernel.
461*/
462
463/*
464 The dev_info variable is the "key" that is used to match up this
465 device driver with appropriate cards, through the card configuration
466 database.
467*/
468
469static const dev_info_t dev_info = "ni_daq_700";
470
Bill Pemberton30570f02009-03-19 17:59:39 -0400471struct local_info_t {
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800472 struct pcmcia_device *link;
473 dev_node_t node;
474 int stop;
475 struct bus_operations *bus;
Bill Pemberton30570f02009-03-19 17:59:39 -0400476};
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800477
478/*======================================================================
479
480 dio700_cs_attach() creates an "instance" of the driver, allocating
481 local data structures for one device. The device is registered
482 with Card Services.
483
484 The dev_link structure is initialized, but we don't actually
485 configure the card at this point -- we wait until we receive a
486 card insertion event.
487
488======================================================================*/
489
490static int dio700_cs_attach(struct pcmcia_device *link)
491{
Bill Pemberton30570f02009-03-19 17:59:39 -0400492 struct local_info_t *local;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800493
494 printk(KERN_INFO "ni_daq_700: cs-attach\n");
495
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100496 dev_dbg(&link->dev, "dio700_cs_attach()\n");
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800497
498 /* Allocate space for private device-specific data */
Bill Pemberton30570f02009-03-19 17:59:39 -0400499 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800500 if (!local)
501 return -ENOMEM;
502 local->link = link;
503 link->priv = local;
504
505 /* Interrupt setup */
Frank Mori Hess3d1c2882009-09-22 16:32:55 -0400506 link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800507 link->irq.Handler = NULL;
508
509 /*
510 General socket configuration defaults can go here. In this
511 client, we assume very little, and rely on the CIS for almost
512 everything. In most clients, many details (i.e., number, sizes,
513 and attributes of IO windows) are fixed by the nature of the
514 device, and can be hard-wired here.
515 */
516 link->conf.Attributes = 0;
517 link->conf.IntType = INT_MEMORY_AND_IO;
518
519 pcmcia_cur_dev = link;
520
521 dio700_config(link);
522
523 return 0;
524} /* dio700_cs_attach */
525
526/*======================================================================
527
528 This deletes a driver "instance". The device is de-registered
529 with Card Services. If it has been released, all local data
530 structures are freed. Otherwise, the structures will be freed
531 when the device is released.
532
533======================================================================*/
534
535static void dio700_cs_detach(struct pcmcia_device *link)
536{
537
538 printk(KERN_INFO "ni_daq_700: cs-detach!\n");
539
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100540 dev_dbg(&link->dev, "dio700_cs_detach\n");
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800541
542 if (link->dev_node) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530543 ((struct local_info_t *)link->priv)->stop = 1;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800544 dio700_release(link);
545 }
546
Bill Pemberton30570f02009-03-19 17:59:39 -0400547 /* This points to the parent struct local_info_t struct */
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800548 if (link->priv)
549 kfree(link->priv);
550
551} /* dio700_cs_detach */
552
553/*======================================================================
554
555 dio700_config() is scheduled to run after a CARD_INSERTION event
556 is received, to configure the PCMCIA socket, and to make the
557 device available to the system.
558
559======================================================================*/
560
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100561static int dio700_pcmcia_config_loop(struct pcmcia_device *p_dev,
562 cistpl_cftable_entry_t *cfg,
563 cistpl_cftable_entry_t *dflt,
564 unsigned int vcc,
565 void *priv_data)
566{
567 win_req_t *req = priv_data;
568 memreq_t map;
569
570 if (cfg->index == 0)
571 return -ENODEV;
572
573 /* Does this card need audio output? */
574 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
575 p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
576 p_dev->conf.Status = CCSR_AUDIO_ENA;
577 }
578
579 /* Do we need to allocate an interrupt? */
580 if (cfg->irq.IRQInfo1 || dflt->irq.IRQInfo1)
581 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
582
583 /* IO window settings */
584 p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0;
585 if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
586 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
587 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
588 if (!(io->flags & CISTPL_IO_8BIT))
589 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
590 if (!(io->flags & CISTPL_IO_16BIT))
591 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
592 p_dev->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
593 p_dev->io.BasePort1 = io->win[0].base;
594 p_dev->io.NumPorts1 = io->win[0].len;
595 if (io->nwin > 1) {
596 p_dev->io.Attributes2 = p_dev->io.Attributes1;
597 p_dev->io.BasePort2 = io->win[1].base;
598 p_dev->io.NumPorts2 = io->win[1].len;
599 }
600 /* This reserves IO space but doesn't actually enable it */
601 if (pcmcia_request_io(p_dev, &p_dev->io) != 0)
602 return -ENODEV;
603 }
604
605 if ((cfg->mem.nwin > 0) || (dflt->mem.nwin > 0)) {
606 cistpl_mem_t *mem =
607 (cfg->mem.nwin) ? &cfg->mem : &dflt->mem;
608 req->Attributes = WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM;
609 req->Attributes |= WIN_ENABLE;
610 req->Base = mem->win[0].host_addr;
611 req->Size = mem->win[0].len;
612 if (req->Size < 0x1000)
613 req->Size = 0x1000;
614 req->AccessSpeed = 0;
Dominik Brodowski6838b032009-11-03 01:31:52 +0100615 if (pcmcia_request_window(p_dev, req, &p_dev->win))
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100616 return -ENODEV;
617 map.Page = 0;
618 map.CardOffset = mem->win[0].card_addr;
Magnus Damm868575d2006-12-13 19:46:43 +0900619 if (pcmcia_map_mem_page(p_dev, p_dev->win, &map))
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100620 return -ENODEV;
621 }
622 /* If we got this far, we're cool! */
623 return 0;
624}
625
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800626static void dio700_config(struct pcmcia_device *link)
627{
Bill Pemberton30570f02009-03-19 17:59:39 -0400628 struct local_info_t *dev = link->priv;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800629 win_req_t req;
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100630 int ret;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800631
632 printk(KERN_INFO "ni_daq_700: cs-config\n");
633
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100634 dev_dbg(&link->dev, "dio700_config\n");
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800635
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100636 ret = pcmcia_loop_config(link, dio700_pcmcia_config_loop, &req);
637 if (ret) {
638 dev_warn(&link->dev, "no configuration found\n");
639 goto failed;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800640 }
641
642 /*
643 Allocate an interrupt line. Note that this does not assign a
644 handler to the interrupt, unless the 'Handler' member of the
645 irq structure is initialized.
646 */
Bill Pembertonc3744132009-04-22 21:11:47 -0400647 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100648 ret = pcmcia_request_irq(link, &link->irq);
649 if (ret)
650 goto failed;
Bill Pembertonc3744132009-04-22 21:11:47 -0400651 }
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800652
653 /*
654 This actually configures the PCMCIA socket -- setting up
655 the I/O windows and the interrupt mapping, and putting the
656 card and host interface into "Memory and IO" mode.
657 */
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100658 ret = pcmcia_request_configuration(link, &link->conf);
659 if (ret != 0)
660 goto failed;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800661
662 /*
663 At this point, the dev_node_t structure(s) need to be
664 initialized and arranged in a linked list at link->dev.
665 */
666 sprintf(dev->node.dev_name, "ni_daq_700");
667 dev->node.major = dev->node.minor = 0;
668 link->dev_node = &dev->node;
669
670 /* Finally, report what we've done */
671 printk(KERN_INFO "%s: index 0x%02x",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530672 dev->node.dev_name, link->conf.ConfigIndex);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800673 if (link->conf.Attributes & CONF_ENABLE_IRQ)
674 printk(", irq %d", link->irq.AssignedIRQ);
675 if (link->io.NumPorts1)
676 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530677 link->io.BasePort1 + link->io.NumPorts1 - 1);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800678 if (link->io.NumPorts2)
679 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530680 link->io.BasePort2 + link->io.NumPorts2 - 1);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800681 if (link->win)
682 printk(", mem 0x%06lx-0x%06lx", req.Base,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530683 req.Base + req.Size - 1);
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800684 printk("\n");
685
686 return;
687
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100688failed:
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800689 printk(KERN_INFO "ni_daq_700 cs failed");
690 dio700_release(link);
691
692} /* dio700_config */
693
694static void dio700_release(struct pcmcia_device *link)
695{
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100696 dev_dbg(&link->dev, "dio700_release\n");
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800697
698 pcmcia_disable_device(link);
699} /* dio700_release */
700
701/*======================================================================
702
703 The card status event handler. Mostly, this schedules other
704 stuff to run after an event is received.
705
706 When a CARD_REMOVAL event is received, we immediately set a
707 private flag to block future accesses to this device. All the
708 functions that actually access the device should check this flag
709 to make sure the card is still present.
710
711======================================================================*/
712
713static int dio700_cs_suspend(struct pcmcia_device *link)
714{
Bill Pemberton30570f02009-03-19 17:59:39 -0400715 struct local_info_t *local = link->priv;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800716
717 /* Mark the device as stopped, to block IO until later */
718 local->stop = 1;
719 return 0;
720} /* dio700_cs_suspend */
721
722static int dio700_cs_resume(struct pcmcia_device *link)
723{
Bill Pemberton30570f02009-03-19 17:59:39 -0400724 struct local_info_t *local = link->priv;
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800725
726 local->stop = 0;
727 return 0;
728} /* dio700_cs_resume */
729
730/*====================================================================*/
731
732static struct pcmcia_device_id dio700_cs_ids[] = {
733 /* N.B. These IDs should match those in dio700_boards */
734 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x4743), /* daqcard-700 */
735 PCMCIA_DEVICE_NULL
736};
737
738MODULE_LICENSE("GPL");
739MODULE_DEVICE_TABLE(pcmcia, dio700_cs_ids);
740
741struct pcmcia_driver dio700_cs_driver = {
742 .probe = dio700_cs_attach,
743 .remove = dio700_cs_detach,
744 .suspend = dio700_cs_suspend,
745 .resume = dio700_cs_resume,
746 .id_table = dio700_cs_ids,
747 .owner = THIS_MODULE,
748 .drv = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530749 .name = dev_info,
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800750 },
751};
752
753static int __init init_dio700_cs(void)
754{
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800755 pcmcia_register_driver(&dio700_cs_driver);
756 return 0;
757}
758
759static void __exit exit_dio700_cs(void)
760{
Dominik Brodowski55a19b32009-10-29 00:54:49 +0100761 pr_debug("ni_daq_700: unloading\n");
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800762 pcmcia_unregister_driver(&dio700_cs_driver);
763}
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530764
Fred Brooks1f9bbbe2009-02-19 10:05:01 -0800765int __init init_module(void)
766{
767 int ret;
768
769 ret = init_dio700_cs();
770 if (ret < 0)
771 return ret;
772
773 return comedi_driver_register(&driver_dio700);
774}
775
776void __exit cleanup_module(void)
777{
778 exit_dio700_cs();
779 comedi_driver_unregister(&driver_dio700);
780}