1 /* Device driver for Meilhaus ME-4000 board family.
2 * ================================================
4 * Copyright (C) 2003 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Author: Guenter Gebhardt <g.gebhardt@meilhaus.de>
23 #include <linux/module.h>
25 #include <linux/sched.h>
26 #include <linux/interrupt.h>
27 #include <linux/pci.h>
28 #include <linux/errno.h>
29 #include <linux/delay.h>
31 #include <linux/unistd.h>
32 #include <linux/list.h>
33 #include <linux/proc_fs.h>
34 #include <linux/types.h>
35 #include <linux/poll.h>
36 #include <linux/vmalloc.h>
37 #include <linux/slab.h>
38 #include <asm/pgtable.h>
39 #include <linux/uaccess.h>
41 #include <asm/system.h>
43 /* Include-File for the Meilhaus ME-4000 I/O board */
45 #include "me4000_firmware.h"
46 #include "me4610_firmware.h"
48 /* Administrative stuff for modinfo */
49 MODULE_AUTHOR("Guenter Gebhardt <g.gebhardt@meilhaus.de>");
51 ("Device Driver Module for Meilhaus ME-4000 boards version 1.0.5");
52 MODULE_SUPPORTED_DEVICE("Meilhaus ME-4000 Multi I/O boards");
53 MODULE_LICENSE("GPL");
55 /* Board specific data are kept in a global list */
56 static LIST_HEAD(me4000_board_info_list);
58 /* Major Device Numbers. 0 means to get it automatically from the System */
59 static int me4000_ao_major_driver_no;
60 static int me4000_ai_major_driver_no;
61 static int me4000_dio_major_driver_no;
62 static int me4000_cnt_major_driver_no;
63 static int me4000_ext_int_major_driver_no;
65 /* Let the user specify a custom major driver number */
66 module_param(me4000_ao_major_driver_no, int, 0);
67 MODULE_PARM_DESC(me4000_ao_major_driver_no,
68 "Major driver number for analog output (default 0)");
70 module_param(me4000_ai_major_driver_no, int, 0);
71 MODULE_PARM_DESC(me4000_ai_major_driver_no,
72 "Major driver number for analog input (default 0)");
74 module_param(me4000_dio_major_driver_no, int, 0);
75 MODULE_PARM_DESC(me4000_dio_major_driver_no,
76 "Major driver number digital I/O (default 0)");
78 module_param(me4000_cnt_major_driver_no, int, 0);
79 MODULE_PARM_DESC(me4000_cnt_major_driver_no,
80 "Major driver number for counter (default 0)");
82 module_param(me4000_ext_int_major_driver_no, int, 0);
83 MODULE_PARM_DESC(me4000_ext_int_major_driver_no,
84 "Major driver number for external interrupt (default 0)");
86 /*-----------------------------------------------------------------------------
87 Board detection and initialization
88 ---------------------------------------------------------------------------*/
89 static int me4000_probe(struct pci_dev *dev, const struct pci_device_id *id);
90 static int me4000_xilinx_download(struct me4000_info *);
91 static int me4000_reset_board(struct me4000_info *);
93 static void clear_board_info_list(void);
94 static void release_ao_contexts(struct me4000_info *board_info);
95 /*-----------------------------------------------------------------------------
96 Stuff used by all device parts
97 ---------------------------------------------------------------------------*/
98 static int me4000_open(struct inode *, struct file *);
99 static int me4000_release(struct inode *, struct file *);
101 static int me4000_get_user_info(struct me4000_user_info *,
102 struct me4000_info *board_info);
103 static int me4000_read_procmem(char *, char **, off_t, int, int *, void *);
105 /*-----------------------------------------------------------------------------
107 ---------------------------------------------------------------------------*/
108 static ssize_t me4000_ao_write_sing(struct file *, const char *, size_t,
110 static ssize_t me4000_ao_write_wrap(struct file *, const char *, size_t,
112 static ssize_t me4000_ao_write_cont(struct file *, const char *, size_t,
115 static int me4000_ao_ioctl_sing(struct inode *, struct file *, unsigned int,
117 static int me4000_ao_ioctl_wrap(struct inode *, struct file *, unsigned int,
119 static int me4000_ao_ioctl_cont(struct inode *, struct file *, unsigned int,
122 static unsigned int me4000_ao_poll_cont(struct file *, poll_table *);
123 static int me4000_ao_fsync_cont(struct file *, struct dentry *, int);
125 static int me4000_ao_start(unsigned long *, struct me4000_ao_context *);
126 static int me4000_ao_stop(struct me4000_ao_context *);
127 static int me4000_ao_immediate_stop(struct me4000_ao_context *);
128 static int me4000_ao_timer_set_divisor(u32 *, struct me4000_ao_context *);
129 static int me4000_ao_preload(struct me4000_ao_context *);
130 static int me4000_ao_preload_update(struct me4000_ao_context *);
131 static int me4000_ao_ex_trig_set_edge(int *, struct me4000_ao_context *);
132 static int me4000_ao_ex_trig_enable(struct me4000_ao_context *);
133 static int me4000_ao_ex_trig_disable(struct me4000_ao_context *);
134 static int me4000_ao_prepare(struct me4000_ao_context *ao_info);
135 static int me4000_ao_reset(struct me4000_ao_context *ao_info);
136 static int me4000_ao_enable_do(struct me4000_ao_context *);
137 static int me4000_ao_disable_do(struct me4000_ao_context *);
138 static int me4000_ao_fsm_state(int *, struct me4000_ao_context *);
140 static int me4000_ao_simultaneous_ex_trig(struct me4000_ao_context *ao_context);
141 static int me4000_ao_simultaneous_sw(struct me4000_ao_context *ao_context);
142 static int me4000_ao_simultaneous_disable(struct me4000_ao_context *ao_context);
143 static int me4000_ao_simultaneous_update(
144 struct me4000_ao_channel_list *channels,
145 struct me4000_ao_context *ao_context);
147 static int me4000_ao_synchronous_ex_trig(struct me4000_ao_context *ao_context);
148 static int me4000_ao_synchronous_sw(struct me4000_ao_context *ao_context);
149 static int me4000_ao_synchronous_disable(struct me4000_ao_context *ao_context);
151 static int me4000_ao_ex_trig_timeout(unsigned long *arg,
152 struct me4000_ao_context *ao_context);
153 static int me4000_ao_get_free_buffer(unsigned long *arg,
154 struct me4000_ao_context *ao_context);
156 /*-----------------------------------------------------------------------------
158 ---------------------------------------------------------------------------*/
159 static int me4000_ai_single(struct me4000_ai_single *,
160 struct me4000_ai_context *);
161 static int me4000_ai_ioctl_sing(struct inode *, struct file *, unsigned int,
164 static ssize_t me4000_ai_read(struct file *, char *, size_t, loff_t *);
165 static int me4000_ai_ioctl_sw(struct inode *, struct file *, unsigned int,
167 static unsigned int me4000_ai_poll(struct file *, poll_table *);
168 static int me4000_ai_fasync(int fd, struct file *file_p, int mode);
170 static int me4000_ai_ioctl_ext(struct inode *, struct file *, unsigned int,
173 static int me4000_ai_prepare(struct me4000_ai_context *ai_context);
174 static int me4000_ai_reset(struct me4000_ai_context *ai_context);
175 static int me4000_ai_config(struct me4000_ai_config *,
176 struct me4000_ai_context *);
177 static int me4000_ai_start(struct me4000_ai_context *);
178 static int me4000_ai_start_ex(unsigned long *, struct me4000_ai_context *);
179 static int me4000_ai_stop(struct me4000_ai_context *);
180 static int me4000_ai_immediate_stop(struct me4000_ai_context *);
181 static int me4000_ai_ex_trig_enable(struct me4000_ai_context *);
182 static int me4000_ai_ex_trig_disable(struct me4000_ai_context *);
183 static int me4000_ai_ex_trig_setup(struct me4000_ai_trigger *,
184 struct me4000_ai_context *);
185 static int me4000_ai_sc_setup(struct me4000_ai_sc *arg,
186 struct me4000_ai_context *ai_context);
187 static int me4000_ai_offset_enable(struct me4000_ai_context *ai_context);
188 static int me4000_ai_offset_disable(struct me4000_ai_context *ai_context);
189 static int me4000_ai_fullscale_enable(struct me4000_ai_context *ai_context);
190 static int me4000_ai_fullscale_disable(struct me4000_ai_context *ai_context);
191 static int me4000_ai_fsm_state(int *arg, struct me4000_ai_context *ai_context);
192 static int me4000_ai_get_count_buffer(unsigned long *arg,
193 struct me4000_ai_context *ai_context);
195 /*-----------------------------------------------------------------------------
197 ---------------------------------------------------------------------------*/
198 static int me4000_eeprom_read(struct me4000_eeprom *arg,
199 struct me4000_ai_context *ai_context);
200 static int me4000_eeprom_write(struct me4000_eeprom *arg,
201 struct me4000_ai_context *ai_context);
203 /*-----------------------------------------------------------------------------
205 ---------------------------------------------------------------------------*/
206 static int me4000_dio_ioctl(struct inode *, struct file *, unsigned int,
208 static int me4000_dio_config(struct me4000_dio_config *,
209 struct me4000_dio_context *);
210 static int me4000_dio_get_byte(struct me4000_dio_byte *,
211 struct me4000_dio_context *);
212 static int me4000_dio_set_byte(struct me4000_dio_byte *,
213 struct me4000_dio_context *);
214 static int me4000_dio_reset(struct me4000_dio_context *);
216 /*-----------------------------------------------------------------------------
218 ---------------------------------------------------------------------------*/
219 static int me4000_cnt_ioctl(struct inode *, struct file *, unsigned int,
221 static int me4000_cnt_config(struct me4000_cnt_config *,
222 struct me4000_cnt_context *);
223 static int me4000_cnt_read(struct me4000_cnt *, struct me4000_cnt_context *);
224 static int me4000_cnt_write(struct me4000_cnt *, struct me4000_cnt_context *);
225 static int me4000_cnt_reset(struct me4000_cnt_context *);
227 /*-----------------------------------------------------------------------------
228 External interrupt routines
229 ---------------------------------------------------------------------------*/
230 static int me4000_ext_int_ioctl(struct inode *, struct file *, unsigned int,
232 static int me4000_ext_int_enable(struct me4000_ext_int_context *);
233 static int me4000_ext_int_disable(struct me4000_ext_int_context *);
234 static int me4000_ext_int_count(unsigned long *arg,
235 struct me4000_ext_int_context *ext_int_context);
236 static int me4000_ext_int_fasync(int fd, struct file *file_ptr, int mode);
238 /*-----------------------------------------------------------------------------
239 The interrupt service routines
240 ---------------------------------------------------------------------------*/
241 static irqreturn_t me4000_ao_isr(int, void *);
242 static irqreturn_t me4000_ai_isr(int, void *);
243 static irqreturn_t me4000_ext_int_isr(int, void *);
245 /*-----------------------------------------------------------------------------
247 ---------------------------------------------------------------------------*/
249 static inline int me4000_buf_count(struct me4000_circ_buf buf, int size)
251 return (buf.head - buf.tail) & (size - 1);
254 static inline int me4000_buf_space(struct me4000_circ_buf buf, int size)
256 return (buf.tail - (buf.head + 1)) & (size - 1);
259 static inline int me4000_values_to_end(struct me4000_circ_buf buf, int size)
263 end = size - buf.tail;
264 n = (buf.head + end) & (size - 1);
265 return (n < end) ? n : end;
268 static inline int me4000_space_to_end(struct me4000_circ_buf buf, int size)
273 end = size - 1 - buf.head;
274 n = (end + buf.tail) & (size - 1);
275 return (n <= end) ? n : (end + 1);
278 static inline void me4000_outb(unsigned char value, unsigned long port)
280 PORT_PDEBUG("--> 0x%02X port 0x%04lX\n", value, port);
284 static inline void me4000_outl(unsigned long value, unsigned long port)
286 PORT_PDEBUG("--> 0x%08lX port 0x%04lX\n", value, port);
290 static inline unsigned long me4000_inl(unsigned long port)
294 PORT_PDEBUG("<-- 0x%08lX port 0x%04lX\n", value, port);
298 static inline unsigned char me4000_inb(unsigned long port)
302 PORT_PDEBUG("<-- 0x%08X port 0x%04lX\n", value, port);
306 static struct pci_driver me4000_driver = {
308 .id_table = me4000_pci_table,
309 .probe = me4000_probe
312 static const 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 const 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 const 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 const struct file_operations me4000_ai_fops_sing = {
339 .owner = THIS_MODULE,
340 .ioctl = me4000_ai_ioctl_sing,
342 .release = me4000_release,
345 static const 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 const 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 const 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 const 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 const struct file_operations me4000_dio_fops = {
383 .owner = THIS_MODULE,
384 .ioctl = me4000_dio_ioctl,
386 .release = me4000_release,
389 static const struct file_operations me4000_cnt_fops = {
390 .owner = THIS_MODULE,
391 .ioctl = me4000_cnt_ioctl,
393 .release = me4000_release,
396 static const 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 const struct file_operations *me4000_ao_fops_array[] = {
405 /* single operations */
406 &me4000_ao_fops_sing,
407 /* wraparound operations */
408 &me4000_ao_fops_wrap,
409 /* continuous operations */
410 &me4000_ao_fops_cont,
413 static const struct file_operations *me4000_ai_fops_array[] = {
414 /* single operations */
415 &me4000_ai_fops_sing,
416 /* continuous operations with software start */
417 &me4000_ai_fops_cont_sw,
418 /* continuous operations with external trigger */
419 &me4000_ai_fops_cont_et,
420 /* sample values by external trigger */
421 &me4000_ai_fops_cont_et_value,
422 /* work through one channel list by external trigger */
423 &me4000_ai_fops_cont_et_chanlist,
426 static int __init me4000_init_module(void)
430 CALL_PDEBUG("init_module() is executed\n");
432 /* Register driver capabilities */
433 result = pci_register_driver(&me4000_driver);
434 PDEBUG("init_module():%d devices detected\n", result);
436 printk(KERN_ERR "ME4000:init_module():Can't register driver\n");
440 /* Allocate major number for analog output */
442 register_chrdev(me4000_ao_major_driver_no, ME4000_AO_NAME,
443 &me4000_ao_fops_sing);
445 printk(KERN_ERR "ME4000:init_module():Can't get AO major no\n");
448 me4000_ao_major_driver_no = result;
450 PDEBUG("init_module():Major driver number for AO = %ld\n",
451 me4000_ao_major_driver_no);
453 /* Allocate major number for analog input */
455 register_chrdev(me4000_ai_major_driver_no, ME4000_AI_NAME,
456 &me4000_ai_fops_sing);
458 printk(KERN_ERR "ME4000:init_module():Can't get AI major no\n");
461 me4000_ai_major_driver_no = result;
463 PDEBUG("init_module():Major driver number for AI = %ld\n",
464 me4000_ai_major_driver_no);
466 /* Allocate major number for digital I/O */
468 register_chrdev(me4000_dio_major_driver_no, ME4000_DIO_NAME,
472 "ME4000:init_module():Can't get DIO major no\n");
475 me4000_dio_major_driver_no = result;
477 PDEBUG("init_module():Major driver number for DIO = %ld\n",
478 me4000_dio_major_driver_no);
480 /* Allocate major number for counter */
482 register_chrdev(me4000_cnt_major_driver_no, ME4000_CNT_NAME,
486 "ME4000:init_module():Can't get CNT major no\n");
489 me4000_cnt_major_driver_no = result;
491 PDEBUG("init_module():Major driver number for CNT = %ld\n",
492 me4000_cnt_major_driver_no);
494 /* Allocate major number for external interrupt */
496 register_chrdev(me4000_ext_int_major_driver_no, ME4000_EXT_INT_NAME,
497 &me4000_ext_int_fops);
500 "ME4000:init_module():Can't get major no for external interrupt\n");
503 me4000_ext_int_major_driver_no = result;
506 ("init_module():Major driver number for external interrupt = %ld\n",
507 me4000_ext_int_major_driver_no);
509 /* Create the /proc/me4000 entry */
510 if (!create_proc_read_entry
511 ("me4000", 0, NULL, me4000_read_procmem, NULL)) {
514 "ME4000:init_module():Can't create proc entry\n");
521 unregister_chrdev(me4000_ext_int_major_driver_no, ME4000_EXT_INT_NAME);
524 unregister_chrdev(me4000_cnt_major_driver_no, ME4000_CNT_NAME);
527 unregister_chrdev(me4000_dio_major_driver_no, ME4000_DIO_NAME);
530 unregister_chrdev(me4000_ai_major_driver_no, ME4000_AI_NAME);
533 unregister_chrdev(me4000_ao_major_driver_no, ME4000_AO_NAME);
536 pci_unregister_driver(&me4000_driver);
537 clear_board_info_list();
543 module_init(me4000_init_module);
545 static void clear_board_info_list(void)
547 struct me4000_info *board_info, *board_info_safe;
548 struct me4000_ao_context *ao_context, *ao_context_safe;
550 /* Clear context lists */
551 list_for_each_entry(board_info, &me4000_board_info_list, list) {
552 /* Clear analog output context list */
553 list_for_each_entry_safe(ao_context, ao_context_safe,
554 &board_info->ao_context_list, list) {
555 me4000_ao_reset(ao_context);
556 free_irq(ao_context->irq, ao_context);
557 kfree(ao_context->circ_buf.buf);
558 list_del(&ao_context->list);
562 /* Clear analog input context */
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 list_for_each_entry_safe(board_info, board_info_safe,
578 &me4000_board_info_list, list) {
579 pci_release_regions(board_info->pci_dev_p);
580 list_del(&board_info->list);
585 static int get_registers(struct pci_dev *dev, struct me4000_info *board_info)
588 /*--------------------------- plx regbase ---------------------------------*/
590 board_info->plx_regbase = pci_resource_start(dev, 1);
591 if (board_info->plx_regbase == 0) {
593 "ME4000:get_registers():PCI base address 1 is not available\n");
596 board_info->plx_regbase_size = pci_resource_len(dev, 1);
599 ("get_registers():PLX configuration registers at address 0x%4lX [0x%4lX]\n",
600 board_info->plx_regbase, board_info->plx_regbase_size);
602 /*--------------------------- me4000 regbase ------------------------------*/
604 board_info->me4000_regbase = pci_resource_start(dev, 2);
605 if (board_info->me4000_regbase == 0) {
607 "ME4000:get_registers():PCI base address 2 is not available\n");
610 board_info->me4000_regbase_size = pci_resource_len(dev, 2);
612 PDEBUG("get_registers():ME4000 registers at address 0x%4lX [0x%4lX]\n",
613 board_info->me4000_regbase, board_info->me4000_regbase_size);
615 /*--------------------------- timer regbase ------------------------------*/
617 board_info->timer_regbase = pci_resource_start(dev, 3);
618 if (board_info->timer_regbase == 0) {
620 "ME4000:get_registers():PCI base address 3 is not available\n");
623 board_info->timer_regbase_size = pci_resource_len(dev, 3);
625 PDEBUG("get_registers():Timer registers at address 0x%4lX [0x%4lX]\n",
626 board_info->timer_regbase, board_info->timer_regbase_size);
628 /*--------------------------- program regbase ------------------------------*/
630 board_info->program_regbase = pci_resource_start(dev, 5);
631 if (board_info->program_regbase == 0) {
633 "get_registers():ME4000:PCI base address 5 is not available\n");
636 board_info->program_regbase_size = pci_resource_len(dev, 5);
638 PDEBUG("get_registers():Program registers at address 0x%4lX [0x%4lX]\n",
639 board_info->program_regbase, board_info->program_regbase_size);
644 static int init_board_info(struct pci_dev *pci_dev_p,
645 struct me4000_info *board_info)
649 struct list_head *board_p;
650 board_info->pci_dev_p = pci_dev_p;
652 for (i = 0; i < ARRAY_SIZE(me4000_boards); i++) {
653 if (me4000_boards[i].device_id == pci_dev_p->device) {
654 board_info->board_p = &me4000_boards[i];
658 if (i == ARRAY_SIZE(me4000_boards)) {
660 "ME4000:init_board_info():Device ID not valid\n");
664 /* Get the index of the board in the global list */
666 list_for_each(board_p, &me4000_board_info_list) {
667 if (board_p == &board_info->list) {
668 board_info->board_count = i;
673 if (board_p == &me4000_board_info_list) {
675 "ME4000:init_board_info():Cannot get index of board\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 me4000_ao_context *ao_context, *ao_context_safe;
868 /* Clear analog output context list */
869 list_for_each_entry_safe(ao_context, ao_context_safe,
870 &board_info->ao_context_list, list) {
871 free_irq(ao_context->irq, ao_context);
872 kfree(ao_context->circ_buf.buf);
873 list_del(&ao_context->list);
878 static int alloc_ai_context(struct me4000_info *info)
880 struct me4000_ai_context *ai_context;
882 if (info->board_p->ai.count) {
883 ai_context = kzalloc(sizeof(struct me4000_ai_context),
887 "ME4000:alloc_ai_context():Can't get memory for ai context\n");
891 info->ai_context = ai_context;
893 spin_lock_init(&ai_context->use_lock);
894 spin_lock_init(&ai_context->int_lock);
895 ai_context->number = 0;
896 ai_context->irq = info->irq;
897 init_waitqueue_head(&ai_context->wait_queue);
898 ai_context->board_info = info;
900 ai_context->ctrl_reg =
901 info->me4000_regbase + ME4000_AI_CTRL_REG;
902 ai_context->status_reg =
903 info->me4000_regbase + ME4000_AI_STATUS_REG;
904 ai_context->channel_list_reg =
905 info->me4000_regbase + ME4000_AI_CHANNEL_LIST_REG;
906 ai_context->data_reg =
907 info->me4000_regbase + ME4000_AI_DATA_REG;
908 ai_context->chan_timer_reg =
909 info->me4000_regbase + ME4000_AI_CHAN_TIMER_REG;
910 ai_context->chan_pre_timer_reg =
911 info->me4000_regbase + ME4000_AI_CHAN_PRE_TIMER_REG;
912 ai_context->scan_timer_low_reg =
913 info->me4000_regbase + ME4000_AI_SCAN_TIMER_LOW_REG;
914 ai_context->scan_timer_high_reg =
915 info->me4000_regbase + ME4000_AI_SCAN_TIMER_HIGH_REG;
916 ai_context->scan_pre_timer_low_reg =
917 info->me4000_regbase + ME4000_AI_SCAN_PRE_TIMER_LOW_REG;
918 ai_context->scan_pre_timer_high_reg =
919 info->me4000_regbase + ME4000_AI_SCAN_PRE_TIMER_HIGH_REG;
920 ai_context->start_reg =
921 info->me4000_regbase + ME4000_AI_START_REG;
922 ai_context->irq_status_reg =
923 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
924 ai_context->sample_counter_reg =
925 info->me4000_regbase + ME4000_AI_SAMPLE_COUNTER_REG;
931 static int alloc_dio_context(struct me4000_info *info)
933 struct me4000_dio_context *dio_context;
935 if (info->board_p->dio.count) {
936 dio_context = kzalloc(sizeof(struct me4000_dio_context),
940 "ME4000:alloc_dio_context():Can't get memory for dio context\n");
944 info->dio_context = dio_context;
946 spin_lock_init(&dio_context->use_lock);
947 dio_context->board_info = info;
949 dio_context->dio_count = info->board_p->dio.count;
951 dio_context->dir_reg =
952 info->me4000_regbase + ME4000_DIO_DIR_REG;
953 dio_context->ctrl_reg =
954 info->me4000_regbase + ME4000_DIO_CTRL_REG;
955 dio_context->port_0_reg =
956 info->me4000_regbase + ME4000_DIO_PORT_0_REG;
957 dio_context->port_1_reg =
958 info->me4000_regbase + ME4000_DIO_PORT_1_REG;
959 dio_context->port_2_reg =
960 info->me4000_regbase + ME4000_DIO_PORT_2_REG;
961 dio_context->port_3_reg =
962 info->me4000_regbase + ME4000_DIO_PORT_3_REG;
968 static int alloc_cnt_context(struct me4000_info *info)
970 struct me4000_cnt_context *cnt_context;
972 if (info->board_p->cnt.count) {
973 cnt_context = kzalloc(sizeof(struct me4000_cnt_context),
977 "ME4000:alloc_cnt_context():Can't get memory for cnt context\n");
981 info->cnt_context = cnt_context;
983 spin_lock_init(&cnt_context->use_lock);
984 cnt_context->board_info = info;
986 cnt_context->ctrl_reg =
987 info->timer_regbase + ME4000_CNT_CTRL_REG;
988 cnt_context->counter_0_reg =
989 info->timer_regbase + ME4000_CNT_COUNTER_0_REG;
990 cnt_context->counter_1_reg =
991 info->timer_regbase + ME4000_CNT_COUNTER_1_REG;
992 cnt_context->counter_2_reg =
993 info->timer_regbase + ME4000_CNT_COUNTER_2_REG;
999 static int alloc_ext_int_context(struct me4000_info *info)
1001 struct me4000_ext_int_context *ext_int_context;
1003 if (info->board_p->cnt.count) {
1005 kzalloc(sizeof(struct me4000_ext_int_context), GFP_KERNEL);
1006 if (!ext_int_context) {
1008 "ME4000:alloc_ext_int_context():Can't get memory for cnt context\n");
1012 info->ext_int_context = ext_int_context;
1014 spin_lock_init(&ext_int_context->use_lock);
1015 ext_int_context->board_info = info;
1017 ext_int_context->fasync_ptr = NULL;
1018 ext_int_context->irq = info->irq;
1020 ext_int_context->ctrl_reg =
1021 info->me4000_regbase + ME4000_AI_CTRL_REG;
1022 ext_int_context->irq_status_reg =
1023 info->me4000_regbase + ME4000_IRQ_STATUS_REG;
1029 static int me4000_probe(struct pci_dev *dev, const struct pci_device_id *id)
1032 struct me4000_info *board_info;
1034 CALL_PDEBUG("me4000_probe() is executed\n");
1036 /* Allocate structure for board context */
1037 board_info = kzalloc(sizeof(struct me4000_info), GFP_KERNEL);
1040 "ME4000:Can't get memory for board info structure\n");
1045 /* Add to global linked list */
1046 list_add_tail(&board_info->list, &me4000_board_info_list);
1048 /* Get the PCI base registers */
1049 result = get_registers(dev, board_info);
1051 printk(KERN_ERR "%s:Cannot get registers\n", __func__);
1055 /* Enable the device */
1056 result = pci_enable_device(dev);
1058 printk(KERN_ERR "%s:Cannot enable PCI device\n", __func__);
1062 /* Request the PCI register regions */
1063 result = pci_request_regions(dev, ME4000_NAME);
1065 printk(KERN_ERR "%s:Cannot request I/O regions\n", __func__);
1069 /* Initialize board info */
1070 result = init_board_info(dev, board_info);
1072 printk(KERN_ERR "%s:Cannot init baord info\n", __func__);
1076 /* Download the xilinx firmware */
1077 result = me4000_xilinx_download(board_info);
1079 printk(KERN_ERR "%s:Can't download firmware\n", __func__);
1083 /* Make a hardware reset */
1084 result = me4000_reset_board(board_info);
1086 printk(KERN_ERR "%s :Can't reset board\n", __func__);
1090 /* Allocate analog output context structures */
1091 result = alloc_ao_contexts(board_info);
1093 printk(KERN_ERR "%s:Cannot allocate ao contexts\n", __func__);
1097 /* Allocate analog input context */
1098 result = alloc_ai_context(board_info);
1100 printk(KERN_ERR "%s:Cannot allocate ai context\n", __func__);
1104 /* Allocate digital I/O context */
1105 result = alloc_dio_context(board_info);
1107 printk(KERN_ERR "%s:Cannot allocate dio context\n", __func__);
1111 /* Allocate counter context */
1112 result = alloc_cnt_context(board_info);
1114 printk(KERN_ERR "%s:Cannot allocate cnt context\n", __func__);
1118 /* Allocate external interrupt context */
1119 result = alloc_ext_int_context(board_info);
1122 "%s:Cannot allocate ext_int context\n", __func__);
1129 kfree(board_info->cnt_context);
1132 kfree(board_info->dio_context);
1135 kfree(board_info->ai_context);
1138 release_ao_contexts(board_info);
1141 pci_release_regions(dev);
1144 list_del(&board_info->list);
1151 static int me4000_xilinx_download(struct me4000_info *info)
1156 unsigned char *firm;
1157 wait_queue_head_t queue;
1159 CALL_PDEBUG("me4000_xilinx_download() is executed\n");
1161 init_waitqueue_head(&queue);
1163 firm = (info->device_id == 0x4610) ? xilinx_firm_4610 : xilinx_firm;
1166 * Set PLX local interrupt 2 polarity to high.
1167 * Interrupt is thrown by init pin of xilinx.
1169 outl(0x10, info->plx_regbase + PLX_INTCSR);
1171 /* Set /CS and /WRITE of the Xilinx */
1172 value = inl(info->plx_regbase + PLX_ICR);
1174 outl(value, info->plx_regbase + PLX_ICR);
1176 /* Init Xilinx with CS1 */
1177 inb(info->program_regbase + 0xC8);
1179 /* Wait until /INIT pin is set */
1181 if (!(inl(info->plx_regbase + PLX_INTCSR) & 0x20)) {
1182 printk(KERN_ERR "%s:Can't init Xilinx\n", __func__);
1186 /* Reset /CS and /WRITE of the Xilinx */
1187 value = inl(info->plx_regbase + PLX_ICR);
1189 outl(value, info->plx_regbase + PLX_ICR);
1191 /* Download Xilinx firmware */
1192 size = (firm[0] << 24) + (firm[1] << 16) + (firm[2] << 8) + firm[3];
1195 for (idx = 0; idx < size; idx++) {
1196 outb(firm[16 + idx], info->program_regbase);
1200 /* Check if BUSY flag is low */
1201 if (inl(info->plx_regbase + PLX_ICR) & 0x20) {
1203 "%s:Xilinx is still busy (idx = %d)\n", __func__,
1209 PDEBUG("me4000_xilinx_download():%d bytes written\n", idx);
1211 /* If done flag is high download was successful */
1212 if (inl(info->plx_regbase + PLX_ICR) & 0x4) {
1213 PDEBUG("me4000_xilinx_download():Done flag is set\n");
1214 PDEBUG("me4000_xilinx_download():Download was successful\n");
1217 "ME4000:%s:DONE flag is not set\n", __func__);
1219 "ME4000:%s:Download not succesful\n", __func__);
1223 /* Set /CS and /WRITE */
1224 value = inl(info->plx_regbase + PLX_ICR);
1226 outl(value, info->plx_regbase + PLX_ICR);
1231 static int me4000_reset_board(struct me4000_info *info)
1235 CALL_PDEBUG("me4000_reset_board() is executed\n");
1237 /* Make a hardware reset */
1238 icr = me4000_inl(info->plx_regbase + PLX_ICR);
1240 me4000_outl(icr, info->plx_regbase + PLX_ICR);
1242 me4000_outl(icr, info->plx_regbase + PLX_ICR);
1244 /* Set both stop bits in the analog input control register */
1245 me4000_outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP | ME4000_AI_CTRL_BIT_STOP,
1246 info->me4000_regbase + ME4000_AI_CTRL_REG);
1248 /* Set both stop bits in the analog output control register */
1249 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1250 info->me4000_regbase + ME4000_AO_00_CTRL_REG);
1251 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1252 info->me4000_regbase + ME4000_AO_01_CTRL_REG);
1253 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1254 info->me4000_regbase + ME4000_AO_02_CTRL_REG);
1255 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP,
1256 info->me4000_regbase + ME4000_AO_03_CTRL_REG);
1258 /* 0x8000 to the DACs means an output voltage of 0V */
1259 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_00_SINGLE_REG);
1260 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_01_SINGLE_REG);
1261 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_02_SINGLE_REG);
1262 me4000_outl(0x8000, info->me4000_regbase + ME4000_AO_03_SINGLE_REG);
1264 /* Enable interrupts on the PLX */
1265 me4000_outl(0x43, info->plx_regbase + PLX_INTCSR);
1267 /* Set the adustment register for AO demux */
1268 me4000_outl(ME4000_AO_DEMUX_ADJUST_VALUE,
1269 info->me4000_regbase + ME4000_AO_DEMUX_ADJUST_REG);
1271 /* Set digital I/O direction for port 0 to output on isolated versions */
1272 if (!(me4000_inl(info->me4000_regbase + ME4000_DIO_DIR_REG) & 0x1))
1273 me4000_outl(0x1, info->me4000_regbase + ME4000_DIO_CTRL_REG);
1278 static int me4000_open(struct inode *inode_p, struct file *file_p)
1280 int board, dev, mode;
1283 struct list_head *ptr;
1284 struct me4000_info *board_info = NULL;
1285 struct me4000_ao_context *ao_context = NULL;
1286 struct me4000_ai_context *ai_context = NULL;
1287 struct me4000_dio_context *dio_context = NULL;
1288 struct me4000_cnt_context *cnt_context = NULL;
1289 struct me4000_ext_int_context *ext_int_context = NULL;
1291 CALL_PDEBUG("me4000_open() is executed\n");
1294 if (MAJOR(inode_p->i_rdev) == me4000_ao_major_driver_no) {
1295 board = AO_BOARD(inode_p->i_rdev);
1296 dev = AO_PORT(inode_p->i_rdev);
1297 mode = AO_MODE(inode_p->i_rdev);
1299 PDEBUG("me4000_open():board = %d ao = %d mode = %d\n", board,
1302 /* Search for the board context */
1304 list_for_each(ptr, &me4000_board_info_list) {
1309 board_info = list_entry(ptr, struct me4000_info, list);
1311 if (ptr == &me4000_board_info_list) {
1313 "ME4000:me4000_open():Board %d not in device list\n",
1318 /* Search for the dac context */
1320 list_for_each(ptr, &board_info->ao_context_list) {
1325 ao_context = list_entry(ptr, struct me4000_ao_context, list);
1327 if (ptr == &board_info->ao_context_list) {
1329 "ME4000:me4000_open():Device %d not in device list\n",
1334 /* Check if mode is valid */
1337 "ME4000:me4000_open():Mode is not valid\n");
1341 /* Check if mode is valid for this AO */
1342 if ((mode != ME4000_AO_CONV_MODE_SINGLE)
1343 && (dev >= board_info->board_p->ao.fifo_count)) {
1345 "ME4000:me4000_open():AO %d only in single mode available\n",
1350 /* Check if already opened */
1351 spin_lock(&ao_context->use_lock);
1352 if (ao_context->dac_in_use) {
1354 "ME4000:me4000_open():AO %d already in use\n",
1356 spin_unlock(&ao_context->use_lock);
1359 ao_context->dac_in_use = 1;
1360 spin_unlock(&ao_context->use_lock);
1362 ao_context->mode = mode;
1364 /* Hold the context in private data */
1365 file_p->private_data = ao_context;
1367 /* Set file operations pointer */
1368 file_p->f_op = me4000_ao_fops_array[mode];
1370 err = me4000_ao_prepare(ao_context);
1372 ao_context->dac_in_use = 0;
1377 else if (MAJOR(inode_p->i_rdev) == me4000_ai_major_driver_no) {
1378 board = AI_BOARD(inode_p->i_rdev);
1379 mode = AI_MODE(inode_p->i_rdev);
1381 PDEBUG("me4000_open():ai board = %d mode = %d\n", board, mode);
1383 /* Search for the board context */
1385 list_for_each(ptr, &me4000_board_info_list) {
1390 board_info = list_entry(ptr, struct me4000_info, list);
1392 if (ptr == &me4000_board_info_list) {
1394 "ME4000:me4000_open():Board %d not in device list\n",
1399 ai_context = board_info->ai_context;
1401 /* Check if mode is valid */
1404 "ME4000:me4000_open():Mode is not valid\n");
1408 /* Check if already opened */
1409 spin_lock(&ai_context->use_lock);
1410 if (ai_context->in_use) {
1412 "ME4000:me4000_open():AI already in use\n");
1413 spin_unlock(&ai_context->use_lock);
1416 ai_context->in_use = 1;
1417 spin_unlock(&ai_context->use_lock);
1419 ai_context->mode = mode;
1421 /* Hold the context in private data */
1422 file_p->private_data = ai_context;
1424 /* Set file operations pointer */
1425 file_p->f_op = me4000_ai_fops_array[mode];
1427 /* Prepare analog input */
1428 me4000_ai_prepare(ai_context);
1431 else if (MAJOR(inode_p->i_rdev) == me4000_dio_major_driver_no) {
1432 board = DIO_BOARD(inode_p->i_rdev);
1436 PDEBUG("me4000_open():board = %d\n", board);
1438 /* Search for the board context */
1439 list_for_each_entry(board_info, &me4000_board_info_list, list) {
1440 if (board_info->board_count == board)
1444 if (&board_info->list == &me4000_board_info_list) {
1446 "ME4000:me4000_open():Board %d not in device list\n",
1451 /* Search for the dio context */
1452 dio_context = board_info->dio_context;
1454 /* Check if already opened */
1455 spin_lock(&dio_context->use_lock);
1456 if (dio_context->in_use) {
1458 "ME4000:me4000_open():DIO already in use\n");
1459 spin_unlock(&dio_context->use_lock);
1462 dio_context->in_use = 1;
1463 spin_unlock(&dio_context->use_lock);
1465 /* Hold the context in private data */
1466 file_p->private_data = dio_context;
1468 /* Set file operations pointer to single functions */
1469 file_p->f_op = &me4000_dio_fops;
1471 /* me4000_dio_reset(dio_context); */
1474 else if (MAJOR(inode_p->i_rdev) == me4000_cnt_major_driver_no) {
1475 board = CNT_BOARD(inode_p->i_rdev);
1479 PDEBUG("me4000_open():board = %d\n", board);
1481 /* Search for the board context */
1482 list_for_each_entry(board_info, &me4000_board_info_list, list) {
1483 if (board_info->board_count == board)
1487 if (&board_info->list == &me4000_board_info_list) {
1489 "ME4000:me4000_open():Board %d not in device list\n",
1494 /* Get the cnt context */
1495 cnt_context = board_info->cnt_context;
1497 /* Check if already opened */
1498 spin_lock(&cnt_context->use_lock);
1499 if (cnt_context->in_use) {
1501 "ME4000:me4000_open():CNT already in use\n");
1502 spin_unlock(&cnt_context->use_lock);
1505 cnt_context->in_use = 1;
1506 spin_unlock(&cnt_context->use_lock);
1508 /* Hold the context in private data */
1509 file_p->private_data = cnt_context;
1511 /* Set file operations pointer to single functions */
1512 file_p->f_op = &me4000_cnt_fops;
1514 /* External Interrupt */
1515 else if (MAJOR(inode_p->i_rdev) == me4000_ext_int_major_driver_no) {
1516 board = EXT_INT_BOARD(inode_p->i_rdev);
1520 PDEBUG("me4000_open():board = %d\n", board);
1522 /* Search for the board context */
1523 list_for_each_entry(board_info, &me4000_board_info_list, list) {
1524 if (board_info->board_count == board)
1528 if (&board_info->list == &me4000_board_info_list) {
1530 "ME4000:me4000_open():Board %d not in device list\n",
1535 /* Get the external interrupt context */
1536 ext_int_context = board_info->ext_int_context;
1538 /* Check if already opened */
1539 spin_lock(&cnt_context->use_lock);
1540 if (ext_int_context->in_use) {
1542 "ME4000:me4000_open():External interrupt already in use\n");
1543 spin_unlock(&ext_int_context->use_lock);
1546 ext_int_context->in_use = 1;
1547 spin_unlock(&ext_int_context->use_lock);
1549 /* Hold the context in private data */
1550 file_p->private_data = ext_int_context;
1552 /* Set file operations pointer to single functions */
1553 file_p->f_op = &me4000_ext_int_fops;
1555 /* Request the interrupt line */
1557 request_irq(ext_int_context->irq, me4000_ext_int_isr,
1558 IRQF_DISABLED | IRQF_SHARED, ME4000_NAME,
1562 "ME4000:me4000_open():Can't get interrupt line");
1563 ext_int_context->in_use = 0;
1567 /* Reset the counter */
1568 me4000_ext_int_disable(ext_int_context);
1570 printk(KERN_ERR "ME4000:me4000_open():Major number unknown\n");
1577 static int me4000_release(struct inode *inode_p, struct file *file_p)
1579 struct me4000_ao_context *ao_context;
1580 struct me4000_ai_context *ai_context;
1581 struct me4000_dio_context *dio_context;
1582 struct me4000_cnt_context *cnt_context;
1583 struct me4000_ext_int_context *ext_int_context;
1585 CALL_PDEBUG("me4000_release() is executed\n");
1587 if (MAJOR(inode_p->i_rdev) == me4000_ao_major_driver_no) {
1588 ao_context = file_p->private_data;
1590 /* Mark DAC as unused */
1591 ao_context->dac_in_use = 0;
1592 } else if (MAJOR(inode_p->i_rdev) == me4000_ai_major_driver_no) {
1593 ai_context = file_p->private_data;
1595 /* Reset the analog input */
1596 me4000_ai_reset(ai_context);
1598 /* Free the interrupt and the circular buffer */
1599 if (ai_context->mode) {
1600 free_irq(ai_context->irq, ai_context);
1601 kfree(ai_context->circ_buf.buf);
1602 ai_context->circ_buf.buf = NULL;
1603 ai_context->circ_buf.head = 0;
1604 ai_context->circ_buf.tail = 0;
1607 /* Mark AI as unused */
1608 ai_context->in_use = 0;
1609 } else if (MAJOR(inode_p->i_rdev) == me4000_dio_major_driver_no) {
1610 dio_context = file_p->private_data;
1612 /* Mark digital I/O as unused */
1613 dio_context->in_use = 0;
1614 } else if (MAJOR(inode_p->i_rdev) == me4000_cnt_major_driver_no) {
1615 cnt_context = file_p->private_data;
1617 /* Mark counters as unused */
1618 cnt_context->in_use = 0;
1619 } else if (MAJOR(inode_p->i_rdev) == me4000_ext_int_major_driver_no) {
1620 ext_int_context = file_p->private_data;
1622 /* Disable the externel interrupt */
1623 me4000_ext_int_disable(ext_int_context);
1625 free_irq(ext_int_context->irq, ext_int_context);
1627 /* Mark as unused */
1628 ext_int_context->in_use = 0;
1631 "ME4000:me4000_release():Major number unknown\n");
1638 /*------------------------------- Analog output stuff --------------------------------------*/
1640 static int me4000_ao_prepare(struct me4000_ao_context *ao_context)
1642 unsigned long flags;
1644 CALL_PDEBUG("me4000_ao_prepare() is executed\n");
1646 if (ao_context->mode == ME4000_AO_CONV_MODE_CONTINUOUS) {
1647 /* Only do anything if not already in the correct mode */
1648 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1649 if ((mode & ME4000_AO_CONV_MODE_CONTINUOUS)
1650 && (mode & ME4000_AO_CTRL_BIT_ENABLE_FIFO)) {
1654 /* Stop any conversion */
1655 me4000_ao_immediate_stop(ao_context);
1657 /* Set the control register to default state */
1658 spin_lock_irqsave(&ao_context->int_lock, flags);
1659 me4000_outl(ME4000_AO_CONV_MODE_CONTINUOUS |
1660 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1661 ME4000_AO_CTRL_BIT_STOP |
1662 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1663 ao_context->ctrl_reg);
1664 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1666 /* Set to fastest sample rate */
1667 me4000_outl(65, ao_context->timer_reg);
1668 } else if (ao_context->mode == ME4000_AO_CONV_MODE_WRAPAROUND) {
1669 /* Only do anything if not already in the correct mode */
1670 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1671 if ((mode & ME4000_AO_CONV_MODE_WRAPAROUND)
1672 && (mode & ME4000_AO_CTRL_BIT_ENABLE_FIFO)) {
1676 /* Stop any conversion */
1677 me4000_ao_immediate_stop(ao_context);
1679 /* Set the control register to default state */
1680 spin_lock_irqsave(&ao_context->int_lock, flags);
1681 me4000_outl(ME4000_AO_CONV_MODE_WRAPAROUND |
1682 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1683 ME4000_AO_CTRL_BIT_STOP |
1684 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1685 ao_context->ctrl_reg);
1686 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1688 /* Set to fastest sample rate */
1689 me4000_outl(65, ao_context->timer_reg);
1690 } else if (ao_context->mode == ME4000_AO_CONV_MODE_SINGLE) {
1691 /* Only do anything if not already in the correct mode */
1692 unsigned long mode = me4000_inl(ao_context->ctrl_reg);
1695 (ME4000_AO_CONV_MODE_WRAPAROUND |
1696 ME4000_AO_CONV_MODE_CONTINUOUS))) {
1700 /* Stop any conversion */
1701 me4000_ao_immediate_stop(ao_context);
1703 /* Clear the control register */
1704 spin_lock_irqsave(&ao_context->int_lock, flags);
1705 me4000_outl(0x0, ao_context->ctrl_reg);
1706 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1708 /* Set voltage to 0V */
1709 me4000_outl(0x8000, ao_context->single_reg);
1712 "ME4000:me4000_ao_prepare():Invalid mode specified\n");
1719 static int me4000_ao_reset(struct me4000_ao_context *ao_context)
1722 wait_queue_head_t queue;
1723 unsigned long flags;
1725 CALL_PDEBUG("me4000_ao_reset() is executed\n");
1727 init_waitqueue_head(&queue);
1729 if (ao_context->mode == ME4000_AO_CONV_MODE_WRAPAROUND) {
1731 * First stop conversion of the DAC before reconfigure.
1732 * This is essantial, cause of the state machine.
1733 * If not stopped before configuring mode, it could
1734 * walk in a undefined state.
1736 tmp = me4000_inl(ao_context->ctrl_reg);
1737 tmp |= ME4000_AO_CTRL_BIT_IMMEDIATE_STOP;
1738 me4000_outl(tmp, ao_context->ctrl_reg);
1740 wait_event_timeout(queue,
1741 (inl(ao_context->status_reg) &
1742 ME4000_AO_STATUS_BIT_FSM) == 0,
1745 /* Set to transparent mode */
1746 me4000_ao_simultaneous_disable(ao_context);
1748 /* Set to single mode in order to set default voltage */
1749 me4000_outl(0x0, ao_context->ctrl_reg);
1751 /* Set voltage to 0V */
1752 me4000_outl(0x8000, ao_context->single_reg);
1754 /* Set to fastest sample rate */
1755 me4000_outl(65, ao_context->timer_reg);
1757 /* Set the original mode and enable FIFO */
1758 me4000_outl(ME4000_AO_CONV_MODE_WRAPAROUND |
1759 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1760 ME4000_AO_CTRL_BIT_STOP |
1761 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1762 ao_context->ctrl_reg);
1763 } else if (ao_context->mode == ME4000_AO_CONV_MODE_CONTINUOUS) {
1765 * First stop conversion of the DAC before reconfigure.
1766 * This is essantial, cause of the state machine.
1767 * If not stopped before configuring mode, it could
1768 * walk in a undefined state.
1770 spin_lock_irqsave(&ao_context->int_lock, flags);
1771 tmp = me4000_inl(ao_context->ctrl_reg);
1772 tmp |= ME4000_AO_CTRL_BIT_STOP;
1773 me4000_outl(tmp, ao_context->ctrl_reg);
1774 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1776 wait_event_timeout(queue,
1777 (inl(ao_context->status_reg) &
1778 ME4000_AO_STATUS_BIT_FSM) == 0,
1781 /* Clear the circular buffer */
1782 ao_context->circ_buf.head = 0;
1783 ao_context->circ_buf.tail = 0;
1785 /* Set to transparent mode */
1786 me4000_ao_simultaneous_disable(ao_context);
1788 /* Set to single mode in order to set default voltage */
1789 spin_lock_irqsave(&ao_context->int_lock, flags);
1790 tmp = me4000_inl(ao_context->ctrl_reg);
1791 me4000_outl(0x0, ao_context->ctrl_reg);
1793 /* Set voltage to 0V */
1794 me4000_outl(0x8000, ao_context->single_reg);
1796 /* Set to fastest sample rate */
1797 me4000_outl(65, ao_context->timer_reg);
1799 /* Set the original mode and enable FIFO */
1800 me4000_outl(ME4000_AO_CONV_MODE_CONTINUOUS |
1801 ME4000_AO_CTRL_BIT_ENABLE_FIFO |
1802 ME4000_AO_CTRL_BIT_STOP |
1803 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP,
1804 ao_context->ctrl_reg);
1805 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1807 /* Set to transparent mode */
1808 me4000_ao_simultaneous_disable(ao_context);
1810 /* Set voltage to 0V */
1811 me4000_outl(0x8000, ao_context->single_reg);
1817 static ssize_t me4000_ao_write_sing(struct file *filep, const char *buff,
1818 size_t cnt, loff_t *offp)
1820 struct me4000_ao_context *ao_context = filep->private_data;
1822 const u16 *buffer = (const u16 *)buff;
1824 CALL_PDEBUG("me4000_ao_write_sing() is executed\n");
1828 "%s:Write count is not 2\n", __func__);
1832 if (get_user(value, buffer)) {
1834 "%s:Cannot copy data from user\n", __func__);
1838 me4000_outl(value, ao_context->single_reg);
1843 static ssize_t me4000_ao_write_wrap(struct file *filep, const char *buff,
1844 size_t cnt, loff_t *offp)
1846 struct me4000_ao_context *ao_context = filep->private_data;
1850 const u16 *buffer = (const u16 *)buff;
1851 size_t count = cnt / 2;
1853 CALL_PDEBUG("me4000_ao_write_wrap() is executed\n");
1855 /* Check if a conversion is already running */
1856 if (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
1858 "%s:There is already a conversion running\n", __func__);
1862 if (count > ME4000_AO_FIFO_COUNT) {
1864 "%s:Can't load more than %d values\n", __func__,
1865 ME4000_AO_FIFO_COUNT);
1869 /* Reset the FIFO */
1870 tmp = inl(ao_context->ctrl_reg);
1871 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_FIFO;
1872 outl(tmp, ao_context->ctrl_reg);
1873 tmp |= ME4000_AO_CTRL_BIT_ENABLE_FIFO;
1874 outl(tmp, ao_context->ctrl_reg);
1876 for (i = 0; i < count; i++) {
1877 if (get_user(value, buffer + i)) {
1879 "%s:Cannot copy data from user\n", __func__);
1882 if (((ao_context->fifo_reg & 0xFF) == ME4000_AO_01_FIFO_REG)
1883 || ((ao_context->fifo_reg & 0xFF) == ME4000_AO_03_FIFO_REG))
1884 value = value << 16;
1885 outl(value, ao_context->fifo_reg);
1887 CALL_PDEBUG("me4000_ao_write_wrap() is leaved with %d\n", i * 2);
1892 static ssize_t me4000_ao_write_cont(struct file *filep, const char *buff,
1893 size_t cnt, loff_t *offp)
1895 struct me4000_ao_context *ao_context = filep->private_data;
1896 const u16 *buffer = (const u16 *)buff;
1897 size_t count = cnt / 2;
1898 unsigned long flags;
1906 wait_queue_head_t queue;
1908 CALL_PDEBUG("me4000_ao_write_cont() is executed\n");
1910 init_waitqueue_head(&queue);
1914 PDEBUG("me4000_ao_write_cont():Count is 0\n");
1918 if (filep->f_flags & O_APPEND) {
1919 PDEBUG("me4000_ao_write_cont():Append data to data stream\n");
1921 if (filep->f_flags & O_NONBLOCK) {
1922 if (ao_context->pipe_flag) {
1924 "ME4000:me4000_ao_write_cont():Broken pipe in nonblocking write\n");
1927 c = me4000_space_to_end(ao_context->circ_buf,
1928 ME4000_AO_BUFFER_COUNT);
1931 ("me4000_ao_write_cont():Returning from nonblocking write\n");
1935 wait_event_interruptible(ao_context->wait_queue,
1938 (ao_context->circ_buf,
1939 ME4000_AO_BUFFER_COUNT)));
1940 if (ao_context->pipe_flag) {
1942 "me4000_ao_write_cont():Broken pipe in blocking write\n");
1945 if (signal_pending(current)) {
1947 "me4000_ao_write_cont():Wait for free buffer interrupted from signal\n");
1952 PDEBUG("me4000_ao_write_cont():Space to end = %d\n", c);
1954 /* Only able to write size of free buffer or size of count */
1959 k -= copy_from_user(ao_context->circ_buf.buf +
1960 ao_context->circ_buf.head, buffer,
1964 ("me4000_ao_write_cont():Copy %d values from user space\n",
1970 ao_context->circ_buf.head =
1971 (ao_context->circ_buf.head +
1972 c) & (ME4000_AO_BUFFER_COUNT - 1);
1977 /* Values are now available so enable interrupts */
1978 spin_lock_irqsave(&ao_context->int_lock, flags);
1979 if (me4000_buf_count
1980 (ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
1981 tmp = me4000_inl(ao_context->ctrl_reg);
1982 tmp |= ME4000_AO_CTRL_BIT_ENABLE_IRQ;
1983 me4000_outl(tmp, ao_context->ctrl_reg);
1985 spin_unlock_irqrestore(&ao_context->int_lock, flags);
1988 /* Wait until the state machine is stopped if O_SYNC is set */
1989 if (filep->f_flags & O_SYNC) {
1990 while (inl(ao_context->status_reg) &
1991 ME4000_AO_STATUS_BIT_FSM) {
1992 interruptible_sleep_on_timeout(&queue, 1);
1993 if (ao_context->pipe_flag) {
1995 ("me4000_ao_write_cont():Broken pipe detected after sync\n");
1998 if (signal_pending(current)) {
2000 "me4000_ao_write_cont():Wait on state machine after sync interrupted\n");
2006 PDEBUG("me4000_ao_write_cont():Preload DAC FIFO\n");
2007 if ((me4000_inl(ao_context->status_reg) &
2008 ME4000_AO_STATUS_BIT_FSM)) {
2010 "me4000_ao_write_cont():Can't Preload DAC FIFO while conversion is running\n");
2014 /* Clear the FIFO */
2015 spin_lock_irqsave(&ao_context->int_lock, flags);
2016 tmp = me4000_inl(ao_context->ctrl_reg);
2018 ~(ME4000_AO_CTRL_BIT_ENABLE_FIFO |
2019 ME4000_AO_CTRL_BIT_ENABLE_IRQ);
2020 me4000_outl(tmp, ao_context->ctrl_reg);
2021 tmp |= ME4000_AO_CTRL_BIT_ENABLE_FIFO;
2022 me4000_outl(tmp, ao_context->ctrl_reg);
2023 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2025 /* Clear the circular buffer */
2026 ao_context->circ_buf.head = 0;
2027 ao_context->circ_buf.tail = 0;
2029 /* Reset the broken pipe flag */
2030 ao_context->pipe_flag = 0;
2032 /* Only able to write size of fifo or count */
2033 c = ME4000_AO_FIFO_COUNT;
2038 ("me4000_ao_write_cont():Write %d values to DAC on 0x%lX\n",
2039 c, ao_context->fifo_reg);
2041 /* Write values to the fifo */
2042 for (i = 0; i < c; i++) {
2043 if (get_user(svalue, buffer))
2046 if (((ao_context->fifo_reg & 0xFF) ==
2047 ME4000_AO_01_FIFO_REG)
2048 || ((ao_context->fifo_reg & 0xFF) ==
2049 ME4000_AO_03_FIFO_REG)) {
2050 lvalue = ((u32) svalue) << 16;
2052 lvalue = (u32) svalue;
2054 outl(lvalue, ao_context->fifo_reg);
2061 /* Get free buffer */
2062 c = me4000_space_to_end(ao_context->circ_buf,
2063 ME4000_AO_BUFFER_COUNT);
2068 /* Only able to write size of free buffer or size of count */
2072 /* If count = 0 return to user */
2075 ("me4000_ao_write_cont():Count reached 0\n");
2080 k -= copy_from_user(ao_context->circ_buf.buf +
2081 ao_context->circ_buf.head, buffer,
2085 ("me4000_ao_write_cont():Wrote %d values to buffer\n",
2091 ao_context->circ_buf.head =
2092 (ao_context->circ_buf.head +
2093 c) & (ME4000_AO_BUFFER_COUNT - 1);
2098 /* If values in the buffer are available so enable interrupts */
2099 spin_lock_irqsave(&ao_context->int_lock, flags);
2100 if (me4000_buf_count
2101 (ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
2103 ("me4000_ao_write_cont():Enable Interrupts\n");
2104 tmp = me4000_inl(ao_context->ctrl_reg);
2105 tmp |= ME4000_AO_CTRL_BIT_ENABLE_IRQ;
2106 me4000_outl(tmp, ao_context->ctrl_reg);
2108 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2112 if (filep->f_flags & O_NONBLOCK)
2113 return (ret == 0) ? -EAGAIN : 2 * ret;
2118 static unsigned int me4000_ao_poll_cont(struct file *file_p, poll_table *wait)
2120 struct me4000_ao_context *ao_context;
2121 unsigned long mask = 0;
2123 CALL_PDEBUG("me4000_ao_poll_cont() is executed\n");
2125 ao_context = file_p->private_data;
2127 poll_wait(file_p, &ao_context->wait_queue, wait);
2129 /* Get free buffer */
2130 if (me4000_space_to_end(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT))
2131 mask |= POLLOUT | POLLWRNORM;
2133 CALL_PDEBUG("me4000_ao_poll_cont():Return mask %lX\n", mask);
2138 static int me4000_ao_fsync_cont(struct file *file_p, struct dentry *dentry_p,
2141 struct me4000_ao_context *ao_context;
2142 wait_queue_head_t queue;
2144 CALL_PDEBUG("me4000_ao_fsync_cont() is executed\n");
2146 ao_context = file_p->private_data;
2147 init_waitqueue_head(&queue);
2149 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2150 interruptible_sleep_on_timeout(&queue, 1);
2151 wait_event_interruptible_timeout(queue,
2152 !(inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM),
2154 if (ao_context->pipe_flag) {
2156 "%s:Broken pipe detected\n", __func__);
2160 if (signal_pending(current)) {
2162 "%s:Wait on state machine interrupted\n",
2171 static int me4000_ao_ioctl_sing(struct inode *inode_p, struct file *file_p,
2172 unsigned int service, unsigned long arg)
2174 struct me4000_ao_context *ao_context;
2176 CALL_PDEBUG("me4000_ao_ioctl_sing() is executed\n");
2178 ao_context = file_p->private_data;
2180 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2182 PDEBUG("me4000_ao_ioctl_sing():Wrong magic number\n");
2186 case ME4000_AO_EX_TRIG_SETUP:
2187 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2188 case ME4000_AO_EX_TRIG_ENABLE:
2189 return me4000_ao_ex_trig_enable(ao_context);
2190 case ME4000_AO_EX_TRIG_DISABLE:
2191 return me4000_ao_ex_trig_disable(ao_context);
2192 case ME4000_AO_PRELOAD:
2193 return me4000_ao_preload(ao_context);
2194 case ME4000_AO_PRELOAD_UPDATE:
2195 return me4000_ao_preload_update(ao_context);
2196 case ME4000_GET_USER_INFO:
2197 return me4000_get_user_info((struct me4000_user_info *)arg,
2198 ao_context->board_info);
2199 case ME4000_AO_SIMULTANEOUS_EX_TRIG:
2200 return me4000_ao_simultaneous_ex_trig(ao_context);
2201 case ME4000_AO_SIMULTANEOUS_SW:
2202 return me4000_ao_simultaneous_sw(ao_context);
2203 case ME4000_AO_SIMULTANEOUS_DISABLE:
2204 return me4000_ao_simultaneous_disable(ao_context);
2205 case ME4000_AO_SIMULTANEOUS_UPDATE:
2207 me4000_ao_simultaneous_update(
2208 (struct me4000_ao_channel_list *)arg,
2210 case ME4000_AO_EX_TRIG_TIMEOUT:
2211 return me4000_ao_ex_trig_timeout((unsigned long *)arg,
2213 case ME4000_AO_DISABLE_DO:
2214 return me4000_ao_disable_do(ao_context);
2217 "me4000_ao_ioctl_sing():Service number invalid\n");
2224 static int me4000_ao_ioctl_wrap(struct inode *inode_p, struct file *file_p,
2225 unsigned int service, unsigned long arg)
2227 struct me4000_ao_context *ao_context;
2229 CALL_PDEBUG("me4000_ao_ioctl_wrap() is executed\n");
2231 ao_context = file_p->private_data;
2233 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2235 PDEBUG("me4000_ao_ioctl_wrap():Wrong magic number\n");
2239 case ME4000_AO_START:
2240 return me4000_ao_start((unsigned long *)arg, ao_context);
2241 case ME4000_AO_STOP:
2242 return me4000_ao_stop(ao_context);
2243 case ME4000_AO_IMMEDIATE_STOP:
2244 return me4000_ao_immediate_stop(ao_context);
2245 case ME4000_AO_RESET:
2246 return me4000_ao_reset(ao_context);
2247 case ME4000_AO_TIMER_SET_DIVISOR:
2248 return me4000_ao_timer_set_divisor((u32 *) arg, ao_context);
2249 case ME4000_AO_EX_TRIG_SETUP:
2250 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2251 case ME4000_AO_EX_TRIG_ENABLE:
2252 return me4000_ao_ex_trig_enable(ao_context);
2253 case ME4000_AO_EX_TRIG_DISABLE:
2254 return me4000_ao_ex_trig_disable(ao_context);
2255 case ME4000_GET_USER_INFO:
2256 return me4000_get_user_info((struct me4000_user_info *)arg,
2257 ao_context->board_info);
2258 case ME4000_AO_FSM_STATE:
2259 return me4000_ao_fsm_state((int *)arg, ao_context);
2260 case ME4000_AO_ENABLE_DO:
2261 return me4000_ao_enable_do(ao_context);
2262 case ME4000_AO_DISABLE_DO:
2263 return me4000_ao_disable_do(ao_context);
2264 case ME4000_AO_SYNCHRONOUS_EX_TRIG:
2265 return me4000_ao_synchronous_ex_trig(ao_context);
2266 case ME4000_AO_SYNCHRONOUS_SW:
2267 return me4000_ao_synchronous_sw(ao_context);
2268 case ME4000_AO_SYNCHRONOUS_DISABLE:
2269 return me4000_ao_synchronous_disable(ao_context);
2276 static int me4000_ao_ioctl_cont(struct inode *inode_p, struct file *file_p,
2277 unsigned int service, unsigned long arg)
2279 struct me4000_ao_context *ao_context;
2281 CALL_PDEBUG("me4000_ao_ioctl_cont() is executed\n");
2283 ao_context = file_p->private_data;
2285 if (_IOC_TYPE(service) != ME4000_MAGIC) {
2287 PDEBUG("me4000_ao_ioctl_cont():Wrong magic number\n");
2291 case ME4000_AO_START:
2292 return me4000_ao_start((unsigned long *)arg, ao_context);
2293 case ME4000_AO_STOP:
2294 return me4000_ao_stop(ao_context);
2295 case ME4000_AO_IMMEDIATE_STOP:
2296 return me4000_ao_immediate_stop(ao_context);
2297 case ME4000_AO_RESET:
2298 return me4000_ao_reset(ao_context);
2299 case ME4000_AO_TIMER_SET_DIVISOR:
2300 return me4000_ao_timer_set_divisor((u32 *) arg, ao_context);
2301 case ME4000_AO_EX_TRIG_SETUP:
2302 return me4000_ao_ex_trig_set_edge((int *)arg, ao_context);
2303 case ME4000_AO_EX_TRIG_ENABLE:
2304 return me4000_ao_ex_trig_enable(ao_context);
2305 case ME4000_AO_EX_TRIG_DISABLE:
2306 return me4000_ao_ex_trig_disable(ao_context);
2307 case ME4000_AO_ENABLE_DO:
2308 return me4000_ao_enable_do(ao_context);
2309 case ME4000_AO_DISABLE_DO:
2310 return me4000_ao_disable_do(ao_context);
2311 case ME4000_AO_FSM_STATE:
2312 return me4000_ao_fsm_state((int *)arg, ao_context);
2313 case ME4000_GET_USER_INFO:
2314 return me4000_get_user_info((struct me4000_user_info *)arg,
2315 ao_context->board_info);
2316 case ME4000_AO_SYNCHRONOUS_EX_TRIG:
2317 return me4000_ao_synchronous_ex_trig(ao_context);
2318 case ME4000_AO_SYNCHRONOUS_SW:
2319 return me4000_ao_synchronous_sw(ao_context);
2320 case ME4000_AO_SYNCHRONOUS_DISABLE:
2321 return me4000_ao_synchronous_disable(ao_context);
2322 case ME4000_AO_GET_FREE_BUFFER:
2323 return me4000_ao_get_free_buffer((unsigned long *)arg,
2331 static int me4000_ao_start(unsigned long *arg,
2332 struct me4000_ao_context *ao_context)
2335 wait_queue_head_t queue;
2337 unsigned long timeout;
2338 unsigned long flags;
2340 CALL_PDEBUG("me4000_ao_start() is executed\n");
2342 if (get_user(timeout, arg)) {
2344 "me4000_ao_start():Cannot copy data from user\n");
2348 init_waitqueue_head(&queue);
2350 spin_lock_irqsave(&ao_context->int_lock, flags);
2351 tmp = inl(ao_context->ctrl_reg);
2352 tmp &= ~(ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2353 me4000_outl(tmp, ao_context->ctrl_reg);
2354 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2356 if ((tmp & ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG)) {
2360 (inl(ao_context->status_reg) &
2361 ME4000_AO_STATUS_BIT_FSM)) {
2362 interruptible_sleep_on_timeout(&queue, 1);
2363 if (signal_pending(current)) {
2365 "ME4000:me4000_ao_start():Wait on start of state machine interrupted\n");
2368 /* kernel 2.6 has different definitions for HZ
2369 * in user and kernel space */
2370 if ((jiffies - ref) > (timeout * HZ / USER_HZ)) {
2372 "ME4000:me4000_ao_start():Timeout reached\n");
2378 me4000_outl(0x8000, ao_context->single_reg);
2384 static int me4000_ao_stop(struct me4000_ao_context *ao_context)
2387 wait_queue_head_t queue;
2388 unsigned long flags;
2390 init_waitqueue_head(&queue);
2392 CALL_PDEBUG("me4000_ao_stop() is executed\n");
2394 /* Set the stop bit */
2395 spin_lock_irqsave(&ao_context->int_lock, flags);
2396 tmp = inl(ao_context->ctrl_reg);
2397 tmp |= ME4000_AO_CTRL_BIT_STOP;
2398 me4000_outl(tmp, ao_context->ctrl_reg);
2399 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2401 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2402 interruptible_sleep_on_timeout(&queue, 1);
2403 if (signal_pending(current)) {
2405 "me4000_ao_stop():Wait on state machine after stop interrupted\n");
2410 /* Clear the stop bit */
2411 /* tmp &= ~ME4000_AO_CTRL_BIT_STOP; */
2412 /* me4000_outl(tmp, ao_context->ctrl_reg); */
2417 static int me4000_ao_immediate_stop(struct me4000_ao_context *ao_context)
2420 wait_queue_head_t queue;
2421 unsigned long flags;
2423 init_waitqueue_head(&queue);
2425 CALL_PDEBUG("me4000_ao_immediate_stop() is executed\n");
2427 spin_lock_irqsave(&ao_context->int_lock, flags);
2428 tmp = inl(ao_context->ctrl_reg);
2429 tmp |= ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP;
2430 me4000_outl(tmp, ao_context->ctrl_reg);
2431 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2433 while (inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) {
2434 interruptible_sleep_on_timeout(&queue, 1);
2435 if (signal_pending(current)) {
2437 "me4000_ao_immediate_stop():Wait on state machine after stop interrupted\n");
2442 /* Clear the stop bits */
2443 /* tmp &= ~(ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP); */
2444 /* me4000_outl(tmp, ao_context->ctrl_reg); */
2449 static int me4000_ao_timer_set_divisor(u32 *arg,
2450 struct me4000_ao_context *ao_context)
2455 CALL_PDEBUG("me4000_ao_timer set_divisor() is executed\n");
2457 if (get_user(divisor, arg))
2460 /* Check if the state machine is stopped */
2461 tmp = me4000_inl(ao_context->status_reg);
2462 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2464 "me4000_ao_timer_set_divisor():Can't set timer while DAC is running\n");
2468 PDEBUG("me4000_ao_timer set_divisor():Divisor from user = %d\n",
2471 /* Check if the divisor is right. ME4000_AO_MIN_TICKS is the lowest */
2472 if (divisor < ME4000_AO_MIN_TICKS) {
2474 "ME4000:me4000_ao_timer set_divisor():Divisor to low\n");
2478 /* Fix bug in Firmware */
2481 PDEBUG("me4000_ao_timer set_divisor():Divisor to HW = %d\n", divisor);
2483 /* Write the divisor */
2484 me4000_outl(divisor, ao_context->timer_reg);
2489 static int me4000_ao_ex_trig_set_edge(int *arg,
2490 struct me4000_ao_context *ao_context)
2494 unsigned long flags;
2496 CALL_PDEBUG("me4000_ao_ex_trig_set_edge() is executed\n");
2498 if (get_user(mode, arg))
2501 /* Check if the state machine is stopped */
2502 tmp = me4000_inl(ao_context->status_reg);
2503 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2505 "me4000_ao_ex_trig_set_edge():Can't set trigger while DAC is running\n");
2509 if (mode == ME4000_AO_TRIGGER_EXT_EDGE_RISING) {
2510 spin_lock_irqsave(&ao_context->int_lock, flags);
2511 tmp = me4000_inl(ao_context->ctrl_reg);
2513 ~(ME4000_AO_CTRL_BIT_EX_TRIG_EDGE |
2514 ME4000_AO_CTRL_BIT_EX_TRIG_BOTH);
2515 me4000_outl(tmp, ao_context->ctrl_reg);
2516 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2517 } else if (mode == ME4000_AO_TRIGGER_EXT_EDGE_FALLING) {
2518 spin_lock_irqsave(&ao_context->int_lock, flags);
2519 tmp = me4000_inl(ao_context->ctrl_reg);
2520 tmp &= ~ME4000_AO_CTRL_BIT_EX_TRIG_BOTH;
2521 tmp |= ME4000_AO_CTRL_BIT_EX_TRIG_EDGE;
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_BOTH) {
2525 spin_lock_irqsave(&ao_context->int_lock, flags);
2526 tmp = me4000_inl(ao_context->ctrl_reg);
2528 ME4000_AO_CTRL_BIT_EX_TRIG_EDGE |
2529 ME4000_AO_CTRL_BIT_EX_TRIG_BOTH;
2530 me4000_outl(tmp, ao_context->ctrl_reg);
2531 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2534 "me4000_ao_ex_trig_set_edge():Invalid trigger mode\n");
2541 static int me4000_ao_ex_trig_enable(struct me4000_ao_context *ao_context)
2544 unsigned long flags;
2546 CALL_PDEBUG("me4000_ao_ex_trig_enable() is executed\n");
2548 /* Check if the state machine is stopped */
2549 tmp = me4000_inl(ao_context->status_reg);
2550 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2552 "me4000_ao_ex_trig_enable():Can't enable trigger while DAC is running\n");
2556 spin_lock_irqsave(&ao_context->int_lock, flags);
2557 tmp = me4000_inl(ao_context->ctrl_reg);
2558 tmp |= ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG;
2559 me4000_outl(tmp, ao_context->ctrl_reg);
2560 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2565 static int me4000_ao_ex_trig_disable(struct me4000_ao_context *ao_context)
2568 unsigned long flags;
2570 CALL_PDEBUG("me4000_ao_ex_trig_disable() is executed\n");
2572 /* Check if the state machine is stopped */
2573 tmp = me4000_inl(ao_context->status_reg);
2574 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2576 "me4000_ao_ex_trig_disable():Can't disable trigger while DAC is running\n");
2580 spin_lock_irqsave(&ao_context->int_lock, flags);
2581 tmp = me4000_inl(ao_context->ctrl_reg);
2582 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG;
2583 me4000_outl(tmp, ao_context->ctrl_reg);
2584 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2589 static int me4000_ao_simultaneous_disable(struct me4000_ao_context *ao_context)
2593 CALL_PDEBUG("me4000_ao_simultaneous_disable() is executed\n");
2595 /* Check if the state machine is stopped */
2596 /* Be careful here because this function is called from
2597 me4000_ao_synchronous disable */
2598 tmp = me4000_inl(ao_context->status_reg);
2599 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2601 "me4000_ao_simultaneous_disable():Can't disable while DAC is running\n");
2605 spin_lock(&ao_context->board_info->preload_lock);
2606 tmp = me4000_inl(ao_context->preload_reg);
2607 /* Disable preload bit */
2608 tmp &= ~(0x1 << ao_context->index);
2609 /* Disable hw simultaneous bit */
2610 tmp &= ~(0x1 << (ao_context->index + 16));
2611 me4000_outl(tmp, ao_context->preload_reg);
2612 spin_unlock(&ao_context->board_info->preload_lock);
2617 static int me4000_ao_simultaneous_ex_trig(struct me4000_ao_context *ao_context)
2621 CALL_PDEBUG("me4000_ao_simultaneous_ex_trig() is executed\n");
2623 spin_lock(&ao_context->board_info->preload_lock);
2624 tmp = me4000_inl(ao_context->preload_reg);
2625 /* Enable preload bit */
2626 tmp |= (0x1 << ao_context->index);
2627 /* Enable hw simulatenous bit */
2628 tmp |= (0x1 << (ao_context->index + 16));
2629 me4000_outl(tmp, ao_context->preload_reg);
2630 spin_unlock(&ao_context->board_info->preload_lock);
2635 static int me4000_ao_simultaneous_sw(struct me4000_ao_context *ao_context)
2639 CALL_PDEBUG("me4000_ao_simultaneous_sw() is executed\n");
2641 spin_lock(&ao_context->board_info->preload_lock);
2642 tmp = me4000_inl(ao_context->preload_reg);
2643 /* Enable preload bit */
2644 tmp |= (0x1 << ao_context->index);
2645 /* Enable hw simulatenous bit */
2646 tmp &= ~(0x1 << (ao_context->index + 16));
2647 me4000_outl(tmp, ao_context->preload_reg);
2648 spin_unlock(&ao_context->board_info->preload_lock);
2653 static int me4000_ao_preload(struct me4000_ao_context *ao_context)
2655 CALL_PDEBUG("me4000_ao_preload() is executed\n");
2656 return me4000_ao_simultaneous_sw(ao_context);
2659 static int me4000_ao_preload_update(struct me4000_ao_context *ao_context)
2663 struct list_head *entry;
2665 CALL_PDEBUG("me4000_ao_preload_update() is executed\n");
2667 spin_lock(&ao_context->board_info->preload_lock);
2668 tmp = me4000_inl(ao_context->preload_reg);
2669 list_for_each(entry, &ao_context->board_info->ao_context_list) {
2670 /* The channels we update must be in the following state :
2672 - Hardware trigger is disabled
2673 - Corresponding simultaneous bit is reset
2675 ctrl = me4000_inl(ao_context->ctrl_reg);
2678 (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1 |
2679 ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG))) {
2683 (((struct me4000_ao_context *)entry)->index
2687 (((struct me4000_ao_context *)entry)->
2692 me4000_outl(tmp, ao_context->preload_reg);
2693 spin_unlock(&ao_context->board_info->preload_lock);
2698 static int me4000_ao_simultaneous_update(struct me4000_ao_channel_list *arg,
2699 struct me4000_ao_context *ao_context)
2704 struct me4000_ao_channel_list channels;
2706 CALL_PDEBUG("me4000_ao_simultaneous_update() is executed\n");
2708 /* Copy data from user */
2709 err = copy_from_user(&channels, arg,
2710 sizeof(struct me4000_ao_channel_list));
2713 "ME4000:me4000_ao_simultaneous_update():Can't copy command\n");
2718 kzalloc(sizeof(unsigned long) * channels.count, GFP_KERNEL);
2719 if (!channels.list) {
2721 "ME4000:me4000_ao_simultaneous_update():Can't get buffer\n");
2725 /* Copy channel list from user */
2727 copy_from_user(channels.list, arg->list,
2728 sizeof(unsigned long) * channels.count);
2731 "ME4000:me4000_ao_simultaneous_update():Can't copy list\n");
2732 kfree(channels.list);
2736 spin_lock(&ao_context->board_info->preload_lock);
2737 tmp = me4000_inl(ao_context->preload_reg);
2738 for (i = 0; i < channels.count; i++) {
2739 if (channels.list[i] >
2740 ao_context->board_info->board_p->ao.count) {
2741 spin_unlock(&ao_context->board_info->preload_lock);
2742 kfree(channels.list);
2744 "ME4000:me4000_ao_simultaneous_update():Invalid board number specified\n");
2747 /* Clear the preload bit */
2748 tmp &= ~(0x1 << channels.list[i]);
2749 /* Clear the hw simultaneous bit */
2750 tmp &= ~(0x1 << (channels.list[i] + 16));
2752 me4000_outl(tmp, ao_context->preload_reg);
2753 spin_unlock(&ao_context->board_info->preload_lock);
2754 kfree(channels.list);
2759 static int me4000_ao_synchronous_ex_trig(struct me4000_ao_context *ao_context)
2762 unsigned long flags;
2764 CALL_PDEBUG("me4000_ao_synchronous_ex_trig() is executed\n");
2766 /* Check if the state machine is stopped */
2767 tmp = me4000_inl(ao_context->status_reg);
2768 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2770 "me4000_ao_synchronous_ex_trig(): DAC is running\n");
2774 spin_lock(&ao_context->board_info->preload_lock);
2775 tmp = me4000_inl(ao_context->preload_reg);
2776 /* Disable synchronous sw bit */
2777 tmp &= ~(0x1 << ao_context->index);
2778 /* Enable synchronous hw bit */
2779 tmp |= 0x1 << (ao_context->index + 16);
2780 me4000_outl(tmp, ao_context->preload_reg);
2781 spin_unlock(&ao_context->board_info->preload_lock);
2784 spin_lock_irqsave(&ao_context->int_lock, flags);
2785 tmp = me4000_inl(ao_context->ctrl_reg);
2786 if (tmp & (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1)) {
2788 ~(ME4000_AO_CTRL_BIT_STOP |
2789 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2790 me4000_outl(tmp, ao_context->ctrl_reg);
2792 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2797 static int me4000_ao_synchronous_sw(struct me4000_ao_context *ao_context)
2800 unsigned long flags;
2802 CALL_PDEBUG("me4000_ao_synchronous_sw() is executed\n");
2804 /* Check if the state machine is stopped */
2805 tmp = me4000_inl(ao_context->status_reg);
2806 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2807 printk(KERN_ERR "me4000_ao_synchronous_sw(): DAC is running\n");
2811 spin_lock(&ao_context->board_info->preload_lock);
2812 tmp = me4000_inl(ao_context->preload_reg);
2813 /* Enable synchronous sw bit */
2814 tmp |= 0x1 << ao_context->index;
2815 /* Disable synchronous hw bit */
2816 tmp &= ~(0x1 << (ao_context->index + 16));
2817 me4000_outl(tmp, ao_context->preload_reg);
2818 spin_unlock(&ao_context->board_info->preload_lock);
2821 spin_lock_irqsave(&ao_context->int_lock, flags);
2822 tmp = me4000_inl(ao_context->ctrl_reg);
2823 if (tmp & (ME4000_AO_CTRL_BIT_MODE_0 | ME4000_AO_CTRL_BIT_MODE_1)) {
2825 ~(ME4000_AO_CTRL_BIT_STOP |
2826 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2827 me4000_outl(tmp, ao_context->ctrl_reg);
2829 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2834 static int me4000_ao_synchronous_disable(struct me4000_ao_context *ao_context)
2836 return me4000_ao_simultaneous_disable(ao_context);
2839 static int me4000_ao_get_free_buffer(unsigned long *arg,
2840 struct me4000_ao_context *ao_context)
2845 c = me4000_buf_space(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT);
2847 err = copy_to_user(arg, &c, sizeof(unsigned long));
2850 "%s:Can't copy to user space\n", __func__);
2857 static int me4000_ao_ex_trig_timeout(unsigned long *arg,
2858 struct me4000_ao_context *ao_context)
2861 wait_queue_head_t queue;
2863 unsigned long timeout;
2865 CALL_PDEBUG("me4000_ao_ex_trig_timeout() is executed\n");
2867 if (get_user(timeout, arg)) {
2869 "me4000_ao_ex_trig_timeout():Cannot copy data from user\n");
2873 init_waitqueue_head(&queue);
2875 tmp = inl(ao_context->ctrl_reg);
2877 if ((tmp & ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG)) {
2880 while ((inl(ao_context->status_reg) &
2881 ME4000_AO_STATUS_BIT_FSM)) {
2882 interruptible_sleep_on_timeout(&queue, 1);
2883 if (signal_pending(current)) {
2885 "ME4000:me4000_ao_ex_trig_timeout():Wait on start of state machine interrupted\n");
2888 /* kernel 2.6 has different definitions for HZ
2889 * in user and kernel space */
2890 if ((jiffies - ref) > (timeout * HZ / USER_HZ)) {
2892 "ME4000:me4000_ao_ex_trig_timeout():Timeout reached\n");
2897 while ((inl(ao_context->status_reg) &
2898 ME4000_AO_STATUS_BIT_FSM)) {
2899 interruptible_sleep_on_timeout(&queue, 1);
2900 if (signal_pending(current)) {
2902 "ME4000:me4000_ao_ex_trig_timeout():Wait on start of state machine interrupted\n");
2909 "ME4000:me4000_ao_ex_trig_timeout():External Trigger is not enabled\n");
2916 static int me4000_ao_enable_do(struct me4000_ao_context *ao_context)
2919 unsigned long flags;
2921 CALL_PDEBUG("me4000_ao_enable_do() is executed\n");
2923 /* Only available for analog output 3 */
2924 if (ao_context->index != 3) {
2926 "me4000_ao_enable_do():Only available for analog output 3\n");
2930 /* Check if the state machine is stopped */
2931 tmp = me4000_inl(ao_context->status_reg);
2932 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2933 printk(KERN_ERR "me4000_ao_enable_do(): DAC is running\n");
2937 /* Set the stop bit */
2938 spin_lock_irqsave(&ao_context->int_lock, flags);
2939 tmp = inl(ao_context->ctrl_reg);
2940 tmp |= ME4000_AO_CTRL_BIT_ENABLE_DO;
2941 me4000_outl(tmp, ao_context->ctrl_reg);
2942 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2947 static int me4000_ao_disable_do(struct me4000_ao_context *ao_context)
2950 unsigned long flags;
2952 CALL_PDEBUG("me4000_ao_disable_do() is executed\n");
2954 /* Only available for analog output 3 */
2955 if (ao_context->index != 3) {
2957 "me4000_ao_disable():Only available for analog output 3\n");
2961 /* Check if the state machine is stopped */
2962 tmp = me4000_inl(ao_context->status_reg);
2963 if (tmp & ME4000_AO_STATUS_BIT_FSM) {
2964 printk(KERN_ERR "me4000_ao_disable_do(): DAC is running\n");
2968 spin_lock_irqsave(&ao_context->int_lock, flags);
2969 tmp = inl(ao_context->ctrl_reg);
2970 tmp &= ~(ME4000_AO_CTRL_BIT_ENABLE_DO);
2971 me4000_outl(tmp, ao_context->ctrl_reg);
2972 spin_unlock_irqrestore(&ao_context->int_lock, flags);
2977 static int me4000_ao_fsm_state(int *arg, struct me4000_ao_context *ao_context)
2981 CALL_PDEBUG("me4000_ao_fsm_state() is executed\n");
2984 (me4000_inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM) ? 1
2987 if (ao_context->pipe_flag) {
2988 printk(KERN_ERR "me4000_ao_fsm_state():Broken pipe detected\n");
2992 if (put_user(tmp, arg)) {
2993 printk(KERN_ERR "me4000_ao_fsm_state():Cannot copy to user\n");
3000 /*------------------------- Analog input stuff -------------------------------*/
3002 static int me4000_ai_prepare(struct me4000_ai_context *ai_context)
3004 wait_queue_head_t queue;
3007 CALL_PDEBUG("me4000_ai_prepare() is executed\n");
3009 init_waitqueue_head(&queue);
3011 /* Set the new mode and stop bits */
3012 me4000_outl(ai_context->
3013 mode | ME4000_AI_CTRL_BIT_STOP |
3014 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP, ai_context->ctrl_reg);
3016 /* Set the timer registers */
3017 ai_context->chan_timer = 66;
3018 ai_context->chan_pre_timer = 66;
3019 ai_context->scan_timer_low = 0;
3020 ai_context->scan_timer_high = 0;
3022 me4000_outl(65, ai_context->chan_timer_reg);
3023 me4000_outl(65, ai_context->chan_pre_timer_reg);
3024 me4000_outl(0, ai_context->scan_timer_low_reg);
3025 me4000_outl(0, ai_context->scan_timer_high_reg);
3026 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3027 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3029 ai_context->channel_list_count = 0;
3031 if (ai_context->mode) {
3032 /* Request the interrupt line */
3034 request_irq(ai_context->irq, me4000_ai_isr,
3035 IRQF_DISABLED | IRQF_SHARED, ME4000_NAME,
3039 "ME4000:me4000_ai_prepare():Can't get interrupt line");
3043 /* Allocate circular buffer */
3044 ai_context->circ_buf.buf =
3045 kzalloc(ME4000_AI_BUFFER_SIZE, GFP_KERNEL);
3046 if (!ai_context->circ_buf.buf) {
3048 "ME4000:me4000_ai_prepare():Can't get circular buffer\n");
3049 free_irq(ai_context->irq, ai_context);
3053 /* Clear the circular buffer */
3054 ai_context->circ_buf.head = 0;
3055 ai_context->circ_buf.tail = 0;
3061 static int me4000_ai_reset(struct me4000_ai_context *ai_context)
3063 wait_queue_head_t queue;
3065 unsigned long flags;
3067 CALL_PDEBUG("me4000_ai_reset() is executed\n");
3069 init_waitqueue_head(&queue);
3072 * First stop conversion of the state machine before reconfigure.
3073 * If not stopped before configuring mode, it could
3074 * walk in a undefined state.
3076 spin_lock_irqsave(&ai_context->int_lock, flags);
3077 tmp = me4000_inl(ai_context->ctrl_reg);
3078 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
3079 me4000_outl(tmp, ai_context->ctrl_reg);
3080 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3082 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3083 interruptible_sleep_on_timeout(&queue, 1);
3084 if (signal_pending(current)) {
3086 "me4000_ai_reset():Wait on state machine after stop interrupted\n");
3091 /* Clear the control register and set the stop bits */
3092 spin_lock_irqsave(&ai_context->int_lock, flags);
3093 tmp = me4000_inl(ai_context->ctrl_reg);
3094 me4000_outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP | ME4000_AI_CTRL_BIT_STOP,
3095 ai_context->ctrl_reg);
3096 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3098 /* Reset timer registers */
3099 ai_context->chan_timer = 66;
3100 ai_context->chan_pre_timer = 66;
3101 ai_context->scan_timer_low = 0;
3102 ai_context->scan_timer_high = 0;
3103 ai_context->sample_counter = 0;
3104 ai_context->sample_counter_reload = 0;
3106 me4000_outl(65, ai_context->chan_timer_reg);
3107 me4000_outl(65, ai_context->chan_pre_timer_reg);
3108 me4000_outl(0, ai_context->scan_timer_low_reg);
3109 me4000_outl(0, ai_context->scan_timer_high_reg);
3110 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3111 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3112 me4000_outl(0, ai_context->sample_counter_reg);
3114 ai_context->channel_list_count = 0;
3116 /* Clear the circular buffer */
3117 ai_context->circ_buf.head = 0;
3118 ai_context->circ_buf.tail = 0;
3123 static int me4000_ai_ioctl_sing(struct inode *inode_p, struct file *file_p,
3124 unsigned int service, unsigned long arg)
3126 struct me4000_ai_context *ai_context;
3128 CALL_PDEBUG("me4000_ai_ioctl_sing() is executed\n");
3130 ai_context = file_p->private_data;
3132 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3133 printk(KERN_ERR "me4000_ai_ioctl_sing():Wrong magic number\n");
3136 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3138 "me4000_ai_ioctl_sing():Service number to high\n");
3143 case ME4000_AI_SINGLE:
3144 return me4000_ai_single((struct me4000_ai_single *)arg,
3146 case ME4000_AI_EX_TRIG_ENABLE:
3147 return me4000_ai_ex_trig_enable(ai_context);
3148 case ME4000_AI_EX_TRIG_DISABLE:
3149 return me4000_ai_ex_trig_disable(ai_context);
3150 case ME4000_AI_EX_TRIG_SETUP:
3151 return me4000_ai_ex_trig_setup((struct me4000_ai_trigger *)arg,
3153 case ME4000_GET_USER_INFO:
3154 return me4000_get_user_info((struct me4000_user_info *)arg,
3155 ai_context->board_info);
3156 case ME4000_AI_OFFSET_ENABLE:
3157 return me4000_ai_offset_enable(ai_context);
3158 case ME4000_AI_OFFSET_DISABLE:
3159 return me4000_ai_offset_disable(ai_context);
3160 case ME4000_AI_FULLSCALE_ENABLE:
3161 return me4000_ai_fullscale_enable(ai_context);
3162 case ME4000_AI_FULLSCALE_DISABLE:
3163 return me4000_ai_fullscale_disable(ai_context);
3164 case ME4000_AI_EEPROM_READ:
3165 return me4000_eeprom_read((struct me4000_eeprom *)arg,
3167 case ME4000_AI_EEPROM_WRITE:
3168 return me4000_eeprom_write((struct me4000_eeprom *)arg,
3172 "me4000_ai_ioctl_sing():Invalid service number\n");
3178 static int me4000_ai_single(struct me4000_ai_single *arg,
3179 struct me4000_ai_context *ai_context)
3181 struct me4000_ai_single cmd;
3184 wait_queue_head_t queue;
3185 unsigned long jiffy;
3187 CALL_PDEBUG("me4000_ai_single() is executed\n");
3189 init_waitqueue_head(&queue);
3191 /* Copy data from user */
3192 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_single));
3195 "ME4000:me4000_ai_single():Can't copy from user space\n");
3199 /* Check range parameter */
3200 switch (cmd.range) {
3201 case ME4000_AI_LIST_RANGE_BIPOLAR_10:
3202 case ME4000_AI_LIST_RANGE_BIPOLAR_2_5:
3203 case ME4000_AI_LIST_RANGE_UNIPOLAR_10:
3204 case ME4000_AI_LIST_RANGE_UNIPOLAR_2_5:
3208 "ME4000:me4000_ai_single():Invalid range specified\n");
3212 /* Check mode and channel number */
3214 case ME4000_AI_LIST_INPUT_SINGLE_ENDED:
3215 if (cmd.channel >= ai_context->board_info->board_p->ai.count) {
3217 "ME4000:me4000_ai_single():Analog input is not available\n");
3221 case ME4000_AI_LIST_INPUT_DIFFERENTIAL:
3223 ai_context->board_info->board_p->ai.diff_count) {
3225 "ME4000:me4000_ai_single():Analog input is not available in differential mode\n");
3231 "ME4000:me4000_ai_single():Invalid mode specified\n");
3235 /* Clear channel list, data fifo and both stop bits */
3236 tmp = me4000_inl(ai_context->ctrl_reg);
3238 ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_DATA_FIFO |
3239 ME4000_AI_CTRL_BIT_STOP | ME4000_AI_CTRL_BIT_IMMEDIATE_STOP);
3240 me4000_outl(tmp, ai_context->ctrl_reg);
3242 /* Enable channel list and data fifo */
3243 tmp |= ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_DATA_FIFO;
3244 me4000_outl(tmp, ai_context->ctrl_reg);
3246 /* Generate channel list entry */
3247 me4000_outl(cmd.channel | cmd.range | cmd.
3248 mode | ME4000_AI_LIST_LAST_ENTRY,
3249 ai_context->channel_list_reg);
3251 /* Set the timer to maximum */
3252 me4000_outl(66, ai_context->chan_timer_reg);
3253 me4000_outl(66, ai_context->chan_pre_timer_reg);
3255 if (tmp & ME4000_AI_CTRL_BIT_EX_TRIG) {
3258 (me4000_inl(ai_context->status_reg) &
3259 ME4000_AI_STATUS_BIT_EF_DATA)) {
3260 interruptible_sleep_on_timeout(&queue, 1);
3261 if (signal_pending(current)) {
3263 "ME4000:me4000_ai_single():Wait on start of state machine interrupted\n");
3266 /* 2.6 has different definitions for HZ in user and kernel space */
3267 if (((jiffies - jiffy) > (cmd.timeout * HZ / USER_HZ)) && cmd.timeout) {
3269 "ME4000:me4000_ai_single():Timeout reached\n");
3274 /* Start conversion */
3275 me4000_inl(ai_context->start_reg);
3277 /* Wait until ready */
3280 (me4000_inl(ai_context->status_reg) &
3281 ME4000_AI_STATUS_BIT_EF_DATA)) {
3283 "ME4000:me4000_ai_single():Value not available after wait\n");
3288 /* Read value from data fifo */
3289 cmd.value = me4000_inl(ai_context->data_reg) & 0xFFFF;
3291 /* Copy result back to user */
3292 err = copy_to_user(arg, &cmd, sizeof(struct me4000_ai_single));
3295 "ME4000:me4000_ai_single():Can't copy to user space\n");
3302 static int me4000_ai_ioctl_sw(struct inode *inode_p, struct file *file_p,
3303 unsigned int service, unsigned long arg)
3305 struct me4000_ai_context *ai_context;
3307 CALL_PDEBUG("me4000_ai_ioctl_sw() is executed\n");
3309 ai_context = file_p->private_data;
3311 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3312 printk(KERN_ERR "me4000_ai_ioctl_sw():Wrong magic number\n");
3315 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3317 "me4000_ai_ioctl_sw():Service number to high\n");
3322 case ME4000_AI_SC_SETUP:
3323 return me4000_ai_sc_setup((struct me4000_ai_sc *)arg,
3325 case ME4000_AI_CONFIG:
3326 return me4000_ai_config((struct me4000_ai_config *)arg,
3328 case ME4000_AI_START:
3329 return me4000_ai_start(ai_context);
3330 case ME4000_AI_STOP:
3331 return me4000_ai_stop(ai_context);
3332 case ME4000_AI_IMMEDIATE_STOP:
3333 return me4000_ai_immediate_stop(ai_context);
3334 case ME4000_AI_FSM_STATE:
3335 return me4000_ai_fsm_state((int *)arg, ai_context);
3336 case ME4000_GET_USER_INFO:
3337 return me4000_get_user_info((struct me4000_user_info *)arg,
3338 ai_context->board_info);
3339 case ME4000_AI_EEPROM_READ:
3340 return me4000_eeprom_read((struct me4000_eeprom *)arg,
3342 case ME4000_AI_EEPROM_WRITE:
3343 return me4000_eeprom_write((struct me4000_eeprom *)arg,
3345 case ME4000_AI_GET_COUNT_BUFFER:
3346 return me4000_ai_get_count_buffer((unsigned long *)arg,
3350 "%s:Invalid service number %d\n", __func__, service);
3356 static int me4000_ai_ioctl_ext(struct inode *inode_p, struct file *file_p,
3357 unsigned int service, unsigned long arg)
3359 struct me4000_ai_context *ai_context;
3361 CALL_PDEBUG("me4000_ai_ioctl_ext() is executed\n");
3363 ai_context = file_p->private_data;
3365 if (_IOC_TYPE(service) != ME4000_MAGIC) {
3366 printk(KERN_ERR "me4000_ai_ioctl_ext():Wrong magic number\n");
3369 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
3371 "me4000_ai_ioctl_ext():Service number to high\n");
3376 case ME4000_AI_SC_SETUP:
3377 return me4000_ai_sc_setup((struct me4000_ai_sc *)arg,
3379 case ME4000_AI_CONFIG:
3380 return me4000_ai_config((struct me4000_ai_config *)arg,
3382 case ME4000_AI_START:
3383 return me4000_ai_start_ex((unsigned long *)arg, ai_context);
3384 case ME4000_AI_STOP:
3385 return me4000_ai_stop(ai_context);
3386 case ME4000_AI_IMMEDIATE_STOP:
3387 return me4000_ai_immediate_stop(ai_context);
3388 case ME4000_AI_EX_TRIG_ENABLE:
3389 return me4000_ai_ex_trig_enable(ai_context);
3390 case ME4000_AI_EX_TRIG_DISABLE:
3391 return me4000_ai_ex_trig_disable(ai_context);
3392 case ME4000_AI_EX_TRIG_SETUP:
3393 return me4000_ai_ex_trig_setup((struct me4000_ai_trigger *)arg,
3395 case ME4000_AI_FSM_STATE:
3396 return me4000_ai_fsm_state((int *)arg, ai_context);
3397 case ME4000_GET_USER_INFO:
3398 return me4000_get_user_info((struct me4000_user_info *)arg,
3399 ai_context->board_info);
3400 case ME4000_AI_GET_COUNT_BUFFER:
3401 return me4000_ai_get_count_buffer((unsigned long *)arg,
3405 "%s:Invalid service number %d\n", __func__ , service);
3411 static int me4000_ai_fasync(int fd, struct file *file_p, int mode)
3413 struct me4000_ai_context *ai_context;
3415 CALL_PDEBUG("me4000_ao_fasync_cont() is executed\n");
3417 ai_context = file_p->private_data;
3418 return fasync_helper(fd, file_p, mode, &ai_context->fasync_p);
3421 static int me4000_ai_config(struct me4000_ai_config *arg,
3422 struct me4000_ai_context *ai_context)
3424 struct me4000_ai_config cmd;
3429 wait_queue_head_t queue;
3433 CALL_PDEBUG("me4000_ai_config() is executed\n");
3435 init_waitqueue_head(&queue);
3437 /* Check if conversion is stopped */
3438 if (inl(ai_context->ctrl_reg) & ME4000_AI_STATUS_BIT_FSM) {
3440 "ME4000:me4000_ai_config():Conversion is not stopped\n");
3445 /* Copy data from user */
3446 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_config));
3449 "ME4000:me4000_ai_config():Can't copy from user space\n");
3455 ("me4000_ai_config():chan = %ld, pre_chan = %ld, scan_low = %ld, scan_high = %ld, count = %ld\n",
3456 cmd.timer.chan, cmd.timer.pre_chan, cmd.timer.scan_low,
3457 cmd.timer.scan_high, cmd.channel_list.count);
3459 /* Check whether sample and hold is available for this board */
3461 if (!ai_context->board_info->board_p->ai.sh_count) {
3463 "ME4000:me4000_ai_config():Sample and Hold is not available for this board\n");
3469 /* Check the channel list size */
3470 if (cmd.channel_list.count > ME4000_AI_CHANNEL_LIST_COUNT) {
3472 "me4000_ai_config():Channel list is to large\n");
3477 /* Copy channel list from user */
3478 list = kmalloc(sizeof(u32) * cmd.channel_list.count, GFP_KERNEL);
3481 "ME4000:me4000_ai_config():Can't get memory for channel list\n");
3486 copy_from_user(list, cmd.channel_list.list,
3487 sizeof(u32) * cmd.channel_list.count);
3490 "ME4000:me4000_ai_config():Can't copy from user space\n");
3495 /* Check if last entry bit is set */
3496 if (!(list[cmd.channel_list.count - 1] & ME4000_AI_LIST_LAST_ENTRY)) {
3498 "me4000_ai_config():Last entry bit is not set\n");
3499 list[cmd.channel_list.count - 1] |= ME4000_AI_LIST_LAST_ENTRY;
3502 /* Check whether mode is equal for all entries */
3503 mode = list[0] & 0x20;
3504 for (i = 0; i < cmd.channel_list.count; i++) {
3505 if ((list[i] & 0x20) != mode) {
3507 "ME4000:me4000_ai_config():Mode is not equal for all entries\n");
3513 /* Check whether channels are available for this mode */
3514 if (mode == ME4000_AI_LIST_INPUT_SINGLE_ENDED) {
3515 for (i = 0; i < cmd.channel_list.count; i++) {
3516 if ((list[i] & 0x1F) >=
3517 ai_context->board_info->board_p->ai.count) {
3519 "ME4000:me4000_ai_config():Channel is not available for single ended\n");
3524 } else if (mode == ME4000_AI_LIST_INPUT_DIFFERENTIAL) {
3525 for (i = 0; i < cmd.channel_list.count; i++) {
3526 if ((list[i] & 0x1F) >=
3527 ai_context->board_info->board_p->ai.diff_count) {
3529 "ME4000:me4000_ai_config():Channel is not available for differential\n");
3536 /* Check if bipolar is set for all entries when in differential mode */
3537 if (mode == ME4000_AI_LIST_INPUT_DIFFERENTIAL) {
3538 for (i = 0; i < cmd.channel_list.count; i++) {
3539 if ((list[i] & 0xC0) != ME4000_AI_LIST_RANGE_BIPOLAR_10
3540 && (list[i] & 0xC0) !=
3541 ME4000_AI_LIST_RANGE_BIPOLAR_2_5) {
3543 "ME4000:me4000_ai_config():Bipolar is not selected in differential mode\n");
3550 if (ai_context->mode != ME4000_AI_ACQ_MODE_EXT_SINGLE_VALUE) {
3551 /* Check for minimum channel divisor */
3552 if (cmd.timer.chan < ME4000_AI_MIN_TICKS) {
3554 "ME4000:me4000_ai_config():Channel timer divisor is to low\n");
3559 /* Check if minimum channel divisor is adjusted when sample and hold is activated */
3560 if ((cmd.sh) && (cmd.timer.chan != ME4000_AI_MIN_TICKS)) {
3562 "ME4000:me4000_ai_config():Channel timer divisor must be at minimum when sample and hold is activated\n");
3567 /* Check for minimum channel pre divisor */
3568 if (cmd.timer.pre_chan < ME4000_AI_MIN_TICKS) {
3570 "ME4000:me4000_ai_config():Channel pre timer divisor is to low\n");
3575 /* Write the channel timers */
3576 me4000_outl(cmd.timer.chan - 1, ai_context->chan_timer_reg);
3577 me4000_outl(cmd.timer.pre_chan - 1,
3578 ai_context->chan_pre_timer_reg);
3580 /* Save the timer values in the board context */
3581 ai_context->chan_timer = cmd.timer.chan;
3582 ai_context->chan_pre_timer = cmd.timer.pre_chan;
3584 if (ai_context->mode != ME4000_AI_ACQ_MODE_EXT_SINGLE_CHANLIST) {
3585 /* Check for scan timer divisor */
3587 (u64) cmd.timer.scan_low | ((u64) cmd.timer.
3591 cmd.channel_list.count * cmd.timer.chan +
3594 "ME4000:me4000_ai_config():Scan timer divisor is to low\n");
3600 /* Write the scan timers */
3603 tmp = (u32) (scan & 0xFFFFFFFF);
3605 ai_context->scan_timer_low_reg);
3606 tmp = (u32) ((scan >> 32) & 0xFFFFFFFF);
3608 ai_context->scan_timer_high_reg);
3611 scan - (cmd.timer.chan - 1) +
3612 (cmd.timer.pre_chan - 1);
3613 tmp = (u32) (scan & 0xFFFFFFFF);
3615 ai_context->scan_pre_timer_low_reg);
3616 tmp = (u32) ((scan >> 32) & 0xFFFFFFFF);
3619 scan_pre_timer_high_reg);
3622 ai_context->scan_timer_low_reg);
3624 ai_context->scan_timer_high_reg);
3627 ai_context->scan_pre_timer_low_reg);
3630 scan_pre_timer_high_reg);
3633 ai_context->scan_timer_low = cmd.timer.scan_low;
3634 ai_context->scan_timer_high = cmd.timer.scan_high;
3638 /* Clear the channel list */
3639 tmp = me4000_inl(ai_context->ctrl_reg);
3640 tmp &= ~ME4000_AI_CTRL_BIT_CHANNEL_FIFO;
3641 me4000_outl(tmp, ai_context->ctrl_reg);
3642 tmp |= ME4000_AI_CTRL_BIT_CHANNEL_FIFO;
3643 me4000_outl(tmp, ai_context->ctrl_reg);
3645 /* Write the channel list */
3646 for (i = 0; i < cmd.channel_list.count; i++)
3647 me4000_outl(list[i], ai_context->channel_list_reg);
3649 /* Setup sample and hold */
3651 tmp |= ME4000_AI_CTRL_BIT_SAMPLE_HOLD;
3652 me4000_outl(tmp, ai_context->ctrl_reg);
3654 tmp &= ~ME4000_AI_CTRL_BIT_SAMPLE_HOLD;
3655 me4000_outl(tmp, ai_context->ctrl_reg);
3658 /* Save the channel list size in the board context */
3659 ai_context->channel_list_count = cmd.channel_list.count;
3667 /* Reset the timers */
3668 ai_context->chan_timer = 66;
3669 ai_context->chan_pre_timer = 66;
3670 ai_context->scan_timer_low = 0;
3671 ai_context->scan_timer_high = 0;
3673 me4000_outl(65, ai_context->chan_timer_reg);
3674 me4000_outl(65, ai_context->chan_pre_timer_reg);
3675 me4000_outl(0, ai_context->scan_timer_high_reg);
3676 me4000_outl(0, ai_context->scan_timer_low_reg);
3677 me4000_outl(0, ai_context->scan_pre_timer_high_reg);
3678 me4000_outl(0, ai_context->scan_pre_timer_low_reg);
3680 ai_context->channel_list_count = 0;
3682 tmp = me4000_inl(ai_context->ctrl_reg);
3684 ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_SAMPLE_HOLD);
3692 static int ai_common_start(struct me4000_ai_context *ai_context)
3695 CALL_PDEBUG("ai_common_start() is executed\n");
3697 tmp = me4000_inl(ai_context->ctrl_reg);
3699 /* Check if conversion is stopped */
3700 if (tmp & ME4000_AI_STATUS_BIT_FSM) {
3702 "ME4000:ai_common_start():Conversion is not stopped\n");
3706 /* Clear data fifo, disable all interrupts, clear sample counter reload */
3707 tmp &= ~(ME4000_AI_CTRL_BIT_DATA_FIFO | ME4000_AI_CTRL_BIT_LE_IRQ |
3708 ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3709 ME4000_AI_CTRL_BIT_SC_RELOAD);
3711 me4000_outl(tmp, ai_context->ctrl_reg);
3713 /* Clear circular buffer */
3714 ai_context->circ_buf.head = 0;
3715 ai_context->circ_buf.tail = 0;
3717 /* Enable data fifo */
3718 tmp |= ME4000_AI_CTRL_BIT_DATA_FIFO;
3720 /* Determine interrupt setup */
3721 if (ai_context->sample_counter && !ai_context->sample_counter_reload) {
3722 /* Enable Half Full Interrupt and Sample Counter Interrupt */
3723 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ | ME4000_AI_CTRL_BIT_HF_IRQ;
3724 } else if (ai_context->sample_counter
3725 && ai_context->sample_counter_reload) {
3726 if (ai_context->sample_counter <= ME4000_AI_FIFO_COUNT / 2) {
3727 /* Enable only Sample Counter Interrupt */
3729 ME4000_AI_CTRL_BIT_SC_IRQ |
3730 ME4000_AI_CTRL_BIT_SC_RELOAD;
3732 /* Enable Half Full Interrupt and Sample Counter Interrupt */
3734 ME4000_AI_CTRL_BIT_SC_IRQ |
3735 ME4000_AI_CTRL_BIT_HF_IRQ |
3736 ME4000_AI_CTRL_BIT_SC_RELOAD;
3739 /* Enable only Half Full Interrupt */
3740 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ;
3743 /* Clear the stop bits */
3744 tmp &= ~(ME4000_AI_CTRL_BIT_STOP | ME4000_AI_CTRL_BIT_IMMEDIATE_STOP);
3746 /* Write setup to hardware */
3747 me4000_outl(tmp, ai_context->ctrl_reg);
3749 /* Write sample counter */
3750 me4000_outl(ai_context->sample_counter, ai_context->sample_counter_reg);
3755 static int me4000_ai_start(struct me4000_ai_context *ai_context)
3758 CALL_PDEBUG("me4000_ai_start() is executed\n");
3760 /* Prepare Hardware */
3761 err = ai_common_start(ai_context);
3765 /* Start conversion by dummy read */
3766 me4000_inl(ai_context->start_reg);
3771 static int me4000_ai_start_ex(unsigned long *arg,
3772 struct me4000_ai_context *ai_context)
3775 wait_queue_head_t queue;
3777 unsigned long timeout;
3779 CALL_PDEBUG("me4000_ai_start_ex() is executed\n");
3781 if (get_user(timeout, arg)) {
3783 "me4000_ai_start_ex():Cannot copy data from user\n");
3787 init_waitqueue_head(&queue);
3789 /* Prepare Hardware */
3790 err = ai_common_start(ai_context);
3796 while (!(inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM)) {
3797 interruptible_sleep_on_timeout(&queue, 1);
3798 if (signal_pending(current)) {
3800 "ME4000:me4000_ai_start_ex():Wait on start of state machine interrupted\n");
3803 /* 2.6 has different definitions for HZ in user and kernel space */
3804 if ((jiffies - ref) > (timeout * HZ / USER_HZ)) {
3806 "ME4000:me4000_ai_start_ex():Timeout reached\n");
3811 while (!(inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM)) {
3812 interruptible_sleep_on_timeout(&queue, 1);
3813 if (signal_pending(current)) {
3815 "ME4000:me4000_ai_start_ex():Wait on start of state machine interrupted\n");
3824 static int me4000_ai_stop(struct me4000_ai_context *ai_context)
3826 wait_queue_head_t queue;
3828 unsigned long flags;
3830 CALL_PDEBUG("me4000_ai_stop() is executed\n");
3832 init_waitqueue_head(&queue);
3834 /* Disable irqs and clear data fifo */
3835 spin_lock_irqsave(&ai_context->int_lock, flags);
3836 tmp = me4000_inl(ai_context->ctrl_reg);
3838 ~(ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3839 ME4000_AI_CTRL_BIT_DATA_FIFO);
3840 /* Stop conversion of the state machine */
3841 tmp |= ME4000_AI_CTRL_BIT_STOP;
3842 me4000_outl(tmp, ai_context->ctrl_reg);
3843 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3845 /* Clear circular buffer */
3846 ai_context->circ_buf.head = 0;
3847 ai_context->circ_buf.tail = 0;
3849 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3850 interruptible_sleep_on_timeout(&queue, 1);
3851 if (signal_pending(current)) {
3853 "ME4000:me4000_ai_stop():Wait on state machine after stop interrupted\n");
3861 static int me4000_ai_immediate_stop(struct me4000_ai_context *ai_context)
3863 wait_queue_head_t queue;
3865 unsigned long flags;
3867 CALL_PDEBUG("me4000_ai_stop() is executed\n");
3869 init_waitqueue_head(&queue);
3871 /* Disable irqs and clear data fifo */
3872 spin_lock_irqsave(&ai_context->int_lock, flags);
3873 tmp = me4000_inl(ai_context->ctrl_reg);
3875 ~(ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ |
3876 ME4000_AI_CTRL_BIT_DATA_FIFO);
3877 /* Stop conversion of the state machine */
3878 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
3879 me4000_outl(tmp, ai_context->ctrl_reg);
3880 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3882 /* Clear circular buffer */
3883 ai_context->circ_buf.head = 0;
3884 ai_context->circ_buf.tail = 0;
3886 while (inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) {
3887 interruptible_sleep_on_timeout(&queue, 1);
3888 if (signal_pending(current)) {
3890 "ME4000:me4000_ai_stop():Wait on state machine after stop interrupted\n");
3898 static int me4000_ai_ex_trig_enable(struct me4000_ai_context *ai_context)
3901 unsigned long flags;
3903 CALL_PDEBUG("me4000_ai_ex_trig_enable() is executed\n");
3905 spin_lock_irqsave(&ai_context->int_lock, flags);
3906 tmp = me4000_inl(ai_context->ctrl_reg);
3907 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG;
3908 me4000_outl(tmp, ai_context->ctrl_reg);
3909 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3914 static int me4000_ai_ex_trig_disable(struct me4000_ai_context *ai_context)
3917 unsigned long flags;
3919 CALL_PDEBUG("me4000_ai_ex_trig_disable() is executed\n");
3921 spin_lock_irqsave(&ai_context->int_lock, flags);
3922 tmp = me4000_inl(ai_context->ctrl_reg);
3923 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG;
3924 me4000_outl(tmp, ai_context->ctrl_reg);
3925 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3930 static int me4000_ai_ex_trig_setup(struct me4000_ai_trigger *arg,
3931 struct me4000_ai_context *ai_context)
3933 struct me4000_ai_trigger cmd;
3936 unsigned long flags;
3938 CALL_PDEBUG("me4000_ai_ex_trig_setup() is executed\n");
3940 /* Copy data from user */
3941 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_trigger));
3944 "ME4000:me4000_ai_ex_trig_setup():Can't copy from user space\n");
3948 spin_lock_irqsave(&ai_context->int_lock, flags);
3949 tmp = me4000_inl(ai_context->ctrl_reg);
3951 if (cmd.mode == ME4000_AI_TRIGGER_EXT_DIGITAL) {
3952 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG;
3953 } else if (cmd.mode == ME4000_AI_TRIGGER_EXT_ANALOG) {
3954 if (!ai_context->board_info->board_p->ai.ex_trig_analog) {
3956 "ME4000:me4000_ai_ex_trig_setup():No analog trigger available\n");
3959 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG;
3961 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3963 "ME4000:me4000_ai_ex_trig_setup():Invalid trigger mode specified\n");
3967 if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_RISING) {
3969 ~(ME4000_AI_CTRL_BIT_EX_TRIG_BOTH |
3970 ME4000_AI_CTRL_BIT_EX_TRIG_FALLING);
3971 } else if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_FALLING) {
3972 tmp |= ME4000_AI_CTRL_BIT_EX_TRIG_FALLING;
3973 tmp &= ~ME4000_AI_CTRL_BIT_EX_TRIG_BOTH;
3974 } else if (cmd.edge == ME4000_AI_TRIGGER_EXT_EDGE_BOTH) {
3976 ME4000_AI_CTRL_BIT_EX_TRIG_BOTH |
3977 ME4000_AI_CTRL_BIT_EX_TRIG_FALLING;
3979 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3981 "ME4000:me4000_ai_ex_trig_setup():Invalid trigger edge specified\n");
3985 me4000_outl(tmp, ai_context->ctrl_reg);
3986 spin_unlock_irqrestore(&ai_context->int_lock, flags);
3990 static int me4000_ai_sc_setup(struct me4000_ai_sc *arg,
3991 struct me4000_ai_context *ai_context)
3993 struct me4000_ai_sc cmd;
3996 CALL_PDEBUG("me4000_ai_sc_setup() is executed\n");
3998 /* Copy data from user */
3999 err = copy_from_user(&cmd, arg, sizeof(struct me4000_ai_sc));
4002 "ME4000:me4000_ai_sc_setup():Can't copy from user space\n");
4006 ai_context->sample_counter = cmd.value;
4007 ai_context->sample_counter_reload = cmd.reload;
4012 static ssize_t me4000_ai_read(struct file *filep, char *buff, size_t cnt,
4015 struct me4000_ai_context *ai_context = filep->private_data;
4016 s16 *buffer = (s16 *) buff;
4017 size_t count = cnt / 2;
4018 unsigned long flags;
4025 CALL_PDEBUG("me4000_ai_read() is executed\n");
4027 init_waitqueue_entry(&wait, current);
4031 PDEBUG("me4000_ai_read():Count is 0\n");
4036 if (filep->f_flags & O_NONBLOCK) {
4037 c = me4000_values_to_end(ai_context->circ_buf,
4038 ME4000_AI_BUFFER_COUNT);
4041 ("me4000_ai_read():Returning from nonblocking read\n");
4045 /* Check if conversion is still running */
4047 (me4000_inl(ai_context->status_reg) &
4048 ME4000_AI_STATUS_BIT_FSM)) {
4050 "ME4000:me4000_ai_read():Conversion interrupted\n");
4054 wait_event_interruptible(ai_context->wait_queue,
4055 (me4000_values_to_end
4056 (ai_context->circ_buf,
4057 ME4000_AI_BUFFER_COUNT)));
4058 if (signal_pending(current)) {
4060 "ME4000:me4000_ai_read():Wait on values interrupted from signal\n");
4065 /* Only read count values or as much as available */
4066 c = me4000_values_to_end(ai_context->circ_buf,
4067 ME4000_AI_BUFFER_COUNT);
4068 PDEBUG("me4000_ai_read():%d values to end\n", c);
4072 PDEBUG("me4000_ai_read():Copy %d values to user space\n", c);
4074 k -= copy_to_user(buffer,
4075 ai_context->circ_buf.buf +
4076 ai_context->circ_buf.tail, k);
4080 "ME4000:me4000_ai_read():Cannot copy new values to user\n");
4084 ai_context->circ_buf.tail =
4085 (ai_context->circ_buf.tail + c) & (ME4000_AI_BUFFER_COUNT -
4091 spin_lock_irqsave(&ai_context->int_lock, flags);
4092 if (me4000_buf_space
4093 (ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
4094 tmp = me4000_inl(ai_context->ctrl_reg);
4096 /* Determine interrupt setup */
4097 if (ai_context->sample_counter
4098 && !ai_context->sample_counter_reload) {
4099 /* Enable Half Full Interrupt and Sample Counter Interrupt */
4101 ME4000_AI_CTRL_BIT_SC_IRQ |
4102 ME4000_AI_CTRL_BIT_HF_IRQ;
4103 } else if (ai_context->sample_counter
4104 && ai_context->sample_counter_reload) {
4105 if (ai_context->sample_counter <
4106 ME4000_AI_FIFO_COUNT / 2) {
4107 /* Enable only Sample Counter Interrupt */
4108 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ;
4110 /* Enable Half Full Interrupt and Sample Counter Interrupt */
4112 ME4000_AI_CTRL_BIT_SC_IRQ |
4113 ME4000_AI_CTRL_BIT_HF_IRQ;
4116 /* Enable only Half Full Interrupt */
4117 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ;
4120 me4000_outl(tmp, ai_context->ctrl_reg);
4122 spin_unlock_irqrestore(&ai_context->int_lock, flags);
4125 /* Check if conversion is still running */
4126 if (!(me4000_inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM)) {
4128 "ME4000:me4000_ai_read():Conversion not running after complete read\n");
4132 if (filep->f_flags & O_NONBLOCK)
4133 return (k == 0) ? -EAGAIN : 2 * ret;
4135 CALL_PDEBUG("me4000_ai_read() is leaved\n");
4139 static unsigned int me4000_ai_poll(struct file *file_p, poll_table *wait)
4141 struct me4000_ai_context *ai_context;
4142 unsigned long mask = 0;
4144 CALL_PDEBUG("me4000_ai_poll() is executed\n");
4146 ai_context = file_p->private_data;
4148 /* Register wait queue */
4149 poll_wait(file_p, &ai_context->wait_queue, wait);
4151 /* Get available values */
4152 if (me4000_values_to_end(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT))
4153 mask |= POLLIN | POLLRDNORM;
4155 PDEBUG("me4000_ai_poll():Return mask %lX\n", mask);
4160 static int me4000_ai_offset_enable(struct me4000_ai_context *ai_context)
4164 CALL_PDEBUG("me4000_ai_offset_enable() is executed\n");
4166 tmp = me4000_inl(ai_context->ctrl_reg);
4167 tmp |= ME4000_AI_CTRL_BIT_OFFSET;
4168 me4000_outl(tmp, ai_context->ctrl_reg);
4173 static int me4000_ai_offset_disable(struct me4000_ai_context *ai_context)
4177 CALL_PDEBUG("me4000_ai_offset_disable() is executed\n");
4179 tmp = me4000_inl(ai_context->ctrl_reg);
4180 tmp &= ~ME4000_AI_CTRL_BIT_OFFSET;
4181 me4000_outl(tmp, ai_context->ctrl_reg);
4186 static int me4000_ai_fullscale_enable(struct me4000_ai_context *ai_context)
4190 CALL_PDEBUG("me4000_ai_fullscale_enable() is executed\n");
4192 tmp = me4000_inl(ai_context->ctrl_reg);
4193 tmp |= ME4000_AI_CTRL_BIT_FULLSCALE;
4194 me4000_outl(tmp, ai_context->ctrl_reg);
4199 static int me4000_ai_fullscale_disable(struct me4000_ai_context *ai_context)
4203 CALL_PDEBUG("me4000_ai_fullscale_disable() is executed\n");
4205 tmp = me4000_inl(ai_context->ctrl_reg);
4206 tmp &= ~ME4000_AI_CTRL_BIT_FULLSCALE;
4207 me4000_outl(tmp, ai_context->ctrl_reg);
4212 static int me4000_ai_fsm_state(int *arg, struct me4000_ai_context *ai_context)
4216 CALL_PDEBUG("me4000_ai_fsm_state() is executed\n");
4219 (me4000_inl(ai_context->status_reg) & ME4000_AI_STATUS_BIT_FSM) ? 1
4222 if (put_user(tmp, arg)) {
4223 printk(KERN_ERR "me4000_ai_fsm_state():Cannot copy to user\n");
4230 static int me4000_ai_get_count_buffer(unsigned long *arg,
4231 struct me4000_ai_context *ai_context)
4236 c = me4000_buf_count(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT);
4238 err = copy_to_user(arg, &c, sizeof(unsigned long));
4241 "%s:Can't copy to user space\n", __func__);
4248 /*---------------------------------- EEPROM stuff ---------------------------*/
4250 static int eeprom_write_cmd(struct me4000_ai_context *ai_context, unsigned long cmd,
4254 unsigned long value;
4256 CALL_PDEBUG("eeprom_write_cmd() is executed\n");
4258 PDEBUG("eeprom_write_cmd():Write command 0x%08lX with length = %d\n",
4261 /* Get the ICR register and clear the related bits */
4262 value = me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR);
4263 value &= ~(PLX_ICR_MASK_EEPROM);
4264 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4266 /* Raise the chip select */
4267 value |= PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4268 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4269 udelay(EEPROM_DELAY);
4271 for (i = 0; i < length; i++) {
4272 if (cmd & ((0x1 << (length - 1)) >> i))
4273 value |= PLX_ICR_BIT_EEPROM_WRITE;
4275 value &= ~PLX_ICR_BIT_EEPROM_WRITE;
4277 /* Write to EEPROM */
4279 ai_context->board_info->plx_regbase + PLX_ICR);
4280 udelay(EEPROM_DELAY);
4282 /* Raising edge of the clock */
4283 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4285 ai_context->board_info->plx_regbase + PLX_ICR);
4286 udelay(EEPROM_DELAY);
4288 /* Falling edge of the clock */
4289 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4291 ai_context->board_info->plx_regbase + PLX_ICR);
4292 udelay(EEPROM_DELAY);
4295 /* Clear the chip select */
4296 value &= ~PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4297 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4298 udelay(EEPROM_DELAY);
4300 /* Wait until hardware is ready for sure */
4306 static unsigned short eeprom_read_cmd(struct me4000_ai_context *ai_context,
4307 unsigned long cmd, int length)
4310 unsigned long value;
4311 unsigned short id = 0;
4313 CALL_PDEBUG("eeprom_read_cmd() is executed\n");
4315 PDEBUG("eeprom_read_cmd():Read command 0x%08lX with length = %d\n", cmd,
4318 /* Get the ICR register and clear the related bits */
4319 value = me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR);
4320 value &= ~(PLX_ICR_MASK_EEPROM);
4322 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4324 /* Raise the chip select */
4325 value |= PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4326 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4327 udelay(EEPROM_DELAY);
4329 /* Write the read command to the eeprom */
4330 for (i = 0; i < length; i++) {
4331 if (cmd & ((0x1 << (length - 1)) >> i))
4332 value |= PLX_ICR_BIT_EEPROM_WRITE;
4334 value &= ~PLX_ICR_BIT_EEPROM_WRITE;
4337 ai_context->board_info->plx_regbase + PLX_ICR);
4338 udelay(EEPROM_DELAY);
4340 /* Raising edge of the clock */
4341 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4343 ai_context->board_info->plx_regbase + PLX_ICR);
4344 udelay(EEPROM_DELAY);
4346 /* Falling edge of the clock */
4347 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4349 ai_context->board_info->plx_regbase + PLX_ICR);
4350 udelay(EEPROM_DELAY);
4353 /* Read the value from the eeprom */
4354 for (i = 0; i < 16; i++) {
4355 /* Raising edge of the clock */
4356 value |= PLX_ICR_BIT_EEPROM_CLOCK_SET;
4358 ai_context->board_info->plx_regbase + PLX_ICR);
4359 udelay(EEPROM_DELAY);
4361 if (me4000_inl(ai_context->board_info->plx_regbase + PLX_ICR) &
4362 PLX_ICR_BIT_EEPROM_READ) {
4363 id |= (0x8000 >> i);
4364 PDEBUG("eeprom_read_cmd():OR with 0x%04X\n",
4367 PDEBUG("eeprom_read_cmd():Dont't OR\n");
4370 /* Falling edge of the clock */
4371 value &= ~PLX_ICR_BIT_EEPROM_CLOCK_SET;
4373 ai_context->board_info->plx_regbase + PLX_ICR);
4374 udelay(EEPROM_DELAY);
4377 /* Clear the chip select */
4378 value &= ~PLX_ICR_BIT_EEPROM_CHIP_SELECT;
4379 me4000_outl(value, ai_context->board_info->plx_regbase + PLX_ICR);
4380 udelay(EEPROM_DELAY);
4385 static int me4000_eeprom_write(struct me4000_eeprom *arg,
4386 struct me4000_ai_context *ai_context)
4389 struct me4000_eeprom setup;
4391 unsigned long date_high;
4392 unsigned long date_low;
4394 CALL_PDEBUG("me4000_eeprom_write() is executed\n");
4396 err = copy_from_user(&setup, arg, sizeof(setup));
4399 "ME4000:me4000_eeprom_write():Cannot copy from user\n");
4403 /* Enable writing */
4404 eeprom_write_cmd(ai_context, ME4000_EEPROM_CMD_WRITE_ENABLE,
4405 ME4000_EEPROM_CMD_LENGTH_WRITE_ENABLE);
4407 /* Command for date */
4408 date_high = (setup.date & 0xFFFF0000) >> 16;
4409 date_low = (setup.date & 0x0000FFFF);
4412 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_DATE_HIGH <<
4413 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4417 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4422 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_DATE_LOW <<
4423 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4427 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4431 /* Command for unipolar 10V offset */
4433 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_UNI_OFFSET <<
4434 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4439 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4443 /* Command for unipolar 10V fullscale */
4445 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_UNI_FULLSCALE <<
4446 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4451 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4455 /* Command for unipolar 2,5V offset */
4457 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_UNI_OFFSET <<
4458 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4463 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4467 /* Command for unipolar 2,5V fullscale */
4469 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_UNI_FULLSCALE <<
4470 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4475 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4479 /* Command for bipolar 10V offset */
4481 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_BI_OFFSET <<
4482 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4487 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4491 /* Command for bipolar 10V fullscale */
4493 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_BI_FULLSCALE <<
4494 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4499 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4503 /* Command for bipolar 2,5V offset */
4505 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_BI_OFFSET <<
4506 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4511 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4515 /* Command for bipolar 2,5V fullscale */
4517 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_BI_FULLSCALE <<
4518 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4523 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4527 /* Command for differential 10V offset */
4529 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_DIFF_OFFSET <<
4530 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4535 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4539 /* Command for differential 10V fullscale */
4541 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_1_DIFF_FULLSCALE
4542 << ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4547 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4551 /* Command for differential 2,5V offset */
4553 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_DIFF_OFFSET <<
4554 ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4559 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4563 /* Command for differential 2,5V fullscale */
4565 ME4000_EEPROM_CMD_WRITE | (ME4000_EEPROM_ADR_GAIN_4_DIFF_FULLSCALE
4566 << ME4000_EEPROM_DATA_LENGTH) | (0xFFFF &
4570 diff_2_5_fullscale);
4571 err = eeprom_write_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_WRITE);
4575 /* Disable writing */
4576 eeprom_write_cmd(ai_context, ME4000_EEPROM_CMD_WRITE_DISABLE,
4577 ME4000_EEPROM_CMD_LENGTH_WRITE_DISABLE);
4582 static int me4000_eeprom_read(struct me4000_eeprom *arg,
4583 struct me4000_ai_context *ai_context)
4587 struct me4000_eeprom setup;
4589 CALL_PDEBUG("me4000_eeprom_read() is executed\n");
4591 /* Command for date */
4592 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_DATE_HIGH;
4594 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4596 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_DATE_LOW;
4598 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4600 /* Command for unipolar 10V offset */
4601 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_UNI_OFFSET;
4602 setup.uni_10_offset =
4603 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4605 /* Command for unipolar 10V fullscale */
4606 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_UNI_FULLSCALE;
4607 setup.uni_10_fullscale =
4608 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4610 /* Command for unipolar 2,5V offset */
4611 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_UNI_OFFSET;
4612 setup.uni_2_5_offset =
4613 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4615 /* Command for unipolar 2,5V fullscale */
4616 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_UNI_FULLSCALE;
4617 setup.uni_2_5_fullscale =
4618 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4620 /* Command for bipolar 10V offset */
4621 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_BI_OFFSET;
4622 setup.bi_10_offset =
4623 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4625 /* Command for bipolar 10V fullscale */
4626 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_BI_FULLSCALE;
4627 setup.bi_10_fullscale =
4628 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4630 /* Command for bipolar 2,5V offset */
4631 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_BI_OFFSET;
4632 setup.bi_2_5_offset =
4633 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4635 /* Command for bipolar 2,5V fullscale */
4636 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_BI_FULLSCALE;
4637 setup.bi_2_5_fullscale =
4638 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4640 /* Command for differntial 10V offset */
4641 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_DIFF_OFFSET;
4642 setup.diff_10_offset =
4643 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4645 /* Command for differential 10V fullscale */
4646 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_1_DIFF_FULLSCALE;
4647 setup.diff_10_fullscale =
4648 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4650 /* Command for differntial 2,5V offset */
4651 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_DIFF_OFFSET;
4652 setup.diff_2_5_offset =
4653 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4655 /* Command for differential 2,5V fullscale */
4656 cmd = ME4000_EEPROM_CMD_READ | ME4000_EEPROM_ADR_GAIN_4_DIFF_FULLSCALE;
4657 setup.diff_2_5_fullscale =
4658 eeprom_read_cmd(ai_context, cmd, ME4000_EEPROM_CMD_LENGTH_READ);
4660 err = copy_to_user(arg, &setup, sizeof(setup));
4663 "ME4000:me4000_eeprom_read():Cannot copy to user\n");
4670 /*------------------------------------ DIO stuff ----------------------------------------------*/
4672 static int me4000_dio_ioctl(struct inode *inode_p, struct file *file_p,
4673 unsigned int service, unsigned long arg)
4675 struct me4000_dio_context *dio_context;
4677 CALL_PDEBUG("me4000_dio_ioctl() is executed\n");
4679 dio_context = file_p->private_data;
4681 if (_IOC_TYPE(service) != ME4000_MAGIC) {
4682 printk(KERN_ERR "me4000_dio_ioctl():Wrong magic number\n");
4685 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
4686 printk(KERN_ERR "me4000_dio_ioctl():Service number to high\n");
4691 case ME4000_DIO_CONFIG:
4692 return me4000_dio_config((struct me4000_dio_config *)arg,
4694 case ME4000_DIO_SET_BYTE:
4695 return me4000_dio_set_byte((struct me4000_dio_byte *)arg,
4697 case ME4000_DIO_GET_BYTE:
4698 return me4000_dio_get_byte((struct me4000_dio_byte *)arg,
4700 case ME4000_DIO_RESET:
4701 return me4000_dio_reset(dio_context);
4704 "ME4000:me4000_dio_ioctl():Invalid service number %d\n",
4711 static int me4000_dio_config(struct me4000_dio_config *arg,
4712 struct me4000_dio_context *dio_context)
4714 struct me4000_dio_config cmd;
4718 CALL_PDEBUG("me4000_dio_config() is executed\n");
4720 /* Copy data from user */
4721 err = copy_from_user(&cmd, arg, sizeof(struct me4000_dio_config));
4724 "ME4000:me4000_dio_config():Can't copy from user space\n");
4728 /* Check port parameter */
4729 if (cmd.port >= dio_context->dio_count) {
4731 "ME4000:me4000_dio_config():Port %d is not available\n",
4736 PDEBUG("me4000_dio_config(): port %d, mode %d, function %d\n", cmd.port,
4737 cmd.mode, cmd.function);
4739 if (cmd.port == ME4000_DIO_PORT_A) {
4740 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4741 /* Check if opto isolated version */
4742 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4744 "ME4000:me4000_dio_config():Cannot set to input on opto isolated versions\n");
4748 tmp = me4000_inl(dio_context->ctrl_reg);
4750 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4751 ME4000_DIO_CTRL_BIT_MODE_1);
4752 me4000_outl(tmp, dio_context->ctrl_reg);
4753 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4754 tmp = me4000_inl(dio_context->ctrl_reg);
4756 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4757 ME4000_DIO_CTRL_BIT_MODE_1);
4758 tmp |= ME4000_DIO_CTRL_BIT_MODE_0;
4759 me4000_outl(tmp, dio_context->ctrl_reg);
4760 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4761 tmp = me4000_inl(dio_context->ctrl_reg);
4763 ~(ME4000_DIO_CTRL_BIT_MODE_0 |
4764 ME4000_DIO_CTRL_BIT_MODE_1 |
4765 ME4000_DIO_CTRL_BIT_FIFO_HIGH_0);
4767 ME4000_DIO_CTRL_BIT_MODE_0 |
4768 ME4000_DIO_CTRL_BIT_MODE_1;
4769 me4000_outl(tmp, dio_context->ctrl_reg);
4770 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4771 tmp = me4000_inl(dio_context->ctrl_reg);
4773 ME4000_DIO_CTRL_BIT_MODE_0 |
4774 ME4000_DIO_CTRL_BIT_MODE_1 |
4775 ME4000_DIO_CTRL_BIT_FIFO_HIGH_0;
4776 me4000_outl(tmp, dio_context->ctrl_reg);
4779 "ME4000:me4000_dio_config():Mode %d is not available\n",
4783 } else if (cmd.port == ME4000_DIO_PORT_B) {
4784 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4785 /* Only do anything when TTL version is installed */
4786 if ((me4000_inl(dio_context->dir_reg) & 0x1)) {
4787 tmp = me4000_inl(dio_context->ctrl_reg);
4789 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4790 ME4000_DIO_CTRL_BIT_MODE_3);
4791 me4000_outl(tmp, dio_context->ctrl_reg);
4793 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4794 /* Check if opto isolated version */
4795 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4797 "ME4000:me4000_dio_config():Cannot set to output on opto isolated versions\n");
4801 tmp = me4000_inl(dio_context->ctrl_reg);
4803 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4804 ME4000_DIO_CTRL_BIT_MODE_3);
4805 tmp |= ME4000_DIO_CTRL_BIT_MODE_2;
4806 me4000_outl(tmp, dio_context->ctrl_reg);
4807 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4808 /* Check if opto isolated version */
4809 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4811 "ME4000:me4000_dio_config():Cannot set to FIFO low output on opto isolated versions\n");
4815 tmp = me4000_inl(dio_context->ctrl_reg);
4817 ~(ME4000_DIO_CTRL_BIT_MODE_2 |
4818 ME4000_DIO_CTRL_BIT_MODE_3 |
4819 ME4000_DIO_CTRL_BIT_FIFO_HIGH_1);
4821 ME4000_DIO_CTRL_BIT_MODE_2 |
4822 ME4000_DIO_CTRL_BIT_MODE_3;
4823 me4000_outl(tmp, dio_context->ctrl_reg);
4824 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4825 /* Check if opto isolated version */
4826 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
4828 "ME4000:me4000_dio_config():Cannot set to FIFO high output on opto isolated versions\n");
4832 tmp = me4000_inl(dio_context->ctrl_reg);
4834 ME4000_DIO_CTRL_BIT_MODE_2 |
4835 ME4000_DIO_CTRL_BIT_MODE_3 |
4836 ME4000_DIO_CTRL_BIT_FIFO_HIGH_1;
4837 me4000_outl(tmp, dio_context->ctrl_reg);
4840 "ME4000:me4000_dio_config():Mode %d is not available\n",
4844 } else if (cmd.port == ME4000_DIO_PORT_C) {
4845 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4846 tmp = me4000_inl(dio_context->ctrl_reg);
4848 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4849 ME4000_DIO_CTRL_BIT_MODE_5);
4850 me4000_outl(tmp, dio_context->ctrl_reg);
4851 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4852 tmp = me4000_inl(dio_context->ctrl_reg);
4854 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4855 ME4000_DIO_CTRL_BIT_MODE_5);
4856 tmp |= ME4000_DIO_CTRL_BIT_MODE_4;
4857 me4000_outl(tmp, dio_context->ctrl_reg);
4858 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4859 tmp = me4000_inl(dio_context->ctrl_reg);
4861 ~(ME4000_DIO_CTRL_BIT_MODE_4 |
4862 ME4000_DIO_CTRL_BIT_MODE_5 |
4863 ME4000_DIO_CTRL_BIT_FIFO_HIGH_2);
4865 ME4000_DIO_CTRL_BIT_MODE_4 |
4866 ME4000_DIO_CTRL_BIT_MODE_5;
4867 me4000_outl(tmp, dio_context->ctrl_reg);
4868 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4869 tmp = me4000_inl(dio_context->ctrl_reg);
4871 ME4000_DIO_CTRL_BIT_MODE_4 |
4872 ME4000_DIO_CTRL_BIT_MODE_5 |
4873 ME4000_DIO_CTRL_BIT_FIFO_HIGH_2;
4874 me4000_outl(tmp, dio_context->ctrl_reg);
4877 "ME4000:me4000_dio_config():Mode %d is not available\n",
4881 } else if (cmd.port == ME4000_DIO_PORT_D) {
4882 if (cmd.mode == ME4000_DIO_PORT_INPUT) {
4883 tmp = me4000_inl(dio_context->ctrl_reg);
4885 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4886 ME4000_DIO_CTRL_BIT_MODE_7);
4887 me4000_outl(tmp, dio_context->ctrl_reg);
4888 } else if (cmd.mode == ME4000_DIO_PORT_OUTPUT) {
4889 tmp = me4000_inl(dio_context->ctrl_reg);
4891 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4892 ME4000_DIO_CTRL_BIT_MODE_7);
4893 tmp |= ME4000_DIO_CTRL_BIT_MODE_6;
4894 me4000_outl(tmp, dio_context->ctrl_reg);
4895 } else if (cmd.mode == ME4000_DIO_FIFO_LOW) {
4896 tmp = me4000_inl(dio_context->ctrl_reg);
4898 ~(ME4000_DIO_CTRL_BIT_MODE_6 |
4899 ME4000_DIO_CTRL_BIT_MODE_7 |
4900 ME4000_DIO_CTRL_BIT_FIFO_HIGH_3);
4902 ME4000_DIO_CTRL_BIT_MODE_6 |
4903 ME4000_DIO_CTRL_BIT_MODE_7;
4904 me4000_outl(tmp, dio_context->ctrl_reg);
4905 } else if (cmd.mode == ME4000_DIO_FIFO_HIGH) {
4906 tmp = me4000_inl(dio_context->ctrl_reg);
4908 ME4000_DIO_CTRL_BIT_MODE_6 |
4909 ME4000_DIO_CTRL_BIT_MODE_7 |
4910 ME4000_DIO_CTRL_BIT_FIFO_HIGH_3;
4911 me4000_outl(tmp, dio_context->ctrl_reg);
4914 "ME4000:me4000_dio_config():Mode %d is not available\n",
4920 "ME4000:me4000_dio_config():Port %d is not available\n",
4925 PDEBUG("me4000_dio_config(): port %d, mode %d, function %d\n", cmd.port,
4926 cmd.mode, cmd.function);
4928 if ((cmd.mode == ME4000_DIO_FIFO_HIGH)
4929 || (cmd.mode == ME4000_DIO_FIFO_LOW)) {
4930 tmp = me4000_inl(dio_context->ctrl_reg);
4932 ~(ME4000_DIO_CTRL_BIT_FUNCTION_0 |
4933 ME4000_DIO_CTRL_BIT_FUNCTION_1);
4934 if (cmd.function == ME4000_DIO_FUNCTION_PATTERN) {
4935 me4000_outl(tmp, dio_context->ctrl_reg);
4936 } else if (cmd.function == ME4000_DIO_FUNCTION_DEMUX) {
4937 tmp |= ME4000_DIO_CTRL_BIT_FUNCTION_0;
4938 me4000_outl(tmp, dio_context->ctrl_reg);
4939 } else if (cmd.function == ME4000_DIO_FUNCTION_MUX) {
4940 tmp |= ME4000_DIO_CTRL_BIT_FUNCTION_1;
4941 me4000_outl(tmp, dio_context->ctrl_reg);
4944 "ME4000:me4000_dio_config():Invalid port function specified\n");
4952 static int me4000_dio_set_byte(struct me4000_dio_byte *arg,
4953 struct me4000_dio_context *dio_context)
4955 struct me4000_dio_byte cmd;
4958 CALL_PDEBUG("me4000_dio_set_byte() is executed\n");
4960 /* Copy data from user */
4961 err = copy_from_user(&cmd, arg, sizeof(struct me4000_dio_byte));
4964 "ME4000:me4000_dio_set_byte():Can't copy from user space\n");
4968 /* Check port parameter */
4969 if (cmd.port >= dio_context->dio_count) {
4971 "ME4000:me4000_dio_set_byte():Port %d is not available\n",
4976 if (cmd.port == ME4000_DIO_PORT_A) {
4977 if ((me4000_inl(dio_context->ctrl_reg) & 0x3) != 0x1) {
4979 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4983 me4000_outl(cmd.byte, dio_context->port_0_reg);
4984 } else if (cmd.port == ME4000_DIO_PORT_B) {
4985 if ((me4000_inl(dio_context->ctrl_reg) & 0xC) != 0x4) {
4987 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4991 me4000_outl(cmd.byte, dio_context->port_1_reg);
4992 } else if (cmd.port == ME4000_DIO_PORT_C) {
4993 if ((me4000_inl(dio_context->ctrl_reg) & 0x30) != 0x10) {
4995 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4999 me4000_outl(cmd.byte, dio_context->port_2_reg);
5000 } else if (cmd.port == ME4000_DIO_PORT_D) {
5001 if ((me4000_inl(dio_context->ctrl_reg) & 0xC0) != 0x40) {
5003 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
5007 me4000_outl(cmd.byte, dio_context->port_3_reg);
5010 "ME4000:me4000_dio_set_byte():Port %d is not available\n",
5018 static int me4000_dio_get_byte(struct me4000_dio_byte *arg,
5019 struct me4000_dio_context *dio_context)
5021 struct me4000_dio_byte cmd;
5024 CALL_PDEBUG("me4000_dio_get_byte() is executed\n");
5026 /* Copy data from user */
5027 err = copy_from_user(&cmd, arg, sizeof(struct me4000_dio_byte));
5030 "ME4000:me4000_dio_get_byte():Can't copy from user space\n");
5034 /* Check port parameter */
5035 if (cmd.port >= dio_context->dio_count) {
5037 "ME4000:me4000_dio_get_byte():Port %d is not available\n",
5042 if (cmd.port == ME4000_DIO_PORT_A) {
5043 cmd.byte = me4000_inl(dio_context->port_0_reg) & 0xFF;
5044 } else if (cmd.port == ME4000_DIO_PORT_B) {
5045 cmd.byte = me4000_inl(dio_context->port_1_reg) & 0xFF;
5046 } else if (cmd.port == ME4000_DIO_PORT_C) {
5047 cmd.byte = me4000_inl(dio_context->port_2_reg) & 0xFF;
5048 } else if (cmd.port == ME4000_DIO_PORT_D) {
5049 cmd.byte = me4000_inl(dio_context->port_3_reg) & 0xFF;
5052 "ME4000:me4000_dio_get_byte():Port %d is not available\n",
5057 /* Copy result back to user */
5058 err = copy_to_user(arg, &cmd, sizeof(struct me4000_dio_byte));
5061 "ME4000:me4000_dio_get_byte():Can't copy to user space\n");
5068 static int me4000_dio_reset(struct me4000_dio_context *dio_context)
5070 CALL_PDEBUG("me4000_dio_reset() is executed\n");
5072 /* Clear the control register */
5073 me4000_outl(0, dio_context->ctrl_reg);
5075 /* Check for opto isolated version */
5076 if (!(me4000_inl(dio_context->dir_reg) & 0x1)) {
5077 me4000_outl(0x1, dio_context->ctrl_reg);
5078 me4000_outl(0x0, dio_context->port_0_reg);
5084 /*------------------------------------ COUNTER STUFF ------------------------------------*/
5086 static int me4000_cnt_ioctl(struct inode *inode_p, struct file *file_p,
5087 unsigned int service, unsigned long arg)
5089 struct me4000_cnt_context *cnt_context;
5091 CALL_PDEBUG("me4000_cnt_ioctl() is executed\n");
5093 cnt_context = file_p->private_data;
5095 if (_IOC_TYPE(service) != ME4000_MAGIC) {
5096 printk(KERN_ERR "me4000_dio_ioctl():Wrong magic number\n");
5099 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
5100 printk(KERN_ERR "me4000_dio_ioctl():Service number to high\n");
5105 case ME4000_CNT_READ:
5106 return me4000_cnt_read((struct me4000_cnt *)arg, cnt_context);
5107 case ME4000_CNT_WRITE:
5108 return me4000_cnt_write((struct me4000_cnt *)arg, cnt_context);
5109 case ME4000_CNT_CONFIG:
5110 return me4000_cnt_config((struct me4000_cnt_config *)arg,
5112 case ME4000_CNT_RESET:
5113 return me4000_cnt_reset(cnt_context);
5116 "ME4000:me4000_dio_ioctl():Invalid service number %d\n",
5123 static int me4000_cnt_config(struct me4000_cnt_config *arg,
5124 struct me4000_cnt_context *cnt_context)
5126 struct me4000_cnt_config cmd;
5131 CALL_PDEBUG("me4000_cnt_config() is executed\n");
5133 /* Copy data from user */
5134 err = copy_from_user(&cmd, arg, sizeof(struct me4000_cnt_config));
5137 "ME4000:me4000_cnt_config():Can't copy from user space\n");
5141 /* Check counter parameter */
5142 switch (cmd.counter) {
5143 case ME4000_CNT_COUNTER_0:
5144 counter = ME4000_CNT_CTRL_BIT_COUNTER_0;
5146 case ME4000_CNT_COUNTER_1:
5147 counter = ME4000_CNT_CTRL_BIT_COUNTER_1;
5149 case ME4000_CNT_COUNTER_2:
5150 counter = ME4000_CNT_CTRL_BIT_COUNTER_2;
5154 "ME4000:me4000_cnt_config():Counter %d is not available\n",
5159 /* Check mode parameter */
5161 case ME4000_CNT_MODE_0:
5162 mode = ME4000_CNT_CTRL_BIT_MODE_0;
5164 case ME4000_CNT_MODE_1:
5165 mode = ME4000_CNT_CTRL_BIT_MODE_1;
5167 case ME4000_CNT_MODE_2:
5168 mode = ME4000_CNT_CTRL_BIT_MODE_2;
5170 case ME4000_CNT_MODE_3:
5171 mode = ME4000_CNT_CTRL_BIT_MODE_3;
5173 case ME4000_CNT_MODE_4:
5174 mode = ME4000_CNT_CTRL_BIT_MODE_4;
5176 case ME4000_CNT_MODE_5:
5177 mode = ME4000_CNT_CTRL_BIT_MODE_5;
5181 "ME4000:me4000_cnt_config():Mode %d is not available\n",
5186 /* Write the control word */
5187 me4000_outb((counter | mode | 0x30), cnt_context->ctrl_reg);
5192 static int me4000_cnt_read(struct me4000_cnt *arg,
5193 struct me4000_cnt_context *cnt_context)
5195 struct me4000_cnt cmd;
5199 CALL_PDEBUG("me4000_cnt_read() is executed\n");
5201 /* Copy data from user */
5202 err = copy_from_user(&cmd, arg, sizeof(struct me4000_cnt));
5205 "ME4000:me4000_cnt_read():Can't copy from user space\n");
5210 switch (cmd.counter) {
5211 case ME4000_CNT_COUNTER_0:
5212 tmp = me4000_inb(cnt_context->counter_0_reg);
5214 tmp = me4000_inb(cnt_context->counter_0_reg);
5215 cmd.value |= ((u16) tmp) << 8;
5217 case ME4000_CNT_COUNTER_1:
5218 tmp = me4000_inb(cnt_context->counter_1_reg);
5220 tmp = me4000_inb(cnt_context->counter_1_reg);
5221 cmd.value |= ((u16) tmp) << 8;
5223 case ME4000_CNT_COUNTER_2:
5224 tmp = me4000_inb(cnt_context->counter_2_reg);
5226 tmp = me4000_inb(cnt_context->counter_2_reg);
5227 cmd.value |= ((u16) tmp) << 8;
5231 "ME4000:me4000_cnt_read():Counter %d is not available\n",
5236 /* Copy result back to user */
5237 err = copy_to_user(arg, &cmd, sizeof(struct me4000_cnt));
5240 "ME4000:me4000_cnt_read():Can't copy to user space\n");
5247 static int me4000_cnt_write(struct me4000_cnt *arg,
5248 struct me4000_cnt_context *cnt_context)
5250 struct me4000_cnt cmd;
5254 CALL_PDEBUG("me4000_cnt_write() is executed\n");
5256 /* Copy data from user */
5257 err = copy_from_user(&cmd, arg, sizeof(struct me4000_cnt));
5260 "ME4000:me4000_cnt_write():Can't copy from user space\n");
5265 switch (cmd.counter) {
5266 case ME4000_CNT_COUNTER_0:
5267 tmp = cmd.value & 0xFF;
5268 me4000_outb(tmp, cnt_context->counter_0_reg);
5269 tmp = (cmd.value >> 8) & 0xFF;
5270 me4000_outb(tmp, cnt_context->counter_0_reg);
5272 case ME4000_CNT_COUNTER_1:
5273 tmp = cmd.value & 0xFF;
5274 me4000_outb(tmp, cnt_context->counter_1_reg);
5275 tmp = (cmd.value >> 8) & 0xFF;
5276 me4000_outb(tmp, cnt_context->counter_1_reg);
5278 case ME4000_CNT_COUNTER_2:
5279 tmp = cmd.value & 0xFF;
5280 me4000_outb(tmp, cnt_context->counter_2_reg);
5281 tmp = (cmd.value >> 8) & 0xFF;
5282 me4000_outb(tmp, cnt_context->counter_2_reg);
5286 "ME4000:me4000_cnt_write():Counter %d is not available\n",
5294 static int me4000_cnt_reset(struct me4000_cnt_context *cnt_context)
5296 CALL_PDEBUG("me4000_cnt_reset() is executed\n");
5298 /* Set the mode and value for counter 0 */
5299 me4000_outb(0x30, cnt_context->ctrl_reg);
5300 me4000_outb(0x00, cnt_context->counter_0_reg);
5301 me4000_outb(0x00, cnt_context->counter_0_reg);
5303 /* Set the mode and value for counter 1 */
5304 me4000_outb(0x70, cnt_context->ctrl_reg);
5305 me4000_outb(0x00, cnt_context->counter_1_reg);
5306 me4000_outb(0x00, cnt_context->counter_1_reg);
5308 /* Set the mode and value for counter 2 */
5309 me4000_outb(0xB0, cnt_context->ctrl_reg);
5310 me4000_outb(0x00, cnt_context->counter_2_reg);
5311 me4000_outb(0x00, cnt_context->counter_2_reg);
5316 /*------------------------------------ External Interrupt stuff ------------------------------------*/
5318 static int me4000_ext_int_ioctl(struct inode *inode_p, struct file *file_p,
5319 unsigned int service, unsigned long arg)
5321 struct me4000_ext_int_context *ext_int_context;
5323 CALL_PDEBUG("me4000_ext_int_ioctl() is executed\n");
5325 ext_int_context = file_p->private_data;
5327 if (_IOC_TYPE(service) != ME4000_MAGIC) {
5328 printk(KERN_ERR "me4000_ext_int_ioctl():Wrong magic number\n");
5331 if (_IOC_NR(service) > ME4000_IOCTL_MAXNR) {
5333 "me4000_ext_int_ioctl():Service number to high\n");
5338 case ME4000_EXT_INT_ENABLE:
5339 return me4000_ext_int_enable(ext_int_context);
5340 case ME4000_EXT_INT_DISABLE:
5341 return me4000_ext_int_disable(ext_int_context);
5342 case ME4000_EXT_INT_COUNT:
5343 return me4000_ext_int_count((unsigned long *)arg,
5347 "ME4000:me4000_ext_int_ioctl():Invalid service number %d\n",
5354 static int me4000_ext_int_enable(struct me4000_ext_int_context *ext_int_context)
5358 CALL_PDEBUG("me4000_ext_int_enable() is executed\n");
5360 tmp = me4000_inl(ext_int_context->ctrl_reg);
5361 tmp |= ME4000_AI_CTRL_BIT_EX_IRQ;
5362 me4000_outl(tmp, ext_int_context->ctrl_reg);
5367 static int me4000_ext_int_disable(struct me4000_ext_int_context *ext_int_context)
5371 CALL_PDEBUG("me4000_ext_int_disable() is executed\n");
5373 tmp = me4000_inl(ext_int_context->ctrl_reg);
5374 tmp &= ~ME4000_AI_CTRL_BIT_EX_IRQ;
5375 me4000_outl(tmp, ext_int_context->ctrl_reg);
5380 static int me4000_ext_int_count(unsigned long *arg,
5381 struct me4000_ext_int_context *ext_int_context)
5384 CALL_PDEBUG("me4000_ext_int_count() is executed\n");
5386 put_user(ext_int_context->int_count, arg);
5390 /*------------------------------------ General stuff ------------------------------------*/
5392 static int me4000_get_user_info(struct me4000_user_info *arg,
5393 struct me4000_info *board_info)
5395 struct me4000_user_info user_info;
5397 CALL_PDEBUG("me4000_get_user_info() is executed\n");
5399 user_info.board_count = board_info->board_count;
5400 user_info.plx_regbase = board_info->plx_regbase;
5401 user_info.plx_regbase_size = board_info->plx_regbase_size;
5402 user_info.me4000_regbase = board_info->me4000_regbase;
5403 user_info.me4000_regbase_size = board_info->me4000_regbase_size;
5404 user_info.serial_no = board_info->serial_no;
5405 user_info.hw_revision = board_info->hw_revision;
5406 user_info.vendor_id = board_info->vendor_id;
5407 user_info.device_id = board_info->device_id;
5408 user_info.pci_bus_no = board_info->pci_bus_no;
5409 user_info.pci_dev_no = board_info->pci_dev_no;
5410 user_info.pci_func_no = board_info->pci_func_no;
5411 user_info.irq = board_info->irq;
5412 user_info.irq_count = board_info->irq_count;
5413 user_info.driver_version = ME4000_DRIVER_VERSION;
5414 user_info.ao_count = board_info->board_p->ao.count;
5415 user_info.ao_fifo_count = board_info->board_p->ao.fifo_count;
5417 user_info.ai_count = board_info->board_p->ai.count;
5418 user_info.ai_sh_count = board_info->board_p->ai.sh_count;
5419 user_info.ai_ex_trig_analog = board_info->board_p->ai.ex_trig_analog;
5421 user_info.dio_count = board_info->board_p->dio.count;
5423 user_info.cnt_count = board_info->board_p->cnt.count;
5425 if (copy_to_user(arg, &user_info, sizeof(struct me4000_user_info)))
5431 /*------------------------------------ ISR STUFF ------------------------------------*/
5433 static int me4000_ext_int_fasync(int fd, struct file *file_ptr, int mode)
5436 struct me4000_ext_int_context *ext_int_context;
5438 CALL_PDEBUG("me4000_ext_int_fasync() is executed\n");
5440 ext_int_context = file_ptr->private_data;
5443 fasync_helper(fd, file_ptr, mode, &ext_int_context->fasync_ptr);
5445 CALL_PDEBUG("me4000_ext_int_fasync() is leaved\n");
5449 static irqreturn_t me4000_ao_isr(int irq, void *dev_id)
5453 struct me4000_ao_context *ao_context;
5458 ISR_PDEBUG("me4000_ao_isr() is executed\n");
5460 ao_context = dev_id;
5462 /* Check if irq number is right */
5463 if (irq != ao_context->irq) {
5464 ISR_PDEBUG("me4000_ao_isr():incorrect interrupt num: %d\n",
5469 /* Check if this DAC rised an interrupt */
5471 ((0x1 << (ao_context->index + 3)) &
5472 me4000_inl(ao_context->irq_status_reg))) {
5473 ISR_PDEBUG("me4000_ao_isr():Not this DAC\n");
5477 /* Read status register to find out what happened */
5478 tmp = me4000_inl(ao_context->status_reg);
5480 if (!(tmp & ME4000_AO_STATUS_BIT_EF) && (tmp & ME4000_AO_STATUS_BIT_HF)
5481 && (tmp & ME4000_AO_STATUS_BIT_HF)) {
5482 c = ME4000_AO_FIFO_COUNT;
5483 ISR_PDEBUG("me4000_ao_isr():Fifo empty\n");
5484 } else if ((tmp & ME4000_AO_STATUS_BIT_EF)
5485 && (tmp & ME4000_AO_STATUS_BIT_HF)
5486 && (tmp & ME4000_AO_STATUS_BIT_HF)) {
5487 c = ME4000_AO_FIFO_COUNT / 2;
5488 ISR_PDEBUG("me4000_ao_isr():Fifo under half full\n");
5491 ISR_PDEBUG("me4000_ao_isr():Fifo full\n");
5494 ISR_PDEBUG("me4000_ao_isr():Try to write 0x%04X values\n", c);
5497 c1 = me4000_values_to_end(ao_context->circ_buf,
5498 ME4000_AO_BUFFER_COUNT);
5499 ISR_PDEBUG("me4000_ao_isr():Values to end = %d\n", c1);
5505 ("me4000_ao_isr():Work done or buffer empty\n");
5508 if (((ao_context->fifo_reg & 0xFF) == ME4000_AO_01_FIFO_REG) ||
5509 ((ao_context->fifo_reg & 0xFF) == ME4000_AO_03_FIFO_REG)) {
5510 for (i = 0; i < c1; i++) {
5514 (ao_context->circ_buf.buf +
5515 ao_context->circ_buf.tail + i))) << 16;
5516 outl(value, ao_context->fifo_reg);
5519 outsw(ao_context->fifo_reg,
5520 ao_context->circ_buf.buf +
5521 ao_context->circ_buf.tail, c1);
5524 ao_context->circ_buf.tail =
5525 (ao_context->circ_buf.tail + c1) & (ME4000_AO_BUFFER_COUNT -
5527 ISR_PDEBUG("me4000_ao_isr():%d values wrote to port 0x%04X\n",
5528 c1, ao_context->fifo_reg);
5532 /* If there are no values left in the buffer, disable interrupts */
5533 spin_lock(&ao_context->int_lock);
5534 if (!me4000_buf_count(ao_context->circ_buf, ME4000_AO_BUFFER_COUNT)) {
5536 ("me4000_ao_isr():Disable Interrupt because no values left in buffer\n");
5537 tmp = me4000_inl(ao_context->ctrl_reg);
5538 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_IRQ;
5539 me4000_outl(tmp, ao_context->ctrl_reg);
5541 spin_unlock(&ao_context->int_lock);
5543 /* Reset the interrupt */
5544 spin_lock(&ao_context->int_lock);
5545 tmp = me4000_inl(ao_context->ctrl_reg);
5546 tmp |= ME4000_AO_CTRL_BIT_RESET_IRQ;
5547 me4000_outl(tmp, ao_context->ctrl_reg);
5548 tmp &= ~ME4000_AO_CTRL_BIT_RESET_IRQ;
5549 me4000_outl(tmp, ao_context->ctrl_reg);
5551 /* If state machine is stopped, flow was interrupted */
5552 if (!(me4000_inl(ao_context->status_reg) & ME4000_AO_STATUS_BIT_FSM)) {
5553 printk(KERN_ERR "ME4000:me4000_ao_isr():Broken pipe\n");
5554 /* Set flag in order to inform write routine */
5555 ao_context->pipe_flag = 1;
5556 /* Disable interrupt */
5557 tmp &= ~ME4000_AO_CTRL_BIT_ENABLE_IRQ;
5559 me4000_outl(tmp, ao_context->ctrl_reg);
5560 spin_unlock(&ao_context->int_lock);
5562 /* Wake up waiting process */
5563 wake_up_interruptible(&(ao_context->wait_queue));
5565 /* Count the interrupt */
5566 ao_context->board_info->irq_count++;
5571 static irqreturn_t me4000_ai_isr(int irq, void *dev_id)
5574 struct me4000_ai_context *ai_context;
5578 #ifdef ME4000_ISR_DEBUG
5579 unsigned long before;
5580 unsigned long after;
5583 ISR_PDEBUG("me4000_ai_isr() is executed\n");
5585 #ifdef ME4000_ISR_DEBUG
5589 ai_context = dev_id;
5591 /* Check if irq number is right */
5592 if (irq != ai_context->irq) {
5593 ISR_PDEBUG("me4000_ai_isr():incorrect interrupt num: %d\n",
5598 if (me4000_inl(ai_context->irq_status_reg) &
5599 ME4000_IRQ_STATUS_BIT_AI_HF) {
5601 ("me4000_ai_isr():Fifo half full interrupt occured\n");
5603 /* Read status register to find out what happened */
5604 tmp = me4000_inl(ai_context->ctrl_reg);
5606 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5607 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5608 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5609 ISR_PDEBUG("me4000_ai_isr():Fifo full\n");
5610 c = ME4000_AI_FIFO_COUNT;
5612 /* FIFO overflow, so stop conversion and disable all interrupts */
5613 spin_lock(&ai_context->int_lock);
5614 tmp = me4000_inl(ai_context->ctrl_reg);
5615 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5617 ~(ME4000_AI_CTRL_BIT_HF_IRQ |
5618 ME4000_AI_CTRL_BIT_SC_IRQ);
5619 outl(tmp, ai_context->ctrl_reg);
5620 spin_unlock(&ai_context->int_lock);
5621 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5622 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5623 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5624 ISR_PDEBUG("me4000_ai_isr():Fifo half full\n");
5625 c = ME4000_AI_FIFO_COUNT / 2;
5629 ("me4000_ai_isr():Can't determine state of fifo\n");
5632 ISR_PDEBUG("me4000_ai_isr():Try to read %d values\n", c);
5635 c1 = me4000_space_to_end(ai_context->circ_buf,
5636 ME4000_AI_BUFFER_COUNT);
5637 ISR_PDEBUG("me4000_ai_isr():Space to end = %d\n", c1);
5643 ("me4000_ai_isr():Work done or buffer full\n");
5647 insw(ai_context->data_reg,
5648 ai_context->circ_buf.buf +
5649 ai_context->circ_buf.head, c1);
5650 ai_context->circ_buf.head =
5651 (ai_context->circ_buf.head +
5652 c1) & (ME4000_AI_BUFFER_COUNT - 1);
5656 /* Work is done, so reset the interrupt */
5658 ("me4000_ai_isr():reset interrupt fifo half full interrupt\n");
5659 spin_lock(&ai_context->int_lock);
5660 tmp = me4000_inl(ai_context->ctrl_reg);
5661 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
5662 me4000_outl(tmp, ai_context->ctrl_reg);
5663 tmp &= ~ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
5664 me4000_outl(tmp, ai_context->ctrl_reg);
5665 spin_unlock(&ai_context->int_lock);
5668 if (me4000_inl(ai_context->irq_status_reg) & ME4000_IRQ_STATUS_BIT_SC) {
5670 ("me4000_ai_isr():Sample counter interrupt occured\n");
5672 if (!ai_context->sample_counter_reload) {
5674 ("me4000_ai_isr():Single data block available\n");
5676 /* Poll data until fifo empty */
5678 (i < ME4000_AI_FIFO_COUNT / 2)
5679 && (inl(ai_context->ctrl_reg) &
5680 ME4000_AI_STATUS_BIT_EF_DATA); i++) {
5681 if (me4000_space_to_end
5682 (ai_context->circ_buf,
5683 ME4000_AI_BUFFER_COUNT)) {
5684 *(ai_context->circ_buf.buf +
5685 ai_context->circ_buf.head) =
5686 inw(ai_context->data_reg);
5687 ai_context->circ_buf.head =
5688 (ai_context->circ_buf.head +
5689 1) & (ME4000_AI_BUFFER_COUNT - 1);
5693 ISR_PDEBUG("me4000_ai_isr():%d values read\n", i);
5695 if (ai_context->sample_counter <=
5696 ME4000_AI_FIFO_COUNT / 2) {
5698 ("me4000_ai_isr():Interrupt from adjustable half full threshold\n");
5700 /* Read status register to find out what happened */
5701 tmp = me4000_inl(ai_context->ctrl_reg);
5703 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5704 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5705 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5707 ("me4000_ai_isr():Fifo full\n");
5708 c = ME4000_AI_FIFO_COUNT;
5710 /* FIFO overflow, so stop conversion */
5711 spin_lock(&ai_context->int_lock);
5712 tmp = me4000_inl(ai_context->ctrl_reg);
5714 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5715 outl(tmp, ai_context->ctrl_reg);
5716 spin_unlock(&ai_context->int_lock);
5717 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA)
5719 ME4000_AI_STATUS_BIT_HF_DATA)
5721 ME4000_AI_STATUS_BIT_EF_DATA)) {
5723 ("me4000_ai_isr():Fifo half full\n");
5724 c = ME4000_AI_FIFO_COUNT / 2;
5726 c = ai_context->sample_counter;
5728 ("me4000_ai_isr():Sample count values\n");
5732 ("me4000_ai_isr():Try to read %d values\n",
5736 c1 = me4000_space_to_end(ai_context->
5738 ME4000_AI_BUFFER_COUNT);
5740 ("me4000_ai_isr():Space to end = %d\n",
5747 ("me4000_ai_isr():Work done or buffer full\n");
5751 insw(ai_context->data_reg,
5752 ai_context->circ_buf.buf +
5753 ai_context->circ_buf.head, c1);
5754 ai_context->circ_buf.head =
5755 (ai_context->circ_buf.head +
5756 c1) & (ME4000_AI_BUFFER_COUNT - 1);
5761 ("me4000_ai_isr():Multiple data block available\n");
5763 /* Read status register to find out what happened */
5764 tmp = me4000_inl(ai_context->ctrl_reg);
5766 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
5767 !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
5768 && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
5770 ("me4000_ai_isr():Fifo full\n");
5771 c = ME4000_AI_FIFO_COUNT;
5773 /* FIFO overflow, so stop conversion */
5774 spin_lock(&ai_context->int_lock);
5775 tmp = me4000_inl(ai_context->ctrl_reg);
5777 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
5778 outl(tmp, ai_context->ctrl_reg);
5779 spin_unlock(&ai_context->int_lock);
5782 c1 = me4000_space_to_end
5783 (ai_context->circ_buf,
5784 ME4000_AI_BUFFER_COUNT);
5786 ("me4000_ai_isr():Space to end = %d\n",
5793 ("me4000_ai_isr():Work done or buffer full\n");
5797 insw(ai_context->data_reg,
5798 ai_context->circ_buf.buf +
5799 ai_context->circ_buf.head,
5801 ai_context->circ_buf.head =
5802 (ai_context->circ_buf.head +
5804 (ME4000_AI_BUFFER_COUNT -
5808 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA)
5810 ME4000_AI_STATUS_BIT_HF_DATA)
5812 ME4000_AI_STATUS_BIT_EF_DATA)) {
5814 ("me4000_ai_isr():Fifo half full\n");
5815 c = ME4000_AI_FIFO_COUNT / 2;
5818 c1 = me4000_space_to_end
5819 (ai_context->circ_buf,
5820 ME4000_AI_BUFFER_COUNT);
5822 ("me4000_ai_isr():Space to end = %d\n",
5829 ("me4000_ai_isr():Work done or buffer full\n");
5833 insw(ai_context->data_reg,
5834 ai_context->circ_buf.buf +
5835 ai_context->circ_buf.head,
5837 ai_context->circ_buf.head =
5838 (ai_context->circ_buf.head +
5840 (ME4000_AI_BUFFER_COUNT -
5845 /* Poll data until fifo empty */
5847 (i < ME4000_AI_FIFO_COUNT / 2)
5848 && (inl(ai_context->ctrl_reg) &
5849 ME4000_AI_STATUS_BIT_EF_DATA);
5851 if (me4000_space_to_end
5852 (ai_context->circ_buf,
5853 ME4000_AI_BUFFER_COUNT)) {
5854 *(ai_context->circ_buf.
5856 ai_context->circ_buf.
5858 inw(ai_context->data_reg);
5859 ai_context->circ_buf.
5864 (ME4000_AI_BUFFER_COUNT
5870 ("me4000_ai_isr():%d values read\n",
5876 /* Work is done, so reset the interrupt */
5878 ("me4000_ai_isr():reset interrupt from sample counter\n");
5879 spin_lock(&ai_context->int_lock);
5880 tmp = me4000_inl(ai_context->ctrl_reg);
5881 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
5882 me4000_outl(tmp, ai_context->ctrl_reg);
5883 tmp &= ~ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
5884 me4000_outl(tmp, ai_context->ctrl_reg);
5885 spin_unlock(&ai_context->int_lock);
5888 /* Values are now available, so wake up waiting process */
5889 if (me4000_buf_count(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
5890 ISR_PDEBUG("me4000_ai_isr():Wake up waiting process\n");
5891 wake_up_interruptible(&(ai_context->wait_queue));
5894 /* If there is no space left in the buffer, disable interrupts */
5895 spin_lock(&ai_context->int_lock);
5896 if (!me4000_buf_space(ai_context->circ_buf, ME4000_AI_BUFFER_COUNT)) {
5898 ("me4000_ai_isr():Disable Interrupt because no space left in buffer\n");
5899 tmp = me4000_inl(ai_context->ctrl_reg);
5901 ~(ME4000_AI_CTRL_BIT_SC_IRQ | ME4000_AI_CTRL_BIT_HF_IRQ |
5902 ME4000_AI_CTRL_BIT_LE_IRQ);
5903 me4000_outl(tmp, ai_context->ctrl_reg);
5905 spin_unlock(&ai_context->int_lock);
5907 #ifdef ME4000_ISR_DEBUG
5909 printk(KERN_ERR "ME4000:me4000_ai_isr():Time lapse = %lu\n",
5916 static irqreturn_t me4000_ext_int_isr(int irq, void *dev_id)
5918 struct me4000_ext_int_context *ext_int_context;
5921 ISR_PDEBUG("me4000_ext_int_isr() is executed\n");
5923 ext_int_context = dev_id;
5925 /* Check if irq number is right */
5926 if (irq != ext_int_context->irq) {
5927 ISR_PDEBUG("me4000_ext_int_isr():incorrect interrupt num: %d\n",
5932 if (me4000_inl(ext_int_context->irq_status_reg) &
5933 ME4000_IRQ_STATUS_BIT_EX) {
5934 ISR_PDEBUG("me4000_ext_int_isr():External interrupt occured\n");
5935 tmp = me4000_inl(ext_int_context->ctrl_reg);
5936 tmp |= ME4000_AI_CTRL_BIT_EX_IRQ_RESET;
5937 me4000_outl(tmp, ext_int_context->ctrl_reg);
5938 tmp &= ~ME4000_AI_CTRL_BIT_EX_IRQ_RESET;
5939 me4000_outl(tmp, ext_int_context->ctrl_reg);
5941 ext_int_context->int_count++;
5943 if (ext_int_context->fasync_ptr) {
5945 ("me2600_ext_int_isr():Send signal to process\n");
5946 kill_fasync(&ext_int_context->fasync_ptr, SIGIO,
5954 static void __exit me4000_module_exit(void)
5956 struct me4000_info *board_info;
5958 CALL_PDEBUG("cleanup_module() is executed\n");
5960 unregister_chrdev(me4000_ext_int_major_driver_no, ME4000_EXT_INT_NAME);
5962 unregister_chrdev(me4000_cnt_major_driver_no, ME4000_CNT_NAME);
5964 unregister_chrdev(me4000_dio_major_driver_no, ME4000_DIO_NAME);
5966 unregister_chrdev(me4000_ai_major_driver_no, ME4000_AI_NAME);
5968 unregister_chrdev(me4000_ao_major_driver_no, ME4000_AO_NAME);
5970 remove_proc_entry("me4000", NULL);
5972 pci_unregister_driver(&me4000_driver);
5974 /* Reset the boards */
5975 list_for_each_entry(board_info, &me4000_board_info_list, list) {
5976 me4000_reset_board(board_info);
5979 clear_board_info_list();
5982 module_exit(me4000_module_exit);
5984 static int me4000_read_procmem(char *buf, char **start, off_t offset, int count,
5985 int *eof, void *data)
5988 int limit = count - 1000;
5989 struct me4000_info *board_info;
5991 len += sprintf(buf + len, "\nME4000 DRIVER VERSION %X.%X.%X\n\n",
5992 (ME4000_DRIVER_VERSION & 0xFF0000) >> 16,
5993 (ME4000_DRIVER_VERSION & 0xFF00) >> 8,
5994 (ME4000_DRIVER_VERSION & 0xFF));
5996 /* Search for the board context */
5997 list_for_each_entry(board_info, &me4000_board_info_list, list) {
5999 sprintf(buf + len, "Board number %d:\n",
6000 board_info->board_count);
6001 len += sprintf(buf + len, "---------------\n");
6003 sprintf(buf + len, "PLX base register = 0x%lX\n",
6004 board_info->plx_regbase);
6006 sprintf(buf + len, "PLX base register size = 0x%X\n",
6007 (unsigned int)board_info->plx_regbase_size);
6009 sprintf(buf + len, "ME4000 base register = 0x%X\n",
6010 (unsigned int)board_info->me4000_regbase);
6012 sprintf(buf + len, "ME4000 base register size = 0x%X\n",
6013 (unsigned int)board_info->me4000_regbase_size);
6015 sprintf(buf + len, "Serial number = 0x%X\n",
6016 board_info->serial_no);
6018 sprintf(buf + len, "Hardware revision = 0x%X\n",
6019 board_info->hw_revision);
6021 sprintf(buf + len, "Vendor id = 0x%X\n",
6022 board_info->vendor_id);
6024 sprintf(buf + len, "Device id = 0x%X\n",
6025 board_info->device_id);
6027 sprintf(buf + len, "PCI bus number = %d\n",
6028 board_info->pci_bus_no);
6030 sprintf(buf + len, "PCI device number = %d\n",
6031 board_info->pci_dev_no);
6033 sprintf(buf + len, "PCI function number = %d\n",
6034 board_info->pci_func_no);
6035 len += sprintf(buf + len, "IRQ = %u\n", board_info->irq);
6038 "Count of interrupts since module was loaded = %d\n",
6039 board_info->irq_count);
6042 sprintf(buf + len, "Count of analog outputs = %d\n",
6043 board_info->board_p->ao.count);
6045 sprintf(buf + len, "Count of analog output fifos = %d\n",
6046 board_info->board_p->ao.fifo_count);
6049 sprintf(buf + len, "Count of analog inputs = %d\n",
6050 board_info->board_p->ai.count);
6053 "Count of sample and hold devices for analog input = %d\n",
6054 board_info->board_p->ai.sh_count);
6057 "Analog external trigger available for analog input = %d\n",
6058 board_info->board_p->ai.ex_trig_analog);
6061 sprintf(buf + len, "Count of digital ports = %d\n",
6062 board_info->board_p->dio.count);
6065 sprintf(buf + len, "Count of counter devices = %d\n",
6066 board_info->board_p->cnt.count);
6068 sprintf(buf + len, "AI control register = 0x%08X\n",
6069 inl(board_info->me4000_regbase +
6070 ME4000_AI_CTRL_REG));
6072 len += sprintf(buf + len, "AO 0 control register = 0x%08X\n",
6073 inl(board_info->me4000_regbase +
6074 ME4000_AO_00_CTRL_REG));
6076 sprintf(buf + len, "AO 0 status register = 0x%08X\n",
6077 inl(board_info->me4000_regbase +
6078 ME4000_AO_00_STATUS_REG));
6080 sprintf(buf + len, "AO 1 control register = 0x%08X\n",
6081 inl(board_info->me4000_regbase +
6082 ME4000_AO_01_CTRL_REG));
6084 sprintf(buf + len, "AO 1 status register = 0x%08X\n",
6085 inl(board_info->me4000_regbase +
6086 ME4000_AO_01_STATUS_REG));
6088 sprintf(buf + len, "AO 2 control register = 0x%08X\n",
6089 inl(board_info->me4000_regbase +
6090 ME4000_AO_02_CTRL_REG));
6092 sprintf(buf + len, "AO 2 status register = 0x%08X\n",
6093 inl(board_info->me4000_regbase +
6094 ME4000_AO_02_STATUS_REG));
6096 sprintf(buf + len, "AO 3 control register = 0x%08X\n",
6097 inl(board_info->me4000_regbase +
6098 ME4000_AO_03_CTRL_REG));
6100 sprintf(buf + len, "AO 3 status register = 0x%08X\n",
6101 inl(board_info->me4000_regbase +
6102 ME4000_AO_03_STATUS_REG));