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>
28 #include <linux/errno.h>
29 #include <linux/delay.h>
31 #include <linux/unistd.h>
32 #include <linux/list.h>
33 #include <linux/proc_fs.h>
34 #include <linux/types.h>
35 #include <linux/poll.h>
36 #include <linux/vmalloc.h>
37 #include <linux/slab.h>
38 #include <asm/pgtable.h>
39 #include <linux/uaccess.h>
41 #include <asm/system.h>
43 /* Include-File for the Meilhaus ME-4000 I/O board */
45 #include "me4000_firmware.h"
46 #include "me4610_firmware.h"
48 /* Administrative stuff for modinfo */
49 MODULE_AUTHOR("Guenter Gebhardt <g.gebhardt@meilhaus.de>");
51 ("Device Driver Module for Meilhaus ME-4000 boards version 1.0.5");
52 MODULE_SUPPORTED_DEVICE("Meilhaus ME-4000 Multi I/O boards");
53 MODULE_LICENSE("GPL");
55 /* Board specific data are kept in a global list */
56 static LIST_HEAD(me4000_board_info_list);
58 /* Major Device Numbers. 0 means to get it automatically from the System */
59 static int me4000_ao_major_driver_no;
60 static int me4000_ai_major_driver_no;
61 static int me4000_dio_major_driver_no;
62 static int me4000_cnt_major_driver_no;
63 static int me4000_ext_int_major_driver_no;
65 /* Let the user specify a custom major driver number */
66 module_param(me4000_ao_major_driver_no, int, 0);
67 MODULE_PARM_DESC(me4000_ao_major_driver_no,
68 "Major driver number for analog output (default 0)");
70 module_param(me4000_ai_major_driver_no, int, 0);
71 MODULE_PARM_DESC(me4000_ai_major_driver_no,
72 "Major driver number for analog input (default 0)");
74 module_param(me4000_dio_major_driver_no, int, 0);
75 MODULE_PARM_DESC(me4000_dio_major_driver_no,
76 "Major driver number digital I/O (default 0)");
78 module_param(me4000_cnt_major_driver_no, int, 0);
79 MODULE_PARM_DESC(me4000_cnt_major_driver_no,
80 "Major driver number for counter (default 0)");
82 module_param(me4000_ext_int_major_driver_no, int, 0);
83 MODULE_PARM_DESC(me4000_ext_int_major_driver_no,
84 "Major driver number for external interrupt (default 0)");
86 /*-----------------------------------------------------------------------------
87 Board detection and initialization
88 ---------------------------------------------------------------------------*/
89 static int me4000_probe(struct pci_dev *dev, const struct pci_device_id *id);
90 static int me4000_xilinx_download(struct me4000_info *);
91 static int me4000_reset_board(struct me4000_info *);
93 static void clear_board_info_list(void);
94 static void release_ao_contexts(struct me4000_info *board_info);
95 /*-----------------------------------------------------------------------------
96 Stuff used by all device parts
97 ---------------------------------------------------------------------------*/
98 static int me4000_open(struct inode *, struct file *);
99 static int me4000_release(struct inode *, struct file *);
101 static int me4000_get_user_info(struct me4000_user_info *,
102 struct me4000_info *board_info);
103 static int me4000_read_procmem(char *, char **, off_t, int, int *, void *);
105 /*-----------------------------------------------------------------------------
107 ---------------------------------------------------------------------------*/
108 static ssize_t me4000_ao_write_sing(struct file *, const char *, size_t,
110 static ssize_t me4000_ao_write_wrap(struct file *, const char *, size_t,
112 static ssize_t me4000_ao_write_cont(struct file *, const char *, size_t,
115 static int me4000_ao_ioctl_sing(struct inode *, struct file *, unsigned int,
117 static int me4000_ao_ioctl_wrap(struct inode *, struct file *, unsigned int,
119 static int me4000_ao_ioctl_cont(struct inode *, struct file *, unsigned int,
122 static unsigned int me4000_ao_poll_cont(struct file *, poll_table *);
123 static int me4000_ao_fsync_cont(struct file *, struct dentry *, int);
125 static int me4000_ao_start(unsigned long *, struct me4000_ao_context *);
126 static int me4000_ao_stop(struct me4000_ao_context *);
127 static int me4000_ao_immediate_stop(struct me4000_ao_context *);
128 static int me4000_ao_timer_set_divisor(u32 *, struct me4000_ao_context *);
129 static int me4000_ao_preload(struct me4000_ao_context *);
130 static int me4000_ao_preload_update(struct me4000_ao_context *);
131 static int me4000_ao_ex_trig_set_edge(int *, struct me4000_ao_context *);
132 static int me4000_ao_ex_trig_enable(struct me4000_ao_context *);
133 static int me4000_ao_ex_trig_disable(struct me4000_ao_context *);
134 static int me4000_ao_prepare(struct me4000_ao_context *ao_info);
135 static int me4000_ao_reset(struct me4000_ao_context *ao_info);
136 static int me4000_ao_enable_do(struct me4000_ao_context *);
137 static int me4000_ao_disable_do(struct me4000_ao_context *);
138 static int me4000_ao_fsm_state(int *, struct me4000_ao_context *);
140 static int me4000_ao_simultaneous_ex_trig(struct me4000_ao_context *ao_context);
141 static int me4000_ao_simultaneous_sw(struct me4000_ao_context *ao_context);
142 static int me4000_ao_simultaneous_disable(struct me4000_ao_context *ao_context);
143 static int me4000_ao_simultaneous_update(
144 struct me4000_ao_channel_list *channels,
145 struct me4000_ao_context *ao_context);
147 static int me4000_ao_synchronous_ex_trig(struct me4000_ao_context *ao_context);
148 static int me4000_ao_synchronous_sw(struct me4000_ao_context *ao_context);
149 static int me4000_ao_synchronous_disable(struct me4000_ao_context *ao_context);
151 static int me4000_ao_ex_trig_timeout(unsigned long *arg,
152 struct me4000_ao_context *ao_context);
153 static int me4000_ao_get_free_buffer(unsigned long *arg,
154 struct me4000_ao_context *ao_context);
156 /*-----------------------------------------------------------------------------
158 ---------------------------------------------------------------------------*/
159 static int me4000_ai_single(struct me4000_ai_single *,
160 struct me4000_ai_context *);
161 static int me4000_ai_ioctl_sing(struct inode *, struct file *, unsigned int,
164 static ssize_t me4000_ai_read(struct file *, char *, size_t, loff_t *);
165 static int me4000_ai_ioctl_sw(struct inode *, struct file *, unsigned int,
167 static unsigned int me4000_ai_poll(struct file *, poll_table *);
168 static int me4000_ai_fasync(int fd, struct file *file_p, int mode);
170 static int me4000_ai_ioctl_ext(struct inode *, struct file *, unsigned int,
173 static int me4000_ai_prepare(struct me4000_ai_context *ai_context);
174 static int me4000_ai_reset(struct me4000_ai_context *ai_context);
175 static int me4000_ai_config(struct me4000_ai_config *,
176 struct me4000_ai_context *);
177 static int me4000_ai_start(struct me4000_ai_context *);
178 static int me4000_ai_start_ex(unsigned long *, struct me4000_ai_context *);
179 static int me4000_ai_stop(struct me4000_ai_context *);
180 static int me4000_ai_immediate_stop(struct me4000_ai_context *);
181 static int me4000_ai_ex_trig_enable(struct me4000_ai_context *);
182 static int me4000_ai_ex_trig_disable(struct me4000_ai_context *);
183 static int me4000_ai_ex_trig_setup(struct me4000_ai_trigger *,
184 struct me4000_ai_context *);
185 static int me4000_ai_sc_setup(struct me4000_ai_sc *arg,
186 struct me4000_ai_context *ai_context);
187 static int me4000_ai_offset_enable(struct me4000_ai_context *ai_context);
188 static int me4000_ai_offset_disable(struct me4000_ai_context *ai_context);
189 static int me4000_ai_fullscale_enable(struct me4000_ai_context *ai_context);
190 static int me4000_ai_fullscale_disable(struct me4000_ai_context *ai_context);
191 static int me4000_ai_fsm_state(int *arg, struct me4000_ai_context *ai_context);
192 static int me4000_ai_get_count_buffer(unsigned long *arg,
193 struct me4000_ai_context *ai_context);
195 /*-----------------------------------------------------------------------------
197 ---------------------------------------------------------------------------*/
198 static int me4000_eeprom_read(struct me4000_eeprom *arg,
199 struct me4000_ai_context *ai_context);
200 static int me4000_eeprom_write(struct me4000_eeprom *arg,
201 struct me4000_ai_context *ai_context);
203 /*-----------------------------------------------------------------------------
205 ---------------------------------------------------------------------------*/
206 static int me4000_dio_ioctl(struct inode *, struct file *, unsigned int,
208 static int me4000_dio_config(struct me4000_dio_config *,
209 struct me4000_dio_context *);
210 static int me4000_dio_get_byte(struct me4000_dio_byte *,
211 struct me4000_dio_context *);
212 static int me4000_dio_set_byte(struct me4000_dio_byte *,
213 struct me4000_dio_context *);
214 static int me4000_dio_reset(struct me4000_dio_context *);
216 /*-----------------------------------------------------------------------------
218 ---------------------------------------------------------------------------*/
219 static int me4000_cnt_ioctl(struct inode *, struct file *, unsigned int,
221 static int me4000_cnt_config(struct me4000_cnt_config *,
222 struct me4000_cnt_context *);
223 static int me4000_cnt_read(struct me4000_cnt *, struct me4000_cnt_context *);
224 static int me4000_cnt_write(struct me4000_cnt *, struct me4000_cnt_context *);
225 static int me4000_cnt_reset(struct me4000_cnt_context *);
227 /*-----------------------------------------------------------------------------
228 External interrupt routines
229 ---------------------------------------------------------------------------*/
230 static int me4000_ext_int_ioctl(struct inode *, struct file *, unsigned int,
232 static int me4000_ext_int_enable(struct me4000_ext_int_context *);
233 static int me4000_ext_int_disable(struct me4000_ext_int_context *);
234 static int me4000_ext_int_count(unsigned long *arg,
235 struct me4000_ext_int_context *ext_int_context);
236 static int me4000_ext_int_fasync(int fd, struct file *file_ptr, int mode);
238 /*-----------------------------------------------------------------------------
239 The interrupt service routines
240 ---------------------------------------------------------------------------*/
241 static irqreturn_t me4000_ao_isr(int, void *);
242 static irqreturn_t me4000_ai_isr(int, void *);
243 static irqreturn_t me4000_ext_int_isr(int, void *);
245 /*-----------------------------------------------------------------------------
247 ---------------------------------------------------------------------------*/
249 static inline int me4000_buf_count(struct me4000_circ_buf buf, int size)
251 return (buf.head - buf.tail) & (size - 1);
254 static inline int me4000_buf_space(struct me4000_circ_buf buf, int size)
256 return (buf.tail - (buf.head + 1)) & (size - 1);
259 static inline int me4000_values_to_end(struct me4000_circ_buf buf, int size)
263 end = size - buf.tail;
264 n = (buf.head + end) & (size - 1);
265 return (n < end) ? n : end;
268 static inline int me4000_space_to_end(struct me4000_circ_buf buf, int size)
273 end = size - 1 - buf.head;
274 n = (end + buf.tail) & (size - 1);
275 return (n <= end) ? n : (end + 1);
278 static inline void me4000_outb(unsigned char value, unsigned long port)
280 PORT_PDEBUG("--> 0x%02X port 0x%04lX\n", value, port);
284 static inline void me4000_outl(unsigned long value, unsigned long port)
286 PORT_PDEBUG("--> 0x%08lX port 0x%04lX\n", value, port);
290 static inline unsigned long me4000_inl(unsigned long port)
294 PORT_PDEBUG("<-- 0x%08lX port 0x%04lX\n", value, port);
298 static inline unsigned char me4000_inb(unsigned long port)
302 PORT_PDEBUG("<-- 0x%08X port 0x%04lX\n", value, port);
306 static struct pci_driver me4000_driver = {
308 .id_table = me4000_pci_table,
309 .probe = me4000_probe
312 static struct file_operations me4000_ao_fops_sing = {
313 .owner = THIS_MODULE,
314 .write = me4000_ao_write_sing,
315 .ioctl = me4000_ao_ioctl_sing,
317 .release = me4000_release,
320 static struct file_operations me4000_ao_fops_wrap = {
321 .owner = THIS_MODULE,
322 .write = me4000_ao_write_wrap,
323 .ioctl = me4000_ao_ioctl_wrap,
325 .release = me4000_release,
328 static struct file_operations me4000_ao_fops_cont = {
329 .owner = THIS_MODULE,
330 .write = me4000_ao_write_cont,
331 .poll = me4000_ao_poll_cont,
332 .ioctl = me4000_ao_ioctl_cont,
334 .release = me4000_release,
335 .fsync = me4000_ao_fsync_cont,
338 static struct file_operations me4000_ai_fops_sing = {
339 .owner = THIS_MODULE,
340 .ioctl = me4000_ai_ioctl_sing,
342 .release = me4000_release,
345 static struct file_operations me4000_ai_fops_cont_sw = {
346 .owner = THIS_MODULE,
347 .read = me4000_ai_read,
348 .poll = me4000_ai_poll,
349 .ioctl = me4000_ai_ioctl_sw,
351 .release = me4000_release,
352 .fasync = me4000_ai_fasync,
355 static struct file_operations me4000_ai_fops_cont_et = {
356 .owner = THIS_MODULE,
357 .read = me4000_ai_read,
358 .poll = me4000_ai_poll,
359 .ioctl = me4000_ai_ioctl_ext,
361 .release = me4000_release,
364 static struct file_operations me4000_ai_fops_cont_et_value = {
365 .owner = THIS_MODULE,
366 .read = me4000_ai_read,
367 .poll = me4000_ai_poll,
368 .ioctl = me4000_ai_ioctl_ext,
370 .release = me4000_release,
373 static struct file_operations me4000_ai_fops_cont_et_chanlist = {
374 .owner = THIS_MODULE,
375 .read = me4000_ai_read,
376 .poll = me4000_ai_poll,
377 .ioctl = me4000_ai_ioctl_ext,
379 .release = me4000_release,
382 static struct file_operations me4000_dio_fops = {
383 .owner = THIS_MODULE,
384 .ioctl = me4000_dio_ioctl,
386 .release = me4000_release,
389 static struct file_operations me4000_cnt_fops = {
390 .owner = THIS_MODULE,
391 .ioctl = me4000_cnt_ioctl,
393 .release = me4000_release,
396 static struct file_operations me4000_ext_int_fops = {
397 .owner = THIS_MODULE,
398 .ioctl = me4000_ext_int_ioctl,
400 .release = me4000_release,
401 .fasync = me4000_ext_int_fasync,
404 static struct file_operations *me4000_ao_fops_array[] = {
405 &me4000_ao_fops_sing, // single operations
406 &me4000_ao_fops_wrap, // wraparound operations
407 &me4000_ao_fops_cont, // continous operations
410 static struct file_operations *me4000_ai_fops_array[] = {
411 &me4000_ai_fops_sing, // single operations
412 &me4000_ai_fops_cont_sw, // continuous operations with software start
413 &me4000_ai_fops_cont_et, // continous operations with external trigger
414 &me4000_ai_fops_cont_et_value, // sample values by external trigger
415 &me4000_ai_fops_cont_et_chanlist, // work through one channel list by external trigger
418 static int __init me4000_init_module(void)
422 CALL_PDEBUG("init_module() is executed\n");
424 /* Register driver capabilities */
425 result = pci_register_driver(&me4000_driver);
426 PDEBUG("init_module():%d devices detected\n", result);
428 printk(KERN_ERR "ME4000:init_module():Can't register driver\n");
432 /* Allocate major number for analog output */
434 register_chrdev(me4000_ao_major_driver_no, ME4000_AO_NAME,
435 &me4000_ao_fops_sing);
437 printk(KERN_ERR "ME4000:init_module():Can't get AO major no\n");
440 me4000_ao_major_driver_no = result;
442 PDEBUG("init_module():Major driver number for AO = %ld\n",
443 me4000_ao_major_driver_no);
445 /* Allocate major number for analog input */
447 register_chrdev(me4000_ai_major_driver_no, ME4000_AI_NAME,
448 &me4000_ai_fops_sing);
450 printk(KERN_ERR "ME4000:init_module():Can't get AI major no\n");
453 me4000_ai_major_driver_no = result;
455 PDEBUG("init_module():Major driver number for AI = %ld\n",
456 me4000_ai_major_driver_no);
458 /* Allocate major number for digital I/O */
460 register_chrdev(me4000_dio_major_driver_no, ME4000_DIO_NAME,
464 "ME4000:init_module():Can't get DIO major no\n");
467 me4000_dio_major_driver_no = result;
469 PDEBUG("init_module():Major driver number for DIO = %ld\n",
470 me4000_dio_major_driver_no);
472 /* Allocate major number for counter */
474 register_chrdev(me4000_cnt_major_driver_no, ME4000_CNT_NAME,
478 "ME4000:init_module():Can't get CNT major no\n");
481 me4000_cnt_major_driver_no = result;
483 PDEBUG("init_module():Major driver number for CNT = %ld\n",
484 me4000_cnt_major_driver_no);
486 /* Allocate major number for external interrupt */
488 register_chrdev(me4000_ext_int_major_driver_no, ME4000_EXT_INT_NAME,
489 &me4000_ext_int_fops);
492 "ME4000:init_module():Can't get major no for external interrupt\n");
495 me4000_ext_int_major_driver_no = result;
498 ("init_module():Major driver number for external interrupt = %ld\n",
499 me4000_ext_int_major_driver_no);
501 /* Create the /proc/me4000 entry */
502 if (!create_proc_read_entry
503 ("me4000", 0, NULL, me4000_read_procmem, NULL)) {
506 "ME4000:init_module():Can't create proc entry\n");
513 unregister_chrdev(me4000_ext_int_major_driver_no, ME4000_EXT_INT_NAME);
516 unregister_chrdev(me4000_cnt_major_driver_no, ME4000_CNT_NAME);
519 unregister_chrdev(me4000_dio_major_driver_no, ME4000_DIO_NAME);
522 unregister_chrdev(me4000_ai_major_driver_no, ME4000_AI_NAME);
525 unregister_chrdev(me4000_ao_major_driver_no, ME4000_AO_NAME);
528 pci_unregister_driver(&me4000_driver);
529 clear_board_info_list();
535 module_init(me4000_init_module);
537 static void clear_board_info_list(void)
539 struct me4000_info *board_info, *board_info_safe;
540 struct me4000_ao_context *ao_context, *ao_context_safe;
542 /* Clear context lists */
543 list_for_each_entry(board_info, &me4000_board_info_list, list) {
544 /* Clear analog output context list */
545 list_for_each_entry_safe(ao_context, ao_context_safe,
546 &board_info->ao_context_list, list) {
547 me4000_ao_reset(ao_context);
548 free_irq(ao_context->irq, ao_context);
549 if (ao_context->circ_buf.buf)
550 kfree(ao_context->circ_buf.buf);
551 list_del(&ao_context->list);
555 /* Clear analog input context */
556 if (board_info->ai_context->circ_buf.buf)
557 kfree(board_info->ai_context->circ_buf.buf);
558 kfree(board_info->ai_context);
560 /* Clear digital I/O context */
561 kfree(board_info->dio_context);
563 /* Clear counter context */
564 kfree(board_info->cnt_context);
566 /* Clear external interrupt context */
567 kfree(board_info->ext_int_context);
570 /* Clear the board info list */
571 list_for_each_entry_safe(board_info, board_info_safe,
572 &me4000_board_info_list, list) {
573 pci_release_regions(board_info->pci_dev_p);
574 list_del(&board_info->list);
579 static int get_registers(struct pci_dev *dev, struct me4000_info *board_info)
582 /*--------------------------- plx regbase ---------------------------------*/
584 board_info->plx_regbase = pci_resource_start(dev, 1);
585 if (board_info->plx_regbase == 0) {
587 "ME4000:get_registers():PCI base address 1 is not available\n");
590 board_info->plx_regbase_size = pci_resource_len(dev, 1);
593 ("get_registers():PLX configuration registers at address 0x%4lX [0x%4lX]\n",
594 board_info->plx_regbase, board_info->plx_regbase_size);
596 /*--------------------------- me4000 regbase ------------------------------*/
598 board_info->me4000_regbase = pci_resource_start(dev, 2);
599 if (board_info->me4000_regbase == 0) {
601 "ME4000:get_registers():PCI base address 2 is not available\n");
604 board_info->me4000_regbase_size = pci_resource_len(dev, 2);
606 PDEBUG("get_registers():ME4000 registers at address 0x%4lX [0x%4lX]\n",
607 board_info->me4000_regbase, board_info->me4000_regbase_size);
609 /*--------------------------- timer regbase ------------------------------*/
611 board_info->timer_regbase = pci_resource_start(dev, 3);
612 if (board_info->timer_regbase == 0) {
614 "ME4000:get_registers():PCI base address 3 is not available\n");
617 board_info->timer_regbase_size = pci_resource_len(dev, 3);
619 PDEBUG("get_registers():Timer registers at address 0x%4lX [0x%4lX]\n",
620 board_info->timer_regbase, board_info->timer_regbase_size);
622 /*--------------------------- program regbase ------------------------------*/
624 board_info->program_regbase = pci_resource_start(dev, 5);
625 if (board_info->program_regbase == 0) {
627 "get_registers():ME4000:PCI base address 5 is not available\n");
630 board_info->program_regbase_size = pci_resource_len(dev, 5);
632 PDEBUG("get_registers():Program registers at address 0x%4lX [0x%4lX]\n",
633 board_info->program_regbase, board_info->program_regbase_size);
638 static int init_board_info(struct pci_dev *pci_dev_p,
639 struct me4000_info *board_info)
643 struct list_head *board_p;
644 board_info->pci_dev_p = pci_dev_p;
646 for (i = 0; i < ARRAY_SIZE(me4000_boards); i++) {
647 if (me4000_boards[i].device_id == pci_dev_p->device) {
648 board_info->board_p = &me4000_boards[i];
652 if (i == ARRAY_SIZE(me4000_boards)) {
654 "ME4000:init_board_info():Device ID not valid\n");
658 /* Get the index of the board in the global list */
660 list_for_each(board_p, &me4000_board_info_list) {
661 if (board_p == &board_info->list) {
662 board_info->board_count = i;
667 if (board_p == &me4000_board_info_list) {
669 "ME4000:init_board_info():Cannot get index of board\n");
673 /* Init list head for analog output contexts */
674 INIT_LIST_HEAD(&board_info->ao_context_list);
676 /* Init spin locks */
677 spin_lock_init(&board_info->preload_lock);
678 spin_lock_init(&board_info->ai_ctrl_lock);
680 /* Get the serial number */
681 result = pci_read_config_dword(pci_dev_p, 0x2C, &board_info->serial_no);
682 if (result != PCIBIOS_SUCCESSFUL) {
684 "ME4000:init_board_info: Can't get serial_no\n");
687 PDEBUG("init_board_info():serial_no = 0x%x\n", board_info->serial_no);
689 /* Get the hardware revision */
691 pci_read_config_byte(pci_dev_p, 0x08, &board_info->hw_revision);
692 if (result != PCIBIOS_SUCCESSFUL) {
694 "ME4000:init_board_info():Can't get hw_revision\n");
697 PDEBUG("init_board_info():hw_revision = 0x%x\n",
698 board_info->hw_revision);
700 /* Get the vendor id */
701 board_info->vendor_id = pci_dev_p->vendor;
702 PDEBUG("init_board_info():vendor_id = 0x%x\n", board_info->vendor_id);
704 /* Get the device id */
705 board_info->device_id = pci_dev_p->device;
706 PDEBUG("init_board_info():device_id = 0x%x\n", board_info->device_id);
708 /* Get the pci device number */
709 board_info->pci_dev_no = PCI_FUNC(pci_dev_p->devfn);
710 PDEBUG("init_board_info():pci_func_no = 0x%x\n",
711 board_info->pci_func_no);
713 /* Get the pci slot number */
714 board_info->pci_dev_no = PCI_SLOT(pci_dev_p->devfn);
715 PDEBUG("init_board_info():pci_dev_no = 0x%x\n", board_info->pci_dev_no);
717 /* Get the pci bus number */
718 board_info->pci_bus_no = pci_dev_p->bus->number;
719 PDEBUG("init_board_info():pci_bus_no = 0x%x\n", board_info->pci_bus_no);
721 /* Get the irq assigned to the board */
722 board_info->irq = pci_dev_p->irq;
723 PDEBUG("init_board_info():irq = %d\n", board_info->irq);
728 static int alloc_ao_contexts(struct me4000_info *info)
732 struct me4000_ao_context *ao_context;
734 for (i = 0; i < info->board_p->ao.count; i++) {
735 ao_context = kzalloc(sizeof(struct me4000_ao_context),
739 "alloc_ao_contexts():Can't get memory for ao context\n");
740 release_ao_contexts(info);
744 spin_lock_init(&ao_context->use_lock);
745 spin_lock_init(&ao_context->int_lock);
746 ao_context->irq = info->irq;
747 init_waitqueue_head(&ao_context->wait_queue);
748 ao_context->board_info = info;
750 if (info->board_p->ao.fifo_count) {
751 /* Allocate circular buffer */
752 ao_context->circ_buf.buf =
753 kzalloc(ME4000_AO_BUFFER_SIZE, GFP_KERNEL);
754 if (!ao_context->circ_buf.buf) {
756 "alloc_ao_contexts():Can't get circular buffer\n");
757 release_ao_contexts(info);
761 /* Clear the circular buffer */
762 ao_context->circ_buf.head = 0;
763 ao_context->circ_buf.tail = 0;
768 ao_context->ctrl_reg =
769 info->me4000_regbase + ME4000_AO_00_CTRL_REG;
770 ao_context->status_reg =
771 info->me4000_regbase + ME4000_AO_00_STATUS_REG;
772 ao_context->fifo_reg =
773 info->me4000_regbase + ME4000_AO_00_FIFO_REG;
774 ao_context->single_reg =
775 info->me4000_regbase + ME4000_AO_00_SINGLE_REG;
776 ao_context->timer_reg =
777 info->me4000_regbase + ME4000_AO_00_TIMER_REG;
778 ao_context->irq_status_reg =
779 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
780 ao_context->preload_reg =
781 info->me4000_regbase + ME4000_AO_LOADSETREG_XX;
784 ao_context->ctrl_reg =
785 info->me4000_regbase + ME4000_AO_01_CTRL_REG;
786 ao_context->status_reg =
787 info->me4000_regbase + ME4000_AO_01_STATUS_REG;
788 ao_context->fifo_reg =
789 info->me4000_regbase + ME4000_AO_01_FIFO_REG;
790 ao_context->single_reg =
791 info->me4000_regbase + ME4000_AO_01_SINGLE_REG;
792 ao_context->timer_reg =
793 info->me4000_regbase + ME4000_AO_01_TIMER_REG;
794 ao_context->irq_status_reg =
795 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
796 ao_context->preload_reg =
797 info->me4000_regbase + ME4000_AO_LOADSETREG_XX;
800 ao_context->ctrl_reg =
801 info->me4000_regbase + ME4000_AO_02_CTRL_REG;
802 ao_context->status_reg =
803 info->me4000_regbase + ME4000_AO_02_STATUS_REG;
804 ao_context->fifo_reg =
805 info->me4000_regbase + ME4000_AO_02_FIFO_REG;
806 ao_context->single_reg =
807 info->me4000_regbase + ME4000_AO_02_SINGLE_REG;
808 ao_context->timer_reg =
809 info->me4000_regbase + ME4000_AO_02_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_03_CTRL_REG;
818 ao_context->status_reg =
819 info->me4000_regbase + ME4000_AO_03_STATUS_REG;
820 ao_context->fifo_reg =
821 info->me4000_regbase + ME4000_AO_03_FIFO_REG;
822 ao_context->single_reg =
823 info->me4000_regbase + ME4000_AO_03_SINGLE_REG;
824 ao_context->timer_reg =
825 info->me4000_regbase + ME4000_AO_03_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;
835 if (info->board_p->ao.fifo_count) {
836 /* Request the interrupt line */
838 request_irq(ao_context->irq, me4000_ao_isr,
839 IRQF_DISABLED | IRQF_SHARED,
840 ME4000_NAME, ao_context);
843 "%s:Can't get interrupt line", __func__);
844 kfree(ao_context->circ_buf.buf);
846 release_ao_contexts(info);
851 list_add_tail(&ao_context->list, &info->ao_context_list);
852 ao_context->index = i;
858 static void release_ao_contexts(struct me4000_info *board_info)
860 struct me4000_ao_context *ao_context, *ao_context_safe;
862 /* Clear analog output context list */
863 list_for_each_entry_safe(ao_context, ao_context_safe,
864 &board_info->ao_context_list, list) {
865 free_irq(ao_context->irq, ao_context);
866 kfree(ao_context->circ_buf.buf);
867 list_del(&ao_context->list);
872 static int alloc_ai_context(struct me4000_info *info)
874 struct me4000_ai_context *ai_context;
876 if (info->board_p->ai.count) {
877 ai_context = kzalloc(sizeof(struct me4000_ai_context),
881 "ME4000:alloc_ai_context():Can't get memory for ai context\n");
885 info->ai_context = ai_context;
887 spin_lock_init(&ai_context->use_lock);
888 spin_lock_init(&ai_context->int_lock);
889 ai_context->number = 0;
890 ai_context->irq = info->irq;
891 init_waitqueue_head(&ai_context->wait_queue);
892 ai_context->board_info = info;
894 ai_context->ctrl_reg =
895 info->me4000_regbase + ME4000_AI_CTRL_REG;
896 ai_context->status_reg =
897 info->me4000_regbase + ME4000_AI_STATUS_REG;
898 ai_context->channel_list_reg =
899 info->me4000_regbase + ME4000_AI_CHANNEL_LIST_REG;
900 ai_context->data_reg =
901 info->me4000_regbase + ME4000_AI_DATA_REG;
902 ai_context->chan_timer_reg =
903 info->me4000_regbase + ME4000_AI_CHAN_TIMER_REG;
904 ai_context->chan_pre_timer_reg =
905 info->me4000_regbase + ME4000_AI_CHAN_PRE_TIMER_REG;
906 ai_context->scan_timer_low_reg =
907 info->me4000_regbase + ME4000_AI_SCAN_TIMER_LOW_REG;
908 ai_context->scan_timer_high_reg =
909 info->me4000_regbase + ME4000_AI_SCAN_TIMER_HIGH_REG;
910 ai_context->scan_pre_timer_low_reg =
911 info->me4000_regbase + ME4000_AI_SCAN_PRE_TIMER_LOW_REG;
912 ai_context->scan_pre_timer_high_reg =
913 info->me4000_regbase + ME4000_AI_SCAN_PRE_TIMER_HIGH_REG;
914 ai_context->start_reg =
915 info->me4000_regbase + ME4000_AI_START_REG;
916 ai_context->irq_status_reg =
917 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
918 ai_context->sample_counter_reg =
919 info->me4000_regbase + ME4000_AI_SAMPLE_COUNTER_REG;
925 static int alloc_dio_context(struct me4000_info *info)
927 struct me4000_dio_context *dio_context;
929 if (info->board_p->dio.count) {
930 dio_context = kzalloc(sizeof(struct me4000_dio_context),
934 "ME4000:alloc_dio_context():Can't get memory for dio context\n");
938 info->dio_context = dio_context;
940 spin_lock_init(&dio_context->use_lock);
941 dio_context->board_info = info;
943 dio_context->dio_count = info->board_p->dio.count;
945 dio_context->dir_reg =
946 info->me4000_regbase + ME4000_DIO_DIR_REG;
947 dio_context->ctrl_reg =
948 info->me4000_regbase + ME4000_DIO_CTRL_REG;
949 dio_context->port_0_reg =
950 info->me4000_regbase + ME4000_DIO_PORT_0_REG;
951 dio_context->port_1_reg =
952 info->me4000_regbase + ME4000_DIO_PORT_1_REG;
953 dio_context->port_2_reg =
954 info->me4000_regbase + ME4000_DIO_PORT_2_REG;
955 dio_context->port_3_reg =
956 info->me4000_regbase + ME4000_DIO_PORT_3_REG;
962 static int alloc_cnt_context(struct me4000_info *info)
964 struct me4000_cnt_context *cnt_context;
966 if (info->board_p->cnt.count) {
967 cnt_context = kzalloc(sizeof(struct me4000_cnt_context),
971 "ME4000:alloc_cnt_context():Can't get memory for cnt context\n");
975 info->cnt_context = cnt_context;
977 spin_lock_init(&cnt_context->use_lock);
978 cnt_context->board_info = info;
980 cnt_context->ctrl_reg =
981 info->timer_regbase + ME4000_CNT_CTRL_REG;
982 cnt_context->counter_0_reg =
983 info->timer_regbase + ME4000_CNT_COUNTER_0_REG;
984 cnt_context->counter_1_reg =
985 info->timer_regbase + ME4000_CNT_COUNTER_1_REG;
986 cnt_context->counter_2_reg =
987 info->timer_regbase + ME4000_CNT_COUNTER_2_REG;
993 static int alloc_ext_int_context(struct me4000_info *info)
995 struct me4000_ext_int_context *ext_int_context;
997 if (info->board_p->cnt.count) {
999 kzalloc(sizeof(struct me4000_ext_int_context), GFP_KERNEL);
1000 if (!ext_int_context) {
1002 "ME4000:alloc_ext_int_context():Can't get memory for cnt context\n");
1006 info->ext_int_context = ext_int_context;
1008 spin_lock_init(&ext_int_context->use_lock);
1009 ext_int_context->board_info = info;
1011 ext_int_context->fasync_ptr = NULL;
1012 ext_int_context->irq = info->irq;
1014 ext_int_context->ctrl_reg =
1015 info->me4000_regbase + ME4000_AI_CTRL_REG;
1016 ext_int_context->irq_status_reg =
1017 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
1023 static int me4000_probe(struct pci_dev *dev, const struct pci_device_id *id)
1026 struct me4000_info *board_info;
1028 CALL_PDEBUG("me4000_probe() is executed\n");
1030 /* Allocate structure for board context */
1031 board_info = kzalloc(sizeof(struct me4000_info), GFP_KERNEL);
1034 "ME4000:Can't get memory for board info structure\n");
1039 /* Add to global linked list */
1040 list_add_tail(&board_info->list, &me4000_board_info_list);
1042 /* Get the PCI base registers */
1043 result = get_registers(dev, board_info);
1045 printk(KERN_ERR "%s:Cannot get registers\n", __func__);
1049 /* Enable the device */
1050 result = pci_enable_device(dev);
1052 printk(KERN_ERR "%s:Cannot enable PCI device\n", __func__);
1056 /* Request the PCI register regions */
1057 result = pci_request_regions(dev, ME4000_NAME);
1059 printk(KERN_ERR "%s:Cannot request I/O regions\n", __func__);
1063 /* Initialize board info */
1064 result = init_board_info(dev, board_info);
1066 printk(KERN_ERR "%s:Cannot init baord info\n", __func__);
1070 /* Download the xilinx firmware */
1071 result = me4000_xilinx_download(board_info);
1073 printk(KERN_ERR "%s:Can't download firmware\n", __func__);
1077 /* Make a hardware reset */
1078 result = me4000_reset_board(board_info);
1080 printk(KERN_ERR "%s :Can't reset board\n", __func__);
1084 /* Allocate analog output context structures */
1085 result = alloc_ao_contexts(board_info);
1087 printk(KERN_ERR "%s:Cannot allocate ao contexts\n", __func__);
1091 /* Allocate analog input context */
1092 result = alloc_ai_context(board_info);
1094 printk(KERN_ERR "%s:Cannot allocate ai context\n", __func__);
1098 /* Allocate digital I/O context */
1099 result = alloc_dio_context(board_info);
1101 printk(KERN_ERR "%s:Cannot allocate dio context\n", __func__);
1105 /* Allocate counter context */
1106 result = alloc_cnt_context(board_info);
1108 printk(KERN_ERR "%s:Cannot allocate cnt context\n", __func__);
1112 /* Allocate external interrupt context */
1113 result = alloc_ext_int_context(board_info);
1116 "%s:Cannot allocate ext_int context\n", __func__);
1123 kfree(board_info->cnt_context);
1126 kfree(board_info->dio_context);
1129 kfree(board_info->ai_context);
1132 release_ao_contexts(board_info);
1135 pci_release_regions(dev);
1138 list_del(&board_info->list);
1145 static int me4000_xilinx_download(struct me4000_info *info)
1150 unsigned char *firm;
1151 wait_queue_head_t queue;
1153 CALL_PDEBUG("me4000_xilinx_download() is executed\n");
1155 init_waitqueue_head(&queue);
1157 firm = (info->device_id == 0x4610) ? xilinx_firm_4610 : xilinx_firm;
1160 * Set PLX local interrupt 2 polarity to high.
1161 * Interrupt is thrown by init pin of xilinx.
1163 outl(0x10, info->plx_regbase + PLX_INTCSR);
1165 /* Set /CS and /WRITE of the Xilinx */
1166 value = inl(info->plx_regbase + PLX_ICR);
1168 outl(value, info->plx_regbase + PLX_ICR);
1170 /* Init Xilinx with CS1 */
1171 inb(info->program_regbase + 0xC8);
1173 /* Wait until /INIT pin is set */
1175 if (!(inl(info->plx_regbase + PLX_INTCSR) & 0x20)) {
1176 printk(KERN_ERR "%s:Can't init Xilinx\n", __func__);
1180 /* Reset /CS and /WRITE of the Xilinx */
1181 value = inl(info->plx_regbase + PLX_ICR);
1183 outl(value, info->plx_regbase + PLX_ICR);
1185 /* Download Xilinx firmware */
1186 size = (firm[0] << 24) + (firm[1] << 16) + (firm[2] << 8) + firm[3];
1189 for (idx = 0; idx < size; idx++) {
1190 outb(firm[16 + idx], info->program_regbase);
1194 /* Check if BUSY flag is low */
1195 if (inl(info->plx_regbase + PLX_ICR) & 0x20) {
1197 "%s:Xilinx is still busy (idx = %d)\n", __func__,
1203 PDEBUG("me4000_xilinx_download():%d bytes written\n", idx);
1205 /* If done flag is high download was successful */
1206 if (inl(info->plx_regbase + PLX_ICR) & 0x4) {
1207 PDEBUG("me4000_xilinx_download():Done flag is set\n");
1208 PDEBUG("me4000_xilinx_download():Download was successful\n");
1211 "ME4000:%s:DONE flag is not set\n", __func__);
1213 "ME4000:%s:Download not succesful\n", __func__);
1217 /* Set /CS and /WRITE */
1218 value = inl(info->plx_regbase + PLX_ICR);
1220 outl(value, info->plx_regbase + PLX_ICR);
1225 static int me4000_reset_board(struct me4000_info *info)
1229 CALL_PDEBUG("me4000_reset_board() is executed\n");
1231 /* Make a hardware reset */
1232 icr = me4000_inl(info->plx_regbase + PLX_ICR);
1234 me4000_outl(icr, info->plx_regbase + PLX_ICR);
1236 me4000_outl(icr, info->plx_regbase + PLX_ICR);
1238 /* Set both stop bits in the analog input control register */
1239 me4000_outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP | ME4000_AI_CTRL_BIT_STOP,
1240 info->me4000_regbase + ME4000_AI_CTRL_REG);
1242 /* Set both stop bits in the analog output control register */
1243 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1244 info->me4000_regbase + ME4000_AO_00_CTRL_REG);
1245 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1246 info->me4000_regbase + ME4000_AO_01_CTRL_REG);
1247 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1248 info->me4000_regbase + ME4000_AO_02_CTRL_REG);
1249 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1250 info->me4000_regbase + ME4000_AO_03_CTRL_REG);
1252 /* 0x8000 to the DACs means an output voltage of 0V */
1253 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_00_SINGLE_REG);
1254 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_01_SINGLE_REG);
1255 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_02_SINGLE_REG);
1256 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_03_SINGLE_REG);
1258 /* Enable interrupts on the PLX */
1259 me4000_outl(0x43, info->plx_regbase + PLX_INTCSR);
1261 /* Set the adustment register for AO demux */
1262 me4000_outl(ME4000_AO_DEMUX_ADJUST_VALUE,
1263 info->me4000_regbase + ME4000_AO_DEMUX_ADJUST_REG);
1265 /* Set digital I/O direction for port 0 to output on isolated versions */
1266 if (!(me4000_inl(info->me4000_regbase + ME4000_DIO_DIR_REG) & 0x1)) {
1267 me4000_outl(0x1, info->me4000_regbase + ME4000_DIO_CTRL_REG);
1273 static int me4000_open(struct inode *inode_p, struct file *file_p)
1275 int board, dev, mode;
1278 struct list_head *ptr;
1279 struct me4000_info *board_info = NULL;
1280 struct me4000_ao_context *ao_context = NULL;
1281 struct me4000_ai_context *ai_context = NULL;
1282 struct me4000_dio_context *dio_context = NULL;
1283 struct me4000_cnt_context *cnt_context = NULL;
1284 struct me4000_ext_int_context *ext_int_context = NULL;
1286 CALL_PDEBUG("me4000_open() is executed\n");
1289 if (MAJOR(inode_p->i_rdev) == me4000_ao_major_driver_no) {
1290 board = AO_BOARD(inode_p->i_rdev);
1291 dev = AO_PORT(inode_p->i_rdev);
1292 mode = AO_MODE(inode_p->i_rdev);
1294 PDEBUG("me4000_open():board = %d ao = %d mode = %d\n", board,
1297 /* Search for the board context */
1299 list_for_each(ptr, &me4000_board_info_list) {
1304 board_info = list_entry(ptr, struct me4000_info, list);
1306 if (ptr == &me4000_board_info_list) {
1308 "ME4000:me4000_open():Board %d not in device list\n",
1313 /* Search for the dac context */
1315 list_for_each(ptr, &board_info->ao_context_list) {
1320 ao_context = list_entry(ptr, struct me4000_ao_context, list);
1322 if (ptr == &board_info->ao_context_list) {
1324 "ME4000:me4000_open():Device %d not in device list\n",
1329 /* Check if mode is valid */
1332 "ME4000:me4000_open():Mode is not valid\n");
1336 /* Check if mode is valid for this AO */
1337 if ((mode != ME4000_AO_CONV_MODE_SINGLE)
1338 && (dev >= board_info->board_p->ao.fifo_count)) {
1340 "ME4000:me4000_open():AO %d only in single mode available\n",
1345 /* Check if already opened */
1346 spin_lock(&ao_context->use_lock);
1347 if (ao_context->dac_in_use) {
1349 "ME4000:me4000_open():AO %d already in use\n",
1351 spin_unlock(&ao_context->use_lock);
1354 ao_context->dac_in_use = 1;
1355 spin_unlock(&ao_context->use_lock);
1357 ao_context->mode = mode;
1359 /* Hold the context in private data */
1360 file_p->private_data = ao_context;
1362 /* Set file operations pointer */
1363 file_p->f_op = me4000_ao_fops_array[mode];
1365 err = me4000_ao_prepare(ao_context);
1367 ao_context->dac_in_use = 0;
1372 else if (MAJOR(inode_p->i_rdev) == me4000_ai_major_driver_no) {
1373 board = AI_BOARD(inode_p->i_rdev);
1374 mode = AI_MODE(inode_p->i_rdev);
1376 PDEBUG("me4000_open():ai board = %d mode = %d\n", board, mode);
1378 /* Search for the board context */
1380 list_for_each(ptr, &me4000_board_info_list) {
1385 board_info = list_entry(ptr, struct me4000_info, list);
1387 if (ptr == &me4000_board_info_list) {
1389 "ME4000:me4000_open():Board %d not in device list\n",
1394 ai_context = board_info->ai_context;
1396 /* Check if mode is valid */
1399 "ME4000:me4000_open():Mode is not valid\n");
1403 /* Check if already opened */
1404 spin_lock(&ai_context->use_lock);
1405 if (ai_context->in_use) {
1407 "ME4000:me4000_open():AI already in use\n");
1408 spin_unlock(&ai_context->use_lock);
1411 ai_context->in_use = 1;
1412 spin_unlock(&ai_context->use_lock);
1414 ai_context->mode = mode;
1416 /* Hold the context in private data */
1417 file_p->private_data = ai_context;
1419 /* Set file operations pointer */
1420 file_p->f_op = me4000_ai_fops_array[mode];
1422 /* Prepare analog input */
1423 me4000_ai_prepare(ai_context);
1426 else if (MAJOR(inode_p->i_rdev) == me4000_dio_major_driver_no) {
1427 board = DIO_BOARD(inode_p->i_rdev);
1431 PDEBUG("me4000_open():board = %d\n", board);
1433 /* Search for the board context */
1434 list_for_each_entry(board_info, &me4000_board_info_list, list) {
1435 if (board_info->board_count == board)
1439 if (&board_info->list == &me4000_board_info_list) {
1441 "ME4000:me4000_open():Board %d not in device list\n",
1446 /* Search for the dio context */
1447 dio_context = board_info->dio_context;
1449 /* Check if already opened */
1450 spin_lock(&dio_context->use_lock);
1451 if (dio_context->in_use) {
1453 "ME4000:me4000_open():DIO already in use\n");
1454 spin_unlock(&dio_context->use_lock);
1457 dio_context->in_use = 1;
1458 spin_unlock(&dio_context->use_lock);
1460 /* Hold the context in private data */
1461 file_p->private_data = dio_context;
1463 /* Set file operations pointer to single functions */
1464 file_p->f_op = &me4000_dio_fops;
1466 //me4000_dio_reset(dio_context);
1469 else if (MAJOR(inode_p->i_rdev) == me4000_cnt_major_driver_no) {
1470 board = CNT_BOARD(inode_p->i_rdev);
1474 PDEBUG("me4000_open():board = %d\n", board);
1476 /* Search for the board context */
1477 list_for_each_entry(board_info, &me4000_board_info_list, list) {
1478 if (board_info->board_count == board)
1482 if (&board_info->list == &me4000_board_info_list) {
1484 "ME4000:me4000_open():Board %d not in device list\n",
1489 /* Get the cnt context */
1490 cnt_context = board_info->cnt_context;
1492 /* Check if already opened */
1493 spin_lock(&cnt_context->use_lock);
1494 if (cnt_context->in_use) {
1496 "ME4000:me4000_open():CNT already in use\n");
1497 spin_unlock(&cnt_context->use_lock);
1500 cnt_context->in_use = 1;
1501 spin_unlock(&cnt_context->use_lock);
1503 /* Hold the context in private data */
1504 file_p->private_data = cnt_context;
1506 /* Set file operations pointer to single functions */
1507 file_p->f_op = &me4000_cnt_fops;
1509 /* External Interrupt */
1510 else if (MAJOR(inode_p->i_rdev) == me4000_ext_int_major_driver_no) {
1511 board = EXT_INT_BOARD(inode_p->i_rdev);
1515 PDEBUG("me4000_open():board = %d\n", board);
1517 /* Search for the board context */
1518 list_for_each_entry(board_info, &me4000_board_info_list, list) {
1519 if (board_info->board_count == board)
1523 if (&board_info->list == &me4000_board_info_list) {
1525 "ME4000:me4000_open():Board %d not in device list\n",
1530 /* Get the external interrupt context */
1531 ext_int_context = board_info->ext_int_context;
1533 /* Check if already opened */
1534 spin_lock(&cnt_context->use_lock);
1535 if (ext_int_context->in_use) {
1537 "ME4000:me4000_open():External interrupt already in use\n");
1538 spin_unlock(&ext_int_context->use_lock);
1541 ext_int_context->in_use = 1;
1542 spin_unlock(&ext_int_context->use_lock);
1544 /* Hold the context in private data */
1545 file_p->private_data = ext_int_context;
1547 /* Set file operations pointer to single functions */
1548 file_p->f_op = &me4000_ext_int_fops;
1550 /* Request the interrupt line */
1552 request_irq(ext_int_context->irq, me4000_ext_int_isr,
1553 IRQF_DISABLED | IRQF_SHARED, ME4000_NAME,
1557 "ME4000:me4000_open():Can't get interrupt line");
1558 ext_int_context->in_use = 0;
1562 /* Reset the counter */
1563 me4000_ext_int_disable(ext_int_context);
1565 printk(KERN_ERR "ME4000:me4000_open():Major number unknown\n");
1572 static int me4000_release(struct inode *inode_p, struct file *file_p)
1574 struct me4000_ao_context *ao_context;
1575 struct me4000_ai_context *ai_context;
1576 struct me4000_dio_context *dio_context;
1577 struct me4000_cnt_context *cnt_context;
1578 struct me4000_ext_int_context *ext_int_context;
1580 CALL_PDEBUG("me4000_release() is executed\n");
1582 if (MAJOR(inode_p->i_rdev) == me4000_ao_major_driver_no) {
1583 ao_context = file_p->private_data;
1585 /* Mark DAC as unused */
1586 ao_context->dac_in_use = 0;
1587 } else if (MAJOR(inode_p->i_rdev) == me4000_ai_major_driver_no) {
1588 ai_context = file_p->private_data;
1590 /* Reset the analog input */
1591 me4000_ai_reset(ai_context);
1593 /* Free the interrupt and the circular buffer */
1594 if (ai_context->mode) {
1595 free_irq(ai_context->irq, ai_context);
1596 kfree(ai_context->circ_buf.buf);
1597 ai_context->circ_buf.buf = NULL;
1598 ai_context->circ_buf.head = 0;
1599 ai_context->circ_buf.tail = 0;
1602 /* Mark AI as unused */
1603 ai_context->in_use = 0;
1604 } else if (MAJOR(inode_p->i_rdev) == me4000_dio_major_driver_no) {
1605 dio_context = file_p->private_data;
1607 /* Mark digital I/O as unused */
1608 dio_context->in_use = 0;
1609 } else if (MAJOR(inode_p->i_rdev) == me4000_cnt_major_driver_no) {
1610 cnt_context = file_p->private_data;
1612 /* Mark counters as unused */
1613 cnt_context->in_use = 0;
1614 } else if (MAJOR(inode_p->i_rdev) == me4000_ext_int_major_driver_no) {
1615 ext_int_context = file_p->private_data;
1617 /* Disable the externel interrupt */
1618 me4000_ext_int_disable(ext_int_context);
1620 free_irq(ext_int_context->irq, ext_int_context);
1622 /* Mark as unused */
1623 ext_int_context->in_use = 0;
1626 "ME4000:me4000_release():Major number unknown\n");
1633 /*------------------------------- Analog output stuff --------------------------------------*/
1635 static int me4000_ao_prepare(struct me4000_ao_context *ao_context)
1637 unsigned long flags;
1639 CALL_PDEBUG("me4000_ao_prepare() is executed\n");
1641 if (ao_context->mode == ME4000_AO_CONV_MODE_CONTINUOUS) {
1642 /* Only do anything if not already in the correct mode */
1643 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1644 if ((mode & ME4000_AO_CONV_MODE_CONTINUOUS)
1645 && (mode & ME4000_AO_CTRL_BIT_ENABLE_FIFO)) {
1649 /* Stop any conversion */
1650 me4000_ao_immediate_stop(ao_context);
1652 /* Set the control register to default state */
1653 spin_lock_irqsave(&ao_context->int_lock, flags);
1654 me4000_outl(ME4000_AO_CONV_MODE_CONTINUOUS |
1655 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1656 ME4000_AO_CTRL_BIT_STOP |
1657 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1658 ao_context->ctrl_reg);
1659 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1661 /* Set to fastest sample rate */
1662 me4000_outl(65, ao_context->timer_reg);
1663 } else if (ao_context->mode == ME4000_AO_CONV_MODE_WRAPAROUND) {
1664 /* Only do anything if not already in the correct mode */
1665 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1666 if ((mode & ME4000_AO_CONV_MODE_WRAPAROUND)
1667 && (mode & ME4000_AO_CTRL_BIT_ENABLE_FIFO)) {
1671 /* Stop any conversion */
1672 me4000_ao_immediate_stop(ao_context);
1674 /* Set the control register to default state */
1675 spin_lock_irqsave(&ao_context->int_lock, flags);
1676 me4000_outl(ME4000_AO_CONV_MODE_WRAPAROUND |
1677 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1678 ME4000_AO_CTRL_BIT_STOP |
1679 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1680 ao_context->ctrl_reg);
1681 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1683 /* Set to fastest sample rate */
1684 me4000_outl(65, ao_context->timer_reg);
1685 } else if (ao_context->mode == ME4000_AO_CONV_MODE_SINGLE) {
1686 /* Only do anything if not already in the correct mode */
1687 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1690 (ME4000_AO_CONV_MODE_WRAPAROUND |
1691 ME4000_AO_CONV_MODE_CONTINUOUS))) {
1695 /* Stop any conversion */
1696 me4000_ao_immediate_stop(ao_context);
1698 /* Clear the control register */
1699 spin_lock_irqsave(&ao_context->int_lock, flags);
1700 me4000_outl(0x0, ao_context->ctrl_reg);
1701 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1703 /* Set voltage to 0V */
1704 me4000_outl(0x8000, ao_context->single_reg);
1707 "ME4000:me4000_ao_prepare():Invalid mode specified\n");
1714 static int me4000_ao_reset(struct me4000_ao_context *ao_context)
1717 wait_queue_head_t queue;
1718 unsigned long flags;
1720 CALL_PDEBUG("me4000_ao_reset() is executed\n");
1722 init_waitqueue_head(&queue);
1724 if (ao_context->mode == ME4000_AO_CONV_MODE_WRAPAROUND) {
1726 * First stop conversion of the DAC before reconfigure.
1727 * This is essantial, cause of the state machine.
1728 * If not stopped before configuring mode, it could
1729 * walk in a undefined state.
1731 tmp = me4000_inl(ao_context->ctrl_reg);
1732 tmp |= ME4000_AO_CTRL_BIT_IMMEDIATE_STOP;
1733 me4000_outl(tmp, ao_context->ctrl_reg);
1735 wait_event_timeout(queue,
1736 (inl(ao_context->status_reg) &
1737 ME4000_AO_STATUS_BIT_FSM) == 0,
1740 /* Set to transparent mode */
1741 me4000_ao_simultaneous_disable(ao_context);
1743 /* Set to single mode in order to set default voltage */
1744 me4000_outl(0x0, ao_context->ctrl_reg);
1746 /* Set voltage to 0V */
1747 me4000_outl(0x8000, ao_context->single_reg);
1749 /* Set to fastest sample rate */
1750 me4000_outl(65, ao_context->timer_reg);
1752 /* Set the original mode and enable FIFO */
1753 me4000_outl(ME4000_AO_CONV_MODE_WRAPAROUND |
1754 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1755 ME4000_AO_CTRL_BIT_STOP |
1756 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1757 ao_context->ctrl_reg);
1758 } else if (ao_context->mode == ME4000_AO_CONV_MODE_CONTINUOUS) {
1760 * First stop conversion of the DAC before reconfigure.
1761 * This is essantial, cause of the state machine.
1762 * If not stopped before configuring mode, it could
1763 * walk in a undefined state.
1765 spin_lock_irqsave(&ao_context->int_lock, flags);
1766 tmp = me4000_inl(ao_context->ctrl_reg);
1767 tmp |= ME4000_AO_CTRL_BIT_STOP;
1768 me4000_outl(tmp, ao_context->ctrl_reg);
1769 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1771 wait_event_timeout(queue,
1772 (inl(ao_context->status_reg) &
1773 ME4000_AO_STATUS_BIT_FSM) == 0,
1776 /* Clear the circular buffer */
1777 ao_context->circ_buf.head = 0;
1778 ao_context->circ_buf.tail = 0;
1780 /* Set to transparent mode */
1781 me4000_ao_simultaneous_disable(ao_context);
1783 /* Set to single mode in order to set default voltage */
1784 spin_lock_irqsave(&ao_context->int_lock, flags);
1785 tmp = me4000_inl(ao_context->ctrl_reg);
1786 me4000_outl(0x0, ao_context->ctrl_reg);
1788 /* Set voltage to 0V */
1789 me4000_outl(0x8000, ao_context->single_reg);
1791 /* Set to fastest sample rate */
1792 me4000_outl(65, ao_context->timer_reg);
1794 /* Set the original mode and enable FIFO */
1795 me4000_outl(ME4000_AO_CONV_MODE_CONTINUOUS |
1796 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1797 ME4000_AO_CTRL_BIT_STOP |
1798 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1799 ao_context->ctrl_reg);
1800 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1802 /* Set to transparent mode */
1803 me4000_ao_simultaneous_disable(ao_context);
1805 /* Set voltage to 0V */
1806 me4000_outl(0x8000, ao_context->single_reg);
1812 static ssize_t me4000_ao_write_sing(struct file *filep, const char *buff,
1813 size_t cnt, loff_t *offp)
1815 struct me4000_ao_context *ao_context = filep->private_data;
1817 const u16 *buffer = (const u16 *)buff;
1819 CALL_PDEBUG("me4000_ao_write_sing() is executed\n");
1823 "%s:Write count is not 2\n", __func__);
1827 if (get_user(value, buffer)) {
1829 "%s:Cannot copy data from user\n", __func__);
1833 me4000_outl(value, ao_context->single_reg);
1838 static ssize_t me4000_ao_write_wrap(struct file *filep, const char *buff,
1839 size_t cnt, loff_t *offp)
1841 struct me4000_ao_context *ao_context = filep->private_data;
1845 const u16 *buffer = (const u16 *)buff;
1846 size_t count = cnt / 2;
1848 CALL_PDEBUG("me4000_ao_write_wrap() is executed\n");
1850 /* Check if a conversion is already running */
1851 if (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
1853 "%s:There is already a conversion running\n", __func__);
1857 if (count > ME4000_AO_FIFO_COUNT) {
1859 "%s:Can't load more than %d values\n", __func__,
1860 ME4000_AO_FIFO_COUNT);
1864 /* Reset the FIFO */
1865 tmp = inl(ao_context->ctrl_reg);
1866 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_FIFO;
1867 outl(tmp, ao_context->ctrl_reg);
1868 tmp |= ME4000_AO_CTRL_BIT_ENABLE_FIFO;
1869 outl(tmp, ao_context->ctrl_reg);
1871 for (i = 0; i < count; i++) {
1872 if (get_user(value, buffer + i)) {
1874 "%s:Cannot copy data from user\n", __func__);
1877 if (((ao_context->fifo_reg & 0xFF) == ME4000_AO_01_FIFO_REG)
1878 || ((ao_context->fifo_reg & 0xFF) == ME4000_AO_03_FIFO_REG))
1879 value = value << 16;
1880 outl(value, ao_context->fifo_reg);
1882 CALL_PDEBUG("me4000_ao_write_wrap() is leaved with %d\n", i * 2);
1887 static ssize_t me4000_ao_write_cont(struct file *filep, const char *buff,
1888 size_t cnt, loff_t *offp)
1890 struct me4000_ao_context *ao_context = filep->private_data;
1891 const u16 *buffer = (const u16 *)buff;
1892 size_t count = cnt / 2;
1893 unsigned long flags;
1901 wait_queue_head_t queue;
1903 CALL_PDEBUG("me4000_ao_write_cont() is executed\n");
1905 init_waitqueue_head(&queue);
1909 PDEBUG("me4000_ao_write_cont():Count is 0\n");
1913 if (filep->f_flags & O_APPEND) {
1914 PDEBUG("me4000_ao_write_cont():Append data to data stream\n");
1916 if (filep->f_flags & O_NONBLOCK) {
1917 if (ao_context->pipe_flag) {
1919 "ME4000:me4000_ao_write_cont():Broken pipe in nonblocking write\n");
1922 c = me4000_space_to_end(ao_context->circ_buf,
1923 ME4000_AO_BUFFER_COUNT);
1926 ("me4000_ao_write_cont():Returning from nonblocking write\n");
1930 wait_event_interruptible(ao_context->wait_queue,
1933 (ao_context->circ_buf,
1934 ME4000_AO_BUFFER_COUNT)));
1935 if (ao_context->pipe_flag) {
1937 "me4000_ao_write_cont():Broken pipe in blocking write\n");
1940 if (signal_pending(current)) {
1942 "me4000_ao_write_cont():Wait for free buffer interrupted from signal\n");
1947 PDEBUG("me4000_ao_write_cont():Space to end = %d\n", c);
1949 /* Only able to write size of free buffer or size of count */
1954 k -= copy_from_user(ao_context->circ_buf.buf +
1955 ao_context->circ_buf.head, buffer,
1959 ("me4000_ao_write_cont():Copy %d values from user space\n",
1965 ao_context->circ_buf.head =
1966 (ao_context->circ_buf.head +
1967 c) & (ME4000_AO_BUFFER_COUNT - 1);
1972 /* Values are now available so enable interrupts */
1973 spin_lock_irqsave(&ao_context->int_lock, flags);
1974 if (me4000_buf_count
1975 (ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
1976 tmp = me4000_inl(ao_context->ctrl_reg);
1977 tmp |= ME4000_AO_CTRL_BIT_ENABLE_IRQ;
1978 me4000_outl(tmp, ao_context->ctrl_reg);
1980 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1983 /* Wait until the state machine is stopped if O_SYNC is set */
1984 if (filep->f_flags & O_SYNC) {
1985 while (inl(ao_context->status_reg) &
1986 ME4000_AO_STATUS_BIT_FSM) {
1987 interruptible_sleep_on_timeout(&queue, 1);
1988 if (ao_context->pipe_flag) {
1990 ("me4000_ao_write_cont():Broken pipe detected after sync\n");
1993 if (signal_pending(current)) {
1995 "me4000_ao_write_cont():Wait on state machine after sync interrupted\n");
2001 PDEBUG("me4000_ao_write_cont():Preload DAC FIFO\n");
2002 if ((me4000_inl(ao_context->status_reg) &
2003 ME4000_AO_STATUS_BIT_FSM)) {
2005 "me4000_ao_write_cont():Can't Preload DAC FIFO while conversion is running\n");
2009 /* Clear the FIFO */
2010 spin_lock_irqsave(&ao_context->int_lock, flags);
2011 tmp = me4000_inl(ao_context->ctrl_reg);
2013 ~(ME4000_AO_CTRL_BIT_ENABLE_FIFO |
2014 ME4000_AO_CTRL_BIT_ENABLE_IRQ);
2015 me4000_outl(tmp, ao_context->ctrl_reg);
2016 tmp |= ME4000_AO_CTRL_BIT_ENABLE_FIFO;
2017 me4000_outl(tmp, ao_context->ctrl_reg);
2018 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2020 /* Clear the circular buffer */
2021 ao_context->circ_buf.head = 0;
2022 ao_context->circ_buf.tail = 0;
2024 /* Reset the broken pipe flag */
2025 ao_context->pipe_flag = 0;
2027 /* Only able to write size of fifo or count */
2028 c = ME4000_AO_FIFO_COUNT;
2033 ("me4000_ao_write_cont():Write %d values to DAC on 0x%lX\n",
2034 c, ao_context->fifo_reg);
2036 /* Write values to the fifo */
2037 for (i = 0; i < c; i++) {
2038 if (get_user(svalue, buffer))
2041 if (((ao_context->fifo_reg & 0xFF) ==
2042 ME4000_AO_01_FIFO_REG)
2043 || ((ao_context->fifo_reg & 0xFF) ==
2044 ME4000_AO_03_FIFO_REG)) {
2045 lvalue = ((u32) svalue) << 16;
2047 lvalue = (u32) svalue;
2049 outl(lvalue, ao_context->fifo_reg);
2056 /* Get free buffer */
2057 c = me4000_space_to_end(ao_context->circ_buf,
2058 ME4000_AO_BUFFER_COUNT);
2063 /* Only able to write size of free buffer or size of count */
2067 /* If count = 0 return to user */
2070 ("me4000_ao_write_cont():Count reached 0\n");
2075 k -= copy_from_user(ao_context->circ_buf.buf +
2076 ao_context->circ_buf.head, buffer,
2080 ("me4000_ao_write_cont():Wrote %d values to buffer\n",
2086 ao_context->circ_buf.head =
2087 (ao_context->circ_buf.head +
2088 c) & (ME4000_AO_BUFFER_COUNT - 1);
2093 /* If values in the buffer are available so enable interrupts */
2094 spin_lock_irqsave(&ao_context->int_lock, flags);
2095 if (me4000_buf_count
2096 (ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
2098 ("me4000_ao_write_cont():Enable Interrupts\n");
2099 tmp = me4000_inl(ao_context->ctrl_reg);
2100 tmp |= ME4000_AO_CTRL_BIT_ENABLE_IRQ;
2101 me4000_outl(tmp, ao_context->ctrl_reg);
2103 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2107 if (filep->f_flags & O_NONBLOCK) {
2108 return (ret == 0) ? -EAGAIN : 2 * ret;
2114 static unsigned int me4000_ao_poll_cont(struct file *file_p, poll_table *wait)
2116 struct me4000_ao_context *ao_context;
2117 unsigned long mask = 0;
2119 CALL_PDEBUG("me4000_ao_poll_cont() is executed\n");
2121 ao_context = file_p->private_data;
2123 poll_wait(file_p, &ao_context->wait_queue, wait);
2125 /* Get free buffer */
2126 if (me4000_space_to_end(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT))
2127 mask |= POLLOUT | POLLWRNORM;
2129 CALL_PDEBUG("me4000_ao_poll_cont():Return mask %lX\n", mask);
2134 static int me4000_ao_fsync_cont(struct file *file_p, struct dentry *dentry_p,
2137 struct me4000_ao_context *ao_context;
2138 wait_queue_head_t queue;
2140 CALL_PDEBUG("me4000_ao_fsync_cont() is executed\n");
2142 ao_context = file_p->private_data;
2143 init_waitqueue_head(&queue);
2145 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2146 interruptible_sleep_on_timeout(&queue, 1);
2147 wait_event_interruptible_timeout(queue,
2148 !(inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM),
2150 if (ao_context->pipe_flag) {
2152 "%s:Broken pipe detected\n", __func__);
2156 if (signal_pending(current)) {
2158 "%s:Wait on state machine interrupted\n",
2167 static int me4000_ao_ioctl_sing(struct inode *inode_p, struct file *file_p,
2168 unsigned int service, unsigned long arg)
2170 struct me4000_ao_context *ao_context;
2172 CALL_PDEBUG("me4000_ao_ioctl_sing() is executed\n");
2174 ao_context = file_p->private_data;
2176 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2178 PDEBUG("me4000_ao_ioctl_sing():Wrong magic number\n");
2182 case ME4000_AO_EX_TRIG_SETUP:
2183 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2184 case ME4000_AO_EX_TRIG_ENABLE:
2185 return me4000_ao_ex_trig_enable(ao_context);
2186 case ME4000_AO_EX_TRIG_DISABLE:
2187 return me4000_ao_ex_trig_disable(ao_context);
2188 case ME4000_AO_PRELOAD:
2189 return me4000_ao_preload(ao_context);
2190 case ME4000_AO_PRELOAD_UPDATE:
2191 return me4000_ao_preload_update(ao_context);
2192 case ME4000_GET_USER_INFO:
2193 return me4000_get_user_info((struct me4000_user_info *)arg,
2194 ao_context->board_info);
2195 case ME4000_AO_SIMULTANEOUS_EX_TRIG:
2196 return me4000_ao_simultaneous_ex_trig(ao_context);
2197 case ME4000_AO_SIMULTANEOUS_SW:
2198 return me4000_ao_simultaneous_sw(ao_context);
2199 case ME4000_AO_SIMULTANEOUS_DISABLE:
2200 return me4000_ao_simultaneous_disable(ao_context);
2201 case ME4000_AO_SIMULTANEOUS_UPDATE:
2203 me4000_ao_simultaneous_update(
2204 (struct me4000_ao_channel_list *)arg,
2206 case ME4000_AO_EX_TRIG_TIMEOUT:
2207 return me4000_ao_ex_trig_timeout((unsigned long *)arg,
2209 case ME4000_AO_DISABLE_DO:
2210 return me4000_ao_disable_do(ao_context);
2213 "me4000_ao_ioctl_sing():Service number invalid\n");
2220 static int me4000_ao_ioctl_wrap(struct inode *inode_p, struct file *file_p,
2221 unsigned int service, unsigned long arg)
2223 struct me4000_ao_context *ao_context;
2225 CALL_PDEBUG("me4000_ao_ioctl_wrap() is executed\n");
2227 ao_context = file_p->private_data;
2229 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2231 PDEBUG("me4000_ao_ioctl_wrap():Wrong magic number\n");
2235 case ME4000_AO_START:
2236 return me4000_ao_start((unsigned long *)arg, ao_context);
2237 case ME4000_AO_STOP:
2238 return me4000_ao_stop(ao_context);
2239 case ME4000_AO_IMMEDIATE_STOP:
2240 return me4000_ao_immediate_stop(ao_context);
2241 case ME4000_AO_RESET:
2242 return me4000_ao_reset(ao_context);
2243 case ME4000_AO_TIMER_SET_DIVISOR:
2244 return me4000_ao_timer_set_divisor((u32 *) arg, ao_context);
2245 case ME4000_AO_EX_TRIG_SETUP:
2246 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2247 case ME4000_AO_EX_TRIG_ENABLE:
2248 return me4000_ao_ex_trig_enable(ao_context);
2249 case ME4000_AO_EX_TRIG_DISABLE:
2250 return me4000_ao_ex_trig_disable(ao_context);
2251 case ME4000_GET_USER_INFO:
2252 return me4000_get_user_info((struct me4000_user_info *)arg,
2253 ao_context->board_info);
2254 case ME4000_AO_FSM_STATE:
2255 return me4000_ao_fsm_state((int *)arg, ao_context);
2256 case ME4000_AO_ENABLE_DO:
2257 return me4000_ao_enable_do(ao_context);
2258 case ME4000_AO_DISABLE_DO:
2259 return me4000_ao_disable_do(ao_context);
2260 case ME4000_AO_SYNCHRONOUS_EX_TRIG:
2261 return me4000_ao_synchronous_ex_trig(ao_context);
2262 case ME4000_AO_SYNCHRONOUS_SW:
2263 return me4000_ao_synchronous_sw(ao_context);
2264 case ME4000_AO_SYNCHRONOUS_DISABLE:
2265 return me4000_ao_synchronous_disable(ao_context);
2272 static int me4000_ao_ioctl_cont(struct inode *inode_p, struct file *file_p,
2273 unsigned int service, unsigned long arg)
2275 struct me4000_ao_context *ao_context;
2277 CALL_PDEBUG("me4000_ao_ioctl_cont() is executed\n");
2279 ao_context = file_p->private_data;
2281 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2283 PDEBUG("me4000_ao_ioctl_cont():Wrong magic number\n");
2287 case ME4000_AO_START:
2288 return me4000_ao_start((unsigned long *)arg, ao_context);
2289 case ME4000_AO_STOP:
2290 return me4000_ao_stop(ao_context);
2291 case ME4000_AO_IMMEDIATE_STOP:
2292 return me4000_ao_immediate_stop(ao_context);
2293 case ME4000_AO_RESET:
2294 return me4000_ao_reset(ao_context);
2295 case ME4000_AO_TIMER_SET_DIVISOR:
2296 return me4000_ao_timer_set_divisor((u32 *) arg, ao_context);
2297 case ME4000_AO_EX_TRIG_SETUP:
2298 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2299 case ME4000_AO_EX_TRIG_ENABLE:
2300 return me4000_ao_ex_trig_enable(ao_context);
2301 case ME4000_AO_EX_TRIG_DISABLE:
2302 return me4000_ao_ex_trig_disable(ao_context);
2303 case ME4000_AO_ENABLE_DO:
2304 return me4000_ao_enable_do(ao_context);
2305 case ME4000_AO_DISABLE_DO:
2306 return me4000_ao_disable_do(ao_context);
2307 case ME4000_AO_FSM_STATE:
2308 return me4000_ao_fsm_state((int *)arg, ao_context);
2309 case ME4000_GET_USER_INFO:
2310 return me4000_get_user_info((struct me4000_user_info *)arg,
2311 ao_context->board_info);
2312 case ME4000_AO_SYNCHRONOUS_EX_TRIG:
2313 return me4000_ao_synchronous_ex_trig(ao_context);
2314 case ME4000_AO_SYNCHRONOUS_SW:
2315 return me4000_ao_synchronous_sw(ao_context);
2316 case ME4000_AO_SYNCHRONOUS_DISABLE:
2317 return me4000_ao_synchronous_disable(ao_context);
2318 case ME4000_AO_GET_FREE_BUFFER:
2319 return me4000_ao_get_free_buffer((unsigned long *)arg,
2327 static int me4000_ao_start(unsigned long *arg,
2328 struct me4000_ao_context *ao_context)
2331 wait_queue_head_t queue;
2333 unsigned long timeout;
2334 unsigned long flags;
2336 CALL_PDEBUG("me4000_ao_start() is executed\n");
2338 if (get_user(timeout, arg)) {
2340 "me4000_ao_start():Cannot copy data from user\n");
2344 init_waitqueue_head(&queue);
2346 spin_lock_irqsave(&ao_context->int_lock, flags);
2347 tmp = inl(ao_context->ctrl_reg);
2348 tmp &= ~(ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2349 me4000_outl(tmp, ao_context->ctrl_reg);
2350 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2352 if ((tmp & ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG)) {
2356 (inl(ao_context->status_reg) &
2357 ME4000_AO_STATUS_BIT_FSM)) {
2358 interruptible_sleep_on_timeout(&queue, 1);
2359 if (signal_pending(current)) {
2361 "ME4000:me4000_ao_start():Wait on start of state machine interrupted\n");
2364 /* kernel 2.6 has different definitions for HZ
2365 * in user and kernel space */
2366 if ((jiffies - ref) > (timeout * HZ / USER_HZ)) {
2368 "ME4000:me4000_ao_start():Timeout reached\n");
2374 me4000_outl(0x8000, ao_context->single_reg);
2380 static int me4000_ao_stop(struct me4000_ao_context *ao_context)
2383 wait_queue_head_t queue;
2384 unsigned long flags;
2386 init_waitqueue_head(&queue);
2388 CALL_PDEBUG("me4000_ao_stop() is executed\n");
2390 /* Set the stop bit */
2391 spin_lock_irqsave(&ao_context->int_lock, flags);
2392 tmp = inl(ao_context->ctrl_reg);
2393 tmp |= ME4000_AO_CTRL_BIT_STOP;
2394 me4000_outl(tmp, ao_context->ctrl_reg);
2395 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2397 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2398 interruptible_sleep_on_timeout(&queue, 1);
2399 if (signal_pending(current)) {
2401 "me4000_ao_stop():Wait on state machine after stop interrupted\n");
2406 /* Clear the stop bit */
2407 //tmp &= ~ME4000_AO_CTRL_BIT_STOP;
2408 //me4000_outl(tmp, ao_context->ctrl_reg);
2413 static int me4000_ao_immediate_stop(struct me4000_ao_context *ao_context)
2416 wait_queue_head_t queue;
2417 unsigned long flags;
2419 init_waitqueue_head(&queue);
2421 CALL_PDEBUG("me4000_ao_immediate_stop() is executed\n");
2423 spin_lock_irqsave(&ao_context->int_lock, flags);
2424 tmp = inl(ao_context->ctrl_reg);
2425 tmp |= ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP;
2426 me4000_outl(tmp, ao_context->ctrl_reg);
2427 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2429 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2430 interruptible_sleep_on_timeout(&queue, 1);
2431 if (signal_pending(current)) {
2433 "me4000_ao_immediate_stop():Wait on state machine after stop interrupted\n");
2438 /* Clear the stop bits */
2439 //tmp &= ~(ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2440 //me4000_outl(tmp, ao_context->ctrl_reg);
2445 static int me4000_ao_timer_set_divisor(u32 *arg,
2446 struct me4000_ao_context *ao_context)
2451 CALL_PDEBUG("me4000_ao_timer set_divisor() is executed\n");
2453 if (get_user(divisor, arg))
2456 /* Check if the state machine is stopped */
2457 tmp = me4000_inl(ao_context->status_reg);
2458 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2460 "me4000_ao_timer_set_divisor():Can't set timer while DAC is running\n");
2464 PDEBUG("me4000_ao_timer set_divisor():Divisor from user = %d\n",
2467 /* Check if the divisor is right. ME4000_AO_MIN_TICKS is the lowest */
2468 if (divisor < ME4000_AO_MIN_TICKS) {
2470 "ME4000:me4000_ao_timer set_divisor():Divisor to low\n");
2474 /* Fix bug in Firmware */
2477 PDEBUG("me4000_ao_timer set_divisor():Divisor to HW = %d\n", divisor);
2479 /* Write the divisor */
2480 me4000_outl(divisor, ao_context->timer_reg);
2485 static int me4000_ao_ex_trig_set_edge(int *arg,
2486 struct me4000_ao_context *ao_context)
2490 unsigned long flags;
2492 CALL_PDEBUG("me4000_ao_ex_trig_set_edge() is executed\n");
2494 if (get_user(mode, arg))
2497 /* Check if the state machine is stopped */
2498 tmp = me4000_inl(ao_context->status_reg);
2499 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2501 "me4000_ao_ex_trig_set_edge():Can't set trigger while DAC is running\n");
2505 if (mode == ME4000_AO_TRIGGER_EXT_EDGE_RISING) {
2506 spin_lock_irqsave(&ao_context->int_lock, flags);
2507 tmp = me4000_inl(ao_context->ctrl_reg);
2509 ~(ME4000_AO_CTRL_BIT_EX_TRIG_EDGE |
2510 ME4000_AO_CTRL_BIT_EX_TRIG_BOTH);
2511 me4000_outl(tmp, ao_context->ctrl_reg);
2512 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2513 } else if (mode == ME4000_AO_TRIGGER_EXT_EDGE_FALLING) {
2514 spin_lock_irqsave(&ao_context->int_lock, flags);
2515 tmp = me4000_inl(ao_context->ctrl_reg);
2516 tmp &= ~ME4000_AO_CTRL_BIT_EX_TRIG_BOTH;
2517 tmp |= ME4000_AO_CTRL_BIT_EX_TRIG_EDGE;
2518 me4000_outl(tmp, ao_context->ctrl_reg);
2519 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2520 } else if (mode == ME4000_AO_TRIGGER_EXT_EDGE_BOTH) {
2521 spin_lock_irqsave(&ao_context->int_lock, flags);
2522 tmp = me4000_inl(ao_context->ctrl_reg);
2524 ME4000_AO_CTRL_BIT_EX_TRIG_EDGE |
2525 ME4000_AO_CTRL_BIT_EX_TRIG_BOTH;
2526 me4000_outl(tmp, ao_context->ctrl_reg);
2527 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2530 "me4000_ao_ex_trig_set_edge():Invalid trigger mode\n");
2537 static int me4000_ao_ex_trig_enable(struct me4000_ao_context *ao_context)
2540 unsigned long flags;
2542 CALL_PDEBUG("me4000_ao_ex_trig_enable() is executed\n");
2544 /* Check if the state machine is stopped */
2545 tmp = me4000_inl(ao_context->status_reg);
2546 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2548 "me4000_ao_ex_trig_enable():Can't enable trigger while DAC is running\n");
2552 spin_lock_irqsave(&ao_context->int_lock, flags);
2553 tmp = me4000_inl(ao_context->ctrl_reg);
2554 tmp |= ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG;
2555 me4000_outl(tmp, ao_context->ctrl_reg);
2556 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2561 static int me4000_ao_ex_trig_disable(struct me4000_ao_context *ao_context)
2564 unsigned long flags;
2566 CALL_PDEBUG("me4000_ao_ex_trig_disable() is executed\n");
2568 /* Check if the state machine is stopped */
2569 tmp = me4000_inl(ao_context->status_reg);
2570 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2572 "me4000_ao_ex_trig_disable():Can't disable trigger while DAC is running\n");
2576 spin_lock_irqsave(&ao_context->int_lock, flags);
2577 tmp = me4000_inl(ao_context->ctrl_reg);
2578 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG;
2579 me4000_outl(tmp, ao_context->ctrl_reg);
2580 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2585 static int me4000_ao_simultaneous_disable(struct me4000_ao_context *ao_context)
2589 CALL_PDEBUG("me4000_ao_simultaneous_disable() is executed\n");
2591 /* Check if the state machine is stopped */
2592 /* Be careful here because this function is called from
2593 me4000_ao_synchronous disable */
2594 tmp = me4000_inl(ao_context->status_reg);
2595 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2597 "me4000_ao_simultaneous_disable():Can't disable while DAC is running\n");
2601 spin_lock(&ao_context->board_info->preload_lock);
2602 tmp = me4000_inl(ao_context->preload_reg);
2603 tmp &= ~(0x1 << ao_context->index); // Disable preload bit
2604 tmp &= ~(0x1 << (ao_context->index + 16)); // Disable hw simultaneous bit
2605 me4000_outl(tmp, ao_context->preload_reg);
2606 spin_unlock(&ao_context->board_info->preload_lock);
2611 static int me4000_ao_simultaneous_ex_trig(struct me4000_ao_context *ao_context)
2615 CALL_PDEBUG("me4000_ao_simultaneous_ex_trig() is executed\n");
2617 spin_lock(&ao_context->board_info->preload_lock);
2618 tmp = me4000_inl(ao_context->preload_reg);
2619 tmp |= (0x1 << ao_context->index); // Enable preload bit
2620 tmp |= (0x1 << (ao_context->index + 16)); // Enable hw simultaneous bit
2621 me4000_outl(tmp, ao_context->preload_reg);
2622 spin_unlock(&ao_context->board_info->preload_lock);
2627 static int me4000_ao_simultaneous_sw(struct me4000_ao_context *ao_context)
2631 CALL_PDEBUG("me4000_ao_simultaneous_sw() is executed\n");
2633 spin_lock(&ao_context->board_info->preload_lock);
2634 tmp = me4000_inl(ao_context->preload_reg);
2635 tmp |= (0x1 << ao_context->index); // Enable preload bit
2636 tmp &= ~(0x1 << (ao_context->index + 16)); // Disable hw simultaneous bit
2637 me4000_outl(tmp, ao_context->preload_reg);
2638 spin_unlock(&ao_context->board_info->preload_lock);
2643 static int me4000_ao_preload(struct me4000_ao_context *ao_context)
2645 CALL_PDEBUG("me4000_ao_preload() is executed\n");
2646 return me4000_ao_simultaneous_sw(ao_context);
2649 static int me4000_ao_preload_update(struct me4000_ao_context *ao_context)
2653 struct list_head *entry;
2655 CALL_PDEBUG("me4000_ao_preload_update() is executed\n");
2657 spin_lock(&ao_context->board_info->preload_lock);
2658 tmp = me4000_inl(ao_context->preload_reg);
2659 list_for_each(entry, &ao_context->board_info->ao_context_list) {
2660 /* The channels we update must be in the following state :
2662 - Hardware trigger is disabled
2663 - Corresponding simultaneous bit is reset
2665 ctrl = me4000_inl(ao_context->ctrl_reg);
2668 (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1 |
2669 ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG))) {
2673 (((struct me4000_ao_context *)entry)->index
2677 (((struct me4000_ao_context *)entry)->
2682 me4000_outl(tmp, ao_context->preload_reg);
2683 spin_unlock(&ao_context->board_info->preload_lock);
2688 static int me4000_ao_simultaneous_update(struct me4000_ao_channel_list *arg,
2689 struct me4000_ao_context *ao_context)
2694 struct me4000_ao_channel_list channels;
2696 CALL_PDEBUG("me4000_ao_simultaneous_update() is executed\n");
2698 /* Copy data from user */
2699 err = copy_from_user(&channels, arg,
2700 sizeof(struct me4000_ao_channel_list));
2703 "ME4000:me4000_ao_simultaneous_update():Can't copy command\n");
2708 kzalloc(sizeof(unsigned long) * channels.count, GFP_KERNEL);
2709 if (!channels.list) {
2711 "ME4000:me4000_ao_simultaneous_update():Can't get buffer\n");
2715 /* Copy channel list from user */
2717 copy_from_user(channels.list, arg->list,
2718 sizeof(unsigned long) * channels.count);
2721 "ME4000:me4000_ao_simultaneous_update():Can't copy list\n");
2722 kfree(channels.list);
2726 spin_lock(&ao_context->board_info->preload_lock);
2727 tmp = me4000_inl(ao_context->preload_reg);
2728 for (i = 0; i < channels.count; i++) {
2729 if (channels.list[i] >
2730 ao_context->board_info->board_p->ao.count) {
2731 spin_unlock(&ao_context->board_info->preload_lock);
2732 kfree(channels.list);
2734 "ME4000:me4000_ao_simultaneous_update():Invalid board number specified\n");
2737 tmp &= ~(0x1 << channels.list[i]); // Clear the preload bit
2738 tmp &= ~(0x1 << (channels.list[i] + 16)); // Clear the hw simultaneous bit
2740 me4000_outl(tmp, ao_context->preload_reg);
2741 spin_unlock(&ao_context->board_info->preload_lock);
2742 kfree(channels.list);
2747 static int me4000_ao_synchronous_ex_trig(struct me4000_ao_context *ao_context)
2750 unsigned long flags;
2752 CALL_PDEBUG("me4000_ao_synchronous_ex_trig() is executed\n");
2754 /* Check if the state machine is stopped */
2755 tmp = me4000_inl(ao_context->status_reg);
2756 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2758 "me4000_ao_synchronous_ex_trig(): DAC is running\n");
2762 spin_lock(&ao_context->board_info->preload_lock);
2763 tmp = me4000_inl(ao_context->preload_reg);
2764 tmp &= ~(0x1 << ao_context->index); // Disable synchronous sw bit
2765 tmp |= 0x1 << (ao_context->index + 16); // Enable synchronous hw bit
2766 me4000_outl(tmp, ao_context->preload_reg);
2767 spin_unlock(&ao_context->board_info->preload_lock);
2770 spin_lock_irqsave(&ao_context->int_lock, flags);
2771 tmp = me4000_inl(ao_context->ctrl_reg);
2772 if (tmp & (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1)) {
2774 ~(ME4000_AO_CTRL_BIT_STOP |
2775 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2776 me4000_outl(tmp, ao_context->ctrl_reg);
2778 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2783 static int me4000_ao_synchronous_sw(struct me4000_ao_context *ao_context)
2786 unsigned long flags;
2788 CALL_PDEBUG("me4000_ao_synchronous_sw() is executed\n");
2790 /* Check if the state machine is stopped */
2791 tmp = me4000_inl(ao_context->status_reg);
2792 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2793 printk(KERN_ERR "me4000_ao_synchronous_sw(): DAC is running\n");
2797 spin_lock(&ao_context->board_info->preload_lock);
2798 tmp = me4000_inl(ao_context->preload_reg);
2799 tmp |= 0x1 << ao_context->index; // Enable synchronous sw bit
2800 tmp &= ~(0x1 << (ao_context->index + 16)); // Disable synchronous hw bit
2801 me4000_outl(tmp, ao_context->preload_reg);
2802 spin_unlock(&ao_context->board_info->preload_lock);
2805 spin_lock_irqsave(&ao_context->int_lock, flags);
2806 tmp = me4000_inl(ao_context->ctrl_reg);
2807 if (tmp & (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1)) {
2809 ~(ME4000_AO_CTRL_BIT_STOP |
2810 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2811 me4000_outl(tmp, ao_context->ctrl_reg);
2813 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2818 static int me4000_ao_synchronous_disable(struct me4000_ao_context *ao_context)
2820 return me4000_ao_simultaneous_disable(ao_context);
2823 static int me4000_ao_get_free_buffer(unsigned long *arg,
2824 struct me4000_ao_context *ao_context)
2829 c = me4000_buf_space(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT);
2831 err = copy_to_user(arg, &c, sizeof(unsigned long));
2834 "%s:Can't copy to user space\n", __func__);
2841 static int me4000_ao_ex_trig_timeout(unsigned long *arg,
2842 struct me4000_ao_context *ao_context)
2845 wait_queue_head_t queue;
2847 unsigned long timeout;
2849 CALL_PDEBUG("me4000_ao_ex_trig_timeout() is executed\n");
2851 if (get_user(timeout, arg)) {
2853 "me4000_ao_ex_trig_timeout():Cannot copy data from user\n");
2857 init_waitqueue_head(&queue);
2859 tmp = inl(ao_context->ctrl_reg);
2861 if ((tmp & ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG)) {
2864 while ((inl(ao_context->status_reg) &
2865 ME4000_AO_STATUS_BIT_FSM)) {
2866 interruptible_sleep_on_timeout(&queue, 1);
2867 if (signal_pending(current)) {
2869 "ME4000:me4000_ao_ex_trig_timeout():Wait on start of state machine interrupted\n");
2872 /* kernel 2.6 has different definitions for HZ
2873 * in user and kernel space */
2874 if ((jiffies - ref) > (timeout * HZ / USER_HZ)) {
2876 "ME4000:me4000_ao_ex_trig_timeout():Timeout reached\n");
2881 while ((inl(ao_context->status_reg) &
2882 ME4000_AO_STATUS_BIT_FSM)) {
2883 interruptible_sleep_on_timeout(&queue, 1);
2884 if (signal_pending(current)) {
2886 "ME4000:me4000_ao_ex_trig_timeout():Wait on start of state machine interrupted\n");
2893 "ME4000:me4000_ao_ex_trig_timeout():External Trigger is not enabled\n");
2900 static int me4000_ao_enable_do(struct me4000_ao_context *ao_context)
2903 unsigned long flags;
2905 CALL_PDEBUG("me4000_ao_enable_do() is executed\n");
2907 /* Only available for analog output 3 */
2908 if (ao_context->index != 3) {
2910 "me4000_ao_enable_do():Only available for analog output 3\n");
2914 /* Check if the state machine is stopped */
2915 tmp = me4000_inl(ao_context->status_reg);
2916 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2917 printk(KERN_ERR "me4000_ao_enable_do(): DAC is running\n");
2921 /* Set the stop bit */
2922 spin_lock_irqsave(&ao_context->int_lock, flags);
2923 tmp = inl(ao_context->ctrl_reg);
2924 tmp |= ME4000_AO_CTRL_BIT_ENABLE_DO;
2925 me4000_outl(tmp, ao_context->ctrl_reg);
2926 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2931 static int me4000_ao_disable_do(struct me4000_ao_context *ao_context)
2934 unsigned long flags;
2936 CALL_PDEBUG("me4000_ao_disable_do() is executed\n");
2938 /* Only available for analog output 3 */
2939 if (ao_context->index != 3) {
2941 "me4000_ao_disable():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_disable_do(): DAC is running\n");
2952 spin_lock_irqsave(&ao_context->int_lock, flags);
2953 tmp = inl(ao_context->ctrl_reg);
2954 tmp &= ~(ME4000_AO_CTRL_BIT_ENABLE_DO);
2955 me4000_outl(tmp, ao_context->ctrl_reg);
2956 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2961 static int me4000_ao_fsm_state(int *arg, struct me4000_ao_context *ao_context)
2965 CALL_PDEBUG("me4000_ao_fsm_state() is executed\n");
2968 (me4000_inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) ? 1
2971 if (ao_context->pipe_flag) {
2972 printk(KERN_ERR "me4000_ao_fsm_state():Broken pipe detected\n");
2976 if (put_user(tmp, arg)) {
2977 printk(KERN_ERR "me4000_ao_fsm_state():Cannot copy to user\n");
2984 /*------------------------- Analog input stuff -------------------------------*/
2986 static int me4000_ai_prepare(struct me4000_ai_context *ai_context)
2988 wait_queue_head_t queue;
2991 CALL_PDEBUG("me4000_ai_prepare() is executed\n");
2993 init_waitqueue_head(&queue);
2995 /* Set the new mode and stop bits */
2996 me4000_outl(ai_context->
2997 mode | ME4000_AI_CTRL_BIT_STOP |
2998 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP, ai_context->ctrl_reg);
3000 /* Set the timer registers */
3001 ai_context->chan_timer = 66;
3002 ai_context->chan_pre_timer = 66;
3003 ai_context->scan_timer_low = 0;
3004 ai_context->scan_timer_high = 0;
3006 me4000_outl(65, ai_context->chan_timer_reg);
3007 me4000_outl(65, ai_context->chan_pre_timer_reg);
3008 me4000_outl(0, ai_context->scan_timer_low_reg);
3009 me4000_outl(0, ai_context->scan_timer_high_reg);
3010 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3011 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3013 ai_context->channel_list_count = 0;
3015 if (ai_context->mode) {
3016 /* Request the interrupt line */
3018 request_irq(ai_context->irq, me4000_ai_isr,
3019 IRQF_DISABLED | IRQF_SHARED, ME4000_NAME,
3023 "ME4000:me4000_ai_prepare():Can't get interrupt line");
3027 /* Allocate circular buffer */
3028 ai_context->circ_buf.buf =
3029 kzalloc(ME4000_AI_BUFFER_SIZE, GFP_KERNEL);
3030 if (!ai_context->circ_buf.buf) {
3032 "ME4000:me4000_ai_prepare():Can't get circular buffer\n");
3033 free_irq(ai_context->irq, ai_context);
3037 /* Clear the circular buffer */
3038 ai_context->circ_buf.head = 0;
3039 ai_context->circ_buf.tail = 0;
3045 static int me4000_ai_reset(struct me4000_ai_context *ai_context)
3047 wait_queue_head_t queue;
3049 unsigned long flags;
3051 CALL_PDEBUG("me4000_ai_reset() is executed\n");
3053 init_waitqueue_head(&queue);
3056 * First stop conversion of the state machine before reconfigure.
3057 * If not stopped before configuring mode, it could
3058 * walk in a undefined state.
3060 spin_lock_irqsave(&ai_context->int_lock, flags);
3061 tmp = me4000_inl(ai_context->ctrl_reg);
3062 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
3063 me4000_outl(tmp, ai_context->ctrl_reg);
3064 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3066 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3067 interruptible_sleep_on_timeout(&queue, 1);
3068 if (signal_pending(current)) {
3070 "me4000_ai_reset():Wait on state machine after stop interrupted\n");
3075 /* Clear the control register and set the stop bits */
3076 spin_lock_irqsave(&ai_context->int_lock, flags);
3077 tmp = me4000_inl(ai_context->ctrl_reg);
3078 me4000_outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP | ME4000_AI_CTRL_BIT_STOP,
3079 ai_context->ctrl_reg);
3080 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3082 /* Reset timer registers */
3083 ai_context->chan_timer = 66;
3084 ai_context->chan_pre_timer = 66;
3085 ai_context->scan_timer_low = 0;
3086 ai_context->scan_timer_high = 0;
3087 ai_context->sample_counter = 0;
3088 ai_context->sample_counter_reload = 0;
3090 me4000_outl(65, ai_context->chan_timer_reg);
3091 me4000_outl(65, ai_context->chan_pre_timer_reg);
3092 me4000_outl(0, ai_context->scan_timer_low_reg);
3093 me4000_outl(0, ai_context->scan_timer_high_reg);
3094 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3095 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3096 me4000_outl(0, ai_context->sample_counter_reg);
3098 ai_context->channel_list_count = 0;
3100 /* Clear the circular buffer */
3101 ai_context->circ_buf.head = 0;
3102 ai_context->circ_buf.tail = 0;
3107 static int me4000_ai_ioctl_sing(struct inode *inode_p, struct file *file_p,
3108 unsigned int service, unsigned long arg)
3110 struct me4000_ai_context *ai_context;
3112 CALL_PDEBUG("me4000_ai_ioctl_sing() is executed\n");
3114 ai_context = file_p->private_data;
3116 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3117 printk(KERN_ERR "me4000_ai_ioctl_sing():Wrong magic number\n");
3120 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3122 "me4000_ai_ioctl_sing():Service number to high\n");
3127 case ME4000_AI_SINGLE:
3128 return me4000_ai_single((struct me4000_ai_single *)arg,
3130 case ME4000_AI_EX_TRIG_ENABLE:
3131 return me4000_ai_ex_trig_enable(ai_context);
3132 case ME4000_AI_EX_TRIG_DISABLE:
3133 return me4000_ai_ex_trig_disable(ai_context);
3134 case ME4000_AI_EX_TRIG_SETUP:
3135 return me4000_ai_ex_trig_setup((struct me4000_ai_trigger *)arg,
3137 case ME4000_GET_USER_INFO:
3138 return me4000_get_user_info((struct me4000_user_info *)arg,
3139 ai_context->board_info);
3140 case ME4000_AI_OFFSET_ENABLE:
3141 return me4000_ai_offset_enable(ai_context);
3142 case ME4000_AI_OFFSET_DISABLE:
3143 return me4000_ai_offset_disable(ai_context);
3144 case ME4000_AI_FULLSCALE_ENABLE:
3145 return me4000_ai_fullscale_enable(ai_context);
3146 case ME4000_AI_FULLSCALE_DISABLE:
3147 return me4000_ai_fullscale_disable(ai_context);
3148 case ME4000_AI_EEPROM_READ:
3149 return me4000_eeprom_read((struct me4000_eeprom *)arg,
3151 case ME4000_AI_EEPROM_WRITE:
3152 return me4000_eeprom_write((struct me4000_eeprom *)arg,
3156 "me4000_ai_ioctl_sing():Invalid service number\n");
3162 static int me4000_ai_single(struct me4000_ai_single *arg,
3163 struct me4000_ai_context *ai_context)
3165 struct me4000_ai_single cmd;
3168 wait_queue_head_t queue;
3169 unsigned long jiffy;
3171 CALL_PDEBUG("me4000_ai_single() is executed\n");
3173 init_waitqueue_head(&queue);
3175 /* Copy data from user */
3176 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_single));
3179 "ME4000:me4000_ai_single():Can't copy from user space\n");
3183 /* Check range parameter */
3184 switch (cmd.range) {
3185 case ME4000_AI_LIST_RANGE_BIPOLAR_10:
3186 case ME4000_AI_LIST_RANGE_BIPOLAR_2_5:
3187 case ME4000_AI_LIST_RANGE_UNIPOLAR_10:
3188 case ME4000_AI_LIST_RANGE_UNIPOLAR_2_5:
3192 "ME4000:me4000_ai_single():Invalid range specified\n");
3196 /* Check mode and channel number */
3198 case ME4000_AI_LIST_INPUT_SINGLE_ENDED:
3199 if (cmd.channel >= ai_context->board_info->board_p->ai.count) {
3201 "ME4000:me4000_ai_single():Analog input is not available\n");
3205 case ME4000_AI_LIST_INPUT_DIFFERENTIAL:
3207 ai_context->board_info->board_p->ai.diff_count) {
3209 "ME4000:me4000_ai_single():Analog input is not available in differential mode\n");
3215 "ME4000:me4000_ai_single():Invalid mode specified\n");
3219 /* Clear channel list, data fifo and both stop bits */
3220 tmp = me4000_inl(ai_context->ctrl_reg);
3222 ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_DATA_FIFO |
3223 ME4000_AI_CTRL_BIT_STOP | ME4000_AI_CTRL_BIT_IMMEDIATE_STOP);
3224 me4000_outl(tmp, ai_context->ctrl_reg);
3226 /* Enable channel list and data fifo */
3227 tmp |= ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_DATA_FIFO;
3228 me4000_outl(tmp, ai_context->ctrl_reg);
3230 /* Generate channel list entry */
3231 me4000_outl(cmd.channel | cmd.range | cmd.
3232 mode | ME4000_AI_LIST_LAST_ENTRY,
3233 ai_context->channel_list_reg);
3235 /* Set the timer to maximum */
3236 me4000_outl(66, ai_context->chan_timer_reg);
3237 me4000_outl(66, ai_context->chan_pre_timer_reg);
3239 if (tmp & ME4000_AI_CTRL_BIT_EX_TRIG) {
3242 (me4000_inl(ai_context->status_reg) &
3243 ME4000_AI_STATUS_BIT_EF_DATA)) {
3244 interruptible_sleep_on_timeout(&queue, 1);
3245 if (signal_pending(current)) {
3247 "ME4000:me4000_ai_single():Wait on start of state machine interrupted\n");
3250 /* 2.6 has different definitions for HZ in user and kernel space */
3251 if (((jiffies - jiffy) > (cmd.timeout * HZ / USER_HZ)) && cmd.timeout) {
3253 "ME4000:me4000_ai_single():Timeout reached\n");
3258 /* Start conversion */
3259 me4000_inl(ai_context->start_reg);
3261 /* Wait until ready */
3264 (me4000_inl(ai_context->status_reg) &
3265 ME4000_AI_STATUS_BIT_EF_DATA)) {
3267 "ME4000:me4000_ai_single():Value not available after wait\n");
3272 /* Read value from data fifo */
3273 cmd.value = me4000_inl(ai_context->data_reg) & 0xFFFF;
3275 /* Copy result back to user */
3276 err = copy_to_user(arg, &cmd, sizeof(struct me4000_ai_single));
3279 "ME4000:me4000_ai_single():Can't copy to user space\n");
3286 static int me4000_ai_ioctl_sw(struct inode *inode_p, struct file *file_p,
3287 unsigned int service, unsigned long arg)
3289 struct me4000_ai_context *ai_context;
3291 CALL_PDEBUG("me4000_ai_ioctl_sw() is executed\n");
3293 ai_context = file_p->private_data;
3295 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3296 printk(KERN_ERR "me4000_ai_ioctl_sw():Wrong magic number\n");
3299 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3301 "me4000_ai_ioctl_sw():Service number to high\n");
3306 case ME4000_AI_SC_SETUP:
3307 return me4000_ai_sc_setup((struct me4000_ai_sc *)arg,
3309 case ME4000_AI_CONFIG:
3310 return me4000_ai_config((struct me4000_ai_config *)arg,
3312 case ME4000_AI_START:
3313 return me4000_ai_start(ai_context);
3314 case ME4000_AI_STOP:
3315 return me4000_ai_stop(ai_context);
3316 case ME4000_AI_IMMEDIATE_STOP:
3317 return me4000_ai_immediate_stop(ai_context);
3318 case ME4000_AI_FSM_STATE:
3319 return me4000_ai_fsm_state((int *)arg, ai_context);
3320 case ME4000_GET_USER_INFO:
3321 return me4000_get_user_info((struct me4000_user_info *)arg,
3322 ai_context->board_info);
3323 case ME4000_AI_EEPROM_READ:
3324 return me4000_eeprom_read((struct me4000_eeprom *)arg,
3326 case ME4000_AI_EEPROM_WRITE:
3327 return me4000_eeprom_write((struct me4000_eeprom *)arg,
3329 case ME4000_AI_GET_COUNT_BUFFER:
3330 return me4000_ai_get_count_buffer((unsigned long *)arg,
3334 "%s:Invalid service number %d\n", __func__, service);
3340 static int me4000_ai_ioctl_ext(struct inode *inode_p, struct file *file_p,
3341 unsigned int service, unsigned long arg)
3343 struct me4000_ai_context *ai_context;
3345 CALL_PDEBUG("me4000_ai_ioctl_ext() is executed\n");
3347 ai_context = file_p->private_data;
3349 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3350 printk(KERN_ERR "me4000_ai_ioctl_ext():Wrong magic number\n");
3353 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3355 "me4000_ai_ioctl_ext():Service number to high\n");
3360 case ME4000_AI_SC_SETUP:
3361 return me4000_ai_sc_setup((struct me4000_ai_sc *)arg,
3363 case ME4000_AI_CONFIG:
3364 return me4000_ai_config((struct me4000_ai_config *)arg,
3366 case ME4000_AI_START:
3367 return me4000_ai_start_ex((unsigned long *)arg, ai_context);
3368 case ME4000_AI_STOP:
3369 return me4000_ai_stop(ai_context);
3370 case ME4000_AI_IMMEDIATE_STOP:
3371 return me4000_ai_immediate_stop(ai_context);
3372 case ME4000_AI_EX_TRIG_ENABLE:
3373 return me4000_ai_ex_trig_enable(ai_context);
3374 case ME4000_AI_EX_TRIG_DISABLE:
3375 return me4000_ai_ex_trig_disable(ai_context);
3376 case ME4000_AI_EX_TRIG_SETUP:
3377 return me4000_ai_ex_trig_setup((struct me4000_ai_trigger *)arg,
3379 case ME4000_AI_FSM_STATE:
3380 return me4000_ai_fsm_state((int *)arg, ai_context);
3381 case ME4000_GET_USER_INFO:
3382 return me4000_get_user_info((struct me4000_user_info *)arg,
3383 ai_context->board_info);
3384 case ME4000_AI_GET_COUNT_BUFFER:
3385 return me4000_ai_get_count_buffer((unsigned long *)arg,
3389 "%s:Invalid service number %d\n", __func__ , service);
3395 static int me4000_ai_fasync(int fd, struct file *file_p, int mode)
3397 struct me4000_ai_context *ai_context;
3399 CALL_PDEBUG("me4000_ao_fasync_cont() is executed\n");
3401 ai_context = file_p->private_data;
3402 return fasync_helper(fd, file_p, mode, &ai_context->fasync_p);
3405 static int me4000_ai_config(struct me4000_ai_config *arg,
3406 struct me4000_ai_context *ai_context)
3408 struct me4000_ai_config cmd;
3413 wait_queue_head_t queue;
3417 CALL_PDEBUG("me4000_ai_config() is executed\n");
3419 init_waitqueue_head(&queue);
3421 /* Check if conversion is stopped */
3422 if (inl(ai_context->ctrl_reg) & ME4000_AI_STATUS_BIT_FSM) {
3424 "ME4000:me4000_ai_config():Conversion is not stopped\n");
3429 /* Copy data from user */
3430 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_config));
3433 "ME4000:me4000_ai_config():Can't copy from user space\n");
3439 ("me4000_ai_config():chan = %ld, pre_chan = %ld, scan_low = %ld, scan_high = %ld, count = %ld\n",
3440 cmd.timer.chan, cmd.timer.pre_chan, cmd.timer.scan_low,
3441 cmd.timer.scan_high, cmd.channel_list.count);
3443 /* Check whether sample and hold is available for this board */
3445 if (!ai_context->board_info->board_p->ai.sh_count) {
3447 "ME4000:me4000_ai_config():Sample and Hold is not available for this board\n");
3453 /* Check the channel list size */
3454 if (cmd.channel_list.count > ME4000_AI_CHANNEL_LIST_COUNT) {
3456 "me4000_ai_config():Channel list is to large\n");
3461 /* Copy channel list from user */
3462 list = kmalloc(sizeof(u32) * cmd.channel_list.count, GFP_KERNEL);
3465 "ME4000:me4000_ai_config():Can't get memory for channel list\n");
3470 copy_from_user(list, cmd.channel_list.list,
3471 sizeof(u32) * cmd.channel_list.count);
3474 "ME4000:me4000_ai_config():Can't copy from user space\n");
3479 /* Check if last entry bit is set */
3480 if (!(list[cmd.channel_list.count - 1] & ME4000_AI_LIST_LAST_ENTRY)) {
3482 "me4000_ai_config():Last entry bit is not set\n");
3483 list[cmd.channel_list.count - 1] |= ME4000_AI_LIST_LAST_ENTRY;
3486 /* Check whether mode is equal for all entries */
3487 mode = list[0] & 0x20;
3488 for (i = 0; i < cmd.channel_list.count; i++) {
3489 if ((list[i] & 0x20) != mode) {
3491 "ME4000:me4000_ai_config():Mode is not equal for all entries\n");
3497 /* Check whether channels are available for this mode */
3498 if (mode == ME4000_AI_LIST_INPUT_SINGLE_ENDED) {
3499 for (i = 0; i < cmd.channel_list.count; i++) {
3500 if ((list[i] & 0x1F) >=
3501 ai_context->board_info->board_p->ai.count) {
3503 "ME4000:me4000_ai_config():Channel is not available for single ended\n");
3508 } else if (mode == ME4000_AI_LIST_INPUT_DIFFERENTIAL) {
3509 for (i = 0; i < cmd.channel_list.count; i++) {
3510 if ((list[i] & 0x1F) >=
3511 ai_context->board_info->board_p->ai.diff_count) {
3513 "ME4000:me4000_ai_config():Channel is not available for differential\n");
3520 /* Check if bipolar is set for all entries when in differential mode */
3521 if (mode == ME4000_AI_LIST_INPUT_DIFFERENTIAL) {
3522 for (i = 0; i < cmd.channel_list.count; i++) {
3523 if ((list[i] & 0xC0) != ME4000_AI_LIST_RANGE_BIPOLAR_10
3524 && (list[i] & 0xC0) !=
3525 ME4000_AI_LIST_RANGE_BIPOLAR_2_5) {
3527 "ME4000:me4000_ai_config():Bipolar is not selected in differential mode\n");
3534 if (ai_context->mode != ME4000_AI_ACQ_MODE_EXT_SINGLE_VALUE) {
3535 /* Check for minimum channel divisor */
3536 if (cmd.timer.chan < ME4000_AI_MIN_TICKS) {
3538 "ME4000:me4000_ai_config():Channel timer divisor is to low\n");
3543 /* Check if minimum channel divisor is adjusted when sample and hold is activated */
3544 if ((cmd.sh) && (cmd.timer.chan != ME4000_AI_MIN_TICKS)) {
3546 "ME4000:me4000_ai_config():Channel timer divisor must be at minimum when sample and hold is activated\n");
3551 /* Check for minimum channel pre divisor */
3552 if (cmd.timer.pre_chan < ME4000_AI_MIN_TICKS) {
3554 "ME4000:me4000_ai_config():Channel pre timer divisor is to low\n");
3559 /* Write the channel timers */
3560 me4000_outl(cmd.timer.chan - 1, ai_context->chan_timer_reg);
3561 me4000_outl(cmd.timer.pre_chan - 1,
3562 ai_context->chan_pre_timer_reg);
3564 /* Save the timer values in the board context */
3565 ai_context->chan_timer = cmd.timer.chan;
3566 ai_context->chan_pre_timer = cmd.timer.pre_chan;
3568 if (ai_context->mode != ME4000_AI_ACQ_MODE_EXT_SINGLE_CHANLIST) {
3569 /* Check for scan timer divisor */
3571 (u64) cmd.timer.scan_low | ((u64) cmd.timer.
3575 cmd.channel_list.count * cmd.timer.chan +
3578 "ME4000:me4000_ai_config():Scan timer divisor is to low\n");
3584 /* Write the scan timers */
3587 tmp = (u32) (scan & 0xFFFFFFFF);
3589 ai_context->scan_timer_low_reg);
3590 tmp = (u32) ((scan >> 32) & 0xFFFFFFFF);
3592 ai_context->scan_timer_high_reg);
3595 scan - (cmd.timer.chan - 1) +
3596 (cmd.timer.pre_chan - 1);
3597 tmp = (u32) (scan & 0xFFFFFFFF);
3599 ai_context->scan_pre_timer_low_reg);
3600 tmp = (u32) ((scan >> 32) & 0xFFFFFFFF);
3603 scan_pre_timer_high_reg);
3606 ai_context->scan_timer_low_reg);
3608 ai_context->scan_timer_high_reg);
3611 ai_context->scan_pre_timer_low_reg);
3614 scan_pre_timer_high_reg);
3617 ai_context->scan_timer_low = cmd.timer.scan_low;
3618 ai_context->scan_timer_high = cmd.timer.scan_high;
3622 /* Clear the channel list */
3623 tmp = me4000_inl(ai_context->ctrl_reg);
3624 tmp &= ~ME4000_AI_CTRL_BIT_CHANNEL_FIFO;
3625 me4000_outl(tmp, ai_context->ctrl_reg);
3626 tmp |= ME4000_AI_CTRL_BIT_CHANNEL_FIFO;
3627 me4000_outl(tmp, ai_context->ctrl_reg);
3629 /* Write the channel list */
3630 for (i = 0; i < cmd.channel_list.count; i++) {
3631 me4000_outl(list[i], ai_context->channel_list_reg);
3634 /* Setup sample and hold */
3636 tmp |= ME4000_AI_CTRL_BIT_SAMPLE_HOLD;
3637 me4000_outl(tmp, ai_context->ctrl_reg);
3639 tmp &= ~ME4000_AI_CTRL_BIT_SAMPLE_HOLD;
3640 me4000_outl(tmp, ai_context->ctrl_reg);
3643 /* Save the channel list size in the board context */
3644 ai_context->channel_list_count = cmd.channel_list.count;
3652 /* Reset the timers */
3653 ai_context->chan_timer = 66;
3654 ai_context->chan_pre_timer = 66;
3655 ai_context->scan_timer_low = 0;
3656 ai_context->scan_timer_high = 0;
3658 me4000_outl(65, ai_context->chan_timer_reg);
3659 me4000_outl(65, ai_context->chan_pre_timer_reg);
3660 me4000_outl(0, ai_context->scan_timer_high_reg);
3661 me4000_outl(0, ai_context->scan_timer_low_reg);
3662 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3663 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3665 ai_context->channel_list_count = 0;
3667 tmp = me4000_inl(ai_context->ctrl_reg);
3669 ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_SAMPLE_HOLD);
3678 static int ai_common_start(struct me4000_ai_context *ai_context)
3681 CALL_PDEBUG("ai_common_start() is executed\n");
3683 tmp = me4000_inl(ai_context->ctrl_reg);
3685 /* Check if conversion is stopped */
3686 if (tmp & ME4000_AI_STATUS_BIT_FSM) {
3688 "ME4000:ai_common_start():Conversion is not stopped\n");
3692 /* Clear data fifo, disable all interrupts, clear sample counter reload */
3693 tmp &= ~(ME4000_AI_CTRL_BIT_DATA_FIFO | ME4000_AI_CTRL_BIT_LE_IRQ |
3694 ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3695 ME4000_AI_CTRL_BIT_SC_RELOAD);
3697 me4000_outl(tmp, ai_context->ctrl_reg);
3699 /* Clear circular buffer */
3700 ai_context->circ_buf.head = 0;
3701 ai_context->circ_buf.tail = 0;
3703 /* Enable data fifo */
3704 tmp |= ME4000_AI_CTRL_BIT_DATA_FIFO;
3706 /* Determine interrupt setup */
3707 if (ai_context->sample_counter && !ai_context->sample_counter_reload) {
3708 /* Enable Half Full Interrupt and Sample Counter Interrupt */
3709 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ | ME4000_AI_CTRL_BIT_HF_IRQ;
3710 } else if (ai_context->sample_counter
3711 && ai_context->sample_counter_reload) {
3712 if (ai_context->sample_counter <= ME4000_AI_FIFO_COUNT / 2) {
3713 /* Enable only Sample Counter Interrupt */
3715 ME4000_AI_CTRL_BIT_SC_IRQ |
3716 ME4000_AI_CTRL_BIT_SC_RELOAD;
3718 /* Enable Half Full Interrupt and Sample Counter Interrupt */
3720 ME4000_AI_CTRL_BIT_SC_IRQ |
3721 ME4000_AI_CTRL_BIT_HF_IRQ |
3722 ME4000_AI_CTRL_BIT_SC_RELOAD;
3725 /* Enable only Half Full Interrupt */
3726 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ;
3729 /* Clear the stop bits */
3730 tmp &= ~(ME4000_AI_CTRL_BIT_STOP | ME4000_AI_CTRL_BIT_IMMEDIATE_STOP);
3732 /* Write setup to hardware */
3733 me4000_outl(tmp, ai_context->ctrl_reg);
3735 /* Write sample counter */
3736 me4000_outl(ai_context->sample_counter, ai_context->sample_counter_reg);
3741 static int me4000_ai_start(struct me4000_ai_context *ai_context)
3744 CALL_PDEBUG("me4000_ai_start() is executed\n");
3746 /* Prepare Hardware */
3747 err = ai_common_start(ai_context);
3751 /* Start conversion by dummy read */
3752 me4000_inl(ai_context->start_reg);
3757 static int me4000_ai_start_ex(unsigned long *arg,
3758 struct me4000_ai_context *ai_context)
3761 wait_queue_head_t queue;
3763 unsigned long timeout;
3765 CALL_PDEBUG("me4000_ai_start_ex() is executed\n");
3767 if (get_user(timeout, arg)) {
3769 "me4000_ai_start_ex():Cannot copy data from user\n");
3773 init_waitqueue_head(&queue);
3775 /* Prepare Hardware */
3776 err = ai_common_start(ai_context);
3783 (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM))
3785 interruptible_sleep_on_timeout(&queue, 1);
3786 if (signal_pending(current)) {
3788 "ME4000:me4000_ai_start_ex():Wait on start of state machine interrupted\n");
3791 /* 2.6 has different definitions for HZ in user and kernel space */
3792 if ((jiffies - ref) > (timeout * HZ / USER_HZ)) {
3794 "ME4000:me4000_ai_start_ex():Timeout reached\n");
3800 (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM))
3802 interruptible_sleep_on_timeout(&queue, 1);
3803 if (signal_pending(current)) {
3805 "ME4000:me4000_ai_start_ex():Wait on start of state machine interrupted\n");
3814 static int me4000_ai_stop(struct me4000_ai_context *ai_context)
3816 wait_queue_head_t queue;
3818 unsigned long flags;
3820 CALL_PDEBUG("me4000_ai_stop() is executed\n");
3822 init_waitqueue_head(&queue);
3824 /* Disable irqs and clear data fifo */
3825 spin_lock_irqsave(&ai_context->int_lock, flags);
3826 tmp = me4000_inl(ai_context->ctrl_reg);
3828 ~(ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3829 ME4000_AI_CTRL_BIT_DATA_FIFO);
3830 /* Stop conversion of the state machine */
3831 tmp |= ME4000_AI_CTRL_BIT_STOP;
3832 me4000_outl(tmp, ai_context->ctrl_reg);
3833 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3835 /* Clear circular buffer */
3836 ai_context->circ_buf.head = 0;
3837 ai_context->circ_buf.tail = 0;
3839 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3840 interruptible_sleep_on_timeout(&queue, 1);
3841 if (signal_pending(current)) {
3843 "ME4000:me4000_ai_stop():Wait on state machine after stop interrupted\n");
3851 static int me4000_ai_immediate_stop(struct me4000_ai_context *ai_context)
3853 wait_queue_head_t queue;
3855 unsigned long flags;
3857 CALL_PDEBUG("me4000_ai_stop() is executed\n");
3859 init_waitqueue_head(&queue);
3861 /* Disable irqs and clear data fifo */
3862 spin_lock_irqsave(&ai_context->int_lock, flags);
3863 tmp = me4000_inl(ai_context->ctrl_reg);
3865 ~(ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3866 ME4000_AI_CTRL_BIT_DATA_FIFO);
3867 /* Stop conversion of the state machine */
3868 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
3869 me4000_outl(tmp, ai_context->ctrl_reg);
3870 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3872 /* Clear circular buffer */
3873 ai_context->circ_buf.head = 0;
3874 ai_context->circ_buf.tail = 0;
3876 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3877 interruptible_sleep_on_timeout(&queue, 1);
3878 if (signal_pending(current)) {
3880 "ME4000:me4000_ai_stop():Wait on state machine after stop interrupted\n");
3888 static int me4000_ai_ex_trig_enable(struct me4000_ai_context *ai_context)
3891 unsigned long flags;
3893 CALL_PDEBUG("me4000_ai_ex_trig_enable() is executed\n");
3895 spin_lock_irqsave(&ai_context->int_lock, flags);
3896 tmp = me4000_inl(ai_context->ctrl_reg);
3897 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG;
3898 me4000_outl(tmp, ai_context->ctrl_reg);
3899 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3904 static int me4000_ai_ex_trig_disable(struct me4000_ai_context *ai_context)
3907 unsigned long flags;
3909 CALL_PDEBUG("me4000_ai_ex_trig_disable() is executed\n");
3911 spin_lock_irqsave(&ai_context->int_lock, flags);
3912 tmp = me4000_inl(ai_context->ctrl_reg);
3913 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG;
3914 me4000_outl(tmp, ai_context->ctrl_reg);
3915 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3920 static int me4000_ai_ex_trig_setup(struct me4000_ai_trigger *arg,
3921 struct me4000_ai_context *ai_context)
3923 struct me4000_ai_trigger cmd;
3926 unsigned long flags;
3928 CALL_PDEBUG("me4000_ai_ex_trig_setup() is executed\n");
3930 /* Copy data from user */
3931 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_trigger));
3934 "ME4000:me4000_ai_ex_trig_setup():Can't copy from user space\n");
3938 spin_lock_irqsave(&ai_context->int_lock, flags);
3939 tmp = me4000_inl(ai_context->ctrl_reg);
3941 if (cmd.mode == ME4000_AI_TRIGGER_EXT_DIGITAL) {
3942 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG;
3943 } else if (cmd.mode == ME4000_AI_TRIGGER_EXT_ANALOG) {
3944 if (!ai_context->board_info->board_p->ai.ex_trig_analog) {
3946 "ME4000:me4000_ai_ex_trig_setup():No analog trigger available\n");
3949 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG;
3951 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3953 "ME4000:me4000_ai_ex_trig_setup():Invalid trigger mode specified\n");
3957 if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_RISING) {
3959 ~(ME4000_AI_CTRL_BIT_EX_TRIG_BOTH |
3960 ME4000_AI_CTRL_BIT_EX_TRIG_FALLING);
3961 } else if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_FALLING) {
3962 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG_FALLING;
3963 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG_BOTH;
3964 } else if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_BOTH) {
3966 ME4000_AI_CTRL_BIT_EX_TRIG_BOTH |
3967 ME4000_AI_CTRL_BIT_EX_TRIG_FALLING;
3969 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3971 "ME4000:me4000_ai_ex_trig_setup():Invalid trigger edge specified\n");
3975 me4000_outl(tmp, ai_context->ctrl_reg);
3976 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3980 static int me4000_ai_sc_setup(struct me4000_ai_sc *arg,
3981 struct me4000_ai_context *ai_context)
3983 struct me4000_ai_sc cmd;
3986 CALL_PDEBUG("me4000_ai_sc_setup() is executed\n");
3988 /* Copy data from user */
3989 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_sc));
3992 "ME4000:me4000_ai_sc_setup():Can't copy from user space\n");
3996 ai_context->sample_counter = cmd.value;
3997 ai_context->sample_counter_reload = cmd.reload;
4002 static ssize_t me4000_ai_read(struct file *filep, char *buff, size_t cnt,
4005 struct me4000_ai_context *ai_context = filep->private_data;
4006 s16 *buffer = (s16 *) buff;
4007 size_t count = cnt / 2;
4008 unsigned long flags;
4015 CALL_PDEBUG("me4000_ai_read() is executed\n");
4017 init_waitqueue_entry(&wait, current);
4021 PDEBUG("me4000_ai_read():Count is 0\n");
4026 if (filep->f_flags & O_NONBLOCK) {
4027 c = me4000_values_to_end(ai_context->circ_buf,
4028 ME4000_AI_BUFFER_COUNT);
4031 ("me4000_ai_read():Returning from nonblocking read\n");
4035 /* Check if conversion is still running */
4037 (me4000_inl(ai_context->status_reg) &
4038 ME4000_AI_STATUS_BIT_FSM)) {
4040 "ME4000:me4000_ai_read():Conversion interrupted\n");
4044 wait_event_interruptible(ai_context->wait_queue,
4045 (me4000_values_to_end
4046 (ai_context->circ_buf,
4047 ME4000_AI_BUFFER_COUNT)));
4048 if (signal_pending(current)) {
4050 "ME4000:me4000_ai_read():Wait on values interrupted from signal\n");
4055 /* Only read count values or as much as available */
4056 c = me4000_values_to_end(ai_context->circ_buf,
4057 ME4000_AI_BUFFER_COUNT);
4058 PDEBUG("me4000_ai_read():%d values to end\n", c);
4062 PDEBUG("me4000_ai_read():Copy %d values to user space\n", c);
4064 k -= copy_to_user(buffer,
4065 ai_context->circ_buf.buf +
4066 ai_context->circ_buf.tail, k);
4070 "ME4000:me4000_ai_read():Cannot copy new values to user\n");
4074 ai_context->circ_buf.tail =
4075 (ai_context->circ_buf.tail + c) & (ME4000_AI_BUFFER_COUNT -
4081 spin_lock_irqsave(&ai_context->int_lock, flags);
4082 if (me4000_buf_space
4083 (ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
4084 tmp = me4000_inl(ai_context->ctrl_reg);
4086 /* Determine interrupt setup */
4087 if (ai_context->sample_counter
4088 && !ai_context->sample_counter_reload) {
4089 /* Enable Half Full Interrupt and Sample Counter Interrupt */
4091 ME4000_AI_CTRL_BIT_SC_IRQ |
4092 ME4000_AI_CTRL_BIT_HF_IRQ;
4093 } else if (ai_context->sample_counter
4094 && ai_context->sample_counter_reload) {
4095 if (ai_context->sample_counter <
4096 ME4000_AI_FIFO_COUNT / 2) {
4097 /* Enable only Sample Counter Interrupt */
4098 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ;
4100 /* Enable Half Full Interrupt and Sample Counter Interrupt */
4102 ME4000_AI_CTRL_BIT_SC_IRQ |
4103 ME4000_AI_CTRL_BIT_HF_IRQ;
4106 /* Enable only Half Full Interrupt */
4107 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ;
4110 me4000_outl(tmp, ai_context->ctrl_reg);
4112 spin_unlock_irqrestore(&ai_context->int_lock, flags);
4115 /* Check if conversion is still running */
4116 if (!(me4000_inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM)) {
4118 "ME4000:me4000_ai_read():Conversion not running after complete read\n");
4122 if (filep->f_flags & O_NONBLOCK) {
4123 return (k == 0) ? -EAGAIN : 2 * ret;
4126 CALL_PDEBUG("me4000_ai_read() is leaved\n");
4130 static unsigned int me4000_ai_poll(struct file *file_p, poll_table *wait)
4132 struct me4000_ai_context *ai_context;
4133 unsigned long mask = 0;
4135 CALL_PDEBUG("me4000_ai_poll() is executed\n");
4137 ai_context = file_p->private_data;
4139 /* Register wait queue */
4140 poll_wait(file_p, &ai_context->wait_queue, wait);
4142 /* Get available values */
4143 if (me4000_values_to_end(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT))
4144 mask |= POLLIN | POLLRDNORM;
4146 PDEBUG("me4000_ai_poll():Return mask %lX\n", mask);
4151 static int me4000_ai_offset_enable(struct me4000_ai_context *ai_context)
4155 CALL_PDEBUG("me4000_ai_offset_enable() is executed\n");
4157 tmp = me4000_inl(ai_context->ctrl_reg);
4158 tmp |= ME4000_AI_CTRL_BIT_OFFSET;
4159 me4000_outl(tmp, ai_context->ctrl_reg);
4164 static int me4000_ai_offset_disable(struct me4000_ai_context *ai_context)
4168 CALL_PDEBUG("me4000_ai_offset_disable() is executed\n");
4170 tmp = me4000_inl(ai_context->ctrl_reg);
4171 tmp &= ~ME4000_AI_CTRL_BIT_OFFSET;
4172 me4000_outl(tmp, ai_context->ctrl_reg);
4177 static int me4000_ai_fullscale_enable(struct me4000_ai_context *ai_context)
4181 CALL_PDEBUG("me4000_ai_fullscale_enable() is executed\n");
4183 tmp = me4000_inl(ai_context->ctrl_reg);
4184 tmp |= ME4000_AI_CTRL_BIT_FULLSCALE;
4185 me4000_outl(tmp, ai_context->ctrl_reg);
4190 static int me4000_ai_fullscale_disable(struct me4000_ai_context *ai_context)
4194 CALL_PDEBUG("me4000_ai_fullscale_disable() is executed\n");
4196 tmp = me4000_inl(ai_context->ctrl_reg);
4197 tmp &= ~ME4000_AI_CTRL_BIT_FULLSCALE;
4198 me4000_outl(tmp, ai_context->ctrl_reg);
4203 static int me4000_ai_fsm_state(int *arg, struct me4000_ai_context *ai_context)
4207 CALL_PDEBUG("me4000_ai_fsm_state() is executed\n");
4210 (me4000_inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) ? 1
4213 if (put_user(tmp, arg)) {
4214 printk(KERN_ERR "me4000_ai_fsm_state():Cannot copy to user\n");
4221 static int me4000_ai_get_count_buffer(unsigned long *arg,
4222 struct me4000_ai_context *ai_context)
4227 c = me4000_buf_count(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT);
4229 err = copy_to_user(arg, &c, sizeof(unsigned long));
4232 "%s:Can't copy to user space\n", __func__);
4239 /*---------------------------------- EEPROM stuff ---------------------------*/
4241 static int eeprom_write_cmd(struct me4000_ai_context *ai_context, unsigned long cmd,
4245 unsigned long value;
4247 CALL_PDEBUG("eeprom_write_cmd() is executed\n");
4249 PDEBUG("eeprom_write_cmd():Write command 0x%08lX with length = %d\n",
4252 /* Get the ICR register and clear the related bits */
4253 value = me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR);
4254 value &= ~(PLX_ICR_MASK_EEPROM);
4255 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4257 /* Raise the chip select */
4258 value |= PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4259 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4260 udelay(EEPROM_DELAY);
4262 for (i = 0; i < length; i++) {
4263 if (cmd & ((0x1 << (length - 1)) >> i)) {
4264 value |= PLX_ICR_BIT_EEPROM_WRITE;
4266 value &= ~PLX_ICR_BIT_EEPROM_WRITE;
4269 /* Write to EEPROM */
4271 ai_context->board_info->plx_regbase + PLX_ICR);
4272 udelay(EEPROM_DELAY);
4274 /* Raising edge of the clock */
4275 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4277 ai_context->board_info->plx_regbase + PLX_ICR);
4278 udelay(EEPROM_DELAY);
4280 /* Falling edge of the clock */
4281 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4283 ai_context->board_info->plx_regbase + PLX_ICR);
4284 udelay(EEPROM_DELAY);
4287 /* Clear the chip select */
4288 value &= ~PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4289 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4290 udelay(EEPROM_DELAY);
4292 /* Wait until hardware is ready for sure */
4298 static unsigned short eeprom_read_cmd(struct me4000_ai_context *ai_context,
4299 unsigned long cmd, int length)
4302 unsigned long value;
4303 unsigned short id = 0;
4305 CALL_PDEBUG("eeprom_read_cmd() is executed\n");
4307 PDEBUG("eeprom_read_cmd():Read command 0x%08lX with length = %d\n", cmd,
4310 /* Get the ICR register and clear the related bits */
4311 value = me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR);
4312 value &= ~(PLX_ICR_MASK_EEPROM);
4314 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4316 /* Raise the chip select */
4317 value |= PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4318 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4319 udelay(EEPROM_DELAY);
4321 /* Write the read command to the eeprom */
4322 for (i = 0; i < length; i++) {
4323 if (cmd & ((0x1 << (length - 1)) >> i)) {
4324 value |= PLX_ICR_BIT_EEPROM_WRITE;
4326 value &= ~PLX_ICR_BIT_EEPROM_WRITE;
4329 ai_context->board_info->plx_regbase + PLX_ICR);
4330 udelay(EEPROM_DELAY);
4332 /* Raising edge of the clock */
4333 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4335 ai_context->board_info->plx_regbase + PLX_ICR);
4336 udelay(EEPROM_DELAY);
4338 /* Falling edge of the clock */
4339 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4341 ai_context->board_info->plx_regbase + PLX_ICR);
4342 udelay(EEPROM_DELAY);
4345 /* Read the value from the eeprom */
4346 for (i = 0; i < 16; i++) {
4347 /* Raising edge of the clock */
4348 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4350 ai_context->board_info->plx_regbase + PLX_ICR);
4351 udelay(EEPROM_DELAY);
4353 if (me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR) &
4354 PLX_ICR_BIT_EEPROM_READ) {
4355 id |= (0x8000 >> i);
4356 PDEBUG("eeprom_read_cmd():OR with 0x%04X\n",
4359 PDEBUG("eeprom_read_cmd():Dont't OR\n");
4362 /* Falling edge of the clock */
4363 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4365 ai_context->board_info->plx_regbase + PLX_ICR);
4366 udelay(EEPROM_DELAY);
4369 /* Clear the chip select */
4370 value &= ~PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4371 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4372 udelay(EEPROM_DELAY);
4377 static int me4000_eeprom_write(struct me4000_eeprom *arg,
4378 struct me4000_ai_context *ai_context)
4381 struct me4000_eeprom setup;
4383 unsigned long date_high;
4384 unsigned long date_low;
4386 CALL_PDEBUG("me4000_eeprom_write() is executed\n");
4388 err = copy_from_user(&setup, arg, sizeof(setup));
4391 "ME4000:me4000_eeprom_write():Cannot copy from user\n");
4395 /* Enable writing */
4396 eeprom_write_cmd(ai_context, ME4000_EEPROM_CMD_WRITE_ENABLE,
4397 ME4000_EEPROM_CMD_LENGTH_WRITE_ENABLE);
4399 /* Command for date */
4400 date_high = (setup.date & 0xFFFF0000) >> 16;
4401 date_low = (setup.date & 0x0000FFFF);
4404 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_DATE_HIGH <<
4405 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4409 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4414 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_DATE_LOW <<
4415 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4419 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4423 /* Command for unipolar 10V offset */
4425 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_UNI_OFFSET <<
4426 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4431 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4435 /* Command for unipolar 10V fullscale */
4437 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_UNI_FULLSCALE <<
4438 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4443 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4447 /* Command for unipolar 2,5V offset */
4449 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_UNI_OFFSET <<
4450 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4455 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4459 /* Command for unipolar 2,5V fullscale */
4461 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_UNI_FULLSCALE <<
4462 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4467 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4471 /* Command for bipolar 10V offset */
4473 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_BI_OFFSET <<
4474 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4479 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4483 /* Command for bipolar 10V fullscale */
4485 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_BI_FULLSCALE <<
4486 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4491 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4495 /* Command for bipolar 2,5V offset */
4497 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_BI_OFFSET <<
4498 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4503 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4507 /* Command for bipolar 2,5V fullscale */
4509 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_BI_FULLSCALE <<
4510 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4515 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4519 /* Command for differential 10V offset */
4521 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_DIFF_OFFSET <<
4522 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4527 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4531 /* Command for differential 10V fullscale */
4533 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_DIFF_FULLSCALE
4534 << ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4539 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4543 /* Command for differential 2,5V offset */
4545 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_DIFF_OFFSET <<
4546 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4551 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4555 /* Command for differential 2,5V fullscale */
4557 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_DIFF_FULLSCALE
4558 << ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4562 diff_2_5_fullscale);
4563 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4567 /* Disable writing */
4568 eeprom_write_cmd(ai_context, ME4000_EEPROM_CMD_WRITE_DISABLE,
4569 ME4000_EEPROM_CMD_LENGTH_WRITE_DISABLE);
4574 static int me4000_eeprom_read(struct me4000_eeprom *arg,
4575 struct me4000_ai_context *ai_context)
4579 struct me4000_eeprom setup;
4581 CALL_PDEBUG("me4000_eeprom_read() is executed\n");
4583 /* Command for date */
4584 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_DATE_HIGH;
4586 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4588 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_DATE_LOW;
4590 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4592 /* Command for unipolar 10V offset */
4593 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_UNI_OFFSET;
4594 setup.uni_10_offset =
4595 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4597 /* Command for unipolar 10V fullscale */
4598 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_UNI_FULLSCALE;
4599 setup.uni_10_fullscale =
4600 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4602 /* Command for unipolar 2,5V offset */
4603 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_UNI_OFFSET;
4604 setup.uni_2_5_offset =
4605 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4607 /* Command for unipolar 2,5V fullscale */
4608 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_UNI_FULLSCALE;
4609 setup.uni_2_5_fullscale =
4610 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4612 /* Command for bipolar 10V offset */
4613 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_BI_OFFSET;
4614 setup.bi_10_offset =
4615 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4617 /* Command for bipolar 10V fullscale */
4618 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_BI_FULLSCALE;
4619 setup.bi_10_fullscale =
4620 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4622 /* Command for bipolar 2,5V offset */
4623 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_BI_OFFSET;
4624 setup.bi_2_5_offset =
4625 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4627 /* Command for bipolar 2,5V fullscale */
4628 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_BI_FULLSCALE;
4629 setup.bi_2_5_fullscale =
4630 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4632 /* Command for differntial 10V offset */
4633 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_DIFF_OFFSET;
4634 setup.diff_10_offset =
4635 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4637 /* Command for differential 10V fullscale */
4638 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_DIFF_FULLSCALE;
4639 setup.diff_10_fullscale =
4640 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4642 /* Command for differntial 2,5V offset */
4643 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_DIFF_OFFSET;
4644 setup.diff_2_5_offset =
4645 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4647 /* Command for differential 2,5V fullscale */
4648 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_DIFF_FULLSCALE;
4649 setup.diff_2_5_fullscale =
4650 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4652 err = copy_to_user(arg, &setup, sizeof(setup));
4655 "ME4000:me4000_eeprom_read():Cannot copy to user\n");
4662 /*------------------------------------ DIO stuff ----------------------------------------------*/
4664 static int me4000_dio_ioctl(struct inode *inode_p, struct file *file_p,
4665 unsigned int service, unsigned long arg)
4667 struct me4000_dio_context *dio_context;
4669 CALL_PDEBUG("me4000_dio_ioctl() is executed\n");
4671 dio_context = file_p->private_data;
4673 if (_IOC_TYPE(service) != ME4000_MAGIC) {
4674 printk(KERN_ERR "me4000_dio_ioctl():Wrong magic number\n");
4677 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
4678 printk(KERN_ERR "me4000_dio_ioctl():Service number to high\n");
4683 case ME4000_DIO_CONFIG:
4684 return me4000_dio_config((struct me4000_dio_config *)arg,
4686 case ME4000_DIO_SET_BYTE:
4687 return me4000_dio_set_byte((struct me4000_dio_byte *)arg,
4689 case ME4000_DIO_GET_BYTE:
4690 return me4000_dio_get_byte((struct me4000_dio_byte *)arg,
4692 case ME4000_DIO_RESET:
4693 return me4000_dio_reset(dio_context);
4696 "ME4000:me4000_dio_ioctl():Invalid service number %d\n",
4703 static int me4000_dio_config(struct me4000_dio_config *arg,
4704 struct me4000_dio_context *dio_context)
4706 struct me4000_dio_config cmd;
4710 CALL_PDEBUG("me4000_dio_config() is executed\n");
4712 /* Copy data from user */
4713 err = copy_from_user(&cmd, arg, sizeof(struct me4000_dio_config));
4716 "ME4000:me4000_dio_config():Can't copy from user space\n");
4720 /* Check port parameter */
4721 if (cmd.port >= dio_context->dio_count) {
4723 "ME4000:me4000_dio_config():Port %d is not available\n",
4728 PDEBUG("me4000_dio_config(): port %d, mode %d, function %d\n", cmd.port,
4729 cmd.mode, cmd.function);
4731 if (cmd.port == ME4000_DIO_PORT_A) {
4732 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4733 /* Check if opto isolated version */
4734 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4736 "ME4000:me4000_dio_config():Cannot set to input on opto isolated versions\n");
4740 tmp = me4000_inl(dio_context->ctrl_reg);
4742 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4743 ME4000_DIO_CTRL_BIT_MODE_1);
4744 me4000_outl(tmp, dio_context->ctrl_reg);
4745 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4746 tmp = me4000_inl(dio_context->ctrl_reg);
4748 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4749 ME4000_DIO_CTRL_BIT_MODE_1);
4750 tmp |= ME4000_DIO_CTRL_BIT_MODE_0;
4751 me4000_outl(tmp, dio_context->ctrl_reg);
4752 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4753 tmp = me4000_inl(dio_context->ctrl_reg);
4755 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4756 ME4000_DIO_CTRL_BIT_MODE_1 |
4757 ME4000_DIO_CTRL_BIT_FIFO_HIGH_0);
4759 ME4000_DIO_CTRL_BIT_MODE_0 |
4760 ME4000_DIO_CTRL_BIT_MODE_1;
4761 me4000_outl(tmp, dio_context->ctrl_reg);
4762 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4763 tmp = me4000_inl(dio_context->ctrl_reg);
4765 ME4000_DIO_CTRL_BIT_MODE_0 |
4766 ME4000_DIO_CTRL_BIT_MODE_1 |
4767 ME4000_DIO_CTRL_BIT_FIFO_HIGH_0;
4768 me4000_outl(tmp, dio_context->ctrl_reg);
4771 "ME4000:me4000_dio_config():Mode %d is not available\n",
4775 } else if (cmd.port == ME4000_DIO_PORT_B) {
4776 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4777 /* Only do anything when TTL version is installed */
4778 if ((me4000_inl(dio_context->dir_reg) & 0x1)) {
4779 tmp = me4000_inl(dio_context->ctrl_reg);
4781 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4782 ME4000_DIO_CTRL_BIT_MODE_3);
4783 me4000_outl(tmp, dio_context->ctrl_reg);
4785 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4786 /* Check if opto isolated version */
4787 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4789 "ME4000:me4000_dio_config():Cannot set to output on opto isolated versions\n");
4793 tmp = me4000_inl(dio_context->ctrl_reg);
4795 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4796 ME4000_DIO_CTRL_BIT_MODE_3);
4797 tmp |= ME4000_DIO_CTRL_BIT_MODE_2;
4798 me4000_outl(tmp, dio_context->ctrl_reg);
4799 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4800 /* Check if opto isolated version */
4801 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4803 "ME4000:me4000_dio_config():Cannot set to FIFO low output on opto isolated versions\n");
4807 tmp = me4000_inl(dio_context->ctrl_reg);
4809 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4810 ME4000_DIO_CTRL_BIT_MODE_3 |
4811 ME4000_DIO_CTRL_BIT_FIFO_HIGH_1);
4813 ME4000_DIO_CTRL_BIT_MODE_2 |
4814 ME4000_DIO_CTRL_BIT_MODE_3;
4815 me4000_outl(tmp, dio_context->ctrl_reg);
4816 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4817 /* Check if opto isolated version */
4818 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4820 "ME4000:me4000_dio_config():Cannot set to FIFO high output on opto isolated versions\n");
4824 tmp = me4000_inl(dio_context->ctrl_reg);
4826 ME4000_DIO_CTRL_BIT_MODE_2 |
4827 ME4000_DIO_CTRL_BIT_MODE_3 |
4828 ME4000_DIO_CTRL_BIT_FIFO_HIGH_1;
4829 me4000_outl(tmp, dio_context->ctrl_reg);
4832 "ME4000:me4000_dio_config():Mode %d is not available\n",
4836 } else if (cmd.port == ME4000_DIO_PORT_C) {
4837 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4838 tmp = me4000_inl(dio_context->ctrl_reg);
4840 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4841 ME4000_DIO_CTRL_BIT_MODE_5);
4842 me4000_outl(tmp, dio_context->ctrl_reg);
4843 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4844 tmp = me4000_inl(dio_context->ctrl_reg);
4846 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4847 ME4000_DIO_CTRL_BIT_MODE_5);
4848 tmp |= ME4000_DIO_CTRL_BIT_MODE_4;
4849 me4000_outl(tmp, dio_context->ctrl_reg);
4850 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4851 tmp = me4000_inl(dio_context->ctrl_reg);
4853 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4854 ME4000_DIO_CTRL_BIT_MODE_5 |
4855 ME4000_DIO_CTRL_BIT_FIFO_HIGH_2);
4857 ME4000_DIO_CTRL_BIT_MODE_4 |
4858 ME4000_DIO_CTRL_BIT_MODE_5;
4859 me4000_outl(tmp, dio_context->ctrl_reg);
4860 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4861 tmp = me4000_inl(dio_context->ctrl_reg);
4863 ME4000_DIO_CTRL_BIT_MODE_4 |
4864 ME4000_DIO_CTRL_BIT_MODE_5 |
4865 ME4000_DIO_CTRL_BIT_FIFO_HIGH_2;
4866 me4000_outl(tmp, dio_context->ctrl_reg);
4869 "ME4000:me4000_dio_config():Mode %d is not available\n",
4873 } else if (cmd.port == ME4000_DIO_PORT_D) {
4874 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4875 tmp = me4000_inl(dio_context->ctrl_reg);
4877 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4878 ME4000_DIO_CTRL_BIT_MODE_7);
4879 me4000_outl(tmp, dio_context->ctrl_reg);
4880 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4881 tmp = me4000_inl(dio_context->ctrl_reg);
4883 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4884 ME4000_DIO_CTRL_BIT_MODE_7);
4885 tmp |= ME4000_DIO_CTRL_BIT_MODE_6;
4886 me4000_outl(tmp, dio_context->ctrl_reg);
4887 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4888 tmp = me4000_inl(dio_context->ctrl_reg);
4890 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4891 ME4000_DIO_CTRL_BIT_MODE_7 |
4892 ME4000_DIO_CTRL_BIT_FIFO_HIGH_3);
4894 ME4000_DIO_CTRL_BIT_MODE_6 |
4895 ME4000_DIO_CTRL_BIT_MODE_7;
4896 me4000_outl(tmp, dio_context->ctrl_reg);
4897 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4898 tmp = me4000_inl(dio_context->ctrl_reg);
4900 ME4000_DIO_CTRL_BIT_MODE_6 |
4901 ME4000_DIO_CTRL_BIT_MODE_7 |
4902 ME4000_DIO_CTRL_BIT_FIFO_HIGH_3;
4903 me4000_outl(tmp, dio_context->ctrl_reg);
4906 "ME4000:me4000_dio_config():Mode %d is not available\n",
4912 "ME4000:me4000_dio_config():Port %d is not available\n",
4917 PDEBUG("me4000_dio_config(): port %d, mode %d, function %d\n", cmd.port,
4918 cmd.mode, cmd.function);
4920 if ((cmd.mode == ME4000_DIO_FIFO_HIGH)
4921 || (cmd.mode == ME4000_DIO_FIFO_LOW)) {
4922 tmp = me4000_inl(dio_context->ctrl_reg);
4924 ~(ME4000_DIO_CTRL_BIT_FUNCTION_0 |
4925 ME4000_DIO_CTRL_BIT_FUNCTION_1);
4926 if (cmd.function == ME4000_DIO_FUNCTION_PATTERN) {
4927 me4000_outl(tmp, dio_context->ctrl_reg);
4928 } else if (cmd.function == ME4000_DIO_FUNCTION_DEMUX) {
4929 tmp |= ME4000_DIO_CTRL_BIT_FUNCTION_0;
4930 me4000_outl(tmp, dio_context->ctrl_reg);
4931 } else if (cmd.function == ME4000_DIO_FUNCTION_MUX) {
4932 tmp |= ME4000_DIO_CTRL_BIT_FUNCTION_1;
4933 me4000_outl(tmp, dio_context->ctrl_reg);
4936 "ME4000:me4000_dio_config():Invalid port function specified\n");
4944 static int me4000_dio_set_byte(struct me4000_dio_byte *arg,
4945 struct me4000_dio_context *dio_context)
4947 struct me4000_dio_byte cmd;
4950 CALL_PDEBUG("me4000_dio_set_byte() is executed\n");
4952 /* Copy data from user */
4953 err = copy_from_user(&cmd, arg, sizeof(struct me4000_dio_byte));
4956 "ME4000:me4000_dio_set_byte():Can't copy from user space\n");
4960 /* Check port parameter */
4961 if (cmd.port >= dio_context->dio_count) {
4963 "ME4000:me4000_dio_set_byte():Port %d is not available\n",
4968 if (cmd.port == ME4000_DIO_PORT_A) {
4969 if ((me4000_inl(dio_context->ctrl_reg) & 0x3) != 0x1) {
4971 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4975 me4000_outl(cmd.byte, dio_context->port_0_reg);
4976 } else if (cmd.port == ME4000_DIO_PORT_B) {
4977 if ((me4000_inl(dio_context->ctrl_reg) & 0xC) != 0x4) {
4979 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4983 me4000_outl(cmd.byte, dio_context->port_1_reg);
4984 } else if (cmd.port == ME4000_DIO_PORT_C) {
4985 if ((me4000_inl(dio_context->ctrl_reg) & 0x30) != 0x10) {
4987 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4991 me4000_outl(cmd.byte, dio_context->port_2_reg);
4992 } else if (cmd.port == ME4000_DIO_PORT_D) {
4993 if ((me4000_inl(dio_context->ctrl_reg) & 0xC0) != 0x40) {
4995 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4999 me4000_outl(cmd.byte, dio_context->port_3_reg);
5002 "ME4000:me4000_dio_set_byte():Port %d is not available\n",
5010 static int me4000_dio_get_byte(struct me4000_dio_byte *arg,
5011 struct me4000_dio_context *dio_context)
5013 struct me4000_dio_byte cmd;
5016 CALL_PDEBUG("me4000_dio_get_byte() is executed\n");
5018 /* Copy data from user */
5019 err = copy_from_user(&cmd, arg, sizeof(struct me4000_dio_byte));
5022 "ME4000:me4000_dio_get_byte():Can't copy from user space\n");
5026 /* Check port parameter */
5027 if (cmd.port >= dio_context->dio_count) {
5029 "ME4000:me4000_dio_get_byte():Port %d is not available\n",
5034 if (cmd.port == ME4000_DIO_PORT_A) {
5035 cmd.byte = me4000_inl(dio_context->port_0_reg) & 0xFF;
5036 } else if (cmd.port == ME4000_DIO_PORT_B) {
5037 cmd.byte = me4000_inl(dio_context->port_1_reg) & 0xFF;
5038 } else if (cmd.port == ME4000_DIO_PORT_C) {
5039 cmd.byte = me4000_inl(dio_context->port_2_reg) & 0xFF;
5040 } else if (cmd.port == ME4000_DIO_PORT_D) {
5041 cmd.byte = me4000_inl(dio_context->port_3_reg) & 0xFF;
5044 "ME4000:me4000_dio_get_byte():Port %d is not available\n",
5049 /* Copy result back to user */
5050 err = copy_to_user(arg, &cmd, sizeof(struct me4000_dio_byte));
5053 "ME4000:me4000_dio_get_byte():Can't copy to user space\n");
5060 static int me4000_dio_reset(struct me4000_dio_context *dio_context)
5062 CALL_PDEBUG("me4000_dio_reset() is executed\n");
5064 /* Clear the control register */
5065 me4000_outl(0, dio_context->ctrl_reg);
5067 /* Check for opto isolated version */
5068 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
5069 me4000_outl(0x1, dio_context->ctrl_reg);
5070 me4000_outl(0x0, dio_context->port_0_reg);
5076 /*------------------------------------ COUNTER STUFF ------------------------------------*/
5078 static int me4000_cnt_ioctl(struct inode *inode_p, struct file *file_p,
5079 unsigned int service, unsigned long arg)
5081 struct me4000_cnt_context *cnt_context;
5083 CALL_PDEBUG("me4000_cnt_ioctl() is executed\n");
5085 cnt_context = file_p->private_data;
5087 if (_IOC_TYPE(service) != ME4000_MAGIC) {
5088 printk(KERN_ERR "me4000_dio_ioctl():Wrong magic number\n");
5091 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
5092 printk(KERN_ERR "me4000_dio_ioctl():Service number to high\n");
5097 case ME4000_CNT_READ:
5098 return me4000_cnt_read((struct me4000_cnt *)arg, cnt_context);
5099 case ME4000_CNT_WRITE:
5100 return me4000_cnt_write((struct me4000_cnt *)arg, cnt_context);
5101 case ME4000_CNT_CONFIG:
5102 return me4000_cnt_config((struct me4000_cnt_config *)arg,
5104 case ME4000_CNT_RESET:
5105 return me4000_cnt_reset(cnt_context);
5108 "ME4000:me4000_dio_ioctl():Invalid service number %d\n",
5115 static int me4000_cnt_config(struct me4000_cnt_config *arg,
5116 struct me4000_cnt_context *cnt_context)
5118 struct me4000_cnt_config cmd;
5123 CALL_PDEBUG("me4000_cnt_config() is executed\n");
5125 /* Copy data from user */
5126 err = copy_from_user(&cmd, arg, sizeof(struct me4000_cnt_config));
5129 "ME4000:me4000_cnt_config():Can't copy from user space\n");
5133 /* Check counter parameter */
5134 switch (cmd.counter) {
5135 case ME4000_CNT_COUNTER_0:
5136 counter = ME4000_CNT_CTRL_BIT_COUNTER_0;
5138 case ME4000_CNT_COUNTER_1:
5139 counter = ME4000_CNT_CTRL_BIT_COUNTER_1;
5141 case ME4000_CNT_COUNTER_2:
5142 counter = ME4000_CNT_CTRL_BIT_COUNTER_2;
5146 "ME4000:me4000_cnt_config():Counter %d is not available\n",
5151 /* Check mode parameter */
5153 case ME4000_CNT_MODE_0:
5154 mode = ME4000_CNT_CTRL_BIT_MODE_0;
5156 case ME4000_CNT_MODE_1:
5157 mode = ME4000_CNT_CTRL_BIT_MODE_1;
5159 case ME4000_CNT_MODE_2:
5160 mode = ME4000_CNT_CTRL_BIT_MODE_2;
5162 case ME4000_CNT_MODE_3:
5163 mode = ME4000_CNT_CTRL_BIT_MODE_3;
5165 case ME4000_CNT_MODE_4:
5166 mode = ME4000_CNT_CTRL_BIT_MODE_4;
5168 case ME4000_CNT_MODE_5:
5169 mode = ME4000_CNT_CTRL_BIT_MODE_5;
5173 "ME4000:me4000_cnt_config():Mode %d is not available\n",
5178 /* Write the control word */
5179 me4000_outb((counter | mode | 0x30), cnt_context->ctrl_reg);
5184 static int me4000_cnt_read(struct me4000_cnt *arg,
5185 struct me4000_cnt_context *cnt_context)
5187 struct me4000_cnt cmd;
5191 CALL_PDEBUG("me4000_cnt_read() is executed\n");
5193 /* Copy data from user */
5194 err = copy_from_user(&cmd, arg, sizeof(struct me4000_cnt));
5197 "ME4000:me4000_cnt_read():Can't copy from user space\n");
5202 switch (cmd.counter) {
5203 case ME4000_CNT_COUNTER_0:
5204 tmp = me4000_inb(cnt_context->counter_0_reg);
5206 tmp = me4000_inb(cnt_context->counter_0_reg);
5207 cmd.value |= ((u16) tmp) << 8;
5209 case ME4000_CNT_COUNTER_1:
5210 tmp = me4000_inb(cnt_context->counter_1_reg);
5212 tmp = me4000_inb(cnt_context->counter_1_reg);
5213 cmd.value |= ((u16) tmp) << 8;
5215 case ME4000_CNT_COUNTER_2:
5216 tmp = me4000_inb(cnt_context->counter_2_reg);
5218 tmp = me4000_inb(cnt_context->counter_2_reg);
5219 cmd.value |= ((u16) tmp) << 8;
5223 "ME4000:me4000_cnt_read():Counter %d is not available\n",
5228 /* Copy result back to user */
5229 err = copy_to_user(arg, &cmd, sizeof(struct me4000_cnt));
5232 "ME4000:me4000_cnt_read():Can't copy to user space\n");
5239 static int me4000_cnt_write(struct me4000_cnt *arg,
5240 struct me4000_cnt_context *cnt_context)
5242 struct me4000_cnt cmd;
5246 CALL_PDEBUG("me4000_cnt_write() is executed\n");
5248 /* Copy data from user */
5249 err = copy_from_user(&cmd, arg, sizeof(struct me4000_cnt));
5252 "ME4000:me4000_cnt_write():Can't copy from user space\n");
5257 switch (cmd.counter) {
5258 case ME4000_CNT_COUNTER_0:
5259 tmp = cmd.value & 0xFF;
5260 me4000_outb(tmp, cnt_context->counter_0_reg);
5261 tmp = (cmd.value >> 8) & 0xFF;
5262 me4000_outb(tmp, cnt_context->counter_0_reg);
5264 case ME4000_CNT_COUNTER_1:
5265 tmp = cmd.value & 0xFF;
5266 me4000_outb(tmp, cnt_context->counter_1_reg);
5267 tmp = (cmd.value >> 8) & 0xFF;
5268 me4000_outb(tmp, cnt_context->counter_1_reg);
5270 case ME4000_CNT_COUNTER_2:
5271 tmp = cmd.value & 0xFF;
5272 me4000_outb(tmp, cnt_context->counter_2_reg);
5273 tmp = (cmd.value >> 8) & 0xFF;
5274 me4000_outb(tmp, cnt_context->counter_2_reg);
5278 "ME4000:me4000_cnt_write():Counter %d is not available\n",
5286 static int me4000_cnt_reset(struct me4000_cnt_context *cnt_context)
5288 CALL_PDEBUG("me4000_cnt_reset() is executed\n");
5290 /* Set the mode and value for counter 0 */
5291 me4000_outb(0x30, cnt_context->ctrl_reg);
5292 me4000_outb(0x00, cnt_context->counter_0_reg);
5293 me4000_outb(0x00, cnt_context->counter_0_reg);
5295 /* Set the mode and value for counter 1 */
5296 me4000_outb(0x70, cnt_context->ctrl_reg);
5297 me4000_outb(0x00, cnt_context->counter_1_reg);
5298 me4000_outb(0x00, cnt_context->counter_1_reg);
5300 /* Set the mode and value for counter 2 */
5301 me4000_outb(0xB0, cnt_context->ctrl_reg);
5302 me4000_outb(0x00, cnt_context->counter_2_reg);
5303 me4000_outb(0x00, cnt_context->counter_2_reg);
5308 /*------------------------------------ External Interrupt stuff ------------------------------------*/
5310 static int me4000_ext_int_ioctl(struct inode *inode_p, struct file *file_p,
5311 unsigned int service, unsigned long arg)
5313 struct me4000_ext_int_context *ext_int_context;
5315 CALL_PDEBUG("me4000_ext_int_ioctl() is executed\n");
5317 ext_int_context = file_p->private_data;
5319 if (_IOC_TYPE(service) != ME4000_MAGIC) {
5320 printk(KERN_ERR "me4000_ext_int_ioctl():Wrong magic number\n");
5323 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
5325 "me4000_ext_int_ioctl():Service number to high\n");
5330 case ME4000_EXT_INT_ENABLE:
5331 return me4000_ext_int_enable(ext_int_context);
5332 case ME4000_EXT_INT_DISABLE:
5333 return me4000_ext_int_disable(ext_int_context);
5334 case ME4000_EXT_INT_COUNT:
5335 return me4000_ext_int_count((unsigned long *)arg,
5339 "ME4000:me4000_ext_int_ioctl():Invalid service number %d\n",
5346 static int me4000_ext_int_enable(struct me4000_ext_int_context *ext_int_context)
5350 CALL_PDEBUG("me4000_ext_int_enable() is executed\n");
5352 tmp = me4000_inl(ext_int_context->ctrl_reg);
5353 tmp |= ME4000_AI_CTRL_BIT_EX_IRQ;
5354 me4000_outl(tmp, ext_int_context->ctrl_reg);
5359 static int me4000_ext_int_disable(struct me4000_ext_int_context *ext_int_context)
5363 CALL_PDEBUG("me4000_ext_int_disable() is executed\n");
5365 tmp = me4000_inl(ext_int_context->ctrl_reg);
5366 tmp &= ~ME4000_AI_CTRL_BIT_EX_IRQ;
5367 me4000_outl(tmp, ext_int_context->ctrl_reg);
5372 static int me4000_ext_int_count(unsigned long *arg,
5373 struct me4000_ext_int_context *ext_int_context)
5376 CALL_PDEBUG("me4000_ext_int_count() is executed\n");
5378 put_user(ext_int_context->int_count, arg);
5382 /*------------------------------------ General stuff ------------------------------------*/
5384 static int me4000_get_user_info(struct me4000_user_info *arg,
5385 struct me4000_info *board_info)
5387 struct me4000_user_info user_info;
5389 CALL_PDEBUG("me4000_get_user_info() is executed\n");
5391 user_info.board_count = board_info->board_count;
5392 user_info.plx_regbase = board_info->plx_regbase;
5393 user_info.plx_regbase_size = board_info->plx_regbase_size;
5394 user_info.me4000_regbase = board_info->me4000_regbase;
5395 user_info.me4000_regbase_size = board_info->me4000_regbase_size;
5396 user_info.serial_no = board_info->serial_no;
5397 user_info.hw_revision = board_info->hw_revision;
5398 user_info.vendor_id = board_info->vendor_id;
5399 user_info.device_id = board_info->device_id;
5400 user_info.pci_bus_no = board_info->pci_bus_no;
5401 user_info.pci_dev_no = board_info->pci_dev_no;
5402 user_info.pci_func_no = board_info->pci_func_no;
5403 user_info.irq = board_info->irq;
5404 user_info.irq_count = board_info->irq_count;
5405 user_info.driver_version = ME4000_DRIVER_VERSION;
5406 user_info.ao_count = board_info->board_p->ao.count;
5407 user_info.ao_fifo_count = board_info->board_p->ao.fifo_count;
5409 user_info.ai_count = board_info->board_p->ai.count;
5410 user_info.ai_sh_count = board_info->board_p->ai.sh_count;
5411 user_info.ai_ex_trig_analog = board_info->board_p->ai.ex_trig_analog;
5413 user_info.dio_count = board_info->board_p->dio.count;
5415 user_info.cnt_count = board_info->board_p->cnt.count;
5417 if (copy_to_user(arg, &user_info, sizeof(struct me4000_user_info)))
5423 /*------------------------------------ ISR STUFF ------------------------------------*/
5425 static int me4000_ext_int_fasync(int fd, struct file *file_ptr, int mode)
5428 struct me4000_ext_int_context *ext_int_context;
5430 CALL_PDEBUG("me4000_ext_int_fasync() is executed\n");
5432 ext_int_context = file_ptr->private_data;
5435 fasync_helper(fd, file_ptr, mode, &ext_int_context->fasync_ptr);
5437 CALL_PDEBUG("me4000_ext_int_fasync() is leaved\n");
5441 static irqreturn_t me4000_ao_isr(int irq, void *dev_id)
5445 struct me4000_ao_context *ao_context;
5449 //unsigned long before;
5450 //unsigned long after;
5452 ISR_PDEBUG("me4000_ao_isr() is executed\n");
5454 ao_context = dev_id;
5456 /* Check if irq number is right */
5457 if (irq != ao_context->irq) {
5458 ISR_PDEBUG("me4000_ao_isr():incorrect interrupt num: %d\n",
5463 /* Check if this DAC rised an interrupt */
5465 ((0x1 << (ao_context->index + 3)) &
5466 me4000_inl(ao_context->irq_status_reg))) {
5467 ISR_PDEBUG("me4000_ao_isr():Not this DAC\n");
5471 /* Read status register to find out what happened */
5472 tmp = me4000_inl(ao_context->status_reg);
5474 if (!(tmp & ME4000_AO_STATUS_BIT_EF) && (tmp & ME4000_AO_STATUS_BIT_HF)
5475 && (tmp & ME4000_AO_STATUS_BIT_HF)) {
5476 c = ME4000_AO_FIFO_COUNT;
5477 ISR_PDEBUG("me4000_ao_isr():Fifo empty\n");
5478 } else if ((tmp & ME4000_AO_STATUS_BIT_EF)
5479 && (tmp & ME4000_AO_STATUS_BIT_HF)
5480 && (tmp & ME4000_AO_STATUS_BIT_HF)) {
5481 c = ME4000_AO_FIFO_COUNT / 2;
5482 ISR_PDEBUG("me4000_ao_isr():Fifo under half full\n");
5485 ISR_PDEBUG("me4000_ao_isr():Fifo full\n");
5488 ISR_PDEBUG("me4000_ao_isr():Try to write 0x%04X values\n", c);
5491 c1 = me4000_values_to_end(ao_context->circ_buf,
5492 ME4000_AO_BUFFER_COUNT);
5493 ISR_PDEBUG("me4000_ao_isr():Values to end = %d\n", c1);
5499 ("me4000_ao_isr():Work done or buffer empty\n");
5503 if (((ao_context->fifo_reg & 0xFF) == ME4000_AO_01_FIFO_REG) ||
5504 ((ao_context->fifo_reg & 0xFF) == ME4000_AO_03_FIFO_REG)) {
5505 for (i = 0; i < c1; i++) {
5509 (ao_context->circ_buf.buf +
5510 ao_context->circ_buf.tail + i))) << 16;
5511 outl(value, ao_context->fifo_reg);
5514 outsw(ao_context->fifo_reg,
5515 ao_context->circ_buf.buf +
5516 ao_context->circ_buf.tail, c1);
5519 //printk(KERN_ERR"ME4000:me4000_ao_isr():Time lapse = %lu\n", after - before);
5521 ao_context->circ_buf.tail =
5522 (ao_context->circ_buf.tail + c1) & (ME4000_AO_BUFFER_COUNT -
5524 ISR_PDEBUG("me4000_ao_isr():%d values wrote to port 0x%04X\n",
5525 c1, ao_context->fifo_reg);
5529 /* If there are no values left in the buffer, disable interrupts */
5530 spin_lock(&ao_context->int_lock);
5531 if (!me4000_buf_count(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
5533 ("me4000_ao_isr():Disable Interrupt because no values left in buffer\n");
5534 tmp = me4000_inl(ao_context->ctrl_reg);
5535 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_IRQ;
5536 me4000_outl(tmp, ao_context->ctrl_reg);
5538 spin_unlock(&ao_context->int_lock);
5540 /* Reset the interrupt */
5541 spin_lock(&ao_context->int_lock);
5542 tmp = me4000_inl(ao_context->ctrl_reg);
5543 tmp |= ME4000_AO_CTRL_BIT_RESET_IRQ;
5544 me4000_outl(tmp, ao_context->ctrl_reg);
5545 tmp &= ~ME4000_AO_CTRL_BIT_RESET_IRQ;
5546 me4000_outl(tmp, ao_context->ctrl_reg);
5548 /* If state machine is stopped, flow was interrupted */
5549 if (!(me4000_inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM)) {
5550 printk(KERN_ERR "ME4000:me4000_ao_isr():Broken pipe\n");
5551 ao_context->pipe_flag = 1; // Set flag in order to inform write routine
5552 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_IRQ; // Disable interrupt
5554 me4000_outl(tmp, ao_context->ctrl_reg);
5555 spin_unlock(&ao_context->int_lock);
5557 /* Wake up waiting process */
5558 wake_up_interruptible(&(ao_context->wait_queue));
5560 /* Count the interrupt */
5561 ao_context->board_info->irq_count++;
5566 static irqreturn_t me4000_ai_isr(int irq, void *dev_id)
5569 struct me4000_ai_context *ai_context;
5573 #ifdef ME4000_ISR_DEBUG
5574 unsigned long before;
5575 unsigned long after;
5578 ISR_PDEBUG("me4000_ai_isr() is executed\n");
5580 #ifdef ME4000_ISR_DEBUG
5584 ai_context = dev_id;
5586 /* Check if irq number is right */
5587 if (irq != ai_context->irq) {
5588 ISR_PDEBUG("me4000_ai_isr():incorrect interrupt num: %d\n",
5593 if (me4000_inl(ai_context->irq_status_reg) &
5594 ME4000_IRQ_STATUS_BIT_AI_HF) {
5596 ("me4000_ai_isr():Fifo half full interrupt occured\n");
5598 /* Read status register to find out what happened */
5599 tmp = me4000_inl(ai_context->ctrl_reg);
5601 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5602 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5603 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5604 ISR_PDEBUG("me4000_ai_isr():Fifo full\n");
5605 c = ME4000_AI_FIFO_COUNT;
5607 /* FIFO overflow, so stop conversion and disable all interrupts */
5608 spin_lock(&ai_context->int_lock);
5609 tmp = me4000_inl(ai_context->ctrl_reg);
5610 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5612 ~(ME4000_AI_CTRL_BIT_HF_IRQ |
5613 ME4000_AI_CTRL_BIT_SC_IRQ);
5614 outl(tmp, ai_context->ctrl_reg);
5615 spin_unlock(&ai_context->int_lock);
5616 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5617 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5618 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5619 ISR_PDEBUG("me4000_ai_isr():Fifo half full\n");
5620 c = ME4000_AI_FIFO_COUNT / 2;
5624 ("me4000_ai_isr():Can't determine state of fifo\n");
5627 ISR_PDEBUG("me4000_ai_isr():Try to read %d values\n", c);
5630 c1 = me4000_space_to_end(ai_context->circ_buf,
5631 ME4000_AI_BUFFER_COUNT);
5632 ISR_PDEBUG("me4000_ai_isr():Space to end = %d\n", c1);
5638 ("me4000_ai_isr():Work done or buffer full\n");
5642 insw(ai_context->data_reg,
5643 ai_context->circ_buf.buf +
5644 ai_context->circ_buf.head, c1);
5645 ai_context->circ_buf.head =
5646 (ai_context->circ_buf.head +
5647 c1) & (ME4000_AI_BUFFER_COUNT - 1);
5651 /* Work is done, so reset the interrupt */
5653 ("me4000_ai_isr():reset interrupt fifo half full interrupt\n");
5654 spin_lock(&ai_context->int_lock);
5655 tmp = me4000_inl(ai_context->ctrl_reg);
5656 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
5657 me4000_outl(tmp, ai_context->ctrl_reg);
5658 tmp &= ~ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
5659 me4000_outl(tmp, ai_context->ctrl_reg);
5660 spin_unlock(&ai_context->int_lock);
5663 if (me4000_inl(ai_context->irq_status_reg) & ME4000_IRQ_STATUS_BIT_SC) {
5665 ("me4000_ai_isr():Sample counter interrupt occured\n");
5667 if (!ai_context->sample_counter_reload) {
5669 ("me4000_ai_isr():Single data block available\n");
5671 /* Poll data until fifo empty */
5673 (i < ME4000_AI_FIFO_COUNT / 2)
5674 && (inl(ai_context->ctrl_reg) &
5675 ME4000_AI_STATUS_BIT_EF_DATA); i++) {
5676 if (me4000_space_to_end
5677 (ai_context->circ_buf,
5678 ME4000_AI_BUFFER_COUNT)) {
5679 *(ai_context->circ_buf.buf +
5680 ai_context->circ_buf.head) =
5681 inw(ai_context->data_reg);
5682 ai_context->circ_buf.head =
5683 (ai_context->circ_buf.head +
5684 1) & (ME4000_AI_BUFFER_COUNT - 1);
5688 ISR_PDEBUG("me4000_ai_isr():%d values read\n", i);
5690 if (ai_context->sample_counter <=
5691 ME4000_AI_FIFO_COUNT / 2) {
5693 ("me4000_ai_isr():Interrupt from adjustable half full threshold\n");
5695 /* Read status register to find out what happened */
5696 tmp = me4000_inl(ai_context->ctrl_reg);
5698 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5699 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5700 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5702 ("me4000_ai_isr():Fifo full\n");
5703 c = ME4000_AI_FIFO_COUNT;
5705 /* FIFO overflow, so stop conversion */
5706 spin_lock(&ai_context->int_lock);
5707 tmp = me4000_inl(ai_context->ctrl_reg);
5709 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5710 outl(tmp, ai_context->ctrl_reg);
5711 spin_unlock(&ai_context->int_lock);
5712 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA)
5714 ME4000_AI_STATUS_BIT_HF_DATA)
5716 ME4000_AI_STATUS_BIT_EF_DATA)) {
5718 ("me4000_ai_isr():Fifo half full\n");
5719 c = ME4000_AI_FIFO_COUNT / 2;
5721 c = ai_context->sample_counter;
5723 ("me4000_ai_isr():Sample count values\n");
5727 ("me4000_ai_isr():Try to read %d values\n",
5731 c1 = me4000_space_to_end(ai_context->
5733 ME4000_AI_BUFFER_COUNT);
5735 ("me4000_ai_isr():Space to end = %d\n",
5742 ("me4000_ai_isr():Work done or buffer full\n");
5746 insw(ai_context->data_reg,
5747 ai_context->circ_buf.buf +
5748 ai_context->circ_buf.head, c1);
5749 ai_context->circ_buf.head =
5750 (ai_context->circ_buf.head +
5751 c1) & (ME4000_AI_BUFFER_COUNT - 1);
5756 ("me4000_ai_isr():Multiple data block available\n");
5758 /* Read status register to find out what happened */
5759 tmp = me4000_inl(ai_context->ctrl_reg);
5761 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5762 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5763 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5765 ("me4000_ai_isr():Fifo full\n");
5766 c = ME4000_AI_FIFO_COUNT;
5768 /* FIFO overflow, so stop conversion */
5769 spin_lock(&ai_context->int_lock);
5770 tmp = me4000_inl(ai_context->ctrl_reg);
5772 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5773 outl(tmp, ai_context->ctrl_reg);
5774 spin_unlock(&ai_context->int_lock);
5777 c1 = me4000_space_to_end
5778 (ai_context->circ_buf,
5779 ME4000_AI_BUFFER_COUNT);
5781 ("me4000_ai_isr():Space to end = %d\n",
5788 ("me4000_ai_isr():Work done or buffer full\n");
5792 insw(ai_context->data_reg,
5793 ai_context->circ_buf.buf +
5794 ai_context->circ_buf.head,
5796 ai_context->circ_buf.head =
5797 (ai_context->circ_buf.head +
5799 (ME4000_AI_BUFFER_COUNT -
5803 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA)
5805 ME4000_AI_STATUS_BIT_HF_DATA)
5807 ME4000_AI_STATUS_BIT_EF_DATA)) {
5809 ("me4000_ai_isr():Fifo half full\n");
5810 c = ME4000_AI_FIFO_COUNT / 2;
5813 c1 = me4000_space_to_end
5814 (ai_context->circ_buf,
5815 ME4000_AI_BUFFER_COUNT);
5817 ("me4000_ai_isr():Space to end = %d\n",
5824 ("me4000_ai_isr():Work done or buffer full\n");
5828 insw(ai_context->data_reg,
5829 ai_context->circ_buf.buf +
5830 ai_context->circ_buf.head,
5832 ai_context->circ_buf.head =
5833 (ai_context->circ_buf.head +
5835 (ME4000_AI_BUFFER_COUNT -
5840 /* Poll data until fifo empty */
5842 (i < ME4000_AI_FIFO_COUNT / 2)
5843 && (inl(ai_context->ctrl_reg) &
5844 ME4000_AI_STATUS_BIT_EF_DATA);
5846 if (me4000_space_to_end
5847 (ai_context->circ_buf,
5848 ME4000_AI_BUFFER_COUNT)) {
5849 *(ai_context->circ_buf.
5851 ai_context->circ_buf.
5853 inw(ai_context->data_reg);
5854 ai_context->circ_buf.
5859 (ME4000_AI_BUFFER_COUNT
5865 ("me4000_ai_isr():%d values read\n",
5871 /* Work is done, so reset the interrupt */
5873 ("me4000_ai_isr():reset interrupt from sample counter\n");
5874 spin_lock(&ai_context->int_lock);
5875 tmp = me4000_inl(ai_context->ctrl_reg);
5876 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
5877 me4000_outl(tmp, ai_context->ctrl_reg);
5878 tmp &= ~ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
5879 me4000_outl(tmp, ai_context->ctrl_reg);
5880 spin_unlock(&ai_context->int_lock);
5883 /* Values are now available, so wake up waiting process */
5884 if (me4000_buf_count(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
5885 ISR_PDEBUG("me4000_ai_isr():Wake up waiting process\n");
5886 wake_up_interruptible(&(ai_context->wait_queue));
5889 /* If there is no space left in the buffer, disable interrupts */
5890 spin_lock(&ai_context->int_lock);
5891 if (!me4000_buf_space(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
5893 ("me4000_ai_isr():Disable Interrupt because no space left in buffer\n");
5894 tmp = me4000_inl(ai_context->ctrl_reg);
5896 ~(ME4000_AI_CTRL_BIT_SC_IRQ | ME4000_AI_CTRL_BIT_HF_IRQ |
5897 ME4000_AI_CTRL_BIT_LE_IRQ);
5898 me4000_outl(tmp, ai_context->ctrl_reg);
5900 spin_unlock(&ai_context->int_lock);
5902 #ifdef ME4000_ISR_DEBUG
5904 printk(KERN_ERR "ME4000:me4000_ai_isr():Time lapse = %lu\n",
5911 static irqreturn_t me4000_ext_int_isr(int irq, void *dev_id)
5913 struct me4000_ext_int_context *ext_int_context;
5916 ISR_PDEBUG("me4000_ext_int_isr() is executed\n");
5918 ext_int_context = dev_id;
5920 /* Check if irq number is right */
5921 if (irq != ext_int_context->irq) {
5922 ISR_PDEBUG("me4000_ext_int_isr():incorrect interrupt num: %d\n",
5927 if (me4000_inl(ext_int_context->irq_status_reg) &
5928 ME4000_IRQ_STATUS_BIT_EX) {
5929 ISR_PDEBUG("me4000_ext_int_isr():External interrupt occured\n");
5930 tmp = me4000_inl(ext_int_context->ctrl_reg);
5931 tmp |= ME4000_AI_CTRL_BIT_EX_IRQ_RESET;
5932 me4000_outl(tmp, ext_int_context->ctrl_reg);
5933 tmp &= ~ME4000_AI_CTRL_BIT_EX_IRQ_RESET;
5934 me4000_outl(tmp, ext_int_context->ctrl_reg);
5936 ext_int_context->int_count++;
5938 if (ext_int_context->fasync_ptr) {
5940 ("me2600_ext_int_isr():Send signal to process\n");
5941 kill_fasync(&ext_int_context->fasync_ptr, SIGIO,
5949 static void __exit me4000_module_exit(void)
5951 struct me4000_info *board_info;
5953 CALL_PDEBUG("cleanup_module() is executed\n");
5955 unregister_chrdev(me4000_ext_int_major_driver_no, ME4000_EXT_INT_NAME);
5957 unregister_chrdev(me4000_cnt_major_driver_no, ME4000_CNT_NAME);
5959 unregister_chrdev(me4000_dio_major_driver_no, ME4000_DIO_NAME);
5961 unregister_chrdev(me4000_ai_major_driver_no, ME4000_AI_NAME);
5963 unregister_chrdev(me4000_ao_major_driver_no, ME4000_AO_NAME);
5965 remove_proc_entry("me4000", NULL);
5967 pci_unregister_driver(&me4000_driver);
5969 /* Reset the boards */
5970 list_for_each_entry(board_info, &me4000_board_info_list, list) {
5971 me4000_reset_board(board_info);
5974 clear_board_info_list();
5977 module_exit(me4000_module_exit);
5979 static int me4000_read_procmem(char *buf, char **start, off_t offset, int count,
5980 int *eof, void *data)
5983 int limit = count - 1000;
5984 struct me4000_info *board_info;
5986 len += sprintf(buf + len, "\nME4000 DRIVER VERSION %X.%X.%X\n\n",
5987 (ME4000_DRIVER_VERSION & 0xFF0000) >> 16,
5988 (ME4000_DRIVER_VERSION & 0xFF00) >> 8,
5989 (ME4000_DRIVER_VERSION & 0xFF));
5991 /* Search for the board context */
5992 list_for_each_entry(board_info, &me4000_board_info_list, list) {
5994 sprintf(buf + len, "Board number %d:\n",
5995 board_info->board_count);
5996 len += sprintf(buf + len, "---------------\n");
5998 sprintf(buf + len, "PLX base register = 0x%lX\n",
5999 board_info->plx_regbase);
6001 sprintf(buf + len, "PLX base register size = 0x%X\n",
6002 (unsigned int)board_info->plx_regbase_size);
6004 sprintf(buf + len, "ME4000 base register = 0x%X\n",
6005 (unsigned int)board_info->me4000_regbase);
6007 sprintf(buf + len, "ME4000 base register size = 0x%X\n",
6008 (unsigned int)board_info->me4000_regbase_size);
6010 sprintf(buf + len, "Serial number = 0x%X\n",
6011 board_info->serial_no);
6013 sprintf(buf + len, "Hardware revision = 0x%X\n",
6014 board_info->hw_revision);
6016 sprintf(buf + len, "Vendor id = 0x%X\n",
6017 board_info->vendor_id);
6019 sprintf(buf + len, "Device id = 0x%X\n",
6020 board_info->device_id);
6022 sprintf(buf + len, "PCI bus number = %d\n",
6023 board_info->pci_bus_no);
6025 sprintf(buf + len, "PCI device number = %d\n",
6026 board_info->pci_dev_no);
6028 sprintf(buf + len, "PCI function number = %d\n",
6029 board_info->pci_func_no);
6030 len += sprintf(buf + len, "IRQ = %u\n", board_info->irq);
6033 "Count of interrupts since module was loaded = %d\n",
6034 board_info->irq_count);
6037 sprintf(buf + len, "Count of analog outputs = %d\n",
6038 board_info->board_p->ao.count);
6040 sprintf(buf + len, "Count of analog output fifos = %d\n",
6041 board_info->board_p->ao.fifo_count);
6044 sprintf(buf + len, "Count of analog inputs = %d\n",
6045 board_info->board_p->ai.count);
6048 "Count of sample and hold devices for analog input = %d\n",
6049 board_info->board_p->ai.sh_count);
6052 "Analog external trigger available for analog input = %d\n",
6053 board_info->board_p->ai.ex_trig_analog);
6056 sprintf(buf + len, "Count of digital ports = %d\n",
6057 board_info->board_p->dio.count);
6060 sprintf(buf + len, "Count of counter devices = %d\n",
6061 board_info->board_p->cnt.count);
6063 sprintf(buf + len, "AI control register = 0x%08X\n",
6064 inl(board_info->me4000_regbase +
6065 ME4000_AI_CTRL_REG));
6067 len += sprintf(buf + len, "AO 0 control register = 0x%08X\n",
6068 inl(board_info->me4000_regbase +
6069 ME4000_AO_00_CTRL_REG));
6071 sprintf(buf + len, "AO 0 status register = 0x%08X\n",
6072 inl(board_info->me4000_regbase +
6073 ME4000_AO_00_STATUS_REG));
6075 sprintf(buf + len, "AO 1 control register = 0x%08X\n",
6076 inl(board_info->me4000_regbase +
6077 ME4000_AO_01_CTRL_REG));
6079 sprintf(buf + len, "AO 1 status register = 0x%08X\n",
6080 inl(board_info->me4000_regbase +
6081 ME4000_AO_01_STATUS_REG));
6083 sprintf(buf + len, "AO 2 control register = 0x%08X\n",
6084 inl(board_info->me4000_regbase +
6085 ME4000_AO_02_CTRL_REG));
6087 sprintf(buf + len, "AO 2 status register = 0x%08X\n",
6088 inl(board_info->me4000_regbase +
6089 ME4000_AO_02_STATUS_REG));
6091 sprintf(buf + len, "AO 3 control register = 0x%08X\n",
6092 inl(board_info->me4000_regbase +
6093 ME4000_AO_03_CTRL_REG));
6095 sprintf(buf + len, "AO 3 status register = 0x%08X\n",
6096 inl(board_info->me4000_regbase +
6097 ME4000_AO_03_STATUS_REG));