1 /* Device driver for Meilhaus ME-4000 board family.
2 * ================================================
4 * Copyright (C) 2003 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Author: Guenter Gebhardt <g.gebhardt@meilhaus.de>
23 #include <linux/module.h>
25 #include <linux/sched.h>
26 #include <linux/interrupt.h>
27 #include <linux/pci.h>
28 #include <linux/errno.h>
29 #include <linux/delay.h>
31 #include <linux/unistd.h>
32 #include <linux/list.h>
33 #include <linux/proc_fs.h>
34 #include <linux/types.h>
35 #include <linux/poll.h>
36 #include <linux/vmalloc.h>
37 #include <linux/slab.h>
38 #include <asm/pgtable.h>
39 #include <linux/uaccess.h>
41 #include <asm/system.h>
43 /* Include-File for the Meilhaus ME-4000 I/O board */
45 #include "me4000_firmware.h"
46 #include "me4610_firmware.h"
48 /* Administrative stuff for modinfo */
49 MODULE_AUTHOR("Guenter Gebhardt <g.gebhardt@meilhaus.de>");
51 ("Device Driver Module for Meilhaus ME-4000 boards version 1.0.5");
52 MODULE_SUPPORTED_DEVICE("Meilhaus ME-4000 Multi I/O boards");
53 MODULE_LICENSE("GPL");
55 /* Board specific data are kept in a global list */
56 static LIST_HEAD(me4000_board_info_list
);
58 /* Major Device Numbers. 0 means to get it automatically from the System */
59 static int me4000_ao_major_driver_no
;
60 static int me4000_ai_major_driver_no
;
61 static int me4000_dio_major_driver_no
;
62 static int me4000_cnt_major_driver_no
;
63 static int me4000_ext_int_major_driver_no
;
65 /* Let the user specify a custom major driver number */
66 module_param(me4000_ao_major_driver_no
, int, 0);
67 MODULE_PARM_DESC(me4000_ao_major_driver_no
,
68 "Major driver number for analog output (default 0)");
70 module_param(me4000_ai_major_driver_no
, int, 0);
71 MODULE_PARM_DESC(me4000_ai_major_driver_no
,
72 "Major driver number for analog input (default 0)");
74 module_param(me4000_dio_major_driver_no
, int, 0);
75 MODULE_PARM_DESC(me4000_dio_major_driver_no
,
76 "Major driver number digital I/O (default 0)");
78 module_param(me4000_cnt_major_driver_no
, int, 0);
79 MODULE_PARM_DESC(me4000_cnt_major_driver_no
,
80 "Major driver number for counter (default 0)");
82 module_param(me4000_ext_int_major_driver_no
, int, 0);
83 MODULE_PARM_DESC(me4000_ext_int_major_driver_no
,
84 "Major driver number for external interrupt (default 0)");
86 /*-----------------------------------------------------------------------------
87 Board detection and initialization
88 ---------------------------------------------------------------------------*/
89 static int me4000_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
);
90 static int me4000_xilinx_download(struct me4000_info
*);
91 static int me4000_reset_board(struct me4000_info
*);
93 static void clear_board_info_list(void);
94 static void release_ao_contexts(struct me4000_info
*board_info
);
95 /*-----------------------------------------------------------------------------
96 Stuff used by all device parts
97 ---------------------------------------------------------------------------*/
98 static int me4000_open(struct inode
*, struct file
*);
99 static int me4000_release(struct inode
*, struct file
*);
101 static int me4000_get_user_info(struct me4000_user_info
*,
102 struct me4000_info
*board_info
);
103 static int me4000_read_procmem(char *, char **, off_t
, int, int *, void *);
105 /*-----------------------------------------------------------------------------
107 ---------------------------------------------------------------------------*/
108 static ssize_t
me4000_ao_write_sing(struct file
*, const char *, size_t,
110 static ssize_t
me4000_ao_write_wrap(struct file
*, const char *, size_t,
112 static ssize_t
me4000_ao_write_cont(struct file
*, const char *, size_t,
115 static int me4000_ao_ioctl_sing(struct inode
*, struct file
*, unsigned int,
117 static int me4000_ao_ioctl_wrap(struct inode
*, struct file
*, unsigned int,
119 static int me4000_ao_ioctl_cont(struct inode
*, struct file
*, unsigned int,
122 static unsigned int me4000_ao_poll_cont(struct file
*, poll_table
*);
123 static int me4000_ao_fsync_cont(struct file
*, struct dentry
*, int);
125 static int me4000_ao_start(unsigned long *, struct me4000_ao_context
*);
126 static int me4000_ao_stop(struct me4000_ao_context
*);
127 static int me4000_ao_immediate_stop(struct me4000_ao_context
*);
128 static int me4000_ao_timer_set_divisor(u32
*, struct me4000_ao_context
*);
129 static int me4000_ao_preload(struct me4000_ao_context
*);
130 static int me4000_ao_preload_update(struct me4000_ao_context
*);
131 static int me4000_ao_ex_trig_set_edge(int *, struct me4000_ao_context
*);
132 static int me4000_ao_ex_trig_enable(struct me4000_ao_context
*);
133 static int me4000_ao_ex_trig_disable(struct me4000_ao_context
*);
134 static int me4000_ao_prepare(struct me4000_ao_context
*ao_info
);
135 static int me4000_ao_reset(struct me4000_ao_context
*ao_info
);
136 static int me4000_ao_enable_do(struct me4000_ao_context
*);
137 static int me4000_ao_disable_do(struct me4000_ao_context
*);
138 static int me4000_ao_fsm_state(int *, struct me4000_ao_context
*);
140 static int me4000_ao_simultaneous_ex_trig(struct me4000_ao_context
*ao_context
);
141 static int me4000_ao_simultaneous_sw(struct me4000_ao_context
*ao_context
);
142 static int me4000_ao_simultaneous_disable(struct me4000_ao_context
*ao_context
);
143 static int me4000_ao_simultaneous_update(
144 struct me4000_ao_channel_list
*channels
,
145 struct me4000_ao_context
*ao_context
);
147 static int me4000_ao_synchronous_ex_trig(struct me4000_ao_context
*ao_context
);
148 static int me4000_ao_synchronous_sw(struct me4000_ao_context
*ao_context
);
149 static int me4000_ao_synchronous_disable(struct me4000_ao_context
*ao_context
);
151 static int me4000_ao_ex_trig_timeout(unsigned long *arg
,
152 struct me4000_ao_context
*ao_context
);
153 static int me4000_ao_get_free_buffer(unsigned long *arg
,
154 struct me4000_ao_context
*ao_context
);
156 /*-----------------------------------------------------------------------------
158 ---------------------------------------------------------------------------*/
159 static int me4000_ai_single(struct me4000_ai_single
*,
160 struct me4000_ai_context
*);
161 static int me4000_ai_ioctl_sing(struct inode
*, struct file
*, unsigned int,
164 static ssize_t
me4000_ai_read(struct file
*, char *, size_t, loff_t
*);
165 static int me4000_ai_ioctl_sw(struct inode
*, struct file
*, unsigned int,
167 static unsigned int me4000_ai_poll(struct file
*, poll_table
*);
168 static int me4000_ai_fasync(int fd
, struct file
*file_p
, int mode
);
170 static int me4000_ai_ioctl_ext(struct inode
*, struct file
*, unsigned int,
173 static int me4000_ai_prepare(struct me4000_ai_context
*ai_context
);
174 static int me4000_ai_reset(struct me4000_ai_context
*ai_context
);
175 static int me4000_ai_config(struct me4000_ai_config
*,
176 struct me4000_ai_context
*);
177 static int me4000_ai_start(struct me4000_ai_context
*);
178 static int me4000_ai_start_ex(unsigned long *, struct me4000_ai_context
*);
179 static int me4000_ai_stop(struct me4000_ai_context
*);
180 static int me4000_ai_immediate_stop(struct me4000_ai_context
*);
181 static int me4000_ai_ex_trig_enable(struct me4000_ai_context
*);
182 static int me4000_ai_ex_trig_disable(struct me4000_ai_context
*);
183 static int me4000_ai_ex_trig_setup(struct me4000_ai_trigger
*,
184 struct me4000_ai_context
*);
185 static int me4000_ai_sc_setup(struct me4000_ai_sc
*arg
,
186 struct me4000_ai_context
*ai_context
);
187 static int me4000_ai_offset_enable(struct me4000_ai_context
*ai_context
);
188 static int me4000_ai_offset_disable(struct me4000_ai_context
*ai_context
);
189 static int me4000_ai_fullscale_enable(struct me4000_ai_context
*ai_context
);
190 static int me4000_ai_fullscale_disable(struct me4000_ai_context
*ai_context
);
191 static int me4000_ai_fsm_state(int *arg
, struct me4000_ai_context
*ai_context
);
192 static int me4000_ai_get_count_buffer(unsigned long *arg
,
193 struct me4000_ai_context
*ai_context
);
195 /*-----------------------------------------------------------------------------
197 ---------------------------------------------------------------------------*/
198 static int me4000_eeprom_read(struct me4000_eeprom
*arg
,
199 struct me4000_ai_context
*ai_context
);
200 static int me4000_eeprom_write(struct me4000_eeprom
*arg
,
201 struct me4000_ai_context
*ai_context
);
203 /*-----------------------------------------------------------------------------
205 ---------------------------------------------------------------------------*/
206 static int me4000_dio_ioctl(struct inode
*, struct file
*, unsigned int,
208 static int me4000_dio_config(struct me4000_dio_config
*,
209 struct me4000_dio_context
*);
210 static int me4000_dio_get_byte(struct me4000_dio_byte
*,
211 struct me4000_dio_context
*);
212 static int me4000_dio_set_byte(struct me4000_dio_byte
*,
213 struct me4000_dio_context
*);
214 static int me4000_dio_reset(struct me4000_dio_context
*);
216 /*-----------------------------------------------------------------------------
218 ---------------------------------------------------------------------------*/
219 static int me4000_cnt_ioctl(struct inode
*, struct file
*, unsigned int,
221 static int me4000_cnt_config(struct me4000_cnt_config
*,
222 struct me4000_cnt_context
*);
223 static int me4000_cnt_read(struct me4000_cnt
*, struct me4000_cnt_context
*);
224 static int me4000_cnt_write(struct me4000_cnt
*, struct me4000_cnt_context
*);
225 static int me4000_cnt_reset(struct me4000_cnt_context
*);
227 /*-----------------------------------------------------------------------------
228 External interrupt routines
229 ---------------------------------------------------------------------------*/
230 static int me4000_ext_int_ioctl(struct inode
*, struct file
*, unsigned int,
232 static int me4000_ext_int_enable(struct me4000_ext_int_context
*);
233 static int me4000_ext_int_disable(struct me4000_ext_int_context
*);
234 static int me4000_ext_int_count(unsigned long *arg
,
235 struct me4000_ext_int_context
*ext_int_context
);
236 static int me4000_ext_int_fasync(int fd
, struct file
*file_ptr
, int mode
);
238 /*-----------------------------------------------------------------------------
239 The interrupt service routines
240 ---------------------------------------------------------------------------*/
241 static irqreturn_t
me4000_ao_isr(int, void *);
242 static irqreturn_t
me4000_ai_isr(int, void *);
243 static irqreturn_t
me4000_ext_int_isr(int, void *);
245 /*-----------------------------------------------------------------------------
247 ---------------------------------------------------------------------------*/
249 static inline int me4000_buf_count(struct me4000_circ_buf buf
, int size
)
251 return (buf
.head
- buf
.tail
) & (size
- 1);
254 static inline int me4000_buf_space(struct me4000_circ_buf buf
, int size
)
256 return (buf
.tail
- (buf
.head
+ 1)) & (size
- 1);
259 static inline int me4000_values_to_end(struct me4000_circ_buf buf
, int size
)
263 end
= size
- buf
.tail
;
264 n
= (buf
.head
+ end
) & (size
- 1);
265 return (n
< end
) ? n
: end
;
268 static inline int me4000_space_to_end(struct me4000_circ_buf buf
, int size
)
273 end
= size
- 1 - buf
.head
;
274 n
= (end
+ buf
.tail
) & (size
- 1);
275 return (n
<= end
) ? n
: (end
+ 1);
278 static inline void me4000_outb(unsigned char value
, unsigned long port
)
280 PORT_PDEBUG("--> 0x%02X port 0x%04lX\n", value
, port
);
284 static inline void me4000_outl(unsigned long value
, unsigned long port
)
286 PORT_PDEBUG("--> 0x%08lX port 0x%04lX\n", value
, port
);
290 static inline unsigned long me4000_inl(unsigned long port
)
294 PORT_PDEBUG("<-- 0x%08lX port 0x%04lX\n", value
, port
);
298 static inline unsigned char me4000_inb(unsigned long port
)
302 PORT_PDEBUG("<-- 0x%08X port 0x%04lX\n", value
, port
);
306 static struct pci_driver me4000_driver
= {
308 .id_table
= me4000_pci_table
,
309 .probe
= me4000_probe
312 static struct file_operations me4000_ao_fops_sing
= {
313 .owner
= THIS_MODULE
,
314 .write
= me4000_ao_write_sing
,
315 .ioctl
= me4000_ao_ioctl_sing
,
317 .release
= me4000_release
,
320 static struct file_operations me4000_ao_fops_wrap
= {
321 .owner
= THIS_MODULE
,
322 .write
= me4000_ao_write_wrap
,
323 .ioctl
= me4000_ao_ioctl_wrap
,
325 .release
= me4000_release
,
328 static struct file_operations me4000_ao_fops_cont
= {
329 .owner
= THIS_MODULE
,
330 .write
= me4000_ao_write_cont
,
331 .poll
= me4000_ao_poll_cont
,
332 .ioctl
= me4000_ao_ioctl_cont
,
334 .release
= me4000_release
,
335 .fsync
= me4000_ao_fsync_cont
,
338 static struct file_operations me4000_ai_fops_sing
= {
339 .owner
= THIS_MODULE
,
340 .ioctl
= me4000_ai_ioctl_sing
,
342 .release
= me4000_release
,
345 static struct file_operations me4000_ai_fops_cont_sw
= {
346 .owner
= THIS_MODULE
,
347 .read
= me4000_ai_read
,
348 .poll
= me4000_ai_poll
,
349 .ioctl
= me4000_ai_ioctl_sw
,
351 .release
= me4000_release
,
352 .fasync
= me4000_ai_fasync
,
355 static struct file_operations me4000_ai_fops_cont_et
= {
356 .owner
= THIS_MODULE
,
357 .read
= me4000_ai_read
,
358 .poll
= me4000_ai_poll
,
359 .ioctl
= me4000_ai_ioctl_ext
,
361 .release
= me4000_release
,
364 static struct file_operations me4000_ai_fops_cont_et_value
= {
365 .owner
= THIS_MODULE
,
366 .read
= me4000_ai_read
,
367 .poll
= me4000_ai_poll
,
368 .ioctl
= me4000_ai_ioctl_ext
,
370 .release
= me4000_release
,
373 static struct file_operations me4000_ai_fops_cont_et_chanlist
= {
374 .owner
= THIS_MODULE
,
375 .read
= me4000_ai_read
,
376 .poll
= me4000_ai_poll
,
377 .ioctl
= me4000_ai_ioctl_ext
,
379 .release
= me4000_release
,
382 static struct file_operations me4000_dio_fops
= {
383 .owner
= THIS_MODULE
,
384 .ioctl
= me4000_dio_ioctl
,
386 .release
= me4000_release
,
389 static struct file_operations me4000_cnt_fops
= {
390 .owner
= THIS_MODULE
,
391 .ioctl
= me4000_cnt_ioctl
,
393 .release
= me4000_release
,
396 static struct file_operations me4000_ext_int_fops
= {
397 .owner
= THIS_MODULE
,
398 .ioctl
= me4000_ext_int_ioctl
,
400 .release
= me4000_release
,
401 .fasync
= me4000_ext_int_fasync
,
404 static struct file_operations
*me4000_ao_fops_array
[] = {
405 &me4000_ao_fops_sing
, // single operations
406 &me4000_ao_fops_wrap
, // wraparound operations
407 &me4000_ao_fops_cont
, // continous operations
410 static struct file_operations
*me4000_ai_fops_array
[] = {
411 &me4000_ai_fops_sing
, // single operations
412 &me4000_ai_fops_cont_sw
, // continuous operations with software start
413 &me4000_ai_fops_cont_et
, // continous operations with external trigger
414 &me4000_ai_fops_cont_et_value
, // sample values by external trigger
415 &me4000_ai_fops_cont_et_chanlist
, // work through one channel list by external trigger
418 static int __init
me4000_init_module(void)
422 CALL_PDEBUG("init_module() is executed\n");
424 /* Register driver capabilities */
425 result
= pci_register_driver(&me4000_driver
);
426 PDEBUG("init_module():%d devices detected\n", result
);
428 printk(KERN_ERR
"ME4000:init_module():Can't register driver\n");
432 /* Allocate major number for analog output */
434 register_chrdev(me4000_ao_major_driver_no
, ME4000_AO_NAME
,
435 &me4000_ao_fops_sing
);
437 printk(KERN_ERR
"ME4000:init_module():Can't get AO major no\n");
440 me4000_ao_major_driver_no
= result
;
442 PDEBUG("init_module():Major driver number for AO = %ld\n",
443 me4000_ao_major_driver_no
);
445 /* Allocate major number for analog input */
447 register_chrdev(me4000_ai_major_driver_no
, ME4000_AI_NAME
,
448 &me4000_ai_fops_sing
);
450 printk(KERN_ERR
"ME4000:init_module():Can't get AI major no\n");
453 me4000_ai_major_driver_no
= result
;
455 PDEBUG("init_module():Major driver number for AI = %ld\n",
456 me4000_ai_major_driver_no
);
458 /* Allocate major number for digital I/O */
460 register_chrdev(me4000_dio_major_driver_no
, ME4000_DIO_NAME
,
464 "ME4000:init_module():Can't get DIO major no\n");
467 me4000_dio_major_driver_no
= result
;
469 PDEBUG("init_module():Major driver number for DIO = %ld\n",
470 me4000_dio_major_driver_no
);
472 /* Allocate major number for counter */
474 register_chrdev(me4000_cnt_major_driver_no
, ME4000_CNT_NAME
,
478 "ME4000:init_module():Can't get CNT major no\n");
481 me4000_cnt_major_driver_no
= result
;
483 PDEBUG("init_module():Major driver number for CNT = %ld\n",
484 me4000_cnt_major_driver_no
);
486 /* Allocate major number for external interrupt */
488 register_chrdev(me4000_ext_int_major_driver_no
, ME4000_EXT_INT_NAME
,
489 &me4000_ext_int_fops
);
492 "ME4000:init_module():Can't get major no for external interrupt\n");
495 me4000_ext_int_major_driver_no
= result
;
498 ("init_module():Major driver number for external interrupt = %ld\n",
499 me4000_ext_int_major_driver_no
);
501 /* Create the /proc/me4000 entry */
502 if (!create_proc_read_entry
503 ("me4000", 0, NULL
, me4000_read_procmem
, NULL
)) {
506 "ME4000:init_module():Can't create proc entry\n");
513 unregister_chrdev(me4000_ext_int_major_driver_no
, ME4000_EXT_INT_NAME
);
516 unregister_chrdev(me4000_cnt_major_driver_no
, ME4000_CNT_NAME
);
519 unregister_chrdev(me4000_dio_major_driver_no
, ME4000_DIO_NAME
);
522 unregister_chrdev(me4000_ai_major_driver_no
, ME4000_AI_NAME
);
525 unregister_chrdev(me4000_ao_major_driver_no
, ME4000_AO_NAME
);
528 pci_unregister_driver(&me4000_driver
);
529 clear_board_info_list();
535 module_init(me4000_init_module
);
537 static void clear_board_info_list(void)
539 struct me4000_info
*board_info
, *board_info_safe
;
540 struct me4000_ao_context
*ao_context
, *ao_context_safe
;
542 /* Clear context lists */
543 list_for_each_entry(board_info
, &me4000_board_info_list
, list
) {
544 /* Clear analog output context list */
545 list_for_each_entry_safe(ao_context
, ao_context_safe
,
546 &board_info
->ao_context_list
, list
) {
547 me4000_ao_reset(ao_context
);
548 free_irq(ao_context
->irq
, ao_context
);
549 kfree(ao_context
->circ_buf
.buf
);
550 list_del(&ao_context
->list
);
554 /* Clear analog input context */
555 kfree(board_info
->ai_context
->circ_buf
.buf
);
556 kfree(board_info
->ai_context
);
558 /* Clear digital I/O context */
559 kfree(board_info
->dio_context
);
561 /* Clear counter context */
562 kfree(board_info
->cnt_context
);
564 /* Clear external interrupt context */
565 kfree(board_info
->ext_int_context
);
568 /* Clear the board info list */
569 list_for_each_entry_safe(board_info
, board_info_safe
,
570 &me4000_board_info_list
, list
) {
571 pci_release_regions(board_info
->pci_dev_p
);
572 list_del(&board_info
->list
);
577 static int get_registers(struct pci_dev
*dev
, struct me4000_info
*board_info
)
580 /*--------------------------- plx regbase ---------------------------------*/
582 board_info
->plx_regbase
= pci_resource_start(dev
, 1);
583 if (board_info
->plx_regbase
== 0) {
585 "ME4000:get_registers():PCI base address 1 is not available\n");
588 board_info
->plx_regbase_size
= pci_resource_len(dev
, 1);
591 ("get_registers():PLX configuration registers at address 0x%4lX [0x%4lX]\n",
592 board_info
->plx_regbase
, board_info
->plx_regbase_size
);
594 /*--------------------------- me4000 regbase ------------------------------*/
596 board_info
->me4000_regbase
= pci_resource_start(dev
, 2);
597 if (board_info
->me4000_regbase
== 0) {
599 "ME4000:get_registers():PCI base address 2 is not available\n");
602 board_info
->me4000_regbase_size
= pci_resource_len(dev
, 2);
604 PDEBUG("get_registers():ME4000 registers at address 0x%4lX [0x%4lX]\n",
605 board_info
->me4000_regbase
, board_info
->me4000_regbase_size
);
607 /*--------------------------- timer regbase ------------------------------*/
609 board_info
->timer_regbase
= pci_resource_start(dev
, 3);
610 if (board_info
->timer_regbase
== 0) {
612 "ME4000:get_registers():PCI base address 3 is not available\n");
615 board_info
->timer_regbase_size
= pci_resource_len(dev
, 3);
617 PDEBUG("get_registers():Timer registers at address 0x%4lX [0x%4lX]\n",
618 board_info
->timer_regbase
, board_info
->timer_regbase_size
);
620 /*--------------------------- program regbase ------------------------------*/
622 board_info
->program_regbase
= pci_resource_start(dev
, 5);
623 if (board_info
->program_regbase
== 0) {
625 "get_registers():ME4000:PCI base address 5 is not available\n");
628 board_info
->program_regbase_size
= pci_resource_len(dev
, 5);
630 PDEBUG("get_registers():Program registers at address 0x%4lX [0x%4lX]\n",
631 board_info
->program_regbase
, board_info
->program_regbase_size
);
636 static int init_board_info(struct pci_dev
*pci_dev_p
,
637 struct me4000_info
*board_info
)
641 struct list_head
*board_p
;
642 board_info
->pci_dev_p
= pci_dev_p
;
644 for (i
= 0; i
< ARRAY_SIZE(me4000_boards
); i
++) {
645 if (me4000_boards
[i
].device_id
== pci_dev_p
->device
) {
646 board_info
->board_p
= &me4000_boards
[i
];
650 if (i
== ARRAY_SIZE(me4000_boards
)) {
652 "ME4000:init_board_info():Device ID not valid\n");
656 /* Get the index of the board in the global list */
658 list_for_each(board_p
, &me4000_board_info_list
) {
659 if (board_p
== &board_info
->list
) {
660 board_info
->board_count
= i
;
665 if (board_p
== &me4000_board_info_list
) {
667 "ME4000:init_board_info():Cannot get index of board\n");
671 /* Init list head for analog output contexts */
672 INIT_LIST_HEAD(&board_info
->ao_context_list
);
674 /* Init spin locks */
675 spin_lock_init(&board_info
->preload_lock
);
676 spin_lock_init(&board_info
->ai_ctrl_lock
);
678 /* Get the serial number */
679 result
= pci_read_config_dword(pci_dev_p
, 0x2C, &board_info
->serial_no
);
680 if (result
!= PCIBIOS_SUCCESSFUL
) {
682 "ME4000:init_board_info: Can't get serial_no\n");
685 PDEBUG("init_board_info():serial_no = 0x%x\n", board_info
->serial_no
);
687 /* Get the hardware revision */
689 pci_read_config_byte(pci_dev_p
, 0x08, &board_info
->hw_revision
);
690 if (result
!= PCIBIOS_SUCCESSFUL
) {
692 "ME4000:init_board_info():Can't get hw_revision\n");
695 PDEBUG("init_board_info():hw_revision = 0x%x\n",
696 board_info
->hw_revision
);
698 /* Get the vendor id */
699 board_info
->vendor_id
= pci_dev_p
->vendor
;
700 PDEBUG("init_board_info():vendor_id = 0x%x\n", board_info
->vendor_id
);
702 /* Get the device id */
703 board_info
->device_id
= pci_dev_p
->device
;
704 PDEBUG("init_board_info():device_id = 0x%x\n", board_info
->device_id
);
706 /* Get the pci device number */
707 board_info
->pci_dev_no
= PCI_FUNC(pci_dev_p
->devfn
);
708 PDEBUG("init_board_info():pci_func_no = 0x%x\n",
709 board_info
->pci_func_no
);
711 /* Get the pci slot number */
712 board_info
->pci_dev_no
= PCI_SLOT(pci_dev_p
->devfn
);
713 PDEBUG("init_board_info():pci_dev_no = 0x%x\n", board_info
->pci_dev_no
);
715 /* Get the pci bus number */
716 board_info
->pci_bus_no
= pci_dev_p
->bus
->number
;
717 PDEBUG("init_board_info():pci_bus_no = 0x%x\n", board_info
->pci_bus_no
);
719 /* Get the irq assigned to the board */
720 board_info
->irq
= pci_dev_p
->irq
;
721 PDEBUG("init_board_info():irq = %d\n", board_info
->irq
);
726 static int alloc_ao_contexts(struct me4000_info
*info
)
730 struct me4000_ao_context
*ao_context
;
732 for (i
= 0; i
< info
->board_p
->ao
.count
; i
++) {
733 ao_context
= kzalloc(sizeof(struct me4000_ao_context
),
737 "alloc_ao_contexts():Can't get memory for ao context\n");
738 release_ao_contexts(info
);
742 spin_lock_init(&ao_context
->use_lock
);
743 spin_lock_init(&ao_context
->int_lock
);
744 ao_context
->irq
= info
->irq
;
745 init_waitqueue_head(&ao_context
->wait_queue
);
746 ao_context
->board_info
= info
;
748 if (info
->board_p
->ao
.fifo_count
) {
749 /* Allocate circular buffer */
750 ao_context
->circ_buf
.buf
=
751 kzalloc(ME4000_AO_BUFFER_SIZE
, GFP_KERNEL
);
752 if (!ao_context
->circ_buf
.buf
) {
754 "alloc_ao_contexts():Can't get circular buffer\n");
755 release_ao_contexts(info
);
759 /* Clear the circular buffer */
760 ao_context
->circ_buf
.head
= 0;
761 ao_context
->circ_buf
.tail
= 0;
766 ao_context
->ctrl_reg
=
767 info
->me4000_regbase
+ ME4000_AO_00_CTRL_REG
;
768 ao_context
->status_reg
=
769 info
->me4000_regbase
+ ME4000_AO_00_STATUS_REG
;
770 ao_context
->fifo_reg
=
771 info
->me4000_regbase
+ ME4000_AO_00_FIFO_REG
;
772 ao_context
->single_reg
=
773 info
->me4000_regbase
+ ME4000_AO_00_SINGLE_REG
;
774 ao_context
->timer_reg
=
775 info
->me4000_regbase
+ ME4000_AO_00_TIMER_REG
;
776 ao_context
->irq_status_reg
=
777 info
->me4000_regbase
+ ME4000_IRQ_STATUS_REG
;
778 ao_context
->preload_reg
=
779 info
->me4000_regbase
+ ME4000_AO_LOADSETREG_XX
;
782 ao_context
->ctrl_reg
=
783 info
->me4000_regbase
+ ME4000_AO_01_CTRL_REG
;
784 ao_context
->status_reg
=
785 info
->me4000_regbase
+ ME4000_AO_01_STATUS_REG
;
786 ao_context
->fifo_reg
=
787 info
->me4000_regbase
+ ME4000_AO_01_FIFO_REG
;
788 ao_context
->single_reg
=
789 info
->me4000_regbase
+ ME4000_AO_01_SINGLE_REG
;
790 ao_context
->timer_reg
=
791 info
->me4000_regbase
+ ME4000_AO_01_TIMER_REG
;
792 ao_context
->irq_status_reg
=
793 info
->me4000_regbase
+ ME4000_IRQ_STATUS_REG
;
794 ao_context
->preload_reg
=
795 info
->me4000_regbase
+ ME4000_AO_LOADSETREG_XX
;
798 ao_context
->ctrl_reg
=
799 info
->me4000_regbase
+ ME4000_AO_02_CTRL_REG
;
800 ao_context
->status_reg
=
801 info
->me4000_regbase
+ ME4000_AO_02_STATUS_REG
;
802 ao_context
->fifo_reg
=
803 info
->me4000_regbase
+ ME4000_AO_02_FIFO_REG
;
804 ao_context
->single_reg
=
805 info
->me4000_regbase
+ ME4000_AO_02_SINGLE_REG
;
806 ao_context
->timer_reg
=
807 info
->me4000_regbase
+ ME4000_AO_02_TIMER_REG
;
808 ao_context
->irq_status_reg
=
809 info
->me4000_regbase
+ ME4000_IRQ_STATUS_REG
;
810 ao_context
->preload_reg
=
811 info
->me4000_regbase
+ ME4000_AO_LOADSETREG_XX
;
814 ao_context
->ctrl_reg
=
815 info
->me4000_regbase
+ ME4000_AO_03_CTRL_REG
;
816 ao_context
->status_reg
=
817 info
->me4000_regbase
+ ME4000_AO_03_STATUS_REG
;
818 ao_context
->fifo_reg
=
819 info
->me4000_regbase
+ ME4000_AO_03_FIFO_REG
;
820 ao_context
->single_reg
=
821 info
->me4000_regbase
+ ME4000_AO_03_SINGLE_REG
;
822 ao_context
->timer_reg
=
823 info
->me4000_regbase
+ ME4000_AO_03_TIMER_REG
;
824 ao_context
->irq_status_reg
=
825 info
->me4000_regbase
+ ME4000_IRQ_STATUS_REG
;
826 ao_context
->preload_reg
=
827 info
->me4000_regbase
+ ME4000_AO_LOADSETREG_XX
;
833 if (info
->board_p
->ao
.fifo_count
) {
834 /* Request the interrupt line */
836 request_irq(ao_context
->irq
, me4000_ao_isr
,
837 IRQF_DISABLED
| IRQF_SHARED
,
838 ME4000_NAME
, ao_context
);
841 "%s:Can't get interrupt line", __func__
);
842 kfree(ao_context
->circ_buf
.buf
);
844 release_ao_contexts(info
);
849 list_add_tail(&ao_context
->list
, &info
->ao_context_list
);
850 ao_context
->index
= i
;
856 static void release_ao_contexts(struct me4000_info
*board_info
)
858 struct me4000_ao_context
*ao_context
, *ao_context_safe
;
860 /* Clear analog output context list */
861 list_for_each_entry_safe(ao_context
, ao_context_safe
,
862 &board_info
->ao_context_list
, list
) {
863 free_irq(ao_context
->irq
, ao_context
);
864 kfree(ao_context
->circ_buf
.buf
);
865 list_del(&ao_context
->list
);
870 static int alloc_ai_context(struct me4000_info
*info
)
872 struct me4000_ai_context
*ai_context
;
874 if (info
->board_p
->ai
.count
) {
875 ai_context
= kzalloc(sizeof(struct me4000_ai_context
),
879 "ME4000:alloc_ai_context():Can't get memory for ai context\n");
883 info
->ai_context
= ai_context
;
885 spin_lock_init(&ai_context
->use_lock
);
886 spin_lock_init(&ai_context
->int_lock
);
887 ai_context
->number
= 0;
888 ai_context
->irq
= info
->irq
;
889 init_waitqueue_head(&ai_context
->wait_queue
);
890 ai_context
->board_info
= info
;
892 ai_context
->ctrl_reg
=
893 info
->me4000_regbase
+ ME4000_AI_CTRL_REG
;
894 ai_context
->status_reg
=
895 info
->me4000_regbase
+ ME4000_AI_STATUS_REG
;
896 ai_context
->channel_list_reg
=
897 info
->me4000_regbase
+ ME4000_AI_CHANNEL_LIST_REG
;
898 ai_context
->data_reg
=
899 info
->me4000_regbase
+ ME4000_AI_DATA_REG
;
900 ai_context
->chan_timer_reg
=
901 info
->me4000_regbase
+ ME4000_AI_CHAN_TIMER_REG
;
902 ai_context
->chan_pre_timer_reg
=
903 info
->me4000_regbase
+ ME4000_AI_CHAN_PRE_TIMER_REG
;
904 ai_context
->scan_timer_low_reg
=
905 info
->me4000_regbase
+ ME4000_AI_SCAN_TIMER_LOW_REG
;
906 ai_context
->scan_timer_high_reg
=
907 info
->me4000_regbase
+ ME4000_AI_SCAN_TIMER_HIGH_REG
;
908 ai_context
->scan_pre_timer_low_reg
=
909 info
->me4000_regbase
+ ME4000_AI_SCAN_PRE_TIMER_LOW_REG
;
910 ai_context
->scan_pre_timer_high_reg
=
911 info
->me4000_regbase
+ ME4000_AI_SCAN_PRE_TIMER_HIGH_REG
;
912 ai_context
->start_reg
=
913 info
->me4000_regbase
+ ME4000_AI_START_REG
;
914 ai_context
->irq_status_reg
=
915 info
->me4000_regbase
+ ME4000_IRQ_STATUS_REG
;
916 ai_context
->sample_counter_reg
=
917 info
->me4000_regbase
+ ME4000_AI_SAMPLE_COUNTER_REG
;
923 static int alloc_dio_context(struct me4000_info
*info
)
925 struct me4000_dio_context
*dio_context
;
927 if (info
->board_p
->dio
.count
) {
928 dio_context
= kzalloc(sizeof(struct me4000_dio_context
),
932 "ME4000:alloc_dio_context():Can't get memory for dio context\n");
936 info
->dio_context
= dio_context
;
938 spin_lock_init(&dio_context
->use_lock
);
939 dio_context
->board_info
= info
;
941 dio_context
->dio_count
= info
->board_p
->dio
.count
;
943 dio_context
->dir_reg
=
944 info
->me4000_regbase
+ ME4000_DIO_DIR_REG
;
945 dio_context
->ctrl_reg
=
946 info
->me4000_regbase
+ ME4000_DIO_CTRL_REG
;
947 dio_context
->port_0_reg
=
948 info
->me4000_regbase
+ ME4000_DIO_PORT_0_REG
;
949 dio_context
->port_1_reg
=
950 info
->me4000_regbase
+ ME4000_DIO_PORT_1_REG
;
951 dio_context
->port_2_reg
=
952 info
->me4000_regbase
+ ME4000_DIO_PORT_2_REG
;
953 dio_context
->port_3_reg
=
954 info
->me4000_regbase
+ ME4000_DIO_PORT_3_REG
;
960 static int alloc_cnt_context(struct me4000_info
*info
)
962 struct me4000_cnt_context
*cnt_context
;
964 if (info
->board_p
->cnt
.count
) {
965 cnt_context
= kzalloc(sizeof(struct me4000_cnt_context
),
969 "ME4000:alloc_cnt_context():Can't get memory for cnt context\n");
973 info
->cnt_context
= cnt_context
;
975 spin_lock_init(&cnt_context
->use_lock
);
976 cnt_context
->board_info
= info
;
978 cnt_context
->ctrl_reg
=
979 info
->timer_regbase
+ ME4000_CNT_CTRL_REG
;
980 cnt_context
->counter_0_reg
=
981 info
->timer_regbase
+ ME4000_CNT_COUNTER_0_REG
;
982 cnt_context
->counter_1_reg
=
983 info
->timer_regbase
+ ME4000_CNT_COUNTER_1_REG
;
984 cnt_context
->counter_2_reg
=
985 info
->timer_regbase
+ ME4000_CNT_COUNTER_2_REG
;
991 static int alloc_ext_int_context(struct me4000_info
*info
)
993 struct me4000_ext_int_context
*ext_int_context
;
995 if (info
->board_p
->cnt
.count
) {
997 kzalloc(sizeof(struct me4000_ext_int_context
), GFP_KERNEL
);
998 if (!ext_int_context
) {
1000 "ME4000:alloc_ext_int_context():Can't get memory for cnt context\n");
1004 info
->ext_int_context
= ext_int_context
;
1006 spin_lock_init(&ext_int_context
->use_lock
);
1007 ext_int_context
->board_info
= info
;
1009 ext_int_context
->fasync_ptr
= NULL
;
1010 ext_int_context
->irq
= info
->irq
;
1012 ext_int_context
->ctrl_reg
=
1013 info
->me4000_regbase
+ ME4000_AI_CTRL_REG
;
1014 ext_int_context
->irq_status_reg
=
1015 info
->me4000_regbase
+ ME4000_IRQ_STATUS_REG
;
1021 static int me4000_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
1024 struct me4000_info
*board_info
;
1026 CALL_PDEBUG("me4000_probe() is executed\n");
1028 /* Allocate structure for board context */
1029 board_info
= kzalloc(sizeof(struct me4000_info
), GFP_KERNEL
);
1032 "ME4000:Can't get memory for board info structure\n");
1037 /* Add to global linked list */
1038 list_add_tail(&board_info
->list
, &me4000_board_info_list
);
1040 /* Get the PCI base registers */
1041 result
= get_registers(dev
, board_info
);
1043 printk(KERN_ERR
"%s:Cannot get registers\n", __func__
);
1047 /* Enable the device */
1048 result
= pci_enable_device(dev
);
1050 printk(KERN_ERR
"%s:Cannot enable PCI device\n", __func__
);
1054 /* Request the PCI register regions */
1055 result
= pci_request_regions(dev
, ME4000_NAME
);
1057 printk(KERN_ERR
"%s:Cannot request I/O regions\n", __func__
);
1061 /* Initialize board info */
1062 result
= init_board_info(dev
, board_info
);
1064 printk(KERN_ERR
"%s:Cannot init baord info\n", __func__
);
1068 /* Download the xilinx firmware */
1069 result
= me4000_xilinx_download(board_info
);
1071 printk(KERN_ERR
"%s:Can't download firmware\n", __func__
);
1075 /* Make a hardware reset */
1076 result
= me4000_reset_board(board_info
);
1078 printk(KERN_ERR
"%s :Can't reset board\n", __func__
);
1082 /* Allocate analog output context structures */
1083 result
= alloc_ao_contexts(board_info
);
1085 printk(KERN_ERR
"%s:Cannot allocate ao contexts\n", __func__
);
1089 /* Allocate analog input context */
1090 result
= alloc_ai_context(board_info
);
1092 printk(KERN_ERR
"%s:Cannot allocate ai context\n", __func__
);
1096 /* Allocate digital I/O context */
1097 result
= alloc_dio_context(board_info
);
1099 printk(KERN_ERR
"%s:Cannot allocate dio context\n", __func__
);
1103 /* Allocate counter context */
1104 result
= alloc_cnt_context(board_info
);
1106 printk(KERN_ERR
"%s:Cannot allocate cnt context\n", __func__
);
1110 /* Allocate external interrupt context */
1111 result
= alloc_ext_int_context(board_info
);
1114 "%s:Cannot allocate ext_int context\n", __func__
);
1121 kfree(board_info
->cnt_context
);
1124 kfree(board_info
->dio_context
);
1127 kfree(board_info
->ai_context
);
1130 release_ao_contexts(board_info
);
1133 pci_release_regions(dev
);
1136 list_del(&board_info
->list
);
1143 static int me4000_xilinx_download(struct me4000_info
*info
)
1148 unsigned char *firm
;
1149 wait_queue_head_t queue
;
1151 CALL_PDEBUG("me4000_xilinx_download() is executed\n");
1153 init_waitqueue_head(&queue
);
1155 firm
= (info
->device_id
== 0x4610) ? xilinx_firm_4610
: xilinx_firm
;
1158 * Set PLX local interrupt 2 polarity to high.
1159 * Interrupt is thrown by init pin of xilinx.
1161 outl(0x10, info
->plx_regbase
+ PLX_INTCSR
);
1163 /* Set /CS and /WRITE of the Xilinx */
1164 value
= inl(info
->plx_regbase
+ PLX_ICR
);
1166 outl(value
, info
->plx_regbase
+ PLX_ICR
);
1168 /* Init Xilinx with CS1 */
1169 inb(info
->program_regbase
+ 0xC8);
1171 /* Wait until /INIT pin is set */
1173 if (!(inl(info
->plx_regbase
+ PLX_INTCSR
) & 0x20)) {
1174 printk(KERN_ERR
"%s:Can't init Xilinx\n", __func__
);
1178 /* Reset /CS and /WRITE of the Xilinx */
1179 value
= inl(info
->plx_regbase
+ PLX_ICR
);
1181 outl(value
, info
->plx_regbase
+ PLX_ICR
);
1183 /* Download Xilinx firmware */
1184 size
= (firm
[0] << 24) + (firm
[1] << 16) + (firm
[2] << 8) + firm
[3];
1187 for (idx
= 0; idx
< size
; idx
++) {
1188 outb(firm
[16 + idx
], info
->program_regbase
);
1192 /* Check if BUSY flag is low */
1193 if (inl(info
->plx_regbase
+ PLX_ICR
) & 0x20) {
1195 "%s:Xilinx is still busy (idx = %d)\n", __func__
,
1201 PDEBUG("me4000_xilinx_download():%d bytes written\n", idx
);
1203 /* If done flag is high download was successful */
1204 if (inl(info
->plx_regbase
+ PLX_ICR
) & 0x4) {
1205 PDEBUG("me4000_xilinx_download():Done flag is set\n");
1206 PDEBUG("me4000_xilinx_download():Download was successful\n");
1209 "ME4000:%s:DONE flag is not set\n", __func__
);
1211 "ME4000:%s:Download not succesful\n", __func__
);
1215 /* Set /CS and /WRITE */
1216 value
= inl(info
->plx_regbase
+ PLX_ICR
);
1218 outl(value
, info
->plx_regbase
+ PLX_ICR
);
1223 static int me4000_reset_board(struct me4000_info
*info
)
1227 CALL_PDEBUG("me4000_reset_board() is executed\n");
1229 /* Make a hardware reset */
1230 icr
= me4000_inl(info
->plx_regbase
+ PLX_ICR
);
1232 me4000_outl(icr
, info
->plx_regbase
+ PLX_ICR
);
1234 me4000_outl(icr
, info
->plx_regbase
+ PLX_ICR
);
1236 /* Set both stop bits in the analog input control register */
1237 me4000_outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP
| ME4000_AI_CTRL_BIT_STOP
,
1238 info
->me4000_regbase
+ ME4000_AI_CTRL_REG
);
1240 /* Set both stop bits in the analog output control register */
1241 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
| ME4000_AO_CTRL_BIT_STOP
,
1242 info
->me4000_regbase
+ ME4000_AO_00_CTRL_REG
);
1243 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
| ME4000_AO_CTRL_BIT_STOP
,
1244 info
->me4000_regbase
+ ME4000_AO_01_CTRL_REG
);
1245 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
| ME4000_AO_CTRL_BIT_STOP
,
1246 info
->me4000_regbase
+ ME4000_AO_02_CTRL_REG
);
1247 me4000_outl(ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
| ME4000_AO_CTRL_BIT_STOP
,
1248 info
->me4000_regbase
+ ME4000_AO_03_CTRL_REG
);
1250 /* 0x8000 to the DACs means an output voltage of 0V */
1251 me4000_outl(0x8000, info
->me4000_regbase
+ ME4000_AO_00_SINGLE_REG
);
1252 me4000_outl(0x8000, info
->me4000_regbase
+ ME4000_AO_01_SINGLE_REG
);
1253 me4000_outl(0x8000, info
->me4000_regbase
+ ME4000_AO_02_SINGLE_REG
);
1254 me4000_outl(0x8000, info
->me4000_regbase
+ ME4000_AO_03_SINGLE_REG
);
1256 /* Enable interrupts on the PLX */
1257 me4000_outl(0x43, info
->plx_regbase
+ PLX_INTCSR
);
1259 /* Set the adustment register for AO demux */
1260 me4000_outl(ME4000_AO_DEMUX_ADJUST_VALUE
,
1261 info
->me4000_regbase
+ ME4000_AO_DEMUX_ADJUST_REG
);
1263 /* Set digital I/O direction for port 0 to output on isolated versions */
1264 if (!(me4000_inl(info
->me4000_regbase
+ ME4000_DIO_DIR_REG
) & 0x1))
1265 me4000_outl(0x1, info
->me4000_regbase
+ ME4000_DIO_CTRL_REG
);
1270 static int me4000_open(struct inode
*inode_p
, struct file
*file_p
)
1272 int board
, dev
, mode
;
1275 struct list_head
*ptr
;
1276 struct me4000_info
*board_info
= NULL
;
1277 struct me4000_ao_context
*ao_context
= NULL
;
1278 struct me4000_ai_context
*ai_context
= NULL
;
1279 struct me4000_dio_context
*dio_context
= NULL
;
1280 struct me4000_cnt_context
*cnt_context
= NULL
;
1281 struct me4000_ext_int_context
*ext_int_context
= NULL
;
1283 CALL_PDEBUG("me4000_open() is executed\n");
1286 if (MAJOR(inode_p
->i_rdev
) == me4000_ao_major_driver_no
) {
1287 board
= AO_BOARD(inode_p
->i_rdev
);
1288 dev
= AO_PORT(inode_p
->i_rdev
);
1289 mode
= AO_MODE(inode_p
->i_rdev
);
1291 PDEBUG("me4000_open():board = %d ao = %d mode = %d\n", board
,
1294 /* Search for the board context */
1296 list_for_each(ptr
, &me4000_board_info_list
) {
1301 board_info
= list_entry(ptr
, struct me4000_info
, list
);
1303 if (ptr
== &me4000_board_info_list
) {
1305 "ME4000:me4000_open():Board %d not in device list\n",
1310 /* Search for the dac context */
1312 list_for_each(ptr
, &board_info
->ao_context_list
) {
1317 ao_context
= list_entry(ptr
, struct me4000_ao_context
, list
);
1319 if (ptr
== &board_info
->ao_context_list
) {
1321 "ME4000:me4000_open():Device %d not in device list\n",
1326 /* Check if mode is valid */
1329 "ME4000:me4000_open():Mode is not valid\n");
1333 /* Check if mode is valid for this AO */
1334 if ((mode
!= ME4000_AO_CONV_MODE_SINGLE
)
1335 && (dev
>= board_info
->board_p
->ao
.fifo_count
)) {
1337 "ME4000:me4000_open():AO %d only in single mode available\n",
1342 /* Check if already opened */
1343 spin_lock(&ao_context
->use_lock
);
1344 if (ao_context
->dac_in_use
) {
1346 "ME4000:me4000_open():AO %d already in use\n",
1348 spin_unlock(&ao_context
->use_lock
);
1351 ao_context
->dac_in_use
= 1;
1352 spin_unlock(&ao_context
->use_lock
);
1354 ao_context
->mode
= mode
;
1356 /* Hold the context in private data */
1357 file_p
->private_data
= ao_context
;
1359 /* Set file operations pointer */
1360 file_p
->f_op
= me4000_ao_fops_array
[mode
];
1362 err
= me4000_ao_prepare(ao_context
);
1364 ao_context
->dac_in_use
= 0;
1369 else if (MAJOR(inode_p
->i_rdev
) == me4000_ai_major_driver_no
) {
1370 board
= AI_BOARD(inode_p
->i_rdev
);
1371 mode
= AI_MODE(inode_p
->i_rdev
);
1373 PDEBUG("me4000_open():ai board = %d mode = %d\n", board
, mode
);
1375 /* Search for the board context */
1377 list_for_each(ptr
, &me4000_board_info_list
) {
1382 board_info
= list_entry(ptr
, struct me4000_info
, list
);
1384 if (ptr
== &me4000_board_info_list
) {
1386 "ME4000:me4000_open():Board %d not in device list\n",
1391 ai_context
= board_info
->ai_context
;
1393 /* Check if mode is valid */
1396 "ME4000:me4000_open():Mode is not valid\n");
1400 /* Check if already opened */
1401 spin_lock(&ai_context
->use_lock
);
1402 if (ai_context
->in_use
) {
1404 "ME4000:me4000_open():AI already in use\n");
1405 spin_unlock(&ai_context
->use_lock
);
1408 ai_context
->in_use
= 1;
1409 spin_unlock(&ai_context
->use_lock
);
1411 ai_context
->mode
= mode
;
1413 /* Hold the context in private data */
1414 file_p
->private_data
= ai_context
;
1416 /* Set file operations pointer */
1417 file_p
->f_op
= me4000_ai_fops_array
[mode
];
1419 /* Prepare analog input */
1420 me4000_ai_prepare(ai_context
);
1423 else if (MAJOR(inode_p
->i_rdev
) == me4000_dio_major_driver_no
) {
1424 board
= DIO_BOARD(inode_p
->i_rdev
);
1428 PDEBUG("me4000_open():board = %d\n", board
);
1430 /* Search for the board context */
1431 list_for_each_entry(board_info
, &me4000_board_info_list
, list
) {
1432 if (board_info
->board_count
== board
)
1436 if (&board_info
->list
== &me4000_board_info_list
) {
1438 "ME4000:me4000_open():Board %d not in device list\n",
1443 /* Search for the dio context */
1444 dio_context
= board_info
->dio_context
;
1446 /* Check if already opened */
1447 spin_lock(&dio_context
->use_lock
);
1448 if (dio_context
->in_use
) {
1450 "ME4000:me4000_open():DIO already in use\n");
1451 spin_unlock(&dio_context
->use_lock
);
1454 dio_context
->in_use
= 1;
1455 spin_unlock(&dio_context
->use_lock
);
1457 /* Hold the context in private data */
1458 file_p
->private_data
= dio_context
;
1460 /* Set file operations pointer to single functions */
1461 file_p
->f_op
= &me4000_dio_fops
;
1463 //me4000_dio_reset(dio_context);
1466 else if (MAJOR(inode_p
->i_rdev
) == me4000_cnt_major_driver_no
) {
1467 board
= CNT_BOARD(inode_p
->i_rdev
);
1471 PDEBUG("me4000_open():board = %d\n", board
);
1473 /* Search for the board context */
1474 list_for_each_entry(board_info
, &me4000_board_info_list
, list
) {
1475 if (board_info
->board_count
== board
)
1479 if (&board_info
->list
== &me4000_board_info_list
) {
1481 "ME4000:me4000_open():Board %d not in device list\n",
1486 /* Get the cnt context */
1487 cnt_context
= board_info
->cnt_context
;
1489 /* Check if already opened */
1490 spin_lock(&cnt_context
->use_lock
);
1491 if (cnt_context
->in_use
) {
1493 "ME4000:me4000_open():CNT already in use\n");
1494 spin_unlock(&cnt_context
->use_lock
);
1497 cnt_context
->in_use
= 1;
1498 spin_unlock(&cnt_context
->use_lock
);
1500 /* Hold the context in private data */
1501 file_p
->private_data
= cnt_context
;
1503 /* Set file operations pointer to single functions */
1504 file_p
->f_op
= &me4000_cnt_fops
;
1506 /* External Interrupt */
1507 else if (MAJOR(inode_p
->i_rdev
) == me4000_ext_int_major_driver_no
) {
1508 board
= EXT_INT_BOARD(inode_p
->i_rdev
);
1512 PDEBUG("me4000_open():board = %d\n", board
);
1514 /* Search for the board context */
1515 list_for_each_entry(board_info
, &me4000_board_info_list
, list
) {
1516 if (board_info
->board_count
== board
)
1520 if (&board_info
->list
== &me4000_board_info_list
) {
1522 "ME4000:me4000_open():Board %d not in device list\n",
1527 /* Get the external interrupt context */
1528 ext_int_context
= board_info
->ext_int_context
;
1530 /* Check if already opened */
1531 spin_lock(&cnt_context
->use_lock
);
1532 if (ext_int_context
->in_use
) {
1534 "ME4000:me4000_open():External interrupt already in use\n");
1535 spin_unlock(&ext_int_context
->use_lock
);
1538 ext_int_context
->in_use
= 1;
1539 spin_unlock(&ext_int_context
->use_lock
);
1541 /* Hold the context in private data */
1542 file_p
->private_data
= ext_int_context
;
1544 /* Set file operations pointer to single functions */
1545 file_p
->f_op
= &me4000_ext_int_fops
;
1547 /* Request the interrupt line */
1549 request_irq(ext_int_context
->irq
, me4000_ext_int_isr
,
1550 IRQF_DISABLED
| IRQF_SHARED
, ME4000_NAME
,
1554 "ME4000:me4000_open():Can't get interrupt line");
1555 ext_int_context
->in_use
= 0;
1559 /* Reset the counter */
1560 me4000_ext_int_disable(ext_int_context
);
1562 printk(KERN_ERR
"ME4000:me4000_open():Major number unknown\n");
1569 static int me4000_release(struct inode
*inode_p
, struct file
*file_p
)
1571 struct me4000_ao_context
*ao_context
;
1572 struct me4000_ai_context
*ai_context
;
1573 struct me4000_dio_context
*dio_context
;
1574 struct me4000_cnt_context
*cnt_context
;
1575 struct me4000_ext_int_context
*ext_int_context
;
1577 CALL_PDEBUG("me4000_release() is executed\n");
1579 if (MAJOR(inode_p
->i_rdev
) == me4000_ao_major_driver_no
) {
1580 ao_context
= file_p
->private_data
;
1582 /* Mark DAC as unused */
1583 ao_context
->dac_in_use
= 0;
1584 } else if (MAJOR(inode_p
->i_rdev
) == me4000_ai_major_driver_no
) {
1585 ai_context
= file_p
->private_data
;
1587 /* Reset the analog input */
1588 me4000_ai_reset(ai_context
);
1590 /* Free the interrupt and the circular buffer */
1591 if (ai_context
->mode
) {
1592 free_irq(ai_context
->irq
, ai_context
);
1593 kfree(ai_context
->circ_buf
.buf
);
1594 ai_context
->circ_buf
.buf
= NULL
;
1595 ai_context
->circ_buf
.head
= 0;
1596 ai_context
->circ_buf
.tail
= 0;
1599 /* Mark AI as unused */
1600 ai_context
->in_use
= 0;
1601 } else if (MAJOR(inode_p
->i_rdev
) == me4000_dio_major_driver_no
) {
1602 dio_context
= file_p
->private_data
;
1604 /* Mark digital I/O as unused */
1605 dio_context
->in_use
= 0;
1606 } else if (MAJOR(inode_p
->i_rdev
) == me4000_cnt_major_driver_no
) {
1607 cnt_context
= file_p
->private_data
;
1609 /* Mark counters as unused */
1610 cnt_context
->in_use
= 0;
1611 } else if (MAJOR(inode_p
->i_rdev
) == me4000_ext_int_major_driver_no
) {
1612 ext_int_context
= file_p
->private_data
;
1614 /* Disable the externel interrupt */
1615 me4000_ext_int_disable(ext_int_context
);
1617 free_irq(ext_int_context
->irq
, ext_int_context
);
1619 /* Mark as unused */
1620 ext_int_context
->in_use
= 0;
1623 "ME4000:me4000_release():Major number unknown\n");
1630 /*------------------------------- Analog output stuff --------------------------------------*/
1632 static int me4000_ao_prepare(struct me4000_ao_context
*ao_context
)
1634 unsigned long flags
;
1636 CALL_PDEBUG("me4000_ao_prepare() is executed\n");
1638 if (ao_context
->mode
== ME4000_AO_CONV_MODE_CONTINUOUS
) {
1639 /* Only do anything if not already in the correct mode */
1640 unsigned long mode
= me4000_inl(ao_context
->ctrl_reg
);
1641 if ((mode
& ME4000_AO_CONV_MODE_CONTINUOUS
)
1642 && (mode
& ME4000_AO_CTRL_BIT_ENABLE_FIFO
)) {
1646 /* Stop any conversion */
1647 me4000_ao_immediate_stop(ao_context
);
1649 /* Set the control register to default state */
1650 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
1651 me4000_outl(ME4000_AO_CONV_MODE_CONTINUOUS
|
1652 ME4000_AO_CTRL_BIT_ENABLE_FIFO
|
1653 ME4000_AO_CTRL_BIT_STOP
|
1654 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
,
1655 ao_context
->ctrl_reg
);
1656 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
1658 /* Set to fastest sample rate */
1659 me4000_outl(65, ao_context
->timer_reg
);
1660 } else if (ao_context
->mode
== ME4000_AO_CONV_MODE_WRAPAROUND
) {
1661 /* Only do anything if not already in the correct mode */
1662 unsigned long mode
= me4000_inl(ao_context
->ctrl_reg
);
1663 if ((mode
& ME4000_AO_CONV_MODE_WRAPAROUND
)
1664 && (mode
& ME4000_AO_CTRL_BIT_ENABLE_FIFO
)) {
1668 /* Stop any conversion */
1669 me4000_ao_immediate_stop(ao_context
);
1671 /* Set the control register to default state */
1672 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
1673 me4000_outl(ME4000_AO_CONV_MODE_WRAPAROUND
|
1674 ME4000_AO_CTRL_BIT_ENABLE_FIFO
|
1675 ME4000_AO_CTRL_BIT_STOP
|
1676 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
,
1677 ao_context
->ctrl_reg
);
1678 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
1680 /* Set to fastest sample rate */
1681 me4000_outl(65, ao_context
->timer_reg
);
1682 } else if (ao_context
->mode
== ME4000_AO_CONV_MODE_SINGLE
) {
1683 /* Only do anything if not already in the correct mode */
1684 unsigned long mode
= me4000_inl(ao_context
->ctrl_reg
);
1687 (ME4000_AO_CONV_MODE_WRAPAROUND
|
1688 ME4000_AO_CONV_MODE_CONTINUOUS
))) {
1692 /* Stop any conversion */
1693 me4000_ao_immediate_stop(ao_context
);
1695 /* Clear the control register */
1696 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
1697 me4000_outl(0x0, ao_context
->ctrl_reg
);
1698 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
1700 /* Set voltage to 0V */
1701 me4000_outl(0x8000, ao_context
->single_reg
);
1704 "ME4000:me4000_ao_prepare():Invalid mode specified\n");
1711 static int me4000_ao_reset(struct me4000_ao_context
*ao_context
)
1714 wait_queue_head_t queue
;
1715 unsigned long flags
;
1717 CALL_PDEBUG("me4000_ao_reset() is executed\n");
1719 init_waitqueue_head(&queue
);
1721 if (ao_context
->mode
== ME4000_AO_CONV_MODE_WRAPAROUND
) {
1723 * First stop conversion of the DAC before reconfigure.
1724 * This is essantial, cause of the state machine.
1725 * If not stopped before configuring mode, it could
1726 * walk in a undefined state.
1728 tmp
= me4000_inl(ao_context
->ctrl_reg
);
1729 tmp
|= ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
;
1730 me4000_outl(tmp
, ao_context
->ctrl_reg
);
1732 wait_event_timeout(queue
,
1733 (inl(ao_context
->status_reg
) &
1734 ME4000_AO_STATUS_BIT_FSM
) == 0,
1737 /* Set to transparent mode */
1738 me4000_ao_simultaneous_disable(ao_context
);
1740 /* Set to single mode in order to set default voltage */
1741 me4000_outl(0x0, ao_context
->ctrl_reg
);
1743 /* Set voltage to 0V */
1744 me4000_outl(0x8000, ao_context
->single_reg
);
1746 /* Set to fastest sample rate */
1747 me4000_outl(65, ao_context
->timer_reg
);
1749 /* Set the original mode and enable FIFO */
1750 me4000_outl(ME4000_AO_CONV_MODE_WRAPAROUND
|
1751 ME4000_AO_CTRL_BIT_ENABLE_FIFO
|
1752 ME4000_AO_CTRL_BIT_STOP
|
1753 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
,
1754 ao_context
->ctrl_reg
);
1755 } else if (ao_context
->mode
== ME4000_AO_CONV_MODE_CONTINUOUS
) {
1757 * First stop conversion of the DAC before reconfigure.
1758 * This is essantial, cause of the state machine.
1759 * If not stopped before configuring mode, it could
1760 * walk in a undefined state.
1762 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
1763 tmp
= me4000_inl(ao_context
->ctrl_reg
);
1764 tmp
|= ME4000_AO_CTRL_BIT_STOP
;
1765 me4000_outl(tmp
, ao_context
->ctrl_reg
);
1766 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
1768 wait_event_timeout(queue
,
1769 (inl(ao_context
->status_reg
) &
1770 ME4000_AO_STATUS_BIT_FSM
) == 0,
1773 /* Clear the circular buffer */
1774 ao_context
->circ_buf
.head
= 0;
1775 ao_context
->circ_buf
.tail
= 0;
1777 /* Set to transparent mode */
1778 me4000_ao_simultaneous_disable(ao_context
);
1780 /* Set to single mode in order to set default voltage */
1781 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
1782 tmp
= me4000_inl(ao_context
->ctrl_reg
);
1783 me4000_outl(0x0, ao_context
->ctrl_reg
);
1785 /* Set voltage to 0V */
1786 me4000_outl(0x8000, ao_context
->single_reg
);
1788 /* Set to fastest sample rate */
1789 me4000_outl(65, ao_context
->timer_reg
);
1791 /* Set the original mode and enable FIFO */
1792 me4000_outl(ME4000_AO_CONV_MODE_CONTINUOUS
|
1793 ME4000_AO_CTRL_BIT_ENABLE_FIFO
|
1794 ME4000_AO_CTRL_BIT_STOP
|
1795 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
,
1796 ao_context
->ctrl_reg
);
1797 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
1799 /* Set to transparent mode */
1800 me4000_ao_simultaneous_disable(ao_context
);
1802 /* Set voltage to 0V */
1803 me4000_outl(0x8000, ao_context
->single_reg
);
1809 static ssize_t
me4000_ao_write_sing(struct file
*filep
, const char *buff
,
1810 size_t cnt
, loff_t
*offp
)
1812 struct me4000_ao_context
*ao_context
= filep
->private_data
;
1814 const u16
*buffer
= (const u16
*)buff
;
1816 CALL_PDEBUG("me4000_ao_write_sing() is executed\n");
1820 "%s:Write count is not 2\n", __func__
);
1824 if (get_user(value
, buffer
)) {
1826 "%s:Cannot copy data from user\n", __func__
);
1830 me4000_outl(value
, ao_context
->single_reg
);
1835 static ssize_t
me4000_ao_write_wrap(struct file
*filep
, const char *buff
,
1836 size_t cnt
, loff_t
*offp
)
1838 struct me4000_ao_context
*ao_context
= filep
->private_data
;
1842 const u16
*buffer
= (const u16
*)buff
;
1843 size_t count
= cnt
/ 2;
1845 CALL_PDEBUG("me4000_ao_write_wrap() is executed\n");
1847 /* Check if a conversion is already running */
1848 if (inl(ao_context
->status_reg
) & ME4000_AO_STATUS_BIT_FSM
) {
1850 "%s:There is already a conversion running\n", __func__
);
1854 if (count
> ME4000_AO_FIFO_COUNT
) {
1856 "%s:Can't load more than %d values\n", __func__
,
1857 ME4000_AO_FIFO_COUNT
);
1861 /* Reset the FIFO */
1862 tmp
= inl(ao_context
->ctrl_reg
);
1863 tmp
&= ~ME4000_AO_CTRL_BIT_ENABLE_FIFO
;
1864 outl(tmp
, ao_context
->ctrl_reg
);
1865 tmp
|= ME4000_AO_CTRL_BIT_ENABLE_FIFO
;
1866 outl(tmp
, ao_context
->ctrl_reg
);
1868 for (i
= 0; i
< count
; i
++) {
1869 if (get_user(value
, buffer
+ i
)) {
1871 "%s:Cannot copy data from user\n", __func__
);
1874 if (((ao_context
->fifo_reg
& 0xFF) == ME4000_AO_01_FIFO_REG
)
1875 || ((ao_context
->fifo_reg
& 0xFF) == ME4000_AO_03_FIFO_REG
))
1876 value
= value
<< 16;
1877 outl(value
, ao_context
->fifo_reg
);
1879 CALL_PDEBUG("me4000_ao_write_wrap() is leaved with %d\n", i
* 2);
1884 static ssize_t
me4000_ao_write_cont(struct file
*filep
, const char *buff
,
1885 size_t cnt
, loff_t
*offp
)
1887 struct me4000_ao_context
*ao_context
= filep
->private_data
;
1888 const u16
*buffer
= (const u16
*)buff
;
1889 size_t count
= cnt
/ 2;
1890 unsigned long flags
;
1898 wait_queue_head_t queue
;
1900 CALL_PDEBUG("me4000_ao_write_cont() is executed\n");
1902 init_waitqueue_head(&queue
);
1906 PDEBUG("me4000_ao_write_cont():Count is 0\n");
1910 if (filep
->f_flags
& O_APPEND
) {
1911 PDEBUG("me4000_ao_write_cont():Append data to data stream\n");
1913 if (filep
->f_flags
& O_NONBLOCK
) {
1914 if (ao_context
->pipe_flag
) {
1916 "ME4000:me4000_ao_write_cont():Broken pipe in nonblocking write\n");
1919 c
= me4000_space_to_end(ao_context
->circ_buf
,
1920 ME4000_AO_BUFFER_COUNT
);
1923 ("me4000_ao_write_cont():Returning from nonblocking write\n");
1927 wait_event_interruptible(ao_context
->wait_queue
,
1930 (ao_context
->circ_buf
,
1931 ME4000_AO_BUFFER_COUNT
)));
1932 if (ao_context
->pipe_flag
) {
1934 "me4000_ao_write_cont():Broken pipe in blocking write\n");
1937 if (signal_pending(current
)) {
1939 "me4000_ao_write_cont():Wait for free buffer interrupted from signal\n");
1944 PDEBUG("me4000_ao_write_cont():Space to end = %d\n", c
);
1946 /* Only able to write size of free buffer or size of count */
1951 k
-= copy_from_user(ao_context
->circ_buf
.buf
+
1952 ao_context
->circ_buf
.head
, buffer
,
1956 ("me4000_ao_write_cont():Copy %d values from user space\n",
1962 ao_context
->circ_buf
.head
=
1963 (ao_context
->circ_buf
.head
+
1964 c
) & (ME4000_AO_BUFFER_COUNT
- 1);
1969 /* Values are now available so enable interrupts */
1970 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
1971 if (me4000_buf_count
1972 (ao_context
->circ_buf
, ME4000_AO_BUFFER_COUNT
)) {
1973 tmp
= me4000_inl(ao_context
->ctrl_reg
);
1974 tmp
|= ME4000_AO_CTRL_BIT_ENABLE_IRQ
;
1975 me4000_outl(tmp
, ao_context
->ctrl_reg
);
1977 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
1980 /* Wait until the state machine is stopped if O_SYNC is set */
1981 if (filep
->f_flags
& O_SYNC
) {
1982 while (inl(ao_context
->status_reg
) &
1983 ME4000_AO_STATUS_BIT_FSM
) {
1984 interruptible_sleep_on_timeout(&queue
, 1);
1985 if (ao_context
->pipe_flag
) {
1987 ("me4000_ao_write_cont():Broken pipe detected after sync\n");
1990 if (signal_pending(current
)) {
1992 "me4000_ao_write_cont():Wait on state machine after sync interrupted\n");
1998 PDEBUG("me4000_ao_write_cont():Preload DAC FIFO\n");
1999 if ((me4000_inl(ao_context
->status_reg
) &
2000 ME4000_AO_STATUS_BIT_FSM
)) {
2002 "me4000_ao_write_cont():Can't Preload DAC FIFO while conversion is running\n");
2006 /* Clear the FIFO */
2007 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2008 tmp
= me4000_inl(ao_context
->ctrl_reg
);
2010 ~(ME4000_AO_CTRL_BIT_ENABLE_FIFO
|
2011 ME4000_AO_CTRL_BIT_ENABLE_IRQ
);
2012 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2013 tmp
|= ME4000_AO_CTRL_BIT_ENABLE_FIFO
;
2014 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2015 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2017 /* Clear the circular buffer */
2018 ao_context
->circ_buf
.head
= 0;
2019 ao_context
->circ_buf
.tail
= 0;
2021 /* Reset the broken pipe flag */
2022 ao_context
->pipe_flag
= 0;
2024 /* Only able to write size of fifo or count */
2025 c
= ME4000_AO_FIFO_COUNT
;
2030 ("me4000_ao_write_cont():Write %d values to DAC on 0x%lX\n",
2031 c
, ao_context
->fifo_reg
);
2033 /* Write values to the fifo */
2034 for (i
= 0; i
< c
; i
++) {
2035 if (get_user(svalue
, buffer
))
2038 if (((ao_context
->fifo_reg
& 0xFF) ==
2039 ME4000_AO_01_FIFO_REG
)
2040 || ((ao_context
->fifo_reg
& 0xFF) ==
2041 ME4000_AO_03_FIFO_REG
)) {
2042 lvalue
= ((u32
) svalue
) << 16;
2044 lvalue
= (u32
) svalue
;
2046 outl(lvalue
, ao_context
->fifo_reg
);
2053 /* Get free buffer */
2054 c
= me4000_space_to_end(ao_context
->circ_buf
,
2055 ME4000_AO_BUFFER_COUNT
);
2060 /* Only able to write size of free buffer or size of count */
2064 /* If count = 0 return to user */
2067 ("me4000_ao_write_cont():Count reached 0\n");
2072 k
-= copy_from_user(ao_context
->circ_buf
.buf
+
2073 ao_context
->circ_buf
.head
, buffer
,
2077 ("me4000_ao_write_cont():Wrote %d values to buffer\n",
2083 ao_context
->circ_buf
.head
=
2084 (ao_context
->circ_buf
.head
+
2085 c
) & (ME4000_AO_BUFFER_COUNT
- 1);
2090 /* If values in the buffer are available so enable interrupts */
2091 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2092 if (me4000_buf_count
2093 (ao_context
->circ_buf
, ME4000_AO_BUFFER_COUNT
)) {
2095 ("me4000_ao_write_cont():Enable Interrupts\n");
2096 tmp
= me4000_inl(ao_context
->ctrl_reg
);
2097 tmp
|= ME4000_AO_CTRL_BIT_ENABLE_IRQ
;
2098 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2100 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2104 if (filep
->f_flags
& O_NONBLOCK
)
2105 return (ret
== 0) ? -EAGAIN
: 2 * ret
;
2110 static unsigned int me4000_ao_poll_cont(struct file
*file_p
, poll_table
*wait
)
2112 struct me4000_ao_context
*ao_context
;
2113 unsigned long mask
= 0;
2115 CALL_PDEBUG("me4000_ao_poll_cont() is executed\n");
2117 ao_context
= file_p
->private_data
;
2119 poll_wait(file_p
, &ao_context
->wait_queue
, wait
);
2121 /* Get free buffer */
2122 if (me4000_space_to_end(ao_context
->circ_buf
, ME4000_AO_BUFFER_COUNT
))
2123 mask
|= POLLOUT
| POLLWRNORM
;
2125 CALL_PDEBUG("me4000_ao_poll_cont():Return mask %lX\n", mask
);
2130 static int me4000_ao_fsync_cont(struct file
*file_p
, struct dentry
*dentry_p
,
2133 struct me4000_ao_context
*ao_context
;
2134 wait_queue_head_t queue
;
2136 CALL_PDEBUG("me4000_ao_fsync_cont() is executed\n");
2138 ao_context
= file_p
->private_data
;
2139 init_waitqueue_head(&queue
);
2141 while (inl(ao_context
->status_reg
) & ME4000_AO_STATUS_BIT_FSM
) {
2142 interruptible_sleep_on_timeout(&queue
, 1);
2143 wait_event_interruptible_timeout(queue
,
2144 !(inl(ao_context
->status_reg
) & ME4000_AO_STATUS_BIT_FSM
),
2146 if (ao_context
->pipe_flag
) {
2148 "%s:Broken pipe detected\n", __func__
);
2152 if (signal_pending(current
)) {
2154 "%s:Wait on state machine interrupted\n",
2163 static int me4000_ao_ioctl_sing(struct inode
*inode_p
, struct file
*file_p
,
2164 unsigned int service
, unsigned long arg
)
2166 struct me4000_ao_context
*ao_context
;
2168 CALL_PDEBUG("me4000_ao_ioctl_sing() is executed\n");
2170 ao_context
= file_p
->private_data
;
2172 if (_IOC_TYPE(service
) != ME4000_MAGIC
) {
2174 PDEBUG("me4000_ao_ioctl_sing():Wrong magic number\n");
2178 case ME4000_AO_EX_TRIG_SETUP
:
2179 return me4000_ao_ex_trig_set_edge((int *)arg
, ao_context
);
2180 case ME4000_AO_EX_TRIG_ENABLE
:
2181 return me4000_ao_ex_trig_enable(ao_context
);
2182 case ME4000_AO_EX_TRIG_DISABLE
:
2183 return me4000_ao_ex_trig_disable(ao_context
);
2184 case ME4000_AO_PRELOAD
:
2185 return me4000_ao_preload(ao_context
);
2186 case ME4000_AO_PRELOAD_UPDATE
:
2187 return me4000_ao_preload_update(ao_context
);
2188 case ME4000_GET_USER_INFO
:
2189 return me4000_get_user_info((struct me4000_user_info
*)arg
,
2190 ao_context
->board_info
);
2191 case ME4000_AO_SIMULTANEOUS_EX_TRIG
:
2192 return me4000_ao_simultaneous_ex_trig(ao_context
);
2193 case ME4000_AO_SIMULTANEOUS_SW
:
2194 return me4000_ao_simultaneous_sw(ao_context
);
2195 case ME4000_AO_SIMULTANEOUS_DISABLE
:
2196 return me4000_ao_simultaneous_disable(ao_context
);
2197 case ME4000_AO_SIMULTANEOUS_UPDATE
:
2199 me4000_ao_simultaneous_update(
2200 (struct me4000_ao_channel_list
*)arg
,
2202 case ME4000_AO_EX_TRIG_TIMEOUT
:
2203 return me4000_ao_ex_trig_timeout((unsigned long *)arg
,
2205 case ME4000_AO_DISABLE_DO
:
2206 return me4000_ao_disable_do(ao_context
);
2209 "me4000_ao_ioctl_sing():Service number invalid\n");
2216 static int me4000_ao_ioctl_wrap(struct inode
*inode_p
, struct file
*file_p
,
2217 unsigned int service
, unsigned long arg
)
2219 struct me4000_ao_context
*ao_context
;
2221 CALL_PDEBUG("me4000_ao_ioctl_wrap() is executed\n");
2223 ao_context
= file_p
->private_data
;
2225 if (_IOC_TYPE(service
) != ME4000_MAGIC
) {
2227 PDEBUG("me4000_ao_ioctl_wrap():Wrong magic number\n");
2231 case ME4000_AO_START
:
2232 return me4000_ao_start((unsigned long *)arg
, ao_context
);
2233 case ME4000_AO_STOP
:
2234 return me4000_ao_stop(ao_context
);
2235 case ME4000_AO_IMMEDIATE_STOP
:
2236 return me4000_ao_immediate_stop(ao_context
);
2237 case ME4000_AO_RESET
:
2238 return me4000_ao_reset(ao_context
);
2239 case ME4000_AO_TIMER_SET_DIVISOR
:
2240 return me4000_ao_timer_set_divisor((u32
*) arg
, ao_context
);
2241 case ME4000_AO_EX_TRIG_SETUP
:
2242 return me4000_ao_ex_trig_set_edge((int *)arg
, ao_context
);
2243 case ME4000_AO_EX_TRIG_ENABLE
:
2244 return me4000_ao_ex_trig_enable(ao_context
);
2245 case ME4000_AO_EX_TRIG_DISABLE
:
2246 return me4000_ao_ex_trig_disable(ao_context
);
2247 case ME4000_GET_USER_INFO
:
2248 return me4000_get_user_info((struct me4000_user_info
*)arg
,
2249 ao_context
->board_info
);
2250 case ME4000_AO_FSM_STATE
:
2251 return me4000_ao_fsm_state((int *)arg
, ao_context
);
2252 case ME4000_AO_ENABLE_DO
:
2253 return me4000_ao_enable_do(ao_context
);
2254 case ME4000_AO_DISABLE_DO
:
2255 return me4000_ao_disable_do(ao_context
);
2256 case ME4000_AO_SYNCHRONOUS_EX_TRIG
:
2257 return me4000_ao_synchronous_ex_trig(ao_context
);
2258 case ME4000_AO_SYNCHRONOUS_SW
:
2259 return me4000_ao_synchronous_sw(ao_context
);
2260 case ME4000_AO_SYNCHRONOUS_DISABLE
:
2261 return me4000_ao_synchronous_disable(ao_context
);
2268 static int me4000_ao_ioctl_cont(struct inode
*inode_p
, struct file
*file_p
,
2269 unsigned int service
, unsigned long arg
)
2271 struct me4000_ao_context
*ao_context
;
2273 CALL_PDEBUG("me4000_ao_ioctl_cont() is executed\n");
2275 ao_context
= file_p
->private_data
;
2277 if (_IOC_TYPE(service
) != ME4000_MAGIC
) {
2279 PDEBUG("me4000_ao_ioctl_cont():Wrong magic number\n");
2283 case ME4000_AO_START
:
2284 return me4000_ao_start((unsigned long *)arg
, ao_context
);
2285 case ME4000_AO_STOP
:
2286 return me4000_ao_stop(ao_context
);
2287 case ME4000_AO_IMMEDIATE_STOP
:
2288 return me4000_ao_immediate_stop(ao_context
);
2289 case ME4000_AO_RESET
:
2290 return me4000_ao_reset(ao_context
);
2291 case ME4000_AO_TIMER_SET_DIVISOR
:
2292 return me4000_ao_timer_set_divisor((u32
*) arg
, ao_context
);
2293 case ME4000_AO_EX_TRIG_SETUP
:
2294 return me4000_ao_ex_trig_set_edge((int *)arg
, ao_context
);
2295 case ME4000_AO_EX_TRIG_ENABLE
:
2296 return me4000_ao_ex_trig_enable(ao_context
);
2297 case ME4000_AO_EX_TRIG_DISABLE
:
2298 return me4000_ao_ex_trig_disable(ao_context
);
2299 case ME4000_AO_ENABLE_DO
:
2300 return me4000_ao_enable_do(ao_context
);
2301 case ME4000_AO_DISABLE_DO
:
2302 return me4000_ao_disable_do(ao_context
);
2303 case ME4000_AO_FSM_STATE
:
2304 return me4000_ao_fsm_state((int *)arg
, ao_context
);
2305 case ME4000_GET_USER_INFO
:
2306 return me4000_get_user_info((struct me4000_user_info
*)arg
,
2307 ao_context
->board_info
);
2308 case ME4000_AO_SYNCHRONOUS_EX_TRIG
:
2309 return me4000_ao_synchronous_ex_trig(ao_context
);
2310 case ME4000_AO_SYNCHRONOUS_SW
:
2311 return me4000_ao_synchronous_sw(ao_context
);
2312 case ME4000_AO_SYNCHRONOUS_DISABLE
:
2313 return me4000_ao_synchronous_disable(ao_context
);
2314 case ME4000_AO_GET_FREE_BUFFER
:
2315 return me4000_ao_get_free_buffer((unsigned long *)arg
,
2323 static int me4000_ao_start(unsigned long *arg
,
2324 struct me4000_ao_context
*ao_context
)
2327 wait_queue_head_t queue
;
2329 unsigned long timeout
;
2330 unsigned long flags
;
2332 CALL_PDEBUG("me4000_ao_start() is executed\n");
2334 if (get_user(timeout
, arg
)) {
2336 "me4000_ao_start():Cannot copy data from user\n");
2340 init_waitqueue_head(&queue
);
2342 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2343 tmp
= inl(ao_context
->ctrl_reg
);
2344 tmp
&= ~(ME4000_AO_CTRL_BIT_STOP
| ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
);
2345 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2346 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2348 if ((tmp
& ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG
)) {
2352 (inl(ao_context
->status_reg
) &
2353 ME4000_AO_STATUS_BIT_FSM
)) {
2354 interruptible_sleep_on_timeout(&queue
, 1);
2355 if (signal_pending(current
)) {
2357 "ME4000:me4000_ao_start():Wait on start of state machine interrupted\n");
2360 /* kernel 2.6 has different definitions for HZ
2361 * in user and kernel space */
2362 if ((jiffies
- ref
) > (timeout
* HZ
/ USER_HZ
)) {
2364 "ME4000:me4000_ao_start():Timeout reached\n");
2370 me4000_outl(0x8000, ao_context
->single_reg
);
2376 static int me4000_ao_stop(struct me4000_ao_context
*ao_context
)
2379 wait_queue_head_t queue
;
2380 unsigned long flags
;
2382 init_waitqueue_head(&queue
);
2384 CALL_PDEBUG("me4000_ao_stop() is executed\n");
2386 /* Set the stop bit */
2387 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2388 tmp
= inl(ao_context
->ctrl_reg
);
2389 tmp
|= ME4000_AO_CTRL_BIT_STOP
;
2390 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2391 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2393 while (inl(ao_context
->status_reg
) & ME4000_AO_STATUS_BIT_FSM
) {
2394 interruptible_sleep_on_timeout(&queue
, 1);
2395 if (signal_pending(current
)) {
2397 "me4000_ao_stop():Wait on state machine after stop interrupted\n");
2402 /* Clear the stop bit */
2403 //tmp &= ~ME4000_AO_CTRL_BIT_STOP;
2404 //me4000_outl(tmp, ao_context->ctrl_reg);
2409 static int me4000_ao_immediate_stop(struct me4000_ao_context
*ao_context
)
2412 wait_queue_head_t queue
;
2413 unsigned long flags
;
2415 init_waitqueue_head(&queue
);
2417 CALL_PDEBUG("me4000_ao_immediate_stop() is executed\n");
2419 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2420 tmp
= inl(ao_context
->ctrl_reg
);
2421 tmp
|= ME4000_AO_CTRL_BIT_STOP
| ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
;
2422 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2423 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2425 while (inl(ao_context
->status_reg
) & ME4000_AO_STATUS_BIT_FSM
) {
2426 interruptible_sleep_on_timeout(&queue
, 1);
2427 if (signal_pending(current
)) {
2429 "me4000_ao_immediate_stop():Wait on state machine after stop interrupted\n");
2434 /* Clear the stop bits */
2435 //tmp &= ~(ME4000_AO_CTRL_BIT_STOP | ME4000_AO_CTRL_BIT_IMMEDIATE_STOP);
2436 //me4000_outl(tmp, ao_context->ctrl_reg);
2441 static int me4000_ao_timer_set_divisor(u32
*arg
,
2442 struct me4000_ao_context
*ao_context
)
2447 CALL_PDEBUG("me4000_ao_timer set_divisor() is executed\n");
2449 if (get_user(divisor
, arg
))
2452 /* Check if the state machine is stopped */
2453 tmp
= me4000_inl(ao_context
->status_reg
);
2454 if (tmp
& ME4000_AO_STATUS_BIT_FSM
) {
2456 "me4000_ao_timer_set_divisor():Can't set timer while DAC is running\n");
2460 PDEBUG("me4000_ao_timer set_divisor():Divisor from user = %d\n",
2463 /* Check if the divisor is right. ME4000_AO_MIN_TICKS is the lowest */
2464 if (divisor
< ME4000_AO_MIN_TICKS
) {
2466 "ME4000:me4000_ao_timer set_divisor():Divisor to low\n");
2470 /* Fix bug in Firmware */
2473 PDEBUG("me4000_ao_timer set_divisor():Divisor to HW = %d\n", divisor
);
2475 /* Write the divisor */
2476 me4000_outl(divisor
, ao_context
->timer_reg
);
2481 static int me4000_ao_ex_trig_set_edge(int *arg
,
2482 struct me4000_ao_context
*ao_context
)
2486 unsigned long flags
;
2488 CALL_PDEBUG("me4000_ao_ex_trig_set_edge() is executed\n");
2490 if (get_user(mode
, arg
))
2493 /* Check if the state machine is stopped */
2494 tmp
= me4000_inl(ao_context
->status_reg
);
2495 if (tmp
& ME4000_AO_STATUS_BIT_FSM
) {
2497 "me4000_ao_ex_trig_set_edge():Can't set trigger while DAC is running\n");
2501 if (mode
== ME4000_AO_TRIGGER_EXT_EDGE_RISING
) {
2502 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2503 tmp
= me4000_inl(ao_context
->ctrl_reg
);
2505 ~(ME4000_AO_CTRL_BIT_EX_TRIG_EDGE
|
2506 ME4000_AO_CTRL_BIT_EX_TRIG_BOTH
);
2507 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2508 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2509 } else if (mode
== ME4000_AO_TRIGGER_EXT_EDGE_FALLING
) {
2510 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2511 tmp
= me4000_inl(ao_context
->ctrl_reg
);
2512 tmp
&= ~ME4000_AO_CTRL_BIT_EX_TRIG_BOTH
;
2513 tmp
|= ME4000_AO_CTRL_BIT_EX_TRIG_EDGE
;
2514 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2515 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2516 } else if (mode
== ME4000_AO_TRIGGER_EXT_EDGE_BOTH
) {
2517 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2518 tmp
= me4000_inl(ao_context
->ctrl_reg
);
2520 ME4000_AO_CTRL_BIT_EX_TRIG_EDGE
|
2521 ME4000_AO_CTRL_BIT_EX_TRIG_BOTH
;
2522 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2523 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2526 "me4000_ao_ex_trig_set_edge():Invalid trigger mode\n");
2533 static int me4000_ao_ex_trig_enable(struct me4000_ao_context
*ao_context
)
2536 unsigned long flags
;
2538 CALL_PDEBUG("me4000_ao_ex_trig_enable() is executed\n");
2540 /* Check if the state machine is stopped */
2541 tmp
= me4000_inl(ao_context
->status_reg
);
2542 if (tmp
& ME4000_AO_STATUS_BIT_FSM
) {
2544 "me4000_ao_ex_trig_enable():Can't enable trigger while DAC is running\n");
2548 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2549 tmp
= me4000_inl(ao_context
->ctrl_reg
);
2550 tmp
|= ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG
;
2551 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2552 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2557 static int me4000_ao_ex_trig_disable(struct me4000_ao_context
*ao_context
)
2560 unsigned long flags
;
2562 CALL_PDEBUG("me4000_ao_ex_trig_disable() is executed\n");
2564 /* Check if the state machine is stopped */
2565 tmp
= me4000_inl(ao_context
->status_reg
);
2566 if (tmp
& ME4000_AO_STATUS_BIT_FSM
) {
2568 "me4000_ao_ex_trig_disable():Can't disable trigger while DAC is running\n");
2572 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2573 tmp
= me4000_inl(ao_context
->ctrl_reg
);
2574 tmp
&= ~ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG
;
2575 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2576 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2581 static int me4000_ao_simultaneous_disable(struct me4000_ao_context
*ao_context
)
2585 CALL_PDEBUG("me4000_ao_simultaneous_disable() is executed\n");
2587 /* Check if the state machine is stopped */
2588 /* Be careful here because this function is called from
2589 me4000_ao_synchronous disable */
2590 tmp
= me4000_inl(ao_context
->status_reg
);
2591 if (tmp
& ME4000_AO_STATUS_BIT_FSM
) {
2593 "me4000_ao_simultaneous_disable():Can't disable while DAC is running\n");
2597 spin_lock(&ao_context
->board_info
->preload_lock
);
2598 tmp
= me4000_inl(ao_context
->preload_reg
);
2599 tmp
&= ~(0x1 << ao_context
->index
); // Disable preload bit
2600 tmp
&= ~(0x1 << (ao_context
->index
+ 16)); // Disable hw simultaneous bit
2601 me4000_outl(tmp
, ao_context
->preload_reg
);
2602 spin_unlock(&ao_context
->board_info
->preload_lock
);
2607 static int me4000_ao_simultaneous_ex_trig(struct me4000_ao_context
*ao_context
)
2611 CALL_PDEBUG("me4000_ao_simultaneous_ex_trig() is executed\n");
2613 spin_lock(&ao_context
->board_info
->preload_lock
);
2614 tmp
= me4000_inl(ao_context
->preload_reg
);
2615 tmp
|= (0x1 << ao_context
->index
); // Enable preload bit
2616 tmp
|= (0x1 << (ao_context
->index
+ 16)); // Enable hw simultaneous bit
2617 me4000_outl(tmp
, ao_context
->preload_reg
);
2618 spin_unlock(&ao_context
->board_info
->preload_lock
);
2623 static int me4000_ao_simultaneous_sw(struct me4000_ao_context
*ao_context
)
2627 CALL_PDEBUG("me4000_ao_simultaneous_sw() is executed\n");
2629 spin_lock(&ao_context
->board_info
->preload_lock
);
2630 tmp
= me4000_inl(ao_context
->preload_reg
);
2631 tmp
|= (0x1 << ao_context
->index
); // Enable preload bit
2632 tmp
&= ~(0x1 << (ao_context
->index
+ 16)); // Disable hw simultaneous bit
2633 me4000_outl(tmp
, ao_context
->preload_reg
);
2634 spin_unlock(&ao_context
->board_info
->preload_lock
);
2639 static int me4000_ao_preload(struct me4000_ao_context
*ao_context
)
2641 CALL_PDEBUG("me4000_ao_preload() is executed\n");
2642 return me4000_ao_simultaneous_sw(ao_context
);
2645 static int me4000_ao_preload_update(struct me4000_ao_context
*ao_context
)
2649 struct list_head
*entry
;
2651 CALL_PDEBUG("me4000_ao_preload_update() is executed\n");
2653 spin_lock(&ao_context
->board_info
->preload_lock
);
2654 tmp
= me4000_inl(ao_context
->preload_reg
);
2655 list_for_each(entry
, &ao_context
->board_info
->ao_context_list
) {
2656 /* The channels we update must be in the following state :
2658 - Hardware trigger is disabled
2659 - Corresponding simultaneous bit is reset
2661 ctrl
= me4000_inl(ao_context
->ctrl_reg
);
2664 (ME4000_AO_CTRL_BIT_MODE_0
| ME4000_AO_CTRL_BIT_MODE_1
|
2665 ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG
))) {
2669 (((struct me4000_ao_context
*)entry
)->index
2673 (((struct me4000_ao_context
*)entry
)->
2678 me4000_outl(tmp
, ao_context
->preload_reg
);
2679 spin_unlock(&ao_context
->board_info
->preload_lock
);
2684 static int me4000_ao_simultaneous_update(struct me4000_ao_channel_list
*arg
,
2685 struct me4000_ao_context
*ao_context
)
2690 struct me4000_ao_channel_list channels
;
2692 CALL_PDEBUG("me4000_ao_simultaneous_update() is executed\n");
2694 /* Copy data from user */
2695 err
= copy_from_user(&channels
, arg
,
2696 sizeof(struct me4000_ao_channel_list
));
2699 "ME4000:me4000_ao_simultaneous_update():Can't copy command\n");
2704 kzalloc(sizeof(unsigned long) * channels
.count
, GFP_KERNEL
);
2705 if (!channels
.list
) {
2707 "ME4000:me4000_ao_simultaneous_update():Can't get buffer\n");
2711 /* Copy channel list from user */
2713 copy_from_user(channels
.list
, arg
->list
,
2714 sizeof(unsigned long) * channels
.count
);
2717 "ME4000:me4000_ao_simultaneous_update():Can't copy list\n");
2718 kfree(channels
.list
);
2722 spin_lock(&ao_context
->board_info
->preload_lock
);
2723 tmp
= me4000_inl(ao_context
->preload_reg
);
2724 for (i
= 0; i
< channels
.count
; i
++) {
2725 if (channels
.list
[i
] >
2726 ao_context
->board_info
->board_p
->ao
.count
) {
2727 spin_unlock(&ao_context
->board_info
->preload_lock
);
2728 kfree(channels
.list
);
2730 "ME4000:me4000_ao_simultaneous_update():Invalid board number specified\n");
2733 tmp
&= ~(0x1 << channels
.list
[i
]); // Clear the preload bit
2734 tmp
&= ~(0x1 << (channels
.list
[i
] + 16)); // Clear the hw simultaneous bit
2736 me4000_outl(tmp
, ao_context
->preload_reg
);
2737 spin_unlock(&ao_context
->board_info
->preload_lock
);
2738 kfree(channels
.list
);
2743 static int me4000_ao_synchronous_ex_trig(struct me4000_ao_context
*ao_context
)
2746 unsigned long flags
;
2748 CALL_PDEBUG("me4000_ao_synchronous_ex_trig() is executed\n");
2750 /* Check if the state machine is stopped */
2751 tmp
= me4000_inl(ao_context
->status_reg
);
2752 if (tmp
& ME4000_AO_STATUS_BIT_FSM
) {
2754 "me4000_ao_synchronous_ex_trig(): DAC is running\n");
2758 spin_lock(&ao_context
->board_info
->preload_lock
);
2759 tmp
= me4000_inl(ao_context
->preload_reg
);
2760 tmp
&= ~(0x1 << ao_context
->index
); // Disable synchronous sw bit
2761 tmp
|= 0x1 << (ao_context
->index
+ 16); // Enable synchronous hw bit
2762 me4000_outl(tmp
, ao_context
->preload_reg
);
2763 spin_unlock(&ao_context
->board_info
->preload_lock
);
2766 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2767 tmp
= me4000_inl(ao_context
->ctrl_reg
);
2768 if (tmp
& (ME4000_AO_CTRL_BIT_MODE_0
| ME4000_AO_CTRL_BIT_MODE_1
)) {
2770 ~(ME4000_AO_CTRL_BIT_STOP
|
2771 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
);
2772 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2774 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2779 static int me4000_ao_synchronous_sw(struct me4000_ao_context
*ao_context
)
2782 unsigned long flags
;
2784 CALL_PDEBUG("me4000_ao_synchronous_sw() is executed\n");
2786 /* Check if the state machine is stopped */
2787 tmp
= me4000_inl(ao_context
->status_reg
);
2788 if (tmp
& ME4000_AO_STATUS_BIT_FSM
) {
2789 printk(KERN_ERR
"me4000_ao_synchronous_sw(): DAC is running\n");
2793 spin_lock(&ao_context
->board_info
->preload_lock
);
2794 tmp
= me4000_inl(ao_context
->preload_reg
);
2795 tmp
|= 0x1 << ao_context
->index
; // Enable synchronous sw bit
2796 tmp
&= ~(0x1 << (ao_context
->index
+ 16)); // Disable synchronous hw bit
2797 me4000_outl(tmp
, ao_context
->preload_reg
);
2798 spin_unlock(&ao_context
->board_info
->preload_lock
);
2801 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2802 tmp
= me4000_inl(ao_context
->ctrl_reg
);
2803 if (tmp
& (ME4000_AO_CTRL_BIT_MODE_0
| ME4000_AO_CTRL_BIT_MODE_1
)) {
2805 ~(ME4000_AO_CTRL_BIT_STOP
|
2806 ME4000_AO_CTRL_BIT_IMMEDIATE_STOP
);
2807 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2809 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2814 static int me4000_ao_synchronous_disable(struct me4000_ao_context
*ao_context
)
2816 return me4000_ao_simultaneous_disable(ao_context
);
2819 static int me4000_ao_get_free_buffer(unsigned long *arg
,
2820 struct me4000_ao_context
*ao_context
)
2825 c
= me4000_buf_space(ao_context
->circ_buf
, ME4000_AO_BUFFER_COUNT
);
2827 err
= copy_to_user(arg
, &c
, sizeof(unsigned long));
2830 "%s:Can't copy to user space\n", __func__
);
2837 static int me4000_ao_ex_trig_timeout(unsigned long *arg
,
2838 struct me4000_ao_context
*ao_context
)
2841 wait_queue_head_t queue
;
2843 unsigned long timeout
;
2845 CALL_PDEBUG("me4000_ao_ex_trig_timeout() is executed\n");
2847 if (get_user(timeout
, arg
)) {
2849 "me4000_ao_ex_trig_timeout():Cannot copy data from user\n");
2853 init_waitqueue_head(&queue
);
2855 tmp
= inl(ao_context
->ctrl_reg
);
2857 if ((tmp
& ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG
)) {
2860 while ((inl(ao_context
->status_reg
) &
2861 ME4000_AO_STATUS_BIT_FSM
)) {
2862 interruptible_sleep_on_timeout(&queue
, 1);
2863 if (signal_pending(current
)) {
2865 "ME4000:me4000_ao_ex_trig_timeout():Wait on start of state machine interrupted\n");
2868 /* kernel 2.6 has different definitions for HZ
2869 * in user and kernel space */
2870 if ((jiffies
- ref
) > (timeout
* HZ
/ USER_HZ
)) {
2872 "ME4000:me4000_ao_ex_trig_timeout():Timeout reached\n");
2877 while ((inl(ao_context
->status_reg
) &
2878 ME4000_AO_STATUS_BIT_FSM
)) {
2879 interruptible_sleep_on_timeout(&queue
, 1);
2880 if (signal_pending(current
)) {
2882 "ME4000:me4000_ao_ex_trig_timeout():Wait on start of state machine interrupted\n");
2889 "ME4000:me4000_ao_ex_trig_timeout():External Trigger is not enabled\n");
2896 static int me4000_ao_enable_do(struct me4000_ao_context
*ao_context
)
2899 unsigned long flags
;
2901 CALL_PDEBUG("me4000_ao_enable_do() is executed\n");
2903 /* Only available for analog output 3 */
2904 if (ao_context
->index
!= 3) {
2906 "me4000_ao_enable_do():Only available for analog output 3\n");
2910 /* Check if the state machine is stopped */
2911 tmp
= me4000_inl(ao_context
->status_reg
);
2912 if (tmp
& ME4000_AO_STATUS_BIT_FSM
) {
2913 printk(KERN_ERR
"me4000_ao_enable_do(): DAC is running\n");
2917 /* Set the stop bit */
2918 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2919 tmp
= inl(ao_context
->ctrl_reg
);
2920 tmp
|= ME4000_AO_CTRL_BIT_ENABLE_DO
;
2921 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2922 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2927 static int me4000_ao_disable_do(struct me4000_ao_context
*ao_context
)
2930 unsigned long flags
;
2932 CALL_PDEBUG("me4000_ao_disable_do() is executed\n");
2934 /* Only available for analog output 3 */
2935 if (ao_context
->index
!= 3) {
2937 "me4000_ao_disable():Only available for analog output 3\n");
2941 /* Check if the state machine is stopped */
2942 tmp
= me4000_inl(ao_context
->status_reg
);
2943 if (tmp
& ME4000_AO_STATUS_BIT_FSM
) {
2944 printk(KERN_ERR
"me4000_ao_disable_do(): DAC is running\n");
2948 spin_lock_irqsave(&ao_context
->int_lock
, flags
);
2949 tmp
= inl(ao_context
->ctrl_reg
);
2950 tmp
&= ~(ME4000_AO_CTRL_BIT_ENABLE_DO
);
2951 me4000_outl(tmp
, ao_context
->ctrl_reg
);
2952 spin_unlock_irqrestore(&ao_context
->int_lock
, flags
);
2957 static int me4000_ao_fsm_state(int *arg
, struct me4000_ao_context
*ao_context
)
2961 CALL_PDEBUG("me4000_ao_fsm_state() is executed\n");
2964 (me4000_inl(ao_context
->status_reg
) & ME4000_AO_STATUS_BIT_FSM
) ? 1
2967 if (ao_context
->pipe_flag
) {
2968 printk(KERN_ERR
"me4000_ao_fsm_state():Broken pipe detected\n");
2972 if (put_user(tmp
, arg
)) {
2973 printk(KERN_ERR
"me4000_ao_fsm_state():Cannot copy to user\n");
2980 /*------------------------- Analog input stuff -------------------------------*/
2982 static int me4000_ai_prepare(struct me4000_ai_context
*ai_context
)
2984 wait_queue_head_t queue
;
2987 CALL_PDEBUG("me4000_ai_prepare() is executed\n");
2989 init_waitqueue_head(&queue
);
2991 /* Set the new mode and stop bits */
2992 me4000_outl(ai_context
->
2993 mode
| ME4000_AI_CTRL_BIT_STOP
|
2994 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP
, ai_context
->ctrl_reg
);
2996 /* Set the timer registers */
2997 ai_context
->chan_timer
= 66;
2998 ai_context
->chan_pre_timer
= 66;
2999 ai_context
->scan_timer_low
= 0;
3000 ai_context
->scan_timer_high
= 0;
3002 me4000_outl(65, ai_context
->chan_timer_reg
);
3003 me4000_outl(65, ai_context
->chan_pre_timer_reg
);
3004 me4000_outl(0, ai_context
->scan_timer_low_reg
);
3005 me4000_outl(0, ai_context
->scan_timer_high_reg
);
3006 me4000_outl(0, ai_context
->scan_pre_timer_low_reg
);
3007 me4000_outl(0, ai_context
->scan_pre_timer_high_reg
);
3009 ai_context
->channel_list_count
= 0;
3011 if (ai_context
->mode
) {
3012 /* Request the interrupt line */
3014 request_irq(ai_context
->irq
, me4000_ai_isr
,
3015 IRQF_DISABLED
| IRQF_SHARED
, ME4000_NAME
,
3019 "ME4000:me4000_ai_prepare():Can't get interrupt line");
3023 /* Allocate circular buffer */
3024 ai_context
->circ_buf
.buf
=
3025 kzalloc(ME4000_AI_BUFFER_SIZE
, GFP_KERNEL
);
3026 if (!ai_context
->circ_buf
.buf
) {
3028 "ME4000:me4000_ai_prepare():Can't get circular buffer\n");
3029 free_irq(ai_context
->irq
, ai_context
);
3033 /* Clear the circular buffer */
3034 ai_context
->circ_buf
.head
= 0;
3035 ai_context
->circ_buf
.tail
= 0;
3041 static int me4000_ai_reset(struct me4000_ai_context
*ai_context
)
3043 wait_queue_head_t queue
;
3045 unsigned long flags
;
3047 CALL_PDEBUG("me4000_ai_reset() is executed\n");
3049 init_waitqueue_head(&queue
);
3052 * First stop conversion of the state machine before reconfigure.
3053 * If not stopped before configuring mode, it could
3054 * walk in a undefined state.
3056 spin_lock_irqsave(&ai_context
->int_lock
, flags
);
3057 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3058 tmp
|= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP
;
3059 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3060 spin_unlock_irqrestore(&ai_context
->int_lock
, flags
);
3062 while (inl(ai_context
->status_reg
) & ME4000_AI_STATUS_BIT_FSM
) {
3063 interruptible_sleep_on_timeout(&queue
, 1);
3064 if (signal_pending(current
)) {
3066 "me4000_ai_reset():Wait on state machine after stop interrupted\n");
3071 /* Clear the control register and set the stop bits */
3072 spin_lock_irqsave(&ai_context
->int_lock
, flags
);
3073 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3074 me4000_outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP
| ME4000_AI_CTRL_BIT_STOP
,
3075 ai_context
->ctrl_reg
);
3076 spin_unlock_irqrestore(&ai_context
->int_lock
, flags
);
3078 /* Reset timer registers */
3079 ai_context
->chan_timer
= 66;
3080 ai_context
->chan_pre_timer
= 66;
3081 ai_context
->scan_timer_low
= 0;
3082 ai_context
->scan_timer_high
= 0;
3083 ai_context
->sample_counter
= 0;
3084 ai_context
->sample_counter_reload
= 0;
3086 me4000_outl(65, ai_context
->chan_timer_reg
);
3087 me4000_outl(65, ai_context
->chan_pre_timer_reg
);
3088 me4000_outl(0, ai_context
->scan_timer_low_reg
);
3089 me4000_outl(0, ai_context
->scan_timer_high_reg
);
3090 me4000_outl(0, ai_context
->scan_pre_timer_low_reg
);
3091 me4000_outl(0, ai_context
->scan_pre_timer_high_reg
);
3092 me4000_outl(0, ai_context
->sample_counter_reg
);
3094 ai_context
->channel_list_count
= 0;
3096 /* Clear the circular buffer */
3097 ai_context
->circ_buf
.head
= 0;
3098 ai_context
->circ_buf
.tail
= 0;
3103 static int me4000_ai_ioctl_sing(struct inode
*inode_p
, struct file
*file_p
,
3104 unsigned int service
, unsigned long arg
)
3106 struct me4000_ai_context
*ai_context
;
3108 CALL_PDEBUG("me4000_ai_ioctl_sing() is executed\n");
3110 ai_context
= file_p
->private_data
;
3112 if (_IOC_TYPE(service
) != ME4000_MAGIC
) {
3113 printk(KERN_ERR
"me4000_ai_ioctl_sing():Wrong magic number\n");
3116 if (_IOC_NR(service
) > ME4000_IOCTL_MAXNR
) {
3118 "me4000_ai_ioctl_sing():Service number to high\n");
3123 case ME4000_AI_SINGLE
:
3124 return me4000_ai_single((struct me4000_ai_single
*)arg
,
3126 case ME4000_AI_EX_TRIG_ENABLE
:
3127 return me4000_ai_ex_trig_enable(ai_context
);
3128 case ME4000_AI_EX_TRIG_DISABLE
:
3129 return me4000_ai_ex_trig_disable(ai_context
);
3130 case ME4000_AI_EX_TRIG_SETUP
:
3131 return me4000_ai_ex_trig_setup((struct me4000_ai_trigger
*)arg
,
3133 case ME4000_GET_USER_INFO
:
3134 return me4000_get_user_info((struct me4000_user_info
*)arg
,
3135 ai_context
->board_info
);
3136 case ME4000_AI_OFFSET_ENABLE
:
3137 return me4000_ai_offset_enable(ai_context
);
3138 case ME4000_AI_OFFSET_DISABLE
:
3139 return me4000_ai_offset_disable(ai_context
);
3140 case ME4000_AI_FULLSCALE_ENABLE
:
3141 return me4000_ai_fullscale_enable(ai_context
);
3142 case ME4000_AI_FULLSCALE_DISABLE
:
3143 return me4000_ai_fullscale_disable(ai_context
);
3144 case ME4000_AI_EEPROM_READ
:
3145 return me4000_eeprom_read((struct me4000_eeprom
*)arg
,
3147 case ME4000_AI_EEPROM_WRITE
:
3148 return me4000_eeprom_write((struct me4000_eeprom
*)arg
,
3152 "me4000_ai_ioctl_sing():Invalid service number\n");
3158 static int me4000_ai_single(struct me4000_ai_single
*arg
,
3159 struct me4000_ai_context
*ai_context
)
3161 struct me4000_ai_single cmd
;
3164 wait_queue_head_t queue
;
3165 unsigned long jiffy
;
3167 CALL_PDEBUG("me4000_ai_single() is executed\n");
3169 init_waitqueue_head(&queue
);
3171 /* Copy data from user */
3172 err
= copy_from_user(&cmd
, arg
, sizeof(struct me4000_ai_single
));
3175 "ME4000:me4000_ai_single():Can't copy from user space\n");
3179 /* Check range parameter */
3180 switch (cmd
.range
) {
3181 case ME4000_AI_LIST_RANGE_BIPOLAR_10
:
3182 case ME4000_AI_LIST_RANGE_BIPOLAR_2_5
:
3183 case ME4000_AI_LIST_RANGE_UNIPOLAR_10
:
3184 case ME4000_AI_LIST_RANGE_UNIPOLAR_2_5
:
3188 "ME4000:me4000_ai_single():Invalid range specified\n");
3192 /* Check mode and channel number */
3194 case ME4000_AI_LIST_INPUT_SINGLE_ENDED
:
3195 if (cmd
.channel
>= ai_context
->board_info
->board_p
->ai
.count
) {
3197 "ME4000:me4000_ai_single():Analog input is not available\n");
3201 case ME4000_AI_LIST_INPUT_DIFFERENTIAL
:
3203 ai_context
->board_info
->board_p
->ai
.diff_count
) {
3205 "ME4000:me4000_ai_single():Analog input is not available in differential mode\n");
3211 "ME4000:me4000_ai_single():Invalid mode specified\n");
3215 /* Clear channel list, data fifo and both stop bits */
3216 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3218 ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO
| ME4000_AI_CTRL_BIT_DATA_FIFO
|
3219 ME4000_AI_CTRL_BIT_STOP
| ME4000_AI_CTRL_BIT_IMMEDIATE_STOP
);
3220 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3222 /* Enable channel list and data fifo */
3223 tmp
|= ME4000_AI_CTRL_BIT_CHANNEL_FIFO
| ME4000_AI_CTRL_BIT_DATA_FIFO
;
3224 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3226 /* Generate channel list entry */
3227 me4000_outl(cmd
.channel
| cmd
.range
| cmd
.
3228 mode
| ME4000_AI_LIST_LAST_ENTRY
,
3229 ai_context
->channel_list_reg
);
3231 /* Set the timer to maximum */
3232 me4000_outl(66, ai_context
->chan_timer_reg
);
3233 me4000_outl(66, ai_context
->chan_pre_timer_reg
);
3235 if (tmp
& ME4000_AI_CTRL_BIT_EX_TRIG
) {
3238 (me4000_inl(ai_context
->status_reg
) &
3239 ME4000_AI_STATUS_BIT_EF_DATA
)) {
3240 interruptible_sleep_on_timeout(&queue
, 1);
3241 if (signal_pending(current
)) {
3243 "ME4000:me4000_ai_single():Wait on start of state machine interrupted\n");
3246 /* 2.6 has different definitions for HZ in user and kernel space */
3247 if (((jiffies
- jiffy
) > (cmd
.timeout
* HZ
/ USER_HZ
)) && cmd
.timeout
) {
3249 "ME4000:me4000_ai_single():Timeout reached\n");
3254 /* Start conversion */
3255 me4000_inl(ai_context
->start_reg
);
3257 /* Wait until ready */
3260 (me4000_inl(ai_context
->status_reg
) &
3261 ME4000_AI_STATUS_BIT_EF_DATA
)) {
3263 "ME4000:me4000_ai_single():Value not available after wait\n");
3268 /* Read value from data fifo */
3269 cmd
.value
= me4000_inl(ai_context
->data_reg
) & 0xFFFF;
3271 /* Copy result back to user */
3272 err
= copy_to_user(arg
, &cmd
, sizeof(struct me4000_ai_single
));
3275 "ME4000:me4000_ai_single():Can't copy to user space\n");
3282 static int me4000_ai_ioctl_sw(struct inode
*inode_p
, struct file
*file_p
,
3283 unsigned int service
, unsigned long arg
)
3285 struct me4000_ai_context
*ai_context
;
3287 CALL_PDEBUG("me4000_ai_ioctl_sw() is executed\n");
3289 ai_context
= file_p
->private_data
;
3291 if (_IOC_TYPE(service
) != ME4000_MAGIC
) {
3292 printk(KERN_ERR
"me4000_ai_ioctl_sw():Wrong magic number\n");
3295 if (_IOC_NR(service
) > ME4000_IOCTL_MAXNR
) {
3297 "me4000_ai_ioctl_sw():Service number to high\n");
3302 case ME4000_AI_SC_SETUP
:
3303 return me4000_ai_sc_setup((struct me4000_ai_sc
*)arg
,
3305 case ME4000_AI_CONFIG
:
3306 return me4000_ai_config((struct me4000_ai_config
*)arg
,
3308 case ME4000_AI_START
:
3309 return me4000_ai_start(ai_context
);
3310 case ME4000_AI_STOP
:
3311 return me4000_ai_stop(ai_context
);
3312 case ME4000_AI_IMMEDIATE_STOP
:
3313 return me4000_ai_immediate_stop(ai_context
);
3314 case ME4000_AI_FSM_STATE
:
3315 return me4000_ai_fsm_state((int *)arg
, ai_context
);
3316 case ME4000_GET_USER_INFO
:
3317 return me4000_get_user_info((struct me4000_user_info
*)arg
,
3318 ai_context
->board_info
);
3319 case ME4000_AI_EEPROM_READ
:
3320 return me4000_eeprom_read((struct me4000_eeprom
*)arg
,
3322 case ME4000_AI_EEPROM_WRITE
:
3323 return me4000_eeprom_write((struct me4000_eeprom
*)arg
,
3325 case ME4000_AI_GET_COUNT_BUFFER
:
3326 return me4000_ai_get_count_buffer((unsigned long *)arg
,
3330 "%s:Invalid service number %d\n", __func__
, service
);
3336 static int me4000_ai_ioctl_ext(struct inode
*inode_p
, struct file
*file_p
,
3337 unsigned int service
, unsigned long arg
)
3339 struct me4000_ai_context
*ai_context
;
3341 CALL_PDEBUG("me4000_ai_ioctl_ext() is executed\n");
3343 ai_context
= file_p
->private_data
;
3345 if (_IOC_TYPE(service
) != ME4000_MAGIC
) {
3346 printk(KERN_ERR
"me4000_ai_ioctl_ext():Wrong magic number\n");
3349 if (_IOC_NR(service
) > ME4000_IOCTL_MAXNR
) {
3351 "me4000_ai_ioctl_ext():Service number to high\n");
3356 case ME4000_AI_SC_SETUP
:
3357 return me4000_ai_sc_setup((struct me4000_ai_sc
*)arg
,
3359 case ME4000_AI_CONFIG
:
3360 return me4000_ai_config((struct me4000_ai_config
*)arg
,
3362 case ME4000_AI_START
:
3363 return me4000_ai_start_ex((unsigned long *)arg
, ai_context
);
3364 case ME4000_AI_STOP
:
3365 return me4000_ai_stop(ai_context
);
3366 case ME4000_AI_IMMEDIATE_STOP
:
3367 return me4000_ai_immediate_stop(ai_context
);
3368 case ME4000_AI_EX_TRIG_ENABLE
:
3369 return me4000_ai_ex_trig_enable(ai_context
);
3370 case ME4000_AI_EX_TRIG_DISABLE
:
3371 return me4000_ai_ex_trig_disable(ai_context
);
3372 case ME4000_AI_EX_TRIG_SETUP
:
3373 return me4000_ai_ex_trig_setup((struct me4000_ai_trigger
*)arg
,
3375 case ME4000_AI_FSM_STATE
:
3376 return me4000_ai_fsm_state((int *)arg
, ai_context
);
3377 case ME4000_GET_USER_INFO
:
3378 return me4000_get_user_info((struct me4000_user_info
*)arg
,
3379 ai_context
->board_info
);
3380 case ME4000_AI_GET_COUNT_BUFFER
:
3381 return me4000_ai_get_count_buffer((unsigned long *)arg
,
3385 "%s:Invalid service number %d\n", __func__
, service
);
3391 static int me4000_ai_fasync(int fd
, struct file
*file_p
, int mode
)
3393 struct me4000_ai_context
*ai_context
;
3395 CALL_PDEBUG("me4000_ao_fasync_cont() is executed\n");
3397 ai_context
= file_p
->private_data
;
3398 return fasync_helper(fd
, file_p
, mode
, &ai_context
->fasync_p
);
3401 static int me4000_ai_config(struct me4000_ai_config
*arg
,
3402 struct me4000_ai_context
*ai_context
)
3404 struct me4000_ai_config cmd
;
3409 wait_queue_head_t queue
;
3413 CALL_PDEBUG("me4000_ai_config() is executed\n");
3415 init_waitqueue_head(&queue
);
3417 /* Check if conversion is stopped */
3418 if (inl(ai_context
->ctrl_reg
) & ME4000_AI_STATUS_BIT_FSM
) {
3420 "ME4000:me4000_ai_config():Conversion is not stopped\n");
3425 /* Copy data from user */
3426 err
= copy_from_user(&cmd
, arg
, sizeof(struct me4000_ai_config
));
3429 "ME4000:me4000_ai_config():Can't copy from user space\n");
3435 ("me4000_ai_config():chan = %ld, pre_chan = %ld, scan_low = %ld, scan_high = %ld, count = %ld\n",
3436 cmd
.timer
.chan
, cmd
.timer
.pre_chan
, cmd
.timer
.scan_low
,
3437 cmd
.timer
.scan_high
, cmd
.channel_list
.count
);
3439 /* Check whether sample and hold is available for this board */
3441 if (!ai_context
->board_info
->board_p
->ai
.sh_count
) {
3443 "ME4000:me4000_ai_config():Sample and Hold is not available for this board\n");
3449 /* Check the channel list size */
3450 if (cmd
.channel_list
.count
> ME4000_AI_CHANNEL_LIST_COUNT
) {
3452 "me4000_ai_config():Channel list is to large\n");
3457 /* Copy channel list from user */
3458 list
= kmalloc(sizeof(u32
) * cmd
.channel_list
.count
, GFP_KERNEL
);
3461 "ME4000:me4000_ai_config():Can't get memory for channel list\n");
3466 copy_from_user(list
, cmd
.channel_list
.list
,
3467 sizeof(u32
) * cmd
.channel_list
.count
);
3470 "ME4000:me4000_ai_config():Can't copy from user space\n");
3475 /* Check if last entry bit is set */
3476 if (!(list
[cmd
.channel_list
.count
- 1] & ME4000_AI_LIST_LAST_ENTRY
)) {
3478 "me4000_ai_config():Last entry bit is not set\n");
3479 list
[cmd
.channel_list
.count
- 1] |= ME4000_AI_LIST_LAST_ENTRY
;
3482 /* Check whether mode is equal for all entries */
3483 mode
= list
[0] & 0x20;
3484 for (i
= 0; i
< cmd
.channel_list
.count
; i
++) {
3485 if ((list
[i
] & 0x20) != mode
) {
3487 "ME4000:me4000_ai_config():Mode is not equal for all entries\n");
3493 /* Check whether channels are available for this mode */
3494 if (mode
== ME4000_AI_LIST_INPUT_SINGLE_ENDED
) {
3495 for (i
= 0; i
< cmd
.channel_list
.count
; i
++) {
3496 if ((list
[i
] & 0x1F) >=
3497 ai_context
->board_info
->board_p
->ai
.count
) {
3499 "ME4000:me4000_ai_config():Channel is not available for single ended\n");
3504 } else if (mode
== ME4000_AI_LIST_INPUT_DIFFERENTIAL
) {
3505 for (i
= 0; i
< cmd
.channel_list
.count
; i
++) {
3506 if ((list
[i
] & 0x1F) >=
3507 ai_context
->board_info
->board_p
->ai
.diff_count
) {
3509 "ME4000:me4000_ai_config():Channel is not available for differential\n");
3516 /* Check if bipolar is set for all entries when in differential mode */
3517 if (mode
== ME4000_AI_LIST_INPUT_DIFFERENTIAL
) {
3518 for (i
= 0; i
< cmd
.channel_list
.count
; i
++) {
3519 if ((list
[i
] & 0xC0) != ME4000_AI_LIST_RANGE_BIPOLAR_10
3520 && (list
[i
] & 0xC0) !=
3521 ME4000_AI_LIST_RANGE_BIPOLAR_2_5
) {
3523 "ME4000:me4000_ai_config():Bipolar is not selected in differential mode\n");
3530 if (ai_context
->mode
!= ME4000_AI_ACQ_MODE_EXT_SINGLE_VALUE
) {
3531 /* Check for minimum channel divisor */
3532 if (cmd
.timer
.chan
< ME4000_AI_MIN_TICKS
) {
3534 "ME4000:me4000_ai_config():Channel timer divisor is to low\n");
3539 /* Check if minimum channel divisor is adjusted when sample and hold is activated */
3540 if ((cmd
.sh
) && (cmd
.timer
.chan
!= ME4000_AI_MIN_TICKS
)) {
3542 "ME4000:me4000_ai_config():Channel timer divisor must be at minimum when sample and hold is activated\n");
3547 /* Check for minimum channel pre divisor */
3548 if (cmd
.timer
.pre_chan
< ME4000_AI_MIN_TICKS
) {
3550 "ME4000:me4000_ai_config():Channel pre timer divisor is to low\n");
3555 /* Write the channel timers */
3556 me4000_outl(cmd
.timer
.chan
- 1, ai_context
->chan_timer_reg
);
3557 me4000_outl(cmd
.timer
.pre_chan
- 1,
3558 ai_context
->chan_pre_timer_reg
);
3560 /* Save the timer values in the board context */
3561 ai_context
->chan_timer
= cmd
.timer
.chan
;
3562 ai_context
->chan_pre_timer
= cmd
.timer
.pre_chan
;
3564 if (ai_context
->mode
!= ME4000_AI_ACQ_MODE_EXT_SINGLE_CHANLIST
) {
3565 /* Check for scan timer divisor */
3567 (u64
) cmd
.timer
.scan_low
| ((u64
) cmd
.timer
.
3571 cmd
.channel_list
.count
* cmd
.timer
.chan
+
3574 "ME4000:me4000_ai_config():Scan timer divisor is to low\n");
3580 /* Write the scan timers */
3583 tmp
= (u32
) (scan
& 0xFFFFFFFF);
3585 ai_context
->scan_timer_low_reg
);
3586 tmp
= (u32
) ((scan
>> 32) & 0xFFFFFFFF);
3588 ai_context
->scan_timer_high_reg
);
3591 scan
- (cmd
.timer
.chan
- 1) +
3592 (cmd
.timer
.pre_chan
- 1);
3593 tmp
= (u32
) (scan
& 0xFFFFFFFF);
3595 ai_context
->scan_pre_timer_low_reg
);
3596 tmp
= (u32
) ((scan
>> 32) & 0xFFFFFFFF);
3599 scan_pre_timer_high_reg
);
3602 ai_context
->scan_timer_low_reg
);
3604 ai_context
->scan_timer_high_reg
);
3607 ai_context
->scan_pre_timer_low_reg
);
3610 scan_pre_timer_high_reg
);
3613 ai_context
->scan_timer_low
= cmd
.timer
.scan_low
;
3614 ai_context
->scan_timer_high
= cmd
.timer
.scan_high
;
3618 /* Clear the channel list */
3619 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3620 tmp
&= ~ME4000_AI_CTRL_BIT_CHANNEL_FIFO
;
3621 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3622 tmp
|= ME4000_AI_CTRL_BIT_CHANNEL_FIFO
;
3623 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3625 /* Write the channel list */
3626 for (i
= 0; i
< cmd
.channel_list
.count
; i
++)
3627 me4000_outl(list
[i
], ai_context
->channel_list_reg
);
3629 /* Setup sample and hold */
3631 tmp
|= ME4000_AI_CTRL_BIT_SAMPLE_HOLD
;
3632 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3634 tmp
&= ~ME4000_AI_CTRL_BIT_SAMPLE_HOLD
;
3635 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3638 /* Save the channel list size in the board context */
3639 ai_context
->channel_list_count
= cmd
.channel_list
.count
;
3647 /* Reset the timers */
3648 ai_context
->chan_timer
= 66;
3649 ai_context
->chan_pre_timer
= 66;
3650 ai_context
->scan_timer_low
= 0;
3651 ai_context
->scan_timer_high
= 0;
3653 me4000_outl(65, ai_context
->chan_timer_reg
);
3654 me4000_outl(65, ai_context
->chan_pre_timer_reg
);
3655 me4000_outl(0, ai_context
->scan_timer_high_reg
);
3656 me4000_outl(0, ai_context
->scan_timer_low_reg
);
3657 me4000_outl(0, ai_context
->scan_pre_timer_high_reg
);
3658 me4000_outl(0, ai_context
->scan_pre_timer_low_reg
);
3660 ai_context
->channel_list_count
= 0;
3662 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3664 ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO
| ME4000_AI_CTRL_BIT_SAMPLE_HOLD
);
3672 static int ai_common_start(struct me4000_ai_context
*ai_context
)
3675 CALL_PDEBUG("ai_common_start() is executed\n");
3677 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3679 /* Check if conversion is stopped */
3680 if (tmp
& ME4000_AI_STATUS_BIT_FSM
) {
3682 "ME4000:ai_common_start():Conversion is not stopped\n");
3686 /* Clear data fifo, disable all interrupts, clear sample counter reload */
3687 tmp
&= ~(ME4000_AI_CTRL_BIT_DATA_FIFO
| ME4000_AI_CTRL_BIT_LE_IRQ
|
3688 ME4000_AI_CTRL_BIT_HF_IRQ
| ME4000_AI_CTRL_BIT_SC_IRQ
|
3689 ME4000_AI_CTRL_BIT_SC_RELOAD
);
3691 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3693 /* Clear circular buffer */
3694 ai_context
->circ_buf
.head
= 0;
3695 ai_context
->circ_buf
.tail
= 0;
3697 /* Enable data fifo */
3698 tmp
|= ME4000_AI_CTRL_BIT_DATA_FIFO
;
3700 /* Determine interrupt setup */
3701 if (ai_context
->sample_counter
&& !ai_context
->sample_counter_reload
) {
3702 /* Enable Half Full Interrupt and Sample Counter Interrupt */
3703 tmp
|= ME4000_AI_CTRL_BIT_SC_IRQ
| ME4000_AI_CTRL_BIT_HF_IRQ
;
3704 } else if (ai_context
->sample_counter
3705 && ai_context
->sample_counter_reload
) {
3706 if (ai_context
->sample_counter
<= ME4000_AI_FIFO_COUNT
/ 2) {
3707 /* Enable only Sample Counter Interrupt */
3709 ME4000_AI_CTRL_BIT_SC_IRQ
|
3710 ME4000_AI_CTRL_BIT_SC_RELOAD
;
3712 /* Enable Half Full Interrupt and Sample Counter Interrupt */
3714 ME4000_AI_CTRL_BIT_SC_IRQ
|
3715 ME4000_AI_CTRL_BIT_HF_IRQ
|
3716 ME4000_AI_CTRL_BIT_SC_RELOAD
;
3719 /* Enable only Half Full Interrupt */
3720 tmp
|= ME4000_AI_CTRL_BIT_HF_IRQ
;
3723 /* Clear the stop bits */
3724 tmp
&= ~(ME4000_AI_CTRL_BIT_STOP
| ME4000_AI_CTRL_BIT_IMMEDIATE_STOP
);
3726 /* Write setup to hardware */
3727 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3729 /* Write sample counter */
3730 me4000_outl(ai_context
->sample_counter
, ai_context
->sample_counter_reg
);
3735 static int me4000_ai_start(struct me4000_ai_context
*ai_context
)
3738 CALL_PDEBUG("me4000_ai_start() is executed\n");
3740 /* Prepare Hardware */
3741 err
= ai_common_start(ai_context
);
3745 /* Start conversion by dummy read */
3746 me4000_inl(ai_context
->start_reg
);
3751 static int me4000_ai_start_ex(unsigned long *arg
,
3752 struct me4000_ai_context
*ai_context
)
3755 wait_queue_head_t queue
;
3757 unsigned long timeout
;
3759 CALL_PDEBUG("me4000_ai_start_ex() is executed\n");
3761 if (get_user(timeout
, arg
)) {
3763 "me4000_ai_start_ex():Cannot copy data from user\n");
3767 init_waitqueue_head(&queue
);
3769 /* Prepare Hardware */
3770 err
= ai_common_start(ai_context
);
3776 while (!(inl(ai_context
->status_reg
) & ME4000_AI_STATUS_BIT_FSM
)) {
3777 interruptible_sleep_on_timeout(&queue
, 1);
3778 if (signal_pending(current
)) {
3780 "ME4000:me4000_ai_start_ex():Wait on start of state machine interrupted\n");
3783 /* 2.6 has different definitions for HZ in user and kernel space */
3784 if ((jiffies
- ref
) > (timeout
* HZ
/ USER_HZ
)) {
3786 "ME4000:me4000_ai_start_ex():Timeout reached\n");
3791 while (!(inl(ai_context
->status_reg
) & ME4000_AI_STATUS_BIT_FSM
)) {
3792 interruptible_sleep_on_timeout(&queue
, 1);
3793 if (signal_pending(current
)) {
3795 "ME4000:me4000_ai_start_ex():Wait on start of state machine interrupted\n");
3804 static int me4000_ai_stop(struct me4000_ai_context
*ai_context
)
3806 wait_queue_head_t queue
;
3808 unsigned long flags
;
3810 CALL_PDEBUG("me4000_ai_stop() is executed\n");
3812 init_waitqueue_head(&queue
);
3814 /* Disable irqs and clear data fifo */
3815 spin_lock_irqsave(&ai_context
->int_lock
, flags
);
3816 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3818 ~(ME4000_AI_CTRL_BIT_HF_IRQ
| ME4000_AI_CTRL_BIT_SC_IRQ
|
3819 ME4000_AI_CTRL_BIT_DATA_FIFO
);
3820 /* Stop conversion of the state machine */
3821 tmp
|= ME4000_AI_CTRL_BIT_STOP
;
3822 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3823 spin_unlock_irqrestore(&ai_context
->int_lock
, flags
);
3825 /* Clear circular buffer */
3826 ai_context
->circ_buf
.head
= 0;
3827 ai_context
->circ_buf
.tail
= 0;
3829 while (inl(ai_context
->status_reg
) & ME4000_AI_STATUS_BIT_FSM
) {
3830 interruptible_sleep_on_timeout(&queue
, 1);
3831 if (signal_pending(current
)) {
3833 "ME4000:me4000_ai_stop():Wait on state machine after stop interrupted\n");
3841 static int me4000_ai_immediate_stop(struct me4000_ai_context
*ai_context
)
3843 wait_queue_head_t queue
;
3845 unsigned long flags
;
3847 CALL_PDEBUG("me4000_ai_stop() is executed\n");
3849 init_waitqueue_head(&queue
);
3851 /* Disable irqs and clear data fifo */
3852 spin_lock_irqsave(&ai_context
->int_lock
, flags
);
3853 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3855 ~(ME4000_AI_CTRL_BIT_HF_IRQ
| ME4000_AI_CTRL_BIT_SC_IRQ
|
3856 ME4000_AI_CTRL_BIT_DATA_FIFO
);
3857 /* Stop conversion of the state machine */
3858 tmp
|= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP
;
3859 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3860 spin_unlock_irqrestore(&ai_context
->int_lock
, flags
);
3862 /* Clear circular buffer */
3863 ai_context
->circ_buf
.head
= 0;
3864 ai_context
->circ_buf
.tail
= 0;
3866 while (inl(ai_context
->status_reg
) & ME4000_AI_STATUS_BIT_FSM
) {
3867 interruptible_sleep_on_timeout(&queue
, 1);
3868 if (signal_pending(current
)) {
3870 "ME4000:me4000_ai_stop():Wait on state machine after stop interrupted\n");
3878 static int me4000_ai_ex_trig_enable(struct me4000_ai_context
*ai_context
)
3881 unsigned long flags
;
3883 CALL_PDEBUG("me4000_ai_ex_trig_enable() is executed\n");
3885 spin_lock_irqsave(&ai_context
->int_lock
, flags
);
3886 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3887 tmp
|= ME4000_AI_CTRL_BIT_EX_TRIG
;
3888 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3889 spin_unlock_irqrestore(&ai_context
->int_lock
, flags
);
3894 static int me4000_ai_ex_trig_disable(struct me4000_ai_context
*ai_context
)
3897 unsigned long flags
;
3899 CALL_PDEBUG("me4000_ai_ex_trig_disable() is executed\n");
3901 spin_lock_irqsave(&ai_context
->int_lock
, flags
);
3902 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3903 tmp
&= ~ME4000_AI_CTRL_BIT_EX_TRIG
;
3904 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3905 spin_unlock_irqrestore(&ai_context
->int_lock
, flags
);
3910 static int me4000_ai_ex_trig_setup(struct me4000_ai_trigger
*arg
,
3911 struct me4000_ai_context
*ai_context
)
3913 struct me4000_ai_trigger cmd
;
3916 unsigned long flags
;
3918 CALL_PDEBUG("me4000_ai_ex_trig_setup() is executed\n");
3920 /* Copy data from user */
3921 err
= copy_from_user(&cmd
, arg
, sizeof(struct me4000_ai_trigger
));
3924 "ME4000:me4000_ai_ex_trig_setup():Can't copy from user space\n");
3928 spin_lock_irqsave(&ai_context
->int_lock
, flags
);
3929 tmp
= me4000_inl(ai_context
->ctrl_reg
);
3931 if (cmd
.mode
== ME4000_AI_TRIGGER_EXT_DIGITAL
) {
3932 tmp
&= ~ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG
;
3933 } else if (cmd
.mode
== ME4000_AI_TRIGGER_EXT_ANALOG
) {
3934 if (!ai_context
->board_info
->board_p
->ai
.ex_trig_analog
) {
3936 "ME4000:me4000_ai_ex_trig_setup():No analog trigger available\n");
3939 tmp
|= ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG
;
3941 spin_unlock_irqrestore(&ai_context
->int_lock
, flags
);
3943 "ME4000:me4000_ai_ex_trig_setup():Invalid trigger mode specified\n");
3947 if (cmd
.edge
== ME4000_AI_TRIGGER_EXT_EDGE_RISING
) {
3949 ~(ME4000_AI_CTRL_BIT_EX_TRIG_BOTH
|
3950 ME4000_AI_CTRL_BIT_EX_TRIG_FALLING
);
3951 } else if (cmd
.edge
== ME4000_AI_TRIGGER_EXT_EDGE_FALLING
) {
3952 tmp
|= ME4000_AI_CTRL_BIT_EX_TRIG_FALLING
;
3953 tmp
&= ~ME4000_AI_CTRL_BIT_EX_TRIG_BOTH
;
3954 } else if (cmd
.edge
== ME4000_AI_TRIGGER_EXT_EDGE_BOTH
) {
3956 ME4000_AI_CTRL_BIT_EX_TRIG_BOTH
|
3957 ME4000_AI_CTRL_BIT_EX_TRIG_FALLING
;
3959 spin_unlock_irqrestore(&ai_context
->int_lock
, flags
);
3961 "ME4000:me4000_ai_ex_trig_setup():Invalid trigger edge specified\n");
3965 me4000_outl(tmp
, ai_context
->ctrl_reg
);
3966 spin_unlock_irqrestore(&ai_context
->int_lock
, flags
);
3970 static int me4000_ai_sc_setup(struct me4000_ai_sc
*arg
,
3971 struct me4000_ai_context
*ai_context
)
3973 struct me4000_ai_sc cmd
;
3976 CALL_PDEBUG("me4000_ai_sc_setup() is executed\n");
3978 /* Copy data from user */
3979 err
= copy_from_user(&cmd
, arg
, sizeof(struct me4000_ai_sc
));
3982 "ME4000:me4000_ai_sc_setup():Can't copy from user space\n");
3986 ai_context
->sample_counter
= cmd
.value
;
3987 ai_context
->sample_counter_reload
= cmd
.reload
;
3992 static ssize_t
me4000_ai_read(struct file
*filep
, char *buff
, size_t cnt
,
3995 struct me4000_ai_context
*ai_context
= filep
->private_data
;
3996 s16
*buffer
= (s16
*) buff
;
3997 size_t count
= cnt
/ 2;
3998 unsigned long flags
;
4005 CALL_PDEBUG("me4000_ai_read() is executed\n");
4007 init_waitqueue_entry(&wait
, current
);
4011 PDEBUG("me4000_ai_read():Count is 0\n");
4016 if (filep
->f_flags
& O_NONBLOCK
) {
4017 c
= me4000_values_to_end(ai_context
->circ_buf
,
4018 ME4000_AI_BUFFER_COUNT
);
4021 ("me4000_ai_read():Returning from nonblocking read\n");
4025 /* Check if conversion is still running */
4027 (me4000_inl(ai_context
->status_reg
) &
4028 ME4000_AI_STATUS_BIT_FSM
)) {
4030 "ME4000:me4000_ai_read():Conversion interrupted\n");
4034 wait_event_interruptible(ai_context
->wait_queue
,
4035 (me4000_values_to_end
4036 (ai_context
->circ_buf
,
4037 ME4000_AI_BUFFER_COUNT
)));
4038 if (signal_pending(current
)) {
4040 "ME4000:me4000_ai_read():Wait on values interrupted from signal\n");
4045 /* Only read count values or as much as available */
4046 c
= me4000_values_to_end(ai_context
->circ_buf
,
4047 ME4000_AI_BUFFER_COUNT
);
4048 PDEBUG("me4000_ai_read():%d values to end\n", c
);
4052 PDEBUG("me4000_ai_read():Copy %d values to user space\n", c
);
4054 k
-= copy_to_user(buffer
,
4055 ai_context
->circ_buf
.buf
+
4056 ai_context
->circ_buf
.tail
, k
);
4060 "ME4000:me4000_ai_read():Cannot copy new values to user\n");
4064 ai_context
->circ_buf
.tail
=
4065 (ai_context
->circ_buf
.tail
+ c
) & (ME4000_AI_BUFFER_COUNT
-
4071 spin_lock_irqsave(&ai_context
->int_lock
, flags
);
4072 if (me4000_buf_space
4073 (ai_context
->circ_buf
, ME4000_AI_BUFFER_COUNT
)) {
4074 tmp
= me4000_inl(ai_context
->ctrl_reg
);
4076 /* Determine interrupt setup */
4077 if (ai_context
->sample_counter
4078 && !ai_context
->sample_counter_reload
) {
4079 /* Enable Half Full Interrupt and Sample Counter Interrupt */
4081 ME4000_AI_CTRL_BIT_SC_IRQ
|
4082 ME4000_AI_CTRL_BIT_HF_IRQ
;
4083 } else if (ai_context
->sample_counter
4084 && ai_context
->sample_counter_reload
) {
4085 if (ai_context
->sample_counter
<
4086 ME4000_AI_FIFO_COUNT
/ 2) {
4087 /* Enable only Sample Counter Interrupt */
4088 tmp
|= ME4000_AI_CTRL_BIT_SC_IRQ
;
4090 /* Enable Half Full Interrupt and Sample Counter Interrupt */
4092 ME4000_AI_CTRL_BIT_SC_IRQ
|
4093 ME4000_AI_CTRL_BIT_HF_IRQ
;
4096 /* Enable only Half Full Interrupt */
4097 tmp
|= ME4000_AI_CTRL_BIT_HF_IRQ
;
4100 me4000_outl(tmp
, ai_context
->ctrl_reg
);
4102 spin_unlock_irqrestore(&ai_context
->int_lock
, flags
);
4105 /* Check if conversion is still running */
4106 if (!(me4000_inl(ai_context
->status_reg
) & ME4000_AI_STATUS_BIT_FSM
)) {
4108 "ME4000:me4000_ai_read():Conversion not running after complete read\n");
4112 if (filep
->f_flags
& O_NONBLOCK
)
4113 return (k
== 0) ? -EAGAIN
: 2 * ret
;
4115 CALL_PDEBUG("me4000_ai_read() is leaved\n");
4119 static unsigned int me4000_ai_poll(struct file
*file_p
, poll_table
*wait
)
4121 struct me4000_ai_context
*ai_context
;
4122 unsigned long mask
= 0;
4124 CALL_PDEBUG("me4000_ai_poll() is executed\n");
4126 ai_context
= file_p
->private_data
;
4128 /* Register wait queue */
4129 poll_wait(file_p
, &ai_context
->wait_queue
, wait
);
4131 /* Get available values */
4132 if (me4000_values_to_end(ai_context
->circ_buf
, ME4000_AI_BUFFER_COUNT
))
4133 mask
|= POLLIN
| POLLRDNORM
;
4135 PDEBUG("me4000_ai_poll():Return mask %lX\n", mask
);
4140 static int me4000_ai_offset_enable(struct me4000_ai_context
*ai_context
)
4144 CALL_PDEBUG("me4000_ai_offset_enable() is executed\n");
4146 tmp
= me4000_inl(ai_context
->ctrl_reg
);
4147 tmp
|= ME4000_AI_CTRL_BIT_OFFSET
;
4148 me4000_outl(tmp
, ai_context
->ctrl_reg
);
4153 static int me4000_ai_offset_disable(struct me4000_ai_context
*ai_context
)
4157 CALL_PDEBUG("me4000_ai_offset_disable() is executed\n");
4159 tmp
= me4000_inl(ai_context
->ctrl_reg
);
4160 tmp
&= ~ME4000_AI_CTRL_BIT_OFFSET
;
4161 me4000_outl(tmp
, ai_context
->ctrl_reg
);
4166 static int me4000_ai_fullscale_enable(struct me4000_ai_context
*ai_context
)
4170 CALL_PDEBUG("me4000_ai_fullscale_enable() is executed\n");
4172 tmp
= me4000_inl(ai_context
->ctrl_reg
);
4173 tmp
|= ME4000_AI_CTRL_BIT_FULLSCALE
;
4174 me4000_outl(tmp
, ai_context
->ctrl_reg
);
4179 static int me4000_ai_fullscale_disable(struct me4000_ai_context
*ai_context
)
4183 CALL_PDEBUG("me4000_ai_fullscale_disable() is executed\n");
4185 tmp
= me4000_inl(ai_context
->ctrl_reg
);
4186 tmp
&= ~ME4000_AI_CTRL_BIT_FULLSCALE
;
4187 me4000_outl(tmp
, ai_context
->ctrl_reg
);
4192 static int me4000_ai_fsm_state(int *arg
, struct me4000_ai_context
*ai_context
)
4196 CALL_PDEBUG("me4000_ai_fsm_state() is executed\n");
4199 (me4000_inl(ai_context
->status_reg
) & ME4000_AI_STATUS_BIT_FSM
) ? 1
4202 if (put_user(tmp
, arg
)) {
4203 printk(KERN_ERR
"me4000_ai_fsm_state():Cannot copy to user\n");
4210 static int me4000_ai_get_count_buffer(unsigned long *arg
,
4211 struct me4000_ai_context
*ai_context
)
4216 c
= me4000_buf_count(ai_context
->circ_buf
, ME4000_AI_BUFFER_COUNT
);
4218 err
= copy_to_user(arg
, &c
, sizeof(unsigned long));
4221 "%s:Can't copy to user space\n", __func__
);
4228 /*---------------------------------- EEPROM stuff ---------------------------*/
4230 static int eeprom_write_cmd(struct me4000_ai_context
*ai_context
, unsigned long cmd
,
4234 unsigned long value
;
4236 CALL_PDEBUG("eeprom_write_cmd() is executed\n");
4238 PDEBUG("eeprom_write_cmd():Write command 0x%08lX with length = %d\n",
4241 /* Get the ICR register and clear the related bits */
4242 value
= me4000_inl(ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4243 value
&= ~(PLX_ICR_MASK_EEPROM
);
4244 me4000_outl(value
, ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4246 /* Raise the chip select */
4247 value
|= PLX_ICR_BIT_EEPROM_CHIP_SELECT
;
4248 me4000_outl(value
, ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4249 udelay(EEPROM_DELAY
);
4251 for (i
= 0; i
< length
; i
++) {
4252 if (cmd
& ((0x1 << (length
- 1)) >> i
))
4253 value
|= PLX_ICR_BIT_EEPROM_WRITE
;
4255 value
&= ~PLX_ICR_BIT_EEPROM_WRITE
;
4257 /* Write to EEPROM */
4259 ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4260 udelay(EEPROM_DELAY
);
4262 /* Raising edge of the clock */
4263 value
|= PLX_ICR_BIT_EEPROM_CLOCK_SET
;
4265 ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4266 udelay(EEPROM_DELAY
);
4268 /* Falling edge of the clock */
4269 value
&= ~PLX_ICR_BIT_EEPROM_CLOCK_SET
;
4271 ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4272 udelay(EEPROM_DELAY
);
4275 /* Clear the chip select */
4276 value
&= ~PLX_ICR_BIT_EEPROM_CHIP_SELECT
;
4277 me4000_outl(value
, ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4278 udelay(EEPROM_DELAY
);
4280 /* Wait until hardware is ready for sure */
4286 static unsigned short eeprom_read_cmd(struct me4000_ai_context
*ai_context
,
4287 unsigned long cmd
, int length
)
4290 unsigned long value
;
4291 unsigned short id
= 0;
4293 CALL_PDEBUG("eeprom_read_cmd() is executed\n");
4295 PDEBUG("eeprom_read_cmd():Read command 0x%08lX with length = %d\n", cmd
,
4298 /* Get the ICR register and clear the related bits */
4299 value
= me4000_inl(ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4300 value
&= ~(PLX_ICR_MASK_EEPROM
);
4302 me4000_outl(value
, ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4304 /* Raise the chip select */
4305 value
|= PLX_ICR_BIT_EEPROM_CHIP_SELECT
;
4306 me4000_outl(value
, ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4307 udelay(EEPROM_DELAY
);
4309 /* Write the read command to the eeprom */
4310 for (i
= 0; i
< length
; i
++) {
4311 if (cmd
& ((0x1 << (length
- 1)) >> i
))
4312 value
|= PLX_ICR_BIT_EEPROM_WRITE
;
4314 value
&= ~PLX_ICR_BIT_EEPROM_WRITE
;
4317 ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4318 udelay(EEPROM_DELAY
);
4320 /* Raising edge of the clock */
4321 value
|= PLX_ICR_BIT_EEPROM_CLOCK_SET
;
4323 ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4324 udelay(EEPROM_DELAY
);
4326 /* Falling edge of the clock */
4327 value
&= ~PLX_ICR_BIT_EEPROM_CLOCK_SET
;
4329 ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4330 udelay(EEPROM_DELAY
);
4333 /* Read the value from the eeprom */
4334 for (i
= 0; i
< 16; i
++) {
4335 /* Raising edge of the clock */
4336 value
|= PLX_ICR_BIT_EEPROM_CLOCK_SET
;
4338 ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4339 udelay(EEPROM_DELAY
);
4341 if (me4000_inl(ai_context
->board_info
->plx_regbase
+ PLX_ICR
) &
4342 PLX_ICR_BIT_EEPROM_READ
) {
4343 id
|= (0x8000 >> i
);
4344 PDEBUG("eeprom_read_cmd():OR with 0x%04X\n",
4347 PDEBUG("eeprom_read_cmd():Dont't OR\n");
4350 /* Falling edge of the clock */
4351 value
&= ~PLX_ICR_BIT_EEPROM_CLOCK_SET
;
4353 ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4354 udelay(EEPROM_DELAY
);
4357 /* Clear the chip select */
4358 value
&= ~PLX_ICR_BIT_EEPROM_CHIP_SELECT
;
4359 me4000_outl(value
, ai_context
->board_info
->plx_regbase
+ PLX_ICR
);
4360 udelay(EEPROM_DELAY
);
4365 static int me4000_eeprom_write(struct me4000_eeprom
*arg
,
4366 struct me4000_ai_context
*ai_context
)
4369 struct me4000_eeprom setup
;
4371 unsigned long date_high
;
4372 unsigned long date_low
;
4374 CALL_PDEBUG("me4000_eeprom_write() is executed\n");
4376 err
= copy_from_user(&setup
, arg
, sizeof(setup
));
4379 "ME4000:me4000_eeprom_write():Cannot copy from user\n");
4383 /* Enable writing */
4384 eeprom_write_cmd(ai_context
, ME4000_EEPROM_CMD_WRITE_ENABLE
,
4385 ME4000_EEPROM_CMD_LENGTH_WRITE_ENABLE
);
4387 /* Command for date */
4388 date_high
= (setup
.date
& 0xFFFF0000) >> 16;
4389 date_low
= (setup
.date
& 0x0000FFFF);
4392 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_DATE_HIGH
<<
4393 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4397 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4402 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_DATE_LOW
<<
4403 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4407 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4411 /* Command for unipolar 10V offset */
4413 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_1_UNI_OFFSET
<<
4414 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4419 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4423 /* Command for unipolar 10V fullscale */
4425 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_1_UNI_FULLSCALE
<<
4426 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4431 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4435 /* Command for unipolar 2,5V offset */
4437 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_4_UNI_OFFSET
<<
4438 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4443 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4447 /* Command for unipolar 2,5V fullscale */
4449 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_4_UNI_FULLSCALE
<<
4450 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4455 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4459 /* Command for bipolar 10V offset */
4461 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_1_BI_OFFSET
<<
4462 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4467 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4471 /* Command for bipolar 10V fullscale */
4473 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_1_BI_FULLSCALE
<<
4474 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4479 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4483 /* Command for bipolar 2,5V offset */
4485 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_4_BI_OFFSET
<<
4486 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4491 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4495 /* Command for bipolar 2,5V fullscale */
4497 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_4_BI_FULLSCALE
<<
4498 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4503 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4507 /* Command for differential 10V offset */
4509 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_1_DIFF_OFFSET
<<
4510 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4515 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4519 /* Command for differential 10V fullscale */
4521 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_1_DIFF_FULLSCALE
4522 << ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4527 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4531 /* Command for differential 2,5V offset */
4533 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_4_DIFF_OFFSET
<<
4534 ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4539 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4543 /* Command for differential 2,5V fullscale */
4545 ME4000_EEPROM_CMD_WRITE
| (ME4000_EEPROM_ADR_GAIN_4_DIFF_FULLSCALE
4546 << ME4000_EEPROM_DATA_LENGTH
) | (0xFFFF &
4550 diff_2_5_fullscale
);
4551 err
= eeprom_write_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_WRITE
);
4555 /* Disable writing */
4556 eeprom_write_cmd(ai_context
, ME4000_EEPROM_CMD_WRITE_DISABLE
,
4557 ME4000_EEPROM_CMD_LENGTH_WRITE_DISABLE
);
4562 static int me4000_eeprom_read(struct me4000_eeprom
*arg
,
4563 struct me4000_ai_context
*ai_context
)
4567 struct me4000_eeprom setup
;
4569 CALL_PDEBUG("me4000_eeprom_read() is executed\n");
4571 /* Command for date */
4572 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_DATE_HIGH
;
4574 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4576 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_DATE_LOW
;
4578 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4580 /* Command for unipolar 10V offset */
4581 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_1_UNI_OFFSET
;
4582 setup
.uni_10_offset
=
4583 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4585 /* Command for unipolar 10V fullscale */
4586 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_1_UNI_FULLSCALE
;
4587 setup
.uni_10_fullscale
=
4588 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4590 /* Command for unipolar 2,5V offset */
4591 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_4_UNI_OFFSET
;
4592 setup
.uni_2_5_offset
=
4593 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4595 /* Command for unipolar 2,5V fullscale */
4596 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_4_UNI_FULLSCALE
;
4597 setup
.uni_2_5_fullscale
=
4598 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4600 /* Command for bipolar 10V offset */
4601 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_1_BI_OFFSET
;
4602 setup
.bi_10_offset
=
4603 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4605 /* Command for bipolar 10V fullscale */
4606 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_1_BI_FULLSCALE
;
4607 setup
.bi_10_fullscale
=
4608 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4610 /* Command for bipolar 2,5V offset */
4611 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_4_BI_OFFSET
;
4612 setup
.bi_2_5_offset
=
4613 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4615 /* Command for bipolar 2,5V fullscale */
4616 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_4_BI_FULLSCALE
;
4617 setup
.bi_2_5_fullscale
=
4618 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4620 /* Command for differntial 10V offset */
4621 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_1_DIFF_OFFSET
;
4622 setup
.diff_10_offset
=
4623 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4625 /* Command for differential 10V fullscale */
4626 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_1_DIFF_FULLSCALE
;
4627 setup
.diff_10_fullscale
=
4628 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4630 /* Command for differntial 2,5V offset */
4631 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_4_DIFF_OFFSET
;
4632 setup
.diff_2_5_offset
=
4633 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4635 /* Command for differential 2,5V fullscale */
4636 cmd
= ME4000_EEPROM_CMD_READ
| ME4000_EEPROM_ADR_GAIN_4_DIFF_FULLSCALE
;
4637 setup
.diff_2_5_fullscale
=
4638 eeprom_read_cmd(ai_context
, cmd
, ME4000_EEPROM_CMD_LENGTH_READ
);
4640 err
= copy_to_user(arg
, &setup
, sizeof(setup
));
4643 "ME4000:me4000_eeprom_read():Cannot copy to user\n");
4650 /*------------------------------------ DIO stuff ----------------------------------------------*/
4652 static int me4000_dio_ioctl(struct inode
*inode_p
, struct file
*file_p
,
4653 unsigned int service
, unsigned long arg
)
4655 struct me4000_dio_context
*dio_context
;
4657 CALL_PDEBUG("me4000_dio_ioctl() is executed\n");
4659 dio_context
= file_p
->private_data
;
4661 if (_IOC_TYPE(service
) != ME4000_MAGIC
) {
4662 printk(KERN_ERR
"me4000_dio_ioctl():Wrong magic number\n");
4665 if (_IOC_NR(service
) > ME4000_IOCTL_MAXNR
) {
4666 printk(KERN_ERR
"me4000_dio_ioctl():Service number to high\n");
4671 case ME4000_DIO_CONFIG
:
4672 return me4000_dio_config((struct me4000_dio_config
*)arg
,
4674 case ME4000_DIO_SET_BYTE
:
4675 return me4000_dio_set_byte((struct me4000_dio_byte
*)arg
,
4677 case ME4000_DIO_GET_BYTE
:
4678 return me4000_dio_get_byte((struct me4000_dio_byte
*)arg
,
4680 case ME4000_DIO_RESET
:
4681 return me4000_dio_reset(dio_context
);
4684 "ME4000:me4000_dio_ioctl():Invalid service number %d\n",
4691 static int me4000_dio_config(struct me4000_dio_config
*arg
,
4692 struct me4000_dio_context
*dio_context
)
4694 struct me4000_dio_config cmd
;
4698 CALL_PDEBUG("me4000_dio_config() is executed\n");
4700 /* Copy data from user */
4701 err
= copy_from_user(&cmd
, arg
, sizeof(struct me4000_dio_config
));
4704 "ME4000:me4000_dio_config():Can't copy from user space\n");
4708 /* Check port parameter */
4709 if (cmd
.port
>= dio_context
->dio_count
) {
4711 "ME4000:me4000_dio_config():Port %d is not available\n",
4716 PDEBUG("me4000_dio_config(): port %d, mode %d, function %d\n", cmd
.port
,
4717 cmd
.mode
, cmd
.function
);
4719 if (cmd
.port
== ME4000_DIO_PORT_A
) {
4720 if (cmd
.mode
== ME4000_DIO_PORT_INPUT
) {
4721 /* Check if opto isolated version */
4722 if (!(me4000_inl(dio_context
->dir_reg
) & 0x1)) {
4724 "ME4000:me4000_dio_config():Cannot set to input on opto isolated versions\n");
4728 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4730 ~(ME4000_DIO_CTRL_BIT_MODE_0
|
4731 ME4000_DIO_CTRL_BIT_MODE_1
);
4732 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4733 } else if (cmd
.mode
== ME4000_DIO_PORT_OUTPUT
) {
4734 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4736 ~(ME4000_DIO_CTRL_BIT_MODE_0
|
4737 ME4000_DIO_CTRL_BIT_MODE_1
);
4738 tmp
|= ME4000_DIO_CTRL_BIT_MODE_0
;
4739 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4740 } else if (cmd
.mode
== ME4000_DIO_FIFO_LOW
) {
4741 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4743 ~(ME4000_DIO_CTRL_BIT_MODE_0
|
4744 ME4000_DIO_CTRL_BIT_MODE_1
|
4745 ME4000_DIO_CTRL_BIT_FIFO_HIGH_0
);
4747 ME4000_DIO_CTRL_BIT_MODE_0
|
4748 ME4000_DIO_CTRL_BIT_MODE_1
;
4749 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4750 } else if (cmd
.mode
== ME4000_DIO_FIFO_HIGH
) {
4751 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4753 ME4000_DIO_CTRL_BIT_MODE_0
|
4754 ME4000_DIO_CTRL_BIT_MODE_1
|
4755 ME4000_DIO_CTRL_BIT_FIFO_HIGH_0
;
4756 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4759 "ME4000:me4000_dio_config():Mode %d is not available\n",
4763 } else if (cmd
.port
== ME4000_DIO_PORT_B
) {
4764 if (cmd
.mode
== ME4000_DIO_PORT_INPUT
) {
4765 /* Only do anything when TTL version is installed */
4766 if ((me4000_inl(dio_context
->dir_reg
) & 0x1)) {
4767 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4769 ~(ME4000_DIO_CTRL_BIT_MODE_2
|
4770 ME4000_DIO_CTRL_BIT_MODE_3
);
4771 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4773 } else if (cmd
.mode
== ME4000_DIO_PORT_OUTPUT
) {
4774 /* Check if opto isolated version */
4775 if (!(me4000_inl(dio_context
->dir_reg
) & 0x1)) {
4777 "ME4000:me4000_dio_config():Cannot set to output on opto isolated versions\n");
4781 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4783 ~(ME4000_DIO_CTRL_BIT_MODE_2
|
4784 ME4000_DIO_CTRL_BIT_MODE_3
);
4785 tmp
|= ME4000_DIO_CTRL_BIT_MODE_2
;
4786 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4787 } else if (cmd
.mode
== ME4000_DIO_FIFO_LOW
) {
4788 /* Check if opto isolated version */
4789 if (!(me4000_inl(dio_context
->dir_reg
) & 0x1)) {
4791 "ME4000:me4000_dio_config():Cannot set to FIFO low output on opto isolated versions\n");
4795 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4797 ~(ME4000_DIO_CTRL_BIT_MODE_2
|
4798 ME4000_DIO_CTRL_BIT_MODE_3
|
4799 ME4000_DIO_CTRL_BIT_FIFO_HIGH_1
);
4801 ME4000_DIO_CTRL_BIT_MODE_2
|
4802 ME4000_DIO_CTRL_BIT_MODE_3
;
4803 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4804 } else if (cmd
.mode
== ME4000_DIO_FIFO_HIGH
) {
4805 /* Check if opto isolated version */
4806 if (!(me4000_inl(dio_context
->dir_reg
) & 0x1)) {
4808 "ME4000:me4000_dio_config():Cannot set to FIFO high output on opto isolated versions\n");
4812 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4814 ME4000_DIO_CTRL_BIT_MODE_2
|
4815 ME4000_DIO_CTRL_BIT_MODE_3
|
4816 ME4000_DIO_CTRL_BIT_FIFO_HIGH_1
;
4817 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4820 "ME4000:me4000_dio_config():Mode %d is not available\n",
4824 } else if (cmd
.port
== ME4000_DIO_PORT_C
) {
4825 if (cmd
.mode
== ME4000_DIO_PORT_INPUT
) {
4826 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4828 ~(ME4000_DIO_CTRL_BIT_MODE_4
|
4829 ME4000_DIO_CTRL_BIT_MODE_5
);
4830 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4831 } else if (cmd
.mode
== ME4000_DIO_PORT_OUTPUT
) {
4832 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4834 ~(ME4000_DIO_CTRL_BIT_MODE_4
|
4835 ME4000_DIO_CTRL_BIT_MODE_5
);
4836 tmp
|= ME4000_DIO_CTRL_BIT_MODE_4
;
4837 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4838 } else if (cmd
.mode
== ME4000_DIO_FIFO_LOW
) {
4839 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4841 ~(ME4000_DIO_CTRL_BIT_MODE_4
|
4842 ME4000_DIO_CTRL_BIT_MODE_5
|
4843 ME4000_DIO_CTRL_BIT_FIFO_HIGH_2
);
4845 ME4000_DIO_CTRL_BIT_MODE_4
|
4846 ME4000_DIO_CTRL_BIT_MODE_5
;
4847 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4848 } else if (cmd
.mode
== ME4000_DIO_FIFO_HIGH
) {
4849 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4851 ME4000_DIO_CTRL_BIT_MODE_4
|
4852 ME4000_DIO_CTRL_BIT_MODE_5
|
4853 ME4000_DIO_CTRL_BIT_FIFO_HIGH_2
;
4854 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4857 "ME4000:me4000_dio_config():Mode %d is not available\n",
4861 } else if (cmd
.port
== ME4000_DIO_PORT_D
) {
4862 if (cmd
.mode
== ME4000_DIO_PORT_INPUT
) {
4863 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4865 ~(ME4000_DIO_CTRL_BIT_MODE_6
|
4866 ME4000_DIO_CTRL_BIT_MODE_7
);
4867 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4868 } else if (cmd
.mode
== ME4000_DIO_PORT_OUTPUT
) {
4869 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4871 ~(ME4000_DIO_CTRL_BIT_MODE_6
|
4872 ME4000_DIO_CTRL_BIT_MODE_7
);
4873 tmp
|= ME4000_DIO_CTRL_BIT_MODE_6
;
4874 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4875 } else if (cmd
.mode
== ME4000_DIO_FIFO_LOW
) {
4876 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4878 ~(ME4000_DIO_CTRL_BIT_MODE_6
|
4879 ME4000_DIO_CTRL_BIT_MODE_7
|
4880 ME4000_DIO_CTRL_BIT_FIFO_HIGH_3
);
4882 ME4000_DIO_CTRL_BIT_MODE_6
|
4883 ME4000_DIO_CTRL_BIT_MODE_7
;
4884 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4885 } else if (cmd
.mode
== ME4000_DIO_FIFO_HIGH
) {
4886 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4888 ME4000_DIO_CTRL_BIT_MODE_6
|
4889 ME4000_DIO_CTRL_BIT_MODE_7
|
4890 ME4000_DIO_CTRL_BIT_FIFO_HIGH_3
;
4891 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4894 "ME4000:me4000_dio_config():Mode %d is not available\n",
4900 "ME4000:me4000_dio_config():Port %d is not available\n",
4905 PDEBUG("me4000_dio_config(): port %d, mode %d, function %d\n", cmd
.port
,
4906 cmd
.mode
, cmd
.function
);
4908 if ((cmd
.mode
== ME4000_DIO_FIFO_HIGH
)
4909 || (cmd
.mode
== ME4000_DIO_FIFO_LOW
)) {
4910 tmp
= me4000_inl(dio_context
->ctrl_reg
);
4912 ~(ME4000_DIO_CTRL_BIT_FUNCTION_0
|
4913 ME4000_DIO_CTRL_BIT_FUNCTION_1
);
4914 if (cmd
.function
== ME4000_DIO_FUNCTION_PATTERN
) {
4915 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4916 } else if (cmd
.function
== ME4000_DIO_FUNCTION_DEMUX
) {
4917 tmp
|= ME4000_DIO_CTRL_BIT_FUNCTION_0
;
4918 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4919 } else if (cmd
.function
== ME4000_DIO_FUNCTION_MUX
) {
4920 tmp
|= ME4000_DIO_CTRL_BIT_FUNCTION_1
;
4921 me4000_outl(tmp
, dio_context
->ctrl_reg
);
4924 "ME4000:me4000_dio_config():Invalid port function specified\n");
4932 static int me4000_dio_set_byte(struct me4000_dio_byte
*arg
,
4933 struct me4000_dio_context
*dio_context
)
4935 struct me4000_dio_byte cmd
;
4938 CALL_PDEBUG("me4000_dio_set_byte() is executed\n");
4940 /* Copy data from user */
4941 err
= copy_from_user(&cmd
, arg
, sizeof(struct me4000_dio_byte
));
4944 "ME4000:me4000_dio_set_byte():Can't copy from user space\n");
4948 /* Check port parameter */
4949 if (cmd
.port
>= dio_context
->dio_count
) {
4951 "ME4000:me4000_dio_set_byte():Port %d is not available\n",
4956 if (cmd
.port
== ME4000_DIO_PORT_A
) {
4957 if ((me4000_inl(dio_context
->ctrl_reg
) & 0x3) != 0x1) {
4959 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4963 me4000_outl(cmd
.byte
, dio_context
->port_0_reg
);
4964 } else if (cmd
.port
== ME4000_DIO_PORT_B
) {
4965 if ((me4000_inl(dio_context
->ctrl_reg
) & 0xC) != 0x4) {
4967 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4971 me4000_outl(cmd
.byte
, dio_context
->port_1_reg
);
4972 } else if (cmd
.port
== ME4000_DIO_PORT_C
) {
4973 if ((me4000_inl(dio_context
->ctrl_reg
) & 0x30) != 0x10) {
4975 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4979 me4000_outl(cmd
.byte
, dio_context
->port_2_reg
);
4980 } else if (cmd
.port
== ME4000_DIO_PORT_D
) {
4981 if ((me4000_inl(dio_context
->ctrl_reg
) & 0xC0) != 0x40) {
4983 "ME4000:me4000_dio_set_byte():Port %d is not in output mode\n",
4987 me4000_outl(cmd
.byte
, dio_context
->port_3_reg
);
4990 "ME4000:me4000_dio_set_byte():Port %d is not available\n",
4998 static int me4000_dio_get_byte(struct me4000_dio_byte
*arg
,
4999 struct me4000_dio_context
*dio_context
)
5001 struct me4000_dio_byte cmd
;
5004 CALL_PDEBUG("me4000_dio_get_byte() is executed\n");
5006 /* Copy data from user */
5007 err
= copy_from_user(&cmd
, arg
, sizeof(struct me4000_dio_byte
));
5010 "ME4000:me4000_dio_get_byte():Can't copy from user space\n");
5014 /* Check port parameter */
5015 if (cmd
.port
>= dio_context
->dio_count
) {
5017 "ME4000:me4000_dio_get_byte():Port %d is not available\n",
5022 if (cmd
.port
== ME4000_DIO_PORT_A
) {
5023 cmd
.byte
= me4000_inl(dio_context
->port_0_reg
) & 0xFF;
5024 } else if (cmd
.port
== ME4000_DIO_PORT_B
) {
5025 cmd
.byte
= me4000_inl(dio_context
->port_1_reg
) & 0xFF;
5026 } else if (cmd
.port
== ME4000_DIO_PORT_C
) {
5027 cmd
.byte
= me4000_inl(dio_context
->port_2_reg
) & 0xFF;
5028 } else if (cmd
.port
== ME4000_DIO_PORT_D
) {
5029 cmd
.byte
= me4000_inl(dio_context
->port_3_reg
) & 0xFF;
5032 "ME4000:me4000_dio_get_byte():Port %d is not available\n",
5037 /* Copy result back to user */
5038 err
= copy_to_user(arg
, &cmd
, sizeof(struct me4000_dio_byte
));
5041 "ME4000:me4000_dio_get_byte():Can't copy to user space\n");
5048 static int me4000_dio_reset(struct me4000_dio_context
*dio_context
)
5050 CALL_PDEBUG("me4000_dio_reset() is executed\n");
5052 /* Clear the control register */
5053 me4000_outl(0, dio_context
->ctrl_reg
);
5055 /* Check for opto isolated version */
5056 if (!(me4000_inl(dio_context
->dir_reg
) & 0x1)) {
5057 me4000_outl(0x1, dio_context
->ctrl_reg
);
5058 me4000_outl(0x0, dio_context
->port_0_reg
);
5064 /*------------------------------------ COUNTER STUFF ------------------------------------*/
5066 static int me4000_cnt_ioctl(struct inode
*inode_p
, struct file
*file_p
,
5067 unsigned int service
, unsigned long arg
)
5069 struct me4000_cnt_context
*cnt_context
;
5071 CALL_PDEBUG("me4000_cnt_ioctl() is executed\n");
5073 cnt_context
= file_p
->private_data
;
5075 if (_IOC_TYPE(service
) != ME4000_MAGIC
) {
5076 printk(KERN_ERR
"me4000_dio_ioctl():Wrong magic number\n");
5079 if (_IOC_NR(service
) > ME4000_IOCTL_MAXNR
) {
5080 printk(KERN_ERR
"me4000_dio_ioctl():Service number to high\n");
5085 case ME4000_CNT_READ
:
5086 return me4000_cnt_read((struct me4000_cnt
*)arg
, cnt_context
);
5087 case ME4000_CNT_WRITE
:
5088 return me4000_cnt_write((struct me4000_cnt
*)arg
, cnt_context
);
5089 case ME4000_CNT_CONFIG
:
5090 return me4000_cnt_config((struct me4000_cnt_config
*)arg
,
5092 case ME4000_CNT_RESET
:
5093 return me4000_cnt_reset(cnt_context
);
5096 "ME4000:me4000_dio_ioctl():Invalid service number %d\n",
5103 static int me4000_cnt_config(struct me4000_cnt_config
*arg
,
5104 struct me4000_cnt_context
*cnt_context
)
5106 struct me4000_cnt_config cmd
;
5111 CALL_PDEBUG("me4000_cnt_config() is executed\n");
5113 /* Copy data from user */
5114 err
= copy_from_user(&cmd
, arg
, sizeof(struct me4000_cnt_config
));
5117 "ME4000:me4000_cnt_config():Can't copy from user space\n");
5121 /* Check counter parameter */
5122 switch (cmd
.counter
) {
5123 case ME4000_CNT_COUNTER_0
:
5124 counter
= ME4000_CNT_CTRL_BIT_COUNTER_0
;
5126 case ME4000_CNT_COUNTER_1
:
5127 counter
= ME4000_CNT_CTRL_BIT_COUNTER_1
;
5129 case ME4000_CNT_COUNTER_2
:
5130 counter
= ME4000_CNT_CTRL_BIT_COUNTER_2
;
5134 "ME4000:me4000_cnt_config():Counter %d is not available\n",
5139 /* Check mode parameter */
5141 case ME4000_CNT_MODE_0
:
5142 mode
= ME4000_CNT_CTRL_BIT_MODE_0
;
5144 case ME4000_CNT_MODE_1
:
5145 mode
= ME4000_CNT_CTRL_BIT_MODE_1
;
5147 case ME4000_CNT_MODE_2
:
5148 mode
= ME4000_CNT_CTRL_BIT_MODE_2
;
5150 case ME4000_CNT_MODE_3
:
5151 mode
= ME4000_CNT_CTRL_BIT_MODE_3
;
5153 case ME4000_CNT_MODE_4
:
5154 mode
= ME4000_CNT_CTRL_BIT_MODE_4
;
5156 case ME4000_CNT_MODE_5
:
5157 mode
= ME4000_CNT_CTRL_BIT_MODE_5
;
5161 "ME4000:me4000_cnt_config():Mode %d is not available\n",
5166 /* Write the control word */
5167 me4000_outb((counter
| mode
| 0x30), cnt_context
->ctrl_reg
);
5172 static int me4000_cnt_read(struct me4000_cnt
*arg
,
5173 struct me4000_cnt_context
*cnt_context
)
5175 struct me4000_cnt cmd
;
5179 CALL_PDEBUG("me4000_cnt_read() is executed\n");
5181 /* Copy data from user */
5182 err
= copy_from_user(&cmd
, arg
, sizeof(struct me4000_cnt
));
5185 "ME4000:me4000_cnt_read():Can't copy from user space\n");
5190 switch (cmd
.counter
) {
5191 case ME4000_CNT_COUNTER_0
:
5192 tmp
= me4000_inb(cnt_context
->counter_0_reg
);
5194 tmp
= me4000_inb(cnt_context
->counter_0_reg
);
5195 cmd
.value
|= ((u16
) tmp
) << 8;
5197 case ME4000_CNT_COUNTER_1
:
5198 tmp
= me4000_inb(cnt_context
->counter_1_reg
);
5200 tmp
= me4000_inb(cnt_context
->counter_1_reg
);
5201 cmd
.value
|= ((u16
) tmp
) << 8;
5203 case ME4000_CNT_COUNTER_2
:
5204 tmp
= me4000_inb(cnt_context
->counter_2_reg
);
5206 tmp
= me4000_inb(cnt_context
->counter_2_reg
);
5207 cmd
.value
|= ((u16
) tmp
) << 8;
5211 "ME4000:me4000_cnt_read():Counter %d is not available\n",
5216 /* Copy result back to user */
5217 err
= copy_to_user(arg
, &cmd
, sizeof(struct me4000_cnt
));
5220 "ME4000:me4000_cnt_read():Can't copy to user space\n");
5227 static int me4000_cnt_write(struct me4000_cnt
*arg
,
5228 struct me4000_cnt_context
*cnt_context
)
5230 struct me4000_cnt cmd
;
5234 CALL_PDEBUG("me4000_cnt_write() is executed\n");
5236 /* Copy data from user */
5237 err
= copy_from_user(&cmd
, arg
, sizeof(struct me4000_cnt
));
5240 "ME4000:me4000_cnt_write():Can't copy from user space\n");
5245 switch (cmd
.counter
) {
5246 case ME4000_CNT_COUNTER_0
:
5247 tmp
= cmd
.value
& 0xFF;
5248 me4000_outb(tmp
, cnt_context
->counter_0_reg
);
5249 tmp
= (cmd
.value
>> 8) & 0xFF;
5250 me4000_outb(tmp
, cnt_context
->counter_0_reg
);
5252 case ME4000_CNT_COUNTER_1
:
5253 tmp
= cmd
.value
& 0xFF;
5254 me4000_outb(tmp
, cnt_context
->counter_1_reg
);
5255 tmp
= (cmd
.value
>> 8) & 0xFF;
5256 me4000_outb(tmp
, cnt_context
->counter_1_reg
);
5258 case ME4000_CNT_COUNTER_2
:
5259 tmp
= cmd
.value
& 0xFF;
5260 me4000_outb(tmp
, cnt_context
->counter_2_reg
);
5261 tmp
= (cmd
.value
>> 8) & 0xFF;
5262 me4000_outb(tmp
, cnt_context
->counter_2_reg
);
5266 "ME4000:me4000_cnt_write():Counter %d is not available\n",
5274 static int me4000_cnt_reset(struct me4000_cnt_context
*cnt_context
)
5276 CALL_PDEBUG("me4000_cnt_reset() is executed\n");
5278 /* Set the mode and value for counter 0 */
5279 me4000_outb(0x30, cnt_context
->ctrl_reg
);
5280 me4000_outb(0x00, cnt_context
->counter_0_reg
);
5281 me4000_outb(0x00, cnt_context
->counter_0_reg
);
5283 /* Set the mode and value for counter 1 */
5284 me4000_outb(0x70, cnt_context
->ctrl_reg
);
5285 me4000_outb(0x00, cnt_context
->counter_1_reg
);
5286 me4000_outb(0x00, cnt_context
->counter_1_reg
);
5288 /* Set the mode and value for counter 2 */
5289 me4000_outb(0xB0, cnt_context
->ctrl_reg
);
5290 me4000_outb(0x00, cnt_context
->counter_2_reg
);
5291 me4000_outb(0x00, cnt_context
->counter_2_reg
);
5296 /*------------------------------------ External Interrupt stuff ------------------------------------*/
5298 static int me4000_ext_int_ioctl(struct inode
*inode_p
, struct file
*file_p
,
5299 unsigned int service
, unsigned long arg
)
5301 struct me4000_ext_int_context
*ext_int_context
;
5303 CALL_PDEBUG("me4000_ext_int_ioctl() is executed\n");
5305 ext_int_context
= file_p
->private_data
;
5307 if (_IOC_TYPE(service
) != ME4000_MAGIC
) {
5308 printk(KERN_ERR
"me4000_ext_int_ioctl():Wrong magic number\n");
5311 if (_IOC_NR(service
) > ME4000_IOCTL_MAXNR
) {
5313 "me4000_ext_int_ioctl():Service number to high\n");
5318 case ME4000_EXT_INT_ENABLE
:
5319 return me4000_ext_int_enable(ext_int_context
);
5320 case ME4000_EXT_INT_DISABLE
:
5321 return me4000_ext_int_disable(ext_int_context
);
5322 case ME4000_EXT_INT_COUNT
:
5323 return me4000_ext_int_count((unsigned long *)arg
,
5327 "ME4000:me4000_ext_int_ioctl():Invalid service number %d\n",
5334 static int me4000_ext_int_enable(struct me4000_ext_int_context
*ext_int_context
)
5338 CALL_PDEBUG("me4000_ext_int_enable() is executed\n");
5340 tmp
= me4000_inl(ext_int_context
->ctrl_reg
);
5341 tmp
|= ME4000_AI_CTRL_BIT_EX_IRQ
;
5342 me4000_outl(tmp
, ext_int_context
->ctrl_reg
);
5347 static int me4000_ext_int_disable(struct me4000_ext_int_context
*ext_int_context
)
5351 CALL_PDEBUG("me4000_ext_int_disable() is executed\n");
5353 tmp
= me4000_inl(ext_int_context
->ctrl_reg
);
5354 tmp
&= ~ME4000_AI_CTRL_BIT_EX_IRQ
;
5355 me4000_outl(tmp
, ext_int_context
->ctrl_reg
);
5360 static int me4000_ext_int_count(unsigned long *arg
,
5361 struct me4000_ext_int_context
*ext_int_context
)
5364 CALL_PDEBUG("me4000_ext_int_count() is executed\n");
5366 put_user(ext_int_context
->int_count
, arg
);
5370 /*------------------------------------ General stuff ------------------------------------*/
5372 static int me4000_get_user_info(struct me4000_user_info
*arg
,
5373 struct me4000_info
*board_info
)
5375 struct me4000_user_info user_info
;
5377 CALL_PDEBUG("me4000_get_user_info() is executed\n");
5379 user_info
.board_count
= board_info
->board_count
;
5380 user_info
.plx_regbase
= board_info
->plx_regbase
;
5381 user_info
.plx_regbase_size
= board_info
->plx_regbase_size
;
5382 user_info
.me4000_regbase
= board_info
->me4000_regbase
;
5383 user_info
.me4000_regbase_size
= board_info
->me4000_regbase_size
;
5384 user_info
.serial_no
= board_info
->serial_no
;
5385 user_info
.hw_revision
= board_info
->hw_revision
;
5386 user_info
.vendor_id
= board_info
->vendor_id
;
5387 user_info
.device_id
= board_info
->device_id
;
5388 user_info
.pci_bus_no
= board_info
->pci_bus_no
;
5389 user_info
.pci_dev_no
= board_info
->pci_dev_no
;
5390 user_info
.pci_func_no
= board_info
->pci_func_no
;
5391 user_info
.irq
= board_info
->irq
;
5392 user_info
.irq_count
= board_info
->irq_count
;
5393 user_info
.driver_version
= ME4000_DRIVER_VERSION
;
5394 user_info
.ao_count
= board_info
->board_p
->ao
.count
;
5395 user_info
.ao_fifo_count
= board_info
->board_p
->ao
.fifo_count
;
5397 user_info
.ai_count
= board_info
->board_p
->ai
.count
;
5398 user_info
.ai_sh_count
= board_info
->board_p
->ai
.sh_count
;
5399 user_info
.ai_ex_trig_analog
= board_info
->board_p
->ai
.ex_trig_analog
;
5401 user_info
.dio_count
= board_info
->board_p
->dio
.count
;
5403 user_info
.cnt_count
= board_info
->board_p
->cnt
.count
;
5405 if (copy_to_user(arg
, &user_info
, sizeof(struct me4000_user_info
)))
5411 /*------------------------------------ ISR STUFF ------------------------------------*/
5413 static int me4000_ext_int_fasync(int fd
, struct file
*file_ptr
, int mode
)
5416 struct me4000_ext_int_context
*ext_int_context
;
5418 CALL_PDEBUG("me4000_ext_int_fasync() is executed\n");
5420 ext_int_context
= file_ptr
->private_data
;
5423 fasync_helper(fd
, file_ptr
, mode
, &ext_int_context
->fasync_ptr
);
5425 CALL_PDEBUG("me4000_ext_int_fasync() is leaved\n");
5429 static irqreturn_t
me4000_ao_isr(int irq
, void *dev_id
)
5433 struct me4000_ao_context
*ao_context
;
5437 //unsigned long before;
5438 //unsigned long after;
5440 ISR_PDEBUG("me4000_ao_isr() is executed\n");
5442 ao_context
= dev_id
;
5444 /* Check if irq number is right */
5445 if (irq
!= ao_context
->irq
) {
5446 ISR_PDEBUG("me4000_ao_isr():incorrect interrupt num: %d\n",
5451 /* Check if this DAC rised an interrupt */
5453 ((0x1 << (ao_context
->index
+ 3)) &
5454 me4000_inl(ao_context
->irq_status_reg
))) {
5455 ISR_PDEBUG("me4000_ao_isr():Not this DAC\n");
5459 /* Read status register to find out what happened */
5460 tmp
= me4000_inl(ao_context
->status_reg
);
5462 if (!(tmp
& ME4000_AO_STATUS_BIT_EF
) && (tmp
& ME4000_AO_STATUS_BIT_HF
)
5463 && (tmp
& ME4000_AO_STATUS_BIT_HF
)) {
5464 c
= ME4000_AO_FIFO_COUNT
;
5465 ISR_PDEBUG("me4000_ao_isr():Fifo empty\n");
5466 } else if ((tmp
& ME4000_AO_STATUS_BIT_EF
)
5467 && (tmp
& ME4000_AO_STATUS_BIT_HF
)
5468 && (tmp
& ME4000_AO_STATUS_BIT_HF
)) {
5469 c
= ME4000_AO_FIFO_COUNT
/ 2;
5470 ISR_PDEBUG("me4000_ao_isr():Fifo under half full\n");
5473 ISR_PDEBUG("me4000_ao_isr():Fifo full\n");
5476 ISR_PDEBUG("me4000_ao_isr():Try to write 0x%04X values\n", c
);
5479 c1
= me4000_values_to_end(ao_context
->circ_buf
,
5480 ME4000_AO_BUFFER_COUNT
);
5481 ISR_PDEBUG("me4000_ao_isr():Values to end = %d\n", c1
);
5487 ("me4000_ao_isr():Work done or buffer empty\n");
5491 if (((ao_context
->fifo_reg
& 0xFF) == ME4000_AO_01_FIFO_REG
) ||
5492 ((ao_context
->fifo_reg
& 0xFF) == ME4000_AO_03_FIFO_REG
)) {
5493 for (i
= 0; i
< c1
; i
++) {
5497 (ao_context
->circ_buf
.buf
+
5498 ao_context
->circ_buf
.tail
+ i
))) << 16;
5499 outl(value
, ao_context
->fifo_reg
);
5502 outsw(ao_context
->fifo_reg
,
5503 ao_context
->circ_buf
.buf
+
5504 ao_context
->circ_buf
.tail
, c1
);
5507 //printk(KERN_ERR"ME4000:me4000_ao_isr():Time lapse = %lu\n", after - before);
5509 ao_context
->circ_buf
.tail
=
5510 (ao_context
->circ_buf
.tail
+ c1
) & (ME4000_AO_BUFFER_COUNT
-
5512 ISR_PDEBUG("me4000_ao_isr():%d values wrote to port 0x%04X\n",
5513 c1
, ao_context
->fifo_reg
);
5517 /* If there are no values left in the buffer, disable interrupts */
5518 spin_lock(&ao_context
->int_lock
);
5519 if (!me4000_buf_count(ao_context
->circ_buf
, ME4000_AO_BUFFER_COUNT
)) {
5521 ("me4000_ao_isr():Disable Interrupt because no values left in buffer\n");
5522 tmp
= me4000_inl(ao_context
->ctrl_reg
);
5523 tmp
&= ~ME4000_AO_CTRL_BIT_ENABLE_IRQ
;
5524 me4000_outl(tmp
, ao_context
->ctrl_reg
);
5526 spin_unlock(&ao_context
->int_lock
);
5528 /* Reset the interrupt */
5529 spin_lock(&ao_context
->int_lock
);
5530 tmp
= me4000_inl(ao_context
->ctrl_reg
);
5531 tmp
|= ME4000_AO_CTRL_BIT_RESET_IRQ
;
5532 me4000_outl(tmp
, ao_context
->ctrl_reg
);
5533 tmp
&= ~ME4000_AO_CTRL_BIT_RESET_IRQ
;
5534 me4000_outl(tmp
, ao_context
->ctrl_reg
);
5536 /* If state machine is stopped, flow was interrupted */
5537 if (!(me4000_inl(ao_context
->status_reg
) & ME4000_AO_STATUS_BIT_FSM
)) {
5538 printk(KERN_ERR
"ME4000:me4000_ao_isr():Broken pipe\n");
5539 ao_context
->pipe_flag
= 1; // Set flag in order to inform write routine
5540 tmp
&= ~ME4000_AO_CTRL_BIT_ENABLE_IRQ
; // Disable interrupt
5542 me4000_outl(tmp
, ao_context
->ctrl_reg
);
5543 spin_unlock(&ao_context
->int_lock
);
5545 /* Wake up waiting process */
5546 wake_up_interruptible(&(ao_context
->wait_queue
));
5548 /* Count the interrupt */
5549 ao_context
->board_info
->irq_count
++;
5554 static irqreturn_t
me4000_ai_isr(int irq
, void *dev_id
)
5557 struct me4000_ai_context
*ai_context
;
5561 #ifdef ME4000_ISR_DEBUG
5562 unsigned long before
;
5563 unsigned long after
;
5566 ISR_PDEBUG("me4000_ai_isr() is executed\n");
5568 #ifdef ME4000_ISR_DEBUG
5572 ai_context
= dev_id
;
5574 /* Check if irq number is right */
5575 if (irq
!= ai_context
->irq
) {
5576 ISR_PDEBUG("me4000_ai_isr():incorrect interrupt num: %d\n",
5581 if (me4000_inl(ai_context
->irq_status_reg
) &
5582 ME4000_IRQ_STATUS_BIT_AI_HF
) {
5584 ("me4000_ai_isr():Fifo half full interrupt occured\n");
5586 /* Read status register to find out what happened */
5587 tmp
= me4000_inl(ai_context
->ctrl_reg
);
5589 if (!(tmp
& ME4000_AI_STATUS_BIT_FF_DATA
) &&
5590 !(tmp
& ME4000_AI_STATUS_BIT_HF_DATA
)
5591 && (tmp
& ME4000_AI_STATUS_BIT_EF_DATA
)) {
5592 ISR_PDEBUG("me4000_ai_isr():Fifo full\n");
5593 c
= ME4000_AI_FIFO_COUNT
;
5595 /* FIFO overflow, so stop conversion and disable all interrupts */
5596 spin_lock(&ai_context
->int_lock
);
5597 tmp
= me4000_inl(ai_context
->ctrl_reg
);
5598 tmp
|= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP
;
5600 ~(ME4000_AI_CTRL_BIT_HF_IRQ
|
5601 ME4000_AI_CTRL_BIT_SC_IRQ
);
5602 outl(tmp
, ai_context
->ctrl_reg
);
5603 spin_unlock(&ai_context
->int_lock
);
5604 } else if ((tmp
& ME4000_AI_STATUS_BIT_FF_DATA
) &&
5605 !(tmp
& ME4000_AI_STATUS_BIT_HF_DATA
)
5606 && (tmp
& ME4000_AI_STATUS_BIT_EF_DATA
)) {
5607 ISR_PDEBUG("me4000_ai_isr():Fifo half full\n");
5608 c
= ME4000_AI_FIFO_COUNT
/ 2;
5612 ("me4000_ai_isr():Can't determine state of fifo\n");
5615 ISR_PDEBUG("me4000_ai_isr():Try to read %d values\n", c
);
5618 c1
= me4000_space_to_end(ai_context
->circ_buf
,
5619 ME4000_AI_BUFFER_COUNT
);
5620 ISR_PDEBUG("me4000_ai_isr():Space to end = %d\n", c1
);
5626 ("me4000_ai_isr():Work done or buffer full\n");
5630 insw(ai_context
->data_reg
,
5631 ai_context
->circ_buf
.buf
+
5632 ai_context
->circ_buf
.head
, c1
);
5633 ai_context
->circ_buf
.head
=
5634 (ai_context
->circ_buf
.head
+
5635 c1
) & (ME4000_AI_BUFFER_COUNT
- 1);
5639 /* Work is done, so reset the interrupt */
5641 ("me4000_ai_isr():reset interrupt fifo half full interrupt\n");
5642 spin_lock(&ai_context
->int_lock
);
5643 tmp
= me4000_inl(ai_context
->ctrl_reg
);
5644 tmp
|= ME4000_AI_CTRL_BIT_HF_IRQ_RESET
;
5645 me4000_outl(tmp
, ai_context
->ctrl_reg
);
5646 tmp
&= ~ME4000_AI_CTRL_BIT_HF_IRQ_RESET
;
5647 me4000_outl(tmp
, ai_context
->ctrl_reg
);
5648 spin_unlock(&ai_context
->int_lock
);
5651 if (me4000_inl(ai_context
->irq_status_reg
) & ME4000_IRQ_STATUS_BIT_SC
) {
5653 ("me4000_ai_isr():Sample counter interrupt occured\n");
5655 if (!ai_context
->sample_counter_reload
) {
5657 ("me4000_ai_isr():Single data block available\n");
5659 /* Poll data until fifo empty */
5661 (i
< ME4000_AI_FIFO_COUNT
/ 2)
5662 && (inl(ai_context
->ctrl_reg
) &
5663 ME4000_AI_STATUS_BIT_EF_DATA
); i
++) {
5664 if (me4000_space_to_end
5665 (ai_context
->circ_buf
,
5666 ME4000_AI_BUFFER_COUNT
)) {
5667 *(ai_context
->circ_buf
.buf
+
5668 ai_context
->circ_buf
.head
) =
5669 inw(ai_context
->data_reg
);
5670 ai_context
->circ_buf
.head
=
5671 (ai_context
->circ_buf
.head
+
5672 1) & (ME4000_AI_BUFFER_COUNT
- 1);
5676 ISR_PDEBUG("me4000_ai_isr():%d values read\n", i
);
5678 if (ai_context
->sample_counter
<=
5679 ME4000_AI_FIFO_COUNT
/ 2) {
5681 ("me4000_ai_isr():Interrupt from adjustable half full threshold\n");
5683 /* Read status register to find out what happened */
5684 tmp
= me4000_inl(ai_context
->ctrl_reg
);
5686 if (!(tmp
& ME4000_AI_STATUS_BIT_FF_DATA
) &&
5687 !(tmp
& ME4000_AI_STATUS_BIT_HF_DATA
)
5688 && (tmp
& ME4000_AI_STATUS_BIT_EF_DATA
)) {
5690 ("me4000_ai_isr():Fifo full\n");
5691 c
= ME4000_AI_FIFO_COUNT
;
5693 /* FIFO overflow, so stop conversion */
5694 spin_lock(&ai_context
->int_lock
);
5695 tmp
= me4000_inl(ai_context
->ctrl_reg
);
5697 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP
;
5698 outl(tmp
, ai_context
->ctrl_reg
);
5699 spin_unlock(&ai_context
->int_lock
);
5700 } else if ((tmp
& ME4000_AI_STATUS_BIT_FF_DATA
)
5702 ME4000_AI_STATUS_BIT_HF_DATA
)
5704 ME4000_AI_STATUS_BIT_EF_DATA
)) {
5706 ("me4000_ai_isr():Fifo half full\n");
5707 c
= ME4000_AI_FIFO_COUNT
/ 2;
5709 c
= ai_context
->sample_counter
;
5711 ("me4000_ai_isr():Sample count values\n");
5715 ("me4000_ai_isr():Try to read %d values\n",
5719 c1
= me4000_space_to_end(ai_context
->
5721 ME4000_AI_BUFFER_COUNT
);
5723 ("me4000_ai_isr():Space to end = %d\n",
5730 ("me4000_ai_isr():Work done or buffer full\n");
5734 insw(ai_context
->data_reg
,
5735 ai_context
->circ_buf
.buf
+
5736 ai_context
->circ_buf
.head
, c1
);
5737 ai_context
->circ_buf
.head
=
5738 (ai_context
->circ_buf
.head
+
5739 c1
) & (ME4000_AI_BUFFER_COUNT
- 1);
5744 ("me4000_ai_isr():Multiple data block available\n");
5746 /* Read status register to find out what happened */
5747 tmp
= me4000_inl(ai_context
->ctrl_reg
);
5749 if (!(tmp
& ME4000_AI_STATUS_BIT_FF_DATA
) &&
5750 !(tmp
& ME4000_AI_STATUS_BIT_HF_DATA
)
5751 && (tmp
& ME4000_AI_STATUS_BIT_EF_DATA
)) {
5753 ("me4000_ai_isr():Fifo full\n");
5754 c
= ME4000_AI_FIFO_COUNT
;
5756 /* FIFO overflow, so stop conversion */
5757 spin_lock(&ai_context
->int_lock
);
5758 tmp
= me4000_inl(ai_context
->ctrl_reg
);
5760 ME4000_AI_CTRL_BIT_IMMEDIATE_STOP
;
5761 outl(tmp
, ai_context
->ctrl_reg
);
5762 spin_unlock(&ai_context
->int_lock
);
5765 c1
= me4000_space_to_end
5766 (ai_context
->circ_buf
,
5767 ME4000_AI_BUFFER_COUNT
);
5769 ("me4000_ai_isr():Space to end = %d\n",
5776 ("me4000_ai_isr():Work done or buffer full\n");
5780 insw(ai_context
->data_reg
,
5781 ai_context
->circ_buf
.buf
+
5782 ai_context
->circ_buf
.head
,
5784 ai_context
->circ_buf
.head
=
5785 (ai_context
->circ_buf
.head
+
5787 (ME4000_AI_BUFFER_COUNT
-
5791 } else if ((tmp
& ME4000_AI_STATUS_BIT_FF_DATA
)
5793 ME4000_AI_STATUS_BIT_HF_DATA
)
5795 ME4000_AI_STATUS_BIT_EF_DATA
)) {
5797 ("me4000_ai_isr():Fifo half full\n");
5798 c
= ME4000_AI_FIFO_COUNT
/ 2;
5801 c1
= me4000_space_to_end
5802 (ai_context
->circ_buf
,
5803 ME4000_AI_BUFFER_COUNT
);
5805 ("me4000_ai_isr():Space to end = %d\n",
5812 ("me4000_ai_isr():Work done or buffer full\n");
5816 insw(ai_context
->data_reg
,
5817 ai_context
->circ_buf
.buf
+
5818 ai_context
->circ_buf
.head
,
5820 ai_context
->circ_buf
.head
=
5821 (ai_context
->circ_buf
.head
+
5823 (ME4000_AI_BUFFER_COUNT
-
5828 /* Poll data until fifo empty */
5830 (i
< ME4000_AI_FIFO_COUNT
/ 2)
5831 && (inl(ai_context
->ctrl_reg
) &
5832 ME4000_AI_STATUS_BIT_EF_DATA
);
5834 if (me4000_space_to_end
5835 (ai_context
->circ_buf
,
5836 ME4000_AI_BUFFER_COUNT
)) {
5837 *(ai_context
->circ_buf
.
5839 ai_context
->circ_buf
.
5841 inw(ai_context
->data_reg
);
5842 ai_context
->circ_buf
.
5847 (ME4000_AI_BUFFER_COUNT
5853 ("me4000_ai_isr():%d values read\n",
5859 /* Work is done, so reset the interrupt */
5861 ("me4000_ai_isr():reset interrupt from sample counter\n");
5862 spin_lock(&ai_context
->int_lock
);
5863 tmp
= me4000_inl(ai_context
->ctrl_reg
);
5864 tmp
|= ME4000_AI_CTRL_BIT_SC_IRQ_RESET
;
5865 me4000_outl(tmp
, ai_context
->ctrl_reg
);
5866 tmp
&= ~ME4000_AI_CTRL_BIT_SC_IRQ_RESET
;
5867 me4000_outl(tmp
, ai_context
->ctrl_reg
);
5868 spin_unlock(&ai_context
->int_lock
);
5871 /* Values are now available, so wake up waiting process */
5872 if (me4000_buf_count(ai_context
->circ_buf
, ME4000_AI_BUFFER_COUNT
)) {
5873 ISR_PDEBUG("me4000_ai_isr():Wake up waiting process\n");
5874 wake_up_interruptible(&(ai_context
->wait_queue
));
5877 /* If there is no space left in the buffer, disable interrupts */
5878 spin_lock(&ai_context
->int_lock
);
5879 if (!me4000_buf_space(ai_context
->circ_buf
, ME4000_AI_BUFFER_COUNT
)) {
5881 ("me4000_ai_isr():Disable Interrupt because no space left in buffer\n");
5882 tmp
= me4000_inl(ai_context
->ctrl_reg
);
5884 ~(ME4000_AI_CTRL_BIT_SC_IRQ
| ME4000_AI_CTRL_BIT_HF_IRQ
|
5885 ME4000_AI_CTRL_BIT_LE_IRQ
);
5886 me4000_outl(tmp
, ai_context
->ctrl_reg
);
5888 spin_unlock(&ai_context
->int_lock
);
5890 #ifdef ME4000_ISR_DEBUG
5892 printk(KERN_ERR
"ME4000:me4000_ai_isr():Time lapse = %lu\n",
5899 static irqreturn_t
me4000_ext_int_isr(int irq
, void *dev_id
)
5901 struct me4000_ext_int_context
*ext_int_context
;
5904 ISR_PDEBUG("me4000_ext_int_isr() is executed\n");
5906 ext_int_context
= dev_id
;
5908 /* Check if irq number is right */
5909 if (irq
!= ext_int_context
->irq
) {
5910 ISR_PDEBUG("me4000_ext_int_isr():incorrect interrupt num: %d\n",
5915 if (me4000_inl(ext_int_context
->irq_status_reg
) &
5916 ME4000_IRQ_STATUS_BIT_EX
) {
5917 ISR_PDEBUG("me4000_ext_int_isr():External interrupt occured\n");
5918 tmp
= me4000_inl(ext_int_context
->ctrl_reg
);
5919 tmp
|= ME4000_AI_CTRL_BIT_EX_IRQ_RESET
;
5920 me4000_outl(tmp
, ext_int_context
->ctrl_reg
);
5921 tmp
&= ~ME4000_AI_CTRL_BIT_EX_IRQ_RESET
;
5922 me4000_outl(tmp
, ext_int_context
->ctrl_reg
);
5924 ext_int_context
->int_count
++;
5926 if (ext_int_context
->fasync_ptr
) {
5928 ("me2600_ext_int_isr():Send signal to process\n");
5929 kill_fasync(&ext_int_context
->fasync_ptr
, SIGIO
,
5937 static void __exit
me4000_module_exit(void)
5939 struct me4000_info
*board_info
;
5941 CALL_PDEBUG("cleanup_module() is executed\n");
5943 unregister_chrdev(me4000_ext_int_major_driver_no
, ME4000_EXT_INT_NAME
);
5945 unregister_chrdev(me4000_cnt_major_driver_no
, ME4000_CNT_NAME
);
5947 unregister_chrdev(me4000_dio_major_driver_no
, ME4000_DIO_NAME
);
5949 unregister_chrdev(me4000_ai_major_driver_no
, ME4000_AI_NAME
);
5951 unregister_chrdev(me4000_ao_major_driver_no
, ME4000_AO_NAME
);
5953 remove_proc_entry("me4000", NULL
);
5955 pci_unregister_driver(&me4000_driver
);
5957 /* Reset the boards */
5958 list_for_each_entry(board_info
, &me4000_board_info_list
, list
) {
5959 me4000_reset_board(board_info
);
5962 clear_board_info_list();
5965 module_exit(me4000_module_exit
);
5967 static int me4000_read_procmem(char *buf
, char **start
, off_t offset
, int count
,
5968 int *eof
, void *data
)
5971 int limit
= count
- 1000;
5972 struct me4000_info
*board_info
;
5974 len
+= sprintf(buf
+ len
, "\nME4000 DRIVER VERSION %X.%X.%X\n\n",
5975 (ME4000_DRIVER_VERSION
& 0xFF0000) >> 16,
5976 (ME4000_DRIVER_VERSION
& 0xFF00) >> 8,
5977 (ME4000_DRIVER_VERSION
& 0xFF));
5979 /* Search for the board context */
5980 list_for_each_entry(board_info
, &me4000_board_info_list
, list
) {
5982 sprintf(buf
+ len
, "Board number %d:\n",
5983 board_info
->board_count
);
5984 len
+= sprintf(buf
+ len
, "---------------\n");
5986 sprintf(buf
+ len
, "PLX base register = 0x%lX\n",
5987 board_info
->plx_regbase
);
5989 sprintf(buf
+ len
, "PLX base register size = 0x%X\n",
5990 (unsigned int)board_info
->plx_regbase_size
);
5992 sprintf(buf
+ len
, "ME4000 base register = 0x%X\n",
5993 (unsigned int)board_info
->me4000_regbase
);
5995 sprintf(buf
+ len
, "ME4000 base register size = 0x%X\n",
5996 (unsigned int)board_info
->me4000_regbase_size
);
5998 sprintf(buf
+ len
, "Serial number = 0x%X\n",
5999 board_info
->serial_no
);
6001 sprintf(buf
+ len
, "Hardware revision = 0x%X\n",
6002 board_info
->hw_revision
);
6004 sprintf(buf
+ len
, "Vendor id = 0x%X\n",
6005 board_info
->vendor_id
);
6007 sprintf(buf
+ len
, "Device id = 0x%X\n",
6008 board_info
->device_id
);
6010 sprintf(buf
+ len
, "PCI bus number = %d\n",
6011 board_info
->pci_bus_no
);
6013 sprintf(buf
+ len
, "PCI device number = %d\n",
6014 board_info
->pci_dev_no
);
6016 sprintf(buf
+ len
, "PCI function number = %d\n",
6017 board_info
->pci_func_no
);
6018 len
+= sprintf(buf
+ len
, "IRQ = %u\n", board_info
->irq
);
6021 "Count of interrupts since module was loaded = %d\n",
6022 board_info
->irq_count
);
6025 sprintf(buf
+ len
, "Count of analog outputs = %d\n",
6026 board_info
->board_p
->ao
.count
);
6028 sprintf(buf
+ len
, "Count of analog output fifos = %d\n",
6029 board_info
->board_p
->ao
.fifo_count
);
6032 sprintf(buf
+ len
, "Count of analog inputs = %d\n",
6033 board_info
->board_p
->ai
.count
);
6036 "Count of sample and hold devices for analog input = %d\n",
6037 board_info
->board_p
->ai
.sh_count
);
6040 "Analog external trigger available for analog input = %d\n",
6041 board_info
->board_p
->ai
.ex_trig_analog
);
6044 sprintf(buf
+ len
, "Count of digital ports = %d\n",
6045 board_info
->board_p
->dio
.count
);
6048 sprintf(buf
+ len
, "Count of counter devices = %d\n",
6049 board_info
->board_p
->cnt
.count
);
6051 sprintf(buf
+ len
, "AI control register = 0x%08X\n",
6052 inl(board_info
->me4000_regbase
+
6053 ME4000_AI_CTRL_REG
));
6055 len
+= sprintf(buf
+ len
, "AO 0 control register = 0x%08X\n",
6056 inl(board_info
->me4000_regbase
+
6057 ME4000_AO_00_CTRL_REG
));
6059 sprintf(buf
+ len
, "AO 0 status register = 0x%08X\n",
6060 inl(board_info
->me4000_regbase
+
6061 ME4000_AO_00_STATUS_REG
));
6063 sprintf(buf
+ len
, "AO 1 control register = 0x%08X\n",
6064 inl(board_info
->me4000_regbase
+
6065 ME4000_AO_01_CTRL_REG
));
6067 sprintf(buf
+ len
, "AO 1 status register = 0x%08X\n",
6068 inl(board_info
->me4000_regbase
+
6069 ME4000_AO_01_STATUS_REG
));
6071 sprintf(buf
+ len
, "AO 2 control register = 0x%08X\n",
6072 inl(board_info
->me4000_regbase
+
6073 ME4000_AO_02_CTRL_REG
));
6075 sprintf(buf
+ len
, "AO 2 status register = 0x%08X\n",
6076 inl(board_info
->me4000_regbase
+
6077 ME4000_AO_02_STATUS_REG
));
6079 sprintf(buf
+ len
, "AO 3 control register = 0x%08X\n",
6080 inl(board_info
->me4000_regbase
+
6081 ME4000_AO_03_CTRL_REG
));
6083 sprintf(buf
+ len
, "AO 3 status register = 0x%08X\n",
6084 inl(board_info
->me4000_regbase
+
6085 ME4000_AO_03_STATUS_REG
));