Commit | Line | Data |
---|---|---|
62ed6662 BB |
1 | /*====================================================================== |
2 | ||
3 | comedi/drivers/quatech_daqp_cs.c | |
4 | ||
5 | Quatech DAQP PCMCIA data capture cards COMEDI client driver | |
6 | Copyright (C) 2000, 2003 Brent Baccala <baccala@freesoft.org> | |
7 | The DAQP interface code in this file is released into the public domain. | |
8 | ||
9 | COMEDI - Linux Control and Measurement Device Interface | |
10 | Copyright (C) 1998 David A. Schleef <ds@schleef.org> | |
11 | http://www.comedi.org/ | |
12 | ||
13 | quatech_daqp_cs.c 1.10 | |
14 | ||
15 | Documentation for the DAQP PCMCIA cards can be found on Quatech's site: | |
16 | ||
17 | ftp://ftp.quatech.com/Manuals/daqp-208.pdf | |
18 | ||
19 | This manual is for both the DAQP-208 and the DAQP-308. | |
20 | ||
21 | What works: | |
22 | ||
23 | - A/D conversion | |
24 | - 8 channels | |
25 | - 4 gain ranges | |
26 | - ground ref or differential | |
27 | - single-shot and timed both supported | |
28 | - D/A conversion, single-shot | |
29 | - digital I/O | |
30 | ||
31 | What doesn't: | |
32 | ||
33 | - any kind of triggering - external or D/A channel 1 | |
34 | - the card's optional expansion board | |
35 | - the card's timer (for anything other than A/D conversion) | |
36 | - D/A update modes other than immediate (i.e, timed) | |
37 | - fancier timing modes | |
38 | - setting card's FIFO buffer thresholds to anything but default | |
39 | ||
40 | ======================================================================*/ | |
41 | ||
42 | /* | |
43 | Driver: quatech_daqp_cs | |
44 | Description: Quatech DAQP PCMCIA data capture cards | |
45 | Author: Brent Baccala <baccala@freesoft.org> | |
46 | Status: works | |
47 | Devices: [Quatech] DAQP-208 (daqp), DAQP-308 | |
48 | */ | |
49 | ||
50 | #include "../comedidev.h" | |
51 | ||
62ed6662 BB |
52 | #include <pcmcia/cs_types.h> |
53 | #include <pcmcia/cs.h> | |
54 | #include <pcmcia/cistpl.h> | |
55 | #include <pcmcia/cisreg.h> | |
56 | #include <pcmcia/ds.h> | |
57 | ||
58 | /* | |
59 | All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If | |
60 | you do not define PCMCIA_DEBUG at all, all the debug code will be | |
61 | left out. If you compile with PCMCIA_DEBUG=0, the debug code will | |
62 | be present but disabled -- but it can then be enabled for specific | |
63 | modules at load time with a 'pc_debug=#' option to insmod. | |
64 | */ | |
65 | ||
66 | #ifdef PCMCIA_DEBUG | |
67 | static int pc_debug = PCMCIA_DEBUG; | |
68 | module_param(pc_debug, int, 0644); | |
69 | #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args) | |
70 | static char *version = "quatech_daqp_cs.c 1.10 2003/04/21 (Brent Baccala)"; | |
71 | #else | |
72 | #define DEBUG(n, args...) | |
73 | #endif | |
74 | ||
75 | /* Maximum number of separate DAQP devices we'll allow */ | |
76 | #define MAX_DEV 4 | |
77 | ||
78 | typedef struct local_info_t { | |
79 | struct pcmcia_device *link; | |
80 | dev_node_t node; | |
81 | int stop; | |
82 | int table_index; | |
83 | char board_name[32]; | |
84 | ||
85 | enum { semaphore, buffer } interrupt_mode; | |
86 | ||
87 | struct semaphore eos; | |
88 | ||
71b5f4f1 | 89 | struct comedi_device *dev; |
34c43922 | 90 | struct comedi_subdevice *s; |
62ed6662 BB |
91 | int count; |
92 | } local_info_t; | |
93 | ||
94 | /* A list of "instances" of the device. */ | |
95 | ||
96 | static local_info_t *dev_table[MAX_DEV] = { NULL, /* ... */ }; | |
97 | ||
98 | /* The DAQP communicates with the system through a 16 byte I/O window. */ | |
99 | ||
100 | #define DAQP_FIFO_SIZE 4096 | |
101 | ||
102 | #define DAQP_FIFO 0 | |
103 | #define DAQP_SCANLIST 1 | |
104 | #define DAQP_CONTROL 2 | |
105 | #define DAQP_STATUS 2 | |
106 | #define DAQP_DIGITAL_IO 3 | |
107 | #define DAQP_PACER_LOW 4 | |
108 | #define DAQP_PACER_MID 5 | |
109 | #define DAQP_PACER_HIGH 6 | |
110 | #define DAQP_COMMAND 7 | |
111 | #define DAQP_DA 8 | |
112 | #define DAQP_TIMER 10 | |
113 | #define DAQP_AUX 15 | |
114 | ||
115 | #define DAQP_SCANLIST_DIFFERENTIAL 0x4000 | |
116 | #define DAQP_SCANLIST_GAIN(x) ((x)<<12) | |
117 | #define DAQP_SCANLIST_CHANNEL(x) ((x)<<8) | |
118 | #define DAQP_SCANLIST_START 0x0080 | |
119 | #define DAQP_SCANLIST_EXT_GAIN(x) ((x)<<4) | |
120 | #define DAQP_SCANLIST_EXT_CHANNEL(x) (x) | |
121 | ||
122 | #define DAQP_CONTROL_PACER_100kHz 0xc0 | |
123 | #define DAQP_CONTROL_PACER_1MHz 0x80 | |
124 | #define DAQP_CONTROL_PACER_5MHz 0x40 | |
125 | #define DAQP_CONTROL_PACER_EXTERNAL 0x00 | |
126 | #define DAQP_CONTORL_EXPANSION 0x20 | |
127 | #define DAQP_CONTROL_EOS_INT_ENABLE 0x10 | |
128 | #define DAQP_CONTROL_FIFO_INT_ENABLE 0x08 | |
129 | #define DAQP_CONTROL_TRIGGER_ONESHOT 0x00 | |
130 | #define DAQP_CONTROL_TRIGGER_CONTINUOUS 0x04 | |
131 | #define DAQP_CONTROL_TRIGGER_INTERNAL 0x00 | |
132 | #define DAQP_CONTROL_TRIGGER_EXTERNAL 0x02 | |
133 | #define DAQP_CONTROL_TRIGGER_RISING 0x00 | |
134 | #define DAQP_CONTROL_TRIGGER_FALLING 0x01 | |
135 | ||
136 | #define DAQP_STATUS_IDLE 0x80 | |
137 | #define DAQP_STATUS_RUNNING 0x40 | |
138 | #define DAQP_STATUS_EVENTS 0x38 | |
139 | #define DAQP_STATUS_DATA_LOST 0x20 | |
140 | #define DAQP_STATUS_END_OF_SCAN 0x10 | |
141 | #define DAQP_STATUS_FIFO_THRESHOLD 0x08 | |
142 | #define DAQP_STATUS_FIFO_FULL 0x04 | |
143 | #define DAQP_STATUS_FIFO_NEARFULL 0x02 | |
144 | #define DAQP_STATUS_FIFO_EMPTY 0x01 | |
145 | ||
146 | #define DAQP_COMMAND_ARM 0x80 | |
147 | #define DAQP_COMMAND_RSTF 0x40 | |
148 | #define DAQP_COMMAND_RSTQ 0x20 | |
149 | #define DAQP_COMMAND_STOP 0x10 | |
150 | #define DAQP_COMMAND_LATCH 0x08 | |
151 | #define DAQP_COMMAND_100kHz 0x00 | |
152 | #define DAQP_COMMAND_50kHz 0x02 | |
153 | #define DAQP_COMMAND_25kHz 0x04 | |
154 | #define DAQP_COMMAND_FIFO_DATA 0x01 | |
155 | #define DAQP_COMMAND_FIFO_PROGRAM 0x00 | |
156 | ||
157 | #define DAQP_AUX_TRIGGER_TTL 0x00 | |
158 | #define DAQP_AUX_TRIGGER_ANALOG 0x80 | |
159 | #define DAQP_AUX_TRIGGER_PRETRIGGER 0x40 | |
160 | #define DAQP_AUX_TIMER_INT_ENABLE 0x20 | |
161 | #define DAQP_AUX_TIMER_RELOAD 0x00 | |
162 | #define DAQP_AUX_TIMER_PAUSE 0x08 | |
163 | #define DAQP_AUX_TIMER_GO 0x10 | |
164 | #define DAQP_AUX_TIMER_GO_EXTERNAL 0x18 | |
165 | #define DAQP_AUX_TIMER_EXTERNAL_SRC 0x04 | |
166 | #define DAQP_AUX_TIMER_INTERNAL_SRC 0x00 | |
167 | #define DAQP_AUX_DA_DIRECT 0x00 | |
168 | #define DAQP_AUX_DA_OVERFLOW 0x01 | |
169 | #define DAQP_AUX_DA_EXTERNAL 0x02 | |
170 | #define DAQP_AUX_DA_PACER 0x03 | |
171 | ||
172 | #define DAQP_AUX_RUNNING 0x80 | |
173 | #define DAQP_AUX_TRIGGERED 0x40 | |
174 | #define DAQP_AUX_DA_BUFFER 0x20 | |
175 | #define DAQP_AUX_TIMER_OVERFLOW 0x10 | |
176 | #define DAQP_AUX_CONVERSION 0x08 | |
177 | #define DAQP_AUX_DATA_LOST 0x04 | |
178 | #define DAQP_AUX_FIFO_NEARFULL 0x02 | |
179 | #define DAQP_AUX_FIFO_EMPTY 0x01 | |
180 | ||
181 | /* These range structures tell COMEDI how the sample values map to | |
182 | * voltages. The A/D converter has four ranges: +/- 10V through | |
183 | * +/- 1.25V, and the D/A converter has only one: +/- 5V. | |
184 | */ | |
185 | ||
9ced1de6 | 186 | static const struct comedi_lrange range_daqp_ai = { 4, { |
62ed6662 BB |
187 | BIP_RANGE(10), |
188 | BIP_RANGE(5), | |
189 | BIP_RANGE(2.5), | |
190 | BIP_RANGE(1.25) | |
191 | } | |
192 | }; | |
193 | ||
9ced1de6 | 194 | static const struct comedi_lrange range_daqp_ao = { 1, {BIP_RANGE(5)} }; |
62ed6662 BB |
195 | |
196 | /*====================================================================*/ | |
197 | ||
198 | /* comedi interface code */ | |
199 | ||
71b5f4f1 BP |
200 | static int daqp_attach(struct comedi_device * dev, comedi_devconfig * it); |
201 | static int daqp_detach(struct comedi_device * dev); | |
139dfbdf | 202 | static struct comedi_driver driver_daqp = { |
62ed6662 BB |
203 | driver_name:"quatech_daqp_cs", |
204 | module:THIS_MODULE, | |
205 | attach:daqp_attach, | |
206 | detach:daqp_detach, | |
207 | }; | |
208 | ||
209 | #ifdef DAQP_DEBUG | |
210 | ||
71b5f4f1 | 211 | static void daqp_dump(struct comedi_device * dev) |
62ed6662 BB |
212 | { |
213 | printk("DAQP: status %02x; aux status %02x\n", | |
214 | inb(dev->iobase + DAQP_STATUS), inb(dev->iobase + DAQP_AUX)); | |
215 | } | |
216 | ||
217 | static void hex_dump(char *str, void *ptr, int len) | |
218 | { | |
219 | unsigned char *cptr = ptr; | |
220 | int i; | |
221 | ||
222 | printk(str); | |
223 | ||
224 | for (i = 0; i < len; i++) { | |
225 | if (i % 16 == 0) { | |
226 | printk("\n0x%08x:", (unsigned int)cptr); | |
227 | } | |
228 | printk(" %02x", *(cptr++)); | |
229 | } | |
230 | printk("\n"); | |
231 | } | |
232 | ||
233 | #endif | |
234 | ||
235 | /* Cancel a running acquisition */ | |
236 | ||
34c43922 | 237 | static int daqp_ai_cancel(struct comedi_device * dev, struct comedi_subdevice * s) |
62ed6662 BB |
238 | { |
239 | local_info_t *local = (local_info_t *) s->private; | |
240 | ||
241 | if (local->stop) { | |
242 | return -EIO; | |
243 | } | |
244 | ||
245 | outb(DAQP_COMMAND_STOP, dev->iobase + DAQP_COMMAND); | |
246 | ||
247 | /* flush any linguring data in FIFO - superfluous here */ | |
248 | /* outb(DAQP_COMMAND_RSTF, dev->iobase+DAQP_COMMAND); */ | |
249 | ||
250 | local->interrupt_mode = semaphore; | |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
255 | /* Interrupt handler | |
256 | * | |
257 | * Operates in one of two modes. If local->interrupt_mode is | |
258 | * 'semaphore', just signal the local->eos semaphore and return | |
259 | * (one-shot mode). Otherwise (continuous mode), read data in from | |
260 | * the card, transfer it to the buffer provided by the higher-level | |
261 | * comedi kernel module, and signal various comedi callback routines, | |
262 | * which run pretty quick. | |
263 | */ | |
264 | ||
265 | static void daqp_interrupt(int irq, void *dev_id PT_REGS_ARG) | |
266 | { | |
267 | local_info_t *local = (local_info_t *) dev_id; | |
71b5f4f1 | 268 | struct comedi_device *dev; |
34c43922 | 269 | struct comedi_subdevice *s; |
62ed6662 BB |
270 | int loop_limit = 10000; |
271 | int status; | |
272 | ||
273 | if (local == NULL) { | |
274 | printk(KERN_WARNING | |
275 | "daqp_interrupt(): irq %d for unknown device.\n", irq); | |
276 | return; | |
277 | } | |
278 | ||
279 | dev = local->dev; | |
280 | if (dev == NULL) { | |
281 | printk(KERN_WARNING "daqp_interrupt(): NULL comedi_device.\n"); | |
282 | return; | |
283 | } | |
284 | ||
285 | if (!dev->attached) { | |
286 | printk(KERN_WARNING | |
71b5f4f1 | 287 | "daqp_interrupt(): struct comedi_device not yet attached.\n"); |
62ed6662 BB |
288 | return; |
289 | } | |
290 | ||
291 | s = local->s; | |
292 | if (s == NULL) { | |
293 | printk(KERN_WARNING | |
294 | "daqp_interrupt(): NULL comedi_subdevice.\n"); | |
295 | return; | |
296 | } | |
297 | ||
298 | if ((local_info_t *) s->private != local) { | |
299 | printk(KERN_WARNING | |
300 | "daqp_interrupt(): invalid comedi_subdevice.\n"); | |
301 | return; | |
302 | } | |
303 | ||
304 | switch (local->interrupt_mode) { | |
305 | ||
306 | case semaphore: | |
307 | ||
308 | up(&local->eos); | |
309 | break; | |
310 | ||
311 | case buffer: | |
312 | ||
313 | while (!((status = inb(dev->iobase + DAQP_STATUS)) | |
314 | & DAQP_STATUS_FIFO_EMPTY)) { | |
315 | ||
790c5541 | 316 | short data; |
62ed6662 BB |
317 | |
318 | if (status & DAQP_STATUS_DATA_LOST) { | |
319 | s->async->events |= | |
320 | COMEDI_CB_EOA | COMEDI_CB_OVERFLOW; | |
321 | printk("daqp: data lost\n"); | |
322 | daqp_ai_cancel(dev, s); | |
323 | break; | |
324 | } | |
325 | ||
326 | data = inb(dev->iobase + DAQP_FIFO); | |
327 | data |= inb(dev->iobase + DAQP_FIFO) << 8; | |
328 | data ^= 0x8000; | |
329 | ||
330 | comedi_buf_put(s->async, data); | |
331 | ||
332 | /* If there's a limit, decrement it | |
333 | * and stop conversion if zero | |
334 | */ | |
335 | ||
336 | if (local->count > 0) { | |
337 | local->count--; | |
338 | if (local->count == 0) { | |
339 | daqp_ai_cancel(dev, s); | |
340 | s->async->events |= COMEDI_CB_EOA; | |
341 | break; | |
342 | } | |
343 | } | |
344 | ||
345 | if ((loop_limit--) <= 0) | |
346 | break; | |
347 | } | |
348 | ||
349 | if (loop_limit <= 0) { | |
350 | printk(KERN_WARNING | |
351 | "loop_limit reached in daqp_interrupt()\n"); | |
352 | daqp_ai_cancel(dev, s); | |
353 | s->async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR; | |
354 | } | |
355 | ||
356 | s->async->events |= COMEDI_CB_BLOCK; | |
357 | ||
358 | comedi_event(dev, s); | |
359 | } | |
360 | } | |
361 | ||
362 | /* One-shot analog data acquisition routine */ | |
363 | ||
34c43922 | 364 | static int daqp_ai_insn_read(struct comedi_device * dev, struct comedi_subdevice * s, |
790c5541 | 365 | comedi_insn * insn, unsigned int * data) |
62ed6662 BB |
366 | { |
367 | local_info_t *local = (local_info_t *) s->private; | |
368 | int i; | |
369 | int v; | |
370 | int counter = 10000; | |
371 | ||
372 | if (local->stop) { | |
373 | return -EIO; | |
374 | } | |
375 | ||
376 | /* Stop any running conversion */ | |
377 | daqp_ai_cancel(dev, s); | |
378 | ||
379 | outb(0, dev->iobase + DAQP_AUX); | |
380 | ||
381 | /* Reset scan list queue */ | |
382 | outb(DAQP_COMMAND_RSTQ, dev->iobase + DAQP_COMMAND); | |
383 | ||
384 | /* Program one scan list entry */ | |
385 | ||
386 | v = DAQP_SCANLIST_CHANNEL(CR_CHAN(insn->chanspec)) | |
387 | | DAQP_SCANLIST_GAIN(CR_RANGE(insn->chanspec)); | |
388 | ||
389 | if (CR_AREF(insn->chanspec) == AREF_DIFF) { | |
390 | v |= DAQP_SCANLIST_DIFFERENTIAL; | |
391 | } | |
392 | ||
393 | v |= DAQP_SCANLIST_START; | |
394 | ||
395 | outb(v & 0xff, dev->iobase + DAQP_SCANLIST); | |
396 | outb(v >> 8, dev->iobase + DAQP_SCANLIST); | |
397 | ||
398 | /* Reset data FIFO (see page 28 of DAQP User's Manual) */ | |
399 | ||
400 | outb(DAQP_COMMAND_RSTF, dev->iobase + DAQP_COMMAND); | |
401 | ||
402 | /* Set trigger */ | |
403 | ||
404 | v = DAQP_CONTROL_TRIGGER_ONESHOT | DAQP_CONTROL_TRIGGER_INTERNAL | |
405 | | DAQP_CONTROL_PACER_100kHz | DAQP_CONTROL_EOS_INT_ENABLE; | |
406 | ||
407 | outb(v, dev->iobase + DAQP_CONTROL); | |
408 | ||
409 | /* Reset any pending interrupts (my card has a tendancy to require | |
410 | * require multiple reads on the status register to achieve this) | |
411 | */ | |
412 | ||
413 | while (--counter | |
414 | && (inb(dev->iobase + DAQP_STATUS) & DAQP_STATUS_EVENTS)) ; | |
415 | if (!counter) { | |
416 | printk("daqp: couldn't clear interrupts in status register\n"); | |
417 | return -1; | |
418 | } | |
419 | ||
420 | /* Make sure semaphore is blocked */ | |
421 | sema_init(&local->eos, 0); | |
422 | local->interrupt_mode = semaphore; | |
423 | local->dev = dev; | |
424 | local->s = s; | |
425 | ||
426 | for (i = 0; i < insn->n; i++) { | |
427 | ||
428 | /* Start conversion */ | |
429 | outb(DAQP_COMMAND_ARM | DAQP_COMMAND_FIFO_DATA, | |
430 | dev->iobase + DAQP_COMMAND); | |
431 | ||
432 | /* Wait for interrupt service routine to unblock semaphore */ | |
433 | /* Maybe could use a timeout here, but it's interruptible */ | |
434 | if (down_interruptible(&local->eos)) | |
435 | return -EINTR; | |
436 | ||
437 | data[i] = inb(dev->iobase + DAQP_FIFO); | |
438 | data[i] |= inb(dev->iobase + DAQP_FIFO) << 8; | |
439 | data[i] ^= 0x8000; | |
440 | } | |
441 | ||
442 | return insn->n; | |
443 | } | |
444 | ||
445 | /* This function converts ns nanoseconds to a counter value suitable | |
446 | * for programming the device. We always use the DAQP's 5 MHz clock, | |
447 | * which with its 24-bit counter, allows values up to 84 seconds. | |
448 | * Also, the function adjusts ns so that it cooresponds to the actual | |
449 | * time that the device will use. | |
450 | */ | |
451 | ||
452 | static int daqp_ns_to_timer(unsigned int *ns, int round) | |
453 | { | |
454 | int timer; | |
455 | ||
456 | timer = *ns / 200; | |
457 | *ns = timer * 200; | |
458 | ||
459 | return timer; | |
460 | } | |
461 | ||
462 | /* cmdtest tests a particular command to see if it is valid. | |
463 | * Using the cmdtest ioctl, a user can create a valid cmd | |
464 | * and then have it executed by the cmd ioctl. | |
465 | * | |
466 | * cmdtest returns 1,2,3,4 or 0, depending on which tests | |
467 | * the command passes. | |
468 | */ | |
469 | ||
34c43922 | 470 | static int daqp_ai_cmdtest(struct comedi_device * dev, struct comedi_subdevice * s, |
62ed6662 BB |
471 | comedi_cmd * cmd) |
472 | { | |
473 | int err = 0; | |
474 | int tmp; | |
475 | ||
476 | /* step 1: make sure trigger sources are trivially valid */ | |
477 | ||
478 | tmp = cmd->start_src; | |
479 | cmd->start_src &= TRIG_NOW; | |
480 | if (!cmd->start_src || tmp != cmd->start_src) | |
481 | err++; | |
482 | ||
483 | tmp = cmd->scan_begin_src; | |
484 | cmd->scan_begin_src &= TRIG_TIMER | TRIG_FOLLOW; | |
485 | if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src) | |
486 | err++; | |
487 | ||
488 | tmp = cmd->convert_src; | |
489 | cmd->convert_src &= TRIG_TIMER | TRIG_NOW; | |
490 | if (!cmd->convert_src || tmp != cmd->convert_src) | |
491 | err++; | |
492 | ||
493 | tmp = cmd->scan_end_src; | |
494 | cmd->scan_end_src &= TRIG_COUNT; | |
495 | if (!cmd->scan_end_src || tmp != cmd->scan_end_src) | |
496 | err++; | |
497 | ||
498 | tmp = cmd->stop_src; | |
499 | cmd->stop_src &= TRIG_COUNT | TRIG_NONE; | |
500 | if (!cmd->stop_src || tmp != cmd->stop_src) | |
501 | err++; | |
502 | ||
503 | if (err) | |
504 | return 1; | |
505 | ||
506 | /* step 2: make sure trigger sources are unique and mutually compatible */ | |
507 | ||
508 | /* note that mutual compatiblity is not an issue here */ | |
509 | if (cmd->scan_begin_src != TRIG_TIMER && | |
510 | cmd->scan_begin_src != TRIG_FOLLOW) | |
511 | err++; | |
512 | if (cmd->convert_src != TRIG_NOW && cmd->convert_src != TRIG_TIMER) | |
513 | err++; | |
514 | if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW) | |
515 | err++; | |
516 | if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE) | |
517 | err++; | |
518 | ||
519 | if (err) | |
520 | return 2; | |
521 | ||
522 | /* step 3: make sure arguments are trivially compatible */ | |
523 | ||
524 | if (cmd->start_arg != 0) { | |
525 | cmd->start_arg = 0; | |
526 | err++; | |
527 | } | |
528 | #define MAX_SPEED 10000 /* 100 kHz - in nanoseconds */ | |
529 | ||
530 | if (cmd->scan_begin_src == TRIG_TIMER | |
531 | && cmd->scan_begin_arg < MAX_SPEED) { | |
532 | cmd->scan_begin_arg = MAX_SPEED; | |
533 | err++; | |
534 | } | |
535 | ||
536 | /* If both scan_begin and convert are both timer values, the only | |
537 | * way that can make sense is if the scan time is the number of | |
538 | * conversions times the convert time | |
539 | */ | |
540 | ||
541 | if (cmd->scan_begin_src == TRIG_TIMER && cmd->convert_src == TRIG_TIMER | |
542 | && cmd->scan_begin_arg != | |
543 | cmd->convert_arg * cmd->scan_end_arg) { | |
544 | err++; | |
545 | } | |
546 | ||
547 | if (cmd->convert_src == TRIG_TIMER && cmd->convert_arg < MAX_SPEED) { | |
548 | cmd->convert_arg = MAX_SPEED; | |
549 | err++; | |
550 | } | |
551 | ||
552 | if (cmd->scan_end_arg != cmd->chanlist_len) { | |
553 | cmd->scan_end_arg = cmd->chanlist_len; | |
554 | err++; | |
555 | } | |
556 | if (cmd->stop_src == TRIG_COUNT) { | |
557 | if (cmd->stop_arg > 0x00ffffff) { | |
558 | cmd->stop_arg = 0x00ffffff; | |
559 | err++; | |
560 | } | |
561 | } else { | |
562 | /* TRIG_NONE */ | |
563 | if (cmd->stop_arg != 0) { | |
564 | cmd->stop_arg = 0; | |
565 | err++; | |
566 | } | |
567 | } | |
568 | ||
569 | if (err) | |
570 | return 3; | |
571 | ||
572 | /* step 4: fix up any arguments */ | |
573 | ||
574 | if (cmd->scan_begin_src == TRIG_TIMER) { | |
575 | tmp = cmd->scan_begin_arg; | |
576 | daqp_ns_to_timer(&cmd->scan_begin_arg, | |
577 | cmd->flags & TRIG_ROUND_MASK); | |
578 | if (tmp != cmd->scan_begin_arg) | |
579 | err++; | |
580 | } | |
581 | ||
582 | if (cmd->convert_src == TRIG_TIMER) { | |
583 | tmp = cmd->convert_arg; | |
584 | daqp_ns_to_timer(&cmd->convert_arg, | |
585 | cmd->flags & TRIG_ROUND_MASK); | |
586 | if (tmp != cmd->convert_arg) | |
587 | err++; | |
588 | } | |
589 | ||
590 | if (err) | |
591 | return 4; | |
592 | ||
593 | return 0; | |
594 | } | |
595 | ||
34c43922 | 596 | static int daqp_ai_cmd(struct comedi_device * dev, struct comedi_subdevice * s) |
62ed6662 BB |
597 | { |
598 | local_info_t *local = (local_info_t *) s->private; | |
599 | comedi_cmd *cmd = &s->async->cmd; | |
600 | int counter = 100; | |
601 | int scanlist_start_on_every_entry; | |
602 | int threshold; | |
603 | ||
604 | int i; | |
605 | int v; | |
606 | ||
607 | if (local->stop) { | |
608 | return -EIO; | |
609 | } | |
610 | ||
611 | /* Stop any running conversion */ | |
612 | daqp_ai_cancel(dev, s); | |
613 | ||
614 | outb(0, dev->iobase + DAQP_AUX); | |
615 | ||
616 | /* Reset scan list queue */ | |
617 | outb(DAQP_COMMAND_RSTQ, dev->iobase + DAQP_COMMAND); | |
618 | ||
619 | /* Program pacer clock | |
620 | * | |
621 | * There's two modes we can operate in. If convert_src is | |
622 | * TRIG_TIMER, then convert_arg specifies the time between | |
623 | * each conversion, so we program the pacer clock to that | |
624 | * frequency and set the SCANLIST_START bit on every scanlist | |
625 | * entry. Otherwise, convert_src is TRIG_NOW, which means | |
626 | * we want the fastest possible conversions, scan_begin_src | |
627 | * is TRIG_TIMER, and scan_begin_arg specifies the time between | |
628 | * each scan, so we program the pacer clock to this frequency | |
629 | * and only set the SCANLIST_START bit on the first entry. | |
630 | */ | |
631 | ||
632 | if (cmd->convert_src == TRIG_TIMER) { | |
633 | int counter = daqp_ns_to_timer(&cmd->convert_arg, | |
634 | cmd->flags & TRIG_ROUND_MASK); | |
635 | outb(counter & 0xff, dev->iobase + DAQP_PACER_LOW); | |
636 | outb((counter >> 8) & 0xff, dev->iobase + DAQP_PACER_MID); | |
637 | outb((counter >> 16) & 0xff, dev->iobase + DAQP_PACER_HIGH); | |
638 | scanlist_start_on_every_entry = 1; | |
639 | } else { | |
640 | int counter = daqp_ns_to_timer(&cmd->scan_begin_arg, | |
641 | cmd->flags & TRIG_ROUND_MASK); | |
642 | outb(counter & 0xff, dev->iobase + DAQP_PACER_LOW); | |
643 | outb((counter >> 8) & 0xff, dev->iobase + DAQP_PACER_MID); | |
644 | outb((counter >> 16) & 0xff, dev->iobase + DAQP_PACER_HIGH); | |
645 | scanlist_start_on_every_entry = 0; | |
646 | } | |
647 | ||
648 | /* Program scan list */ | |
649 | ||
650 | for (i = 0; i < cmd->chanlist_len; i++) { | |
651 | ||
652 | int chanspec = cmd->chanlist[i]; | |
653 | ||
654 | /* Program one scan list entry */ | |
655 | ||
656 | v = DAQP_SCANLIST_CHANNEL(CR_CHAN(chanspec)) | |
657 | | DAQP_SCANLIST_GAIN(CR_RANGE(chanspec)); | |
658 | ||
659 | if (CR_AREF(chanspec) == AREF_DIFF) { | |
660 | v |= DAQP_SCANLIST_DIFFERENTIAL; | |
661 | } | |
662 | ||
663 | if (i == 0 || scanlist_start_on_every_entry) { | |
664 | v |= DAQP_SCANLIST_START; | |
665 | } | |
666 | ||
667 | outb(v & 0xff, dev->iobase + DAQP_SCANLIST); | |
668 | outb(v >> 8, dev->iobase + DAQP_SCANLIST); | |
669 | } | |
670 | ||
671 | /* Now it's time to program the FIFO threshold, basically the | |
672 | * number of samples the card will buffer before it interrupts | |
673 | * the CPU. | |
674 | * | |
675 | * If we don't have a stop count, then use half the size of | |
676 | * the FIFO (the manufacturer's recommendation). Consider | |
677 | * that the FIFO can hold 2K samples (4K bytes). With the | |
678 | * threshold set at half the FIFO size, we have a margin of | |
679 | * error of 1024 samples. At the chip's maximum sample rate | |
680 | * of 100,000 Hz, the CPU would have to delay interrupt | |
681 | * service for a full 10 milliseconds in order to lose data | |
682 | * here (as opposed to higher up in the kernel). I've never | |
683 | * seen it happen. However, for slow sample rates it may | |
684 | * buffer too much data and introduce too much delay for the | |
685 | * user application. | |
686 | * | |
687 | * If we have a stop count, then things get more interesting. | |
688 | * If the stop count is less than the FIFO size (actually | |
689 | * three-quarters of the FIFO size - see below), we just use | |
690 | * the stop count itself as the threshold, the card interrupts | |
691 | * us when that many samples have been taken, and we kill the | |
692 | * acquisition at that point and are done. If the stop count | |
693 | * is larger than that, then we divide it by 2 until it's less | |
694 | * than three quarters of the FIFO size (we always leave the | |
695 | * top quarter of the FIFO as protection against sluggish CPU | |
696 | * interrupt response) and use that as the threshold. So, if | |
697 | * the stop count is 4000 samples, we divide by two twice to | |
698 | * get 1000 samples, use that as the threshold, take four | |
699 | * interrupts to get our 4000 samples and are done. | |
700 | * | |
701 | * The algorithm could be more clever. For example, if 81000 | |
702 | * samples are requested, we could set the threshold to 1500 | |
703 | * samples and take 54 interrupts to get 81000. But 54 isn't | |
704 | * a power of two, so this algorithm won't find that option. | |
705 | * Instead, it'll set the threshold at 1266 and take 64 | |
706 | * interrupts to get 81024 samples, of which the last 24 will | |
707 | * be discarded... but we won't get the last interrupt until | |
708 | * they've been collected. To find the first option, the | |
709 | * computer could look at the prime decomposition of the | |
710 | * sample count (81000 = 3^4 * 5^3 * 2^3) and factor it into a | |
711 | * threshold (1500 = 3 * 5^3 * 2^2) and an interrupt count (54 | |
712 | * = 3^3 * 2). Hmmm... a one-line while loop or prime | |
713 | * decomposition of integers... I'll leave it the way it is. | |
714 | * | |
715 | * I'll also note a mini-race condition before ignoring it in | |
716 | * the code. Let's say we're taking 4000 samples, as before. | |
717 | * After 1000 samples, we get an interrupt. But before that | |
718 | * interrupt is completely serviced, another sample is taken | |
719 | * and loaded into the FIFO. Since the interrupt handler | |
720 | * empties the FIFO before returning, it will read 1001 samples. | |
721 | * If that happens four times, we'll end up taking 4004 samples, | |
722 | * not 4000. The interrupt handler will discard the extra four | |
723 | * samples (by halting the acquisition with four samples still | |
724 | * in the FIFO), but we will have to wait for them. | |
725 | * | |
726 | * In short, this code works pretty well, but for either of | |
727 | * the two reasons noted, might end up waiting for a few more | |
728 | * samples than actually requested. Shouldn't make too much | |
729 | * of a difference. | |
730 | */ | |
731 | ||
732 | /* Save away the number of conversions we should perform, and | |
733 | * compute the FIFO threshold (in bytes, not samples - that's | |
734 | * why we multiple local->count by 2 = sizeof(sample)) | |
735 | */ | |
736 | ||
737 | if (cmd->stop_src == TRIG_COUNT) { | |
738 | local->count = cmd->stop_arg * cmd->scan_end_arg; | |
739 | threshold = 2 * local->count; | |
740 | while (threshold > DAQP_FIFO_SIZE * 3 / 4) | |
741 | threshold /= 2; | |
742 | } else { | |
743 | local->count = -1; | |
744 | threshold = DAQP_FIFO_SIZE / 2; | |
745 | } | |
746 | ||
747 | /* Reset data FIFO (see page 28 of DAQP User's Manual) */ | |
748 | ||
749 | outb(DAQP_COMMAND_RSTF, dev->iobase + DAQP_COMMAND); | |
750 | ||
751 | /* Set FIFO threshold. First two bytes are near-empty | |
752 | * threshold, which is unused; next two bytes are near-full | |
753 | * threshold. We computed the number of bytes we want in the | |
754 | * FIFO when the interrupt is generated, what the card wants | |
755 | * is actually the number of available bytes left in the FIFO | |
756 | * when the interrupt is to happen. | |
757 | */ | |
758 | ||
759 | outb(0x00, dev->iobase + DAQP_FIFO); | |
760 | outb(0x00, dev->iobase + DAQP_FIFO); | |
761 | ||
762 | outb((DAQP_FIFO_SIZE - threshold) & 0xff, dev->iobase + DAQP_FIFO); | |
763 | outb((DAQP_FIFO_SIZE - threshold) >> 8, dev->iobase + DAQP_FIFO); | |
764 | ||
765 | /* Set trigger */ | |
766 | ||
767 | v = DAQP_CONTROL_TRIGGER_CONTINUOUS | DAQP_CONTROL_TRIGGER_INTERNAL | |
768 | | DAQP_CONTROL_PACER_5MHz | DAQP_CONTROL_FIFO_INT_ENABLE; | |
769 | ||
770 | outb(v, dev->iobase + DAQP_CONTROL); | |
771 | ||
772 | /* Reset any pending interrupts (my card has a tendancy to require | |
773 | * require multiple reads on the status register to achieve this) | |
774 | */ | |
775 | ||
776 | while (--counter | |
777 | && (inb(dev->iobase + DAQP_STATUS) & DAQP_STATUS_EVENTS)) ; | |
778 | if (!counter) { | |
779 | printk("daqp: couldn't clear interrupts in status register\n"); | |
780 | return -1; | |
781 | } | |
782 | ||
783 | local->interrupt_mode = buffer; | |
784 | local->dev = dev; | |
785 | local->s = s; | |
786 | ||
787 | /* Start conversion */ | |
788 | outb(DAQP_COMMAND_ARM | DAQP_COMMAND_FIFO_DATA, | |
789 | dev->iobase + DAQP_COMMAND); | |
790 | ||
791 | return 0; | |
792 | } | |
793 | ||
794 | /* Single-shot analog output routine */ | |
795 | ||
34c43922 | 796 | static int daqp_ao_insn_write(struct comedi_device * dev, struct comedi_subdevice * s, |
790c5541 | 797 | comedi_insn * insn, unsigned int * data) |
62ed6662 BB |
798 | { |
799 | local_info_t *local = (local_info_t *) s->private; | |
800 | int d; | |
801 | unsigned int chan; | |
802 | ||
803 | if (local->stop) { | |
804 | return -EIO; | |
805 | } | |
806 | ||
807 | chan = CR_CHAN(insn->chanspec); | |
808 | d = data[0]; | |
809 | d &= 0x0fff; | |
810 | d ^= 0x0800; /* Flip the sign */ | |
811 | d |= chan << 12; | |
812 | ||
813 | /* Make sure D/A update mode is direct update */ | |
814 | outb(0, dev->iobase + DAQP_AUX); | |
815 | ||
816 | outw(d, dev->iobase + DAQP_DA); | |
817 | ||
818 | return 1; | |
819 | } | |
820 | ||
821 | /* Digital input routine */ | |
822 | ||
34c43922 | 823 | static int daqp_di_insn_read(struct comedi_device * dev, struct comedi_subdevice * s, |
790c5541 | 824 | comedi_insn * insn, unsigned int * data) |
62ed6662 BB |
825 | { |
826 | local_info_t *local = (local_info_t *) s->private; | |
827 | ||
828 | if (local->stop) { | |
829 | return -EIO; | |
830 | } | |
831 | ||
832 | data[0] = inb(dev->iobase + DAQP_DIGITAL_IO); | |
833 | ||
834 | return 1; | |
835 | } | |
836 | ||
837 | /* Digital output routine */ | |
838 | ||
34c43922 | 839 | static int daqp_do_insn_write(struct comedi_device * dev, struct comedi_subdevice * s, |
790c5541 | 840 | comedi_insn * insn, unsigned int * data) |
62ed6662 BB |
841 | { |
842 | local_info_t *local = (local_info_t *) s->private; | |
843 | ||
844 | if (local->stop) { | |
845 | return -EIO; | |
846 | } | |
847 | ||
848 | outw(data[0] & 0xf, dev->iobase + DAQP_DIGITAL_IO); | |
849 | ||
850 | return 1; | |
851 | } | |
852 | ||
853 | /* daqp_attach is called via comedi_config to attach a comedi device | |
854 | * to a /dev/comedi*. Note that this is different from daqp_cs_attach() | |
855 | * which is called by the pcmcia subsystem to attach the PCMCIA card | |
856 | * when it is inserted. | |
857 | */ | |
858 | ||
71b5f4f1 | 859 | static int daqp_attach(struct comedi_device * dev, comedi_devconfig * it) |
62ed6662 BB |
860 | { |
861 | int ret; | |
862 | local_info_t *local = dev_table[it->options[0]]; | |
863 | tuple_t tuple; | |
864 | int i; | |
34c43922 | 865 | struct comedi_subdevice *s; |
62ed6662 BB |
866 | |
867 | if (it->options[0] < 0 || it->options[0] >= MAX_DEV || !local) { | |
868 | printk("comedi%d: No such daqp device %d\n", | |
869 | dev->minor, it->options[0]); | |
870 | return -EIO; | |
871 | } | |
872 | ||
873 | /* Typically brittle code that I don't completely understand, | |
874 | * but "it works on my card". The intent is to pull the model | |
875 | * number of the card out the PCMCIA CIS and stash it away as | |
876 | * the COMEDI board_name. Looks like the third field in | |
877 | * CISTPL_VERS_1 (offset 2) holds what we're looking for. If | |
878 | * it doesn't work, who cares, just leave it as "DAQP". | |
879 | */ | |
880 | ||
881 | strcpy(local->board_name, "DAQP"); | |
882 | dev->board_name = local->board_name; | |
883 | ||
884 | tuple.DesiredTuple = CISTPL_VERS_1; | |
885 | if (pcmcia_get_first_tuple(local->link, &tuple) == 0) { | |
886 | u_char buf[128]; | |
887 | ||
888 | buf[0] = buf[sizeof(buf) - 1] = 0; | |
889 | tuple.TupleData = buf; | |
890 | tuple.TupleDataMax = sizeof(buf); | |
891 | tuple.TupleOffset = 2; | |
892 | if (pcmcia_get_tuple_data(local->link, &tuple) == 0) { | |
893 | ||
894 | for (i = 0; i < tuple.TupleDataLen - 4; i++) | |
895 | if (buf[i] == 0) | |
896 | break; | |
897 | for (i++; i < tuple.TupleDataLen - 4; i++) | |
898 | if (buf[i] == 0) | |
899 | break; | |
900 | i++; | |
901 | if ((i < tuple.TupleDataLen - 4) | |
902 | && (strncmp(buf + i, "DAQP", 4) == 0)) { | |
903 | strncpy(local->board_name, buf + i, | |
904 | sizeof(local->board_name)); | |
905 | } | |
906 | } | |
907 | } | |
908 | ||
909 | dev->iobase = local->link->io.BasePort1; | |
910 | ||
911 | if ((ret = alloc_subdevices(dev, 4)) < 0) | |
912 | return ret; | |
913 | ||
914 | printk("comedi%d: attaching daqp%d (io 0x%04lx)\n", | |
915 | dev->minor, it->options[0], dev->iobase); | |
916 | ||
917 | s = dev->subdevices + 0; | |
918 | dev->read_subdev = s; | |
919 | s->private = local; | |
920 | s->type = COMEDI_SUBD_AI; | |
921 | s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ; | |
922 | s->n_chan = 8; | |
923 | s->len_chanlist = 2048; | |
924 | s->maxdata = 0xffff; | |
925 | s->range_table = &range_daqp_ai; | |
926 | s->insn_read = daqp_ai_insn_read; | |
927 | s->do_cmdtest = daqp_ai_cmdtest; | |
928 | s->do_cmd = daqp_ai_cmd; | |
929 | s->cancel = daqp_ai_cancel; | |
930 | ||
931 | s = dev->subdevices + 1; | |
932 | dev->write_subdev = s; | |
933 | s->private = local; | |
934 | s->type = COMEDI_SUBD_AO; | |
935 | s->subdev_flags = SDF_WRITEABLE; | |
936 | s->n_chan = 2; | |
937 | s->len_chanlist = 1; | |
938 | s->maxdata = 0x0fff; | |
939 | s->range_table = &range_daqp_ao; | |
940 | s->insn_write = daqp_ao_insn_write; | |
941 | ||
942 | s = dev->subdevices + 2; | |
943 | s->private = local; | |
944 | s->type = COMEDI_SUBD_DI; | |
945 | s->subdev_flags = SDF_READABLE; | |
946 | s->n_chan = 1; | |
947 | s->len_chanlist = 1; | |
948 | s->insn_read = daqp_di_insn_read; | |
949 | ||
950 | s = dev->subdevices + 3; | |
951 | s->private = local; | |
952 | s->type = COMEDI_SUBD_DO; | |
953 | s->subdev_flags = SDF_WRITEABLE; | |
954 | s->n_chan = 1; | |
955 | s->len_chanlist = 1; | |
956 | s->insn_write = daqp_do_insn_write; | |
957 | ||
958 | return 1; | |
959 | } | |
960 | ||
961 | /* daqp_detach (called from comedi_comdig) does nothing. If the PCMCIA | |
962 | * card is removed, daqp_cs_detach() is called by the pcmcia subsystem. | |
963 | */ | |
964 | ||
71b5f4f1 | 965 | static int daqp_detach(struct comedi_device * dev) |
62ed6662 BB |
966 | { |
967 | printk("comedi%d: detaching daqp\n", dev->minor); | |
968 | ||
969 | return 0; | |
970 | } | |
971 | ||
972 | /*==================================================================== | |
973 | ||
974 | PCMCIA interface code | |
975 | ||
976 | The rest of the code in this file is based on dummy_cs.c v1.24 | |
977 | from the Linux pcmcia_cs distribution v3.1.8 and is subject | |
978 | to the following license agreement. | |
979 | ||
980 | The remaining contents of this file are subject to the Mozilla Public | |
981 | License Version 1.1 (the "License"); you may not use this file | |
982 | except in compliance with the License. You may obtain a copy of | |
983 | the License at http://www.mozilla.org/MPL/ | |
984 | ||
985 | Software distributed under the License is distributed on an "AS | |
986 | IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
987 | implied. See the License for the specific language governing | |
988 | rights and limitations under the License. | |
989 | ||
990 | The initial developer of the original code is David A. Hinds | |
991 | <dhinds@pcmcia.sourceforge.org>. Portions created by David A. Hinds | |
992 | are Copyright (C) 1999 David A. Hinds. All Rights Reserved. | |
993 | ||
994 | Alternatively, the contents of this file may be used under the | |
995 | terms of the GNU Public License version 2 (the "GPL"), in which | |
996 | case the provisions of the GPL are applicable instead of the | |
997 | above. If you wish to allow the use of your version of this file | |
998 | only under the terms of the GPL and not to allow others to use | |
999 | your version of this file under the MPL, indicate your decision | |
1000 | by deleting the provisions above and replace them with the notice | |
1001 | and other provisions required by the GPL. If you do not delete | |
1002 | the provisions above, a recipient may use your version of this | |
1003 | file under either the MPL or the GPL. | |
1004 | ||
1005 | ======================================================================*/ | |
1006 | ||
1007 | /* | |
1008 | The event() function is this driver's Card Services event handler. | |
1009 | It will be called by Card Services when an appropriate card status | |
1010 | event is received. The config() and release() entry points are | |
1011 | used to configure or release a socket, in response to card | |
1012 | insertion and ejection events. | |
1013 | ||
1014 | Kernel version 2.6.16 upwards uses suspend() and resume() functions | |
1015 | instead of an event() function. | |
1016 | */ | |
1017 | ||
1018 | static void daqp_cs_config(struct pcmcia_device *link); | |
1019 | static void daqp_cs_release(struct pcmcia_device *link); | |
1020 | static int daqp_cs_suspend(struct pcmcia_device *p_dev); | |
1021 | static int daqp_cs_resume(struct pcmcia_device *p_dev); | |
1022 | ||
1023 | /* | |
1024 | The attach() and detach() entry points are used to create and destroy | |
1025 | "instances" of the driver, where each instance represents everything | |
1026 | needed to manage one actual PCMCIA card. | |
1027 | */ | |
1028 | ||
1029 | static int daqp_cs_attach(struct pcmcia_device *); | |
1030 | static void daqp_cs_detach(struct pcmcia_device *); | |
1031 | ||
1032 | /* | |
1033 | The dev_info variable is the "key" that is used to match up this | |
1034 | device driver with appropriate cards, through the card configuration | |
1035 | database. | |
1036 | */ | |
1037 | ||
1038 | static const dev_info_t dev_info = "quatech_daqp_cs"; | |
1039 | ||
1040 | /*====================================================================== | |
1041 | ||
1042 | daqp_cs_attach() creates an "instance" of the driver, allocating | |
1043 | local data structures for one device. The device is registered | |
1044 | with Card Services. | |
1045 | ||
1046 | The dev_link structure is initialized, but we don't actually | |
1047 | configure the card at this point -- we wait until we receive a | |
1048 | card insertion event. | |
1049 | ||
1050 | ======================================================================*/ | |
1051 | ||
1052 | static int daqp_cs_attach(struct pcmcia_device *link) | |
1053 | { | |
1054 | local_info_t *local; | |
1055 | int i; | |
1056 | ||
1057 | DEBUG(0, "daqp_cs_attach()\n"); | |
1058 | ||
1059 | for (i = 0; i < MAX_DEV; i++) | |
1060 | if (dev_table[i] == NULL) | |
1061 | break; | |
1062 | if (i == MAX_DEV) { | |
1063 | printk(KERN_NOTICE "daqp_cs: no devices available\n"); | |
1064 | return -ENODEV; | |
1065 | } | |
1066 | ||
1067 | /* Allocate space for private device-specific data */ | |
1068 | local = kzalloc(sizeof(local_info_t), GFP_KERNEL); | |
1069 | if (!local) | |
1070 | return -ENOMEM; | |
1071 | ||
1072 | local->table_index = i; | |
1073 | dev_table[i] = local; | |
1074 | local->link = link; | |
1075 | link->priv = local; | |
1076 | ||
1077 | /* Interrupt setup */ | |
1078 | link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT; | |
1079 | link->irq.IRQInfo1 = IRQ_LEVEL_ID; | |
1080 | link->irq.Handler = daqp_interrupt; | |
1081 | link->irq.Instance = local; | |
1082 | ||
1083 | /* | |
1084 | General socket configuration defaults can go here. In this | |
1085 | client, we assume very little, and rely on the CIS for almost | |
1086 | everything. In most clients, many details (i.e., number, sizes, | |
1087 | and attributes of IO windows) are fixed by the nature of the | |
1088 | device, and can be hard-wired here. | |
1089 | */ | |
1090 | link->conf.Attributes = 0; | |
1091 | link->conf.IntType = INT_MEMORY_AND_IO; | |
1092 | ||
1093 | daqp_cs_config(link); | |
1094 | ||
1095 | return 0; | |
1096 | } /* daqp_cs_attach */ | |
1097 | ||
1098 | /*====================================================================== | |
1099 | ||
1100 | This deletes a driver "instance". The device is de-registered | |
1101 | with Card Services. If it has been released, all local data | |
1102 | structures are freed. Otherwise, the structures will be freed | |
1103 | when the device is released. | |
1104 | ||
1105 | ======================================================================*/ | |
1106 | ||
1107 | static void daqp_cs_detach(struct pcmcia_device *link) | |
1108 | { | |
1109 | local_info_t *dev = link->priv; | |
1110 | ||
1111 | DEBUG(0, "daqp_cs_detach(0x%p)\n", link); | |
1112 | ||
1113 | if (link->dev_node) { | |
1114 | dev->stop = 1; | |
1115 | daqp_cs_release(link); | |
1116 | } | |
1117 | ||
1118 | /* Unlink device structure, and free it */ | |
1119 | dev_table[dev->table_index] = NULL; | |
1120 | if (dev) | |
1121 | kfree(dev); | |
1122 | ||
1123 | } /* daqp_cs_detach */ | |
1124 | ||
1125 | /*====================================================================== | |
1126 | ||
1127 | daqp_cs_config() is scheduled to run after a CARD_INSERTION event | |
1128 | is received, to configure the PCMCIA socket, and to make the | |
1129 | device available to the system. | |
1130 | ||
1131 | ======================================================================*/ | |
1132 | ||
1133 | static void daqp_cs_config(struct pcmcia_device *link) | |
1134 | { | |
1135 | local_info_t *dev = link->priv; | |
1136 | tuple_t tuple; | |
1137 | cisparse_t parse; | |
1138 | int last_ret; | |
1139 | u_char buf[64]; | |
1140 | ||
1141 | DEBUG(0, "daqp_cs_config(0x%p)\n", link); | |
1142 | ||
1143 | /* | |
1144 | This reads the card's CONFIG tuple to find its configuration | |
1145 | registers. | |
1146 | */ | |
1147 | tuple.DesiredTuple = CISTPL_CONFIG; | |
1148 | tuple.Attributes = 0; | |
1149 | tuple.TupleData = buf; | |
1150 | tuple.TupleDataMax = sizeof(buf); | |
1151 | tuple.TupleOffset = 0; | |
1152 | if ((last_ret = pcmcia_get_first_tuple(link, &tuple))) { | |
1153 | cs_error(link, GetFirstTuple, last_ret); | |
1154 | goto cs_failed; | |
1155 | } | |
1156 | if ((last_ret = pcmcia_get_tuple_data(link, &tuple))) { | |
1157 | cs_error(link, GetTupleData, last_ret); | |
1158 | goto cs_failed; | |
1159 | } | |
1160 | if ((last_ret = pcmcia_parse_tuple(&tuple, &parse))) { | |
1161 | cs_error(link, ParseTuple, last_ret); | |
1162 | goto cs_failed; | |
1163 | } | |
1164 | link->conf.ConfigBase = parse.config.base; | |
1165 | link->conf.Present = parse.config.rmask[0]; | |
1166 | ||
1167 | /* | |
1168 | In this loop, we scan the CIS for configuration table entries, | |
1169 | each of which describes a valid card configuration, including | |
1170 | voltage, IO window, memory window, and interrupt settings. | |
1171 | ||
1172 | We make no assumptions about the card to be configured: we use | |
1173 | just the information available in the CIS. In an ideal world, | |
1174 | this would work for any PCMCIA card, but it requires a complete | |
1175 | and accurate CIS. In practice, a driver usually "knows" most of | |
1176 | these things without consulting the CIS, and most client drivers | |
1177 | will only use the CIS to fill in implementation-defined details. | |
1178 | */ | |
1179 | tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; | |
1180 | if ((last_ret = pcmcia_get_first_tuple(link, &tuple))) { | |
1181 | cs_error(link, GetFirstTuple, last_ret); | |
1182 | goto cs_failed; | |
1183 | } | |
1184 | while (1) { | |
1185 | cistpl_cftable_entry_t dflt = { 0 }; | |
1186 | cistpl_cftable_entry_t *cfg = &(parse.cftable_entry); | |
1187 | if (pcmcia_get_tuple_data(link, &tuple)) | |
1188 | goto next_entry; | |
1189 | if (pcmcia_parse_tuple(&tuple, &parse)) | |
1190 | goto next_entry; | |
1191 | ||
1192 | if (cfg->flags & CISTPL_CFTABLE_DEFAULT) | |
1193 | dflt = *cfg; | |
1194 | if (cfg->index == 0) | |
1195 | goto next_entry; | |
1196 | link->conf.ConfigIndex = cfg->index; | |
1197 | ||
1198 | /* Do we need to allocate an interrupt? */ | |
1199 | if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1) | |
1200 | link->conf.Attributes |= CONF_ENABLE_IRQ; | |
1201 | ||
1202 | /* IO window settings */ | |
1203 | link->io.NumPorts1 = link->io.NumPorts2 = 0; | |
1204 | if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) { | |
1205 | cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io; | |
1206 | link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO; | |
1207 | if (!(io->flags & CISTPL_IO_8BIT)) | |
1208 | link->io.Attributes1 = IO_DATA_PATH_WIDTH_16; | |
1209 | if (!(io->flags & CISTPL_IO_16BIT)) | |
1210 | link->io.Attributes1 = IO_DATA_PATH_WIDTH_8; | |
1211 | link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK; | |
1212 | link->io.BasePort1 = io->win[0].base; | |
1213 | link->io.NumPorts1 = io->win[0].len; | |
1214 | if (io->nwin > 1) { | |
1215 | link->io.Attributes2 = link->io.Attributes1; | |
1216 | link->io.BasePort2 = io->win[1].base; | |
1217 | link->io.NumPorts2 = io->win[1].len; | |
1218 | } | |
1219 | } | |
1220 | ||
1221 | /* This reserves IO space but doesn't actually enable it */ | |
1222 | if (pcmcia_request_io(link, &link->io)) | |
1223 | goto next_entry; | |
1224 | ||
1225 | /* If we got this far, we're cool! */ | |
1226 | break; | |
1227 | ||
1228 | next_entry: | |
1229 | if ((last_ret = pcmcia_get_next_tuple(link, &tuple))) { | |
1230 | cs_error(link, GetNextTuple, last_ret); | |
1231 | goto cs_failed; | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | /* | |
1236 | Allocate an interrupt line. Note that this does not assign a | |
1237 | handler to the interrupt, unless the 'Handler' member of the | |
1238 | irq structure is initialized. | |
1239 | */ | |
1240 | if (link->conf.Attributes & CONF_ENABLE_IRQ) | |
1241 | if ((last_ret = pcmcia_request_irq(link, &link->irq))) { | |
1242 | cs_error(link, RequestIRQ, last_ret); | |
1243 | goto cs_failed; | |
1244 | } | |
1245 | ||
1246 | /* | |
1247 | This actually configures the PCMCIA socket -- setting up | |
1248 | the I/O windows and the interrupt mapping, and putting the | |
1249 | card and host interface into "Memory and IO" mode. | |
1250 | */ | |
1251 | if ((last_ret = pcmcia_request_configuration(link, &link->conf))) { | |
1252 | cs_error(link, RequestConfiguration, last_ret); | |
1253 | goto cs_failed; | |
1254 | } | |
1255 | ||
1256 | /* | |
1257 | At this point, the dev_node_t structure(s) need to be | |
1258 | initialized and arranged in a linked list at link->dev. | |
1259 | */ | |
1260 | /* Comedi's PCMCIA script uses this device name (extracted | |
1261 | * from /var/lib/pcmcia/stab) to pass to comedi_config | |
1262 | */ | |
1263 | /* sprintf(dev->node.dev_name, "daqp%d", dev->table_index); */ | |
1264 | sprintf(dev->node.dev_name, "quatech_daqp_cs"); | |
1265 | dev->node.major = dev->node.minor = 0; | |
1266 | link->dev_node = &dev->node; | |
1267 | ||
1268 | /* Finally, report what we've done */ | |
1269 | printk(KERN_INFO "%s: index 0x%02x", | |
1270 | dev->node.dev_name, link->conf.ConfigIndex); | |
1271 | if (link->conf.Attributes & CONF_ENABLE_IRQ) | |
1272 | printk(", irq %u", link->irq.AssignedIRQ); | |
1273 | if (link->io.NumPorts1) | |
1274 | printk(", io 0x%04x-0x%04x", link->io.BasePort1, | |
1275 | link->io.BasePort1 + link->io.NumPorts1 - 1); | |
1276 | if (link->io.NumPorts2) | |
1277 | printk(" & 0x%04x-0x%04x", link->io.BasePort2, | |
1278 | link->io.BasePort2 + link->io.NumPorts2 - 1); | |
1279 | printk("\n"); | |
1280 | ||
1281 | return; | |
1282 | ||
1283 | cs_failed: | |
1284 | daqp_cs_release(link); | |
1285 | ||
1286 | } /* daqp_cs_config */ | |
1287 | ||
1288 | static void daqp_cs_release(struct pcmcia_device *link) | |
1289 | { | |
1290 | DEBUG(0, "daqp_cs_release(0x%p)\n", link); | |
1291 | ||
1292 | pcmcia_disable_device(link); | |
1293 | } /* daqp_cs_release */ | |
1294 | ||
1295 | /*====================================================================== | |
1296 | ||
1297 | The card status event handler. Mostly, this schedules other | |
1298 | stuff to run after an event is received. | |
1299 | ||
1300 | When a CARD_REMOVAL event is received, we immediately set a | |
1301 | private flag to block future accesses to this device. All the | |
1302 | functions that actually access the device should check this flag | |
1303 | to make sure the card is still present. | |
1304 | ||
1305 | ======================================================================*/ | |
1306 | ||
1307 | static int daqp_cs_suspend(struct pcmcia_device *link) | |
1308 | { | |
1309 | local_info_t *local = link->priv; | |
1310 | ||
1311 | /* Mark the device as stopped, to block IO until later */ | |
1312 | local->stop = 1; | |
1313 | return 0; | |
1314 | } | |
1315 | ||
1316 | static int daqp_cs_resume(struct pcmcia_device *link) | |
1317 | { | |
1318 | local_info_t *local = link->priv; | |
1319 | ||
1320 | local->stop = 0; | |
1321 | ||
1322 | return 0; | |
1323 | } | |
1324 | ||
1325 | /*====================================================================*/ | |
1326 | ||
1327 | #ifdef MODULE | |
1328 | ||
1329 | static struct pcmcia_device_id daqp_cs_id_table[] = { | |
1330 | PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0027), | |
1331 | PCMCIA_DEVICE_NULL | |
1332 | }; | |
1333 | ||
1334 | MODULE_DEVICE_TABLE(pcmcia, daqp_cs_id_table); | |
1335 | ||
1336 | struct pcmcia_driver daqp_cs_driver = { | |
1337 | .probe = daqp_cs_attach, | |
1338 | .remove = daqp_cs_detach, | |
1339 | .suspend = daqp_cs_suspend, | |
1340 | .resume = daqp_cs_resume, | |
1341 | .id_table = daqp_cs_id_table, | |
1342 | .owner = THIS_MODULE, | |
1343 | .drv = { | |
1344 | .name = dev_info, | |
1345 | }, | |
1346 | }; | |
1347 | ||
1348 | int __init init_module(void) | |
1349 | { | |
1350 | DEBUG(0, "%s\n", version); | |
1351 | pcmcia_register_driver(&daqp_cs_driver); | |
1352 | comedi_driver_register(&driver_daqp); | |
1353 | return 0; | |
1354 | } | |
1355 | ||
1356 | void __exit cleanup_module(void) | |
1357 | { | |
1358 | DEBUG(0, "daqp_cs: unloading\n"); | |
1359 | comedi_driver_unregister(&driver_daqp); | |
1360 | pcmcia_unregister_driver(&daqp_cs_driver); | |
1361 | } | |
1362 | ||
1363 | #endif |