29 #include <linux/module.h> 30 #include <linux/vmalloc.h> 46 static int eccdev_open(
struct inode *,
struct file *);
48 static long eccdev_ioctl(
struct file *,
unsigned int,
unsigned long);
49 static int eccdev_mmap(
struct file *,
struct vm_area_struct *);
55 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) 56 # define FAULT_RETURN_TYPE int 58 # define FAULT_RETURN_TYPE vm_fault_t 62 #
if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)
63 struct vm_area_struct *,
91 ec_ioctl_context_t
ctx;
111 cdev->
cdev.owner = THIS_MODULE;
113 ret = cdev_add(&cdev->
cdev,
114 MKDEV(MAJOR(dev_num), master->
index), 1);
128 cdev_del(&cdev->
cdev);
145 "Failed to allocate memory for private data structure.\n");
150 priv->
ctx.writable = (filp->f_mode & FMODE_WRITE) != 0;
151 priv->
ctx.requested = 0;
152 priv->
ctx.process_data = NULL;
153 priv->
ctx.process_data_size = 0;
155 filp->private_data = priv;
172 if (priv->
ctx.requested) {
176 if (priv->
ctx.process_data) {
177 vfree(priv->
ctx.process_data);
192 long eccdev_ioctl(
struct file *filp,
unsigned int cmd,
unsigned long arg)
198 "ioctl(filp = 0x%p, cmd = 0x%08x (0x%02x), arg = 0x%lx)\n",
199 filp, cmd, _IOC_NR(cmd), arg);
210 #define VM_DONTDUMP VM_RESERVED 222 struct vm_area_struct *vma
230 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) 235 vma->vm_private_data = priv;
250 #
if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)
251 struct vm_area_struct *vma,
256 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) 257 struct vm_area_struct *vma = vmf->vma;
259 unsigned long offset = vmf->pgoff << PAGE_SHIFT;
263 if (offset >= priv->
ctx.process_data_size) {
264 return VM_FAULT_SIGBUS;
267 page = vmalloc_to_page(priv->
ctx.process_data + offset);
269 return VM_FAULT_SIGBUS;
276 " offset = %lu, page = %p\n", offset, page);
ec_master_t * master
Master owning the device.
#define VM_DONTDUMP
VM_RESERVED disappeared in 3.7.
Private data structure for file handles.
struct cdev cdev
Character device.
ec_cdev_t * cdev
Character device.
EtherCAT master structure.
#define EC_MASTER_DBG(master, level, fmt, args...)
Convenience macro for printing master-specific debug messages to syslog.
Ethernet over EtherCAT (EoE)
#define FAULT_RETURN_TYPE
This is the kernel version from which the .fault member of the vm_operations_struct is usable...
static int eccdev_mmap(struct file *, struct vm_area_struct *)
Memory-map callback for the EtherCAT character device.
struct vm_operations_struct eccdev_vm_ops
Callbacks for a virtual memory area retrieved with ecdevc_mmap().
long ec_ioctl(ec_master_t *master, ec_ioctl_context_t *ctx, unsigned int cmd, void *arg)
Called when an ioctl() command is issued.
#define EC_MASTER_ERR(master, fmt, args...)
Convenience macro for printing master-specific errors to syslog.
static long eccdev_ioctl(struct file *, unsigned int, unsigned long)
Called when an ioctl() command is issued.
ec_ioctl_context_t ctx
Context.
static int eccdev_open(struct inode *, struct file *)
Called when the cdev is opened.
Vendor specific over EtherCAT protocol handler.
static int eccdev_release(struct inode *, struct file *)
Called when the cdev is closed.
void ec_cdev_clear(ec_cdev_t *cdev)
Destructor.
#define DEBUG
Set to 1 to enable device operations debugging.
EtherCAT master character device.
static FAULT_RETURN_TYPE eccdev_vma_fault(struct vm_fault *)
Page fault callback for a virtual memory area.
EtherCAT master character device IOCTL commands.
EtherCAT slave configuration structure.
EtherCAT master character device.
void ecrt_release_master(ec_master_t *master)
Releases a requested EtherCAT master.
static struct file_operations eccdev_fops
File operation callbacks for the EtherCAT character device.
int ec_cdev_init(ec_cdev_t *cdev, ec_master_t *master, dev_t dev_num)
Constructor.