10 #define DEBUG_CONFIG(x) x
12 #define DEBUG_CONFIG(x)
16 #define DEBUG_PIO(x) x
22 #define DEBUG_MAP(x) x
27 #ifdef TRACE_INTERRUPT
28 #define DEBUG_INT(x) x
34 #define DEBUG_DMA(x) x
40 #if defined(USE_MODVERSIONS) && USE_MODVERSIONS
42 # include <config/modversions.h>
46 #include <linux/version.h>
47 #include <linux/kernel.h>
48 #include <linux/module.h>
50 #include <linux/cdev.h>
51 #include <linux/init.h>
52 #include <linux/slab.h>
53 #include <linux/ioctl.h>
55 #include <linux/poll.h>
56 #include <linux/sched.h>
57 #include <linux/interrupt.h>
58 #include <linux/pci.h>
59 #include <linux/ioport.h>
60 #include <linux/errno.h>
61 #include <linux/dma-mapping.h>
63 #include <asm/uaccess.h>
64 #include <asm/segment.h>
82 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
85 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36))
86 #define USE_OLD_IOCTL 1
88 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 32))
89 #define USE_OLD_DMABITMASK 1
155 static int vmedrv_mmap(
struct file* filep,
struct vm_area_struct* vma);
156 static unsigned int vmedrv_poll(
struct file* filep, poll_table* table);
176 static void free_windows(
int window_index,
int number_of_windows);
177 static void*
map_windows(
unsigned vme_address,
unsigned window_index,
unsigned number_of_windows,
unsigned flags);
202 static int start_dma(
struct dev_prop_t* dev_prop,
unsigned long pci_address,
unsigned long vme_address,
unsigned long size,
int direction);
206 static int initiate_dma(
struct dev_prop_t* dev_prop,
unsigned mapped_pci_address,
unsigned vme_address,
unsigned size,
int direction);
211 static int vmedrv_ioctl(
struct inode *inode,
struct file *filep,
unsigned int cmd,
unsigned long arg);
216 .owner = THIS_MODULE,
221 .ioctl = vmedrv_ioctl,
253 int result = -ENODEV;
259 printk(KERN_WARNING
"%s: unable to register PCI driver\n",
vmedrv_name);
260 goto FAIL_PCI_DRIVER_REGISTRATION;
263 printk(KERN_WARNING
"%s: unable to find VME-PCI Bus Adapter.\n",
vmedrv_name);
265 goto FAIL_DEVICE_DETECTION;
269 result = alloc_chrdev_region(
275 result = register_chrdev_region(
281 goto FAIL_DRIVER_REGISTRATION;
307 cdev.owner = THIS_MODULE;
311 printk(KERN_WARNING
"%s: unable to register driver.\n",
vmedrv_name);
312 goto FAIL_CDEV_REGISTRATION;
315 printk(KERN_INFO
"%s: successfully installed at 0x%04x on irq %d (major = %d).\n",
321 FAIL_CDEV_REGISTRATION:
323 FAIL_DRIVER_REGISTRATION:
325 FAIL_DEVICE_DETECTION:
327 FAIL_PCI_DRIVER_REGISTRATION:
354 int minor_id, mode, method;
356 minor_id = MINOR(inode->i_rdev);
358 printk(KERN_WARNING
"%s: invalid minor ID.\n",
vmedrv_name);
362 filep->private_data = kmalloc(
sizeof(
struct dev_prop_t), GFP_KERNEL);
363 if (filep->private_data == 0) {
364 printk(KERN_WARNING
"%s: unable to allocate memory.",
vmedrv_name);
367 dev_prop = filep->private_data;
370 struct resource* resource;
373 resource = request_region(
376 if (resource == NULL) {
377 printk(KERN_WARNING
"%s: I/O region request fault.\n",
vmedrv_name);
379 goto FAIL_REGION_REQUEST;
384 printk(KERN_WARNING
"%s: initialization fault.\n",
vmedrv_name);
385 goto FAIL_INITIALIZATION;
389 result = request_irq(
394 result = request_irq(
400 printk(KERN_WARNING
"%s: IRQ request fault.\n",
vmedrv_name);
401 goto FAIL_IRQ_REQUEST;
432 try_module_get(THIS_MODULE);
442 dev_prop = filep->private_data;
476 module_put(THIS_MODULE);
485 unsigned long vme_address;
486 int total_read_size, read_size, remainder_size;
488 dev_prop = filep->private_data;
489 vme_address = *f_pos;
492 remainder_size =
count;
498 while (remainder_size > 0) {
503 read_size =
dma_read(dev_prop, buf, vme_address, remainder_size);
506 read_size =
pio_read(dev_prop, buf, vme_address, remainder_size);
515 else if (read_size == 0) {
519 remainder_size -= read_size;
520 total_read_size += read_size;
521 vme_address += read_size;
525 *f_pos += total_read_size;
527 return total_read_size;
534 unsigned long vme_address;
535 int total_written_size, written_size, remainder_size;
537 dev_prop = filep->private_data;
538 vme_address = *f_pos;
540 total_written_size = 0;
541 remainder_size =
count;
547 while (remainder_size > 0) {
552 written_size =
dma_write(dev_prop, buf, vme_address, remainder_size);
555 written_size =
pio_write(dev_prop, buf, vme_address, remainder_size);
561 if (written_size < 0) {
564 else if (written_size == 0) {
568 remainder_size -= written_size;
569 total_written_size += written_size;
570 vme_address += written_size;
574 *f_pos += total_written_size;
576 return total_written_size;
584 filep->f_pos = offset;
587 filep->f_pos += offset;
600 static int vmedrv_ioctl(
struct inode* inode,
struct file *filep,
unsigned int cmd,
unsigned long arg)
610 int result = -EINVAL;
612 unsigned long uncopied_length;
626 if (get_user(value, (
int*) arg) < 0) {
640 uncopied_length = copy_from_user(
641 &interrupt_property, (
const __user
void *) arg, argument_size
643 if (uncopied_length != 0) {
647 irq = interrupt_property.
irq;
654 dev_prop = filep->private_data;
713 unsigned long vme_address,
size;
714 unsigned long physical_address, page_frame_number;
717 dev_prop = filep->private_data;
718 size = vma->vm_end - vma->vm_start;
719 vme_address = vma->vm_pgoff * PAGE_SIZE;
721 DEBUG_MAP(printk(KERN_DEBUG
"mapping vme memory...\n"));
722 DEBUG_MAP(printk(KERN_DEBUG
" vme address: 0x%lx\n", vme_address));
723 DEBUG_MAP(printk(KERN_DEBUG
" mapping size: 0x%lx\n", size));
725 if (vme_address & (PAGE_SIZE - 1)) {
751 page_frame_number = physical_address >> PAGE_SHIFT;
753 DEBUG_MAP(printk(KERN_DEBUG
" physical address: 0x%lx\n", physical_address));
755 if (
remap_pfn_range(vma, vma->vm_start, page_frame_number, size, vma->vm_page_prot) < 0) {
759 DEBUG_MAP(printk(KERN_DEBUG
" mapped address: 0x%lx\n", vma->vm_start));
767 struct dev_prop_t* dev_prop = filep->private_data;
769 unsigned int mask = 0;
775 local_interrupt_client != 0;
776 local_interrupt_client = local_interrupt_client->
next
779 mask |= POLLIN | POLLRDNORM;
795 DEBUG_INT(printk(KERN_DEBUG
"interrupt handled.\n"));
805 DEBUG_INT(printk(KERN_DEBUG
"the card is not generating an interrupt.\n"));
845 printk(KERN_WARNING
"%s: Unknown interrupt handled...\n",
vmedrv_name);
855 KERN_INFO
"%s: %s is detected at ioport 0x%04lx on irq %d.\n",
862 KERN_INFO
" I/O Mapped Node at 0x%04lx.\n",
863 (
unsigned long) pci_dev->resource[bit3_IO_NODE_IO_BASE_INDEX].start
866 KERN_INFO
" Memory Mapped Node at 0x%04lx.\n",
870 KERN_INFO
" Mapping Register at 0x%04lx.\n",
874 KERN_INFO
" Remote Memory at 0x%04lx.\n",
880 printk(KERN_INFO
"%s: this is not our target card...\n",
vmedrv_name);
888 result = pci_enable_device(pci_dev);
890 printk(KERN_WARNING
"%s: enabling device failed.\n",
vmedrv_name);
894 #ifdef USE_OLD_DMABITMASK
895 pci_set_dma_mask(pci_dev, DMA_32BIT_MASK);
897 pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
908 printk(KERN_WARNING
"%s: device removed.\n",
vmedrv_name);
938 DEBUG_CONFIG(printk(KERN_DEBUG
"remapping pci memories...\n"));
950 printk(KERN_WARNING
"%s: ERROR: VME chassis power is off.\n",
vmedrv_name);
951 printk(KERN_WARNING
" (or I/O cable is not connected, or SYSRESET is active.)\n");
952 printk(KERN_WARNING
" Local Status Register: 0x%02x\n", status);
974 printk(KERN_WARNING
"%s: ERROR: interface parity error.\n",
vmedrv_name);
977 printk(KERN_WARNING
"%s: ERROR: remote bus error.\n",
vmedrv_name);
980 printk(KERN_WARNING
"%s: ERROR: receiving PR interrupt.\n",
vmedrv_name);
983 printk(KERN_WARNING
"%s: ERROR: interface timed out.\n",
vmedrv_name);
986 printk(KERN_WARNING
"%s: ERROR: LRC(Longitudinal Redundancy Check) error.\n",
vmedrv_name);
988 if (status & lsREMOTE_BUS_POWER_OFF) {
989 printk(KERN_WARNING
"%s: ERROR: remote bus power off or I/O cable is off.\n",
vmedrv_name);
992 printk(KERN_WARNING
" Local Status Register: 0x%02x\n", status);
995 printk(KERN_WARNING
" Adapter ID (I/O): 0x%02x\n", status);
997 printk(KERN_WARNING
" Adapter ID (mem): 0x%02x\n", status);
1044 DEBUG_CONFIG(printk(KERN_DEBUG
"transfer mode is set to PIO.\n"));
1052 DEBUG_CONFIG(printk(KERN_DEBUG
"transfer mode is set to DMA.\n"));
1056 DEBUG_CONFIG(printk(KERN_DEBUG
"DMA transfer mode is requested, but not available.\n"));
1057 DEBUG_CONFIG(printk(KERN_DEBUG
"transfer mode is set to PIO.\n"));
1065 DEBUG_CONFIG(printk(KERN_DEBUG
"transfer mode is set to Non-Blocking DMA.\n"));
1078 unsigned long address = 0, data = 0;
1080 if (get_user(address, &word_access->address) < 0) {
1083 if (put_user(data, &word_access->data) < 0) {
1087 result =
pio_read(dev_prop, (__user
char*) &word_access->data, address, 1);
1098 unsigned long offset_address;
1099 unsigned long size, read_size;
1100 unsigned long uncopied_length;
1101 void* window_address;
1103 unsigned long pio_buf_data_size, pio_buf_index;
1114 if (offset_address + count <= bit3_WINDOW_SIZE * dev_prop->number_of_pio_windows) {
1124 pio_buf_data_size = 0;
1126 for (read_size = 0; read_size <
size; read_size += dev_prop->
data_width) {
1128 ((
unsigned short *) pio_buf)[pio_buf_index] =
ioread16(window_address);
1131 ((
unsigned int *) pio_buf)[pio_buf_index] =
ioread32(window_address);
1139 uncopied_length = copy_to_user(buf, pio_buf, pio_buf_data_size);
1140 if (uncopied_length > 0) {
1143 buf += pio_buf_data_size;
1144 pio_buf_data_size = 0;
1149 uncopied_length = copy_to_user(buf, pio_buf, pio_buf_data_size);
1150 if (uncopied_length > 0) {
1162 printk(KERN_WARNING
"%s: VME I/O failed.\n",
vmedrv_name);
1163 printk(KERN_WARNING
" Local Status Register: 0x%02x\n", status);
1173 unsigned long offset_address;
1174 unsigned long size, remain_size;
1175 unsigned long uncopied_length;
1176 void* window_address;
1178 unsigned long pio_buf_data_size, pio_buf_index;
1189 if (offset_address + count <= bit3_WINDOW_SIZE * dev_prop->number_of_pio_windows) {
1199 pio_buf_data_size = 0;
1201 for (remain_size = size; remain_size > 0; remain_size -= dev_prop->
data_width) {
1202 if (pio_buf_data_size <= 0) {
1204 pio_buf_data_size = remain_size;
1209 uncopied_length = copy_from_user(pio_buf, buf, pio_buf_data_size);
1210 if (uncopied_length > 0) {
1213 buf += pio_buf_data_size;
1219 ((
unsigned short *) pio_buf)[pio_buf_index], window_address
1224 ((
unsigned int *) pio_buf)[pio_buf_index], window_address
1241 printk(KERN_WARNING
"%s: VME I/O failed.\n",
vmedrv_name);
1242 printk(KERN_WARNING
" Local Status Register: 0x%02x\n", status);
1253 DEBUG_PIO(printk(KERN_DEBUG
"allocating PIO buffer\n"));
1258 printk(KERN_WARNING
"%s: unable to allocate PIO buffer.",
vmedrv_name);
1267 DEBUG_PIO(printk(KERN_DEBUG
"allocating PIO windows\n"));
1282 int number_of_free_windows;
1283 int window_index = -ENOMEM;
1286 number_of_free_windows = 0;
1289 number_of_free_windows = 0;
1293 if (number_of_free_windows == 0) {
1297 number_of_free_windows++;
1298 if (number_of_free_windows == number_of_windows) {
1303 if (i == bit3_NUMBER_OF_WINDOWS) {
1307 for (i = 0; i < number_of_windows; i++) {
1311 return window_index;
1318 for (i = 0; i < number_of_windows; i++) {
1324 static void*
map_windows(
unsigned vme_address,
unsigned size,
unsigned window_index,
unsigned flags)
1326 unsigned base_address, offset_address;
1327 unsigned number_of_windows;
1328 void* window_address;
1333 number_of_windows = ((
unsigned long) size - 1) / (
unsigned long)
bit3_WINDOW_SIZE + 1;
1336 for (i = 0; i < number_of_windows; i++) {
1338 base_address | flags,
1345 return window_address;
1357 DEBUG_INT(printk(KERN_DEBUG
"normal interrupt enabled.\n"));
1371 DEBUG_INT(printk(KERN_DEBUG
"normal interrupt disabled.\n"));
1405 KERN_DEBUG
"interrupt flags are saved: 0x%02x\n",
1422 KERN_DEBUG
"interrupt flags are restored: 0x%02x\n",
1433 printk(KERN_WARNING
"%s: error interrupt handled.\n",
vmedrv_name);
1434 printk(KERN_WARNING
" Local Status Register: 0x%02x\n", local_status);
1436 printk(KERN_WARNING
" Fiber-Optic Interface Data Error\n");
1439 printk(KERN_WARNING
" Remote Bus Error\n");
1442 printk(KERN_WARNING
" Interface Timeout\n");
1445 printk(KERN_WARNING
" Remote Bus Power Off or I/O Cable Is Off\n");
1449 if (local_status & lsINTERFACE_TIMEOUT) {
1460 DEBUG_INT(printk(KERN_DEBUG
"pt interrupt handled...\n"));
1470 DEBUG_INT(printk(KERN_DEBUG
"DEBUG_INT: dma interrupt handled...\n"));
1471 DEBUG_DMA(printk(KERN_DEBUG
"DEBUG_DMA: dma interrupt handled...\n"));
1486 DEBUG_INT(printk(KERN_DEBUG
"pr interrupt handled...\n"));
1499 unsigned irq, vector;
1501 struct task_struct* task;
1505 DEBUG_INT(printk(KERN_DEBUG
"vme interrupt handled...\n"));
1509 if (! (interrupt_status & (0x0001 << irq))) {
1512 DEBUG_INT(printk(KERN_DEBUG
" irq = %d\n", irq));
1518 DEBUG_INT(printk(KERN_DEBUG
" vector = 0x%04x\n", vector));
1522 while (interrupt_client != 0) {
1526 DEBUG_INT(printk(KERN_DEBUG
" auto-disabled.\n"));
1529 task = interrupt_client->
task;
1530 signal_id = interrupt_client->
signal_id;
1531 if (signal_id > 0) {
1532 send_sig(signal_id, task, priv = 1);
1533 DEBUG_INT(printk(KERN_DEBUG
" send signal.\n"));
1538 DEBUG_INT(printk(KERN_DEBUG
" wake up.\n"));
1541 interrupt_client = interrupt_client->
next;
1545 DEBUG_INT(printk(KERN_DEBUG
"now exit vme interrupt handling routine.\n"));
1562 if ((interrupt_client == 0) || (local_interrupt_client == 0)) {
1563 printk(KERN_WARNING
"%s: unable to allocate memory for interrupt client entry.\n",
vmedrv_name);
1567 interrupt_client->
task = task;
1568 interrupt_client->
irq =
irq;
1569 interrupt_client->
vector = vector;
1570 interrupt_client->
signal_id = signal_id;
1583 "vme interrupt is registered, "
1584 "irq: %d, vector: 0x%04x, pid: %d, signal: %d.\n",
1585 irq, vector, task->pid, signal_id
1601 process_id = task->pid;
1605 prev_interrupt_client = 0;
1606 while (interrupt_client != 0) {
1608 (process_id != interrupt_client->
task->pid) ||
1609 ((irq != 0) && (irq != interrupt_client->
irq)) ||
1610 ((vector != 0) && (vector != interrupt_client->
vector))
1613 interrupt_client = interrupt_client->
next;
1618 "vme interrupt is unregistered, "
1619 "irq: %d, vector: 0x%04x, pid: %d.\n",
1620 interrupt_client->
irq, interrupt_client->
vector, process_id
1624 interrupt_client = interrupt_client->
next;
1625 if (prev_interrupt_client == 0) {
1633 prev_local_interrupt_client = 0;
1634 while (local_interrupt_client != 0) {
1635 if (local_interrupt_client->
interrupt_client != unregistered_interrupt_client) {
1636 prev_local_interrupt_client = local_interrupt_client;
1637 local_interrupt_client = local_interrupt_client->
next;
1641 unregistered_local_interrupt_client = local_interrupt_client;
1642 local_interrupt_client = local_interrupt_client->
next;
1643 if (prev_local_interrupt_client == 0) {
1647 prev_local_interrupt_client->
next = local_interrupt_client;
1649 kfree(unregistered_local_interrupt_client);
1652 kfree(unregistered_interrupt_client);
1662 long remaining_time;
1669 process_id = task->pid;
1671 while (interrupt_client != 0) {
1673 (process_id == interrupt_client->
task->pid) &&
1674 (vector == interrupt_client->
vector) &&
1679 interrupt_client = interrupt_client->
next;
1681 if (interrupt_client == 0)
1683 printk(KERN_WARNING
"%s: no interrupt is registered to wait for\n",
vmedrv_name);
1688 remaining_time = wait_event_interruptible_timeout(
1693 if (remaining_time == 0) {
1697 return -ERESTARTSYS;
1712 while (interrupt_client != 0) {
1713 if (vector == interrupt_client->
vector) {
1717 interrupt_client = interrupt_client->
next;
1729 while (interrupt_client != 0) {
1730 if (vector == interrupt_client->
vector) {
1733 interrupt_client = interrupt_client->
next;
1745 while (interrupt_client != 0) {
1746 if (vector == interrupt_client->
vector) {
1749 interrupt_client = interrupt_client->
next;
1761 while (interrupt_client != 0) {
1762 if (vector == interrupt_client->
vector) {
1765 interrupt_client = interrupt_client->
next;
1784 unsigned long pci_address,
size;
1785 unsigned long uncopied_length;
1805 result =
start_dma(dev_prop, pci_address, vme_address, size, direction);
1813 if (uncopied_length > 0) {
1823 unsigned long pci_address,
size;
1824 unsigned long uncopied_length;
1843 if (uncopied_length > 0) {
1851 result =
start_dma(dev_prop, pci_address, vme_address, size, direction);
1853 return (result < 0) ? result :
size;
1862 printk(KERN_WARNING
"%s: unable to allocate dma buffer.\n",
vmedrv_name);
1876 static int start_dma(
struct dev_prop_t* dev_prop,
unsigned long pci_address,
unsigned long vme_address,
unsigned long size,
int direction)
1880 int error_interrupt_count_before_dma;
1883 initiate_dma(dev_prop, pci_address, vme_address, size, direction);
1884 DEBUG_DMA(printk(KERN_DEBUG
"now start dma transfer...\n"));
1889 timeout = ((size*HZ)/100000 + HZ/10);
1890 remaining_time = wait_event_timeout(
1900 printk(KERN_WARNING
"%s: dma transfer timed out.\n",
vmedrv_name);
1906 DEBUG_DMA(printk(KERN_DEBUG
"dma transfer completed.\n"));
1911 printk(KERN_WARNING
"%s: dma transfer failed.\n",
vmedrv_name);
1912 printk(KERN_WARNING
" Local Status Register: 0x%02x\n", status);
1917 printk(KERN_WARNING
"%s: error during dma transfer.\n",
vmedrv_name);
1918 printk(KERN_WARNING
" Local Status Register: 0x%02x\n", status);
1928 dma_addr_t dma_handle;
1942 DEBUG_DMA(printk(KERN_DEBUG
"dma buffer is allocated.\n"));
1955 pci_free_consistent(
1960 DEBUG_DMA(printk(KERN_DEBUG
"dma buffer is released.\n"));
1967 unsigned base_address, offset_address;
1968 unsigned number_of_windows;
1969 unsigned window_index;
1970 void* mapping_register_address;
1971 unsigned mapped_pci_address;
1977 if (offset_address > 0) {
1978 number_of_windows += 1;
1982 mapping_register_address = (
1987 DEBUG_DMA(printk(KERN_DEBUG
"writing dma mapping registers...\n"));
1988 for (i = 0; i < number_of_windows; i++) {
1990 base_address | flags,
1991 mapping_register_address
1995 KERN_DEBUG
" reg: 0x%08x, value: 0x%08x\n",
1996 (
unsigned) mapping_register_address,
1997 base_address | flags
2005 mapped_pci_address |= offset_address;
2007 DEBUG_DMA(printk(KERN_DEBUG
" mapped pci address: 0x%08x\n", mapped_pci_address));
2009 return mapped_pci_address;
2015 unsigned remainder_count, packet_count;
2016 unsigned dma_register_value, address_modifier, remote_command2_value;
2018 DEBUG_DMA(printk(KERN_DEBUG
"setting dma parameters...\n"));
2022 if (direction ==
tdREAD) {
2038 DEBUG_DMA(printk(KERN_DEBUG
" dma reg value: 0x%02x\n", dma_register_value));
2042 (mapped_pci_address >> 0) & 0x000000ff,
2046 (mapped_pci_address >> 8) & 0x000000ff,
2050 (mapped_pci_address >> 16) & 0x000000ff,
2053 DEBUG_DMA(printk(KERN_DEBUG
" mapped pci address: 0x%08x\n", mapped_pci_address));
2057 (vme_address >> 0) & 0x000000ff,
2061 (vme_address >> 8) & 0x000000ff,
2065 (vme_address >> 16) & 0x000000ff,
2069 (vme_address >> 24) & 0x000000ff,
2073 DEBUG_DMA(printk(KERN_DEBUG
" vme address: 0x%08x\n", vme_address));
2087 (packet_count >> 0) & 0x00ff,
2091 (packet_count >> 8) & 0x00ff,
2094 DEBUG_DMA(printk(KERN_DEBUG
" remainder count: 0x%02x\n", remainder_count));
2095 DEBUG_DMA(printk(KERN_DEBUG
" packet count: 0x%04x\n", packet_count));
2111 remote_command2_value,
2115 DEBUG_DMA(printk(KERN_DEBUG
" remote command 2: 0x%02x\n", remote_command2_value));
2138 DEBUG_DMA(printk(KERN_DEBUG
"Local Status Register: 0x%02x\n", status));