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 <asm/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 int inline me4000_buf_count(struct me4000_circ_buf buf, int size)
251 return ((buf.head - buf.tail) & (size - 1));
254 static int inline me4000_buf_space(struct me4000_circ_buf buf, int size)
256 return ((buf.tail - (buf.head + 1)) & (size - 1));
259 static int inline 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 int inline 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 void inline me4000_outb(unsigned char value, unsigned long port)
280 PORT_PDEBUG("--> 0x%02X port 0x%04lX\n", value, port);
284 static void inline me4000_outl(unsigned long value, unsigned long port)
286 PORT_PDEBUG("--> 0x%08lX port 0x%04lX\n", value, port);
290 static unsigned long inline me4000_inl(unsigned long port)
294 PORT_PDEBUG("<-- 0x%08lX port 0x%04lX\n", value, port);
298 static unsigned char inline 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 list_head *board_p;
540 struct list_head *dac_p;
541 struct me4000_info *board_info;
542 struct me4000_ao_context *ao_context;
544 /* Clear context lists */
545 for (board_p = me4000_board_info_list.next;
546 board_p != &me4000_board_info_list; board_p = board_p->next) {
547 board_info = list_entry(board_p, struct me4000_info, list);
548 /* Clear analog output context list */
549 while (!list_empty(&board_info->ao_context_list)) {
550 dac_p = board_info->ao_context_list.next;
552 list_entry(dac_p, struct me4000_ao_context, list);
553 me4000_ao_reset(ao_context);
554 free_irq(ao_context->irq, ao_context);
555 if (ao_context->circ_buf.buf)
556 kfree(ao_context->circ_buf.buf);
561 /* Clear analog input context */
562 if (board_info->ai_context->circ_buf.buf)
563 kfree(board_info->ai_context->circ_buf.buf);
564 kfree(board_info->ai_context);
566 /* Clear digital I/O context */
567 kfree(board_info->dio_context);
569 /* Clear counter context */
570 kfree(board_info->cnt_context);
572 /* Clear external interrupt context */
573 kfree(board_info->ext_int_context);
576 /* Clear the board info list */
577 while (!list_empty(&me4000_board_info_list)) {
578 board_p = me4000_board_info_list.next;
579 board_info = list_entry(board_p, struct me4000_info, list);
580 pci_release_regions(board_info->pci_dev_p);
586 static int get_registers(struct pci_dev *dev, struct me4000_info *board_info)
589 /*--------------------------- plx regbase ---------------------------------*/
591 board_info->plx_regbase = pci_resource_start(dev, 1);
592 if (board_info->plx_regbase == 0) {
594 "ME4000:get_registers():PCI base address 1 is not available\n");
597 board_info->plx_regbase_size = pci_resource_len(dev, 1);
600 ("get_registers():PLX configuration registers at address 0x%4lX [0x%4lX]\n",
601 board_info->plx_regbase, board_info->plx_regbase_size);
603 /*--------------------------- me4000 regbase ------------------------------*/
605 board_info->me4000_regbase = pci_resource_start(dev, 2);
606 if (board_info->me4000_regbase == 0) {
608 "ME4000:get_registers():PCI base address 2 is not available\n");
611 board_info->me4000_regbase_size = pci_resource_len(dev, 2);
613 PDEBUG("get_registers():ME4000 registers at address 0x%4lX [0x%4lX]\n",
614 board_info->me4000_regbase, board_info->me4000_regbase_size);
616 /*--------------------------- timer regbase ------------------------------*/
618 board_info->timer_regbase = pci_resource_start(dev, 3);
619 if (board_info->timer_regbase == 0) {
621 "ME4000:get_registers():PCI base address 3 is not available\n");
624 board_info->timer_regbase_size = pci_resource_len(dev, 3);
626 PDEBUG("get_registers():Timer registers at address 0x%4lX [0x%4lX]\n",
627 board_info->timer_regbase, board_info->timer_regbase_size);
629 /*--------------------------- program regbase ------------------------------*/
631 board_info->program_regbase = pci_resource_start(dev, 5);
632 if (board_info->program_regbase == 0) {
634 "get_registers():ME4000:PCI base address 5 is not available\n");
637 board_info->program_regbase_size = pci_resource_len(dev, 5);
639 PDEBUG("get_registers():Program registers at address 0x%4lX [0x%4lX]\n",
640 board_info->program_regbase, board_info->program_regbase_size);
645 static int init_board_info(struct pci_dev *pci_dev_p,
646 struct me4000_info *board_info)
650 struct list_head *board_p;
651 board_info->pci_dev_p = pci_dev_p;
653 for (i = 0; i < ARRAY_SIZE(me4000_boards); i++) {
654 if (me4000_boards[i].device_id == pci_dev_p->device) {
655 board_info->board_p = &me4000_boards[i];
659 if (i == ARRAY_SIZE(me4000_boards)) {
661 "ME4000:init_board_info():Device ID not valid\n");
665 /* Get the index of the board in the global list */
666 for (board_p = me4000_board_info_list.next, i = 0;
667 board_p != &me4000_board_info_list; board_p = board_p->next, i++) {
668 if (board_p == &board_info->list) {
669 board_info->board_count = i;
673 if (board_p == &me4000_board_info_list) {
675 "ME4000:init_board_info():Cannot get index of baord\n");
679 /* Init list head for analog output contexts */
680 INIT_LIST_HEAD(&board_info->ao_context_list);
682 /* Init spin locks */
683 spin_lock_init(&board_info->preload_lock);
684 spin_lock_init(&board_info->ai_ctrl_lock);
686 /* Get the serial number */
687 result = pci_read_config_dword(pci_dev_p, 0x2C, &board_info->serial_no);
688 if (result != PCIBIOS_SUCCESSFUL) {
690 "ME4000:init_board_info: Can't get serial_no\n");
693 PDEBUG("init_board_info():serial_no = 0x%x\n", board_info->serial_no);
695 /* Get the hardware revision */
697 pci_read_config_byte(pci_dev_p, 0x08, &board_info->hw_revision);
698 if (result != PCIBIOS_SUCCESSFUL) {
700 "ME4000:init_board_info():Can't get hw_revision\n");
703 PDEBUG("init_board_info():hw_revision = 0x%x\n",
704 board_info->hw_revision);
706 /* Get the vendor id */
707 board_info->vendor_id = pci_dev_p->vendor;
708 PDEBUG("init_board_info():vendor_id = 0x%x\n", board_info->vendor_id);
710 /* Get the device id */
711 board_info->device_id = pci_dev_p->device;
712 PDEBUG("init_board_info():device_id = 0x%x\n", board_info->device_id);
714 /* Get the pci device number */
715 board_info->pci_dev_no = PCI_FUNC(pci_dev_p->devfn);
716 PDEBUG("init_board_info():pci_func_no = 0x%x\n",
717 board_info->pci_func_no);
719 /* Get the pci slot number */
720 board_info->pci_dev_no = PCI_SLOT(pci_dev_p->devfn);
721 PDEBUG("init_board_info():pci_dev_no = 0x%x\n", board_info->pci_dev_no);
723 /* Get the pci bus number */
724 board_info->pci_bus_no = pci_dev_p->bus->number;
725 PDEBUG("init_board_info():pci_bus_no = 0x%x\n", board_info->pci_bus_no);
727 /* Get the irq assigned to the board */
728 board_info->irq = pci_dev_p->irq;
729 PDEBUG("init_board_info():irq = %d\n", board_info->irq);
734 static int alloc_ao_contexts(struct me4000_info *info)
738 struct me4000_ao_context *ao_context;
740 for (i = 0; i < info->board_p->ao.count; i++) {
741 ao_context = kzalloc(sizeof(struct me4000_ao_context),
745 "alloc_ao_contexts():Can't get memory for ao context\n");
746 release_ao_contexts(info);
750 spin_lock_init(&ao_context->use_lock);
751 spin_lock_init(&ao_context->int_lock);
752 ao_context->irq = info->irq;
753 init_waitqueue_head(&ao_context->wait_queue);
754 ao_context->board_info = info;
756 if (info->board_p->ao.fifo_count) {
757 /* Allocate circular buffer */
758 ao_context->circ_buf.buf =
759 kzalloc(ME4000_AO_BUFFER_SIZE, GFP_KERNEL);
760 if (!ao_context->circ_buf.buf) {
762 "alloc_ao_contexts():Can't get circular buffer\n");
763 release_ao_contexts(info);
767 /* Clear the circular buffer */
768 ao_context->circ_buf.head = 0;
769 ao_context->circ_buf.tail = 0;
774 ao_context->ctrl_reg =
775 info->me4000_regbase + ME4000_AO_00_CTRL_REG;
776 ao_context->status_reg =
777 info->me4000_regbase + ME4000_AO_00_STATUS_REG;
778 ao_context->fifo_reg =
779 info->me4000_regbase + ME4000_AO_00_FIFO_REG;
780 ao_context->single_reg =
781 info->me4000_regbase + ME4000_AO_00_SINGLE_REG;
782 ao_context->timer_reg =
783 info->me4000_regbase + ME4000_AO_00_TIMER_REG;
784 ao_context->irq_status_reg =
785 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
786 ao_context->preload_reg =
787 info->me4000_regbase + ME4000_AO_LOADSETREG_XX;
790 ao_context->ctrl_reg =
791 info->me4000_regbase + ME4000_AO_01_CTRL_REG;
792 ao_context->status_reg =
793 info->me4000_regbase + ME4000_AO_01_STATUS_REG;
794 ao_context->fifo_reg =
795 info->me4000_regbase + ME4000_AO_01_FIFO_REG;
796 ao_context->single_reg =
797 info->me4000_regbase + ME4000_AO_01_SINGLE_REG;
798 ao_context->timer_reg =
799 info->me4000_regbase + ME4000_AO_01_TIMER_REG;
800 ao_context->irq_status_reg =
801 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
802 ao_context->preload_reg =
803 info->me4000_regbase + ME4000_AO_LOADSETREG_XX;
806 ao_context->ctrl_reg =
807 info->me4000_regbase + ME4000_AO_02_CTRL_REG;
808 ao_context->status_reg =
809 info->me4000_regbase + ME4000_AO_02_STATUS_REG;
810 ao_context->fifo_reg =
811 info->me4000_regbase + ME4000_AO_02_FIFO_REG;
812 ao_context->single_reg =
813 info->me4000_regbase + ME4000_AO_02_SINGLE_REG;
814 ao_context->timer_reg =
815 info->me4000_regbase + ME4000_AO_02_TIMER_REG;
816 ao_context->irq_status_reg =
817 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
818 ao_context->preload_reg =
819 info->me4000_regbase + ME4000_AO_LOADSETREG_XX;
822 ao_context->ctrl_reg =
823 info->me4000_regbase + ME4000_AO_03_CTRL_REG;
824 ao_context->status_reg =
825 info->me4000_regbase + ME4000_AO_03_STATUS_REG;
826 ao_context->fifo_reg =
827 info->me4000_regbase + ME4000_AO_03_FIFO_REG;
828 ao_context->single_reg =
829 info->me4000_regbase + ME4000_AO_03_SINGLE_REG;
830 ao_context->timer_reg =
831 info->me4000_regbase + ME4000_AO_03_TIMER_REG;
832 ao_context->irq_status_reg =
833 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
834 ao_context->preload_reg =
835 info->me4000_regbase + ME4000_AO_LOADSETREG_XX;
841 if (info->board_p->ao.fifo_count) {
842 /* Request the interrupt line */
844 request_irq(ao_context->irq, me4000_ao_isr,
845 IRQF_DISABLED | IRQF_SHARED,
846 ME4000_NAME, ao_context);
849 "%s:Can't get interrupt line", __func__);
850 kfree(ao_context->circ_buf.buf);
852 release_ao_contexts(info);
857 list_add_tail(&ao_context->list, &info->ao_context_list);
858 ao_context->index = i;
864 static void release_ao_contexts(struct me4000_info *board_info)
866 struct list_head *dac_p;
867 struct me4000_ao_context *ao_context;
869 /* Clear analog output context list */
870 while (!list_empty(&board_info->ao_context_list)) {
871 dac_p = board_info->ao_context_list.next;
872 ao_context = list_entry(dac_p, struct me4000_ao_context, list);
873 free_irq(ao_context->irq, ao_context);
874 kfree(ao_context->circ_buf.buf);
880 static int alloc_ai_context(struct me4000_info *info)
882 struct me4000_ai_context *ai_context;
884 if (info->board_p->ai.count) {
885 ai_context = kzalloc(sizeof(struct me4000_ai_context),
889 "ME4000:alloc_ai_context():Can't get memory for ai context\n");
893 info->ai_context = ai_context;
895 spin_lock_init(&ai_context->use_lock);
896 spin_lock_init(&ai_context->int_lock);
897 ai_context->number = 0;
898 ai_context->irq = info->irq;
899 init_waitqueue_head(&ai_context->wait_queue);
900 ai_context->board_info = info;
902 ai_context->ctrl_reg =
903 info->me4000_regbase + ME4000_AI_CTRL_REG;
904 ai_context->status_reg =
905 info->me4000_regbase + ME4000_AI_STATUS_REG;
906 ai_context->channel_list_reg =
907 info->me4000_regbase + ME4000_AI_CHANNEL_LIST_REG;
908 ai_context->data_reg =
909 info->me4000_regbase + ME4000_AI_DATA_REG;
910 ai_context->chan_timer_reg =
911 info->me4000_regbase + ME4000_AI_CHAN_TIMER_REG;
912 ai_context->chan_pre_timer_reg =
913 info->me4000_regbase + ME4000_AI_CHAN_PRE_TIMER_REG;
914 ai_context->scan_timer_low_reg =
915 info->me4000_regbase + ME4000_AI_SCAN_TIMER_LOW_REG;
916 ai_context->scan_timer_high_reg =
917 info->me4000_regbase + ME4000_AI_SCAN_TIMER_HIGH_REG;
918 ai_context->scan_pre_timer_low_reg =
919 info->me4000_regbase + ME4000_AI_SCAN_PRE_TIMER_LOW_REG;
920 ai_context->scan_pre_timer_high_reg =
921 info->me4000_regbase + ME4000_AI_SCAN_PRE_TIMER_HIGH_REG;
922 ai_context->start_reg =
923 info->me4000_regbase + ME4000_AI_START_REG;
924 ai_context->irq_status_reg =
925 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
926 ai_context->sample_counter_reg =
927 info->me4000_regbase + ME4000_AI_SAMPLE_COUNTER_REG;
933 static int alloc_dio_context(struct me4000_info *info)
935 struct me4000_dio_context *dio_context;
937 if (info->board_p->dio.count) {
938 dio_context = kzalloc(sizeof(struct me4000_dio_context),
942 "ME4000:alloc_dio_context():Can't get memory for dio context\n");
946 info->dio_context = dio_context;
948 spin_lock_init(&dio_context->use_lock);
949 dio_context->board_info = info;
951 dio_context->dio_count = info->board_p->dio.count;
953 dio_context->dir_reg =
954 info->me4000_regbase + ME4000_DIO_DIR_REG;
955 dio_context->ctrl_reg =
956 info->me4000_regbase + ME4000_DIO_CTRL_REG;
957 dio_context->port_0_reg =
958 info->me4000_regbase + ME4000_DIO_PORT_0_REG;
959 dio_context->port_1_reg =
960 info->me4000_regbase + ME4000_DIO_PORT_1_REG;
961 dio_context->port_2_reg =
962 info->me4000_regbase + ME4000_DIO_PORT_2_REG;
963 dio_context->port_3_reg =
964 info->me4000_regbase + ME4000_DIO_PORT_3_REG;
970 static int alloc_cnt_context(struct me4000_info *info)
972 struct me4000_cnt_context *cnt_context;
974 if (info->board_p->cnt.count) {
975 cnt_context = kzalloc(sizeof(struct me4000_cnt_context),
979 "ME4000:alloc_cnt_context():Can't get memory for cnt context\n");
983 info->cnt_context = cnt_context;
985 spin_lock_init(&cnt_context->use_lock);
986 cnt_context->board_info = info;
988 cnt_context->ctrl_reg =
989 info->timer_regbase + ME4000_CNT_CTRL_REG;
990 cnt_context->counter_0_reg =
991 info->timer_regbase + ME4000_CNT_COUNTER_0_REG;
992 cnt_context->counter_1_reg =
993 info->timer_regbase + ME4000_CNT_COUNTER_1_REG;
994 cnt_context->counter_2_reg =
995 info->timer_regbase + ME4000_CNT_COUNTER_2_REG;
1001 static int alloc_ext_int_context(struct me4000_info *info)
1003 struct me4000_ext_int_context *ext_int_context;
1005 if (info->board_p->cnt.count) {
1007 kzalloc(sizeof(struct me4000_ext_int_context), GFP_KERNEL);
1008 if (!ext_int_context) {
1010 "ME4000:alloc_ext_int_context():Can't get memory for cnt context\n");
1014 info->ext_int_context = ext_int_context;
1016 spin_lock_init(&ext_int_context->use_lock);
1017 ext_int_context->board_info = info;
1019 ext_int_context->fasync_ptr = NULL;
1020 ext_int_context->irq = info->irq;
1022 ext_int_context->ctrl_reg =
1023 info->me4000_regbase + ME4000_AI_CTRL_REG;
1024 ext_int_context->irq_status_reg =
1025 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
1031 static int me4000_probe(struct pci_dev *dev, const struct pci_device_id *id)
1034 struct me4000_info *board_info;
1036 CALL_PDEBUG("me4000_probe() is executed\n");
1038 /* Allocate structure for board context */
1039 board_info = kzalloc(sizeof(struct me4000_info), GFP_KERNEL);
1042 "ME4000:Can't get memory for board info structure\n");
1047 /* Add to global linked list */
1048 list_add_tail(&board_info->list, &me4000_board_info_list);
1050 /* Get the PCI base registers */
1051 result = get_registers(dev, board_info);
1053 printk(KERN_ERR "%s:Cannot get registers\n", __func__);
1057 /* Enable the device */
1058 result = pci_enable_device(dev);
1060 printk(KERN_ERR "%s:Cannot enable PCI device\n", __func__);
1064 /* Request the PCI register regions */
1065 result = pci_request_regions(dev, ME4000_NAME);
1067 printk(KERN_ERR "%s:Cannot request I/O regions\n", __func__);
1071 /* Initialize board info */
1072 result = init_board_info(dev, board_info);
1074 printk(KERN_ERR "%s:Cannot init baord info\n", __func__);
1078 /* Download the xilinx firmware */
1079 result = me4000_xilinx_download(board_info);
1081 printk(KERN_ERR "%s:Can't download firmware\n", __func__);
1085 /* Make a hardware reset */
1086 result = me4000_reset_board(board_info);
1088 printk(KERN_ERR "%s :Can't reset board\n", __func__);
1092 /* Allocate analog output context structures */
1093 result = alloc_ao_contexts(board_info);
1095 printk(KERN_ERR "%s:Cannot allocate ao contexts\n", __func__);
1099 /* Allocate analog input context */
1100 result = alloc_ai_context(board_info);
1102 printk(KERN_ERR "%s:Cannot allocate ai context\n", __func__);
1106 /* Allocate digital I/O context */
1107 result = alloc_dio_context(board_info);
1109 printk(KERN_ERR "%s:Cannot allocate dio context\n", __func__);
1113 /* Allocate counter context */
1114 result = alloc_cnt_context(board_info);
1116 printk(KERN_ERR "%s:Cannot allocate cnt context\n", __func__);
1120 /* Allocate external interrupt context */
1121 result = alloc_ext_int_context(board_info);
1124 "%s:Cannot allocate ext_int context\n", __func__);
1131 kfree(board_info->cnt_context);
1134 kfree(board_info->dio_context);
1137 kfree(board_info->ai_context);
1140 release_ao_contexts(board_info);
1143 pci_release_regions(dev);
1146 list_del(&board_info->list);
1153 static int me4000_xilinx_download(struct me4000_info *info)
1158 unsigned char *firm;
1159 wait_queue_head_t queue;
1161 CALL_PDEBUG("me4000_xilinx_download() is executed\n");
1163 init_waitqueue_head(&queue);
1165 firm = (info->device_id == 0x4610) ? xilinx_firm_4610 : xilinx_firm;
1168 * Set PLX local interrupt 2 polarity to high.
1169 * Interrupt is thrown by init pin of xilinx.
1171 outl(0x10, info->plx_regbase + PLX_INTCSR);
1173 /* Set /CS and /WRITE of the Xilinx */
1174 value = inl(info->plx_regbase + PLX_ICR);
1176 outl(value, info->plx_regbase + PLX_ICR);
1178 /* Init Xilinx with CS1 */
1179 inb(info->program_regbase + 0xC8);
1181 /* Wait until /INIT pin is set */
1183 if (!inl(info->plx_regbase + PLX_INTCSR) & 0x20) {
1184 printk(KERN_ERR "%s:Can't init Xilinx\n", __func__);
1188 /* Reset /CS and /WRITE of the Xilinx */
1189 value = inl(info->plx_regbase + PLX_ICR);
1191 outl(value, info->plx_regbase + PLX_ICR);
1193 /* Download Xilinx firmware */
1194 size = (firm[0] << 24) + (firm[1] << 16) + (firm[2] << 8) + firm[3];
1197 for (idx = 0; idx < size; idx++) {
1198 outb(firm[16 + idx], info->program_regbase);
1202 /* Check if BUSY flag is low */
1203 if (inl(info->plx_regbase + PLX_ICR) & 0x20) {
1205 "%s:Xilinx is still busy (idx = %d)\n", __func__,
1211 PDEBUG("me4000_xilinx_download():%d bytes written\n", idx);
1213 /* If done flag is high download was successful */
1214 if (inl(info->plx_regbase + PLX_ICR) & 0x4) {
1215 PDEBUG("me4000_xilinx_download():Done flag is set\n");
1216 PDEBUG("me4000_xilinx_download():Download was successful\n");
1219 "ME4000:%s:DONE flag is not set\n", __func__);
1221 "ME4000:%s:Download not succesful\n", __func__);
1225 /* Set /CS and /WRITE */
1226 value = inl(info->plx_regbase + PLX_ICR);
1228 outl(value, info->plx_regbase + PLX_ICR);
1233 static int me4000_reset_board(struct me4000_info *info)
1237 CALL_PDEBUG("me4000_reset_board() is executed\n");
1239 /* Make a hardware reset */
1240 icr = me4000_inl(info->plx_regbase + PLX_ICR);
1242 me4000_outl(icr, info->plx_regbase + PLX_ICR);
1244 me4000_outl(icr, info->plx_regbase + PLX_ICR);
1246 /* Set both stop bits in the analog input control register */
1247 me4000_outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP | ME4000_AI_CTRL_BIT_STOP,
1248 info->me4000_regbase + ME4000_AI_CTRL_REG);
1250 /* Set both stop bits in the analog output control register */
1251 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1252 info->me4000_regbase + ME4000_AO_00_CTRL_REG);
1253 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1254 info->me4000_regbase + ME4000_AO_01_CTRL_REG);
1255 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1256 info->me4000_regbase + ME4000_AO_02_CTRL_REG);
1257 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1258 info->me4000_regbase + ME4000_AO_03_CTRL_REG);
1260 /* 0x8000 to the DACs means an output voltage of 0V */
1261 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_00_SINGLE_REG);
1262 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_01_SINGLE_REG);
1263 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_02_SINGLE_REG);
1264 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_03_SINGLE_REG);
1266 /* Enable interrupts on the PLX */
1267 me4000_outl(0x43, info->plx_regbase + PLX_INTCSR);
1269 /* Set the adustment register for AO demux */
1270 me4000_outl(ME4000_AO_DEMUX_ADJUST_VALUE,
1271 info->me4000_regbase + ME4000_AO_DEMUX_ADJUST_REG);
1273 /* Set digital I/O direction for port 0 to output on isolated versions */
1274 if (!(me4000_inl(info->me4000_regbase + ME4000_DIO_DIR_REG) & 0x1)) {
1275 me4000_outl(0x1, info->me4000_regbase + ME4000_DIO_CTRL_REG);
1281 static int me4000_open(struct inode *inode_p, struct file *file_p)
1283 int board, dev, mode;
1286 struct list_head *ptr;
1287 struct me4000_info *board_info = NULL;
1288 struct me4000_ao_context *ao_context = NULL;
1289 struct me4000_ai_context *ai_context = NULL;
1290 struct me4000_dio_context *dio_context = NULL;
1291 struct me4000_cnt_context *cnt_context = NULL;
1292 struct me4000_ext_int_context *ext_int_context = NULL;
1294 CALL_PDEBUG("me4000_open() is executed\n");
1297 if (MAJOR(inode_p->i_rdev) == me4000_ao_major_driver_no) {
1298 board = AO_BOARD(inode_p->i_rdev);
1299 dev = AO_PORT(inode_p->i_rdev);
1300 mode = AO_MODE(inode_p->i_rdev);
1302 PDEBUG("me4000_open():board = %d ao = %d mode = %d\n", board,
1305 /* Search for the board context */
1306 for (ptr = me4000_board_info_list.next, i = 0;
1307 ptr != &me4000_board_info_list; ptr = ptr->next, i++) {
1308 board_info = list_entry(ptr, struct me4000_info, list);
1313 if (ptr == &me4000_board_info_list) {
1315 "ME4000:me4000_open():Board %d not in device list\n",
1320 /* Search for the dac context */
1321 for (ptr = board_info->ao_context_list.next, i = 0;
1322 ptr != &board_info->ao_context_list;
1323 ptr = ptr->next, i++) {
1324 ao_context = list_entry(ptr, struct me4000_ao_context,
1330 if (ptr == &board_info->ao_context_list) {
1332 "ME4000:me4000_open():Device %d not in device list\n",
1337 /* Check if mode is valid */
1340 "ME4000:me4000_open():Mode is not valid\n");
1344 /* Check if mode is valid for this AO */
1345 if ((mode != ME4000_AO_CONV_MODE_SINGLE)
1346 && (dev >= board_info->board_p->ao.fifo_count)) {
1348 "ME4000:me4000_open():AO %d only in single mode available\n",
1353 /* Check if already opened */
1354 spin_lock(&ao_context->use_lock);
1355 if (ao_context->dac_in_use) {
1357 "ME4000:me4000_open():AO %d already in use\n",
1359 spin_unlock(&ao_context->use_lock);
1362 ao_context->dac_in_use = 1;
1363 spin_unlock(&ao_context->use_lock);
1365 ao_context->mode = mode;
1367 /* Hold the context in private data */
1368 file_p->private_data = ao_context;
1370 /* Set file operations pointer */
1371 file_p->f_op = me4000_ao_fops_array[mode];
1373 err = me4000_ao_prepare(ao_context);
1375 ao_context->dac_in_use = 0;
1380 else if (MAJOR(inode_p->i_rdev) == me4000_ai_major_driver_no) {
1381 board = AI_BOARD(inode_p->i_rdev);
1382 mode = AI_MODE(inode_p->i_rdev);
1384 PDEBUG("me4000_open():ai board = %d mode = %d\n", board, mode);
1386 /* Search for the board context */
1387 for (ptr = me4000_board_info_list.next, i = 0;
1388 ptr != &me4000_board_info_list; ptr = ptr->next, i++) {
1389 board_info = list_entry(ptr, struct me4000_info, list);
1394 if (ptr == &me4000_board_info_list) {
1396 "ME4000:me4000_open():Board %d not in device list\n",
1401 ai_context = board_info->ai_context;
1403 /* Check if mode is valid */
1406 "ME4000:me4000_open():Mode is not valid\n");
1410 /* Check if already opened */
1411 spin_lock(&ai_context->use_lock);
1412 if (ai_context->in_use) {
1414 "ME4000:me4000_open():AI already in use\n");
1415 spin_unlock(&ai_context->use_lock);
1418 ai_context->in_use = 1;
1419 spin_unlock(&ai_context->use_lock);
1421 ai_context->mode = mode;
1423 /* Hold the context in private data */
1424 file_p->private_data = ai_context;
1426 /* Set file operations pointer */
1427 file_p->f_op = me4000_ai_fops_array[mode];
1429 /* Prepare analog input */
1430 me4000_ai_prepare(ai_context);
1433 else if (MAJOR(inode_p->i_rdev) == me4000_dio_major_driver_no) {
1434 board = DIO_BOARD(inode_p->i_rdev);
1438 PDEBUG("me4000_open():board = %d\n", board);
1440 /* Search for the board context */
1441 for (ptr = me4000_board_info_list.next;
1442 ptr != &me4000_board_info_list; ptr = ptr->next) {
1443 board_info = list_entry(ptr, struct me4000_info, list);
1444 if (board_info->board_count == board)
1448 if (ptr == &me4000_board_info_list) {
1450 "ME4000:me4000_open():Board %d not in device list\n",
1455 /* Search for the dio context */
1456 dio_context = board_info->dio_context;
1458 /* Check if already opened */
1459 spin_lock(&dio_context->use_lock);
1460 if (dio_context->in_use) {
1462 "ME4000:me4000_open():DIO already in use\n");
1463 spin_unlock(&dio_context->use_lock);
1466 dio_context->in_use = 1;
1467 spin_unlock(&dio_context->use_lock);
1469 /* Hold the context in private data */
1470 file_p->private_data = dio_context;
1472 /* Set file operations pointer to single functions */
1473 file_p->f_op = &me4000_dio_fops;
1475 //me4000_dio_reset(dio_context);
1478 else if (MAJOR(inode_p->i_rdev) == me4000_cnt_major_driver_no) {
1479 board = CNT_BOARD(inode_p->i_rdev);
1483 PDEBUG("me4000_open():board = %d\n", board);
1485 /* Search for the board context */
1486 for (ptr = me4000_board_info_list.next;
1487 ptr != &me4000_board_info_list; ptr = ptr->next) {
1488 board_info = list_entry(ptr, struct me4000_info, list);
1489 if (board_info->board_count == board)
1493 if (ptr == &me4000_board_info_list) {
1495 "ME4000:me4000_open():Board %d not in device list\n",
1500 /* Get the cnt context */
1501 cnt_context = board_info->cnt_context;
1503 /* Check if already opened */
1504 spin_lock(&cnt_context->use_lock);
1505 if (cnt_context->in_use) {
1507 "ME4000:me4000_open():CNT already in use\n");
1508 spin_unlock(&cnt_context->use_lock);
1511 cnt_context->in_use = 1;
1512 spin_unlock(&cnt_context->use_lock);
1514 /* Hold the context in private data */
1515 file_p->private_data = cnt_context;
1517 /* Set file operations pointer to single functions */
1518 file_p->f_op = &me4000_cnt_fops;
1520 /* External Interrupt */
1521 else if (MAJOR(inode_p->i_rdev) == me4000_ext_int_major_driver_no) {
1522 board = EXT_INT_BOARD(inode_p->i_rdev);
1526 PDEBUG("me4000_open():board = %d\n", board);
1528 /* Search for the board context */
1529 for (ptr = me4000_board_info_list.next;
1530 ptr != &me4000_board_info_list; ptr = ptr->next) {
1531 board_info = list_entry(ptr, struct me4000_info, list);
1532 if (board_info->board_count == board)
1536 if (ptr == &me4000_board_info_list) {
1538 "ME4000:me4000_open():Board %d not in device list\n",
1543 /* Get the external interrupt context */
1544 ext_int_context = board_info->ext_int_context;
1546 /* Check if already opened */
1547 spin_lock(&cnt_context->use_lock);
1548 if (ext_int_context->in_use) {
1550 "ME4000:me4000_open():External interrupt already in use\n");
1551 spin_unlock(&ext_int_context->use_lock);
1554 ext_int_context->in_use = 1;
1555 spin_unlock(&ext_int_context->use_lock);
1557 /* Hold the context in private data */
1558 file_p->private_data = ext_int_context;
1560 /* Set file operations pointer to single functions */
1561 file_p->f_op = &me4000_ext_int_fops;
1563 /* Request the interrupt line */
1565 request_irq(ext_int_context->irq, me4000_ext_int_isr,
1566 IRQF_DISABLED | IRQF_SHARED, ME4000_NAME,
1570 "ME4000:me4000_open():Can't get interrupt line");
1571 ext_int_context->in_use = 0;
1575 /* Reset the counter */
1576 me4000_ext_int_disable(ext_int_context);
1578 printk(KERN_ERR "ME4000:me4000_open():Major number unknown\n");
1585 static int me4000_release(struct inode *inode_p, struct file *file_p)
1587 struct me4000_ao_context *ao_context;
1588 struct me4000_ai_context *ai_context;
1589 struct me4000_dio_context *dio_context;
1590 struct me4000_cnt_context *cnt_context;
1591 struct me4000_ext_int_context *ext_int_context;
1593 CALL_PDEBUG("me4000_release() is executed\n");
1595 if (MAJOR(inode_p->i_rdev) == me4000_ao_major_driver_no) {
1596 ao_context = file_p->private_data;
1598 /* Mark DAC as unused */
1599 ao_context->dac_in_use = 0;
1600 } else if (MAJOR(inode_p->i_rdev) == me4000_ai_major_driver_no) {
1601 ai_context = file_p->private_data;
1603 /* Reset the analog input */
1604 me4000_ai_reset(ai_context);
1606 /* Free the interrupt and the circular buffer */
1607 if (ai_context->mode) {
1608 free_irq(ai_context->irq, ai_context);
1609 kfree(ai_context->circ_buf.buf);
1610 ai_context->circ_buf.buf = NULL;
1611 ai_context->circ_buf.head = 0;
1612 ai_context->circ_buf.tail = 0;
1615 /* Mark AI as unused */
1616 ai_context->in_use = 0;
1617 } else if (MAJOR(inode_p->i_rdev) == me4000_dio_major_driver_no) {
1618 dio_context = file_p->private_data;
1620 /* Mark digital I/O as unused */
1621 dio_context->in_use = 0;
1622 } else if (MAJOR(inode_p->i_rdev) == me4000_cnt_major_driver_no) {
1623 cnt_context = file_p->private_data;
1625 /* Mark counters as unused */
1626 cnt_context->in_use = 0;
1627 } else if (MAJOR(inode_p->i_rdev) == me4000_ext_int_major_driver_no) {
1628 ext_int_context = file_p->private_data;
1630 /* Disable the externel interrupt */
1631 me4000_ext_int_disable(ext_int_context);
1633 free_irq(ext_int_context->irq, ext_int_context);
1635 /* Mark as unused */
1636 ext_int_context->in_use = 0;
1639 "ME4000:me4000_release():Major number unknown\n");
1646 /*------------------------------- Analog output stuff --------------------------------------*/
1648 static int me4000_ao_prepare(struct me4000_ao_context *ao_context)
1650 unsigned long flags;
1652 CALL_PDEBUG("me4000_ao_prepare() is executed\n");
1654 if (ao_context->mode == ME4000_AO_CONV_MODE_CONTINUOUS) {
1655 /* Only do anything if not already in the correct mode */
1656 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1657 if ((mode & ME4000_AO_CONV_MODE_CONTINUOUS)
1658 && (mode & ME4000_AO_CTRL_BIT_ENABLE_FIFO)) {
1662 /* Stop any conversion */
1663 me4000_ao_immediate_stop(ao_context);
1665 /* Set the control register to default state */
1666 spin_lock_irqsave(&ao_context->int_lock, flags);
1667 me4000_outl(ME4000_AO_CONV_MODE_CONTINUOUS |
1668 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1669 ME4000_AO_CTRL_BIT_STOP |
1670 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1671 ao_context->ctrl_reg);
1672 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1674 /* Set to fastest sample rate */
1675 me4000_outl(65, ao_context->timer_reg);
1676 } else if (ao_context->mode == ME4000_AO_CONV_MODE_WRAPAROUND) {
1677 /* Only do anything if not already in the correct mode */
1678 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1679 if ((mode & ME4000_AO_CONV_MODE_WRAPAROUND)
1680 && (mode & ME4000_AO_CTRL_BIT_ENABLE_FIFO)) {
1684 /* Stop any conversion */
1685 me4000_ao_immediate_stop(ao_context);
1687 /* Set the control register to default state */
1688 spin_lock_irqsave(&ao_context->int_lock, flags);
1689 me4000_outl(ME4000_AO_CONV_MODE_WRAPAROUND |
1690 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1691 ME4000_AO_CTRL_BIT_STOP |
1692 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1693 ao_context->ctrl_reg);
1694 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1696 /* Set to fastest sample rate */
1697 me4000_outl(65, ao_context->timer_reg);
1698 } else if (ao_context->mode == ME4000_AO_CONV_MODE_SINGLE) {
1699 /* Only do anything if not already in the correct mode */
1700 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1703 (ME4000_AO_CONV_MODE_WRAPAROUND |
1704 ME4000_AO_CONV_MODE_CONTINUOUS))) {
1708 /* Stop any conversion */
1709 me4000_ao_immediate_stop(ao_context);
1711 /* Clear the control register */
1712 spin_lock_irqsave(&ao_context->int_lock, flags);
1713 me4000_outl(0x0, ao_context->ctrl_reg);
1714 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1716 /* Set voltage to 0V */
1717 me4000_outl(0x8000, ao_context->single_reg);
1720 "ME4000:me4000_ao_prepare():Invalid mode specified\n");
1727 static int me4000_ao_reset(struct me4000_ao_context *ao_context)
1730 wait_queue_head_t queue;
1731 unsigned long flags;
1733 CALL_PDEBUG("me4000_ao_reset() is executed\n");
1735 init_waitqueue_head(&queue);
1737 if (ao_context->mode == ME4000_AO_CONV_MODE_WRAPAROUND) {
1739 * First stop conversion of the DAC before reconfigure.
1740 * This is essantial, cause of the state machine.
1741 * If not stopped before configuring mode, it could
1742 * walk in a undefined state.
1744 tmp = me4000_inl(ao_context->ctrl_reg);
1745 tmp |= ME4000_AO_CTRL_BIT_IMMEDIATE_STOP;
1746 me4000_outl(tmp, ao_context->ctrl_reg);
1748 wait_event_timeout(queue,
1749 (inl(ao_context->status_reg) &
1750 ME4000_AO_STATUS_BIT_FSM) == 0,
1753 /* Set to transparent mode */
1754 me4000_ao_simultaneous_disable(ao_context);
1756 /* Set to single mode in order to set default voltage */
1757 me4000_outl(0x0, ao_context->ctrl_reg);
1759 /* Set voltage to 0V */
1760 me4000_outl(0x8000, ao_context->single_reg);
1762 /* Set to fastest sample rate */
1763 me4000_outl(65, ao_context->timer_reg);
1765 /* Set the original mode and enable FIFO */
1766 me4000_outl(ME4000_AO_CONV_MODE_WRAPAROUND |
1767 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1768 ME4000_AO_CTRL_BIT_STOP |
1769 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1770 ao_context->ctrl_reg);
1771 } else if (ao_context->mode == ME4000_AO_CONV_MODE_CONTINUOUS) {
1773 * First stop conversion of the DAC before reconfigure.
1774 * This is essantial, cause of the state machine.
1775 * If not stopped before configuring mode, it could
1776 * walk in a undefined state.
1778 spin_lock_irqsave(&ao_context->int_lock, flags);
1779 tmp = me4000_inl(ao_context->ctrl_reg);
1780 tmp |= ME4000_AO_CTRL_BIT_STOP;
1781 me4000_outl(tmp, ao_context->ctrl_reg);
1782 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1784 wait_event_timeout(queue,
1785 (inl(ao_context->status_reg) &
1786 ME4000_AO_STATUS_BIT_FSM) == 0,
1789 /* Clear the circular buffer */
1790 ao_context->circ_buf.head = 0;
1791 ao_context->circ_buf.tail = 0;
1793 /* Set to transparent mode */
1794 me4000_ao_simultaneous_disable(ao_context);
1796 /* Set to single mode in order to set default voltage */
1797 spin_lock_irqsave(&ao_context->int_lock, flags);
1798 tmp = me4000_inl(ao_context->ctrl_reg);
1799 me4000_outl(0x0, ao_context->ctrl_reg);
1801 /* Set voltage to 0V */
1802 me4000_outl(0x8000, ao_context->single_reg);
1804 /* Set to fastest sample rate */
1805 me4000_outl(65, ao_context->timer_reg);
1807 /* Set the original mode and enable FIFO */
1808 me4000_outl(ME4000_AO_CONV_MODE_CONTINUOUS |
1809 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1810 ME4000_AO_CTRL_BIT_STOP |
1811 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1812 ao_context->ctrl_reg);
1813 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1815 /* Set to transparent mode */
1816 me4000_ao_simultaneous_disable(ao_context);
1818 /* Set voltage to 0V */
1819 me4000_outl(0x8000, ao_context->single_reg);
1825 static ssize_t me4000_ao_write_sing(struct file *filep, const char *buff,
1826 size_t cnt, loff_t *offp)
1828 struct me4000_ao_context *ao_context = filep->private_data;
1830 const u16 *buffer = (const u16 *)buff;
1832 CALL_PDEBUG("me4000_ao_write_sing() is executed\n");
1836 "%s:Write count is not 2\n", __func__);
1840 if (get_user(value, buffer)) {
1842 "%s:Cannot copy data from user\n", __func__);
1846 me4000_outl(value, ao_context->single_reg);
1851 static ssize_t me4000_ao_write_wrap(struct file *filep, const char *buff,
1852 size_t cnt, loff_t *offp)
1854 struct me4000_ao_context *ao_context = filep->private_data;
1858 const u16 *buffer = (const u16 *)buff;
1859 size_t count = cnt / 2;
1861 CALL_PDEBUG("me4000_ao_write_wrap() is executed\n");
1863 /* Check if a conversion is already running */
1864 if (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
1866 "%s:There is already a conversion running\n", __func__);
1870 if (count > ME4000_AO_FIFO_COUNT) {
1872 "%s:Can't load more than %d values\n", __func__,
1873 ME4000_AO_FIFO_COUNT);
1877 /* Reset the FIFO */
1878 tmp = inl(ao_context->ctrl_reg);
1879 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_FIFO;
1880 outl(tmp, ao_context->ctrl_reg);
1881 tmp |= ME4000_AO_CTRL_BIT_ENABLE_FIFO;
1882 outl(tmp, ao_context->ctrl_reg);
1884 for (i = 0; i < count; i++) {
1885 if (get_user(value, buffer + i)) {
1887 "%s:Cannot copy data from user\n", __func__);
1890 if (((ao_context->fifo_reg & 0xFF) == ME4000_AO_01_FIFO_REG)
1891 || ((ao_context->fifo_reg & 0xFF) == ME4000_AO_03_FIFO_REG))
1892 value = value << 16;
1893 outl(value, ao_context->fifo_reg);
1895 CALL_PDEBUG("me4000_ao_write_wrap() is leaved with %d\n", i * 2);
1900 static ssize_t me4000_ao_write_cont(struct file *filep, const char *buff,
1901 size_t cnt, loff_t *offp)
1903 struct me4000_ao_context *ao_context = filep->private_data;
1904 const u16 *buffer = (const u16 *)buff;
1905 size_t count = cnt / 2;
1906 unsigned long flags;
1914 wait_queue_head_t queue;
1916 CALL_PDEBUG("me4000_ao_write_cont() is executed\n");
1918 init_waitqueue_head(&queue);
1922 PDEBUG("me4000_ao_write_cont():Count is 0\n");
1926 if (filep->f_flags & O_APPEND) {
1927 PDEBUG("me4000_ao_write_cont():Append data to data stream\n");
1929 if (filep->f_flags & O_NONBLOCK) {
1930 if (ao_context->pipe_flag) {
1932 "ME4000:me4000_ao_write_cont():Broken pipe in nonblocking write\n");
1935 c = me4000_space_to_end(ao_context->circ_buf,
1936 ME4000_AO_BUFFER_COUNT);
1939 ("me4000_ao_write_cont():Returning from nonblocking write\n");
1943 wait_event_interruptible(ao_context->wait_queue,
1946 (ao_context->circ_buf,
1947 ME4000_AO_BUFFER_COUNT)));
1948 if (ao_context->pipe_flag) {
1950 "me4000_ao_write_cont():Broken pipe in blocking write\n");
1953 if (signal_pending(current)) {
1955 "me4000_ao_write_cont():Wait for free buffer interrupted from signal\n");
1960 PDEBUG("me4000_ao_write_cont():Space to end = %d\n", c);
1962 /* Only able to write size of free buffer or size of count */
1967 k -= copy_from_user(ao_context->circ_buf.buf +
1968 ao_context->circ_buf.head, buffer,
1972 ("me4000_ao_write_cont():Copy %d values from user space\n",
1978 ao_context->circ_buf.head =
1979 (ao_context->circ_buf.head +
1980 c) & (ME4000_AO_BUFFER_COUNT - 1);
1985 /* Values are now available so enable interrupts */
1986 spin_lock_irqsave(&ao_context->int_lock, flags);
1987 if (me4000_buf_count
1988 (ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
1989 tmp = me4000_inl(ao_context->ctrl_reg);
1990 tmp |= ME4000_AO_CTRL_BIT_ENABLE_IRQ;
1991 me4000_outl(tmp, ao_context->ctrl_reg);
1993 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1996 /* Wait until the state machine is stopped if O_SYNC is set */
1997 if (filep->f_flags & O_SYNC) {
1998 while (inl(ao_context->status_reg) &
1999 ME4000_AO_STATUS_BIT_FSM) {
2000 interruptible_sleep_on_timeout(&queue, 1);
2001 if (ao_context->pipe_flag) {
2003 ("me4000_ao_write_cont():Broken pipe detected after sync\n");
2006 if (signal_pending(current)) {
2008 "me4000_ao_write_cont():Wait on state machine after sync interrupted\n");
2014 PDEBUG("me4000_ao_write_cont():Preload DAC FIFO\n");
2015 if ((me4000_inl(ao_context->status_reg) &
2016 ME4000_AO_STATUS_BIT_FSM)) {
2018 "me4000_ao_write_cont():Can't Preload DAC FIFO while conversion is running\n");
2022 /* Clear the FIFO */
2023 spin_lock_irqsave(&ao_context->int_lock, flags);
2024 tmp = me4000_inl(ao_context->ctrl_reg);
2026 ~(ME4000_AO_CTRL_BIT_ENABLE_FIFO |
2027 ME4000_AO_CTRL_BIT_ENABLE_IRQ);
2028 me4000_outl(tmp, ao_context->ctrl_reg);
2029 tmp |= ME4000_AO_CTRL_BIT_ENABLE_FIFO;
2030 me4000_outl(tmp, ao_context->ctrl_reg);
2031 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2033 /* Clear the circular buffer */
2034 ao_context->circ_buf.head = 0;
2035 ao_context->circ_buf.tail = 0;
2037 /* Reset the broken pipe flag */
2038 ao_context->pipe_flag = 0;
2040 /* Only able to write size of fifo or count */
2041 c = ME4000_AO_FIFO_COUNT;
2046 ("me4000_ao_write_cont():Write %d values to DAC on 0x%lX\n",
2047 c, ao_context->fifo_reg);
2049 /* Write values to the fifo */
2050 for (i = 0; i < c; i++) {
2051 if (get_user(svalue, buffer))
2054 if (((ao_context->fifo_reg & 0xFF) ==
2055 ME4000_AO_01_FIFO_REG)
2056 || ((ao_context->fifo_reg & 0xFF) ==
2057 ME4000_AO_03_FIFO_REG)) {
2058 lvalue = ((u32) svalue) << 16;
2060 lvalue = (u32) svalue;
2062 outl(lvalue, ao_context->fifo_reg);
2069 /* Get free buffer */
2070 c = me4000_space_to_end(ao_context->circ_buf,
2071 ME4000_AO_BUFFER_COUNT);
2076 /* Only able to write size of free buffer or size of count */
2080 /* If count = 0 return to user */
2083 ("me4000_ao_write_cont():Count reached 0\n");
2088 k -= copy_from_user(ao_context->circ_buf.buf +
2089 ao_context->circ_buf.head, buffer,
2093 ("me4000_ao_write_cont():Wrote %d values to buffer\n",
2099 ao_context->circ_buf.head =
2100 (ao_context->circ_buf.head +
2101 c) & (ME4000_AO_BUFFER_COUNT - 1);
2106 /* If values in the buffer are available so enable interrupts */
2107 spin_lock_irqsave(&ao_context->int_lock, flags);
2108 if (me4000_buf_count
2109 (ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
2111 ("me4000_ao_write_cont():Enable Interrupts\n");
2112 tmp = me4000_inl(ao_context->ctrl_reg);
2113 tmp |= ME4000_AO_CTRL_BIT_ENABLE_IRQ;
2114 me4000_outl(tmp, ao_context->ctrl_reg);
2116 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2120 if (filep->f_flags & O_NONBLOCK) {
2121 return (ret == 0) ? -EAGAIN : 2 * ret;
2127 static unsigned int me4000_ao_poll_cont(struct file *file_p, poll_table *wait)
2129 struct me4000_ao_context *ao_context;
2130 unsigned long mask = 0;
2132 CALL_PDEBUG("me4000_ao_poll_cont() is executed\n");
2134 ao_context = file_p->private_data;
2136 poll_wait(file_p, &ao_context->wait_queue, wait);
2138 /* Get free buffer */
2139 if (me4000_space_to_end(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT))
2140 mask |= POLLOUT | POLLWRNORM;
2142 CALL_PDEBUG("me4000_ao_poll_cont():Return mask %lX\n", mask);
2147 static int me4000_ao_fsync_cont(struct file *file_p, struct dentry *dentry_p,
2150 struct me4000_ao_context *ao_context;
2151 wait_queue_head_t queue;
2153 CALL_PDEBUG("me4000_ao_fsync_cont() is executed\n");
2155 ao_context = file_p->private_data;
2156 init_waitqueue_head(&queue);
2158 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2159 interruptible_sleep_on_timeout(&queue, 1);
2160 wait_event_interruptible_timeout(queue,
2161 !(inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM),
2163 if (ao_context->pipe_flag) {
2165 "%s:Broken pipe detected\n", __func__);
2169 if (signal_pending(current)) {
2171 "%s:Wait on state machine interrupted\n",
2180 static int me4000_ao_ioctl_sing(struct inode *inode_p, struct file *file_p,
2181 unsigned int service, unsigned long arg)
2183 struct me4000_ao_context *ao_context;
2185 CALL_PDEBUG("me4000_ao_ioctl_sing() is executed\n");
2187 ao_context = file_p->private_data;
2189 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2191 PDEBUG("me4000_ao_ioctl_sing():Wrong magic number\n");
2195 case ME4000_AO_EX_TRIG_SETUP:
2196 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2197 case ME4000_AO_EX_TRIG_ENABLE:
2198 return me4000_ao_ex_trig_enable(ao_context);
2199 case ME4000_AO_EX_TRIG_DISABLE:
2200 return me4000_ao_ex_trig_disable(ao_context);
2201 case ME4000_AO_PRELOAD:
2202 return me4000_ao_preload(ao_context);
2203 case ME4000_AO_PRELOAD_UPDATE:
2204 return me4000_ao_preload_update(ao_context);
2205 case ME4000_GET_USER_INFO:
2206 return me4000_get_user_info((struct me4000_user_info *)arg,
2207 ao_context->board_info);
2208 case ME4000_AO_SIMULTANEOUS_EX_TRIG:
2209 return me4000_ao_simultaneous_ex_trig(ao_context);
2210 case ME4000_AO_SIMULTANEOUS_SW:
2211 return me4000_ao_simultaneous_sw(ao_context);
2212 case ME4000_AO_SIMULTANEOUS_DISABLE:
2213 return me4000_ao_simultaneous_disable(ao_context);
2214 case ME4000_AO_SIMULTANEOUS_UPDATE:
2216 me4000_ao_simultaneous_update(
2217 (struct me4000_ao_channel_list *)arg,
2219 case ME4000_AO_EX_TRIG_TIMEOUT:
2220 return me4000_ao_ex_trig_timeout((unsigned long *)arg,
2222 case ME4000_AO_DISABLE_DO:
2223 return me4000_ao_disable_do(ao_context);
2226 "me4000_ao_ioctl_sing():Service number invalid\n");
2233 static int me4000_ao_ioctl_wrap(struct inode *inode_p, struct file *file_p,
2234 unsigned int service, unsigned long arg)
2236 struct me4000_ao_context *ao_context;
2238 CALL_PDEBUG("me4000_ao_ioctl_wrap() is executed\n");
2240 ao_context = file_p->private_data;
2242 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2244 PDEBUG("me4000_ao_ioctl_wrap():Wrong magic number\n");
2248 case ME4000_AO_START:
2249 return me4000_ao_start((unsigned long *)arg, ao_context);
2250 case ME4000_AO_STOP:
2251 return me4000_ao_stop(ao_context);
2252 case ME4000_AO_IMMEDIATE_STOP:
2253 return me4000_ao_immediate_stop(ao_context);
2254 case ME4000_AO_RESET:
2255 return me4000_ao_reset(ao_context);
2256 case ME4000_AO_TIMER_SET_DIVISOR:
2257 return me4000_ao_timer_set_divisor((u32 *) arg, ao_context);
2258 case ME4000_AO_EX_TRIG_SETUP:
2259 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2260 case ME4000_AO_EX_TRIG_ENABLE:
2261 return me4000_ao_ex_trig_enable(ao_context);
2262 case ME4000_AO_EX_TRIG_DISABLE:
2263 return me4000_ao_ex_trig_disable(ao_context);
2264 case ME4000_GET_USER_INFO:
2265 return me4000_get_user_info((struct me4000_user_info *)arg,
2266 ao_context->board_info);
2267 case ME4000_AO_FSM_STATE:
2268 return me4000_ao_fsm_state((int *)arg, ao_context);
2269 case ME4000_AO_ENABLE_DO:
2270 return me4000_ao_enable_do(ao_context);
2271 case ME4000_AO_DISABLE_DO:
2272 return me4000_ao_disable_do(ao_context);
2273 case ME4000_AO_SYNCHRONOUS_EX_TRIG:
2274 return me4000_ao_synchronous_ex_trig(ao_context);
2275 case ME4000_AO_SYNCHRONOUS_SW:
2276 return me4000_ao_synchronous_sw(ao_context);
2277 case ME4000_AO_SYNCHRONOUS_DISABLE:
2278 return me4000_ao_synchronous_disable(ao_context);
2285 static int me4000_ao_ioctl_cont(struct inode *inode_p, struct file *file_p,
2286 unsigned int service, unsigned long arg)
2288 struct me4000_ao_context *ao_context;
2290 CALL_PDEBUG("me4000_ao_ioctl_cont() is executed\n");
2292 ao_context = file_p->private_data;
2294 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2296 PDEBUG("me4000_ao_ioctl_cont():Wrong magic number\n");
2300 case ME4000_AO_START:
2301 return me4000_ao_start((unsigned long *)arg, ao_context);
2302 case ME4000_AO_STOP:
2303 return me4000_ao_stop(ao_context);
2304 case ME4000_AO_IMMEDIATE_STOP:
2305 return me4000_ao_immediate_stop(ao_context);
2306 case ME4000_AO_RESET:
2307 return me4000_ao_reset(ao_context);
2308 case ME4000_AO_TIMER_SET_DIVISOR:
2309 return me4000_ao_timer_set_divisor((u32 *) arg, ao_context);
2310 case ME4000_AO_EX_TRIG_SETUP:
2311 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2312 case ME4000_AO_EX_TRIG_ENABLE:
2313 return me4000_ao_ex_trig_enable(ao_context);
2314 case ME4000_AO_EX_TRIG_DISABLE:
2315 return me4000_ao_ex_trig_disable(ao_context);
2316 case ME4000_AO_ENABLE_DO:
2317 return me4000_ao_enable_do(ao_context);
2318 case ME4000_AO_DISABLE_DO:
2319 return me4000_ao_disable_do(ao_context);
2320 case ME4000_AO_FSM_STATE:
2321 return me4000_ao_fsm_state((int *)arg, ao_context);
2322 case ME4000_GET_USER_INFO:
2323 return me4000_get_user_info((struct me4000_user_info *)arg,
2324 ao_context->board_info);
2325 case ME4000_AO_SYNCHRONOUS_EX_TRIG:
2326 return me4000_ao_synchronous_ex_trig(ao_context);
2327 case ME4000_AO_SYNCHRONOUS_SW:
2328 return me4000_ao_synchronous_sw(ao_context);
2329 case ME4000_AO_SYNCHRONOUS_DISABLE:
2330 return me4000_ao_synchronous_disable(ao_context);
2331 case ME4000_AO_GET_FREE_BUFFER:
2332 return me4000_ao_get_free_buffer((unsigned long *)arg,
2340 static int me4000_ao_start(unsigned long *arg,
2341 struct me4000_ao_context *ao_context)
2344 wait_queue_head_t queue;
2346 unsigned long timeout;
2347 unsigned long flags;
2349 CALL_PDEBUG("me4000_ao_start() is executed\n");
2351 if (get_user(timeout, arg)) {
2353 "me4000_ao_start():Cannot copy data from user\n");
2357 init_waitqueue_head(&queue);
2359 spin_lock_irqsave(&ao_context->int_lock, flags);
2360 tmp = inl(ao_context->ctrl_reg);
2361 tmp &= ~(ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2362 me4000_outl(tmp, ao_context->ctrl_reg);
2363 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2365 if ((tmp & ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG)) {
2369 (inl(ao_context->status_reg) &
2370 ME4000_AO_STATUS_BIT_FSM)) {
2371 interruptible_sleep_on_timeout(&queue, 1);
2372 if (signal_pending(current)) {
2374 "ME4000:me4000_ao_start():Wait on start of state machine interrupted\n");
2377 if (((jiffies - ref) > (timeout * HZ / USER_HZ))) { // 2.6 has diffrent definitions for HZ in user and kernel space
2379 "ME4000:me4000_ao_start():Timeout reached\n");
2385 me4000_outl(0x8000, ao_context->single_reg);
2391 static int me4000_ao_stop(struct me4000_ao_context *ao_context)
2394 wait_queue_head_t queue;
2395 unsigned long flags;
2397 init_waitqueue_head(&queue);
2399 CALL_PDEBUG("me4000_ao_stop() is executed\n");
2401 /* Set the stop bit */
2402 spin_lock_irqsave(&ao_context->int_lock, flags);
2403 tmp = inl(ao_context->ctrl_reg);
2404 tmp |= ME4000_AO_CTRL_BIT_STOP;
2405 me4000_outl(tmp, ao_context->ctrl_reg);
2406 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2408 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2409 interruptible_sleep_on_timeout(&queue, 1);
2410 if (signal_pending(current)) {
2412 "me4000_ao_stop():Wait on state machine after stop interrupted\n");
2417 /* Clear the stop bit */
2418 //tmp &= ~ME4000_AO_CTRL_BIT_STOP;
2419 //me4000_outl(tmp, ao_context->ctrl_reg);
2424 static int me4000_ao_immediate_stop(struct me4000_ao_context *ao_context)
2427 wait_queue_head_t queue;
2428 unsigned long flags;
2430 init_waitqueue_head(&queue);
2432 CALL_PDEBUG("me4000_ao_immediate_stop() is executed\n");
2434 spin_lock_irqsave(&ao_context->int_lock, flags);
2435 tmp = inl(ao_context->ctrl_reg);
2436 tmp |= ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP;
2437 me4000_outl(tmp, ao_context->ctrl_reg);
2438 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2440 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2441 interruptible_sleep_on_timeout(&queue, 1);
2442 if (signal_pending(current)) {
2444 "me4000_ao_immediate_stop():Wait on state machine after stop interrupted\n");
2449 /* Clear the stop bits */
2450 //tmp &= ~(ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2451 //me4000_outl(tmp, ao_context->ctrl_reg);
2456 static int me4000_ao_timer_set_divisor(u32 *arg,
2457 struct me4000_ao_context *ao_context)
2462 CALL_PDEBUG("me4000_ao_timer set_divisor() is executed\n");
2464 if (get_user(divisor, arg))
2467 /* Check if the state machine is stopped */
2468 tmp = me4000_inl(ao_context->status_reg);
2469 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2471 "me4000_ao_timer_set_divisor():Can't set timer while DAC is running\n");
2475 PDEBUG("me4000_ao_timer set_divisor():Divisor from user = %d\n",
2478 /* Check if the divisor is right. ME4000_AO_MIN_TICKS is the lowest */
2479 if (divisor < ME4000_AO_MIN_TICKS) {
2481 "ME4000:me4000_ao_timer set_divisor():Divisor to low\n");
2485 /* Fix bug in Firmware */
2488 PDEBUG("me4000_ao_timer set_divisor():Divisor to HW = %d\n", divisor);
2490 /* Write the divisor */
2491 me4000_outl(divisor, ao_context->timer_reg);
2496 static int me4000_ao_ex_trig_set_edge(int *arg,
2497 struct me4000_ao_context *ao_context)
2501 unsigned long flags;
2503 CALL_PDEBUG("me4000_ao_ex_trig_set_edge() is executed\n");
2505 if (get_user(mode, arg))
2508 /* Check if the state machine is stopped */
2509 tmp = me4000_inl(ao_context->status_reg);
2510 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2512 "me4000_ao_ex_trig_set_edge():Can't set trigger while DAC is running\n");
2516 if (mode == ME4000_AO_TRIGGER_EXT_EDGE_RISING) {
2517 spin_lock_irqsave(&ao_context->int_lock, flags);
2518 tmp = me4000_inl(ao_context->ctrl_reg);
2520 ~(ME4000_AO_CTRL_BIT_EX_TRIG_EDGE |
2521 ME4000_AO_CTRL_BIT_EX_TRIG_BOTH);
2522 me4000_outl(tmp, ao_context->ctrl_reg);
2523 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2524 } else if (mode == ME4000_AO_TRIGGER_EXT_EDGE_FALLING) {
2525 spin_lock_irqsave(&ao_context->int_lock, flags);
2526 tmp = me4000_inl(ao_context->ctrl_reg);
2527 tmp &= ~ME4000_AO_CTRL_BIT_EX_TRIG_BOTH;
2528 tmp |= ME4000_AO_CTRL_BIT_EX_TRIG_EDGE;
2529 me4000_outl(tmp, ao_context->ctrl_reg);
2530 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2531 } else if (mode == ME4000_AO_TRIGGER_EXT_EDGE_BOTH) {
2532 spin_lock_irqsave(&ao_context->int_lock, flags);
2533 tmp = me4000_inl(ao_context->ctrl_reg);
2535 ME4000_AO_CTRL_BIT_EX_TRIG_EDGE |
2536 ME4000_AO_CTRL_BIT_EX_TRIG_BOTH;
2537 me4000_outl(tmp, ao_context->ctrl_reg);
2538 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2541 "me4000_ao_ex_trig_set_edge():Invalid trigger mode\n");
2548 static int me4000_ao_ex_trig_enable(struct me4000_ao_context *ao_context)
2551 unsigned long flags;
2553 CALL_PDEBUG("me4000_ao_ex_trig_enable() is executed\n");
2555 /* Check if the state machine is stopped */
2556 tmp = me4000_inl(ao_context->status_reg);
2557 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2559 "me4000_ao_ex_trig_enable():Can't enable trigger while DAC is running\n");
2563 spin_lock_irqsave(&ao_context->int_lock, flags);
2564 tmp = me4000_inl(ao_context->ctrl_reg);
2565 tmp |= ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG;
2566 me4000_outl(tmp, ao_context->ctrl_reg);
2567 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2572 static int me4000_ao_ex_trig_disable(struct me4000_ao_context *ao_context)
2575 unsigned long flags;
2577 CALL_PDEBUG("me4000_ao_ex_trig_disable() is executed\n");
2579 /* Check if the state machine is stopped */
2580 tmp = me4000_inl(ao_context->status_reg);
2581 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2583 "me4000_ao_ex_trig_disable():Can't disable trigger while DAC is running\n");
2587 spin_lock_irqsave(&ao_context->int_lock, flags);
2588 tmp = me4000_inl(ao_context->ctrl_reg);
2589 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG;
2590 me4000_outl(tmp, ao_context->ctrl_reg);
2591 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2596 static int me4000_ao_simultaneous_disable(struct me4000_ao_context *ao_context)
2600 CALL_PDEBUG("me4000_ao_simultaneous_disable() is executed\n");
2602 /* Check if the state machine is stopped */
2603 /* Be careful here because this function is called from
2604 me4000_ao_synchronous disable */
2605 tmp = me4000_inl(ao_context->status_reg);
2606 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2608 "me4000_ao_simultaneous_disable():Can't disable while DAC is running\n");
2612 spin_lock(&ao_context->board_info->preload_lock);
2613 tmp = me4000_inl(ao_context->preload_reg);
2614 tmp &= ~(0x1 << ao_context->index); // Disable preload bit
2615 tmp &= ~(0x1 << (ao_context->index + 16)); // Disable hw simultaneous bit
2616 me4000_outl(tmp, ao_context->preload_reg);
2617 spin_unlock(&ao_context->board_info->preload_lock);
2622 static int me4000_ao_simultaneous_ex_trig(struct me4000_ao_context *ao_context)
2626 CALL_PDEBUG("me4000_ao_simultaneous_ex_trig() is executed\n");
2628 spin_lock(&ao_context->board_info->preload_lock);
2629 tmp = me4000_inl(ao_context->preload_reg);
2630 tmp |= (0x1 << ao_context->index); // Enable preload bit
2631 tmp |= (0x1 << (ao_context->index + 16)); // Enable hw simultaneous bit
2632 me4000_outl(tmp, ao_context->preload_reg);
2633 spin_unlock(&ao_context->board_info->preload_lock);
2638 static int me4000_ao_simultaneous_sw(struct me4000_ao_context *ao_context)
2642 CALL_PDEBUG("me4000_ao_simultaneous_sw() is executed\n");
2644 spin_lock(&ao_context->board_info->preload_lock);
2645 tmp = me4000_inl(ao_context->preload_reg);
2646 tmp |= (0x1 << ao_context->index); // Enable preload bit
2647 tmp &= ~(0x1 << (ao_context->index + 16)); // Disable hw simultaneous bit
2648 me4000_outl(tmp, ao_context->preload_reg);
2649 spin_unlock(&ao_context->board_info->preload_lock);
2654 static int me4000_ao_preload(struct me4000_ao_context *ao_context)
2656 CALL_PDEBUG("me4000_ao_preload() is executed\n");
2657 return me4000_ao_simultaneous_sw(ao_context);
2660 static int me4000_ao_preload_update(struct me4000_ao_context *ao_context)
2664 struct list_head *entry;
2666 CALL_PDEBUG("me4000_ao_preload_update() is executed\n");
2668 spin_lock(&ao_context->board_info->preload_lock);
2669 tmp = me4000_inl(ao_context->preload_reg);
2670 list_for_each(entry, &ao_context->board_info->ao_context_list) {
2671 /* The channels we update must be in the following state :
2673 - Hardware trigger is disabled
2674 - Corresponding simultaneous bit is reset
2676 ctrl = me4000_inl(ao_context->ctrl_reg);
2679 (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1 |
2680 ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG))) {
2684 (((struct me4000_ao_context *)entry)->index
2688 (((struct me4000_ao_context *)entry)->
2693 me4000_outl(tmp, ao_context->preload_reg);
2694 spin_unlock(&ao_context->board_info->preload_lock);
2699 static int me4000_ao_simultaneous_update(struct me4000_ao_channel_list *arg,
2700 struct me4000_ao_context *ao_context)
2705 struct me4000_ao_channel_list channels;
2707 CALL_PDEBUG("me4000_ao_simultaneous_update() is executed\n");
2709 /* Copy data from user */
2710 err = copy_from_user(&channels, arg,
2711 sizeof(struct me4000_ao_channel_list));
2714 "ME4000:me4000_ao_simultaneous_update():Can't copy command\n");
2719 kzalloc(sizeof(unsigned long) * channels.count, GFP_KERNEL);
2720 if (!channels.list) {
2722 "ME4000:me4000_ao_simultaneous_update():Can't get buffer\n");
2726 /* Copy channel list from user */
2728 copy_from_user(channels.list, arg->list,
2729 sizeof(unsigned long) * channels.count);
2732 "ME4000:me4000_ao_simultaneous_update():Can't copy list\n");
2733 kfree(channels.list);
2737 spin_lock(&ao_context->board_info->preload_lock);
2738 tmp = me4000_inl(ao_context->preload_reg);
2739 for (i = 0; i < channels.count; i++) {
2740 if (channels.list[i] >
2741 ao_context->board_info->board_p->ao.count) {
2742 spin_unlock(&ao_context->board_info->preload_lock);
2743 kfree(channels.list);
2745 "ME4000:me4000_ao_simultaneous_update():Invalid board number specified\n");
2748 tmp &= ~(0x1 << channels.list[i]); // Clear the preload bit
2749 tmp &= ~(0x1 << (channels.list[i] + 16)); // Clear the hw simultaneous bit
2751 me4000_outl(tmp, ao_context->preload_reg);
2752 spin_unlock(&ao_context->board_info->preload_lock);
2753 kfree(channels.list);
2758 static int me4000_ao_synchronous_ex_trig(struct me4000_ao_context *ao_context)
2761 unsigned long flags;
2763 CALL_PDEBUG("me4000_ao_synchronous_ex_trig() is executed\n");
2765 /* Check if the state machine is stopped */
2766 tmp = me4000_inl(ao_context->status_reg);
2767 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2769 "me4000_ao_synchronous_ex_trig(): DAC is running\n");
2773 spin_lock(&ao_context->board_info->preload_lock);
2774 tmp = me4000_inl(ao_context->preload_reg);
2775 tmp &= ~(0x1 << ao_context->index); // Disable synchronous sw bit
2776 tmp |= 0x1 << (ao_context->index + 16); // Enable synchronous hw bit
2777 me4000_outl(tmp, ao_context->preload_reg);
2778 spin_unlock(&ao_context->board_info->preload_lock);
2781 spin_lock_irqsave(&ao_context->int_lock, flags);
2782 tmp = me4000_inl(ao_context->ctrl_reg);
2783 if (tmp & (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1)) {
2785 ~(ME4000_AO_CTRL_BIT_STOP |
2786 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2787 me4000_outl(tmp, ao_context->ctrl_reg);
2789 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2794 static int me4000_ao_synchronous_sw(struct me4000_ao_context *ao_context)
2797 unsigned long flags;
2799 CALL_PDEBUG("me4000_ao_synchronous_sw() is executed\n");
2801 /* Check if the state machine is stopped */
2802 tmp = me4000_inl(ao_context->status_reg);
2803 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2804 printk(KERN_ERR "me4000_ao_synchronous_sw(): DAC is running\n");
2808 spin_lock(&ao_context->board_info->preload_lock);
2809 tmp = me4000_inl(ao_context->preload_reg);
2810 tmp |= 0x1 << ao_context->index; // Enable synchronous sw bit
2811 tmp &= ~(0x1 << (ao_context->index + 16)); // Disable synchronous hw bit
2812 me4000_outl(tmp, ao_context->preload_reg);
2813 spin_unlock(&ao_context->board_info->preload_lock);
2816 spin_lock_irqsave(&ao_context->int_lock, flags);
2817 tmp = me4000_inl(ao_context->ctrl_reg);
2818 if (tmp & (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1)) {
2820 ~(ME4000_AO_CTRL_BIT_STOP |
2821 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2822 me4000_outl(tmp, ao_context->ctrl_reg);
2824 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2829 static int me4000_ao_synchronous_disable(struct me4000_ao_context *ao_context)
2831 return me4000_ao_simultaneous_disable(ao_context);
2834 static int me4000_ao_get_free_buffer(unsigned long *arg,
2835 struct me4000_ao_context *ao_context)
2840 c = me4000_buf_space(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT);
2842 err = copy_to_user(arg, &c, sizeof(unsigned long));
2845 "%s:Can't copy to user space\n", __func__);
2852 static int me4000_ao_ex_trig_timeout(unsigned long *arg,
2853 struct me4000_ao_context *ao_context)
2856 wait_queue_head_t queue;
2858 unsigned long timeout;
2860 CALL_PDEBUG("me4000_ao_ex_trig_timeout() is executed\n");
2862 if (get_user(timeout, arg)) {
2864 "me4000_ao_ex_trig_timeout():Cannot copy data from user\n");
2868 init_waitqueue_head(&queue);
2870 tmp = inl(ao_context->ctrl_reg);
2872 if ((tmp & ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG)) {
2875 while ((inl(ao_context->status_reg) &
2876 ME4000_AO_STATUS_BIT_FSM)) {
2877 interruptible_sleep_on_timeout(&queue, 1);
2878 if (signal_pending(current)) {
2880 "ME4000:me4000_ao_ex_trig_timeout():Wait on start of state machine interrupted\n");
2883 if (((jiffies - ref) > (timeout * HZ / USER_HZ))) { // 2.6 has diffrent definitions for HZ in user and kernel space
2885 "ME4000:me4000_ao_ex_trig_timeout():Timeout reached\n");
2890 while ((inl(ao_context->status_reg) &
2891 ME4000_AO_STATUS_BIT_FSM)) {
2892 interruptible_sleep_on_timeout(&queue, 1);
2893 if (signal_pending(current)) {
2895 "ME4000:me4000_ao_ex_trig_timeout():Wait on start of state machine interrupted\n");
2902 "ME4000:me4000_ao_ex_trig_timeout():External Trigger is not enabled\n");
2909 static int me4000_ao_enable_do(struct me4000_ao_context *ao_context)
2912 unsigned long flags;
2914 CALL_PDEBUG("me4000_ao_enable_do() is executed\n");
2916 /* Only available for analog output 3 */
2917 if (ao_context->index != 3) {
2919 "me4000_ao_enable_do():Only available for analog output 3\n");
2923 /* Check if the state machine is stopped */
2924 tmp = me4000_inl(ao_context->status_reg);
2925 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2926 printk(KERN_ERR "me4000_ao_enable_do(): DAC is running\n");
2930 /* Set the stop bit */
2931 spin_lock_irqsave(&ao_context->int_lock, flags);
2932 tmp = inl(ao_context->ctrl_reg);
2933 tmp |= ME4000_AO_CTRL_BIT_ENABLE_DO;
2934 me4000_outl(tmp, ao_context->ctrl_reg);
2935 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2940 static int me4000_ao_disable_do(struct me4000_ao_context *ao_context)
2943 unsigned long flags;
2945 CALL_PDEBUG("me4000_ao_disable_do() is executed\n");
2947 /* Only available for analog output 3 */
2948 if (ao_context->index != 3) {
2950 "me4000_ao_disable():Only available for analog output 3\n");
2954 /* Check if the state machine is stopped */
2955 tmp = me4000_inl(ao_context->status_reg);
2956 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2957 printk(KERN_ERR "me4000_ao_disable_do(): DAC is running\n");
2961 spin_lock_irqsave(&ao_context->int_lock, flags);
2962 tmp = inl(ao_context->ctrl_reg);
2963 tmp &= ~(ME4000_AO_CTRL_BIT_ENABLE_DO);
2964 me4000_outl(tmp, ao_context->ctrl_reg);
2965 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2970 static int me4000_ao_fsm_state(int *arg, struct me4000_ao_context *ao_context)
2974 CALL_PDEBUG("me4000_ao_fsm_state() is executed\n");
2977 (me4000_inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) ? 1
2980 if (ao_context->pipe_flag) {
2981 printk(KERN_ERR "me4000_ao_fsm_state():Broken pipe detected\n");
2985 if (put_user(tmp, arg)) {
2986 printk(KERN_ERR "me4000_ao_fsm_state():Cannot copy to user\n");
2993 /*------------------------- Analog input stuff -------------------------------*/
2995 static int me4000_ai_prepare(struct me4000_ai_context *ai_context)
2997 wait_queue_head_t queue;
3000 CALL_PDEBUG("me4000_ai_prepare() is executed\n");
3002 init_waitqueue_head(&queue);
3004 /* Set the new mode and stop bits */
3005 me4000_outl(ai_context->
3006 mode | ME4000_AI_CTRL_BIT_STOP |
3007 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP, ai_context->ctrl_reg);
3009 /* Set the timer registers */
3010 ai_context->chan_timer = 66;
3011 ai_context->chan_pre_timer = 66;
3012 ai_context->scan_timer_low = 0;
3013 ai_context->scan_timer_high = 0;
3015 me4000_outl(65, ai_context->chan_timer_reg);
3016 me4000_outl(65, ai_context->chan_pre_timer_reg);
3017 me4000_outl(0, ai_context->scan_timer_low_reg);
3018 me4000_outl(0, ai_context->scan_timer_high_reg);
3019 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3020 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3022 ai_context->channel_list_count = 0;
3024 if (ai_context->mode) {
3025 /* Request the interrupt line */
3027 request_irq(ai_context->irq, me4000_ai_isr,
3028 IRQF_DISABLED | IRQF_SHARED, ME4000_NAME,
3032 "ME4000:me4000_ai_prepare():Can't get interrupt line");
3036 /* Allocate circular buffer */
3037 ai_context->circ_buf.buf =
3038 kzalloc(ME4000_AI_BUFFER_SIZE, GFP_KERNEL);
3039 if (!ai_context->circ_buf.buf) {
3041 "ME4000:me4000_ai_prepare():Can't get circular buffer\n");
3042 free_irq(ai_context->irq, ai_context);
3046 /* Clear the circular buffer */
3047 ai_context->circ_buf.head = 0;
3048 ai_context->circ_buf.tail = 0;
3054 static int me4000_ai_reset(struct me4000_ai_context *ai_context)
3056 wait_queue_head_t queue;
3058 unsigned long flags;
3060 CALL_PDEBUG("me4000_ai_reset() is executed\n");
3062 init_waitqueue_head(&queue);
3065 * First stop conversion of the state machine before reconfigure.
3066 * If not stopped before configuring mode, it could
3067 * walk in a undefined state.
3069 spin_lock_irqsave(&ai_context->int_lock, flags);
3070 tmp = me4000_inl(ai_context->ctrl_reg);
3071 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
3072 me4000_outl(tmp, ai_context->ctrl_reg);
3073 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3075 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3076 interruptible_sleep_on_timeout(&queue, 1);
3077 if (signal_pending(current)) {
3079 "me4000_ai_reset():Wait on state machine after stop interrupted\n");
3084 /* Clear the control register and set the stop bits */
3085 spin_lock_irqsave(&ai_context->int_lock, flags);
3086 tmp = me4000_inl(ai_context->ctrl_reg);
3087 me4000_outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP | ME4000_AI_CTRL_BIT_STOP,
3088 ai_context->ctrl_reg);
3089 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3091 /* Reset timer registers */
3092 ai_context->chan_timer = 66;
3093 ai_context->chan_pre_timer = 66;
3094 ai_context->scan_timer_low = 0;
3095 ai_context->scan_timer_high = 0;
3096 ai_context->sample_counter = 0;
3097 ai_context->sample_counter_reload = 0;
3099 me4000_outl(65, ai_context->chan_timer_reg);
3100 me4000_outl(65, ai_context->chan_pre_timer_reg);
3101 me4000_outl(0, ai_context->scan_timer_low_reg);
3102 me4000_outl(0, ai_context->scan_timer_high_reg);
3103 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3104 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3105 me4000_outl(0, ai_context->sample_counter_reg);
3107 ai_context->channel_list_count = 0;
3109 /* Clear the circular buffer */
3110 ai_context->circ_buf.head = 0;
3111 ai_context->circ_buf.tail = 0;
3116 static int me4000_ai_ioctl_sing(struct inode *inode_p, struct file *file_p,
3117 unsigned int service, unsigned long arg)
3119 struct me4000_ai_context *ai_context;
3121 CALL_PDEBUG("me4000_ai_ioctl_sing() is executed\n");
3123 ai_context = file_p->private_data;
3125 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3126 printk(KERN_ERR "me4000_ai_ioctl_sing():Wrong magic number\n");
3129 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3131 "me4000_ai_ioctl_sing():Service number to high\n");
3136 case ME4000_AI_SINGLE:
3137 return me4000_ai_single((struct me4000_ai_single *)arg,
3139 case ME4000_AI_EX_TRIG_ENABLE:
3140 return me4000_ai_ex_trig_enable(ai_context);
3141 case ME4000_AI_EX_TRIG_DISABLE:
3142 return me4000_ai_ex_trig_disable(ai_context);
3143 case ME4000_AI_EX_TRIG_SETUP:
3144 return me4000_ai_ex_trig_setup((struct me4000_ai_trigger *)arg,
3146 case ME4000_GET_USER_INFO:
3147 return me4000_get_user_info((struct me4000_user_info *)arg,
3148 ai_context->board_info);
3149 case ME4000_AI_OFFSET_ENABLE:
3150 return me4000_ai_offset_enable(ai_context);
3151 case ME4000_AI_OFFSET_DISABLE:
3152 return me4000_ai_offset_disable(ai_context);
3153 case ME4000_AI_FULLSCALE_ENABLE:
3154 return me4000_ai_fullscale_enable(ai_context);
3155 case ME4000_AI_FULLSCALE_DISABLE:
3156 return me4000_ai_fullscale_disable(ai_context);
3157 case ME4000_AI_EEPROM_READ:
3158 return me4000_eeprom_read((struct me4000_eeprom *)arg,
3160 case ME4000_AI_EEPROM_WRITE:
3161 return me4000_eeprom_write((struct me4000_eeprom *)arg,
3165 "me4000_ai_ioctl_sing():Invalid service number\n");
3171 static int me4000_ai_single(struct me4000_ai_single *arg,
3172 struct me4000_ai_context *ai_context)
3174 struct me4000_ai_single cmd;
3177 wait_queue_head_t queue;
3178 unsigned long jiffy;
3180 CALL_PDEBUG("me4000_ai_single() is executed\n");
3182 init_waitqueue_head(&queue);
3184 /* Copy data from user */
3185 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_single));
3188 "ME4000:me4000_ai_single():Can't copy from user space\n");
3192 /* Check range parameter */
3193 switch (cmd.range) {
3194 case ME4000_AI_LIST_RANGE_BIPOLAR_10:
3195 case ME4000_AI_LIST_RANGE_BIPOLAR_2_5:
3196 case ME4000_AI_LIST_RANGE_UNIPOLAR_10:
3197 case ME4000_AI_LIST_RANGE_UNIPOLAR_2_5:
3201 "ME4000:me4000_ai_single():Invalid range specified\n");
3205 /* Check mode and channel number */
3207 case ME4000_AI_LIST_INPUT_SINGLE_ENDED:
3208 if (cmd.channel >= ai_context->board_info->board_p->ai.count) {
3210 "ME4000:me4000_ai_single():Analog input is not available\n");
3214 case ME4000_AI_LIST_INPUT_DIFFERENTIAL:
3216 ai_context->board_info->board_p->ai.diff_count) {
3218 "ME4000:me4000_ai_single():Analog input is not available in differential mode\n");
3224 "ME4000:me4000_ai_single():Invalid mode specified\n");
3228 /* Clear channel list, data fifo and both stop bits */
3229 tmp = me4000_inl(ai_context->ctrl_reg);
3231 ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_DATA_FIFO |
3232 ME4000_AI_CTRL_BIT_STOP | ME4000_AI_CTRL_BIT_IMMEDIATE_STOP);
3233 me4000_outl(tmp, ai_context->ctrl_reg);
3235 /* Enable channel list and data fifo */
3236 tmp |= ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_DATA_FIFO;
3237 me4000_outl(tmp, ai_context->ctrl_reg);
3239 /* Generate channel list entry */
3240 me4000_outl(cmd.channel | cmd.range | cmd.
3241 mode | ME4000_AI_LIST_LAST_ENTRY,
3242 ai_context->channel_list_reg);
3244 /* Set the timer to maximum */
3245 me4000_outl(66, ai_context->chan_timer_reg);
3246 me4000_outl(66, ai_context->chan_pre_timer_reg);
3248 if (tmp & ME4000_AI_CTRL_BIT_EX_TRIG) {
3251 (me4000_inl(ai_context->status_reg) &
3252 ME4000_AI_STATUS_BIT_EF_DATA)) {
3253 interruptible_sleep_on_timeout(&queue, 1);
3254 if (signal_pending(current)) {
3256 "ME4000:me4000_ai_single():Wait on start of state machine interrupted\n");
3259 if (((jiffies - jiffy) > (cmd.timeout * HZ / USER_HZ)) && cmd.timeout) { // 2.6 has diffrent definitions for HZ in user and kernel space
3261 "ME4000:me4000_ai_single():Timeout reached\n");
3266 /* Start conversion */
3267 me4000_inl(ai_context->start_reg);
3269 /* Wait until ready */
3272 (me4000_inl(ai_context->status_reg) &
3273 ME4000_AI_STATUS_BIT_EF_DATA)) {
3275 "ME4000:me4000_ai_single():Value not available after wait\n");
3280 /* Read value from data fifo */
3281 cmd.value = me4000_inl(ai_context->data_reg) & 0xFFFF;
3283 /* Copy result back to user */
3284 err = copy_to_user(arg, &cmd, sizeof(struct me4000_ai_single));
3287 "ME4000:me4000_ai_single():Can't copy to user space\n");
3294 static int me4000_ai_ioctl_sw(struct inode *inode_p, struct file *file_p,
3295 unsigned int service, unsigned long arg)
3297 struct me4000_ai_context *ai_context;
3299 CALL_PDEBUG("me4000_ai_ioctl_sw() is executed\n");
3301 ai_context = file_p->private_data;
3303 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3304 printk(KERN_ERR "me4000_ai_ioctl_sw():Wrong magic number\n");
3307 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3309 "me4000_ai_ioctl_sw():Service number to high\n");
3314 case ME4000_AI_SC_SETUP:
3315 return me4000_ai_sc_setup((struct me4000_ai_sc *)arg,
3317 case ME4000_AI_CONFIG:
3318 return me4000_ai_config((struct me4000_ai_config *)arg,
3320 case ME4000_AI_START:
3321 return me4000_ai_start(ai_context);
3322 case ME4000_AI_STOP:
3323 return me4000_ai_stop(ai_context);
3324 case ME4000_AI_IMMEDIATE_STOP:
3325 return me4000_ai_immediate_stop(ai_context);
3326 case ME4000_AI_FSM_STATE:
3327 return me4000_ai_fsm_state((int *)arg, ai_context);
3328 case ME4000_GET_USER_INFO:
3329 return me4000_get_user_info((struct me4000_user_info *)arg,
3330 ai_context->board_info);
3331 case ME4000_AI_EEPROM_READ:
3332 return me4000_eeprom_read((struct me4000_eeprom *)arg,
3334 case ME4000_AI_EEPROM_WRITE:
3335 return me4000_eeprom_write((struct me4000_eeprom *)arg,
3337 case ME4000_AI_GET_COUNT_BUFFER:
3338 return me4000_ai_get_count_buffer((unsigned long *)arg,
3342 "%s:Invalid service number %d\n", __func__, service);
3348 static int me4000_ai_ioctl_ext(struct inode *inode_p, struct file *file_p,
3349 unsigned int service, unsigned long arg)
3351 struct me4000_ai_context *ai_context;
3353 CALL_PDEBUG("me4000_ai_ioctl_ext() is executed\n");
3355 ai_context = file_p->private_data;
3357 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3358 printk(KERN_ERR "me4000_ai_ioctl_ext():Wrong magic number\n");
3361 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3363 "me4000_ai_ioctl_ext():Service number to high\n");
3368 case ME4000_AI_SC_SETUP:
3369 return me4000_ai_sc_setup((struct me4000_ai_sc *)arg,
3371 case ME4000_AI_CONFIG:
3372 return me4000_ai_config((struct me4000_ai_config *)arg,
3374 case ME4000_AI_START:
3375 return me4000_ai_start_ex((unsigned long *)arg, ai_context);
3376 case ME4000_AI_STOP:
3377 return me4000_ai_stop(ai_context);
3378 case ME4000_AI_IMMEDIATE_STOP:
3379 return me4000_ai_immediate_stop(ai_context);
3380 case ME4000_AI_EX_TRIG_ENABLE:
3381 return me4000_ai_ex_trig_enable(ai_context);
3382 case ME4000_AI_EX_TRIG_DISABLE:
3383 return me4000_ai_ex_trig_disable(ai_context);
3384 case ME4000_AI_EX_TRIG_SETUP:
3385 return me4000_ai_ex_trig_setup((struct me4000_ai_trigger *)arg,
3387 case ME4000_AI_FSM_STATE:
3388 return me4000_ai_fsm_state((int *)arg, ai_context);
3389 case ME4000_GET_USER_INFO:
3390 return me4000_get_user_info((struct me4000_user_info *)arg,
3391 ai_context->board_info);
3392 case ME4000_AI_GET_COUNT_BUFFER:
3393 return me4000_ai_get_count_buffer((unsigned long *)arg,
3397 "%s:Invalid service number %d\n", __func__ , service);
3403 static int me4000_ai_fasync(int fd, struct file *file_p, int mode)
3405 struct me4000_ai_context *ai_context;
3407 CALL_PDEBUG("me4000_ao_fasync_cont() is executed\n");
3409 ai_context = file_p->private_data;
3410 return fasync_helper(fd, file_p, mode, &ai_context->fasync_p);
3413 static int me4000_ai_config(struct me4000_ai_config *arg,
3414 struct me4000_ai_context *ai_context)
3416 struct me4000_ai_config cmd;
3421 wait_queue_head_t queue;
3425 CALL_PDEBUG("me4000_ai_config() is executed\n");
3427 init_waitqueue_head(&queue);
3429 /* Check if conversion is stopped */
3430 if (inl(ai_context->ctrl_reg) & ME4000_AI_STATUS_BIT_FSM) {
3432 "ME4000:me4000_ai_config():Conversion is not stopped\n");
3437 /* Copy data from user */
3438 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_config));
3441 "ME4000:me4000_ai_config():Can't copy from user space\n");
3447 ("me4000_ai_config():chan = %ld, pre_chan = %ld, scan_low = %ld, scan_high = %ld, count = %ld\n",
3448 cmd.timer.chan, cmd.timer.pre_chan, cmd.timer.scan_low,
3449 cmd.timer.scan_high, cmd.channel_list.count);
3451 /* Check whether sample and hold is available for this board */
3453 if (!ai_context->board_info->board_p->ai.sh_count) {
3455 "ME4000:me4000_ai_config():Sample and Hold is not available for this board\n");
3461 /* Check the channel list size */
3462 if (cmd.channel_list.count > ME4000_AI_CHANNEL_LIST_COUNT) {
3464 "me4000_ai_config():Channel list is to large\n");
3469 /* Copy channel list from user */
3470 list = kmalloc(sizeof(u32) * cmd.channel_list.count, GFP_KERNEL);
3473 "ME4000:me4000_ai_config():Can't get memory for channel list\n");
3478 copy_from_user(list, cmd.channel_list.list,
3479 sizeof(u32) * cmd.channel_list.count);
3482 "ME4000:me4000_ai_config():Can't copy from user space\n");
3487 /* Check if last entry bit is set */
3488 if (!(list[cmd.channel_list.count - 1] & ME4000_AI_LIST_LAST_ENTRY)) {
3490 "me4000_ai_config():Last entry bit is not set\n");
3491 list[cmd.channel_list.count - 1] |= ME4000_AI_LIST_LAST_ENTRY;
3494 /* Check whether mode is equal for all entries */
3495 mode = list[0] & 0x20;
3496 for (i = 0; i < cmd.channel_list.count; i++) {
3497 if ((list[i] & 0x20) != mode) {
3499 "ME4000:me4000_ai_config():Mode is not equal for all entries\n");
3505 /* Check whether channels are available for this mode */
3506 if (mode == ME4000_AI_LIST_INPUT_SINGLE_ENDED) {
3507 for (i = 0; i < cmd.channel_list.count; i++) {
3508 if ((list[i] & 0x1F) >=
3509 ai_context->board_info->board_p->ai.count) {
3511 "ME4000:me4000_ai_config():Channel is not available for single ended\n");
3516 } else if (mode == ME4000_AI_LIST_INPUT_DIFFERENTIAL) {
3517 for (i = 0; i < cmd.channel_list.count; i++) {
3518 if ((list[i] & 0x1F) >=
3519 ai_context->board_info->board_p->ai.diff_count) {
3521 "ME4000:me4000_ai_config():Channel is not available for differential\n");
3528 /* Check if bipolar is set for all entries when in differential mode */
3529 if (mode == ME4000_AI_LIST_INPUT_DIFFERENTIAL) {
3530 for (i = 0; i < cmd.channel_list.count; i++) {
3531 if ((list[i] & 0xC0) != ME4000_AI_LIST_RANGE_BIPOLAR_10
3532 && (list[i] & 0xC0) !=
3533 ME4000_AI_LIST_RANGE_BIPOLAR_2_5) {
3535 "ME4000:me4000_ai_config():Bipolar is not selected in differential mode\n");
3542 if (ai_context->mode != ME4000_AI_ACQ_MODE_EXT_SINGLE_VALUE) {
3543 /* Check for minimum channel divisor */
3544 if (cmd.timer.chan < ME4000_AI_MIN_TICKS) {
3546 "ME4000:me4000_ai_config():Channel timer divisor is to low\n");
3551 /* Check if minimum channel divisor is adjusted when sample and hold is activated */
3552 if ((cmd.sh) && (cmd.timer.chan != ME4000_AI_MIN_TICKS)) {
3554 "ME4000:me4000_ai_config():Channel timer divisor must be at minimum when sample and hold is activated\n");
3559 /* Check for minimum channel pre divisor */
3560 if (cmd.timer.pre_chan < ME4000_AI_MIN_TICKS) {
3562 "ME4000:me4000_ai_config():Channel pre timer divisor is to low\n");
3567 /* Write the channel timers */
3568 me4000_outl(cmd.timer.chan - 1, ai_context->chan_timer_reg);
3569 me4000_outl(cmd.timer.pre_chan - 1,
3570 ai_context->chan_pre_timer_reg);
3572 /* Save the timer values in the board context */
3573 ai_context->chan_timer = cmd.timer.chan;
3574 ai_context->chan_pre_timer = cmd.timer.pre_chan;
3576 if (ai_context->mode != ME4000_AI_ACQ_MODE_EXT_SINGLE_CHANLIST) {
3577 /* Check for scan timer divisor */
3579 (u64) cmd.timer.scan_low | ((u64) cmd.timer.
3583 cmd.channel_list.count * cmd.timer.chan +
3586 "ME4000:me4000_ai_config():Scan timer divisor is to low\n");
3592 /* Write the scan timers */
3595 tmp = (u32) (scan & 0xFFFFFFFF);
3597 ai_context->scan_timer_low_reg);
3598 tmp = (u32) ((scan >> 32) & 0xFFFFFFFF);
3600 ai_context->scan_timer_high_reg);
3603 scan - (cmd.timer.chan - 1) +
3604 (cmd.timer.pre_chan - 1);
3605 tmp = (u32) (scan & 0xFFFFFFFF);
3607 ai_context->scan_pre_timer_low_reg);
3608 tmp = (u32) ((scan >> 32) & 0xFFFFFFFF);
3611 scan_pre_timer_high_reg);
3614 ai_context->scan_timer_low_reg);
3616 ai_context->scan_timer_high_reg);
3619 ai_context->scan_pre_timer_low_reg);
3622 scan_pre_timer_high_reg);
3625 ai_context->scan_timer_low = cmd.timer.scan_low;
3626 ai_context->scan_timer_high = cmd.timer.scan_high;
3630 /* Clear the channel list */
3631 tmp = me4000_inl(ai_context->ctrl_reg);
3632 tmp &= ~ME4000_AI_CTRL_BIT_CHANNEL_FIFO;
3633 me4000_outl(tmp, ai_context->ctrl_reg);
3634 tmp |= ME4000_AI_CTRL_BIT_CHANNEL_FIFO;
3635 me4000_outl(tmp, ai_context->ctrl_reg);
3637 /* Write the channel list */
3638 for (i = 0; i < cmd.channel_list.count; i++) {
3639 me4000_outl(list[i], ai_context->channel_list_reg);
3642 /* Setup sample and hold */
3644 tmp |= ME4000_AI_CTRL_BIT_SAMPLE_HOLD;
3645 me4000_outl(tmp, ai_context->ctrl_reg);
3647 tmp &= ~ME4000_AI_CTRL_BIT_SAMPLE_HOLD;
3648 me4000_outl(tmp, ai_context->ctrl_reg);
3651 /* Save the channel list size in the board context */
3652 ai_context->channel_list_count = cmd.channel_list.count;
3660 /* Reset the timers */
3661 ai_context->chan_timer = 66;
3662 ai_context->chan_pre_timer = 66;
3663 ai_context->scan_timer_low = 0;
3664 ai_context->scan_timer_high = 0;
3666 me4000_outl(65, ai_context->chan_timer_reg);
3667 me4000_outl(65, ai_context->chan_pre_timer_reg);
3668 me4000_outl(0, ai_context->scan_timer_high_reg);
3669 me4000_outl(0, ai_context->scan_timer_low_reg);
3670 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3671 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3673 ai_context->channel_list_count = 0;
3675 tmp = me4000_inl(ai_context->ctrl_reg);
3677 ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_SAMPLE_HOLD);
3686 static int ai_common_start(struct me4000_ai_context *ai_context)
3689 CALL_PDEBUG("ai_common_start() is executed\n");
3691 tmp = me4000_inl(ai_context->ctrl_reg);
3693 /* Check if conversion is stopped */
3694 if (tmp & ME4000_AI_STATUS_BIT_FSM) {
3696 "ME4000:ai_common_start():Conversion is not stopped\n");
3700 /* Clear data fifo, disable all interrupts, clear sample counter reload */
3701 tmp &= ~(ME4000_AI_CTRL_BIT_DATA_FIFO | ME4000_AI_CTRL_BIT_LE_IRQ |
3702 ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3703 ME4000_AI_CTRL_BIT_SC_RELOAD);
3705 me4000_outl(tmp, ai_context->ctrl_reg);
3707 /* Clear circular buffer */
3708 ai_context->circ_buf.head = 0;
3709 ai_context->circ_buf.tail = 0;
3711 /* Enable data fifo */
3712 tmp |= ME4000_AI_CTRL_BIT_DATA_FIFO;
3714 /* Determine interrupt setup */
3715 if (ai_context->sample_counter && !ai_context->sample_counter_reload) {
3716 /* Enable Half Full Interrupt and Sample Counter Interrupt */
3717 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ | ME4000_AI_CTRL_BIT_HF_IRQ;
3718 } else if (ai_context->sample_counter
3719 && ai_context->sample_counter_reload) {
3720 if (ai_context->sample_counter <= ME4000_AI_FIFO_COUNT / 2) {
3721 /* Enable only Sample Counter Interrupt */
3723 ME4000_AI_CTRL_BIT_SC_IRQ |
3724 ME4000_AI_CTRL_BIT_SC_RELOAD;
3726 /* Enable Half Full Interrupt and Sample Counter Interrupt */
3728 ME4000_AI_CTRL_BIT_SC_IRQ |
3729 ME4000_AI_CTRL_BIT_HF_IRQ |
3730 ME4000_AI_CTRL_BIT_SC_RELOAD;
3733 /* Enable only Half Full Interrupt */
3734 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ;
3737 /* Clear the stop bits */
3738 tmp &= ~(ME4000_AI_CTRL_BIT_STOP | ME4000_AI_CTRL_BIT_IMMEDIATE_STOP);
3740 /* Write setup to hardware */
3741 me4000_outl(tmp, ai_context->ctrl_reg);
3743 /* Write sample counter */
3744 me4000_outl(ai_context->sample_counter, ai_context->sample_counter_reg);
3749 static int me4000_ai_start(struct me4000_ai_context *ai_context)
3752 CALL_PDEBUG("me4000_ai_start() is executed\n");
3754 /* Prepare Hardware */
3755 err = ai_common_start(ai_context);
3759 /* Start conversion by dummy read */
3760 me4000_inl(ai_context->start_reg);
3765 static int me4000_ai_start_ex(unsigned long *arg,
3766 struct me4000_ai_context *ai_context)
3769 wait_queue_head_t queue;
3771 unsigned long timeout;
3773 CALL_PDEBUG("me4000_ai_start_ex() is executed\n");
3775 if (get_user(timeout, arg)) {
3777 "me4000_ai_start_ex():Cannot copy data from user\n");
3781 init_waitqueue_head(&queue);
3783 /* Prepare Hardware */
3784 err = ai_common_start(ai_context);
3791 (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM))
3793 interruptible_sleep_on_timeout(&queue, 1);
3794 if (signal_pending(current)) {
3796 "ME4000:me4000_ai_start_ex():Wait on start of state machine interrupted\n");
3799 if (((jiffies - ref) > (timeout * HZ / USER_HZ))) { // 2.6 has diffrent definitions for HZ in user and kernel space
3801 "ME4000:me4000_ai_start_ex():Timeout reached\n");
3807 (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM))
3809 interruptible_sleep_on_timeout(&queue, 1);
3810 if (signal_pending(current)) {
3812 "ME4000:me4000_ai_start_ex():Wait on start of state machine interrupted\n");
3821 static int me4000_ai_stop(struct me4000_ai_context *ai_context)
3823 wait_queue_head_t queue;
3825 unsigned long flags;
3827 CALL_PDEBUG("me4000_ai_stop() is executed\n");
3829 init_waitqueue_head(&queue);
3831 /* Disable irqs and clear data fifo */
3832 spin_lock_irqsave(&ai_context->int_lock, flags);
3833 tmp = me4000_inl(ai_context->ctrl_reg);
3835 ~(ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3836 ME4000_AI_CTRL_BIT_DATA_FIFO);
3837 /* Stop conversion of the state machine */
3838 tmp |= ME4000_AI_CTRL_BIT_STOP;
3839 me4000_outl(tmp, ai_context->ctrl_reg);
3840 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3842 /* Clear circular buffer */
3843 ai_context->circ_buf.head = 0;
3844 ai_context->circ_buf.tail = 0;
3846 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3847 interruptible_sleep_on_timeout(&queue, 1);
3848 if (signal_pending(current)) {
3850 "ME4000:me4000_ai_stop():Wait on state machine after stop interrupted\n");
3858 static int me4000_ai_immediate_stop(struct me4000_ai_context *ai_context)
3860 wait_queue_head_t queue;
3862 unsigned long flags;
3864 CALL_PDEBUG("me4000_ai_stop() is executed\n");
3866 init_waitqueue_head(&queue);
3868 /* Disable irqs and clear data fifo */
3869 spin_lock_irqsave(&ai_context->int_lock, flags);
3870 tmp = me4000_inl(ai_context->ctrl_reg);
3872 ~(ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3873 ME4000_AI_CTRL_BIT_DATA_FIFO);
3874 /* Stop conversion of the state machine */
3875 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
3876 me4000_outl(tmp, ai_context->ctrl_reg);
3877 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3879 /* Clear circular buffer */
3880 ai_context->circ_buf.head = 0;
3881 ai_context->circ_buf.tail = 0;
3883 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3884 interruptible_sleep_on_timeout(&queue, 1);
3885 if (signal_pending(current)) {
3887 "ME4000:me4000_ai_stop():Wait on state machine after stop interrupted\n");
3895 static int me4000_ai_ex_trig_enable(struct me4000_ai_context *ai_context)
3898 unsigned long flags;
3900 CALL_PDEBUG("me4000_ai_ex_trig_enable() is executed\n");
3902 spin_lock_irqsave(&ai_context->int_lock, flags);
3903 tmp = me4000_inl(ai_context->ctrl_reg);
3904 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG;
3905 me4000_outl(tmp, ai_context->ctrl_reg);
3906 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3911 static int me4000_ai_ex_trig_disable(struct me4000_ai_context *ai_context)
3914 unsigned long flags;
3916 CALL_PDEBUG("me4000_ai_ex_trig_disable() is executed\n");
3918 spin_lock_irqsave(&ai_context->int_lock, flags);
3919 tmp = me4000_inl(ai_context->ctrl_reg);
3920 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG;
3921 me4000_outl(tmp, ai_context->ctrl_reg);
3922 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3927 static int me4000_ai_ex_trig_setup(struct me4000_ai_trigger *arg,
3928 struct me4000_ai_context *ai_context)
3930 struct me4000_ai_trigger cmd;
3933 unsigned long flags;
3935 CALL_PDEBUG("me4000_ai_ex_trig_setup() is executed\n");
3937 /* Copy data from user */
3938 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_trigger));
3941 "ME4000:me4000_ai_ex_trig_setup():Can't copy from user space\n");
3945 spin_lock_irqsave(&ai_context->int_lock, flags);
3946 tmp = me4000_inl(ai_context->ctrl_reg);
3948 if (cmd.mode == ME4000_AI_TRIGGER_EXT_DIGITAL) {
3949 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG;
3950 } else if (cmd.mode == ME4000_AI_TRIGGER_EXT_ANALOG) {
3951 if (!ai_context->board_info->board_p->ai.ex_trig_analog) {
3953 "ME4000:me4000_ai_ex_trig_setup():No analog trigger available\n");
3956 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG;
3958 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3960 "ME4000:me4000_ai_ex_trig_setup():Invalid trigger mode specified\n");
3964 if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_RISING) {
3966 ~(ME4000_AI_CTRL_BIT_EX_TRIG_BOTH |
3967 ME4000_AI_CTRL_BIT_EX_TRIG_FALLING);
3968 } else if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_FALLING) {
3969 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG_FALLING;
3970 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG_BOTH;
3971 } else if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_BOTH) {
3973 ME4000_AI_CTRL_BIT_EX_TRIG_BOTH |
3974 ME4000_AI_CTRL_BIT_EX_TRIG_FALLING;
3976 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3978 "ME4000:me4000_ai_ex_trig_setup():Invalid trigger edge specified\n");
3982 me4000_outl(tmp, ai_context->ctrl_reg);
3983 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3987 static int me4000_ai_sc_setup(struct me4000_ai_sc *arg,
3988 struct me4000_ai_context *ai_context)
3990 struct me4000_ai_sc cmd;
3993 CALL_PDEBUG("me4000_ai_sc_setup() is executed\n");
3995 /* Copy data from user */
3996 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_sc));
3999 "ME4000:me4000_ai_sc_setup():Can't copy from user space\n");
4003 ai_context->sample_counter = cmd.value;
4004 ai_context->sample_counter_reload = cmd.reload;
4009 static ssize_t me4000_ai_read(struct file *filep, char *buff, size_t cnt,
4012 struct me4000_ai_context *ai_context = filep->private_data;
4013 s16 *buffer = (s16 *) buff;
4014 size_t count = cnt / 2;
4015 unsigned long flags;
4022 CALL_PDEBUG("me4000_ai_read() is executed\n");
4024 init_waitqueue_entry(&wait, current);
4028 PDEBUG("me4000_ai_read():Count is 0\n");
4033 if (filep->f_flags & O_NONBLOCK) {
4034 c = me4000_values_to_end(ai_context->circ_buf,
4035 ME4000_AI_BUFFER_COUNT);
4038 ("me4000_ai_read():Returning from nonblocking read\n");
4042 /* Check if conversion is still running */
4044 (me4000_inl(ai_context->status_reg) &
4045 ME4000_AI_STATUS_BIT_FSM)) {
4047 "ME4000:me4000_ai_read():Conversion interrupted\n");
4051 wait_event_interruptible(ai_context->wait_queue,
4052 (me4000_values_to_end
4053 (ai_context->circ_buf,
4054 ME4000_AI_BUFFER_COUNT)));
4055 if (signal_pending(current)) {
4057 "ME4000:me4000_ai_read():Wait on values interrupted from signal\n");
4062 /* Only read count values or as much as available */
4063 c = me4000_values_to_end(ai_context->circ_buf,
4064 ME4000_AI_BUFFER_COUNT);
4065 PDEBUG("me4000_ai_read():%d values to end\n", c);
4069 PDEBUG("me4000_ai_read():Copy %d values to user space\n", c);
4071 k -= copy_to_user(buffer,
4072 ai_context->circ_buf.buf +
4073 ai_context->circ_buf.tail, k);
4077 "ME4000:me4000_ai_read():Cannot copy new values to user\n");
4081 ai_context->circ_buf.tail =
4082 (ai_context->circ_buf.tail + c) & (ME4000_AI_BUFFER_COUNT -
4088 spin_lock_irqsave(&ai_context->int_lock, flags);
4089 if (me4000_buf_space
4090 (ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
4091 tmp = me4000_inl(ai_context->ctrl_reg);
4093 /* Determine interrupt setup */
4094 if (ai_context->sample_counter
4095 && !ai_context->sample_counter_reload) {
4096 /* Enable Half Full Interrupt and Sample Counter Interrupt */
4098 ME4000_AI_CTRL_BIT_SC_IRQ |
4099 ME4000_AI_CTRL_BIT_HF_IRQ;
4100 } else if (ai_context->sample_counter
4101 && ai_context->sample_counter_reload) {
4102 if (ai_context->sample_counter <
4103 ME4000_AI_FIFO_COUNT / 2) {
4104 /* Enable only Sample Counter Interrupt */
4105 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ;
4107 /* Enable Half Full Interrupt and Sample Counter Interrupt */
4109 ME4000_AI_CTRL_BIT_SC_IRQ |
4110 ME4000_AI_CTRL_BIT_HF_IRQ;
4113 /* Enable only Half Full Interrupt */
4114 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ;
4117 me4000_outl(tmp, ai_context->ctrl_reg);
4119 spin_unlock_irqrestore(&ai_context->int_lock, flags);
4122 /* Check if conversion is still running */
4123 if (!(me4000_inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM)) {
4125 "ME4000:me4000_ai_read():Conversion not running after complete read\n");
4129 if (filep->f_flags & O_NONBLOCK) {
4130 return (k == 0) ? -EAGAIN : 2 * ret;
4133 CALL_PDEBUG("me4000_ai_read() is leaved\n");
4137 static unsigned int me4000_ai_poll(struct file *file_p, poll_table *wait)
4139 struct me4000_ai_context *ai_context;
4140 unsigned long mask = 0;
4142 CALL_PDEBUG("me4000_ai_poll() is executed\n");
4144 ai_context = file_p->private_data;
4146 /* Register wait queue */
4147 poll_wait(file_p, &ai_context->wait_queue, wait);
4149 /* Get available values */
4150 if (me4000_values_to_end(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT))
4151 mask |= POLLIN | POLLRDNORM;
4153 PDEBUG("me4000_ai_poll():Return mask %lX\n", mask);
4158 static int me4000_ai_offset_enable(struct me4000_ai_context *ai_context)
4162 CALL_PDEBUG("me4000_ai_offset_enable() is executed\n");
4164 tmp = me4000_inl(ai_context->ctrl_reg);
4165 tmp |= ME4000_AI_CTRL_BIT_OFFSET;
4166 me4000_outl(tmp, ai_context->ctrl_reg);
4171 static int me4000_ai_offset_disable(struct me4000_ai_context *ai_context)
4175 CALL_PDEBUG("me4000_ai_offset_disable() is executed\n");
4177 tmp = me4000_inl(ai_context->ctrl_reg);
4178 tmp &= ~ME4000_AI_CTRL_BIT_OFFSET;
4179 me4000_outl(tmp, ai_context->ctrl_reg);
4184 static int me4000_ai_fullscale_enable(struct me4000_ai_context *ai_context)
4188 CALL_PDEBUG("me4000_ai_fullscale_enable() is executed\n");
4190 tmp = me4000_inl(ai_context->ctrl_reg);
4191 tmp |= ME4000_AI_CTRL_BIT_FULLSCALE;
4192 me4000_outl(tmp, ai_context->ctrl_reg);
4197 static int me4000_ai_fullscale_disable(struct me4000_ai_context *ai_context)
4201 CALL_PDEBUG("me4000_ai_fullscale_disable() is executed\n");
4203 tmp = me4000_inl(ai_context->ctrl_reg);
4204 tmp &= ~ME4000_AI_CTRL_BIT_FULLSCALE;
4205 me4000_outl(tmp, ai_context->ctrl_reg);
4210 static int me4000_ai_fsm_state(int *arg, struct me4000_ai_context *ai_context)
4214 CALL_PDEBUG("me4000_ai_fsm_state() is executed\n");
4217 (me4000_inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) ? 1
4220 if (put_user(tmp, arg)) {
4221 printk(KERN_ERR "me4000_ai_fsm_state():Cannot copy to user\n");
4228 static int me4000_ai_get_count_buffer(unsigned long *arg,
4229 struct me4000_ai_context *ai_context)
4234 c = me4000_buf_count(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT);
4236 err = copy_to_user(arg, &c, sizeof(unsigned long));
4239 "%s:Can't copy to user space\n", __func__);
4246 /*---------------------------------- EEPROM stuff ---------------------------*/
4248 static int eeprom_write_cmd(struct me4000_ai_context *ai_context, unsigned long cmd,
4252 unsigned long value;
4254 CALL_PDEBUG("eeprom_write_cmd() is executed\n");
4256 PDEBUG("eeprom_write_cmd():Write command 0x%08lX with length = %d\n",
4259 /* Get the ICR register and clear the related bits */
4260 value = me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR);
4261 value &= ~(PLX_ICR_MASK_EEPROM);
4262 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4264 /* Raise the chip select */
4265 value |= PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4266 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4267 udelay(EEPROM_DELAY);
4269 for (i = 0; i < length; i++) {
4270 if (cmd & ((0x1 << (length - 1)) >> i)) {
4271 value |= PLX_ICR_BIT_EEPROM_WRITE;
4273 value &= ~PLX_ICR_BIT_EEPROM_WRITE;
4276 /* Write to EEPROM */
4278 ai_context->board_info->plx_regbase + PLX_ICR);
4279 udelay(EEPROM_DELAY);
4281 /* Raising edge of the clock */
4282 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4284 ai_context->board_info->plx_regbase + PLX_ICR);
4285 udelay(EEPROM_DELAY);
4287 /* Falling edge of the clock */
4288 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4290 ai_context->board_info->plx_regbase + PLX_ICR);
4291 udelay(EEPROM_DELAY);
4294 /* Clear the chip select */
4295 value &= ~PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4296 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4297 udelay(EEPROM_DELAY);
4299 /* Wait until hardware is ready for sure */
4305 static unsigned short eeprom_read_cmd(struct me4000_ai_context *ai_context,
4306 unsigned long cmd, int length)
4309 unsigned long value;
4310 unsigned short id = 0;
4312 CALL_PDEBUG("eeprom_read_cmd() is executed\n");
4314 PDEBUG("eeprom_read_cmd():Read command 0x%08lX with length = %d\n", cmd,
4317 /* Get the ICR register and clear the related bits */
4318 value = me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR);
4319 value &= ~(PLX_ICR_MASK_EEPROM);
4321 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4323 /* Raise the chip select */
4324 value |= PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4325 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4326 udelay(EEPROM_DELAY);
4328 /* Write the read command to the eeprom */
4329 for (i = 0; i < length; i++) {
4330 if (cmd & ((0x1 << (length - 1)) >> i)) {
4331 value |= PLX_ICR_BIT_EEPROM_WRITE;
4333 value &= ~PLX_ICR_BIT_EEPROM_WRITE;
4336 ai_context->board_info->plx_regbase + PLX_ICR);
4337 udelay(EEPROM_DELAY);
4339 /* Raising edge of the clock */
4340 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4342 ai_context->board_info->plx_regbase + PLX_ICR);
4343 udelay(EEPROM_DELAY);
4345 /* Falling edge of the clock */
4346 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4348 ai_context->board_info->plx_regbase + PLX_ICR);
4349 udelay(EEPROM_DELAY);
4352 /* Read the value from the eeprom */
4353 for (i = 0; i < 16; i++) {
4354 /* Raising edge of the clock */
4355 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4357 ai_context->board_info->plx_regbase + PLX_ICR);
4358 udelay(EEPROM_DELAY);
4360 if (me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR) &
4361 PLX_ICR_BIT_EEPROM_READ) {
4362 id |= (0x8000 >> i);
4363 PDEBUG("eeprom_read_cmd():OR with 0x%04X\n",
4366 PDEBUG("eeprom_read_cmd():Dont't OR\n");
4369 /* Falling edge of the clock */
4370 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4372 ai_context->board_info->plx_regbase + PLX_ICR);
4373 udelay(EEPROM_DELAY);
4376 /* Clear the chip select */
4377 value &= ~PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4378 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4379 udelay(EEPROM_DELAY);
4384 static int me4000_eeprom_write(struct me4000_eeprom *arg,
4385 struct me4000_ai_context *ai_context)
4388 struct me4000_eeprom setup;
4390 unsigned long date_high;
4391 unsigned long date_low;
4393 CALL_PDEBUG("me4000_eeprom_write() is executed\n");
4395 err = copy_from_user(&setup, arg, sizeof(setup));
4398 "ME4000:me4000_eeprom_write():Cannot copy from user\n");
4402 /* Enable writing */
4403 eeprom_write_cmd(ai_context, ME4000_EEPROM_CMD_WRITE_ENABLE,
4404 ME4000_EEPROM_CMD_LENGTH_WRITE_ENABLE);
4406 /* Command for date */
4407 date_high = (setup.date & 0xFFFF0000) >> 16;
4408 date_low = (setup.date & 0x0000FFFF);
4411 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_DATE_HIGH <<
4412 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4416 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4421 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_DATE_LOW <<
4422 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4426 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4430 /* Command for unipolar 10V offset */
4432 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_UNI_OFFSET <<
4433 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4438 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4442 /* Command for unipolar 10V fullscale */
4444 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_UNI_FULLSCALE <<
4445 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4450 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4454 /* Command for unipolar 2,5V offset */
4456 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_UNI_OFFSET <<
4457 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4462 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4466 /* Command for unipolar 2,5V fullscale */
4468 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_UNI_FULLSCALE <<
4469 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4474 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4478 /* Command for bipolar 10V offset */
4480 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_BI_OFFSET <<
4481 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4486 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4490 /* Command for bipolar 10V fullscale */
4492 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_BI_FULLSCALE <<
4493 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4498 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4502 /* Command for bipolar 2,5V offset */
4504 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_BI_OFFSET <<
4505 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4510 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4514 /* Command for bipolar 2,5V fullscale */
4516 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_BI_FULLSCALE <<
4517 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4522 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4526 /* Command for differential 10V offset */
4528 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_DIFF_OFFSET <<
4529 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4534 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4538 /* Command for differential 10V fullscale */
4540 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_DIFF_FULLSCALE
4541 << ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4546 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4550 /* Command for differential 2,5V offset */
4552 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_DIFF_OFFSET <<
4553 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4558 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4562 /* Command for differential 2,5V fullscale */
4564 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_DIFF_FULLSCALE
4565 << ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4569 diff_2_5_fullscale);
4570 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4574 /* Disable writing */
4575 eeprom_write_cmd(ai_context, ME4000_EEPROM_CMD_WRITE_DISABLE,
4576 ME4000_EEPROM_CMD_LENGTH_WRITE_DISABLE);
4581 static int me4000_eeprom_read(struct me4000_eeprom *arg,
4582 struct me4000_ai_context *ai_context)
4586 struct me4000_eeprom setup;
4588 CALL_PDEBUG("me4000_eeprom_read() is executed\n");
4590 /* Command for date */
4591 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_DATE_HIGH;
4593 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4595 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_DATE_LOW;
4597 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4599 /* Command for unipolar 10V offset */
4600 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_UNI_OFFSET;
4601 setup.uni_10_offset =
4602 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4604 /* Command for unipolar 10V fullscale */
4605 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_UNI_FULLSCALE;
4606 setup.uni_10_fullscale =
4607 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4609 /* Command for unipolar 2,5V offset */
4610 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_UNI_OFFSET;
4611 setup.uni_2_5_offset =
4612 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4614 /* Command for unipolar 2,5V fullscale */
4615 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_UNI_FULLSCALE;
4616 setup.uni_2_5_fullscale =
4617 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4619 /* Command for bipolar 10V offset */
4620 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_BI_OFFSET;
4621 setup.bi_10_offset =
4622 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4624 /* Command for bipolar 10V fullscale */
4625 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_BI_FULLSCALE;
4626 setup.bi_10_fullscale =
4627 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4629 /* Command for bipolar 2,5V offset */
4630 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_BI_OFFSET;
4631 setup.bi_2_5_offset =
4632 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4634 /* Command for bipolar 2,5V fullscale */
4635 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_BI_FULLSCALE;
4636 setup.bi_2_5_fullscale =
4637 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4639 /* Command for differntial 10V offset */
4640 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_DIFF_OFFSET;
4641 setup.diff_10_offset =
4642 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4644 /* Command for differential 10V fullscale */
4645 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_DIFF_FULLSCALE;
4646 setup.diff_10_fullscale =
4647 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4649 /* Command for differntial 2,5V offset */
4650 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_DIFF_OFFSET;
4651 setup.diff_2_5_offset =
4652 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4654 /* Command for differential 2,5V fullscale */
4655 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_DIFF_FULLSCALE;
4656 setup.diff_2_5_fullscale =
4657 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4659 err = copy_to_user(arg, &setup, sizeof(setup));
4662 "ME4000:me4000_eeprom_read():Cannot copy to user\n");
4669 /*------------------------------------ DIO stuff ----------------------------------------------*/
4671 static int me4000_dio_ioctl(struct inode *inode_p, struct file *file_p,
4672 unsigned int service, unsigned long arg)
4674 struct me4000_dio_context *dio_context;
4676 CALL_PDEBUG("me4000_dio_ioctl() is executed\n");
4678 dio_context = file_p->private_data;
4680 if (_IOC_TYPE(service) != ME4000_MAGIC) {
4681 printk(KERN_ERR "me4000_dio_ioctl():Wrong magic number\n");
4684 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
4685 printk(KERN_ERR "me4000_dio_ioctl():Service number to high\n");
4690 case ME4000_DIO_CONFIG:
4691 return me4000_dio_config((struct me4000_dio_config *)arg,
4693 case ME4000_DIO_SET_BYTE:
4694 return me4000_dio_set_byte((struct me4000_dio_byte *)arg,
4696 case ME4000_DIO_GET_BYTE:
4697 return me4000_dio_get_byte((struct me4000_dio_byte *)arg,
4699 case ME4000_DIO_RESET:
4700 return me4000_dio_reset(dio_context);
4703 "ME4000:me4000_dio_ioctl():Invalid service number %d\n",
4710 static int me4000_dio_config(struct me4000_dio_config *arg,
4711 struct me4000_dio_context *dio_context)
4713 struct me4000_dio_config cmd;
4717 CALL_PDEBUG("me4000_dio_config() is executed\n");
4719 /* Copy data from user */
4720 err = copy_from_user(&cmd, arg, sizeof(struct me4000_dio_config));
4723 "ME4000:me4000_dio_config():Can't copy from user space\n");
4727 /* Check port parameter */
4728 if (cmd.port >= dio_context->dio_count) {
4730 "ME4000:me4000_dio_config():Port %d is not available\n",
4735 PDEBUG("me4000_dio_config(): port %d, mode %d, function %d\n", cmd.port,
4736 cmd.mode, cmd.function);
4738 if (cmd.port == ME4000_DIO_PORT_A) {
4739 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4740 /* Check if opto isolated version */
4741 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4743 "ME4000:me4000_dio_config():Cannot set to input on opto isolated versions\n");
4747 tmp = me4000_inl(dio_context->ctrl_reg);
4749 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4750 ME4000_DIO_CTRL_BIT_MODE_1);
4751 me4000_outl(tmp, dio_context->ctrl_reg);
4752 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4753 tmp = me4000_inl(dio_context->ctrl_reg);
4755 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4756 ME4000_DIO_CTRL_BIT_MODE_1);
4757 tmp |= ME4000_DIO_CTRL_BIT_MODE_0;
4758 me4000_outl(tmp, dio_context->ctrl_reg);
4759 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4760 tmp = me4000_inl(dio_context->ctrl_reg);
4762 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4763 ME4000_DIO_CTRL_BIT_MODE_1 |
4764 ME4000_DIO_CTRL_BIT_FIFO_HIGH_0);
4766 ME4000_DIO_CTRL_BIT_MODE_0 |
4767 ME4000_DIO_CTRL_BIT_MODE_1;
4768 me4000_outl(tmp, dio_context->ctrl_reg);
4769 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4770 tmp = me4000_inl(dio_context->ctrl_reg);
4772 ME4000_DIO_CTRL_BIT_MODE_0 |
4773 ME4000_DIO_CTRL_BIT_MODE_1 |
4774 ME4000_DIO_CTRL_BIT_FIFO_HIGH_0;
4775 me4000_outl(tmp, dio_context->ctrl_reg);
4778 "ME4000:me4000_dio_config():Mode %d is not available\n",
4782 } else if (cmd.port == ME4000_DIO_PORT_B) {
4783 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4784 /* Only do anything when TTL version is installed */
4785 if ((me4000_inl(dio_context->dir_reg) & 0x1)) {
4786 tmp = me4000_inl(dio_context->ctrl_reg);
4788 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4789 ME4000_DIO_CTRL_BIT_MODE_3);
4790 me4000_outl(tmp, dio_context->ctrl_reg);
4792 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4793 /* Check if opto isolated version */
4794 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4796 "ME4000:me4000_dio_config():Cannot set to output on opto isolated versions\n");
4800 tmp = me4000_inl(dio_context->ctrl_reg);
4802 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4803 ME4000_DIO_CTRL_BIT_MODE_3);
4804 tmp |= ME4000_DIO_CTRL_BIT_MODE_2;
4805 me4000_outl(tmp, dio_context->ctrl_reg);
4806 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4807 /* Check if opto isolated version */
4808 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4810 "ME4000:me4000_dio_config():Cannot set to FIFO low output on opto isolated versions\n");
4814 tmp = me4000_inl(dio_context->ctrl_reg);
4816 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4817 ME4000_DIO_CTRL_BIT_MODE_3 |
4818 ME4000_DIO_CTRL_BIT_FIFO_HIGH_1);
4820 ME4000_DIO_CTRL_BIT_MODE_2 |
4821 ME4000_DIO_CTRL_BIT_MODE_3;
4822 me4000_outl(tmp, dio_context->ctrl_reg);
4823 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4824 /* Check if opto isolated version */
4825 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4827 "ME4000:me4000_dio_config():Cannot set to FIFO high output on opto isolated versions\n");
4831 tmp = me4000_inl(dio_context->ctrl_reg);
4833 ME4000_DIO_CTRL_BIT_MODE_2 |
4834 ME4000_DIO_CTRL_BIT_MODE_3 |
4835 ME4000_DIO_CTRL_BIT_FIFO_HIGH_1;
4836 me4000_outl(tmp, dio_context->ctrl_reg);
4839 "ME4000:me4000_dio_config():Mode %d is not available\n",
4843 } else if (cmd.port == ME4000_DIO_PORT_C) {
4844 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4845 tmp = me4000_inl(dio_context->ctrl_reg);
4847 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4848 ME4000_DIO_CTRL_BIT_MODE_5);
4849 me4000_outl(tmp, dio_context->ctrl_reg);
4850 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4851 tmp = me4000_inl(dio_context->ctrl_reg);
4853 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4854 ME4000_DIO_CTRL_BIT_MODE_5);
4855 tmp |= ME4000_DIO_CTRL_BIT_MODE_4;
4856 me4000_outl(tmp, dio_context->ctrl_reg);
4857 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4858 tmp = me4000_inl(dio_context->ctrl_reg);
4860 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4861 ME4000_DIO_CTRL_BIT_MODE_5 |
4862 ME4000_DIO_CTRL_BIT_FIFO_HIGH_2);
4864 ME4000_DIO_CTRL_BIT_MODE_4 |
4865 ME4000_DIO_CTRL_BIT_MODE_5;
4866 me4000_outl(tmp, dio_context->ctrl_reg);
4867 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4868 tmp = me4000_inl(dio_context->ctrl_reg);
4870 ME4000_DIO_CTRL_BIT_MODE_4 |
4871 ME4000_DIO_CTRL_BIT_MODE_5 |
4872 ME4000_DIO_CTRL_BIT_FIFO_HIGH_2;
4873 me4000_outl(tmp, dio_context->ctrl_reg);
4876 "ME4000:me4000_dio_config():Mode %d is not available\n",
4880 } else if (cmd.port == ME4000_DIO_PORT_D) {
4881 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4882 tmp = me4000_inl(dio_context->ctrl_reg);
4884 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4885 ME4000_DIO_CTRL_BIT_MODE_7);
4886 me4000_outl(tmp, dio_context->ctrl_reg);
4887 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4888 tmp = me4000_inl(dio_context->ctrl_reg);
4890 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4891 ME4000_DIO_CTRL_BIT_MODE_7);
4892 tmp |= ME4000_DIO_CTRL_BIT_MODE_6;
4893 me4000_outl(tmp, dio_context->ctrl_reg);
4894 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4895 tmp = me4000_inl(dio_context->ctrl_reg);
4897 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4898 ME4000_DIO_CTRL_BIT_MODE_7 |
4899 ME4000_DIO_CTRL_BIT_FIFO_HIGH_3);
4901 ME4000_DIO_CTRL_BIT_MODE_6 |
4902 ME4000_DIO_CTRL_BIT_MODE_7;
4903 me4000_outl(tmp, dio_context->ctrl_reg);
4904 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4905 tmp = me4000_inl(dio_context->ctrl_reg);
4907 ME4000_DIO_CTRL_BIT_MODE_6 |
4908 ME4000_DIO_CTRL_BIT_MODE_7 |
4909 ME4000_DIO_CTRL_BIT_FIFO_HIGH_3;
4910 me4000_outl(tmp, dio_context->ctrl_reg);
4913 "ME4000:me4000_dio_config():Mode %d is not available\n",
4919 "ME4000:me4000_dio_config():Port %d is not available\n",
4924 PDEBUG("me4000_dio_config(): port %d, mode %d, function %d\n", cmd.port,
4925 cmd.mode, cmd.function);
4927 if ((cmd.mode == ME4000_DIO_FIFO_HIGH)
4928 || (cmd.mode == ME4000_DIO_FIFO_LOW)) {
4929 tmp = me4000_inl(dio_context->ctrl_reg);
4931 ~(ME4000_DIO_CTRL_BIT_FUNCTION_0 |
4932 ME4000_DIO_CTRL_BIT_FUNCTION_1);
4933 if (cmd.function == ME4000_DIO_FUNCTION_PATTERN) {
4934 me4000_outl(tmp, dio_context->ctrl_reg);
4935 } else if (cmd.function == ME4000_DIO_FUNCTION_DEMUX) {
4936 tmp |= ME4000_DIO_CTRL_BIT_FUNCTION_0;
4937 me4000_outl(tmp, dio_context->ctrl_reg);
4938 } else if (cmd.function == ME4000_DIO_FUNCTION_MUX) {
4939 tmp |= ME4000_DIO_CTRL_BIT_FUNCTION_1;
4940 me4000_outl(tmp, dio_context->ctrl_reg);
4943 "ME4000:me4000_dio_config():Invalid port function specified\n");
4951 static int me4000_dio_set_byte(struct me4000_dio_byte *arg,
4952 struct me4000_dio_context *dio_context)
4954 struct me4000_dio_byte cmd;
4957 CALL_PDEBUG("me4000_dio_set_byte() is executed\n");
4959 /* Copy data from user */
4960 err = copy_from_user(&cmd, arg, sizeof(struct me4000_dio_byte));
4963 "ME4000:me4000_dio_set_byte():Can't copy from user space\n");
4967 /* Check port parameter */
4968 if (cmd.port >= dio_context->dio_count) {
4970 "ME4000:me4000_dio_set_byte():Port %d is not available\n",
4975 if (cmd.port == ME4000_DIO_PORT_A) {
4976 if ((me4000_inl(dio_context->ctrl_reg) & 0x3) != 0x1) {
4978 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4982 me4000_outl(cmd.byte, dio_context->port_0_reg);
4983 } else if (cmd.port == ME4000_DIO_PORT_B) {
4984 if ((me4000_inl(dio_context->ctrl_reg) & 0xC) != 0x4) {
4986 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4990 me4000_outl(cmd.byte, dio_context->port_1_reg);
4991 } else if (cmd.port == ME4000_DIO_PORT_C) {
4992 if ((me4000_inl(dio_context->ctrl_reg) & 0x30) != 0x10) {
4994 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4998 me4000_outl(cmd.byte, dio_context->port_2_reg);
4999 } else if (cmd.port == ME4000_DIO_PORT_D) {
5000 if ((me4000_inl(dio_context->ctrl_reg) & 0xC0) != 0x40) {
5002 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
5006 me4000_outl(cmd.byte, dio_context->port_3_reg);
5009 "ME4000:me4000_dio_set_byte():Port %d is not available\n",
5017 static int me4000_dio_get_byte(struct me4000_dio_byte *arg,
5018 struct me4000_dio_context *dio_context)
5020 struct me4000_dio_byte cmd;
5023 CALL_PDEBUG("me4000_dio_get_byte() is executed\n");
5025 /* Copy data from user */
5026 err = copy_from_user(&cmd, arg, sizeof(struct me4000_dio_byte));
5029 "ME4000:me4000_dio_get_byte():Can't copy from user space\n");
5033 /* Check port parameter */
5034 if (cmd.port >= dio_context->dio_count) {
5036 "ME4000:me4000_dio_get_byte():Port %d is not available\n",
5041 if (cmd.port == ME4000_DIO_PORT_A) {
5042 cmd.byte = me4000_inl(dio_context->port_0_reg) & 0xFF;
5043 } else if (cmd.port == ME4000_DIO_PORT_B) {
5044 cmd.byte = me4000_inl(dio_context->port_1_reg) & 0xFF;
5045 } else if (cmd.port == ME4000_DIO_PORT_C) {
5046 cmd.byte = me4000_inl(dio_context->port_2_reg) & 0xFF;
5047 } else if (cmd.port == ME4000_DIO_PORT_D) {
5048 cmd.byte = me4000_inl(dio_context->port_3_reg) & 0xFF;
5051 "ME4000:me4000_dio_get_byte():Port %d is not available\n",
5056 /* Copy result back to user */
5057 err = copy_to_user(arg, &cmd, sizeof(struct me4000_dio_byte));
5060 "ME4000:me4000_dio_get_byte():Can't copy to user space\n");
5067 static int me4000_dio_reset(struct me4000_dio_context *dio_context)
5069 CALL_PDEBUG("me4000_dio_reset() is executed\n");
5071 /* Clear the control register */
5072 me4000_outl(0, dio_context->ctrl_reg);
5074 /* Check for opto isolated version */
5075 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
5076 me4000_outl(0x1, dio_context->ctrl_reg);
5077 me4000_outl(0x0, dio_context->port_0_reg);
5083 /*------------------------------------ COUNTER STUFF ------------------------------------*/
5085 static int me4000_cnt_ioctl(struct inode *inode_p, struct file *file_p,
5086 unsigned int service, unsigned long arg)
5088 struct me4000_cnt_context *cnt_context;
5090 CALL_PDEBUG("me4000_cnt_ioctl() is executed\n");
5092 cnt_context = file_p->private_data;
5094 if (_IOC_TYPE(service) != ME4000_MAGIC) {
5095 printk(KERN_ERR "me4000_dio_ioctl():Wrong magic number\n");
5098 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
5099 printk(KERN_ERR "me4000_dio_ioctl():Service number to high\n");
5104 case ME4000_CNT_READ:
5105 return me4000_cnt_read((struct me4000_cnt *)arg, cnt_context);
5106 case ME4000_CNT_WRITE:
5107 return me4000_cnt_write((struct me4000_cnt *)arg, cnt_context);
5108 case ME4000_CNT_CONFIG:
5109 return me4000_cnt_config((struct me4000_cnt_config *)arg,
5111 case ME4000_CNT_RESET:
5112 return me4000_cnt_reset(cnt_context);
5115 "ME4000:me4000_dio_ioctl():Invalid service number %d\n",
5122 static int me4000_cnt_config(struct me4000_cnt_config *arg,
5123 struct me4000_cnt_context *cnt_context)
5125 struct me4000_cnt_config cmd;
5130 CALL_PDEBUG("me4000_cnt_config() is executed\n");
5132 /* Copy data from user */
5133 err = copy_from_user(&cmd, arg, sizeof(struct me4000_cnt_config));
5136 "ME4000:me4000_cnt_config():Can't copy from user space\n");
5140 /* Check counter parameter */
5141 switch (cmd.counter) {
5142 case ME4000_CNT_COUNTER_0:
5143 counter = ME4000_CNT_CTRL_BIT_COUNTER_0;
5145 case ME4000_CNT_COUNTER_1:
5146 counter = ME4000_CNT_CTRL_BIT_COUNTER_1;
5148 case ME4000_CNT_COUNTER_2:
5149 counter = ME4000_CNT_CTRL_BIT_COUNTER_2;
5153 "ME4000:me4000_cnt_config():Counter %d is not available\n",
5158 /* Check mode parameter */
5160 case ME4000_CNT_MODE_0:
5161 mode = ME4000_CNT_CTRL_BIT_MODE_0;
5163 case ME4000_CNT_MODE_1:
5164 mode = ME4000_CNT_CTRL_BIT_MODE_1;
5166 case ME4000_CNT_MODE_2:
5167 mode = ME4000_CNT_CTRL_BIT_MODE_2;
5169 case ME4000_CNT_MODE_3:
5170 mode = ME4000_CNT_CTRL_BIT_MODE_3;
5172 case ME4000_CNT_MODE_4:
5173 mode = ME4000_CNT_CTRL_BIT_MODE_4;
5175 case ME4000_CNT_MODE_5:
5176 mode = ME4000_CNT_CTRL_BIT_MODE_5;
5180 "ME4000:me4000_cnt_config():Mode %d is not available\n",
5185 /* Write the control word */
5186 me4000_outb((counter | mode | 0x30), cnt_context->ctrl_reg);
5191 static int me4000_cnt_read(struct me4000_cnt *arg,
5192 struct me4000_cnt_context *cnt_context)
5194 struct me4000_cnt cmd;
5198 CALL_PDEBUG("me4000_cnt_read() is executed\n");
5200 /* Copy data from user */
5201 err = copy_from_user(&cmd, arg, sizeof(struct me4000_cnt));
5204 "ME4000:me4000_cnt_read():Can't copy from user space\n");
5209 switch (cmd.counter) {
5210 case ME4000_CNT_COUNTER_0:
5211 tmp = me4000_inb(cnt_context->counter_0_reg);
5213 tmp = me4000_inb(cnt_context->counter_0_reg);
5214 cmd.value |= ((u16) tmp) << 8;
5216 case ME4000_CNT_COUNTER_1:
5217 tmp = me4000_inb(cnt_context->counter_1_reg);
5219 tmp = me4000_inb(cnt_context->counter_1_reg);
5220 cmd.value |= ((u16) tmp) << 8;
5222 case ME4000_CNT_COUNTER_2:
5223 tmp = me4000_inb(cnt_context->counter_2_reg);
5225 tmp = me4000_inb(cnt_context->counter_2_reg);
5226 cmd.value |= ((u16) tmp) << 8;
5230 "ME4000:me4000_cnt_read():Counter %d is not available\n",
5235 /* Copy result back to user */
5236 err = copy_to_user(arg, &cmd, sizeof(struct me4000_cnt));
5239 "ME4000:me4000_cnt_read():Can't copy to user space\n");
5246 static int me4000_cnt_write(struct me4000_cnt *arg,
5247 struct me4000_cnt_context *cnt_context)
5249 struct me4000_cnt cmd;
5253 CALL_PDEBUG("me4000_cnt_write() is executed\n");
5255 /* Copy data from user */
5256 err = copy_from_user(&cmd, arg, sizeof(struct me4000_cnt));
5259 "ME4000:me4000_cnt_write():Can't copy from user space\n");
5264 switch (cmd.counter) {
5265 case ME4000_CNT_COUNTER_0:
5266 tmp = cmd.value & 0xFF;
5267 me4000_outb(tmp, cnt_context->counter_0_reg);
5268 tmp = (cmd.value >> 8) & 0xFF;
5269 me4000_outb(tmp, cnt_context->counter_0_reg);
5271 case ME4000_CNT_COUNTER_1:
5272 tmp = cmd.value & 0xFF;
5273 me4000_outb(tmp, cnt_context->counter_1_reg);
5274 tmp = (cmd.value >> 8) & 0xFF;
5275 me4000_outb(tmp, cnt_context->counter_1_reg);
5277 case ME4000_CNT_COUNTER_2:
5278 tmp = cmd.value & 0xFF;
5279 me4000_outb(tmp, cnt_context->counter_2_reg);
5280 tmp = (cmd.value >> 8) & 0xFF;
5281 me4000_outb(tmp, cnt_context->counter_2_reg);
5285 "ME4000:me4000_cnt_write():Counter %d is not available\n",
5293 static int me4000_cnt_reset(struct me4000_cnt_context *cnt_context)
5295 CALL_PDEBUG("me4000_cnt_reset() is executed\n");
5297 /* Set the mode and value for counter 0 */
5298 me4000_outb(0x30, cnt_context->ctrl_reg);
5299 me4000_outb(0x00, cnt_context->counter_0_reg);
5300 me4000_outb(0x00, cnt_context->counter_0_reg);
5302 /* Set the mode and value for counter 1 */
5303 me4000_outb(0x70, cnt_context->ctrl_reg);
5304 me4000_outb(0x00, cnt_context->counter_1_reg);
5305 me4000_outb(0x00, cnt_context->counter_1_reg);
5307 /* Set the mode and value for counter 2 */
5308 me4000_outb(0xB0, cnt_context->ctrl_reg);
5309 me4000_outb(0x00, cnt_context->counter_2_reg);
5310 me4000_outb(0x00, cnt_context->counter_2_reg);
5315 /*------------------------------------ External Interrupt stuff ------------------------------------*/
5317 static int me4000_ext_int_ioctl(struct inode *inode_p, struct file *file_p,
5318 unsigned int service, unsigned long arg)
5320 struct me4000_ext_int_context *ext_int_context;
5322 CALL_PDEBUG("me4000_ext_int_ioctl() is executed\n");
5324 ext_int_context = file_p->private_data;
5326 if (_IOC_TYPE(service) != ME4000_MAGIC) {
5327 printk(KERN_ERR "me4000_ext_int_ioctl():Wrong magic number\n");
5330 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
5332 "me4000_ext_int_ioctl():Service number to high\n");
5337 case ME4000_EXT_INT_ENABLE:
5338 return me4000_ext_int_enable(ext_int_context);
5339 case ME4000_EXT_INT_DISABLE:
5340 return me4000_ext_int_disable(ext_int_context);
5341 case ME4000_EXT_INT_COUNT:
5342 return me4000_ext_int_count((unsigned long *)arg,
5346 "ME4000:me4000_ext_int_ioctl():Invalid service number %d\n",
5353 static int me4000_ext_int_enable(struct me4000_ext_int_context *ext_int_context)
5357 CALL_PDEBUG("me4000_ext_int_enable() is executed\n");
5359 tmp = me4000_inl(ext_int_context->ctrl_reg);
5360 tmp |= ME4000_AI_CTRL_BIT_EX_IRQ;
5361 me4000_outl(tmp, ext_int_context->ctrl_reg);
5366 static int me4000_ext_int_disable(struct me4000_ext_int_context *ext_int_context)
5370 CALL_PDEBUG("me4000_ext_int_disable() is executed\n");
5372 tmp = me4000_inl(ext_int_context->ctrl_reg);
5373 tmp &= ~ME4000_AI_CTRL_BIT_EX_IRQ;
5374 me4000_outl(tmp, ext_int_context->ctrl_reg);
5379 static int me4000_ext_int_count(unsigned long *arg,
5380 struct me4000_ext_int_context *ext_int_context)
5383 CALL_PDEBUG("me4000_ext_int_count() is executed\n");
5385 put_user(ext_int_context->int_count, arg);
5389 /*------------------------------------ General stuff ------------------------------------*/
5391 static int me4000_get_user_info(struct me4000_user_info *arg,
5392 struct me4000_info *board_info)
5394 struct me4000_user_info user_info;
5396 CALL_PDEBUG("me4000_get_user_info() is executed\n");
5398 user_info.board_count = board_info->board_count;
5399 user_info.plx_regbase = board_info->plx_regbase;
5400 user_info.plx_regbase_size = board_info->plx_regbase_size;
5401 user_info.me4000_regbase = board_info->me4000_regbase;
5402 user_info.me4000_regbase_size = board_info->me4000_regbase_size;
5403 user_info.serial_no = board_info->serial_no;
5404 user_info.hw_revision = board_info->hw_revision;
5405 user_info.vendor_id = board_info->vendor_id;
5406 user_info.device_id = board_info->device_id;
5407 user_info.pci_bus_no = board_info->pci_bus_no;
5408 user_info.pci_dev_no = board_info->pci_dev_no;
5409 user_info.pci_func_no = board_info->pci_func_no;
5410 user_info.irq = board_info->irq;
5411 user_info.irq_count = board_info->irq_count;
5412 user_info.driver_version = ME4000_DRIVER_VERSION;
5413 user_info.ao_count = board_info->board_p->ao.count;
5414 user_info.ao_fifo_count = board_info->board_p->ao.fifo_count;
5416 user_info.ai_count = board_info->board_p->ai.count;
5417 user_info.ai_sh_count = board_info->board_p->ai.sh_count;
5418 user_info.ai_ex_trig_analog = board_info->board_p->ai.ex_trig_analog;
5420 user_info.dio_count = board_info->board_p->dio.count;
5422 user_info.cnt_count = board_info->board_p->cnt.count;
5424 if (copy_to_user(arg, &user_info, sizeof(struct me4000_user_info)))
5430 /*------------------------------------ ISR STUFF ------------------------------------*/
5432 static int me4000_ext_int_fasync(int fd, struct file *file_ptr, int mode)
5435 struct me4000_ext_int_context *ext_int_context;
5437 CALL_PDEBUG("me4000_ext_int_fasync() is executed\n");
5439 ext_int_context = file_ptr->private_data;
5442 fasync_helper(fd, file_ptr, mode, &ext_int_context->fasync_ptr);
5444 CALL_PDEBUG("me4000_ext_int_fasync() is leaved\n");
5448 static irqreturn_t me4000_ao_isr(int irq, void *dev_id)
5452 struct me4000_ao_context *ao_context;
5456 //unsigned long before;
5457 //unsigned long after;
5459 ISR_PDEBUG("me4000_ao_isr() is executed\n");
5461 ao_context = dev_id;
5463 /* Check if irq number is right */
5464 if (irq != ao_context->irq) {
5465 ISR_PDEBUG("me4000_ao_isr():incorrect interrupt num: %d\n",
5470 /* Check if this DAC rised an interrupt */
5472 ((0x1 << (ao_context->index + 3)) &
5473 me4000_inl(ao_context->irq_status_reg))) {
5474 ISR_PDEBUG("me4000_ao_isr():Not this DAC\n");
5478 /* Read status register to find out what happened */
5479 tmp = me4000_inl(ao_context->status_reg);
5481 if (!(tmp & ME4000_AO_STATUS_BIT_EF) && (tmp & ME4000_AO_STATUS_BIT_HF)
5482 && (tmp & ME4000_AO_STATUS_BIT_HF)) {
5483 c = ME4000_AO_FIFO_COUNT;
5484 ISR_PDEBUG("me4000_ao_isr():Fifo empty\n");
5485 } else if ((tmp & ME4000_AO_STATUS_BIT_EF)
5486 && (tmp & ME4000_AO_STATUS_BIT_HF)
5487 && (tmp & ME4000_AO_STATUS_BIT_HF)) {
5488 c = ME4000_AO_FIFO_COUNT / 2;
5489 ISR_PDEBUG("me4000_ao_isr():Fifo under half full\n");
5492 ISR_PDEBUG("me4000_ao_isr():Fifo full\n");
5495 ISR_PDEBUG("me4000_ao_isr():Try to write 0x%04X values\n", c);
5498 c1 = me4000_values_to_end(ao_context->circ_buf,
5499 ME4000_AO_BUFFER_COUNT);
5500 ISR_PDEBUG("me4000_ao_isr():Values to end = %d\n", c1);
5506 ("me4000_ao_isr():Work done or buffer empty\n");
5510 if (((ao_context->fifo_reg & 0xFF) == ME4000_AO_01_FIFO_REG) ||
5511 ((ao_context->fifo_reg & 0xFF) == ME4000_AO_03_FIFO_REG)) {
5512 for (i = 0; i < c1; i++) {
5516 (ao_context->circ_buf.buf +
5517 ao_context->circ_buf.tail + i))) << 16;
5518 outl(value, ao_context->fifo_reg);
5521 outsw(ao_context->fifo_reg,
5522 ao_context->circ_buf.buf +
5523 ao_context->circ_buf.tail, c1);
5526 //printk(KERN_ERR"ME4000:me4000_ao_isr():Time lapse = %lu\n", after - before);
5528 ao_context->circ_buf.tail =
5529 (ao_context->circ_buf.tail + c1) & (ME4000_AO_BUFFER_COUNT -
5531 ISR_PDEBUG("me4000_ao_isr():%d values wrote to port 0x%04X\n",
5532 c1, ao_context->fifo_reg);
5536 /* If there are no values left in the buffer, disable interrupts */
5537 spin_lock(&ao_context->int_lock);
5538 if (!me4000_buf_count(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
5540 ("me4000_ao_isr():Disable Interrupt because no values left in buffer\n");
5541 tmp = me4000_inl(ao_context->ctrl_reg);
5542 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_IRQ;
5543 me4000_outl(tmp, ao_context->ctrl_reg);
5545 spin_unlock(&ao_context->int_lock);
5547 /* Reset the interrupt */
5548 spin_lock(&ao_context->int_lock);
5549 tmp = me4000_inl(ao_context->ctrl_reg);
5550 tmp |= ME4000_AO_CTRL_BIT_RESET_IRQ;
5551 me4000_outl(tmp, ao_context->ctrl_reg);
5552 tmp &= ~ME4000_AO_CTRL_BIT_RESET_IRQ;
5553 me4000_outl(tmp, ao_context->ctrl_reg);
5555 /* If state machine is stopped, flow was interrupted */
5556 if (!(me4000_inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM)) {
5557 printk(KERN_ERR "ME4000:me4000_ao_isr():Broken pipe\n");
5558 ao_context->pipe_flag = 1; // Set flag in order to inform write routine
5559 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_IRQ; // Disable interrupt
5561 me4000_outl(tmp, ao_context->ctrl_reg);
5562 spin_unlock(&ao_context->int_lock);
5564 /* Wake up waiting process */
5565 wake_up_interruptible(&(ao_context->wait_queue));
5567 /* Count the interrupt */
5568 ao_context->board_info->irq_count++;
5573 static irqreturn_t me4000_ai_isr(int irq, void *dev_id)
5576 struct me4000_ai_context *ai_context;
5580 #ifdef ME4000_ISR_DEBUG
5581 unsigned long before;
5582 unsigned long after;
5585 ISR_PDEBUG("me4000_ai_isr() is executed\n");
5587 #ifdef ME4000_ISR_DEBUG
5591 ai_context = dev_id;
5593 /* Check if irq number is right */
5594 if (irq != ai_context->irq) {
5595 ISR_PDEBUG("me4000_ai_isr():incorrect interrupt num: %d\n",
5600 if (me4000_inl(ai_context->irq_status_reg) &
5601 ME4000_IRQ_STATUS_BIT_AI_HF) {
5603 ("me4000_ai_isr():Fifo half full interrupt occured\n");
5605 /* Read status register to find out what happened */
5606 tmp = me4000_inl(ai_context->ctrl_reg);
5608 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5609 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5610 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5611 ISR_PDEBUG("me4000_ai_isr():Fifo full\n");
5612 c = ME4000_AI_FIFO_COUNT;
5614 /* FIFO overflow, so stop conversion and disable all interrupts */
5615 spin_lock(&ai_context->int_lock);
5616 tmp = me4000_inl(ai_context->ctrl_reg);
5617 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5619 ~(ME4000_AI_CTRL_BIT_HF_IRQ |
5620 ME4000_AI_CTRL_BIT_SC_IRQ);
5621 outl(tmp, ai_context->ctrl_reg);
5622 spin_unlock(&ai_context->int_lock);
5623 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5624 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5625 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5626 ISR_PDEBUG("me4000_ai_isr():Fifo half full\n");
5627 c = ME4000_AI_FIFO_COUNT / 2;
5631 ("me4000_ai_isr():Can't determine state of fifo\n");
5634 ISR_PDEBUG("me4000_ai_isr():Try to read %d values\n", c);
5637 c1 = me4000_space_to_end(ai_context->circ_buf,
5638 ME4000_AI_BUFFER_COUNT);
5639 ISR_PDEBUG("me4000_ai_isr():Space to end = %d\n", c1);
5645 ("me4000_ai_isr():Work done or buffer full\n");
5649 insw(ai_context->data_reg,
5650 ai_context->circ_buf.buf +
5651 ai_context->circ_buf.head, c1);
5652 ai_context->circ_buf.head =
5653 (ai_context->circ_buf.head +
5654 c1) & (ME4000_AI_BUFFER_COUNT - 1);
5658 /* Work is done, so reset the interrupt */
5660 ("me4000_ai_isr():reset interrupt fifo half full interrupt\n");
5661 spin_lock(&ai_context->int_lock);
5662 tmp = me4000_inl(ai_context->ctrl_reg);
5663 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
5664 me4000_outl(tmp, ai_context->ctrl_reg);
5665 tmp &= ~ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
5666 me4000_outl(tmp, ai_context->ctrl_reg);
5667 spin_unlock(&ai_context->int_lock);
5670 if (me4000_inl(ai_context->irq_status_reg) & ME4000_IRQ_STATUS_BIT_SC) {
5672 ("me4000_ai_isr():Sample counter interrupt occured\n");
5674 if (!ai_context->sample_counter_reload) {
5676 ("me4000_ai_isr():Single data block available\n");
5678 /* Poll data until fifo empty */
5680 (i < ME4000_AI_FIFO_COUNT / 2)
5681 && (inl(ai_context->ctrl_reg) &
5682 ME4000_AI_STATUS_BIT_EF_DATA); i++) {
5683 if (me4000_space_to_end
5684 (ai_context->circ_buf,
5685 ME4000_AI_BUFFER_COUNT)) {
5686 *(ai_context->circ_buf.buf +
5687 ai_context->circ_buf.head) =
5688 inw(ai_context->data_reg);
5689 ai_context->circ_buf.head =
5690 (ai_context->circ_buf.head +
5691 1) & (ME4000_AI_BUFFER_COUNT - 1);
5695 ISR_PDEBUG("me4000_ai_isr():%d values read\n", i);
5697 if (ai_context->sample_counter <=
5698 ME4000_AI_FIFO_COUNT / 2) {
5700 ("me4000_ai_isr():Interrupt from adjustable half full threshold\n");
5702 /* Read status register to find out what happened */
5703 tmp = me4000_inl(ai_context->ctrl_reg);
5705 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5706 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5707 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5709 ("me4000_ai_isr():Fifo full\n");
5710 c = ME4000_AI_FIFO_COUNT;
5712 /* FIFO overflow, so stop conversion */
5713 spin_lock(&ai_context->int_lock);
5714 tmp = me4000_inl(ai_context->ctrl_reg);
5716 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5717 outl(tmp, ai_context->ctrl_reg);
5718 spin_unlock(&ai_context->int_lock);
5719 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA)
5721 ME4000_AI_STATUS_BIT_HF_DATA)
5723 ME4000_AI_STATUS_BIT_EF_DATA)) {
5725 ("me4000_ai_isr():Fifo half full\n");
5726 c = ME4000_AI_FIFO_COUNT / 2;
5728 c = ai_context->sample_counter;
5730 ("me4000_ai_isr():Sample count values\n");
5734 ("me4000_ai_isr():Try to read %d values\n",
5738 c1 = me4000_space_to_end(ai_context->
5740 ME4000_AI_BUFFER_COUNT);
5742 ("me4000_ai_isr():Space to end = %d\n",
5749 ("me4000_ai_isr():Work done or buffer full\n");
5753 insw(ai_context->data_reg,
5754 ai_context->circ_buf.buf +
5755 ai_context->circ_buf.head, c1);
5756 ai_context->circ_buf.head =
5757 (ai_context->circ_buf.head +
5758 c1) & (ME4000_AI_BUFFER_COUNT - 1);
5763 ("me4000_ai_isr():Multiple data block available\n");
5765 /* Read status register to find out what happened */
5766 tmp = me4000_inl(ai_context->ctrl_reg);
5768 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5769 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5770 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5772 ("me4000_ai_isr():Fifo full\n");
5773 c = ME4000_AI_FIFO_COUNT;
5775 /* FIFO overflow, so stop conversion */
5776 spin_lock(&ai_context->int_lock);
5777 tmp = me4000_inl(ai_context->ctrl_reg);
5779 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5780 outl(tmp, ai_context->ctrl_reg);
5781 spin_unlock(&ai_context->int_lock);
5784 c1 = me4000_space_to_end
5785 (ai_context->circ_buf,
5786 ME4000_AI_BUFFER_COUNT);
5788 ("me4000_ai_isr():Space to end = %d\n",
5795 ("me4000_ai_isr():Work done or buffer full\n");
5799 insw(ai_context->data_reg,
5800 ai_context->circ_buf.buf +
5801 ai_context->circ_buf.head,
5803 ai_context->circ_buf.head =
5804 (ai_context->circ_buf.head +
5806 (ME4000_AI_BUFFER_COUNT -
5810 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA)
5812 ME4000_AI_STATUS_BIT_HF_DATA)
5814 ME4000_AI_STATUS_BIT_EF_DATA)) {
5816 ("me4000_ai_isr():Fifo half full\n");
5817 c = ME4000_AI_FIFO_COUNT / 2;
5820 c1 = me4000_space_to_end
5821 (ai_context->circ_buf,
5822 ME4000_AI_BUFFER_COUNT);
5824 ("me4000_ai_isr():Space to end = %d\n",
5831 ("me4000_ai_isr():Work done or buffer full\n");
5835 insw(ai_context->data_reg,
5836 ai_context->circ_buf.buf +
5837 ai_context->circ_buf.head,
5839 ai_context->circ_buf.head =
5840 (ai_context->circ_buf.head +
5842 (ME4000_AI_BUFFER_COUNT -
5847 /* Poll data until fifo empty */
5849 (i < ME4000_AI_FIFO_COUNT / 2)
5850 && (inl(ai_context->ctrl_reg) &
5851 ME4000_AI_STATUS_BIT_EF_DATA);
5853 if (me4000_space_to_end
5854 (ai_context->circ_buf,
5855 ME4000_AI_BUFFER_COUNT)) {
5856 *(ai_context->circ_buf.
5858 ai_context->circ_buf.
5860 inw(ai_context->data_reg);
5861 ai_context->circ_buf.
5866 (ME4000_AI_BUFFER_COUNT
5872 ("me4000_ai_isr():%d values read\n",
5878 /* Work is done, so reset the interrupt */
5880 ("me4000_ai_isr():reset interrupt from sample counter\n");
5881 spin_lock(&ai_context->int_lock);
5882 tmp = me4000_inl(ai_context->ctrl_reg);
5883 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
5884 me4000_outl(tmp, ai_context->ctrl_reg);
5885 tmp &= ~ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
5886 me4000_outl(tmp, ai_context->ctrl_reg);
5887 spin_unlock(&ai_context->int_lock);
5890 /* Values are now available, so wake up waiting process */
5891 if (me4000_buf_count(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
5892 ISR_PDEBUG("me4000_ai_isr():Wake up waiting process\n");
5893 wake_up_interruptible(&(ai_context->wait_queue));
5896 /* If there is no space left in the buffer, disable interrupts */
5897 spin_lock(&ai_context->int_lock);
5898 if (!me4000_buf_space(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
5900 ("me4000_ai_isr():Disable Interrupt because no space left in buffer\n");
5901 tmp = me4000_inl(ai_context->ctrl_reg);
5903 ~(ME4000_AI_CTRL_BIT_SC_IRQ | ME4000_AI_CTRL_BIT_HF_IRQ |
5904 ME4000_AI_CTRL_BIT_LE_IRQ);
5905 me4000_outl(tmp, ai_context->ctrl_reg);
5907 spin_unlock(&ai_context->int_lock);
5909 #ifdef ME4000_ISR_DEBUG
5911 printk(KERN_ERR "ME4000:me4000_ai_isr():Time lapse = %lu\n",
5918 static irqreturn_t me4000_ext_int_isr(int irq, void *dev_id)
5920 struct me4000_ext_int_context *ext_int_context;
5923 ISR_PDEBUG("me4000_ext_int_isr() is executed\n");
5925 ext_int_context = dev_id;
5927 /* Check if irq number is right */
5928 if (irq != ext_int_context->irq) {
5929 ISR_PDEBUG("me4000_ext_int_isr():incorrect interrupt num: %d\n",
5934 if (me4000_inl(ext_int_context->irq_status_reg) &
5935 ME4000_IRQ_STATUS_BIT_EX) {
5936 ISR_PDEBUG("me4000_ext_int_isr():External interrupt occured\n");
5937 tmp = me4000_inl(ext_int_context->ctrl_reg);
5938 tmp |= ME4000_AI_CTRL_BIT_EX_IRQ_RESET;
5939 me4000_outl(tmp, ext_int_context->ctrl_reg);
5940 tmp &= ~ME4000_AI_CTRL_BIT_EX_IRQ_RESET;
5941 me4000_outl(tmp, ext_int_context->ctrl_reg);
5943 ext_int_context->int_count++;
5945 if (ext_int_context->fasync_ptr) {
5947 ("me2600_ext_int_isr():Send signal to process\n");
5948 kill_fasync(&ext_int_context->fasync_ptr, SIGIO,
5956 static void __exit me4000_module_exit(void)
5958 struct list_head *board_p;
5959 struct me4000_info *board_info;
5961 CALL_PDEBUG("cleanup_module() is executed\n");
5963 unregister_chrdev(me4000_ext_int_major_driver_no, ME4000_EXT_INT_NAME);
5965 unregister_chrdev(me4000_cnt_major_driver_no, ME4000_CNT_NAME);
5967 unregister_chrdev(me4000_dio_major_driver_no, ME4000_DIO_NAME);
5969 unregister_chrdev(me4000_ai_major_driver_no, ME4000_AI_NAME);
5971 unregister_chrdev(me4000_ao_major_driver_no, ME4000_AO_NAME);
5973 remove_proc_entry("me4000", NULL);
5975 pci_unregister_driver(&me4000_driver);
5977 /* Reset the boards */
5978 for (board_p = me4000_board_info_list.next;
5979 board_p != &me4000_board_info_list; board_p = board_p->next) {
5980 board_info = list_entry(board_p, struct me4000_info, list);
5981 me4000_reset_board(board_info);
5984 clear_board_info_list();
5987 module_exit(me4000_module_exit);
5989 static int me4000_read_procmem(char *buf, char **start, off_t offset, int count,
5990 int *eof, void *data)
5993 int limit = count - 1000;
5994 struct me4000_info *board_info;
5995 struct list_head *ptr;
5997 len += sprintf(buf + len, "\nME4000 DRIVER VERSION %X.%X.%X\n\n",
5998 (ME4000_DRIVER_VERSION & 0xFF0000) >> 16,
5999 (ME4000_DRIVER_VERSION & 0xFF00) >> 8,
6000 (ME4000_DRIVER_VERSION & 0xFF));
6002 /* Search for the board context */
6003 for (ptr = me4000_board_info_list.next;
6004 (ptr != &me4000_board_info_list) && (len < limit);
6006 board_info = list_entry(ptr, struct me4000_info, list);
6009 sprintf(buf + len, "Board number %d:\n",
6010 board_info->board_count);
6011 len += sprintf(buf + len, "---------------\n");
6013 sprintf(buf + len, "PLX base register = 0x%lX\n",
6014 board_info->plx_regbase);
6016 sprintf(buf + len, "PLX base register size = 0x%X\n",
6017 (unsigned int)board_info->plx_regbase_size);
6019 sprintf(buf + len, "ME4000 base register = 0x%X\n",
6020 (unsigned int)board_info->me4000_regbase);
6022 sprintf(buf + len, "ME4000 base register size = 0x%X\n",
6023 (unsigned int)board_info->me4000_regbase_size);
6025 sprintf(buf + len, "Serial number = 0x%X\n",
6026 board_info->serial_no);
6028 sprintf(buf + len, "Hardware revision = 0x%X\n",
6029 board_info->hw_revision);
6031 sprintf(buf + len, "Vendor id = 0x%X\n",
6032 board_info->vendor_id);
6034 sprintf(buf + len, "Device id = 0x%X\n",
6035 board_info->device_id);
6037 sprintf(buf + len, "PCI bus number = %d\n",
6038 board_info->pci_bus_no);
6040 sprintf(buf + len, "PCI device number = %d\n",
6041 board_info->pci_dev_no);
6043 sprintf(buf + len, "PCI function number = %d\n",
6044 board_info->pci_func_no);
6045 len += sprintf(buf + len, "IRQ = %u\n", board_info->irq);
6048 "Count of interrupts since module was loaded = %d\n",
6049 board_info->irq_count);
6052 sprintf(buf + len, "Count of analog outputs = %d\n",
6053 board_info->board_p->ao.count);
6055 sprintf(buf + len, "Count of analog output fifos = %d\n",
6056 board_info->board_p->ao.fifo_count);
6059 sprintf(buf + len, "Count of analog inputs = %d\n",
6060 board_info->board_p->ai.count);
6063 "Count of sample and hold devices for analog input = %d\n",
6064 board_info->board_p->ai.sh_count);
6067 "Analog external trigger available for analog input = %d\n",
6068 board_info->board_p->ai.ex_trig_analog);
6071 sprintf(buf + len, "Count of digital ports = %d\n",
6072 board_info->board_p->dio.count);
6075 sprintf(buf + len, "Count of counter devices = %d\n",
6076 board_info->board_p->cnt.count);
6078 sprintf(buf + len, "AI control register = 0x%08X\n",
6079 inl(board_info->me4000_regbase +
6080 ME4000_AI_CTRL_REG));
6082 len += sprintf(buf + len, "AO 0 control register = 0x%08X\n",
6083 inl(board_info->me4000_regbase +
6084 ME4000_AO_00_CTRL_REG));
6086 sprintf(buf + len, "AO 0 status register = 0x%08X\n",
6087 inl(board_info->me4000_regbase +
6088 ME4000_AO_00_STATUS_REG));
6090 sprintf(buf + len, "AO 1 control register = 0x%08X\n",
6091 inl(board_info->me4000_regbase +
6092 ME4000_AO_01_CTRL_REG));
6094 sprintf(buf + len, "AO 1 status register = 0x%08X\n",
6095 inl(board_info->me4000_regbase +
6096 ME4000_AO_01_STATUS_REG));
6098 sprintf(buf + len, "AO 2 control register = 0x%08X\n",
6099 inl(board_info->me4000_regbase +
6100 ME4000_AO_02_CTRL_REG));
6102 sprintf(buf + len, "AO 2 status register = 0x%08X\n",
6103 inl(board_info->me4000_regbase +
6104 ME4000_AO_02_STATUS_REG));
6106 sprintf(buf + len, "AO 3 control register = 0x%08X\n",
6107 inl(board_info->me4000_regbase +
6108 ME4000_AO_03_CTRL_REG));
6110 sprintf(buf + len, "AO 3 status register = 0x%08X\n",
6111 inl(board_info->me4000_regbase +
6112 ME4000_AO_03_STATUS_REG));