1 /* Device driver for Meilhaus ME-4000 board family.
2 * ================================================
4 * Copyright (C) 2003 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Author: Guenter Gebhardt <g.gebhardt@meilhaus.de>
23 #include <linux/module.h>
25 #include <linux/sched.h>
26 #include <linux/interrupt.h>
27 #include <linux/pci.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <linux/errno.h>
32 #include <linux/delay.h>
35 #include <linux/unistd.h>
36 #include <linux/list.h>
37 #include <linux/proc_fs.h>
39 #include <linux/poll.h>
40 #include <linux/vmalloc.h>
41 #include <asm/pgtable.h>
42 #include <asm/uaccess.h>
43 #include <linux/types.h>
45 #include <linux/slab.h>
47 /* Include-File for the Meilhaus ME-4000 I/O board */
49 #include "me4000_firmware.h"
50 #include "me4610_firmware.h"
52 /* Administrative stuff for modinfo */
53 MODULE_AUTHOR("Guenter Gebhardt <g.gebhardt@meilhaus.de>");
55 ("Device Driver Module for Meilhaus ME-4000 boards version 1.0.5");
56 MODULE_SUPPORTED_DEVICE("Meilhaus ME-4000 Multi I/O boards");
57 MODULE_LICENSE("GPL");
59 /* Board specific data are kept in a global list */
60 LIST_HEAD(me4000_board_info_list);
62 /* Major Device Numbers. 0 means to get it automatically from the System */
63 static int me4000_ao_major_driver_no = 0;
64 static int me4000_ai_major_driver_no = 0;
65 static int me4000_dio_major_driver_no = 0;
66 static int me4000_cnt_major_driver_no = 0;
67 static int me4000_ext_int_major_driver_no = 0;
69 /* Let the user specify a custom major driver number */
70 module_param(me4000_ao_major_driver_no, int, 0);
71 MODULE_PARM_DESC(me4000_ao_major_driver_no,
72 "Major driver number for analog output (default 0)");
74 module_param(me4000_ai_major_driver_no, int, 0);
75 MODULE_PARM_DESC(me4000_ai_major_driver_no,
76 "Major driver number for analog input (default 0)");
78 module_param(me4000_dio_major_driver_no, int, 0);
79 MODULE_PARM_DESC(me4000_dio_major_driver_no,
80 "Major driver number digital I/O (default 0)");
82 module_param(me4000_cnt_major_driver_no, int, 0);
83 MODULE_PARM_DESC(me4000_cnt_major_driver_no,
84 "Major driver number for counter (default 0)");
86 module_param(me4000_ext_int_major_driver_no, int, 0);
87 MODULE_PARM_DESC(me4000_ext_int_major_driver_no,
88 "Major driver number for external interrupt (default 0)");
90 /*-----------------------------------------------------------------------------
92 ---------------------------------------------------------------------------*/
93 int init_module(void);
94 void cleanup_module(void);
96 /*-----------------------------------------------------------------------------
97 Board detection and initialization
98 ---------------------------------------------------------------------------*/
99 static int me4000_probe(struct pci_dev *dev, const struct pci_device_id *id);
100 static int me4000_xilinx_download(me4000_info_t *);
101 static int me4000_reset_board(me4000_info_t *);
103 static void clear_board_info_list(void);
104 static int get_registers(struct pci_dev *dev, me4000_info_t * info);
105 static int init_board_info(struct pci_dev *dev, me4000_info_t * board_info);
106 static int alloc_ao_contexts(me4000_info_t * info);
107 static void release_ao_contexts(me4000_info_t * board_info);
108 static int alloc_ai_context(me4000_info_t * info);
109 static int alloc_dio_context(me4000_info_t * info);
110 static int alloc_cnt_context(me4000_info_t * info);
111 static int alloc_ext_int_context(me4000_info_t * info);
113 /*-----------------------------------------------------------------------------
114 Stuff used by all device parts
115 ---------------------------------------------------------------------------*/
116 static int me4000_open(struct inode *, struct file *);
117 static int me4000_release(struct inode *, struct file *);
119 static int me4000_get_user_info(me4000_user_info_t *,
120 me4000_info_t * board_info);
121 static int me4000_read_procmem(char *, char **, off_t, int, int *, void *);
123 /*-----------------------------------------------------------------------------
125 ---------------------------------------------------------------------------*/
126 static ssize_t me4000_ao_write_sing(struct file *, const char *, size_t,
128 static ssize_t me4000_ao_write_wrap(struct file *, const char *, size_t,
130 static ssize_t me4000_ao_write_cont(struct file *, const char *, size_t,
133 static int me4000_ao_ioctl_sing(struct inode *, struct file *, unsigned int,
135 static int me4000_ao_ioctl_wrap(struct inode *, struct file *, unsigned int,
137 static int me4000_ao_ioctl_cont(struct inode *, struct file *, unsigned int,
140 static unsigned int me4000_ao_poll_cont(struct file *, poll_table *);
141 static int me4000_ao_fsync_cont(struct file *, struct dentry *, int);
143 static int me4000_ao_start(unsigned long *, me4000_ao_context_t *);
144 static int me4000_ao_stop(me4000_ao_context_t *);
145 static int me4000_ao_immediate_stop(me4000_ao_context_t *);
146 static int me4000_ao_timer_set_divisor(u32 *, me4000_ao_context_t *);
147 static int me4000_ao_preload(me4000_ao_context_t *);
148 static int me4000_ao_preload_update(me4000_ao_context_t *);
149 static int me4000_ao_ex_trig_set_edge(int *, me4000_ao_context_t *);
150 static int me4000_ao_ex_trig_enable(me4000_ao_context_t *);
151 static int me4000_ao_ex_trig_disable(me4000_ao_context_t *);
152 static int me4000_ao_prepare(me4000_ao_context_t * ao_info);
153 static int me4000_ao_reset(me4000_ao_context_t * ao_info);
154 static int me4000_ao_enable_do(me4000_ao_context_t *);
155 static int me4000_ao_disable_do(me4000_ao_context_t *);
156 static int me4000_ao_fsm_state(int *, me4000_ao_context_t *);
158 static int me4000_ao_simultaneous_ex_trig(me4000_ao_context_t * ao_context);
159 static int me4000_ao_simultaneous_sw(me4000_ao_context_t * ao_context);
160 static int me4000_ao_simultaneous_disable(me4000_ao_context_t * ao_context);
161 static int me4000_ao_simultaneous_update(me4000_ao_channel_list_t * channels,
162 me4000_ao_context_t * ao_context);
164 static int me4000_ao_synchronous_ex_trig(me4000_ao_context_t * ao_context);
165 static int me4000_ao_synchronous_sw(me4000_ao_context_t * ao_context);
166 static int me4000_ao_synchronous_disable(me4000_ao_context_t * ao_context);
168 static int me4000_ao_ex_trig_timeout(unsigned long *arg,
169 me4000_ao_context_t * ao_context);
170 static int me4000_ao_get_free_buffer(unsigned long *arg,
171 me4000_ao_context_t * ao_context);
173 /*-----------------------------------------------------------------------------
175 ---------------------------------------------------------------------------*/
176 static int me4000_ai_single(me4000_ai_single_t *, me4000_ai_context_t *);
177 static int me4000_ai_ioctl_sing(struct inode *, struct file *, unsigned int,
180 static ssize_t me4000_ai_read(struct file *, char *, size_t, loff_t *);
181 static int me4000_ai_ioctl_sw(struct inode *, struct file *, unsigned int,
183 static unsigned int me4000_ai_poll(struct file *, poll_table *);
184 static int me4000_ai_fasync(int fd, struct file *file_p, int mode);
186 static int me4000_ai_ioctl_ext(struct inode *, struct file *, unsigned int,
189 static int me4000_ai_prepare(me4000_ai_context_t * ai_context);
190 static int me4000_ai_reset(me4000_ai_context_t * ai_context);
191 static int me4000_ai_config(me4000_ai_config_t *, me4000_ai_context_t *);
192 static int me4000_ai_start(me4000_ai_context_t *);
193 static int me4000_ai_start_ex(unsigned long *, me4000_ai_context_t *);
194 static int me4000_ai_stop(me4000_ai_context_t *);
195 static int me4000_ai_immediate_stop(me4000_ai_context_t *);
196 static int me4000_ai_ex_trig_enable(me4000_ai_context_t *);
197 static int me4000_ai_ex_trig_disable(me4000_ai_context_t *);
198 static int me4000_ai_ex_trig_setup(me4000_ai_trigger_t *,
199 me4000_ai_context_t *);
200 static int me4000_ai_sc_setup(me4000_ai_sc_t * arg,
201 me4000_ai_context_t * ai_context);
202 static int me4000_ai_offset_enable(me4000_ai_context_t * ai_context);
203 static int me4000_ai_offset_disable(me4000_ai_context_t * ai_context);
204 static int me4000_ai_fullscale_enable(me4000_ai_context_t * ai_context);
205 static int me4000_ai_fullscale_disable(me4000_ai_context_t * ai_context);
206 static int me4000_ai_fsm_state(int *arg, me4000_ai_context_t * ai_context);
207 static int me4000_ai_get_count_buffer(unsigned long *arg,
208 me4000_ai_context_t * ai_context);
210 /*-----------------------------------------------------------------------------
212 ---------------------------------------------------------------------------*/
213 static int me4000_eeprom_read(me4000_eeprom_t * arg,
214 me4000_ai_context_t * ai_context);
215 static int me4000_eeprom_write(me4000_eeprom_t * arg,
216 me4000_ai_context_t * ai_context);
217 static unsigned short eeprom_read_cmd(me4000_ai_context_t * ai_context,
218 unsigned long cmd, int length);
219 static int eeprom_write_cmd(me4000_ai_context_t * ai_context, unsigned long cmd,
222 /*-----------------------------------------------------------------------------
224 ---------------------------------------------------------------------------*/
225 static int me4000_dio_ioctl(struct inode *, struct file *, unsigned int,
227 static int me4000_dio_config(me4000_dio_config_t *, me4000_dio_context_t *);
228 static int me4000_dio_get_byte(me4000_dio_byte_t *, me4000_dio_context_t *);
229 static int me4000_dio_set_byte(me4000_dio_byte_t *, me4000_dio_context_t *);
230 static int me4000_dio_reset(me4000_dio_context_t *);
232 /*-----------------------------------------------------------------------------
234 ---------------------------------------------------------------------------*/
235 static int me4000_cnt_ioctl(struct inode *, struct file *, unsigned int,
237 static int me4000_cnt_config(me4000_cnt_config_t *, me4000_cnt_context_t *);
238 static int me4000_cnt_read(me4000_cnt_t *, me4000_cnt_context_t *);
239 static int me4000_cnt_write(me4000_cnt_t *, me4000_cnt_context_t *);
240 static int me4000_cnt_reset(me4000_cnt_context_t *);
242 /*-----------------------------------------------------------------------------
243 External interrupt routines
244 ---------------------------------------------------------------------------*/
245 static int me4000_ext_int_ioctl(struct inode *, struct file *, unsigned int,
247 static int me4000_ext_int_enable(me4000_ext_int_context_t *);
248 static int me4000_ext_int_disable(me4000_ext_int_context_t *);
249 static int me4000_ext_int_count(unsigned long *arg,
250 me4000_ext_int_context_t * ext_int_context);
251 static int me4000_ext_int_fasync(int fd, struct file *file_ptr, int mode);
253 /*-----------------------------------------------------------------------------
254 The interrupt service routines
255 ---------------------------------------------------------------------------*/
256 static irqreturn_t me4000_ao_isr(int, void *);
257 static irqreturn_t me4000_ai_isr(int, void *);
258 static irqreturn_t me4000_ext_int_isr(int, void *);
260 /*-----------------------------------------------------------------------------
262 ---------------------------------------------------------------------------*/
263 static int inline me4000_buf_count(me4000_circ_buf_t, int);
264 static int inline me4000_buf_space(me4000_circ_buf_t, int);
265 static int inline me4000_space_to_end(me4000_circ_buf_t, int);
266 static int inline me4000_values_to_end(me4000_circ_buf_t, int);
268 static void inline me4000_outb(unsigned char value, unsigned long port);
269 static void inline me4000_outl(unsigned long value, unsigned long port);
270 static unsigned long inline me4000_inl(unsigned long port);
271 static unsigned char inline me4000_inb(unsigned long port);
273 static int me4000_buf_count(me4000_circ_buf_t buf, int size)
275 return ((buf.head - buf.tail) & (size - 1));
278 static int me4000_buf_space(me4000_circ_buf_t buf, int size)
280 return ((buf.tail - (buf.head + 1)) & (size - 1));
283 static int me4000_values_to_end(me4000_circ_buf_t buf, int size)
287 end = size - buf.tail;
288 n = (buf.head + end) & (size - 1);
289 return (n < end) ? n : end;
292 static int me4000_space_to_end(me4000_circ_buf_t buf, int size)
297 end = size - 1 - buf.head;
298 n = (end + buf.tail) & (size - 1);
299 return (n <= end) ? n : (end + 1);
302 static void me4000_outb(unsigned char value, unsigned long port)
304 PORT_PDEBUG("--> 0x%02X port 0x%04lX\n", value, port);
308 static void me4000_outl(unsigned long value, unsigned long port)
310 PORT_PDEBUG("--> 0x%08lX port 0x%04lX\n", value, port);
314 static unsigned long me4000_inl(unsigned long port)
318 PORT_PDEBUG("<-- 0x%08lX port 0x%04lX\n", value, port);
322 static unsigned char me4000_inb(unsigned long port)
326 PORT_PDEBUG("<-- 0x%08X port 0x%04lX\n", value, port);
330 struct pci_driver me4000_driver = {
332 .id_table = me4000_pci_table,
333 .probe = me4000_probe
336 static struct file_operations me4000_ao_fops_sing = {
338 write:me4000_ao_write_sing,
339 ioctl:me4000_ao_ioctl_sing,
341 release:me4000_release,
344 static struct file_operations me4000_ao_fops_wrap = {
346 write:me4000_ao_write_wrap,
347 ioctl:me4000_ao_ioctl_wrap,
349 release:me4000_release,
352 static struct file_operations me4000_ao_fops_cont = {
354 write:me4000_ao_write_cont,
355 poll:me4000_ao_poll_cont,
356 ioctl:me4000_ao_ioctl_cont,
358 release:me4000_release,
359 fsync:me4000_ao_fsync_cont,
362 static struct file_operations me4000_ai_fops_sing = {
364 ioctl:me4000_ai_ioctl_sing,
366 release:me4000_release,
369 static struct file_operations me4000_ai_fops_cont_sw = {
373 ioctl:me4000_ai_ioctl_sw,
375 release:me4000_release,
376 fasync:me4000_ai_fasync,
379 static struct file_operations me4000_ai_fops_cont_et = {
383 ioctl:me4000_ai_ioctl_ext,
385 release:me4000_release,
388 static struct file_operations me4000_ai_fops_cont_et_value = {
392 ioctl:me4000_ai_ioctl_ext,
394 release:me4000_release,
397 static struct file_operations me4000_ai_fops_cont_et_chanlist = {
401 ioctl:me4000_ai_ioctl_ext,
403 release:me4000_release,
406 static struct file_operations me4000_dio_fops = {
408 ioctl:me4000_dio_ioctl,
410 release:me4000_release,
413 static struct file_operations me4000_cnt_fops = {
415 ioctl:me4000_cnt_ioctl,
417 release:me4000_release,
420 static struct file_operations me4000_ext_int_fops = {
422 ioctl:me4000_ext_int_ioctl,
424 release:me4000_release,
425 fasync:me4000_ext_int_fasync,
428 static struct file_operations *me4000_ao_fops_array[] = {
429 &me4000_ao_fops_sing, // single operations
430 &me4000_ao_fops_wrap, // wraparound operations
431 &me4000_ao_fops_cont, // continous operations
434 static struct file_operations *me4000_ai_fops_array[] = {
435 &me4000_ai_fops_sing, // single operations
436 &me4000_ai_fops_cont_sw, // continuous operations with software start
437 &me4000_ai_fops_cont_et, // continous operations with external trigger
438 &me4000_ai_fops_cont_et_value, // sample values by external trigger
439 &me4000_ai_fops_cont_et_chanlist, // work through one channel list by external trigger
442 int __init me4000_init_module(void)
446 CALL_PDEBUG("init_module() is executed\n");
448 /* Register driver capabilities */
449 result = pci_register_driver(&me4000_driver);
450 PDEBUG("init_module():%d devices detected\n", result);
452 printk(KERN_ERR "ME4000:init_module():Can't register driver\n");
456 /* Allocate major number for analog output */
458 register_chrdev(me4000_ao_major_driver_no, ME4000_AO_NAME,
459 &me4000_ao_fops_sing);
461 printk(KERN_ERR "ME4000:init_module():Can't get AO major no\n");
464 me4000_ao_major_driver_no = result;
466 PDEBUG("init_module():Major driver number for AO = %ld\n",
467 me4000_ao_major_driver_no);
469 /* Allocate major number for analog input */
471 register_chrdev(me4000_ai_major_driver_no, ME4000_AI_NAME,
472 &me4000_ai_fops_sing);
474 printk(KERN_ERR "ME4000:init_module():Can't get AI major no\n");
477 me4000_ai_major_driver_no = result;
479 PDEBUG("init_module():Major driver number for AI = %ld\n",
480 me4000_ai_major_driver_no);
482 /* Allocate major number for digital I/O */
484 register_chrdev(me4000_dio_major_driver_no, ME4000_DIO_NAME,
488 "ME4000:init_module():Can't get DIO major no\n");
491 me4000_dio_major_driver_no = result;
493 PDEBUG("init_module():Major driver number for DIO = %ld\n",
494 me4000_dio_major_driver_no);
496 /* Allocate major number for counter */
498 register_chrdev(me4000_cnt_major_driver_no, ME4000_CNT_NAME,
502 "ME4000:init_module():Can't get CNT major no\n");
505 me4000_cnt_major_driver_no = result;
507 PDEBUG("init_module():Major driver number for CNT = %ld\n",
508 me4000_cnt_major_driver_no);
510 /* Allocate major number for external interrupt */
512 register_chrdev(me4000_ext_int_major_driver_no, ME4000_EXT_INT_NAME,
513 &me4000_ext_int_fops);
516 "ME4000:init_module():Can't get major no for external interrupt\n");
519 me4000_ext_int_major_driver_no = result;
522 ("init_module():Major driver number for external interrupt = %ld\n",
523 me4000_ext_int_major_driver_no);
525 /* Create the /proc/me4000 entry */
526 if (!create_proc_read_entry
527 ("me4000", 0, NULL, me4000_read_procmem, NULL)) {
530 "ME4000:init_module():Can't create proc entry\n");
537 unregister_chrdev(me4000_ext_int_major_driver_no, ME4000_EXT_INT_NAME);
540 unregister_chrdev(me4000_cnt_major_driver_no, ME4000_CNT_NAME);
543 unregister_chrdev(me4000_dio_major_driver_no, ME4000_DIO_NAME);
546 unregister_chrdev(me4000_ai_major_driver_no, ME4000_AI_NAME);
549 unregister_chrdev(me4000_ao_major_driver_no, ME4000_AO_NAME);
552 pci_unregister_driver(&me4000_driver);
553 clear_board_info_list();
559 module_init(me4000_init_module);
561 static void clear_board_info_list(void)
563 struct list_head *board_p;
564 struct list_head *dac_p;
565 me4000_info_t *board_info;
566 me4000_ao_context_t *ao_context;
568 /* Clear context lists */
569 for (board_p = me4000_board_info_list.next;
570 board_p != &me4000_board_info_list; board_p = board_p->next) {
571 board_info = list_entry(board_p, me4000_info_t, list);
572 /* Clear analog output context list */
573 while (!list_empty(&board_info->ao_context_list)) {
574 dac_p = board_info->ao_context_list.next;
576 list_entry(dac_p, me4000_ao_context_t, list);
577 me4000_ao_reset(ao_context);
578 free_irq(ao_context->irq, ao_context);
579 if (ao_context->circ_buf.buf)
580 kfree(ao_context->circ_buf.buf);
585 /* Clear analog input context */
586 if (board_info->ai_context->circ_buf.buf)
587 kfree(board_info->ai_context->circ_buf.buf);
588 kfree(board_info->ai_context);
590 /* Clear digital I/O context */
591 kfree(board_info->dio_context);
593 /* Clear counter context */
594 kfree(board_info->cnt_context);
596 /* Clear external interrupt context */
597 kfree(board_info->ext_int_context);
600 /* Clear the board info list */
601 while (!list_empty(&me4000_board_info_list)) {
602 board_p = me4000_board_info_list.next;
603 board_info = list_entry(board_p, me4000_info_t, list);
604 pci_release_regions(board_info->pci_dev_p);
610 static int get_registers(struct pci_dev *dev, me4000_info_t * board_info)
613 /*--------------------------- plx regbase ---------------------------------*/
615 board_info->plx_regbase = pci_resource_start(dev, 1);
616 if (board_info->plx_regbase == 0) {
618 "ME4000:get_registers():PCI base address 1 is not available\n");
621 board_info->plx_regbase_size = pci_resource_len(dev, 1);
624 ("get_registers():PLX configuration registers at address 0x%4lX [0x%4lX]\n",
625 board_info->plx_regbase, board_info->plx_regbase_size);
627 /*--------------------------- me4000 regbase ------------------------------*/
629 board_info->me4000_regbase = pci_resource_start(dev, 2);
630 if (board_info->me4000_regbase == 0) {
632 "ME4000:get_registers():PCI base address 2 is not available\n");
635 board_info->me4000_regbase_size = pci_resource_len(dev, 2);
637 PDEBUG("get_registers():ME4000 registers at address 0x%4lX [0x%4lX]\n",
638 board_info->me4000_regbase, board_info->me4000_regbase_size);
640 /*--------------------------- timer regbase ------------------------------*/
642 board_info->timer_regbase = pci_resource_start(dev, 3);
643 if (board_info->timer_regbase == 0) {
645 "ME4000:get_registers():PCI base address 3 is not available\n");
648 board_info->timer_regbase_size = pci_resource_len(dev, 3);
650 PDEBUG("get_registers():Timer registers at address 0x%4lX [0x%4lX]\n",
651 board_info->timer_regbase, board_info->timer_regbase_size);
653 /*--------------------------- program regbase ------------------------------*/
655 board_info->program_regbase = pci_resource_start(dev, 5);
656 if (board_info->program_regbase == 0) {
658 "get_registers():ME4000:PCI base address 5 is not available\n");
661 board_info->program_regbase_size = pci_resource_len(dev, 5);
663 PDEBUG("get_registers():Program registers at address 0x%4lX [0x%4lX]\n",
664 board_info->program_regbase, board_info->program_regbase_size);
669 static int init_board_info(struct pci_dev *pci_dev_p,
670 me4000_info_t * board_info)
674 struct list_head *board_p;
675 board_info->pci_dev_p = pci_dev_p;
677 for (i = 0; i < ME4000_BOARD_VERSIONS; i++) {
678 if (me4000_boards[i].device_id == pci_dev_p->device) {
679 board_info->board_p = &me4000_boards[i];
683 if (i == ME4000_BOARD_VERSIONS) {
685 "ME4000:init_board_info():Device ID not valid\n");
689 /* Get the index of the board in the global list */
690 for (board_p = me4000_board_info_list.next, i = 0;
691 board_p != &me4000_board_info_list; board_p = board_p->next, i++) {
692 if (board_p == &board_info->list) {
693 board_info->board_count = i;
697 if (board_p == &me4000_board_info_list) {
699 "ME4000:init_board_info():Cannot get index of baord\n");
703 /* Init list head for analog output contexts */
704 INIT_LIST_HEAD(&board_info->ao_context_list);
706 /* Init spin locks */
707 spin_lock_init(&board_info->preload_lock);
708 spin_lock_init(&board_info->ai_ctrl_lock);
710 /* Get the serial number */
711 result = pci_read_config_dword(pci_dev_p, 0x2C, &board_info->serial_no);
712 if (result != PCIBIOS_SUCCESSFUL) {
714 "ME4000:init_board_info: Can't get serial_no\n");
717 PDEBUG("init_board_info():serial_no = 0x%x\n", board_info->serial_no);
719 /* Get the hardware revision */
721 pci_read_config_byte(pci_dev_p, 0x08, &board_info->hw_revision);
722 if (result != PCIBIOS_SUCCESSFUL) {
724 "ME4000:init_board_info():Can't get hw_revision\n");
727 PDEBUG("init_board_info():hw_revision = 0x%x\n",
728 board_info->hw_revision);
730 /* Get the vendor id */
731 board_info->vendor_id = pci_dev_p->vendor;
732 PDEBUG("init_board_info():vendor_id = 0x%x\n", board_info->vendor_id);
734 /* Get the device id */
735 board_info->device_id = pci_dev_p->device;
736 PDEBUG("init_board_info():device_id = 0x%x\n", board_info->device_id);
738 /* Get the pci device number */
739 board_info->pci_dev_no = PCI_FUNC(pci_dev_p->devfn);
740 PDEBUG("init_board_info():pci_func_no = 0x%x\n",
741 board_info->pci_func_no);
743 /* Get the pci slot number */
744 board_info->pci_dev_no = PCI_SLOT(pci_dev_p->devfn);
745 PDEBUG("init_board_info():pci_dev_no = 0x%x\n", board_info->pci_dev_no);
747 /* Get the pci bus number */
748 board_info->pci_bus_no = pci_dev_p->bus->number;
749 PDEBUG("init_board_info():pci_bus_no = 0x%x\n", board_info->pci_bus_no);
751 /* Get the irq assigned to the board */
752 board_info->irq = pci_dev_p->irq;
753 PDEBUG("init_board_info():irq = %d\n", board_info->irq);
758 static int alloc_ao_contexts(me4000_info_t * info)
762 me4000_ao_context_t *ao_context;
764 for (i = 0; i < info->board_p->ao.count; i++) {
765 ao_context = kmalloc(sizeof(me4000_ao_context_t), GFP_KERNEL);
768 "alloc_ao_contexts():Can't get memory for ao context\n");
769 release_ao_contexts(info);
772 memset(ao_context, 0, sizeof(me4000_ao_context_t));
774 spin_lock_init(&ao_context->use_lock);
775 spin_lock_init(&ao_context->int_lock);
776 ao_context->irq = info->irq;
777 init_waitqueue_head(&ao_context->wait_queue);
778 ao_context->board_info = info;
780 if (info->board_p->ao.fifo_count) {
781 /* Allocate circular buffer */
782 ao_context->circ_buf.buf =
783 kmalloc(ME4000_AO_BUFFER_SIZE, GFP_KERNEL);
784 if (!ao_context->circ_buf.buf) {
786 "alloc_ao_contexts():Can't get circular buffer\n");
787 release_ao_contexts(info);
790 memset(ao_context->circ_buf.buf, 0,
791 ME4000_AO_BUFFER_SIZE);
793 /* Clear the circular buffer */
794 ao_context->circ_buf.head = 0;
795 ao_context->circ_buf.tail = 0;
800 ao_context->ctrl_reg =
801 info->me4000_regbase + ME4000_AO_00_CTRL_REG;
802 ao_context->status_reg =
803 info->me4000_regbase + ME4000_AO_00_STATUS_REG;
804 ao_context->fifo_reg =
805 info->me4000_regbase + ME4000_AO_00_FIFO_REG;
806 ao_context->single_reg =
807 info->me4000_regbase + ME4000_AO_00_SINGLE_REG;
808 ao_context->timer_reg =
809 info->me4000_regbase + ME4000_AO_00_TIMER_REG;
810 ao_context->irq_status_reg =
811 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
812 ao_context->preload_reg =
813 info->me4000_regbase + ME4000_AO_LOADSETREG_XX;
816 ao_context->ctrl_reg =
817 info->me4000_regbase + ME4000_AO_01_CTRL_REG;
818 ao_context->status_reg =
819 info->me4000_regbase + ME4000_AO_01_STATUS_REG;
820 ao_context->fifo_reg =
821 info->me4000_regbase + ME4000_AO_01_FIFO_REG;
822 ao_context->single_reg =
823 info->me4000_regbase + ME4000_AO_01_SINGLE_REG;
824 ao_context->timer_reg =
825 info->me4000_regbase + ME4000_AO_01_TIMER_REG;
826 ao_context->irq_status_reg =
827 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
828 ao_context->preload_reg =
829 info->me4000_regbase + ME4000_AO_LOADSETREG_XX;
832 ao_context->ctrl_reg =
833 info->me4000_regbase + ME4000_AO_02_CTRL_REG;
834 ao_context->status_reg =
835 info->me4000_regbase + ME4000_AO_02_STATUS_REG;
836 ao_context->fifo_reg =
837 info->me4000_regbase + ME4000_AO_02_FIFO_REG;
838 ao_context->single_reg =
839 info->me4000_regbase + ME4000_AO_02_SINGLE_REG;
840 ao_context->timer_reg =
841 info->me4000_regbase + ME4000_AO_02_TIMER_REG;
842 ao_context->irq_status_reg =
843 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
844 ao_context->preload_reg =
845 info->me4000_regbase + ME4000_AO_LOADSETREG_XX;
848 ao_context->ctrl_reg =
849 info->me4000_regbase + ME4000_AO_03_CTRL_REG;
850 ao_context->status_reg =
851 info->me4000_regbase + ME4000_AO_03_STATUS_REG;
852 ao_context->fifo_reg =
853 info->me4000_regbase + ME4000_AO_03_FIFO_REG;
854 ao_context->single_reg =
855 info->me4000_regbase + ME4000_AO_03_SINGLE_REG;
856 ao_context->timer_reg =
857 info->me4000_regbase + ME4000_AO_03_TIMER_REG;
858 ao_context->irq_status_reg =
859 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
860 ao_context->preload_reg =
861 info->me4000_regbase + ME4000_AO_LOADSETREG_XX;
867 if (info->board_p->ao.fifo_count) {
868 /* Request the interrupt line */
870 request_irq(ao_context->irq, me4000_ao_isr,
871 IRQF_DISABLED | IRQF_SHARED,
872 ME4000_NAME, ao_context);
875 "alloc_ao_contexts():Can't get interrupt line");
876 if (ao_context->circ_buf.buf)
877 kfree(ao_context->circ_buf.buf);
879 release_ao_contexts(info);
884 list_add_tail(&ao_context->list, &info->ao_context_list);
885 ao_context->index = i;
891 static void release_ao_contexts(me4000_info_t * board_info)
893 struct list_head *dac_p;
894 me4000_ao_context_t *ao_context;
896 /* Clear analog output context list */
897 while (!list_empty(&board_info->ao_context_list)) {
898 dac_p = board_info->ao_context_list.next;
899 ao_context = list_entry(dac_p, me4000_ao_context_t, list);
900 free_irq(ao_context->irq, ao_context);
901 if (ao_context->circ_buf.buf)
902 kfree(ao_context->circ_buf.buf);
908 static int alloc_ai_context(me4000_info_t * info)
910 me4000_ai_context_t *ai_context;
912 if (info->board_p->ai.count) {
913 ai_context = kmalloc(sizeof(me4000_ai_context_t), GFP_KERNEL);
916 "ME4000:alloc_ai_context():Can't get memory for ai context\n");
919 memset(ai_context, 0, sizeof(me4000_ai_context_t));
921 info->ai_context = ai_context;
923 spin_lock_init(&ai_context->use_lock);
924 spin_lock_init(&ai_context->int_lock);
925 ai_context->number = 0;
926 ai_context->irq = info->irq;
927 init_waitqueue_head(&ai_context->wait_queue);
928 ai_context->board_info = info;
930 ai_context->ctrl_reg =
931 info->me4000_regbase + ME4000_AI_CTRL_REG;
932 ai_context->status_reg =
933 info->me4000_regbase + ME4000_AI_STATUS_REG;
934 ai_context->channel_list_reg =
935 info->me4000_regbase + ME4000_AI_CHANNEL_LIST_REG;
936 ai_context->data_reg =
937 info->me4000_regbase + ME4000_AI_DATA_REG;
938 ai_context->chan_timer_reg =
939 info->me4000_regbase + ME4000_AI_CHAN_TIMER_REG;
940 ai_context->chan_pre_timer_reg =
941 info->me4000_regbase + ME4000_AI_CHAN_PRE_TIMER_REG;
942 ai_context->scan_timer_low_reg =
943 info->me4000_regbase + ME4000_AI_SCAN_TIMER_LOW_REG;
944 ai_context->scan_timer_high_reg =
945 info->me4000_regbase + ME4000_AI_SCAN_TIMER_HIGH_REG;
946 ai_context->scan_pre_timer_low_reg =
947 info->me4000_regbase + ME4000_AI_SCAN_PRE_TIMER_LOW_REG;
948 ai_context->scan_pre_timer_high_reg =
949 info->me4000_regbase + ME4000_AI_SCAN_PRE_TIMER_HIGH_REG;
950 ai_context->start_reg =
951 info->me4000_regbase + ME4000_AI_START_REG;
952 ai_context->irq_status_reg =
953 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
954 ai_context->sample_counter_reg =
955 info->me4000_regbase + ME4000_AI_SAMPLE_COUNTER_REG;
961 static int alloc_dio_context(me4000_info_t * info)
963 me4000_dio_context_t *dio_context;
965 if (info->board_p->dio.count) {
966 dio_context = kmalloc(sizeof(me4000_dio_context_t), GFP_KERNEL);
969 "ME4000:alloc_dio_context():Can't get memory for dio context\n");
972 memset(dio_context, 0, sizeof(me4000_dio_context_t));
974 info->dio_context = dio_context;
976 spin_lock_init(&dio_context->use_lock);
977 dio_context->board_info = info;
979 dio_context->dio_count = info->board_p->dio.count;
981 dio_context->dir_reg =
982 info->me4000_regbase + ME4000_DIO_DIR_REG;
983 dio_context->ctrl_reg =
984 info->me4000_regbase + ME4000_DIO_CTRL_REG;
985 dio_context->port_0_reg =
986 info->me4000_regbase + ME4000_DIO_PORT_0_REG;
987 dio_context->port_1_reg =
988 info->me4000_regbase + ME4000_DIO_PORT_1_REG;
989 dio_context->port_2_reg =
990 info->me4000_regbase + ME4000_DIO_PORT_2_REG;
991 dio_context->port_3_reg =
992 info->me4000_regbase + ME4000_DIO_PORT_3_REG;
998 static int alloc_cnt_context(me4000_info_t * info)
1000 me4000_cnt_context_t *cnt_context;
1002 if (info->board_p->cnt.count) {
1003 cnt_context = kmalloc(sizeof(me4000_cnt_context_t), GFP_KERNEL);
1006 "ME4000:alloc_cnt_context():Can't get memory for cnt context\n");
1009 memset(cnt_context, 0, sizeof(me4000_cnt_context_t));
1011 info->cnt_context = cnt_context;
1013 spin_lock_init(&cnt_context->use_lock);
1014 cnt_context->board_info = info;
1016 cnt_context->ctrl_reg =
1017 info->timer_regbase + ME4000_CNT_CTRL_REG;
1018 cnt_context->counter_0_reg =
1019 info->timer_regbase + ME4000_CNT_COUNTER_0_REG;
1020 cnt_context->counter_1_reg =
1021 info->timer_regbase + ME4000_CNT_COUNTER_1_REG;
1022 cnt_context->counter_2_reg =
1023 info->timer_regbase + ME4000_CNT_COUNTER_2_REG;
1029 static int alloc_ext_int_context(me4000_info_t * info)
1031 me4000_ext_int_context_t *ext_int_context;
1033 if (info->board_p->cnt.count) {
1035 kmalloc(sizeof(me4000_ext_int_context_t), GFP_KERNEL);
1036 if (!ext_int_context) {
1038 "ME4000:alloc_ext_int_context():Can't get memory for cnt context\n");
1041 memset(ext_int_context, 0, sizeof(me4000_ext_int_context_t));
1043 info->ext_int_context = ext_int_context;
1045 spin_lock_init(&ext_int_context->use_lock);
1046 ext_int_context->board_info = info;
1048 ext_int_context->fasync_ptr = NULL;
1049 ext_int_context->irq = info->irq;
1051 ext_int_context->ctrl_reg =
1052 info->me4000_regbase + ME4000_AI_CTRL_REG;
1053 ext_int_context->irq_status_reg =
1054 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
1060 static int me4000_probe(struct pci_dev *dev, const struct pci_device_id *id)
1063 me4000_info_t *board_info;
1065 CALL_PDEBUG("me4000_probe() is executed\n");
1067 /* Allocate structure for board context */
1068 board_info = kmalloc(sizeof(me4000_info_t), GFP_KERNEL);
1071 "ME4000:Can't get memory for board info structure\n");
1075 memset(board_info, 0, sizeof(me4000_info_t));
1077 /* Add to global linked list */
1078 list_add_tail(&board_info->list, &me4000_board_info_list);
1080 /* Get the PCI base registers */
1081 result = get_registers(dev, board_info);
1083 printk(KERN_ERR "me4000_probe():Cannot get registers\n");
1087 /* Enable the device */
1088 result = pci_enable_device(dev);
1090 printk(KERN_ERR "me4000_probe():Cannot enable PCI device\n");
1094 /* Request the PCI register regions */
1095 result = pci_request_regions(dev, ME4000_NAME);
1097 printk(KERN_ERR "me4000_probe():Cannot request I/O regions\n");
1101 /* Initialize board info */
1102 result = init_board_info(dev, board_info);
1104 printk(KERN_ERR "me4000_probe():Cannot init baord info\n");
1108 /* Download the xilinx firmware */
1109 result = me4000_xilinx_download(board_info);
1111 printk(KERN_ERR "me4000_probe:Can't download firmware\n");
1115 /* Make a hardware reset */
1116 result = me4000_reset_board(board_info);
1118 printk(KERN_ERR "me4000_probe:Can't reset board\n");
1122 /* Allocate analog output context structures */
1123 result = alloc_ao_contexts(board_info);
1125 printk(KERN_ERR "me4000_probe():Cannot allocate ao contexts\n");
1129 /* Allocate analog input context */
1130 result = alloc_ai_context(board_info);
1132 printk(KERN_ERR "me4000_probe():Cannot allocate ai context\n");
1136 /* Allocate digital I/O context */
1137 result = alloc_dio_context(board_info);
1139 printk(KERN_ERR "me4000_probe():Cannot allocate dio context\n");
1143 /* Allocate counter context */
1144 result = alloc_cnt_context(board_info);
1146 printk(KERN_ERR "me4000_probe():Cannot allocate cnt context\n");
1150 /* Allocate external interrupt context */
1151 result = alloc_ext_int_context(board_info);
1154 "me4000_probe():Cannot allocate ext_int context\n");
1161 kfree(board_info->cnt_context);
1164 kfree(board_info->dio_context);
1167 kfree(board_info->ai_context);
1170 release_ao_contexts(board_info);
1173 pci_release_regions(dev);
1176 list_del(&board_info->list);
1183 static int me4000_xilinx_download(me4000_info_t * info)
1188 unsigned char *firm;
1189 wait_queue_head_t queue;
1191 CALL_PDEBUG("me4000_xilinx_download() is executed\n");
1193 init_waitqueue_head(&queue);
1195 firm = (info->device_id == 0x4610) ? xilinx_firm_4610 : xilinx_firm;
1198 * Set PLX local interrupt 2 polarity to high.
1199 * Interrupt is thrown by init pin of xilinx.
1201 outl(0x10, info->plx_regbase + PLX_INTCSR);
1203 /* Set /CS and /WRITE of the Xilinx */
1204 value = inl(info->plx_regbase + PLX_ICR);
1206 outl(value, info->plx_regbase + PLX_ICR);
1208 /* Init Xilinx with CS1 */
1209 inb(info->program_regbase + 0xC8);
1211 /* Wait until /INIT pin is set */
1213 if (!inl(info->plx_regbase + PLX_INTCSR) & 0x20) {
1214 printk(KERN_ERR "me4000_xilinx_download():Can't init Xilinx\n");
1218 /* Reset /CS and /WRITE of the Xilinx */
1219 value = inl(info->plx_regbase + PLX_ICR);
1221 outl(value, info->plx_regbase + PLX_ICR);
1223 /* Download Xilinx firmware */
1224 size = (firm[0] << 24) + (firm[1] << 16) + (firm[2] << 8) + firm[3];
1227 for (idx = 0; idx < size; idx++) {
1228 outb(firm[16 + idx], info->program_regbase);
1232 /* Check if BUSY flag is low */
1233 if (inl(info->plx_regbase + PLX_ICR) & 0x20) {
1235 "me4000_xilinx_download():Xilinx is still busy (idx = %d)\n",
1241 PDEBUG("me4000_xilinx_download():%d bytes written\n", idx);
1243 /* If done flag is high download was successful */
1244 if (inl(info->plx_regbase + PLX_ICR) & 0x4) {
1245 PDEBUG("me4000_xilinx_download():Done flag is set\n");
1246 PDEBUG("me4000_xilinx_download():Download was successful\n");
1249 "ME4000:me4000_xilinx_download():DONE flag is not set\n");
1251 "ME4000:me4000_xilinx_download():Download not succesful\n");
1255 /* Set /CS and /WRITE */
1256 value = inl(info->plx_regbase + PLX_ICR);
1258 outl(value, info->plx_regbase + PLX_ICR);
1263 static int me4000_reset_board(me4000_info_t * info)
1267 CALL_PDEBUG("me4000_reset_board() is executed\n");
1269 /* Make a hardware reset */
1270 icr = me4000_inl(info->plx_regbase + PLX_ICR);
1272 me4000_outl(icr, info->plx_regbase + PLX_ICR);
1274 me4000_outl(icr, info->plx_regbase + PLX_ICR);
1276 /* Set both stop bits in the analog input control register */
1277 me4000_outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP | ME4000_AI_CTRL_BIT_STOP,
1278 info->me4000_regbase + ME4000_AI_CTRL_REG);
1280 /* Set both stop bits in the analog output control register */
1281 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1282 info->me4000_regbase + ME4000_AO_00_CTRL_REG);
1283 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1284 info->me4000_regbase + ME4000_AO_01_CTRL_REG);
1285 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1286 info->me4000_regbase + ME4000_AO_02_CTRL_REG);
1287 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1288 info->me4000_regbase + ME4000_AO_03_CTRL_REG);
1290 /* 0x8000 to the DACs means an output voltage of 0V */
1291 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_00_SINGLE_REG);
1292 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_01_SINGLE_REG);
1293 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_02_SINGLE_REG);
1294 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_03_SINGLE_REG);
1296 /* Enable interrupts on the PLX */
1297 me4000_outl(0x43, info->plx_regbase + PLX_INTCSR);
1299 /* Set the adustment register for AO demux */
1300 me4000_outl(ME4000_AO_DEMUX_ADJUST_VALUE,
1301 info->me4000_regbase + ME4000_AO_DEMUX_ADJUST_REG);
1303 /* Set digital I/O direction for port 0 to output on isolated versions */
1304 if (!(me4000_inl(info->me4000_regbase + ME4000_DIO_DIR_REG) & 0x1)) {
1305 me4000_outl(0x1, info->me4000_regbase + ME4000_DIO_CTRL_REG);
1311 static int me4000_open(struct inode *inode_p, struct file *file_p)
1313 int board, dev, mode;
1316 struct list_head *ptr;
1317 me4000_info_t *board_info = NULL;
1318 me4000_ao_context_t *ao_context = NULL;
1319 me4000_ai_context_t *ai_context = NULL;
1320 me4000_dio_context_t *dio_context = NULL;
1321 me4000_cnt_context_t *cnt_context = NULL;
1322 me4000_ext_int_context_t *ext_int_context = NULL;
1324 CALL_PDEBUG("me4000_open() is executed\n");
1327 if (MAJOR(inode_p->i_rdev) == me4000_ao_major_driver_no) {
1328 board = AO_BOARD(inode_p->i_rdev);
1329 dev = AO_PORT(inode_p->i_rdev);
1330 mode = AO_MODE(inode_p->i_rdev);
1332 PDEBUG("me4000_open():board = %d ao = %d mode = %d\n", board,
1335 /* Search for the board context */
1336 for (ptr = me4000_board_info_list.next, i = 0;
1337 ptr != &me4000_board_info_list; ptr = ptr->next, i++) {
1338 board_info = list_entry(ptr, me4000_info_t, list);
1343 if (ptr == &me4000_board_info_list) {
1345 "ME4000:me4000_open():Board %d not in device list\n",
1350 /* Search for the dac context */
1351 for (ptr = board_info->ao_context_list.next, i = 0;
1352 ptr != &board_info->ao_context_list;
1353 ptr = ptr->next, i++) {
1354 ao_context = list_entry(ptr, me4000_ao_context_t, list);
1359 if (ptr == &board_info->ao_context_list) {
1361 "ME4000:me4000_open():Device %d not in device list\n",
1366 /* Check if mode is valid */
1369 "ME4000:me4000_open():Mode is not valid\n");
1373 /* Check if mode is valid for this AO */
1374 if ((mode != ME4000_AO_CONV_MODE_SINGLE)
1375 && (dev >= board_info->board_p->ao.fifo_count)) {
1377 "ME4000:me4000_open():AO %d only in single mode available\n",
1382 /* Check if already opened */
1383 spin_lock(&ao_context->use_lock);
1384 if (ao_context->dac_in_use) {
1386 "ME4000:me4000_open():AO %d already in use\n",
1388 spin_unlock(&ao_context->use_lock);
1391 ao_context->dac_in_use = 1;
1392 spin_unlock(&ao_context->use_lock);
1394 ao_context->mode = mode;
1396 /* Hold the context in private data */
1397 file_p->private_data = ao_context;
1399 /* Set file operations pointer */
1400 file_p->f_op = me4000_ao_fops_array[mode];
1402 err = me4000_ao_prepare(ao_context);
1404 ao_context->dac_in_use = 0;
1409 else if (MAJOR(inode_p->i_rdev) == me4000_ai_major_driver_no) {
1410 board = AI_BOARD(inode_p->i_rdev);
1411 mode = AI_MODE(inode_p->i_rdev);
1413 PDEBUG("me4000_open():ai board = %d mode = %d\n", board, mode);
1415 /* Search for the board context */
1416 for (ptr = me4000_board_info_list.next, i = 0;
1417 ptr != &me4000_board_info_list; ptr = ptr->next, i++) {
1418 board_info = list_entry(ptr, me4000_info_t, list);
1423 if (ptr == &me4000_board_info_list) {
1425 "ME4000:me4000_open():Board %d not in device list\n",
1430 ai_context = board_info->ai_context;
1432 /* Check if mode is valid */
1435 "ME4000:me4000_open():Mode is not valid\n");
1439 /* Check if already opened */
1440 spin_lock(&ai_context->use_lock);
1441 if (ai_context->in_use) {
1443 "ME4000:me4000_open():AI already in use\n");
1444 spin_unlock(&ai_context->use_lock);
1447 ai_context->in_use = 1;
1448 spin_unlock(&ai_context->use_lock);
1450 ai_context->mode = mode;
1452 /* Hold the context in private data */
1453 file_p->private_data = ai_context;
1455 /* Set file operations pointer */
1456 file_p->f_op = me4000_ai_fops_array[mode];
1458 /* Prepare analog input */
1459 me4000_ai_prepare(ai_context);
1462 else if (MAJOR(inode_p->i_rdev) == me4000_dio_major_driver_no) {
1463 board = DIO_BOARD(inode_p->i_rdev);
1467 PDEBUG("me4000_open():board = %d\n", board);
1469 /* Search for the board context */
1470 for (ptr = me4000_board_info_list.next;
1471 ptr != &me4000_board_info_list; ptr = ptr->next) {
1472 board_info = list_entry(ptr, me4000_info_t, list);
1473 if (board_info->board_count == board)
1477 if (ptr == &me4000_board_info_list) {
1479 "ME4000:me4000_open():Board %d not in device list\n",
1484 /* Search for the dio context */
1485 dio_context = board_info->dio_context;
1487 /* Check if already opened */
1488 spin_lock(&dio_context->use_lock);
1489 if (dio_context->in_use) {
1491 "ME4000:me4000_open():DIO already in use\n");
1492 spin_unlock(&dio_context->use_lock);
1495 dio_context->in_use = 1;
1496 spin_unlock(&dio_context->use_lock);
1498 /* Hold the context in private data */
1499 file_p->private_data = dio_context;
1501 /* Set file operations pointer to single functions */
1502 file_p->f_op = &me4000_dio_fops;
1504 //me4000_dio_reset(dio_context);
1507 else if (MAJOR(inode_p->i_rdev) == me4000_cnt_major_driver_no) {
1508 board = CNT_BOARD(inode_p->i_rdev);
1512 PDEBUG("me4000_open():board = %d\n", board);
1514 /* Search for the board context */
1515 for (ptr = me4000_board_info_list.next;
1516 ptr != &me4000_board_info_list; ptr = ptr->next) {
1517 board_info = list_entry(ptr, me4000_info_t, list);
1518 if (board_info->board_count == board)
1522 if (ptr == &me4000_board_info_list) {
1524 "ME4000:me4000_open():Board %d not in device list\n",
1529 /* Get the cnt context */
1530 cnt_context = board_info->cnt_context;
1532 /* Check if already opened */
1533 spin_lock(&cnt_context->use_lock);
1534 if (cnt_context->in_use) {
1536 "ME4000:me4000_open():CNT already in use\n");
1537 spin_unlock(&cnt_context->use_lock);
1540 cnt_context->in_use = 1;
1541 spin_unlock(&cnt_context->use_lock);
1543 /* Hold the context in private data */
1544 file_p->private_data = cnt_context;
1546 /* Set file operations pointer to single functions */
1547 file_p->f_op = &me4000_cnt_fops;
1549 /* External Interrupt */
1550 else if (MAJOR(inode_p->i_rdev) == me4000_ext_int_major_driver_no) {
1551 board = EXT_INT_BOARD(inode_p->i_rdev);
1555 PDEBUG("me4000_open():board = %d\n", board);
1557 /* Search for the board context */
1558 for (ptr = me4000_board_info_list.next;
1559 ptr != &me4000_board_info_list; ptr = ptr->next) {
1560 board_info = list_entry(ptr, me4000_info_t, list);
1561 if (board_info->board_count == board)
1565 if (ptr == &me4000_board_info_list) {
1567 "ME4000:me4000_open():Board %d not in device list\n",
1572 /* Get the external interrupt context */
1573 ext_int_context = board_info->ext_int_context;
1575 /* Check if already opened */
1576 spin_lock(&cnt_context->use_lock);
1577 if (ext_int_context->in_use) {
1579 "ME4000:me4000_open():External interrupt already in use\n");
1580 spin_unlock(&ext_int_context->use_lock);
1583 ext_int_context->in_use = 1;
1584 spin_unlock(&ext_int_context->use_lock);
1586 /* Hold the context in private data */
1587 file_p->private_data = ext_int_context;
1589 /* Set file operations pointer to single functions */
1590 file_p->f_op = &me4000_ext_int_fops;
1592 /* Request the interrupt line */
1594 request_irq(ext_int_context->irq, me4000_ext_int_isr,
1595 IRQF_DISABLED | IRQF_SHARED, ME4000_NAME,
1599 "ME4000:me4000_open():Can't get interrupt line");
1600 ext_int_context->in_use = 0;
1604 /* Reset the counter */
1605 me4000_ext_int_disable(ext_int_context);
1607 printk(KERN_ERR "ME4000:me4000_open():Major number unknown\n");
1614 static int me4000_release(struct inode *inode_p, struct file *file_p)
1616 me4000_ao_context_t *ao_context;
1617 me4000_ai_context_t *ai_context;
1618 me4000_dio_context_t *dio_context;
1619 me4000_cnt_context_t *cnt_context;
1620 me4000_ext_int_context_t *ext_int_context;
1622 CALL_PDEBUG("me4000_release() is executed\n");
1624 if (MAJOR(inode_p->i_rdev) == me4000_ao_major_driver_no) {
1625 ao_context = file_p->private_data;
1627 /* Mark DAC as unused */
1628 ao_context->dac_in_use = 0;
1629 } else if (MAJOR(inode_p->i_rdev) == me4000_ai_major_driver_no) {
1630 ai_context = file_p->private_data;
1632 /* Reset the analog input */
1633 me4000_ai_reset(ai_context);
1635 /* Free the interrupt and the circular buffer */
1636 if (ai_context->mode) {
1637 free_irq(ai_context->irq, ai_context);
1638 kfree(ai_context->circ_buf.buf);
1639 ai_context->circ_buf.buf = NULL;
1640 ai_context->circ_buf.head = 0;
1641 ai_context->circ_buf.tail = 0;
1644 /* Mark AI as unused */
1645 ai_context->in_use = 0;
1646 } else if (MAJOR(inode_p->i_rdev) == me4000_dio_major_driver_no) {
1647 dio_context = file_p->private_data;
1649 /* Mark digital I/O as unused */
1650 dio_context->in_use = 0;
1651 } else if (MAJOR(inode_p->i_rdev) == me4000_cnt_major_driver_no) {
1652 cnt_context = file_p->private_data;
1654 /* Mark counters as unused */
1655 cnt_context->in_use = 0;
1656 } else if (MAJOR(inode_p->i_rdev) == me4000_ext_int_major_driver_no) {
1657 ext_int_context = file_p->private_data;
1659 /* Disable the externel interrupt */
1660 me4000_ext_int_disable(ext_int_context);
1662 free_irq(ext_int_context->irq, ext_int_context);
1664 /* Delete the fasync structure and free memory */
1665 me4000_ext_int_fasync(0, file_p, 0);
1667 /* Mark as unused */
1668 ext_int_context->in_use = 0;
1671 "ME4000:me4000_release():Major number unknown\n");
1678 /*------------------------------- Analog output stuff --------------------------------------*/
1680 static int me4000_ao_prepare(me4000_ao_context_t * ao_context)
1682 unsigned long flags;
1684 CALL_PDEBUG("me4000_ao_prepare() is executed\n");
1686 if (ao_context->mode == ME4000_AO_CONV_MODE_CONTINUOUS) {
1687 /* Only do anything if not already in the correct mode */
1688 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1689 if ((mode & ME4000_AO_CONV_MODE_CONTINUOUS)
1690 && (mode & ME4000_AO_CTRL_BIT_ENABLE_FIFO)) {
1694 /* Stop any conversion */
1695 me4000_ao_immediate_stop(ao_context);
1697 /* Set the control register to default state */
1698 spin_lock_irqsave(&ao_context->int_lock, flags);
1699 me4000_outl(ME4000_AO_CONV_MODE_CONTINUOUS |
1700 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1701 ME4000_AO_CTRL_BIT_STOP |
1702 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1703 ao_context->ctrl_reg);
1704 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1706 /* Set to fastest sample rate */
1707 me4000_outl(65, ao_context->timer_reg);
1708 } else if (ao_context->mode == ME4000_AO_CONV_MODE_WRAPAROUND) {
1709 /* Only do anything if not already in the correct mode */
1710 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1711 if ((mode & ME4000_AO_CONV_MODE_WRAPAROUND)
1712 && (mode & ME4000_AO_CTRL_BIT_ENABLE_FIFO)) {
1716 /* Stop any conversion */
1717 me4000_ao_immediate_stop(ao_context);
1719 /* Set the control register to default state */
1720 spin_lock_irqsave(&ao_context->int_lock, flags);
1721 me4000_outl(ME4000_AO_CONV_MODE_WRAPAROUND |
1722 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1723 ME4000_AO_CTRL_BIT_STOP |
1724 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1725 ao_context->ctrl_reg);
1726 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1728 /* Set to fastest sample rate */
1729 me4000_outl(65, ao_context->timer_reg);
1730 } else if (ao_context->mode == ME4000_AO_CONV_MODE_SINGLE) {
1731 /* Only do anything if not already in the correct mode */
1732 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1735 (ME4000_AO_CONV_MODE_WRAPAROUND |
1736 ME4000_AO_CONV_MODE_CONTINUOUS))) {
1740 /* Stop any conversion */
1741 me4000_ao_immediate_stop(ao_context);
1743 /* Clear the control register */
1744 spin_lock_irqsave(&ao_context->int_lock, flags);
1745 me4000_outl(0x0, ao_context->ctrl_reg);
1746 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1748 /* Set voltage to 0V */
1749 me4000_outl(0x8000, ao_context->single_reg);
1752 "ME4000:me4000_ao_prepare():Invalid mode specified\n");
1759 static int me4000_ao_reset(me4000_ao_context_t * ao_context)
1762 wait_queue_head_t queue;
1763 unsigned long flags;
1765 CALL_PDEBUG("me4000_ao_reset() is executed\n");
1767 init_waitqueue_head(&queue);
1769 if (ao_context->mode == ME4000_AO_CONV_MODE_WRAPAROUND) {
1771 * First stop conversion of the DAC before reconfigure.
1772 * This is essantial, cause of the state machine.
1773 * If not stopped before configuring mode, it could
1774 * walk in a undefined state.
1776 tmp = me4000_inl(ao_context->ctrl_reg);
1777 tmp |= ME4000_AO_CTRL_BIT_IMMEDIATE_STOP;
1778 me4000_outl(tmp, ao_context->ctrl_reg);
1780 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
1781 sleep_on_timeout(&queue, 1);
1784 /* Set to transparent mode */
1785 me4000_ao_simultaneous_disable(ao_context);
1787 /* Set to single mode in order to set default voltage */
1788 me4000_outl(0x0, ao_context->ctrl_reg);
1790 /* Set voltage to 0V */
1791 me4000_outl(0x8000, ao_context->single_reg);
1793 /* Set to fastest sample rate */
1794 me4000_outl(65, ao_context->timer_reg);
1796 /* Set the original mode and enable FIFO */
1797 me4000_outl(ME4000_AO_CONV_MODE_WRAPAROUND |
1798 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1799 ME4000_AO_CTRL_BIT_STOP |
1800 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1801 ao_context->ctrl_reg);
1802 } else if (ao_context->mode == ME4000_AO_CONV_MODE_CONTINUOUS) {
1804 * First stop conversion of the DAC before reconfigure.
1805 * This is essantial, cause of the state machine.
1806 * If not stopped before configuring mode, it could
1807 * walk in a undefined state.
1809 spin_lock_irqsave(&ao_context->int_lock, flags);
1810 tmp = me4000_inl(ao_context->ctrl_reg);
1811 tmp |= ME4000_AO_CTRL_BIT_STOP;
1812 me4000_outl(tmp, ao_context->ctrl_reg);
1813 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1815 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
1816 sleep_on_timeout(&queue, 1);
1819 /* Clear the circular buffer */
1820 ao_context->circ_buf.head = 0;
1821 ao_context->circ_buf.tail = 0;
1823 /* Set to transparent mode */
1824 me4000_ao_simultaneous_disable(ao_context);
1826 /* Set to single mode in order to set default voltage */
1827 spin_lock_irqsave(&ao_context->int_lock, flags);
1828 tmp = me4000_inl(ao_context->ctrl_reg);
1829 me4000_outl(0x0, ao_context->ctrl_reg);
1831 /* Set voltage to 0V */
1832 me4000_outl(0x8000, ao_context->single_reg);
1834 /* Set to fastest sample rate */
1835 me4000_outl(65, ao_context->timer_reg);
1837 /* Set the original mode and enable FIFO */
1838 me4000_outl(ME4000_AO_CONV_MODE_CONTINUOUS |
1839 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1840 ME4000_AO_CTRL_BIT_STOP |
1841 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1842 ao_context->ctrl_reg);
1843 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1845 /* Set to transparent mode */
1846 me4000_ao_simultaneous_disable(ao_context);
1848 /* Set voltage to 0V */
1849 me4000_outl(0x8000, ao_context->single_reg);
1855 static ssize_t me4000_ao_write_sing(struct file *filep, const char *buff,
1856 size_t cnt, loff_t * offp)
1858 me4000_ao_context_t *ao_context = filep->private_data;
1860 const u16 *buffer = (const u16 *)buff;
1862 CALL_PDEBUG("me4000_ao_write_sing() is executed\n");
1866 "me4000_ao_write_sing():Write count is not 2\n");
1870 if (get_user(value, buffer)) {
1872 "me4000_ao_write_sing():Cannot copy data from user\n");
1876 me4000_outl(value, ao_context->single_reg);
1881 static ssize_t me4000_ao_write_wrap(struct file *filep, const char *buff,
1882 size_t cnt, loff_t * offp)
1884 me4000_ao_context_t *ao_context = filep->private_data;
1888 const u16 *buffer = (const u16 *)buff;
1889 size_t count = cnt / 2;
1891 CALL_PDEBUG("me4000_ao_write_wrap() is executed\n");
1893 /* Check if a conversion is already running */
1894 if (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
1896 "ME4000:me4000_ao_write_wrap():There is already a conversion running\n");
1900 if (count > ME4000_AO_FIFO_COUNT) {
1902 "me4000_ao_write_wrap():Can't load more than %d values\n",
1903 ME4000_AO_FIFO_COUNT);
1907 /* Reset the FIFO */
1908 tmp = inl(ao_context->ctrl_reg);
1909 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_FIFO;
1910 outl(tmp, ao_context->ctrl_reg);
1911 tmp |= ME4000_AO_CTRL_BIT_ENABLE_FIFO;
1912 outl(tmp, ao_context->ctrl_reg);
1914 for (i = 0; i < count; i++) {
1915 if (get_user(value, buffer + i)) {
1917 "me4000_ao_write_single():Cannot copy data from user\n");
1920 if (((ao_context->fifo_reg & 0xFF) == ME4000_AO_01_FIFO_REG)
1921 || ((ao_context->fifo_reg & 0xFF) == ME4000_AO_03_FIFO_REG))
1922 value = value << 16;
1923 outl(value, ao_context->fifo_reg);
1925 CALL_PDEBUG("me4000_ao_write_wrap() is leaved with %d\n", i * 2);
1930 static ssize_t me4000_ao_write_cont(struct file *filep, const char *buff,
1931 size_t cnt, loff_t * offp)
1933 me4000_ao_context_t *ao_context = filep->private_data;
1934 const u16 *buffer = (const u16 *)buff;
1935 size_t count = cnt / 2;
1936 unsigned long flags;
1944 wait_queue_head_t queue;
1946 CALL_PDEBUG("me4000_ao_write_cont() is executed\n");
1948 init_waitqueue_head(&queue);
1952 PDEBUG("me4000_ao_write_cont():Count is 0\n");
1956 if (filep->f_flags & O_APPEND) {
1957 PDEBUG("me4000_ao_write_cont():Append data to data stream\n");
1959 if (filep->f_flags & O_NONBLOCK) {
1960 if (ao_context->pipe_flag) {
1962 "ME4000:me4000_ao_write_cont():Broken pipe in nonblocking write\n");
1965 c = me4000_space_to_end(ao_context->circ_buf,
1966 ME4000_AO_BUFFER_COUNT);
1969 ("me4000_ao_write_cont():Returning from nonblocking write\n");
1973 wait_event_interruptible(ao_context->wait_queue,
1976 (ao_context->circ_buf,
1977 ME4000_AO_BUFFER_COUNT)));
1978 if (ao_context->pipe_flag) {
1980 "me4000_ao_write_cont():Broken pipe in blocking write\n");
1983 if (signal_pending(current)) {
1985 "me4000_ao_write_cont():Wait for free buffer interrupted from signal\n");
1990 PDEBUG("me4000_ao_write_cont():Space to end = %d\n", c);
1992 /* Only able to write size of free buffer or size of count */
1997 k -= copy_from_user(ao_context->circ_buf.buf +
1998 ao_context->circ_buf.head, buffer,
2002 ("me4000_ao_write_cont():Copy %d values from user space\n",
2008 ao_context->circ_buf.head =
2009 (ao_context->circ_buf.head +
2010 c) & (ME4000_AO_BUFFER_COUNT - 1);
2015 /* Values are now available so enable interrupts */
2016 spin_lock_irqsave(&ao_context->int_lock, flags);
2017 if (me4000_buf_count
2018 (ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
2019 tmp = me4000_inl(ao_context->ctrl_reg);
2020 tmp |= ME4000_AO_CTRL_BIT_ENABLE_IRQ;
2021 me4000_outl(tmp, ao_context->ctrl_reg);
2023 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2026 /* Wait until the state machine is stopped if O_SYNC is set */
2027 if (filep->f_flags & O_SYNC) {
2028 while (inl(ao_context->status_reg) &
2029 ME4000_AO_STATUS_BIT_FSM) {
2030 interruptible_sleep_on_timeout(&queue, 1);
2031 if (ao_context->pipe_flag) {
2033 ("me4000_ao_write_cont():Broken pipe detected after sync\n");
2036 if (signal_pending(current)) {
2038 "me4000_ao_write_cont():Wait on state machine after sync interrupted\n");
2044 PDEBUG("me4000_ao_write_cont():Preload DAC FIFO\n");
2045 if ((me4000_inl(ao_context->status_reg) &
2046 ME4000_AO_STATUS_BIT_FSM)) {
2048 "me4000_ao_write_cont():Can't Preload DAC FIFO while conversion is running\n");
2052 /* Clear the FIFO */
2053 spin_lock_irqsave(&ao_context->int_lock, flags);
2054 tmp = me4000_inl(ao_context->ctrl_reg);
2056 ~(ME4000_AO_CTRL_BIT_ENABLE_FIFO |
2057 ME4000_AO_CTRL_BIT_ENABLE_IRQ);
2058 me4000_outl(tmp, ao_context->ctrl_reg);
2059 tmp |= ME4000_AO_CTRL_BIT_ENABLE_FIFO;
2060 me4000_outl(tmp, ao_context->ctrl_reg);
2061 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2063 /* Clear the circular buffer */
2064 ao_context->circ_buf.head = 0;
2065 ao_context->circ_buf.tail = 0;
2067 /* Reset the broken pipe flag */
2068 ao_context->pipe_flag = 0;
2070 /* Only able to write size of fifo or count */
2071 c = ME4000_AO_FIFO_COUNT;
2076 ("me4000_ao_write_cont():Write %d values to DAC on 0x%lX\n",
2077 c, ao_context->fifo_reg);
2079 /* Write values to the fifo */
2080 for (i = 0; i < c; i++) {
2081 if (get_user(svalue, buffer))
2084 if (((ao_context->fifo_reg & 0xFF) ==
2085 ME4000_AO_01_FIFO_REG)
2086 || ((ao_context->fifo_reg & 0xFF) ==
2087 ME4000_AO_03_FIFO_REG)) {
2088 lvalue = ((u32) svalue) << 16;
2090 lvalue = (u32) svalue;
2092 outl(lvalue, ao_context->fifo_reg);
2099 /* Get free buffer */
2100 c = me4000_space_to_end(ao_context->circ_buf,
2101 ME4000_AO_BUFFER_COUNT);
2106 /* Only able to write size of free buffer or size of count */
2110 /* If count = 0 return to user */
2113 ("me4000_ao_write_cont():Count reached 0\n");
2118 k -= copy_from_user(ao_context->circ_buf.buf +
2119 ao_context->circ_buf.head, buffer,
2123 ("me4000_ao_write_cont():Wrote %d values to buffer\n",
2129 ao_context->circ_buf.head =
2130 (ao_context->circ_buf.head +
2131 c) & (ME4000_AO_BUFFER_COUNT - 1);
2136 /* If values in the buffer are available so enable interrupts */
2137 spin_lock_irqsave(&ao_context->int_lock, flags);
2138 if (me4000_buf_count
2139 (ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
2141 ("me4000_ao_write_cont():Enable Interrupts\n");
2142 tmp = me4000_inl(ao_context->ctrl_reg);
2143 tmp |= ME4000_AO_CTRL_BIT_ENABLE_IRQ;
2144 me4000_outl(tmp, ao_context->ctrl_reg);
2146 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2150 if (filep->f_flags & O_NONBLOCK) {
2151 return (ret == 0) ? -EAGAIN : 2 * ret;
2157 static unsigned int me4000_ao_poll_cont(struct file *file_p, poll_table * wait)
2159 me4000_ao_context_t *ao_context;
2160 unsigned long mask = 0;
2162 CALL_PDEBUG("me4000_ao_poll_cont() is executed\n");
2164 ao_context = file_p->private_data;
2166 poll_wait(file_p, &ao_context->wait_queue, wait);
2168 /* Get free buffer */
2169 if (me4000_space_to_end(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT))
2170 mask |= POLLOUT | POLLWRNORM;
2172 CALL_PDEBUG("me4000_ao_poll_cont():Return mask %lX\n", mask);
2177 static int me4000_ao_fsync_cont(struct file *file_p, struct dentry *dentry_p,
2180 me4000_ao_context_t *ao_context;
2181 wait_queue_head_t queue;
2183 CALL_PDEBUG("me4000_ao_fsync_cont() is executed\n");
2185 ao_context = file_p->private_data;
2186 init_waitqueue_head(&queue);
2188 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2189 interruptible_sleep_on_timeout(&queue, 1);
2190 if (ao_context->pipe_flag) {
2192 "me4000_ao_fsync_cont():Broken pipe detected\n");
2196 if (signal_pending(current)) {
2198 "me4000_ao_fsync_cont():Wait on state machine interrupted\n");
2206 static int me4000_ao_ioctl_sing(struct inode *inode_p, struct file *file_p,
2207 unsigned int service, unsigned long arg)
2209 me4000_ao_context_t *ao_context;
2211 CALL_PDEBUG("me4000_ao_ioctl_sing() is executed\n");
2213 ao_context = file_p->private_data;
2215 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2217 PDEBUG("me4000_ao_ioctl_sing():Wrong magic number\n");
2221 case ME4000_AO_EX_TRIG_SETUP:
2222 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2223 case ME4000_AO_EX_TRIG_ENABLE:
2224 return me4000_ao_ex_trig_enable(ao_context);
2225 case ME4000_AO_EX_TRIG_DISABLE:
2226 return me4000_ao_ex_trig_disable(ao_context);
2227 case ME4000_AO_PRELOAD:
2228 return me4000_ao_preload(ao_context);
2229 case ME4000_AO_PRELOAD_UPDATE:
2230 return me4000_ao_preload_update(ao_context);
2231 case ME4000_GET_USER_INFO:
2232 return me4000_get_user_info((me4000_user_info_t *) arg,
2233 ao_context->board_info);
2234 case ME4000_AO_SIMULTANEOUS_EX_TRIG:
2235 return me4000_ao_simultaneous_ex_trig(ao_context);
2236 case ME4000_AO_SIMULTANEOUS_SW:
2237 return me4000_ao_simultaneous_sw(ao_context);
2238 case ME4000_AO_SIMULTANEOUS_DISABLE:
2239 return me4000_ao_simultaneous_disable(ao_context);
2240 case ME4000_AO_SIMULTANEOUS_UPDATE:
2242 me4000_ao_simultaneous_update((me4000_ao_channel_list_t *)
2244 case ME4000_AO_EX_TRIG_TIMEOUT:
2245 return me4000_ao_ex_trig_timeout((unsigned long *)arg,
2247 case ME4000_AO_DISABLE_DO:
2248 return me4000_ao_disable_do(ao_context);
2251 "me4000_ao_ioctl_sing():Service number invalid\n");
2258 static int me4000_ao_ioctl_wrap(struct inode *inode_p, struct file *file_p,
2259 unsigned int service, unsigned long arg)
2261 me4000_ao_context_t *ao_context;
2263 CALL_PDEBUG("me4000_ao_ioctl_wrap() is executed\n");
2265 ao_context = file_p->private_data;
2267 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2269 PDEBUG("me4000_ao_ioctl_wrap():Wrong magic number\n");
2273 case ME4000_AO_START:
2274 return me4000_ao_start((unsigned long *)arg, ao_context);
2275 case ME4000_AO_STOP:
2276 return me4000_ao_stop(ao_context);
2277 case ME4000_AO_IMMEDIATE_STOP:
2278 return me4000_ao_immediate_stop(ao_context);
2279 case ME4000_AO_RESET:
2280 return me4000_ao_reset(ao_context);
2281 case ME4000_AO_TIMER_SET_DIVISOR:
2282 return me4000_ao_timer_set_divisor((u32 *) arg, ao_context);
2283 case ME4000_AO_EX_TRIG_SETUP:
2284 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2285 case ME4000_AO_EX_TRIG_ENABLE:
2286 return me4000_ao_ex_trig_enable(ao_context);
2287 case ME4000_AO_EX_TRIG_DISABLE:
2288 return me4000_ao_ex_trig_disable(ao_context);
2289 case ME4000_GET_USER_INFO:
2290 return me4000_get_user_info((me4000_user_info_t *) arg,
2291 ao_context->board_info);
2292 case ME4000_AO_FSM_STATE:
2293 return me4000_ao_fsm_state((int *)arg, ao_context);
2294 case ME4000_AO_ENABLE_DO:
2295 return me4000_ao_enable_do(ao_context);
2296 case ME4000_AO_DISABLE_DO:
2297 return me4000_ao_disable_do(ao_context);
2298 case ME4000_AO_SYNCHRONOUS_EX_TRIG:
2299 return me4000_ao_synchronous_ex_trig(ao_context);
2300 case ME4000_AO_SYNCHRONOUS_SW:
2301 return me4000_ao_synchronous_sw(ao_context);
2302 case ME4000_AO_SYNCHRONOUS_DISABLE:
2303 return me4000_ao_synchronous_disable(ao_context);
2310 static int me4000_ao_ioctl_cont(struct inode *inode_p, struct file *file_p,
2311 unsigned int service, unsigned long arg)
2313 me4000_ao_context_t *ao_context;
2315 CALL_PDEBUG("me4000_ao_ioctl_cont() is executed\n");
2317 ao_context = file_p->private_data;
2319 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2321 PDEBUG("me4000_ao_ioctl_cont():Wrong magic number\n");
2325 case ME4000_AO_START:
2326 return me4000_ao_start((unsigned long *)arg, ao_context);
2327 case ME4000_AO_STOP:
2328 return me4000_ao_stop(ao_context);
2329 case ME4000_AO_IMMEDIATE_STOP:
2330 return me4000_ao_immediate_stop(ao_context);
2331 case ME4000_AO_RESET:
2332 return me4000_ao_reset(ao_context);
2333 case ME4000_AO_TIMER_SET_DIVISOR:
2334 return me4000_ao_timer_set_divisor((u32 *) arg, ao_context);
2335 case ME4000_AO_EX_TRIG_SETUP:
2336 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2337 case ME4000_AO_EX_TRIG_ENABLE:
2338 return me4000_ao_ex_trig_enable(ao_context);
2339 case ME4000_AO_EX_TRIG_DISABLE:
2340 return me4000_ao_ex_trig_disable(ao_context);
2341 case ME4000_AO_ENABLE_DO:
2342 return me4000_ao_enable_do(ao_context);
2343 case ME4000_AO_DISABLE_DO:
2344 return me4000_ao_disable_do(ao_context);
2345 case ME4000_AO_FSM_STATE:
2346 return me4000_ao_fsm_state((int *)arg, ao_context);
2347 case ME4000_GET_USER_INFO:
2348 return me4000_get_user_info((me4000_user_info_t *) arg,
2349 ao_context->board_info);
2350 case ME4000_AO_SYNCHRONOUS_EX_TRIG:
2351 return me4000_ao_synchronous_ex_trig(ao_context);
2352 case ME4000_AO_SYNCHRONOUS_SW:
2353 return me4000_ao_synchronous_sw(ao_context);
2354 case ME4000_AO_SYNCHRONOUS_DISABLE:
2355 return me4000_ao_synchronous_disable(ao_context);
2356 case ME4000_AO_GET_FREE_BUFFER:
2357 return me4000_ao_get_free_buffer((unsigned long *)arg,
2365 static int me4000_ao_start(unsigned long *arg, me4000_ao_context_t * ao_context)
2368 wait_queue_head_t queue;
2370 unsigned long timeout;
2371 unsigned long flags;
2373 CALL_PDEBUG("me4000_ao_start() is executed\n");
2375 if (get_user(timeout, arg)) {
2377 "me4000_ao_start():Cannot copy data from user\n");
2381 init_waitqueue_head(&queue);
2383 spin_lock_irqsave(&ao_context->int_lock, flags);
2384 tmp = inl(ao_context->ctrl_reg);
2385 tmp &= ~(ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2386 me4000_outl(tmp, ao_context->ctrl_reg);
2387 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2389 if ((tmp & ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG)) {
2393 (inl(ao_context->status_reg) &
2394 ME4000_AO_STATUS_BIT_FSM)) {
2395 interruptible_sleep_on_timeout(&queue, 1);
2396 if (signal_pending(current)) {
2398 "ME4000:me4000_ao_start():Wait on start of state machine interrupted\n");
2401 if (((jiffies - ref) > (timeout * HZ / USER_HZ))) { // 2.6 has diffrent definitions for HZ in user and kernel space
2403 "ME4000:me4000_ao_start():Timeout reached\n");
2409 me4000_outl(0x8000, ao_context->single_reg);
2415 static int me4000_ao_stop(me4000_ao_context_t * ao_context)
2418 wait_queue_head_t queue;
2419 unsigned long flags;
2421 init_waitqueue_head(&queue);
2423 CALL_PDEBUG("me4000_ao_stop() is executed\n");
2425 /* Set the stop bit */
2426 spin_lock_irqsave(&ao_context->int_lock, flags);
2427 tmp = inl(ao_context->ctrl_reg);
2428 tmp |= ME4000_AO_CTRL_BIT_STOP;
2429 me4000_outl(tmp, ao_context->ctrl_reg);
2430 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2432 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2433 interruptible_sleep_on_timeout(&queue, 1);
2434 if (signal_pending(current)) {
2436 "me4000_ao_stop():Wait on state machine after stop interrupted\n");
2441 /* Clear the stop bit */
2442 //tmp &= ~ME4000_AO_CTRL_BIT_STOP;
2443 //me4000_outl(tmp, ao_context->ctrl_reg);
2448 static int me4000_ao_immediate_stop(me4000_ao_context_t * ao_context)
2451 wait_queue_head_t queue;
2452 unsigned long flags;
2454 init_waitqueue_head(&queue);
2456 CALL_PDEBUG("me4000_ao_immediate_stop() is executed\n");
2458 spin_lock_irqsave(&ao_context->int_lock, flags);
2459 tmp = inl(ao_context->ctrl_reg);
2460 tmp |= ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP;
2461 me4000_outl(tmp, ao_context->ctrl_reg);
2462 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2464 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2465 interruptible_sleep_on_timeout(&queue, 1);
2466 if (signal_pending(current)) {
2468 "me4000_ao_immediate_stop():Wait on state machine after stop interrupted\n");
2473 /* Clear the stop bits */
2474 //tmp &= ~(ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2475 //me4000_outl(tmp, ao_context->ctrl_reg);
2480 static int me4000_ao_timer_set_divisor(u32 * arg,
2481 me4000_ao_context_t * ao_context)
2486 CALL_PDEBUG("me4000_ao_timer set_divisor() is executed\n");
2488 if (get_user(divisor, arg))
2491 /* Check if the state machine is stopped */
2492 tmp = me4000_inl(ao_context->status_reg);
2493 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2495 "me4000_ao_timer_set_divisor():Can't set timer while DAC is running\n");
2499 PDEBUG("me4000_ao_timer set_divisor():Divisor from user = %d\n",
2502 /* Check if the divisor is right. ME4000_AO_MIN_TICKS is the lowest */
2503 if (divisor < ME4000_AO_MIN_TICKS) {
2505 "ME4000:me4000_ao_timer set_divisor():Divisor to low\n");
2509 /* Fix bug in Firmware */
2512 PDEBUG("me4000_ao_timer set_divisor():Divisor to HW = %d\n", divisor);
2514 /* Write the divisor */
2515 me4000_outl(divisor, ao_context->timer_reg);
2520 static int me4000_ao_ex_trig_set_edge(int *arg,
2521 me4000_ao_context_t * ao_context)
2525 unsigned long flags;
2527 CALL_PDEBUG("me4000_ao_ex_trig_set_edge() is executed\n");
2529 if (get_user(mode, arg))
2532 /* Check if the state machine is stopped */
2533 tmp = me4000_inl(ao_context->status_reg);
2534 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2536 "me4000_ao_ex_trig_set_edge():Can't set trigger while DAC is running\n");
2540 if (mode == ME4000_AO_TRIGGER_EXT_EDGE_RISING) {
2541 spin_lock_irqsave(&ao_context->int_lock, flags);
2542 tmp = me4000_inl(ao_context->ctrl_reg);
2544 ~(ME4000_AO_CTRL_BIT_EX_TRIG_EDGE |
2545 ME4000_AO_CTRL_BIT_EX_TRIG_BOTH);
2546 me4000_outl(tmp, ao_context->ctrl_reg);
2547 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2548 } else if (mode == ME4000_AO_TRIGGER_EXT_EDGE_FALLING) {
2549 spin_lock_irqsave(&ao_context->int_lock, flags);
2550 tmp = me4000_inl(ao_context->ctrl_reg);
2551 tmp &= ~ME4000_AO_CTRL_BIT_EX_TRIG_BOTH;
2552 tmp |= ME4000_AO_CTRL_BIT_EX_TRIG_EDGE;
2553 me4000_outl(tmp, ao_context->ctrl_reg);
2554 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2555 } else if (mode == ME4000_AO_TRIGGER_EXT_EDGE_BOTH) {
2556 spin_lock_irqsave(&ao_context->int_lock, flags);
2557 tmp = me4000_inl(ao_context->ctrl_reg);
2559 ME4000_AO_CTRL_BIT_EX_TRIG_EDGE |
2560 ME4000_AO_CTRL_BIT_EX_TRIG_BOTH;
2561 me4000_outl(tmp, ao_context->ctrl_reg);
2562 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2565 "me4000_ao_ex_trig_set_edge():Invalid trigger mode\n");
2572 static int me4000_ao_ex_trig_enable(me4000_ao_context_t * ao_context)
2575 unsigned long flags;
2577 CALL_PDEBUG("me4000_ao_ex_trig_enable() is executed\n");
2579 /* Check if the state machine is stopped */
2580 tmp = me4000_inl(ao_context->status_reg);
2581 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2583 "me4000_ao_ex_trig_enable():Can't enable trigger while DAC is running\n");
2587 spin_lock_irqsave(&ao_context->int_lock, flags);
2588 tmp = me4000_inl(ao_context->ctrl_reg);
2589 tmp |= ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG;
2590 me4000_outl(tmp, ao_context->ctrl_reg);
2591 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2596 static int me4000_ao_ex_trig_disable(me4000_ao_context_t * ao_context)
2599 unsigned long flags;
2601 CALL_PDEBUG("me4000_ao_ex_trig_disable() is executed\n");
2603 /* Check if the state machine is stopped */
2604 tmp = me4000_inl(ao_context->status_reg);
2605 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2607 "me4000_ao_ex_trig_disable():Can't disable trigger while DAC is running\n");
2611 spin_lock_irqsave(&ao_context->int_lock, flags);
2612 tmp = me4000_inl(ao_context->ctrl_reg);
2613 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG;
2614 me4000_outl(tmp, ao_context->ctrl_reg);
2615 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2620 static int me4000_ao_simultaneous_disable(me4000_ao_context_t * ao_context)
2624 CALL_PDEBUG("me4000_ao_simultaneous_disable() is executed\n");
2626 /* Check if the state machine is stopped */
2627 /* Be careful here because this function is called from
2628 me4000_ao_synchronous disable */
2629 tmp = me4000_inl(ao_context->status_reg);
2630 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2632 "me4000_ao_simultaneous_disable():Can't disable while DAC is running\n");
2636 spin_lock(&ao_context->board_info->preload_lock);
2637 tmp = me4000_inl(ao_context->preload_reg);
2638 tmp &= ~(0x1 << ao_context->index); // Disable preload bit
2639 tmp &= ~(0x1 << (ao_context->index + 16)); // Disable hw simultaneous bit
2640 me4000_outl(tmp, ao_context->preload_reg);
2641 spin_unlock(&ao_context->board_info->preload_lock);
2646 static int me4000_ao_simultaneous_ex_trig(me4000_ao_context_t * ao_context)
2650 CALL_PDEBUG("me4000_ao_simultaneous_ex_trig() is executed\n");
2652 spin_lock(&ao_context->board_info->preload_lock);
2653 tmp = me4000_inl(ao_context->preload_reg);
2654 tmp |= (0x1 << ao_context->index); // Enable preload bit
2655 tmp |= (0x1 << (ao_context->index + 16)); // Enable hw simultaneous bit
2656 me4000_outl(tmp, ao_context->preload_reg);
2657 spin_unlock(&ao_context->board_info->preload_lock);
2662 static int me4000_ao_simultaneous_sw(me4000_ao_context_t * ao_context)
2666 CALL_PDEBUG("me4000_ao_simultaneous_sw() is executed\n");
2668 spin_lock(&ao_context->board_info->preload_lock);
2669 tmp = me4000_inl(ao_context->preload_reg);
2670 tmp |= (0x1 << ao_context->index); // Enable preload bit
2671 tmp &= ~(0x1 << (ao_context->index + 16)); // Disable hw simultaneous bit
2672 me4000_outl(tmp, ao_context->preload_reg);
2673 spin_unlock(&ao_context->board_info->preload_lock);
2678 static int me4000_ao_preload(me4000_ao_context_t * ao_context)
2680 CALL_PDEBUG("me4000_ao_preload() is executed\n");
2681 return me4000_ao_simultaneous_sw(ao_context);
2684 static int me4000_ao_preload_update(me4000_ao_context_t * ao_context)
2688 struct list_head *entry;
2690 CALL_PDEBUG("me4000_ao_preload_update() is executed\n");
2692 spin_lock(&ao_context->board_info->preload_lock);
2693 tmp = me4000_inl(ao_context->preload_reg);
2694 list_for_each(entry, &ao_context->board_info->ao_context_list) {
2695 /* The channels we update must be in the following state :
2697 - Hardware trigger is disabled
2698 - Corresponding simultaneous bit is reset
2700 ctrl = me4000_inl(ao_context->ctrl_reg);
2703 (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1 |
2704 ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG))) {
2708 (((me4000_ao_context_t *) entry)->index + 16)))) {
2711 (((me4000_ao_context_t *) entry)->index));
2715 me4000_outl(tmp, ao_context->preload_reg);
2716 spin_unlock(&ao_context->board_info->preload_lock);
2721 static int me4000_ao_simultaneous_update(me4000_ao_channel_list_t * arg,
2722 me4000_ao_context_t * ao_context)
2727 me4000_ao_channel_list_t channels;
2729 CALL_PDEBUG("me4000_ao_simultaneous_update() is executed\n");
2731 /* Copy data from user */
2732 err = copy_from_user(&channels, arg, sizeof(me4000_ao_channel_list_t));
2735 "ME4000:me4000_ao_simultaneous_update():Can't copy command\n");
2740 kmalloc(sizeof(unsigned long) * channels.count, GFP_KERNEL);
2741 if (!channels.list) {
2743 "ME4000:me4000_ao_simultaneous_update():Can't get buffer\n");
2746 memset(channels.list, 0, sizeof(unsigned long) * channels.count);
2748 /* Copy channel list from user */
2750 copy_from_user(channels.list, arg->list,
2751 sizeof(unsigned long) * channels.count);
2754 "ME4000:me4000_ao_simultaneous_update():Can't copy list\n");
2755 kfree(channels.list);
2759 spin_lock(&ao_context->board_info->preload_lock);
2760 tmp = me4000_inl(ao_context->preload_reg);
2761 for (i = 0; i < channels.count; i++) {
2762 if (channels.list[i] >
2763 ao_context->board_info->board_p->ao.count) {
2764 spin_unlock(&ao_context->board_info->preload_lock);
2765 kfree(channels.list);
2767 "ME4000:me4000_ao_simultaneous_update():Invalid board number specified\n");
2770 tmp &= ~(0x1 << channels.list[i]); // Clear the preload bit
2771 tmp &= ~(0x1 << (channels.list[i] + 16)); // Clear the hw simultaneous bit
2773 me4000_outl(tmp, ao_context->preload_reg);
2774 spin_unlock(&ao_context->board_info->preload_lock);
2775 kfree(channels.list);
2780 static int me4000_ao_synchronous_ex_trig(me4000_ao_context_t * ao_context)
2783 unsigned long flags;
2785 CALL_PDEBUG("me4000_ao_synchronous_ex_trig() is executed\n");
2787 /* Check if the state machine is stopped */
2788 tmp = me4000_inl(ao_context->status_reg);
2789 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2791 "me4000_ao_synchronous_ex_trig(): DAC is running\n");
2795 spin_lock(&ao_context->board_info->preload_lock);
2796 tmp = me4000_inl(ao_context->preload_reg);
2797 tmp &= ~(0x1 << ao_context->index); // Disable synchronous sw bit
2798 tmp |= 0x1 << (ao_context->index + 16); // Enable synchronous hw bit
2799 me4000_outl(tmp, ao_context->preload_reg);
2800 spin_unlock(&ao_context->board_info->preload_lock);
2803 spin_lock_irqsave(&ao_context->int_lock, flags);
2804 tmp = me4000_inl(ao_context->ctrl_reg);
2805 if (tmp & (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1)) {
2807 ~(ME4000_AO_CTRL_BIT_STOP |
2808 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2809 me4000_outl(tmp, ao_context->ctrl_reg);
2811 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2816 static int me4000_ao_synchronous_sw(me4000_ao_context_t * ao_context)
2819 unsigned long flags;
2821 CALL_PDEBUG("me4000_ao_synchronous_sw() is executed\n");
2823 /* Check if the state machine is stopped */
2824 tmp = me4000_inl(ao_context->status_reg);
2825 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2826 printk(KERN_ERR "me4000_ao_synchronous_sw(): DAC is running\n");
2830 spin_lock(&ao_context->board_info->preload_lock);
2831 tmp = me4000_inl(ao_context->preload_reg);
2832 tmp |= 0x1 << ao_context->index; // Enable synchronous sw bit
2833 tmp &= ~(0x1 << (ao_context->index + 16)); // Disable synchronous hw bit
2834 me4000_outl(tmp, ao_context->preload_reg);
2835 spin_unlock(&ao_context->board_info->preload_lock);
2838 spin_lock_irqsave(&ao_context->int_lock, flags);
2839 tmp = me4000_inl(ao_context->ctrl_reg);
2840 if (tmp & (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1)) {
2842 ~(ME4000_AO_CTRL_BIT_STOP |
2843 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2844 me4000_outl(tmp, ao_context->ctrl_reg);
2846 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2851 static int me4000_ao_synchronous_disable(me4000_ao_context_t * ao_context)
2853 return me4000_ao_simultaneous_disable(ao_context);
2856 static int me4000_ao_get_free_buffer(unsigned long *arg,
2857 me4000_ao_context_t * ao_context)
2862 c = me4000_buf_space(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT);
2864 err = copy_to_user(arg, &c, sizeof(unsigned long));
2867 "ME4000:me4000_ao_get_free_buffer():Can't copy to user space\n");
2874 static int me4000_ao_ex_trig_timeout(unsigned long *arg,
2875 me4000_ao_context_t * ao_context)
2878 wait_queue_head_t queue;
2880 unsigned long timeout;
2882 CALL_PDEBUG("me4000_ao_ex_trig_timeout() is executed\n");
2884 if (get_user(timeout, arg)) {
2886 "me4000_ao_ex_trig_timeout():Cannot copy data from user\n");
2890 init_waitqueue_head(&queue);
2892 tmp = inl(ao_context->ctrl_reg);
2894 if ((tmp & ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG)) {
2897 while ((inl(ao_context->status_reg) &
2898 ME4000_AO_STATUS_BIT_FSM)) {
2899 interruptible_sleep_on_timeout(&queue, 1);
2900 if (signal_pending(current)) {
2902 "ME4000:me4000_ao_ex_trig_timeout():Wait on start of state machine interrupted\n");
2905 if (((jiffies - ref) > (timeout * HZ / USER_HZ))) { // 2.6 has diffrent definitions for HZ in user and kernel space
2907 "ME4000:me4000_ao_ex_trig_timeout():Timeout reached\n");
2912 while ((inl(ao_context->status_reg) &
2913 ME4000_AO_STATUS_BIT_FSM)) {
2914 interruptible_sleep_on_timeout(&queue, 1);
2915 if (signal_pending(current)) {
2917 "ME4000:me4000_ao_ex_trig_timeout():Wait on start of state machine interrupted\n");
2924 "ME4000:me4000_ao_ex_trig_timeout():External Trigger is not enabled\n");
2931 static int me4000_ao_enable_do(me4000_ao_context_t * ao_context)
2934 unsigned long flags;
2936 CALL_PDEBUG("me4000_ao_enable_do() is executed\n");
2938 /* Only available for analog output 3 */
2939 if (ao_context->index != 3) {
2941 "me4000_ao_enable_do():Only available for analog output 3\n");
2945 /* Check if the state machine is stopped */
2946 tmp = me4000_inl(ao_context->status_reg);
2947 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2948 printk(KERN_ERR "me4000_ao_enable_do(): DAC is running\n");
2952 /* Set the stop bit */
2953 spin_lock_irqsave(&ao_context->int_lock, flags);
2954 tmp = inl(ao_context->ctrl_reg);
2955 tmp |= ME4000_AO_CTRL_BIT_ENABLE_DO;
2956 me4000_outl(tmp, ao_context->ctrl_reg);
2957 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2962 static int me4000_ao_disable_do(me4000_ao_context_t * ao_context)
2965 unsigned long flags;
2967 CALL_PDEBUG("me4000_ao_disable_do() is executed\n");
2969 /* Only available for analog output 3 */
2970 if (ao_context->index != 3) {
2972 "me4000_ao_disable():Only available for analog output 3\n");
2976 /* Check if the state machine is stopped */
2977 tmp = me4000_inl(ao_context->status_reg);
2978 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2979 printk(KERN_ERR "me4000_ao_disable_do(): DAC is running\n");
2983 spin_lock_irqsave(&ao_context->int_lock, flags);
2984 tmp = inl(ao_context->ctrl_reg);
2985 tmp &= ~(ME4000_AO_CTRL_BIT_ENABLE_DO);
2986 me4000_outl(tmp, ao_context->ctrl_reg);
2987 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2992 static int me4000_ao_fsm_state(int *arg, me4000_ao_context_t * ao_context)
2996 CALL_PDEBUG("me4000_ao_fsm_state() is executed\n");
2999 (me4000_inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) ? 1
3002 if (ao_context->pipe_flag) {
3003 printk(KERN_ERR "me4000_ao_fsm_state():Broken pipe detected\n");
3007 if (put_user(tmp, arg)) {
3008 printk(KERN_ERR "me4000_ao_fsm_state():Cannot copy to user\n");
3015 /*------------------------------- Analog input stuff --------------------------------------*/
3017 static int me4000_ai_prepare(me4000_ai_context_t * ai_context)
3019 wait_queue_head_t queue;
3022 CALL_PDEBUG("me4000_ai_prepare() is executed\n");
3024 init_waitqueue_head(&queue);
3026 /* Set the new mode and stop bits */
3027 me4000_outl(ai_context->
3028 mode | ME4000_AI_CTRL_BIT_STOP |
3029 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP, ai_context->ctrl_reg);
3031 /* Set the timer registers */
3032 ai_context->chan_timer = 66;
3033 ai_context->chan_pre_timer = 66;
3034 ai_context->scan_timer_low = 0;
3035 ai_context->scan_timer_high = 0;
3037 me4000_outl(65, ai_context->chan_timer_reg);
3038 me4000_outl(65, ai_context->chan_pre_timer_reg);
3039 me4000_outl(0, ai_context->scan_timer_low_reg);
3040 me4000_outl(0, ai_context->scan_timer_high_reg);
3041 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3042 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3044 ai_context->channel_list_count = 0;
3046 if (ai_context->mode) {
3047 /* Request the interrupt line */
3049 request_irq(ai_context->irq, me4000_ai_isr,
3050 IRQF_DISABLED | IRQF_SHARED, ME4000_NAME,
3054 "ME4000:me4000_ai_prepare():Can't get interrupt line");
3058 /* Allocate circular buffer */
3059 ai_context->circ_buf.buf =
3060 kmalloc(ME4000_AI_BUFFER_SIZE, GFP_KERNEL);
3061 if (!ai_context->circ_buf.buf) {
3063 "ME4000:me4000_ai_prepare():Can't get circular buffer\n");
3064 free_irq(ai_context->irq, ai_context);
3067 memset(ai_context->circ_buf.buf, 0, ME4000_AI_BUFFER_SIZE);
3069 /* Clear the circular buffer */
3070 ai_context->circ_buf.head = 0;
3071 ai_context->circ_buf.tail = 0;
3077 static int me4000_ai_reset(me4000_ai_context_t * ai_context)
3079 wait_queue_head_t queue;
3081 unsigned long flags;
3083 CALL_PDEBUG("me4000_ai_reset() is executed\n");
3085 init_waitqueue_head(&queue);
3088 * First stop conversion of the state machine before reconfigure.
3089 * If not stopped before configuring mode, it could
3090 * walk in a undefined state.
3092 spin_lock_irqsave(&ai_context->int_lock, flags);
3093 tmp = me4000_inl(ai_context->ctrl_reg);
3094 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
3095 me4000_outl(tmp, ai_context->ctrl_reg);
3096 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3098 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3099 interruptible_sleep_on_timeout(&queue, 1);
3100 if (signal_pending(current)) {
3102 "me4000_ai_reset():Wait on state machine after stop interrupted\n");
3107 /* Clear the control register and set the stop bits */
3108 spin_lock_irqsave(&ai_context->int_lock, flags);
3109 tmp = me4000_inl(ai_context->ctrl_reg);
3110 me4000_outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP | ME4000_AI_CTRL_BIT_STOP,
3111 ai_context->ctrl_reg);
3112 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3114 /* Reset timer registers */
3115 ai_context->chan_timer = 66;
3116 ai_context->chan_pre_timer = 66;
3117 ai_context->scan_timer_low = 0;
3118 ai_context->scan_timer_high = 0;
3119 ai_context->sample_counter = 0;
3120 ai_context->sample_counter_reload = 0;
3122 me4000_outl(65, ai_context->chan_timer_reg);
3123 me4000_outl(65, ai_context->chan_pre_timer_reg);
3124 me4000_outl(0, ai_context->scan_timer_low_reg);
3125 me4000_outl(0, ai_context->scan_timer_high_reg);
3126 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3127 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3128 me4000_outl(0, ai_context->sample_counter_reg);
3130 ai_context->channel_list_count = 0;
3132 /* Clear the circular buffer */
3133 ai_context->circ_buf.head = 0;
3134 ai_context->circ_buf.tail = 0;
3139 static int me4000_ai_ioctl_sing(struct inode *inode_p, struct file *file_p,
3140 unsigned int service, unsigned long arg)
3142 me4000_ai_context_t *ai_context;
3144 CALL_PDEBUG("me4000_ai_ioctl_sing() is executed\n");
3146 ai_context = file_p->private_data;
3148 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3149 printk(KERN_ERR "me4000_ai_ioctl_sing():Wrong magic number\n");
3152 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3154 "me4000_ai_ioctl_sing():Service number to high\n");
3159 case ME4000_AI_SINGLE:
3160 return me4000_ai_single((me4000_ai_single_t *) arg, ai_context);
3161 case ME4000_AI_EX_TRIG_ENABLE:
3162 return me4000_ai_ex_trig_enable(ai_context);
3163 case ME4000_AI_EX_TRIG_DISABLE:
3164 return me4000_ai_ex_trig_disable(ai_context);
3165 case ME4000_AI_EX_TRIG_SETUP:
3166 return me4000_ai_ex_trig_setup((me4000_ai_trigger_t *) arg,
3168 case ME4000_GET_USER_INFO:
3169 return me4000_get_user_info((me4000_user_info_t *) arg,
3170 ai_context->board_info);
3171 case ME4000_AI_OFFSET_ENABLE:
3172 return me4000_ai_offset_enable(ai_context);
3173 case ME4000_AI_OFFSET_DISABLE:
3174 return me4000_ai_offset_disable(ai_context);
3175 case ME4000_AI_FULLSCALE_ENABLE:
3176 return me4000_ai_fullscale_enable(ai_context);
3177 case ME4000_AI_FULLSCALE_DISABLE:
3178 return me4000_ai_fullscale_disable(ai_context);
3179 case ME4000_AI_EEPROM_READ:
3180 return me4000_eeprom_read((me4000_eeprom_t *) arg, ai_context);
3181 case ME4000_AI_EEPROM_WRITE:
3182 return me4000_eeprom_write((me4000_eeprom_t *) arg, ai_context);
3185 "me4000_ai_ioctl_sing():Invalid service number\n");
3191 static int me4000_ai_single(me4000_ai_single_t * arg,
3192 me4000_ai_context_t * ai_context)
3194 me4000_ai_single_t cmd;
3197 wait_queue_head_t queue;
3198 unsigned long jiffy;
3200 CALL_PDEBUG("me4000_ai_single() is executed\n");
3202 init_waitqueue_head(&queue);
3204 /* Copy data from user */
3205 err = copy_from_user(&cmd, arg, sizeof(me4000_ai_single_t));
3208 "ME4000:me4000_ai_single():Can't copy from user space\n");
3212 /* Check range parameter */
3213 switch (cmd.range) {
3214 case ME4000_AI_LIST_RANGE_BIPOLAR_10:
3215 case ME4000_AI_LIST_RANGE_BIPOLAR_2_5:
3216 case ME4000_AI_LIST_RANGE_UNIPOLAR_10:
3217 case ME4000_AI_LIST_RANGE_UNIPOLAR_2_5:
3221 "ME4000:me4000_ai_single():Invalid range specified\n");
3225 /* Check mode and channel number */
3227 case ME4000_AI_LIST_INPUT_SINGLE_ENDED:
3228 if (cmd.channel >= ai_context->board_info->board_p->ai.count) {
3230 "ME4000:me4000_ai_single():Analog input is not available\n");
3234 case ME4000_AI_LIST_INPUT_DIFFERENTIAL:
3236 ai_context->board_info->board_p->ai.diff_count) {
3238 "ME4000:me4000_ai_single():Analog input is not available in differential mode\n");
3244 "ME4000:me4000_ai_single():Invalid mode specified\n");
3248 /* Clear channel list, data fifo and both stop bits */
3249 tmp = me4000_inl(ai_context->ctrl_reg);
3251 ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_DATA_FIFO |
3252 ME4000_AI_CTRL_BIT_STOP | ME4000_AI_CTRL_BIT_IMMEDIATE_STOP);
3253 me4000_outl(tmp, ai_context->ctrl_reg);
3255 /* Enable channel list and data fifo */
3256 tmp |= ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_DATA_FIFO;
3257 me4000_outl(tmp, ai_context->ctrl_reg);
3259 /* Generate channel list entry */
3260 me4000_outl(cmd.channel | cmd.range | cmd.
3261 mode | ME4000_AI_LIST_LAST_ENTRY,
3262 ai_context->channel_list_reg);
3264 /* Set the timer to maximum */
3265 me4000_outl(66, ai_context->chan_timer_reg);
3266 me4000_outl(66, ai_context->chan_pre_timer_reg);
3268 if (tmp & ME4000_AI_CTRL_BIT_EX_TRIG) {
3271 (me4000_inl(ai_context->status_reg) &
3272 ME4000_AI_STATUS_BIT_EF_DATA)) {
3273 interruptible_sleep_on_timeout(&queue, 1);
3274 if (signal_pending(current)) {
3276 "ME4000:me4000_ai_single():Wait on start of state machine interrupted\n");
3279 if (((jiffies - jiffy) > (cmd.timeout * HZ / USER_HZ)) && cmd.timeout) { // 2.6 has diffrent definitions for HZ in user and kernel space
3281 "ME4000:me4000_ai_single():Timeout reached\n");
3286 /* Start conversion */
3287 me4000_inl(ai_context->start_reg);
3289 /* Wait until ready */
3292 (me4000_inl(ai_context->status_reg) &
3293 ME4000_AI_STATUS_BIT_EF_DATA)) {
3295 "ME4000:me4000_ai_single():Value not available after wait\n");
3300 /* Read value from data fifo */
3301 cmd.value = me4000_inl(ai_context->data_reg) & 0xFFFF;
3303 /* Copy result back to user */
3304 err = copy_to_user(arg, &cmd, sizeof(me4000_ai_single_t));
3307 "ME4000:me4000_ai_single():Can't copy to user space\n");
3314 static int me4000_ai_ioctl_sw(struct inode *inode_p, struct file *file_p,
3315 unsigned int service, unsigned long arg)
3317 me4000_ai_context_t *ai_context;
3319 CALL_PDEBUG("me4000_ai_ioctl_sw() is executed\n");
3321 ai_context = file_p->private_data;
3323 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3324 printk(KERN_ERR "me4000_ai_ioctl_sw():Wrong magic number\n");
3327 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3329 "me4000_ai_ioctl_sw():Service number to high\n");
3334 case ME4000_AI_SC_SETUP:
3335 return me4000_ai_sc_setup((me4000_ai_sc_t *) arg, ai_context);
3336 case ME4000_AI_CONFIG:
3337 return me4000_ai_config((me4000_ai_config_t *) arg, ai_context);
3338 case ME4000_AI_START:
3339 return me4000_ai_start(ai_context);
3340 case ME4000_AI_STOP:
3341 return me4000_ai_stop(ai_context);
3342 case ME4000_AI_IMMEDIATE_STOP:
3343 return me4000_ai_immediate_stop(ai_context);
3344 case ME4000_AI_FSM_STATE:
3345 return me4000_ai_fsm_state((int *)arg, ai_context);
3346 case ME4000_GET_USER_INFO:
3347 return me4000_get_user_info((me4000_user_info_t *) arg,
3348 ai_context->board_info);
3349 case ME4000_AI_EEPROM_READ:
3350 return me4000_eeprom_read((me4000_eeprom_t *) arg, ai_context);
3351 case ME4000_AI_EEPROM_WRITE:
3352 return me4000_eeprom_write((me4000_eeprom_t *) arg, ai_context);
3353 case ME4000_AI_GET_COUNT_BUFFER:
3354 return me4000_ai_get_count_buffer((unsigned long *)arg,
3358 "ME4000:me4000_ai_ioctl_sw():Invalid service number %d\n",
3365 static int me4000_ai_ioctl_ext(struct inode *inode_p, struct file *file_p,
3366 unsigned int service, unsigned long arg)
3368 me4000_ai_context_t *ai_context;
3370 CALL_PDEBUG("me4000_ai_ioctl_ext() is executed\n");
3372 ai_context = file_p->private_data;
3374 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3375 printk(KERN_ERR "me4000_ai_ioctl_ext():Wrong magic number\n");
3378 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3380 "me4000_ai_ioctl_ext():Service number to high\n");
3385 case ME4000_AI_SC_SETUP:
3386 return me4000_ai_sc_setup((me4000_ai_sc_t *) arg, ai_context);
3387 case ME4000_AI_CONFIG:
3388 return me4000_ai_config((me4000_ai_config_t *) arg, ai_context);
3389 case ME4000_AI_START:
3390 return me4000_ai_start_ex((unsigned long *)arg, ai_context);
3391 case ME4000_AI_STOP:
3392 return me4000_ai_stop(ai_context);
3393 case ME4000_AI_IMMEDIATE_STOP:
3394 return me4000_ai_immediate_stop(ai_context);
3395 case ME4000_AI_EX_TRIG_ENABLE:
3396 return me4000_ai_ex_trig_enable(ai_context);
3397 case ME4000_AI_EX_TRIG_DISABLE:
3398 return me4000_ai_ex_trig_disable(ai_context);
3399 case ME4000_AI_EX_TRIG_SETUP:
3400 return me4000_ai_ex_trig_setup((me4000_ai_trigger_t *) arg,
3402 case ME4000_AI_FSM_STATE:
3403 return me4000_ai_fsm_state((int *)arg, ai_context);
3404 case ME4000_GET_USER_INFO:
3405 return me4000_get_user_info((me4000_user_info_t *) arg,
3406 ai_context->board_info);
3407 case ME4000_AI_GET_COUNT_BUFFER:
3408 return me4000_ai_get_count_buffer((unsigned long *)arg,
3412 "ME4000:me4000_ai_ioctl_ext():Invalid service number %d\n",
3419 static int me4000_ai_fasync(int fd, struct file *file_p, int mode)
3421 me4000_ai_context_t *ai_context;
3423 CALL_PDEBUG("me4000_ao_fasync_cont() is executed\n");
3425 ai_context = file_p->private_data;
3426 return fasync_helper(fd, file_p, mode, &ai_context->fasync_p);
3429 static int me4000_ai_config(me4000_ai_config_t * arg,
3430 me4000_ai_context_t * ai_context)
3432 me4000_ai_config_t cmd;
3437 wait_queue_head_t queue;
3441 CALL_PDEBUG("me4000_ai_config() is executed\n");
3443 init_waitqueue_head(&queue);
3445 /* Check if conversion is stopped */
3446 if (inl(ai_context->ctrl_reg) & ME4000_AI_STATUS_BIT_FSM) {
3448 "ME4000:me4000_ai_config():Conversion is not stopped\n");
3453 /* Copy data from user */
3454 err = copy_from_user(&cmd, arg, sizeof(me4000_ai_config_t));
3457 "ME4000:me4000_ai_config():Can't copy from user space\n");
3463 ("me4000_ai_config():chan = %ld, pre_chan = %ld, scan_low = %ld, scan_high = %ld, count = %ld\n",
3464 cmd.timer.chan, cmd.timer.pre_chan, cmd.timer.scan_low,
3465 cmd.timer.scan_high, cmd.channel_list.count);
3467 /* Check whether sample and hold is available for this board */
3469 if (!ai_context->board_info->board_p->ai.sh_count) {
3471 "ME4000:me4000_ai_config():Sample and Hold is not available for this board\n");
3477 /* Check the channel list size */
3478 if (cmd.channel_list.count > ME4000_AI_CHANNEL_LIST_COUNT) {
3480 "me4000_ai_config():Channel list is to large\n");
3485 /* Copy channel list from user */
3486 list = kmalloc(sizeof(u32) * cmd.channel_list.count, GFP_KERNEL);
3489 "ME4000:me4000_ai_config():Can't get memory for channel list\n");
3494 copy_from_user(list, cmd.channel_list.list,
3495 sizeof(u32) * cmd.channel_list.count);
3498 "ME4000:me4000_ai_config():Can't copy from user space\n");
3503 /* Check if last entry bit is set */
3504 if (!(list[cmd.channel_list.count - 1] & ME4000_AI_LIST_LAST_ENTRY)) {
3506 "me4000_ai_config():Last entry bit is not set\n");
3507 list[cmd.channel_list.count - 1] |= ME4000_AI_LIST_LAST_ENTRY;
3510 /* Check whether mode is equal for all entries */
3511 mode = list[0] & 0x20;
3512 for (i = 0; i < cmd.channel_list.count; i++) {
3513 if ((list[i] & 0x20) != mode) {
3515 "ME4000:me4000_ai_config():Mode is not equal for all entries\n");
3521 /* Check whether channels are available for this mode */
3522 if (mode == ME4000_AI_LIST_INPUT_SINGLE_ENDED) {
3523 for (i = 0; i < cmd.channel_list.count; i++) {
3524 if ((list[i] & 0x1F) >=
3525 ai_context->board_info->board_p->ai.count) {
3527 "ME4000:me4000_ai_config():Channel is not available for single ended\n");
3532 } else if (mode == ME4000_AI_LIST_INPUT_DIFFERENTIAL) {
3533 for (i = 0; i < cmd.channel_list.count; i++) {
3534 if ((list[i] & 0x1F) >=
3535 ai_context->board_info->board_p->ai.diff_count) {
3537 "ME4000:me4000_ai_config():Channel is not available for differential\n");
3544 /* Check if bipolar is set for all entries when in differential mode */
3545 if (mode == ME4000_AI_LIST_INPUT_DIFFERENTIAL) {
3546 for (i = 0; i < cmd.channel_list.count; i++) {
3547 if ((list[i] & 0xC0) != ME4000_AI_LIST_RANGE_BIPOLAR_10
3548 && (list[i] & 0xC0) !=
3549 ME4000_AI_LIST_RANGE_BIPOLAR_2_5) {
3551 "ME4000:me4000_ai_config():Bipolar is not selected in differential mode\n");
3558 if (ai_context->mode != ME4000_AI_ACQ_MODE_EXT_SINGLE_VALUE) {
3559 /* Check for minimum channel divisor */
3560 if (cmd.timer.chan < ME4000_AI_MIN_TICKS) {
3562 "ME4000:me4000_ai_config():Channel timer divisor is to low\n");
3567 /* Check if minimum channel divisor is adjusted when sample and hold is activated */
3568 if ((cmd.sh) && (cmd.timer.chan != ME4000_AI_MIN_TICKS)) {
3570 "ME4000:me4000_ai_config():Channel timer divisor must be at minimum when sample and hold is activated\n");
3575 /* Check for minimum channel pre divisor */
3576 if (cmd.timer.pre_chan < ME4000_AI_MIN_TICKS) {
3578 "ME4000:me4000_ai_config():Channel pre timer divisor is to low\n");
3583 /* Write the channel timers */
3584 me4000_outl(cmd.timer.chan - 1, ai_context->chan_timer_reg);
3585 me4000_outl(cmd.timer.pre_chan - 1,
3586 ai_context->chan_pre_timer_reg);
3588 /* Save the timer values in the board context */
3589 ai_context->chan_timer = cmd.timer.chan;
3590 ai_context->chan_pre_timer = cmd.timer.pre_chan;
3592 if (ai_context->mode != ME4000_AI_ACQ_MODE_EXT_SINGLE_CHANLIST) {
3593 /* Check for scan timer divisor */
3595 (u64) cmd.timer.scan_low | ((u64) cmd.timer.
3599 cmd.channel_list.count * cmd.timer.chan +
3602 "ME4000:me4000_ai_config():Scan timer divisor is to low\n");
3608 /* Write the scan timers */
3611 tmp = (u32) (scan & 0xFFFFFFFF);
3613 ai_context->scan_timer_low_reg);
3614 tmp = (u32) ((scan >> 32) & 0xFFFFFFFF);
3616 ai_context->scan_timer_high_reg);
3619 scan - (cmd.timer.chan - 1) +
3620 (cmd.timer.pre_chan - 1);
3621 tmp = (u32) (scan & 0xFFFFFFFF);
3623 ai_context->scan_pre_timer_low_reg);
3624 tmp = (u32) ((scan >> 32) & 0xFFFFFFFF);
3627 scan_pre_timer_high_reg);
3630 ai_context->scan_timer_low_reg);
3632 ai_context->scan_timer_high_reg);
3635 ai_context->scan_pre_timer_low_reg);
3638 scan_pre_timer_high_reg);
3641 ai_context->scan_timer_low = cmd.timer.scan_low;
3642 ai_context->scan_timer_high = cmd.timer.scan_high;
3646 /* Clear the channel list */
3647 tmp = me4000_inl(ai_context->ctrl_reg);
3648 tmp &= ~ME4000_AI_CTRL_BIT_CHANNEL_FIFO;
3649 me4000_outl(tmp, ai_context->ctrl_reg);
3650 tmp |= ME4000_AI_CTRL_BIT_CHANNEL_FIFO;
3651 me4000_outl(tmp, ai_context->ctrl_reg);
3653 /* Write the channel list */
3654 for (i = 0; i < cmd.channel_list.count; i++) {
3655 me4000_outl(list[i], ai_context->channel_list_reg);
3658 /* Setup sample and hold */
3660 tmp |= ME4000_AI_CTRL_BIT_SAMPLE_HOLD;
3661 me4000_outl(tmp, ai_context->ctrl_reg);
3663 tmp &= ~ME4000_AI_CTRL_BIT_SAMPLE_HOLD;
3664 me4000_outl(tmp, ai_context->ctrl_reg);
3667 /* Save the channel list size in the board context */
3668 ai_context->channel_list_count = cmd.channel_list.count;
3676 /* Reset the timers */
3677 ai_context->chan_timer = 66;
3678 ai_context->chan_pre_timer = 66;
3679 ai_context->scan_timer_low = 0;
3680 ai_context->scan_timer_high = 0;
3682 me4000_outl(65, ai_context->chan_timer_reg);
3683 me4000_outl(65, ai_context->chan_pre_timer_reg);
3684 me4000_outl(0, ai_context->scan_timer_high_reg);
3685 me4000_outl(0, ai_context->scan_timer_low_reg);
3686 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3687 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3689 ai_context->channel_list_count = 0;
3691 tmp = me4000_inl(ai_context->ctrl_reg);
3693 ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_SAMPLE_HOLD);
3702 static int ai_common_start(me4000_ai_context_t * ai_context)
3705 CALL_PDEBUG("ai_common_start() is executed\n");
3707 tmp = me4000_inl(ai_context->ctrl_reg);
3709 /* Check if conversion is stopped */
3710 if (tmp & ME4000_AI_STATUS_BIT_FSM) {
3712 "ME4000:ai_common_start():Conversion is not stopped\n");
3716 /* Clear data fifo, disable all interrupts, clear sample counter reload */
3717 tmp &= ~(ME4000_AI_CTRL_BIT_DATA_FIFO | ME4000_AI_CTRL_BIT_LE_IRQ |
3718 ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3719 ME4000_AI_CTRL_BIT_SC_RELOAD);
3721 me4000_outl(tmp, ai_context->ctrl_reg);
3723 /* Clear circular buffer */
3724 ai_context->circ_buf.head = 0;
3725 ai_context->circ_buf.tail = 0;
3727 /* Enable data fifo */
3728 tmp |= ME4000_AI_CTRL_BIT_DATA_FIFO;
3730 /* Determine interrupt setup */
3731 if (ai_context->sample_counter && !ai_context->sample_counter_reload) {
3732 /* Enable Half Full Interrupt and Sample Counter Interrupt */
3733 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ | ME4000_AI_CTRL_BIT_HF_IRQ;
3734 } else if (ai_context->sample_counter
3735 && ai_context->sample_counter_reload) {
3736 if (ai_context->sample_counter <= ME4000_AI_FIFO_COUNT / 2) {
3737 /* Enable only Sample Counter Interrupt */
3739 ME4000_AI_CTRL_BIT_SC_IRQ |
3740 ME4000_AI_CTRL_BIT_SC_RELOAD;
3742 /* Enable Half Full Interrupt and Sample Counter Interrupt */
3744 ME4000_AI_CTRL_BIT_SC_IRQ |
3745 ME4000_AI_CTRL_BIT_HF_IRQ |
3746 ME4000_AI_CTRL_BIT_SC_RELOAD;
3749 /* Enable only Half Full Interrupt */
3750 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ;
3753 /* Clear the stop bits */
3754 tmp &= ~(ME4000_AI_CTRL_BIT_STOP | ME4000_AI_CTRL_BIT_IMMEDIATE_STOP);
3756 /* Write setup to hardware */
3757 me4000_outl(tmp, ai_context->ctrl_reg);
3759 /* Write sample counter */
3760 me4000_outl(ai_context->sample_counter, ai_context->sample_counter_reg);
3765 static int me4000_ai_start(me4000_ai_context_t * ai_context)
3768 CALL_PDEBUG("me4000_ai_start() is executed\n");
3770 /* Prepare Hardware */
3771 err = ai_common_start(ai_context);
3775 /* Start conversion by dummy read */
3776 me4000_inl(ai_context->start_reg);
3781 static int me4000_ai_start_ex(unsigned long *arg,
3782 me4000_ai_context_t * ai_context)
3785 wait_queue_head_t queue;
3787 unsigned long timeout;
3789 CALL_PDEBUG("me4000_ai_start_ex() is executed\n");
3791 if (get_user(timeout, arg)) {
3793 "me4000_ai_start_ex():Cannot copy data from user\n");
3797 init_waitqueue_head(&queue);
3799 /* Prepare Hardware */
3800 err = ai_common_start(ai_context);
3807 (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM))
3809 interruptible_sleep_on_timeout(&queue, 1);
3810 if (signal_pending(current)) {
3812 "ME4000:me4000_ai_start_ex():Wait on start of state machine interrupted\n");
3815 if (((jiffies - ref) > (timeout * HZ / USER_HZ))) { // 2.6 has diffrent definitions for HZ in user and kernel space
3817 "ME4000:me4000_ai_start_ex():Timeout reached\n");
3823 (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM))
3825 interruptible_sleep_on_timeout(&queue, 1);
3826 if (signal_pending(current)) {
3828 "ME4000:me4000_ai_start_ex():Wait on start of state machine interrupted\n");
3837 static int me4000_ai_stop(me4000_ai_context_t * ai_context)
3839 wait_queue_head_t queue;
3841 unsigned long flags;
3843 CALL_PDEBUG("me4000_ai_stop() is executed\n");
3845 init_waitqueue_head(&queue);
3847 /* Disable irqs and clear data fifo */
3848 spin_lock_irqsave(&ai_context->int_lock, flags);
3849 tmp = me4000_inl(ai_context->ctrl_reg);
3851 ~(ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3852 ME4000_AI_CTRL_BIT_DATA_FIFO);
3853 /* Stop conversion of the state machine */
3854 tmp |= ME4000_AI_CTRL_BIT_STOP;
3855 me4000_outl(tmp, ai_context->ctrl_reg);
3856 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3858 /* Clear circular buffer */
3859 ai_context->circ_buf.head = 0;
3860 ai_context->circ_buf.tail = 0;
3862 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3863 interruptible_sleep_on_timeout(&queue, 1);
3864 if (signal_pending(current)) {
3866 "ME4000:me4000_ai_stop():Wait on state machine after stop interrupted\n");
3874 static int me4000_ai_immediate_stop(me4000_ai_context_t * ai_context)
3876 wait_queue_head_t queue;
3878 unsigned long flags;
3880 CALL_PDEBUG("me4000_ai_stop() is executed\n");
3882 init_waitqueue_head(&queue);
3884 /* Disable irqs and clear data fifo */
3885 spin_lock_irqsave(&ai_context->int_lock, flags);
3886 tmp = me4000_inl(ai_context->ctrl_reg);
3888 ~(ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3889 ME4000_AI_CTRL_BIT_DATA_FIFO);
3890 /* Stop conversion of the state machine */
3891 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
3892 me4000_outl(tmp, ai_context->ctrl_reg);
3893 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3895 /* Clear circular buffer */
3896 ai_context->circ_buf.head = 0;
3897 ai_context->circ_buf.tail = 0;
3899 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3900 interruptible_sleep_on_timeout(&queue, 1);
3901 if (signal_pending(current)) {
3903 "ME4000:me4000_ai_stop():Wait on state machine after stop interrupted\n");
3911 static int me4000_ai_ex_trig_enable(me4000_ai_context_t * ai_context)
3914 unsigned long flags;
3916 CALL_PDEBUG("me4000_ai_ex_trig_enable() is executed\n");
3918 spin_lock_irqsave(&ai_context->int_lock, flags);
3919 tmp = me4000_inl(ai_context->ctrl_reg);
3920 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG;
3921 me4000_outl(tmp, ai_context->ctrl_reg);
3922 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3927 static int me4000_ai_ex_trig_disable(me4000_ai_context_t * ai_context)
3930 unsigned long flags;
3932 CALL_PDEBUG("me4000_ai_ex_trig_disable() is executed\n");
3934 spin_lock_irqsave(&ai_context->int_lock, flags);
3935 tmp = me4000_inl(ai_context->ctrl_reg);
3936 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG;
3937 me4000_outl(tmp, ai_context->ctrl_reg);
3938 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3943 static int me4000_ai_ex_trig_setup(me4000_ai_trigger_t * arg,
3944 me4000_ai_context_t * ai_context)
3946 me4000_ai_trigger_t cmd;
3949 unsigned long flags;
3951 CALL_PDEBUG("me4000_ai_ex_trig_setup() is executed\n");
3953 /* Copy data from user */
3954 err = copy_from_user(&cmd, arg, sizeof(me4000_ai_trigger_t));
3957 "ME4000:me4000_ai_ex_trig_setup():Can't copy from user space\n");
3961 spin_lock_irqsave(&ai_context->int_lock, flags);
3962 tmp = me4000_inl(ai_context->ctrl_reg);
3964 if (cmd.mode == ME4000_AI_TRIGGER_EXT_DIGITAL) {
3965 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG;
3966 } else if (cmd.mode == ME4000_AI_TRIGGER_EXT_ANALOG) {
3967 if (!ai_context->board_info->board_p->ai.ex_trig_analog) {
3969 "ME4000:me4000_ai_ex_trig_setup():No analog trigger available\n");
3972 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG;
3974 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3976 "ME4000:me4000_ai_ex_trig_setup():Invalid trigger mode specified\n");
3980 if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_RISING) {
3982 ~(ME4000_AI_CTRL_BIT_EX_TRIG_BOTH |
3983 ME4000_AI_CTRL_BIT_EX_TRIG_FALLING);
3984 } else if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_FALLING) {
3985 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG_FALLING;
3986 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG_BOTH;
3987 } else if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_BOTH) {
3989 ME4000_AI_CTRL_BIT_EX_TRIG_BOTH |
3990 ME4000_AI_CTRL_BIT_EX_TRIG_FALLING;
3992 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3994 "ME4000:me4000_ai_ex_trig_setup():Invalid trigger edge specified\n");
3998 me4000_outl(tmp, ai_context->ctrl_reg);
3999 spin_unlock_irqrestore(&ai_context->int_lock, flags);
4003 static int me4000_ai_sc_setup(me4000_ai_sc_t * arg,
4004 me4000_ai_context_t * ai_context)
4009 CALL_PDEBUG("me4000_ai_sc_setup() is executed\n");
4011 /* Copy data from user */
4012 err = copy_from_user(&cmd, arg, sizeof(me4000_ai_sc_t));
4015 "ME4000:me4000_ai_sc_setup():Can't copy from user space\n");
4019 ai_context->sample_counter = cmd.value;
4020 ai_context->sample_counter_reload = cmd.reload;
4025 static ssize_t me4000_ai_read(struct file *filep, char *buff, size_t cnt,
4028 me4000_ai_context_t *ai_context = filep->private_data;
4029 s16 *buffer = (s16 *) buff;
4030 size_t count = cnt / 2;
4031 unsigned long flags;
4038 CALL_PDEBUG("me4000_ai_read() is executed\n");
4040 init_waitqueue_entry(&wait, current);
4044 PDEBUG("me4000_ai_read():Count is 0\n");
4049 if (filep->f_flags & O_NONBLOCK) {
4050 c = me4000_values_to_end(ai_context->circ_buf,
4051 ME4000_AI_BUFFER_COUNT);
4054 ("me4000_ai_read():Returning from nonblocking read\n");
4058 /* Check if conversion is still running */
4060 (me4000_inl(ai_context->status_reg) &
4061 ME4000_AI_STATUS_BIT_FSM)) {
4063 "ME4000:me4000_ai_read():Conversion interrupted\n");
4067 wait_event_interruptible(ai_context->wait_queue,
4068 (me4000_values_to_end
4069 (ai_context->circ_buf,
4070 ME4000_AI_BUFFER_COUNT)));
4071 if (signal_pending(current)) {
4073 "ME4000:me4000_ai_read():Wait on values interrupted from signal\n");
4078 /* Only read count values or as much as available */
4079 c = me4000_values_to_end(ai_context->circ_buf,
4080 ME4000_AI_BUFFER_COUNT);
4081 PDEBUG("me4000_ai_read():%d values to end\n", c);
4085 PDEBUG("me4000_ai_read():Copy %d values to user space\n", c);
4087 k -= copy_to_user(buffer,
4088 ai_context->circ_buf.buf +
4089 ai_context->circ_buf.tail, k);
4093 "ME4000:me4000_ai_read():Cannot copy new values to user\n");
4097 ai_context->circ_buf.tail =
4098 (ai_context->circ_buf.tail + c) & (ME4000_AI_BUFFER_COUNT -
4104 spin_lock_irqsave(&ai_context->int_lock, flags);
4105 if (me4000_buf_space
4106 (ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
4107 tmp = me4000_inl(ai_context->ctrl_reg);
4109 /* Determine interrupt setup */
4110 if (ai_context->sample_counter
4111 && !ai_context->sample_counter_reload) {
4112 /* Enable Half Full Interrupt and Sample Counter Interrupt */
4114 ME4000_AI_CTRL_BIT_SC_IRQ |
4115 ME4000_AI_CTRL_BIT_HF_IRQ;
4116 } else if (ai_context->sample_counter
4117 && ai_context->sample_counter_reload) {
4118 if (ai_context->sample_counter <
4119 ME4000_AI_FIFO_COUNT / 2) {
4120 /* Enable only Sample Counter Interrupt */
4121 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ;
4123 /* Enable Half Full Interrupt and Sample Counter Interrupt */
4125 ME4000_AI_CTRL_BIT_SC_IRQ |
4126 ME4000_AI_CTRL_BIT_HF_IRQ;
4129 /* Enable only Half Full Interrupt */
4130 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ;
4133 me4000_outl(tmp, ai_context->ctrl_reg);
4135 spin_unlock_irqrestore(&ai_context->int_lock, flags);
4138 /* Check if conversion is still running */
4139 if (!(me4000_inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM)) {
4141 "ME4000:me4000_ai_read():Conversion not running after complete read\n");
4145 if (filep->f_flags & O_NONBLOCK) {
4146 return (k == 0) ? -EAGAIN : 2 * ret;
4149 CALL_PDEBUG("me4000_ai_read() is leaved\n");
4153 static unsigned int me4000_ai_poll(struct file *file_p, poll_table * wait)
4155 me4000_ai_context_t *ai_context;
4156 unsigned long mask = 0;
4158 CALL_PDEBUG("me4000_ai_poll() is executed\n");
4160 ai_context = file_p->private_data;
4162 /* Register wait queue */
4163 poll_wait(file_p, &ai_context->wait_queue, wait);
4165 /* Get available values */
4166 if (me4000_values_to_end(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT))
4167 mask |= POLLIN | POLLRDNORM;
4169 PDEBUG("me4000_ai_poll():Return mask %lX\n", mask);
4174 static int me4000_ai_offset_enable(me4000_ai_context_t * ai_context)
4178 CALL_PDEBUG("me4000_ai_offset_enable() is executed\n");
4180 tmp = me4000_inl(ai_context->ctrl_reg);
4181 tmp |= ME4000_AI_CTRL_BIT_OFFSET;
4182 me4000_outl(tmp, ai_context->ctrl_reg);
4187 static int me4000_ai_offset_disable(me4000_ai_context_t * ai_context)
4191 CALL_PDEBUG("me4000_ai_offset_disable() is executed\n");
4193 tmp = me4000_inl(ai_context->ctrl_reg);
4194 tmp &= ~ME4000_AI_CTRL_BIT_OFFSET;
4195 me4000_outl(tmp, ai_context->ctrl_reg);
4200 static int me4000_ai_fullscale_enable(me4000_ai_context_t * ai_context)
4204 CALL_PDEBUG("me4000_ai_fullscale_enable() is executed\n");
4206 tmp = me4000_inl(ai_context->ctrl_reg);
4207 tmp |= ME4000_AI_CTRL_BIT_FULLSCALE;
4208 me4000_outl(tmp, ai_context->ctrl_reg);
4213 static int me4000_ai_fullscale_disable(me4000_ai_context_t * ai_context)
4217 CALL_PDEBUG("me4000_ai_fullscale_disable() is executed\n");
4219 tmp = me4000_inl(ai_context->ctrl_reg);
4220 tmp &= ~ME4000_AI_CTRL_BIT_FULLSCALE;
4221 me4000_outl(tmp, ai_context->ctrl_reg);
4226 static int me4000_ai_fsm_state(int *arg, me4000_ai_context_t * ai_context)
4230 CALL_PDEBUG("me4000_ai_fsm_state() is executed\n");
4233 (me4000_inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) ? 1
4236 if (put_user(tmp, arg)) {
4237 printk(KERN_ERR "me4000_ai_fsm_state():Cannot copy to user\n");
4244 static int me4000_ai_get_count_buffer(unsigned long *arg,
4245 me4000_ai_context_t * ai_context)
4250 c = me4000_buf_count(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT);
4252 err = copy_to_user(arg, &c, sizeof(unsigned long));
4255 "ME4000:me4000_ai_get_count_buffer():Can't copy to user space\n");
4262 /*---------------------------------- EEPROM stuff ---------------------------*/
4264 static int eeprom_write_cmd(me4000_ai_context_t * ai_context, unsigned long cmd,
4268 unsigned long value;
4270 CALL_PDEBUG("eeprom_write_cmd() is executed\n");
4272 PDEBUG("eeprom_write_cmd():Write command 0x%08lX with length = %d\n",
4275 /* Get the ICR register and clear the related bits */
4276 value = me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR);
4277 value &= ~(PLX_ICR_MASK_EEPROM);
4278 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4280 /* Raise the chip select */
4281 value |= PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4282 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4283 udelay(EEPROM_DELAY);
4285 for (i = 0; i < length; i++) {
4286 if (cmd & ((0x1 << (length - 1)) >> i)) {
4287 value |= PLX_ICR_BIT_EEPROM_WRITE;
4289 value &= ~PLX_ICR_BIT_EEPROM_WRITE;
4292 /* Write to EEPROM */
4294 ai_context->board_info->plx_regbase + PLX_ICR);
4295 udelay(EEPROM_DELAY);
4297 /* Raising edge of the clock */
4298 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4300 ai_context->board_info->plx_regbase + PLX_ICR);
4301 udelay(EEPROM_DELAY);
4303 /* Falling edge of the clock */
4304 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4306 ai_context->board_info->plx_regbase + PLX_ICR);
4307 udelay(EEPROM_DELAY);
4310 /* Clear the chip select */
4311 value &= ~PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4312 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4313 udelay(EEPROM_DELAY);
4315 /* Wait until hardware is ready for sure */
4321 static unsigned short eeprom_read_cmd(me4000_ai_context_t * ai_context,
4322 unsigned long cmd, int length)
4325 unsigned long value;
4326 unsigned short id = 0;
4328 CALL_PDEBUG("eeprom_read_cmd() is executed\n");
4330 PDEBUG("eeprom_read_cmd():Read command 0x%08lX with length = %d\n", cmd,
4333 /* Get the ICR register and clear the related bits */
4334 value = me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR);
4335 value &= ~(PLX_ICR_MASK_EEPROM);
4337 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4339 /* Raise the chip select */
4340 value |= PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4341 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4342 udelay(EEPROM_DELAY);
4344 /* Write the read command to the eeprom */
4345 for (i = 0; i < length; i++) {
4346 if (cmd & ((0x1 << (length - 1)) >> i)) {
4347 value |= PLX_ICR_BIT_EEPROM_WRITE;
4349 value &= ~PLX_ICR_BIT_EEPROM_WRITE;
4352 ai_context->board_info->plx_regbase + PLX_ICR);
4353 udelay(EEPROM_DELAY);
4355 /* Raising edge of the clock */
4356 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4358 ai_context->board_info->plx_regbase + PLX_ICR);
4359 udelay(EEPROM_DELAY);
4361 /* Falling edge of the clock */
4362 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4364 ai_context->board_info->plx_regbase + PLX_ICR);
4365 udelay(EEPROM_DELAY);
4368 /* Read the value from the eeprom */
4369 for (i = 0; i < 16; i++) {
4370 /* Raising edge of the clock */
4371 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4373 ai_context->board_info->plx_regbase + PLX_ICR);
4374 udelay(EEPROM_DELAY);
4376 if (me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR) &
4377 PLX_ICR_BIT_EEPROM_READ) {
4378 id |= (0x8000 >> i);
4379 PDEBUG("eeprom_read_cmd():OR with 0x%04X\n",
4382 PDEBUG("eeprom_read_cmd():Dont't OR\n");
4385 /* Falling edge of the clock */
4386 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4388 ai_context->board_info->plx_regbase + PLX_ICR);
4389 udelay(EEPROM_DELAY);
4392 /* Clear the chip select */
4393 value &= ~PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4394 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4395 udelay(EEPROM_DELAY);
4400 static int me4000_eeprom_write(me4000_eeprom_t * arg,
4401 me4000_ai_context_t * ai_context)
4404 me4000_eeprom_t setup;
4406 unsigned long date_high;
4407 unsigned long date_low;
4409 CALL_PDEBUG("me4000_eeprom_write() is executed\n");
4411 err = copy_from_user(&setup, arg, sizeof(setup));
4414 "ME4000:me4000_eeprom_write():Cannot copy from user\n");
4418 /* Enable writing */
4419 eeprom_write_cmd(ai_context, ME4000_EEPROM_CMD_WRITE_ENABLE,
4420 ME4000_EEPROM_CMD_LENGTH_WRITE_ENABLE);
4422 /* Command for date */
4423 date_high = (setup.date & 0xFFFF0000) >> 16;
4424 date_low = (setup.date & 0x0000FFFF);
4427 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_DATE_HIGH <<
4428 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4432 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4437 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_DATE_LOW <<
4438 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4442 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4446 /* Command for unipolar 10V offset */
4448 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_UNI_OFFSET <<
4449 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4454 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4458 /* Command for unipolar 10V fullscale */
4460 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_UNI_FULLSCALE <<
4461 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4466 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4470 /* Command for unipolar 2,5V offset */
4472 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_UNI_OFFSET <<
4473 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4478 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4482 /* Command for unipolar 2,5V fullscale */
4484 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_UNI_FULLSCALE <<
4485 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4490 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4494 /* Command for bipolar 10V offset */
4496 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_BI_OFFSET <<
4497 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4502 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4506 /* Command for bipolar 10V fullscale */
4508 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_BI_FULLSCALE <<
4509 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4514 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4518 /* Command for bipolar 2,5V offset */
4520 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_BI_OFFSET <<
4521 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4526 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4530 /* Command for bipolar 2,5V fullscale */
4532 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_BI_FULLSCALE <<
4533 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4538 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4542 /* Command for differential 10V offset */
4544 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_DIFF_OFFSET <<
4545 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4550 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4554 /* Command for differential 10V fullscale */
4556 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_DIFF_FULLSCALE
4557 << ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4562 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4566 /* Command for differential 2,5V offset */
4568 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_DIFF_OFFSET <<
4569 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4574 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4578 /* Command for differential 2,5V fullscale */
4580 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_DIFF_FULLSCALE
4581 << ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4585 diff_2_5_fullscale);
4586 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4590 /* Disable writing */
4591 eeprom_write_cmd(ai_context, ME4000_EEPROM_CMD_WRITE_DISABLE,
4592 ME4000_EEPROM_CMD_LENGTH_WRITE_DISABLE);
4597 static int me4000_eeprom_read(me4000_eeprom_t * arg,
4598 me4000_ai_context_t * ai_context)
4602 me4000_eeprom_t setup;
4604 CALL_PDEBUG("me4000_eeprom_read() is executed\n");
4606 /* Command for date */
4607 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_DATE_HIGH;
4609 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4611 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_DATE_LOW;
4613 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4615 /* Command for unipolar 10V offset */
4616 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_UNI_OFFSET;
4617 setup.uni_10_offset =
4618 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4620 /* Command for unipolar 10V fullscale */
4621 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_UNI_FULLSCALE;
4622 setup.uni_10_fullscale =
4623 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4625 /* Command for unipolar 2,5V offset */
4626 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_UNI_OFFSET;
4627 setup.uni_2_5_offset =
4628 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4630 /* Command for unipolar 2,5V fullscale */
4631 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_UNI_FULLSCALE;
4632 setup.uni_2_5_fullscale =
4633 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4635 /* Command for bipolar 10V offset */
4636 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_BI_OFFSET;
4637 setup.bi_10_offset =
4638 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4640 /* Command for bipolar 10V fullscale */
4641 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_BI_FULLSCALE;
4642 setup.bi_10_fullscale =
4643 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4645 /* Command for bipolar 2,5V offset */
4646 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_BI_OFFSET;
4647 setup.bi_2_5_offset =
4648 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4650 /* Command for bipolar 2,5V fullscale */
4651 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_BI_FULLSCALE;
4652 setup.bi_2_5_fullscale =
4653 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4655 /* Command for differntial 10V offset */
4656 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_DIFF_OFFSET;
4657 setup.diff_10_offset =
4658 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4660 /* Command for differential 10V fullscale */
4661 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_DIFF_FULLSCALE;
4662 setup.diff_10_fullscale =
4663 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4665 /* Command for differntial 2,5V offset */
4666 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_DIFF_OFFSET;
4667 setup.diff_2_5_offset =
4668 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4670 /* Command for differential 2,5V fullscale */
4671 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_DIFF_FULLSCALE;
4672 setup.diff_2_5_fullscale =
4673 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4675 err = copy_to_user(arg, &setup, sizeof(setup));
4678 "ME4000:me4000_eeprom_read():Cannot copy to user\n");
4685 /*------------------------------------ DIO stuff ----------------------------------------------*/
4687 static int me4000_dio_ioctl(struct inode *inode_p, struct file *file_p,
4688 unsigned int service, unsigned long arg)
4690 me4000_dio_context_t *dio_context;
4692 CALL_PDEBUG("me4000_dio_ioctl() is executed\n");
4694 dio_context = file_p->private_data;
4696 if (_IOC_TYPE(service) != ME4000_MAGIC) {
4697 printk(KERN_ERR "me4000_dio_ioctl():Wrong magic number\n");
4700 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
4701 printk(KERN_ERR "me4000_dio_ioctl():Service number to high\n");
4706 case ME4000_DIO_CONFIG:
4707 return me4000_dio_config((me4000_dio_config_t *) arg,
4709 case ME4000_DIO_SET_BYTE:
4710 return me4000_dio_set_byte((me4000_dio_byte_t *) arg,
4712 case ME4000_DIO_GET_BYTE:
4713 return me4000_dio_get_byte((me4000_dio_byte_t *) arg,
4715 case ME4000_DIO_RESET:
4716 return me4000_dio_reset(dio_context);
4719 "ME4000:me4000_dio_ioctl():Invalid service number %d\n",
4726 static int me4000_dio_config(me4000_dio_config_t * arg,
4727 me4000_dio_context_t * dio_context)
4729 me4000_dio_config_t cmd;
4733 CALL_PDEBUG("me4000_dio_config() is executed\n");
4735 /* Copy data from user */
4736 err = copy_from_user(&cmd, arg, sizeof(me4000_dio_config_t));
4739 "ME4000:me4000_dio_config():Can't copy from user space\n");
4743 /* Check port parameter */
4744 if (cmd.port >= dio_context->dio_count) {
4746 "ME4000:me4000_dio_config():Port %d is not available\n",
4751 PDEBUG("me4000_dio_config(): port %d, mode %d, function %d\n", cmd.port,
4752 cmd.mode, cmd.function);
4754 if (cmd.port == ME4000_DIO_PORT_A) {
4755 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4756 /* Check if opto isolated version */
4757 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4759 "ME4000:me4000_dio_config():Cannot set to input on opto isolated versions\n");
4763 tmp = me4000_inl(dio_context->ctrl_reg);
4765 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4766 ME4000_DIO_CTRL_BIT_MODE_1);
4767 me4000_outl(tmp, dio_context->ctrl_reg);
4768 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4769 tmp = me4000_inl(dio_context->ctrl_reg);
4771 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4772 ME4000_DIO_CTRL_BIT_MODE_1);
4773 tmp |= ME4000_DIO_CTRL_BIT_MODE_0;
4774 me4000_outl(tmp, dio_context->ctrl_reg);
4775 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4776 tmp = me4000_inl(dio_context->ctrl_reg);
4778 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4779 ME4000_DIO_CTRL_BIT_MODE_1 |
4780 ME4000_DIO_CTRL_BIT_FIFO_HIGH_0);
4782 ME4000_DIO_CTRL_BIT_MODE_0 |
4783 ME4000_DIO_CTRL_BIT_MODE_1;
4784 me4000_outl(tmp, dio_context->ctrl_reg);
4785 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4786 tmp = me4000_inl(dio_context->ctrl_reg);
4788 ME4000_DIO_CTRL_BIT_MODE_0 |
4789 ME4000_DIO_CTRL_BIT_MODE_1 |
4790 ME4000_DIO_CTRL_BIT_FIFO_HIGH_0;
4791 me4000_outl(tmp, dio_context->ctrl_reg);
4794 "ME4000:me4000_dio_config():Mode %d is not available\n",
4798 } else if (cmd.port == ME4000_DIO_PORT_B) {
4799 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4800 /* Only do anything when TTL version is installed */
4801 if ((me4000_inl(dio_context->dir_reg) & 0x1)) {
4802 tmp = me4000_inl(dio_context->ctrl_reg);
4804 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4805 ME4000_DIO_CTRL_BIT_MODE_3);
4806 me4000_outl(tmp, dio_context->ctrl_reg);
4808 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4809 /* Check if opto isolated version */
4810 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4812 "ME4000:me4000_dio_config():Cannot set to output on opto isolated versions\n");
4816 tmp = me4000_inl(dio_context->ctrl_reg);
4818 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4819 ME4000_DIO_CTRL_BIT_MODE_3);
4820 tmp |= ME4000_DIO_CTRL_BIT_MODE_2;
4821 me4000_outl(tmp, dio_context->ctrl_reg);
4822 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4823 /* Check if opto isolated version */
4824 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4826 "ME4000:me4000_dio_config():Cannot set to FIFO low output on opto isolated versions\n");
4830 tmp = me4000_inl(dio_context->ctrl_reg);
4832 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4833 ME4000_DIO_CTRL_BIT_MODE_3 |
4834 ME4000_DIO_CTRL_BIT_FIFO_HIGH_1);
4836 ME4000_DIO_CTRL_BIT_MODE_2 |
4837 ME4000_DIO_CTRL_BIT_MODE_3;
4838 me4000_outl(tmp, dio_context->ctrl_reg);
4839 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4840 /* Check if opto isolated version */
4841 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4843 "ME4000:me4000_dio_config():Cannot set to FIFO high output on opto isolated versions\n");
4847 tmp = me4000_inl(dio_context->ctrl_reg);
4849 ME4000_DIO_CTRL_BIT_MODE_2 |
4850 ME4000_DIO_CTRL_BIT_MODE_3 |
4851 ME4000_DIO_CTRL_BIT_FIFO_HIGH_1;
4852 me4000_outl(tmp, dio_context->ctrl_reg);
4855 "ME4000:me4000_dio_config():Mode %d is not available\n",
4859 } else if (cmd.port == ME4000_DIO_PORT_C) {
4860 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4861 tmp = me4000_inl(dio_context->ctrl_reg);
4863 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4864 ME4000_DIO_CTRL_BIT_MODE_5);
4865 me4000_outl(tmp, dio_context->ctrl_reg);
4866 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4867 tmp = me4000_inl(dio_context->ctrl_reg);
4869 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4870 ME4000_DIO_CTRL_BIT_MODE_5);
4871 tmp |= ME4000_DIO_CTRL_BIT_MODE_4;
4872 me4000_outl(tmp, dio_context->ctrl_reg);
4873 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4874 tmp = me4000_inl(dio_context->ctrl_reg);
4876 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4877 ME4000_DIO_CTRL_BIT_MODE_5 |
4878 ME4000_DIO_CTRL_BIT_FIFO_HIGH_2);
4880 ME4000_DIO_CTRL_BIT_MODE_4 |
4881 ME4000_DIO_CTRL_BIT_MODE_5;
4882 me4000_outl(tmp, dio_context->ctrl_reg);
4883 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4884 tmp = me4000_inl(dio_context->ctrl_reg);
4886 ME4000_DIO_CTRL_BIT_MODE_4 |
4887 ME4000_DIO_CTRL_BIT_MODE_5 |
4888 ME4000_DIO_CTRL_BIT_FIFO_HIGH_2;
4889 me4000_outl(tmp, dio_context->ctrl_reg);
4892 "ME4000:me4000_dio_config():Mode %d is not available\n",
4896 } else if (cmd.port == ME4000_DIO_PORT_D) {
4897 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4898 tmp = me4000_inl(dio_context->ctrl_reg);
4900 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4901 ME4000_DIO_CTRL_BIT_MODE_7);
4902 me4000_outl(tmp, dio_context->ctrl_reg);
4903 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4904 tmp = me4000_inl(dio_context->ctrl_reg);
4906 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4907 ME4000_DIO_CTRL_BIT_MODE_7);
4908 tmp |= ME4000_DIO_CTRL_BIT_MODE_6;
4909 me4000_outl(tmp, dio_context->ctrl_reg);
4910 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4911 tmp = me4000_inl(dio_context->ctrl_reg);
4913 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4914 ME4000_DIO_CTRL_BIT_MODE_7 |
4915 ME4000_DIO_CTRL_BIT_FIFO_HIGH_3);
4917 ME4000_DIO_CTRL_BIT_MODE_6 |
4918 ME4000_DIO_CTRL_BIT_MODE_7;
4919 me4000_outl(tmp, dio_context->ctrl_reg);
4920 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4921 tmp = me4000_inl(dio_context->ctrl_reg);
4923 ME4000_DIO_CTRL_BIT_MODE_6 |
4924 ME4000_DIO_CTRL_BIT_MODE_7 |
4925 ME4000_DIO_CTRL_BIT_FIFO_HIGH_3;
4926 me4000_outl(tmp, dio_context->ctrl_reg);
4929 "ME4000:me4000_dio_config():Mode %d is not available\n",
4935 "ME4000:me4000_dio_config():Port %d is not available\n",
4940 PDEBUG("me4000_dio_config(): port %d, mode %d, function %d\n", cmd.port,
4941 cmd.mode, cmd.function);
4943 if ((cmd.mode == ME4000_DIO_FIFO_HIGH)
4944 || (cmd.mode == ME4000_DIO_FIFO_LOW)) {
4945 tmp = me4000_inl(dio_context->ctrl_reg);
4947 ~(ME4000_DIO_CTRL_BIT_FUNCTION_0 |
4948 ME4000_DIO_CTRL_BIT_FUNCTION_1);
4949 if (cmd.function == ME4000_DIO_FUNCTION_PATTERN) {
4950 me4000_outl(tmp, dio_context->ctrl_reg);
4951 } else if (cmd.function == ME4000_DIO_FUNCTION_DEMUX) {
4952 tmp |= ME4000_DIO_CTRL_BIT_FUNCTION_0;
4953 me4000_outl(tmp, dio_context->ctrl_reg);
4954 } else if (cmd.function == ME4000_DIO_FUNCTION_MUX) {
4955 tmp |= ME4000_DIO_CTRL_BIT_FUNCTION_1;
4956 me4000_outl(tmp, dio_context->ctrl_reg);
4959 "ME4000:me4000_dio_config():Invalid port function specified\n");
4967 static int me4000_dio_set_byte(me4000_dio_byte_t * arg,
4968 me4000_dio_context_t * dio_context)
4970 me4000_dio_byte_t cmd;
4973 CALL_PDEBUG("me4000_dio_set_byte() is executed\n");
4975 /* Copy data from user */
4976 err = copy_from_user(&cmd, arg, sizeof(me4000_dio_byte_t));
4979 "ME4000:me4000_dio_set_byte():Can't copy from user space\n");
4983 /* Check port parameter */
4984 if (cmd.port >= dio_context->dio_count) {
4986 "ME4000:me4000_dio_set_byte():Port %d is not available\n",
4991 if (cmd.port == ME4000_DIO_PORT_A) {
4992 if ((me4000_inl(dio_context->ctrl_reg) & 0x3) != 0x1) {
4994 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4998 me4000_outl(cmd.byte, dio_context->port_0_reg);
4999 } else if (cmd.port == ME4000_DIO_PORT_B) {
5000 if ((me4000_inl(dio_context->ctrl_reg) & 0xC) != 0x4) {
5002 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
5006 me4000_outl(cmd.byte, dio_context->port_1_reg);
5007 } else if (cmd.port == ME4000_DIO_PORT_C) {
5008 if ((me4000_inl(dio_context->ctrl_reg) & 0x30) != 0x10) {
5010 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
5014 me4000_outl(cmd.byte, dio_context->port_2_reg);
5015 } else if (cmd.port == ME4000_DIO_PORT_D) {
5016 if ((me4000_inl(dio_context->ctrl_reg) & 0xC0) != 0x40) {
5018 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
5022 me4000_outl(cmd.byte, dio_context->port_3_reg);
5025 "ME4000:me4000_dio_set_byte():Port %d is not available\n",
5033 static int me4000_dio_get_byte(me4000_dio_byte_t * arg,
5034 me4000_dio_context_t * dio_context)
5036 me4000_dio_byte_t cmd;
5039 CALL_PDEBUG("me4000_dio_get_byte() is executed\n");
5041 /* Copy data from user */
5042 err = copy_from_user(&cmd, arg, sizeof(me4000_dio_byte_t));
5045 "ME4000:me4000_dio_get_byte():Can't copy from user space\n");
5049 /* Check port parameter */
5050 if (cmd.port >= dio_context->dio_count) {
5052 "ME4000:me4000_dio_get_byte():Port %d is not available\n",
5057 if (cmd.port == ME4000_DIO_PORT_A) {
5058 cmd.byte = me4000_inl(dio_context->port_0_reg) & 0xFF;
5059 } else if (cmd.port == ME4000_DIO_PORT_B) {
5060 cmd.byte = me4000_inl(dio_context->port_1_reg) & 0xFF;
5061 } else if (cmd.port == ME4000_DIO_PORT_C) {
5062 cmd.byte = me4000_inl(dio_context->port_2_reg) & 0xFF;
5063 } else if (cmd.port == ME4000_DIO_PORT_D) {
5064 cmd.byte = me4000_inl(dio_context->port_3_reg) & 0xFF;
5067 "ME4000:me4000_dio_get_byte():Port %d is not available\n",
5072 /* Copy result back to user */
5073 err = copy_to_user(arg, &cmd, sizeof(me4000_dio_byte_t));
5076 "ME4000:me4000_dio_get_byte():Can't copy to user space\n");
5083 static int me4000_dio_reset(me4000_dio_context_t * dio_context)
5085 CALL_PDEBUG("me4000_dio_reset() is executed\n");
5087 /* Clear the control register */
5088 me4000_outl(0, dio_context->ctrl_reg);
5090 /* Check for opto isolated version */
5091 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
5092 me4000_outl(0x1, dio_context->ctrl_reg);
5093 me4000_outl(0x0, dio_context->port_0_reg);
5099 /*------------------------------------ COUNTER STUFF ------------------------------------*/
5101 static int me4000_cnt_ioctl(struct inode *inode_p, struct file *file_p,
5102 unsigned int service, unsigned long arg)
5104 me4000_cnt_context_t *cnt_context;
5106 CALL_PDEBUG("me4000_cnt_ioctl() is executed\n");
5108 cnt_context = file_p->private_data;
5110 if (_IOC_TYPE(service) != ME4000_MAGIC) {
5111 printk(KERN_ERR "me4000_dio_ioctl():Wrong magic number\n");
5114 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
5115 printk(KERN_ERR "me4000_dio_ioctl():Service number to high\n");
5120 case ME4000_CNT_READ:
5121 return me4000_cnt_read((me4000_cnt_t *) arg, cnt_context);
5122 case ME4000_CNT_WRITE:
5123 return me4000_cnt_write((me4000_cnt_t *) arg, cnt_context);
5124 case ME4000_CNT_CONFIG:
5125 return me4000_cnt_config((me4000_cnt_config_t *) arg,
5127 case ME4000_CNT_RESET:
5128 return me4000_cnt_reset(cnt_context);
5131 "ME4000:me4000_dio_ioctl():Invalid service number %d\n",
5138 static int me4000_cnt_config(me4000_cnt_config_t * arg,
5139 me4000_cnt_context_t * cnt_context)
5141 me4000_cnt_config_t cmd;
5146 CALL_PDEBUG("me4000_cnt_config() is executed\n");
5148 /* Copy data from user */
5149 err = copy_from_user(&cmd, arg, sizeof(me4000_cnt_config_t));
5152 "ME4000:me4000_cnt_config():Can't copy from user space\n");
5156 /* Check counter parameter */
5157 switch (cmd.counter) {
5158 case ME4000_CNT_COUNTER_0:
5159 counter = ME4000_CNT_CTRL_BIT_COUNTER_0;
5161 case ME4000_CNT_COUNTER_1:
5162 counter = ME4000_CNT_CTRL_BIT_COUNTER_1;
5164 case ME4000_CNT_COUNTER_2:
5165 counter = ME4000_CNT_CTRL_BIT_COUNTER_2;
5169 "ME4000:me4000_cnt_config():Counter %d is not available\n",
5174 /* Check mode parameter */
5176 case ME4000_CNT_MODE_0:
5177 mode = ME4000_CNT_CTRL_BIT_MODE_0;
5179 case ME4000_CNT_MODE_1:
5180 mode = ME4000_CNT_CTRL_BIT_MODE_1;
5182 case ME4000_CNT_MODE_2:
5183 mode = ME4000_CNT_CTRL_BIT_MODE_2;
5185 case ME4000_CNT_MODE_3:
5186 mode = ME4000_CNT_CTRL_BIT_MODE_3;
5188 case ME4000_CNT_MODE_4:
5189 mode = ME4000_CNT_CTRL_BIT_MODE_4;
5191 case ME4000_CNT_MODE_5:
5192 mode = ME4000_CNT_CTRL_BIT_MODE_5;
5196 "ME4000:me4000_cnt_config():Mode %d is not available\n",
5201 /* Write the control word */
5202 me4000_outb((counter | mode | 0x30), cnt_context->ctrl_reg);
5207 static int me4000_cnt_read(me4000_cnt_t * arg,
5208 me4000_cnt_context_t * cnt_context)
5214 CALL_PDEBUG("me4000_cnt_read() is executed\n");
5216 /* Copy data from user */
5217 err = copy_from_user(&cmd, arg, sizeof(me4000_cnt_t));
5220 "ME4000:me4000_cnt_read():Can't copy from user space\n");
5225 switch (cmd.counter) {
5226 case ME4000_CNT_COUNTER_0:
5227 tmp = me4000_inb(cnt_context->counter_0_reg);
5229 tmp = me4000_inb(cnt_context->counter_0_reg);
5230 cmd.value |= ((u16) tmp) << 8;
5232 case ME4000_CNT_COUNTER_1:
5233 tmp = me4000_inb(cnt_context->counter_1_reg);
5235 tmp = me4000_inb(cnt_context->counter_1_reg);
5236 cmd.value |= ((u16) tmp) << 8;
5238 case ME4000_CNT_COUNTER_2:
5239 tmp = me4000_inb(cnt_context->counter_2_reg);
5241 tmp = me4000_inb(cnt_context->counter_2_reg);
5242 cmd.value |= ((u16) tmp) << 8;
5246 "ME4000:me4000_cnt_read():Counter %d is not available\n",
5251 /* Copy result back to user */
5252 err = copy_to_user(arg, &cmd, sizeof(me4000_cnt_t));
5255 "ME4000:me4000_cnt_read():Can't copy to user space\n");
5262 static int me4000_cnt_write(me4000_cnt_t * arg,
5263 me4000_cnt_context_t * cnt_context)
5269 CALL_PDEBUG("me4000_cnt_write() is executed\n");
5271 /* Copy data from user */
5272 err = copy_from_user(&cmd, arg, sizeof(me4000_cnt_t));
5275 "ME4000:me4000_cnt_write():Can't copy from user space\n");
5280 switch (cmd.counter) {
5281 case ME4000_CNT_COUNTER_0:
5282 tmp = cmd.value & 0xFF;
5283 me4000_outb(tmp, cnt_context->counter_0_reg);
5284 tmp = (cmd.value >> 8) & 0xFF;
5285 me4000_outb(tmp, cnt_context->counter_0_reg);
5287 case ME4000_CNT_COUNTER_1:
5288 tmp = cmd.value & 0xFF;
5289 me4000_outb(tmp, cnt_context->counter_1_reg);
5290 tmp = (cmd.value >> 8) & 0xFF;
5291 me4000_outb(tmp, cnt_context->counter_1_reg);
5293 case ME4000_CNT_COUNTER_2:
5294 tmp = cmd.value & 0xFF;
5295 me4000_outb(tmp, cnt_context->counter_2_reg);
5296 tmp = (cmd.value >> 8) & 0xFF;
5297 me4000_outb(tmp, cnt_context->counter_2_reg);
5301 "ME4000:me4000_cnt_write():Counter %d is not available\n",
5309 static int me4000_cnt_reset(me4000_cnt_context_t * cnt_context)
5311 CALL_PDEBUG("me4000_cnt_reset() is executed\n");
5313 /* Set the mode and value for counter 0 */
5314 me4000_outb(0x30, cnt_context->ctrl_reg);
5315 me4000_outb(0x00, cnt_context->counter_0_reg);
5316 me4000_outb(0x00, cnt_context->counter_0_reg);
5318 /* Set the mode and value for counter 1 */
5319 me4000_outb(0x70, cnt_context->ctrl_reg);
5320 me4000_outb(0x00, cnt_context->counter_1_reg);
5321 me4000_outb(0x00, cnt_context->counter_1_reg);
5323 /* Set the mode and value for counter 2 */
5324 me4000_outb(0xB0, cnt_context->ctrl_reg);
5325 me4000_outb(0x00, cnt_context->counter_2_reg);
5326 me4000_outb(0x00, cnt_context->counter_2_reg);
5331 /*------------------------------------ External Interrupt stuff ------------------------------------*/
5333 static int me4000_ext_int_ioctl(struct inode *inode_p, struct file *file_p,
5334 unsigned int service, unsigned long arg)
5336 me4000_ext_int_context_t *ext_int_context;
5338 CALL_PDEBUG("me4000_ext_int_ioctl() is executed\n");
5340 ext_int_context = file_p->private_data;
5342 if (_IOC_TYPE(service) != ME4000_MAGIC) {
5343 printk(KERN_ERR "me4000_ext_int_ioctl():Wrong magic number\n");
5346 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
5348 "me4000_ext_int_ioctl():Service number to high\n");
5353 case ME4000_EXT_INT_ENABLE:
5354 return me4000_ext_int_enable(ext_int_context);
5355 case ME4000_EXT_INT_DISABLE:
5356 return me4000_ext_int_disable(ext_int_context);
5357 case ME4000_EXT_INT_COUNT:
5358 return me4000_ext_int_count((unsigned long *)arg,
5362 "ME4000:me4000_ext_int_ioctl():Invalid service number %d\n",
5369 static int me4000_ext_int_enable(me4000_ext_int_context_t * ext_int_context)
5373 CALL_PDEBUG("me4000_ext_int_enable() is executed\n");
5375 tmp = me4000_inl(ext_int_context->ctrl_reg);
5376 tmp |= ME4000_AI_CTRL_BIT_EX_IRQ;
5377 me4000_outl(tmp, ext_int_context->ctrl_reg);
5382 static int me4000_ext_int_disable(me4000_ext_int_context_t * ext_int_context)
5386 CALL_PDEBUG("me4000_ext_int_disable() is executed\n");
5388 tmp = me4000_inl(ext_int_context->ctrl_reg);
5389 tmp &= ~ME4000_AI_CTRL_BIT_EX_IRQ;
5390 me4000_outl(tmp, ext_int_context->ctrl_reg);
5395 static int me4000_ext_int_count(unsigned long *arg,
5396 me4000_ext_int_context_t * ext_int_context)
5399 CALL_PDEBUG("me4000_ext_int_count() is executed\n");
5401 put_user(ext_int_context->int_count, arg);
5405 /*------------------------------------ General stuff ------------------------------------*/
5407 static int me4000_get_user_info(me4000_user_info_t * arg,
5408 me4000_info_t * board_info)
5410 me4000_user_info_t user_info;
5412 CALL_PDEBUG("me4000_get_user_info() is executed\n");
5414 user_info.board_count = board_info->board_count;
5415 user_info.plx_regbase = board_info->plx_regbase;
5416 user_info.plx_regbase_size = board_info->plx_regbase_size;
5417 user_info.me4000_regbase = board_info->me4000_regbase;
5418 user_info.me4000_regbase_size = board_info->me4000_regbase_size;
5419 user_info.serial_no = board_info->serial_no;
5420 user_info.hw_revision = board_info->hw_revision;
5421 user_info.vendor_id = board_info->vendor_id;
5422 user_info.device_id = board_info->device_id;
5423 user_info.pci_bus_no = board_info->pci_bus_no;
5424 user_info.pci_dev_no = board_info->pci_dev_no;
5425 user_info.pci_func_no = board_info->pci_func_no;
5426 user_info.irq = board_info->irq;
5427 user_info.irq_count = board_info->irq_count;
5428 user_info.driver_version = ME4000_DRIVER_VERSION;
5429 user_info.ao_count = board_info->board_p->ao.count;
5430 user_info.ao_fifo_count = board_info->board_p->ao.fifo_count;
5432 user_info.ai_count = board_info->board_p->ai.count;
5433 user_info.ai_sh_count = board_info->board_p->ai.sh_count;
5434 user_info.ai_ex_trig_analog = board_info->board_p->ai.ex_trig_analog;
5436 user_info.dio_count = board_info->board_p->dio.count;
5438 user_info.cnt_count = board_info->board_p->cnt.count;
5440 if (copy_to_user(arg, &user_info, sizeof(me4000_user_info_t)))
5446 /*------------------------------------ ISR STUFF ------------------------------------*/
5448 static int me4000_ext_int_fasync(int fd, struct file *file_ptr, int mode)
5451 me4000_ext_int_context_t *ext_int_context;
5453 CALL_PDEBUG("me4000_ext_int_fasync() is executed\n");
5455 ext_int_context = file_ptr->private_data;
5458 fasync_helper(fd, file_ptr, mode, &ext_int_context->fasync_ptr);
5460 CALL_PDEBUG("me4000_ext_int_fasync() is leaved\n");
5464 static irqreturn_t me4000_ao_isr(int irq, void *dev_id)
5468 me4000_ao_context_t *ao_context;
5472 //unsigned long before;
5473 //unsigned long after;
5475 ISR_PDEBUG("me4000_ao_isr() is executed\n");
5477 ao_context = dev_id;
5479 /* Check if irq number is right */
5480 if (irq != ao_context->irq) {
5481 ISR_PDEBUG("me4000_ao_isr():incorrect interrupt num: %d\n",
5486 /* Check if this DAC rised an interrupt */
5488 ((0x1 << (ao_context->index + 3)) &
5489 me4000_inl(ao_context->irq_status_reg))) {
5490 ISR_PDEBUG("me4000_ao_isr():Not this DAC\n");
5494 /* Read status register to find out what happened */
5495 tmp = me4000_inl(ao_context->status_reg);
5497 if (!(tmp & ME4000_AO_STATUS_BIT_EF) && (tmp & ME4000_AO_STATUS_BIT_HF)
5498 && (tmp & ME4000_AO_STATUS_BIT_HF)) {
5499 c = ME4000_AO_FIFO_COUNT;
5500 ISR_PDEBUG("me4000_ao_isr():Fifo empty\n");
5501 } else if ((tmp & ME4000_AO_STATUS_BIT_EF)
5502 && (tmp & ME4000_AO_STATUS_BIT_HF)
5503 && (tmp & ME4000_AO_STATUS_BIT_HF)) {
5504 c = ME4000_AO_FIFO_COUNT / 2;
5505 ISR_PDEBUG("me4000_ao_isr():Fifo under half full\n");
5508 ISR_PDEBUG("me4000_ao_isr():Fifo full\n");
5511 ISR_PDEBUG("me4000_ao_isr():Try to write 0x%04X values\n", c);
5514 c1 = me4000_values_to_end(ao_context->circ_buf,
5515 ME4000_AO_BUFFER_COUNT);
5516 ISR_PDEBUG("me4000_ao_isr():Values to end = %d\n", c1);
5522 ("me4000_ao_isr():Work done or buffer empty\n");
5526 if (((ao_context->fifo_reg & 0xFF) == ME4000_AO_01_FIFO_REG) ||
5527 ((ao_context->fifo_reg & 0xFF) == ME4000_AO_03_FIFO_REG)) {
5528 for (i = 0; i < c1; i++) {
5532 (ao_context->circ_buf.buf +
5533 ao_context->circ_buf.tail + i))) << 16;
5534 outl(value, ao_context->fifo_reg);
5537 outsw(ao_context->fifo_reg,
5538 ao_context->circ_buf.buf +
5539 ao_context->circ_buf.tail, c1);
5542 //printk(KERN_ERR"ME4000:me4000_ao_isr():Time lapse = %lu\n", after - before);
5544 ao_context->circ_buf.tail =
5545 (ao_context->circ_buf.tail + c1) & (ME4000_AO_BUFFER_COUNT -
5547 ISR_PDEBUG("me4000_ao_isr():%d values wrote to port 0x%04X\n",
5548 c1, ao_context->fifo_reg);
5552 /* If there are no values left in the buffer, disable interrupts */
5553 spin_lock(&ao_context->int_lock);
5554 if (!me4000_buf_count(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
5556 ("me4000_ao_isr():Disable Interrupt because no values left in buffer\n");
5557 tmp = me4000_inl(ao_context->ctrl_reg);
5558 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_IRQ;
5559 me4000_outl(tmp, ao_context->ctrl_reg);
5561 spin_unlock(&ao_context->int_lock);
5563 /* Reset the interrupt */
5564 spin_lock(&ao_context->int_lock);
5565 tmp = me4000_inl(ao_context->ctrl_reg);
5566 tmp |= ME4000_AO_CTRL_BIT_RESET_IRQ;
5567 me4000_outl(tmp, ao_context->ctrl_reg);
5568 tmp &= ~ME4000_AO_CTRL_BIT_RESET_IRQ;
5569 me4000_outl(tmp, ao_context->ctrl_reg);
5571 /* If state machine is stopped, flow was interrupted */
5572 if (!(me4000_inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM)) {
5573 printk(KERN_ERR "ME4000:me4000_ao_isr():Broken pipe\n");
5574 ao_context->pipe_flag = 1; // Set flag in order to inform write routine
5575 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_IRQ; // Disable interrupt
5577 me4000_outl(tmp, ao_context->ctrl_reg);
5578 spin_unlock(&ao_context->int_lock);
5580 /* Wake up waiting process */
5581 wake_up_interruptible(&(ao_context->wait_queue));
5583 /* Count the interrupt */
5584 ao_context->board_info->irq_count++;
5589 static irqreturn_t me4000_ai_isr(int irq, void *dev_id)
5592 me4000_ai_context_t *ai_context;
5596 #ifdef ME4000_ISR_DEBUG
5597 unsigned long before;
5598 unsigned long after;
5601 ISR_PDEBUG("me4000_ai_isr() is executed\n");
5603 #ifdef ME4000_ISR_DEBUG
5607 ai_context = dev_id;
5609 /* Check if irq number is right */
5610 if (irq != ai_context->irq) {
5611 ISR_PDEBUG("me4000_ai_isr():incorrect interrupt num: %d\n",
5616 if (me4000_inl(ai_context->irq_status_reg) &
5617 ME4000_IRQ_STATUS_BIT_AI_HF) {
5619 ("me4000_ai_isr():Fifo half full interrupt occured\n");
5621 /* Read status register to find out what happened */
5622 tmp = me4000_inl(ai_context->ctrl_reg);
5624 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5625 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5626 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5627 ISR_PDEBUG("me4000_ai_isr():Fifo full\n");
5628 c = ME4000_AI_FIFO_COUNT;
5630 /* FIFO overflow, so stop conversion and disable all interrupts */
5631 spin_lock(&ai_context->int_lock);
5632 tmp = me4000_inl(ai_context->ctrl_reg);
5633 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5635 ~(ME4000_AI_CTRL_BIT_HF_IRQ |
5636 ME4000_AI_CTRL_BIT_SC_IRQ);
5637 outl(tmp, ai_context->ctrl_reg);
5638 spin_unlock(&ai_context->int_lock);
5639 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5640 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5641 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5642 ISR_PDEBUG("me4000_ai_isr():Fifo half full\n");
5643 c = ME4000_AI_FIFO_COUNT / 2;
5647 ("me4000_ai_isr():Can't determine state of fifo\n");
5650 ISR_PDEBUG("me4000_ai_isr():Try to read %d values\n", c);
5653 c1 = me4000_space_to_end(ai_context->circ_buf,
5654 ME4000_AI_BUFFER_COUNT);
5655 ISR_PDEBUG("me4000_ai_isr():Space to end = %d\n", c1);
5661 ("me4000_ai_isr():Work done or buffer full\n");
5665 insw(ai_context->data_reg,
5666 ai_context->circ_buf.buf +
5667 ai_context->circ_buf.head, c1);
5668 ai_context->circ_buf.head =
5669 (ai_context->circ_buf.head +
5670 c1) & (ME4000_AI_BUFFER_COUNT - 1);
5674 /* Work is done, so reset the interrupt */
5676 ("me4000_ai_isr():reset interrupt fifo half full interrupt\n");
5677 spin_lock(&ai_context->int_lock);
5678 tmp = me4000_inl(ai_context->ctrl_reg);
5679 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
5680 me4000_outl(tmp, ai_context->ctrl_reg);
5681 tmp &= ~ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
5682 me4000_outl(tmp, ai_context->ctrl_reg);
5683 spin_unlock(&ai_context->int_lock);
5686 if (me4000_inl(ai_context->irq_status_reg) & ME4000_IRQ_STATUS_BIT_SC) {
5688 ("me4000_ai_isr():Sample counter interrupt occured\n");
5690 if (!ai_context->sample_counter_reload) {
5692 ("me4000_ai_isr():Single data block available\n");
5694 /* Poll data until fifo empty */
5696 (i < ME4000_AI_FIFO_COUNT / 2)
5697 && (inl(ai_context->ctrl_reg) &
5698 ME4000_AI_STATUS_BIT_EF_DATA); i++) {
5699 if (me4000_space_to_end
5700 (ai_context->circ_buf,
5701 ME4000_AI_BUFFER_COUNT)) {
5702 *(ai_context->circ_buf.buf +
5703 ai_context->circ_buf.head) =
5704 inw(ai_context->data_reg);
5705 ai_context->circ_buf.head =
5706 (ai_context->circ_buf.head +
5707 1) & (ME4000_AI_BUFFER_COUNT - 1);
5711 ISR_PDEBUG("me4000_ai_isr():%d values read\n", i);
5713 if (ai_context->sample_counter <=
5714 ME4000_AI_FIFO_COUNT / 2) {
5716 ("me4000_ai_isr():Interrupt from adjustable half full threshold\n");
5718 /* Read status register to find out what happened */
5719 tmp = me4000_inl(ai_context->ctrl_reg);
5721 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5722 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5723 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5725 ("me4000_ai_isr():Fifo full\n");
5726 c = ME4000_AI_FIFO_COUNT;
5728 /* FIFO overflow, so stop conversion */
5729 spin_lock(&ai_context->int_lock);
5730 tmp = me4000_inl(ai_context->ctrl_reg);
5732 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5733 outl(tmp, ai_context->ctrl_reg);
5734 spin_unlock(&ai_context->int_lock);
5735 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA)
5737 ME4000_AI_STATUS_BIT_HF_DATA)
5739 ME4000_AI_STATUS_BIT_EF_DATA)) {
5741 ("me4000_ai_isr():Fifo half full\n");
5742 c = ME4000_AI_FIFO_COUNT / 2;
5744 c = ai_context->sample_counter;
5746 ("me4000_ai_isr():Sample count values\n");
5750 ("me4000_ai_isr():Try to read %d values\n",
5754 c1 = me4000_space_to_end(ai_context->
5756 ME4000_AI_BUFFER_COUNT);
5758 ("me4000_ai_isr():Space to end = %d\n",
5765 ("me4000_ai_isr():Work done or buffer full\n");
5769 insw(ai_context->data_reg,
5770 ai_context->circ_buf.buf +
5771 ai_context->circ_buf.head, c1);
5772 ai_context->circ_buf.head =
5773 (ai_context->circ_buf.head +
5774 c1) & (ME4000_AI_BUFFER_COUNT - 1);
5779 ("me4000_ai_isr():Multiple data block available\n");
5781 /* Read status register to find out what happened */
5782 tmp = me4000_inl(ai_context->ctrl_reg);
5784 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5785 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5786 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5788 ("me4000_ai_isr():Fifo full\n");
5789 c = ME4000_AI_FIFO_COUNT;
5791 /* FIFO overflow, so stop conversion */
5792 spin_lock(&ai_context->int_lock);
5793 tmp = me4000_inl(ai_context->ctrl_reg);
5795 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5796 outl(tmp, ai_context->ctrl_reg);
5797 spin_unlock(&ai_context->int_lock);
5800 c1 = me4000_space_to_end
5801 (ai_context->circ_buf,
5802 ME4000_AI_BUFFER_COUNT);
5804 ("me4000_ai_isr():Space to end = %d\n",
5811 ("me4000_ai_isr():Work done or buffer full\n");
5815 insw(ai_context->data_reg,
5816 ai_context->circ_buf.buf +
5817 ai_context->circ_buf.head,
5819 ai_context->circ_buf.head =
5820 (ai_context->circ_buf.head +
5822 (ME4000_AI_BUFFER_COUNT -
5826 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA)
5828 ME4000_AI_STATUS_BIT_HF_DATA)
5830 ME4000_AI_STATUS_BIT_EF_DATA)) {
5832 ("me4000_ai_isr():Fifo half full\n");
5833 c = ME4000_AI_FIFO_COUNT / 2;
5836 c1 = me4000_space_to_end
5837 (ai_context->circ_buf,
5838 ME4000_AI_BUFFER_COUNT);
5840 ("me4000_ai_isr():Space to end = %d\n",
5847 ("me4000_ai_isr():Work done or buffer full\n");
5851 insw(ai_context->data_reg,
5852 ai_context->circ_buf.buf +
5853 ai_context->circ_buf.head,
5855 ai_context->circ_buf.head =
5856 (ai_context->circ_buf.head +
5858 (ME4000_AI_BUFFER_COUNT -
5863 /* Poll data until fifo empty */
5865 (i < ME4000_AI_FIFO_COUNT / 2)
5866 && (inl(ai_context->ctrl_reg) &
5867 ME4000_AI_STATUS_BIT_EF_DATA);
5869 if (me4000_space_to_end
5870 (ai_context->circ_buf,
5871 ME4000_AI_BUFFER_COUNT)) {
5872 *(ai_context->circ_buf.
5874 ai_context->circ_buf.
5876 inw(ai_context->data_reg);
5877 ai_context->circ_buf.
5882 (ME4000_AI_BUFFER_COUNT
5888 ("me4000_ai_isr():%d values read\n",
5894 /* Work is done, so reset the interrupt */
5896 ("me4000_ai_isr():reset interrupt from sample counter\n");
5897 spin_lock(&ai_context->int_lock);
5898 tmp = me4000_inl(ai_context->ctrl_reg);
5899 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
5900 me4000_outl(tmp, ai_context->ctrl_reg);
5901 tmp &= ~ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
5902 me4000_outl(tmp, ai_context->ctrl_reg);
5903 spin_unlock(&ai_context->int_lock);
5906 /* Values are now available, so wake up waiting process */
5907 if (me4000_buf_count(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
5908 ISR_PDEBUG("me4000_ai_isr():Wake up waiting process\n");
5909 wake_up_interruptible(&(ai_context->wait_queue));
5912 /* If there is no space left in the buffer, disable interrupts */
5913 spin_lock(&ai_context->int_lock);
5914 if (!me4000_buf_space(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
5916 ("me4000_ai_isr():Disable Interrupt because no space left in buffer\n");
5917 tmp = me4000_inl(ai_context->ctrl_reg);
5919 ~(ME4000_AI_CTRL_BIT_SC_IRQ | ME4000_AI_CTRL_BIT_HF_IRQ |
5920 ME4000_AI_CTRL_BIT_LE_IRQ);
5921 me4000_outl(tmp, ai_context->ctrl_reg);
5923 spin_unlock(&ai_context->int_lock);
5925 #ifdef ME4000_ISR_DEBUG
5927 printk(KERN_ERR "ME4000:me4000_ai_isr():Time lapse = %lu\n",
5934 static irqreturn_t me4000_ext_int_isr(int irq, void *dev_id)
5936 me4000_ext_int_context_t *ext_int_context;
5939 ISR_PDEBUG("me4000_ext_int_isr() is executed\n");
5941 ext_int_context = dev_id;
5943 /* Check if irq number is right */
5944 if (irq != ext_int_context->irq) {
5945 ISR_PDEBUG("me4000_ext_int_isr():incorrect interrupt num: %d\n",
5950 if (me4000_inl(ext_int_context->irq_status_reg) &
5951 ME4000_IRQ_STATUS_BIT_EX) {
5952 ISR_PDEBUG("me4000_ext_int_isr():External interrupt occured\n");
5953 tmp = me4000_inl(ext_int_context->ctrl_reg);
5954 tmp |= ME4000_AI_CTRL_BIT_EX_IRQ_RESET;
5955 me4000_outl(tmp, ext_int_context->ctrl_reg);
5956 tmp &= ~ME4000_AI_CTRL_BIT_EX_IRQ_RESET;
5957 me4000_outl(tmp, ext_int_context->ctrl_reg);
5959 ext_int_context->int_count++;
5961 if (ext_int_context->fasync_ptr) {
5963 ("me2600_ext_int_isr():Send signal to process\n");
5964 kill_fasync(&ext_int_context->fasync_ptr, SIGIO,
5972 void __exit me4000_module_exit(void)
5974 struct list_head *board_p;
5975 me4000_info_t *board_info;
5977 CALL_PDEBUG("cleanup_module() is executed\n");
5979 unregister_chrdev(me4000_ext_int_major_driver_no, ME4000_EXT_INT_NAME);
5981 unregister_chrdev(me4000_cnt_major_driver_no, ME4000_CNT_NAME);
5983 unregister_chrdev(me4000_dio_major_driver_no, ME4000_DIO_NAME);
5985 unregister_chrdev(me4000_ai_major_driver_no, ME4000_AI_NAME);
5987 unregister_chrdev(me4000_ao_major_driver_no, ME4000_AO_NAME);
5989 remove_proc_entry("me4000", NULL);
5991 pci_unregister_driver(&me4000_driver);
5993 /* Reset the boards */
5994 for (board_p = me4000_board_info_list.next;
5995 board_p != &me4000_board_info_list; board_p = board_p->next) {
5996 board_info = list_entry(board_p, me4000_info_t, list);
5997 me4000_reset_board(board_info);
6000 clear_board_info_list();
6003 module_exit(me4000_module_exit);
6005 static int me4000_read_procmem(char *buf, char **start, off_t offset, int count,
6006 int *eof, void *data)
6009 int limit = count - 1000;
6010 me4000_info_t *board_info;
6011 struct list_head *ptr;
6013 len += sprintf(buf + len, "\nME4000 DRIVER VERSION %X.%X.%X\n\n",
6014 (ME4000_DRIVER_VERSION & 0xFF0000) >> 16,
6015 (ME4000_DRIVER_VERSION & 0xFF00) >> 8,
6016 (ME4000_DRIVER_VERSION & 0xFF));
6018 /* Search for the board context */
6019 for (ptr = me4000_board_info_list.next;
6020 (ptr != &me4000_board_info_list) && (len < limit);
6022 board_info = list_entry(ptr, me4000_info_t, list);
6025 sprintf(buf + len, "Board number %d:\n",
6026 board_info->board_count);
6027 len += sprintf(buf + len, "---------------\n");
6029 sprintf(buf + len, "PLX base register = 0x%lX\n",
6030 board_info->plx_regbase);
6032 sprintf(buf + len, "PLX base register size = 0x%lX\n",
6033 board_info->plx_regbase_size);
6035 sprintf(buf + len, "ME4000 base register = 0x%lX\n",
6036 board_info->me4000_regbase);
6038 sprintf(buf + len, "ME4000 base register size = 0x%lX\n",
6039 board_info->me4000_regbase_size);
6041 sprintf(buf + len, "Serial number = 0x%X\n",
6042 board_info->serial_no);
6044 sprintf(buf + len, "Hardware revision = 0x%X\n",
6045 board_info->hw_revision);
6047 sprintf(buf + len, "Vendor id = 0x%X\n",
6048 board_info->vendor_id);
6050 sprintf(buf + len, "Device id = 0x%X\n",
6051 board_info->device_id);
6053 sprintf(buf + len, "PCI bus number = %d\n",
6054 board_info->pci_bus_no);
6056 sprintf(buf + len, "PCI device number = %d\n",
6057 board_info->pci_dev_no);
6059 sprintf(buf + len, "PCI function number = %d\n",
6060 board_info->pci_func_no);
6061 len += sprintf(buf + len, "IRQ = %u\n", board_info->irq);
6064 "Count of interrupts since module was loaded = %d\n",
6065 board_info->irq_count);
6068 sprintf(buf + len, "Count of analog outputs = %d\n",
6069 board_info->board_p->ao.count);
6071 sprintf(buf + len, "Count of analog output fifos = %d\n",
6072 board_info->board_p->ao.fifo_count);
6075 sprintf(buf + len, "Count of analog inputs = %d\n",
6076 board_info->board_p->ai.count);
6079 "Count of sample and hold devices for analog input = %d\n",
6080 board_info->board_p->ai.sh_count);
6083 "Analog external trigger available for analog input = %d\n",
6084 board_info->board_p->ai.ex_trig_analog);
6087 sprintf(buf + len, "Count of digital ports = %d\n",
6088 board_info->board_p->dio.count);
6091 sprintf(buf + len, "Count of counter devices = %d\n",
6092 board_info->board_p->cnt.count);
6094 sprintf(buf + len, "AI control register = 0x%08X\n",
6095 inl(board_info->me4000_regbase +
6096 ME4000_AI_CTRL_REG));
6098 len += sprintf(buf + len, "AO 0 control register = 0x%08X\n",
6099 inl(board_info->me4000_regbase +
6100 ME4000_AO_00_CTRL_REG));
6102 sprintf(buf + len, "AO 0 status register = 0x%08X\n",
6103 inl(board_info->me4000_regbase +
6104 ME4000_AO_00_STATUS_REG));
6106 sprintf(buf + len, "AO 1 control register = 0x%08X\n",
6107 inl(board_info->me4000_regbase +
6108 ME4000_AO_01_CTRL_REG));
6110 sprintf(buf + len, "AO 1 status register = 0x%08X\n",
6111 inl(board_info->me4000_regbase +
6112 ME4000_AO_01_STATUS_REG));
6114 sprintf(buf + len, "AO 2 control register = 0x%08X\n",
6115 inl(board_info->me4000_regbase +
6116 ME4000_AO_02_CTRL_REG));
6118 sprintf(buf + len, "AO 2 status register = 0x%08X\n",
6119 inl(board_info->me4000_regbase +
6120 ME4000_AO_02_STATUS_REG));
6122 sprintf(buf + len, "AO 3 control register = 0x%08X\n",
6123 inl(board_info->me4000_regbase +
6124 ME4000_AO_03_CTRL_REG));
6126 sprintf(buf + len, "AO 3 status register = 0x%08X\n",
6127 inl(board_info->me4000_regbase +
6128 ME4000_AO_03_STATUS_REG));