/* mrfcommon.c -- Micro-Research Event Generator/Event Receiver Linux 2.6 driver common functions This driver handles multiple boards. Author: Jukka Pietarinen (MRF) Date: 29.11.2006 17.1.2012 Modified to support unlocked_ioctl if available */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "pci_mrfev.h" MODULE_LICENSE("GPL"); #ifndef DEVICE_NAME #define DEVICE_NAME "pci_mrf" #endif struct mrf_dev mrf_devices[MAX_MRF_DEVICES]; int ev_assign_irq(struct mrf_dev *ev_device); int ev_plx_irq_enable(struct mrf_dev *ev_device); int ev_plx_irq_disable(struct mrf_dev *ev_device); /* Open and close */ int ev_open(struct inode *inode, struct file *filp) { int result; struct mrf_dev *ev_device; int major, minor; ev_device = container_of(inode->i_cdev, struct mrf_dev, cdev); filp->private_data = ev_device; major = MAJOR(inode->i_rdev); minor = MINOR(inode->i_rdev); if (down_interruptible(&ev_device->sem)) return -ERESTARTSYS; /* Check that none of the first three devices is open. We allow several simultaneous connections on the 4th minor device. */ if ((ev_device->access_device > 0 && ev_device->access_device < DEVICE_EV) || (ev_device->access_device == DEVICE_EV && minor != DEVICE_EV)) { printk(KERN_WARNING DEVICE_NAME ": open device major %d minor %d" "already open. Only one open minor device allowed.\n", major, ev_device->access_device); result = -EPERM; goto out; } if (minor >= DEVICE_FIRST && minor <= DEVICE_LAST) { ev_device->access_mode = filp->f_flags & O_ACCMODE; ev_device->access_device = minor; switch (ev_device->access_device) { case DEVICE_EEPROM: /* Allocate and clear memory for EEPROM data */ ev_device->eeprom_data = kmalloc(EEPROM_DATA_ALLOC_SIZE, GFP_KERNEL); memset(ev_device->eeprom_data, 0, EEPROM_DATA_ALLOC_SIZE); /* If the EEPROM device was opened in read-only mode we need to read in the data from the EEPROM. */ if (ev_device->access_mode == O_RDONLY) { result = eeprom_sprint(ev_device->pLC, ev_device->eeprom_data, EEPROM_DATA_ALLOC_SIZE); if (result < 0) { /* If there was an error, clean up */ kfree(ev_device->eeprom_data); ev_device->eeprom_data = NULL; ev_device->access_mode = 0; ev_device->access_device = 0; goto out; } ev_device->eeprom_data_size = result; } break; case DEVICE_XCF: ev_device->xcf_data = kmalloc(XCF_DATA_ALLOC_SIZE, GFP_KERNEL); memset(ev_device->xcf_data, 0xffffffff, XCF_DATA_ALLOC_SIZE); /* If the XCF device was opened in write-only mode we erase the Platform Flash and start the programming sequence */ if (ev_device->access_mode == O_RDONLY) { ev_device->xcf_data_pos = -1; result = jtag_XCF_readstart(ev_device); if (result < 0) { /* If there was an error, clean up */ kfree(ev_device->xcf_data); ev_device->xcf_data = NULL; ev_device->access_mode = 0; ev_device->xcf_data_pos = 0; ev_device->access_device = 0; goto out; } } if (ev_device->access_mode == O_WRONLY) { ev_device->xcf_data_pos = 0; result = jtag_XCF_progstart(ev_device); if (result < 0) { /* If there was an error, clean up */ kfree(ev_device->xcf_data); ev_device->xcf_data = NULL; ev_device->access_mode = 0; ev_device->access_device = 0; goto out; } } break; case DEVICE_FPGA: /* Allocate and clear memory for FPGA data */ ev_device->fpga_conf_data = kmalloc(FPGA_DATA_ALLOC_SIZE, GFP_KERNEL); memset(ev_device->fpga_conf_data, 0, FPGA_DATA_ALLOC_SIZE); /* If the FPGA device was opened in read-only mode we read in the FPGA status - reading back configuration data is not supported */ result = -1; if (ev_device->access_mode == O_RDONLY) { result = fpga_sprint(ev_device, ev_device->fpga_conf_data, FPGA_DATA_ALLOC_SIZE); ev_device->fpga_conf_size = result; } if (ev_device->access_mode == O_WRONLY) { result = jtag_init_load_fpga(ev_device); ev_device->fpga_conf_size = 0; } if (result < 0) { /* If there was an error, clean up */ kfree(ev_device->fpga_conf_data); ev_device->fpga_conf_data = NULL; ev_device->access_mode = 0; ev_device->access_device = 0; goto out; } break; case DEVICE_EV: /* Assign interrupt handler when first instance opened. */ if (!(ev_device->refcount_ev)) { result = ev_assign_irq(ev_device); ev_plx_irq_enable(ev_device); } /* Inrease device reference count. */ ev_device->refcount_ev++; break; default: result = -ENXIO; goto out; } } else { result = -ENXIO; goto out; } result = 0; out: up(&ev_device->sem); return result; } int ev_release(struct inode *inode, struct file *filp) { int result = 0; struct mrf_dev *ev_device = (struct mrf_dev *) filp->private_data; if (down_interruptible(&ev_device->sem)) return -ERESTARTSYS; switch (ev_device->access_device) { case DEVICE_EEPROM: if (ev_device->access_mode == O_WRONLY) { result = eeprom_sscan(ev_device->pLC, ev_device->eeprom_data); } /* Release memory allocated for EEPROM data */ kfree(ev_device->eeprom_data); ev_device->eeprom_data = NULL; ev_device->access_mode = 0; ev_device->access_device = 0; break; case DEVICE_XCF: if (ev_device->access_mode == O_RDONLY) { result = jtag_XCF_readend(ev_device); } if (ev_device->access_mode == O_WRONLY) { /* Flush buffer */ jtag_XCF_write(ev_device, ev_device->xcf_data, ev_device->xcf_data_pos, XCF_DATA_ALLOC_SIZE); result = jtag_XCF_progend(ev_device); /* Issue load FPGA from flash (cycle nPROG) */ jtag_XCF_conf(ev_device); } /* Release memory allocated for XCF data */ kfree(ev_device->xcf_data); ev_device->xcf_data = NULL; ev_device->access_mode = 0; ev_device->access_device = 0; break; case DEVICE_FPGA: if (ev_device->access_mode == O_WRONLY) { result = jtag_end_load_fpga(ev_device); } /* Release memory allocated for FPGA data */ kfree(ev_device->fpga_conf_data); ev_device->fpga_conf_data = NULL; ev_device->access_mode = 0; ev_device->access_device = 0; break; case DEVICE_EV: /* Remove from list of asynchronously notified filps */ ev_fasync(-1, filp, 0); if (ev_device->refcount_ev) ev_device->refcount_ev--; if (!ev_device->refcount_ev) { /* Release interrupt handler when device is closed for the last time. */ ev_plx_irq_disable(ev_device); free_irq(ev_device->irq, (void *) ev_device); ev_device->access_mode = 0; ev_device->access_device = 0; } break; default: printk(KERN_WARNING DEVICE_NAME ": Trying to release unknown device major %d minor %d\n", MAJOR(inode->i_rdev), MINOR(inode->i_rdev)); result = -ENXIO; } up(&ev_device->sem); return result; } /* Read and write */ ssize_t ev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) { ssize_t retval = 0; struct mrf_dev *ev_device = (struct mrf_dev *) filp->private_data; if (down_interruptible(&ev_device->sem)) return -ERESTARTSYS; switch (ev_device->access_device) { case DEVICE_EEPROM: { if (*f_pos > ev_device->eeprom_data_size) goto read_out; if (*f_pos + count > ev_device->eeprom_data_size) count = ev_device->eeprom_data_size - *f_pos; if (copy_to_user(buf, &ev_device->eeprom_data[*f_pos], count)) { retval = -EFAULT; goto read_out; } *f_pos += count; retval = count; break; } case DEVICE_XCF: { if ((*f_pos & ~(XCF_DATA_ALLOC_SIZE - 1)) != ev_device->xcf_data_pos) { jtag_XCF_read(ev_device, ev_device->xcf_data, (unsigned int) (*f_pos & ~(XCF_DATA_ALLOC_SIZE - 1)), XCF_DATA_ALLOC_SIZE); ev_device->xcf_data_pos = *f_pos & ~(XCF_DATA_ALLOC_SIZE - 1); } if (*f_pos + count > ev_device->xcf_data_pos + XCF_DATA_ALLOC_SIZE) count = ev_device->xcf_data_pos + XCF_DATA_ALLOC_SIZE - *f_pos; if (copy_to_user(buf, &ev_device->xcf_data[(*f_pos & (XCF_DATA_ALLOC_SIZE - 1))], count)) { retval = -EFAULT; goto read_out; } *f_pos += count; retval = count; break; } case DEVICE_FPGA: { if (*f_pos > ev_device->fpga_conf_size) goto read_out; if (*f_pos + count > ev_device->fpga_conf_size) count = ev_device->fpga_conf_size - *f_pos; if (copy_to_user(buf, &ev_device->fpga_conf_data[*f_pos], count)) { retval = -EFAULT; goto read_out; } *f_pos += count; retval = count; break; } } read_out: up(&ev_device->sem); return retval; } ssize_t ev_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos) { ssize_t retval = -ENOMEM; struct mrf_dev *ev_device = (struct mrf_dev *) filp->private_data; #ifdef DEBUG printk(KERN_INFO DEVICE_NAME ": write %d bytes.\n", count); #endif if (down_interruptible(&ev_device->sem)) return -ERESTARTSYS; switch (ev_device->access_device) { case DEVICE_EEPROM: { if (*f_pos > EEPROM_DATA_ALLOC_SIZE - 1) goto write_out; if (*f_pos + count > EEPROM_DATA_ALLOC_SIZE - 1) count = EEPROM_DATA_ALLOC_SIZE - 1 - *f_pos; if (copy_from_user(&ev_device->eeprom_data[*f_pos], buf, count)) { retval = -EFAULT; goto write_out; } *f_pos += count; ev_device->eeprom_data_size = *f_pos; retval = count; break; } case DEVICE_XCF: { if (*f_pos < ev_device->xcf_data_pos) goto write_out; if (*f_pos + count > ev_device->xcf_data_pos + XCF_DATA_ALLOC_SIZE) count = ev_device->xcf_data_pos + XCF_DATA_ALLOC_SIZE - *f_pos; if (copy_from_user(&ev_device->xcf_data[*f_pos & (XCF_DATA_ALLOC_SIZE - 1)], buf, count)) { retval = -EFAULT; goto write_out; } *f_pos += count; if (*f_pos - ev_device->xcf_data_pos == XCF_DATA_ALLOC_SIZE) { jtag_XCF_write(ev_device, ev_device->xcf_data, ev_device->xcf_data_pos, XCF_DATA_ALLOC_SIZE); memset(ev_device->xcf_data, 0xffffffff, XCF_DATA_ALLOC_SIZE); ev_device->xcf_data_pos = *f_pos; } retval = count; break; } case DEVICE_FPGA: { int i; if (count > FPGA_DATA_ALLOC_SIZE - 1) count = FPGA_DATA_ALLOC_SIZE - 1; if (copy_from_user(ev_device->fpga_conf_data, buf, count)) { retval = -EFAULT; goto write_out; } *f_pos += count; ev_device->fpga_conf_size = *f_pos; for (i = 0; i < count; i++) jtag_load_fpga_byte(ev_device->pLC, ev_device->fpga_conf_data[i]); retval = count; break; } } write_out: up(&ev_device->sem); return retval; } #ifdef HAVE_UNLOCKED_IOCTL long ev_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) #else int ev_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) #endif { struct mrf_dev *ev_device = (struct mrf_dev *) filp->private_data; int ret = 0; /* Check that cmd is valid */ if (_IOC_TYPE(cmd) != EV_IOC_MAGIC) return -ENOTTY; if (_IOC_NR(cmd) > EV_IOC_MAX) return -ENOTTY; /* Check access */ if (_IOC_DIR(cmd) & _IOC_READ) ret = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd)); else if (_IOC_DIR(cmd) & _IOC_WRITE) ret = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd)); if (ret) return -EFAULT; switch (cmd) { case EV_IOCRESET: ret = 0; break; case EV_IOCIRQEN: ev_plx_irq_enable(ev_device); ret = 0; break; case EV_IOCIRQDIS: ret = 0; break; default: return -ENOTTY; } return ret; } /* Virtual Memory Area operations */ void ev_vma_open(struct vm_area_struct *vma) { } void ev_vma_close(struct vm_area_struct *vma) { } static struct vm_operations_struct ev_remap_vm_ops = { .open = ev_vma_open, .close = ev_vma_close, }; int ev_remap_mmap(struct file *filp, struct vm_area_struct *vma) { struct mrf_dev *ev_device = (struct mrf_dev *) filp->private_data; unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; unsigned long physical = ((unsigned long) ev_device->mrEv) + offset; unsigned long vsize = vma->vm_end - vma->vm_start; int result; /* Memory mapping supported only on minor device DEVICE_EV. */ if (ev_device->access_device != DEVICE_EV) { printk(KERN_NOTICE DEVICE_NAME ": mmap not allowed for access_device %d\n", ev_device->access_device); return -EPERM; } if (vsize > ev_device->lenEv) { printk(KERN_NOTICE DEVICE_NAME ": mmap vsize %08x, ev_device->lenEv %08x\n", (unsigned int) vsize, (unsigned int) ev_device->lenEv); return -EINVAL; } #if LINUX_VERSION_CODE > 0x020609 result = io_remap_pfn_range(vma, vma->vm_start, physical >> PAGE_SHIFT, vsize, vma->vm_page_prot); #else result = io_remap_page_range(vma, vma->vm_start, physical, vsize, vma->vm_page_prot); #endif if (result) return -EAGAIN; vma->vm_ops = &ev_remap_vm_ops; ev_vma_open(vma); return 0; } int ev_fasync(int fd, struct file *filp, int mode) { struct mrf_dev *ev_device = (struct mrf_dev *) filp->private_data; return fasync_helper(fd, filp, mode, &ev_device->async_queue); } irqreturn_t ev_interrupt(int irq, void *dev_id) /* , struct pt_regs *regs) */ { struct mrf_dev *ev_device = (struct mrf_dev *) dev_id; volatile struct Pci9030LocalConf *pLC = ev_device->pLC; /* We use shared interrupts: return immediately if irq is not from our device. */ if (!(le16_to_cpu(pLC->INTCSR) & PLX9030_INTCSR_LINTI1_STAT)) return IRQ_NONE; /* We turn off the interrupt line here to signal the kernel that the interrupt is handled and we notify the user space application about a pending irq. */ ev_plx_irq_disable(ev_device); kill_fasync(&ev_device->async_queue, SIGIO, POLL_IN); return IRQ_HANDLED; } int ev_plx_irq_enable(struct mrf_dev *ev_device) { volatile struct Pci9030LocalConf *pLC = ev_device->pLC; pLC->INTCSR = __constant_cpu_to_le16(PLX9030_INTCSR_LINTI1_ENA | PLX9030_INTCSR_LINTI1_POL | PLX9030_INTCSR_PCI_IRQENA); barrier(); return le16_to_cpu(pLC->INTCSR); } int ev_plx_irq_disable(struct mrf_dev *ev_device) { volatile struct Pci9030LocalConf *pLC = ev_device->pLC; pLC->INTCSR = __constant_cpu_to_le16(PLX9030_INTCSR_LINTI1_ENA | PLX9030_INTCSR_LINTI1_POL); barrier(); return le16_to_cpu(pLC->INTCSR); }