1 #include <linux/kernel.h>
2 #include <linux/module.h>
4 #include <linux/init.h>
5 #include <linux/cdev.h>
7 #include <asm/uaccess.h> /* copy_*_user */
11 #include "modulbus_register.h"
13 #define DRIVER_NAME "vmod16a2"
14 #define PFX DRIVER_NAME ": "
16 /* The One And Only Device (OAOD) */
18 static struct cdev cdev
;
20 /* module config tables */
21 static struct vmod_devices config
;
23 static int open(struct inode
*ino
, struct file
*filp
)
25 unsigned int lun
= iminor(ino
);
26 unsigned int idx
= lun_to_index(&config
, lun
);
30 "cannot open, invalid lun %d\n", lun
);
33 filp
->private_data
= &config
.module
[idx
];
37 static int release(struct inode
*ino
, struct file
*filp
)
42 static int do_output(struct vmod_dev
*dev
,
43 struct vmod16a2_convert
*cvrt
)
45 int value
= cvrt
->value
;
46 int channel
= cvrt
->channel
;
47 struct vmod16a2_registers __iomem
*regp
=
48 (struct vmod16a2_registers __iomem
*)dev
->address
;
51 iowrite16be(value
, &(regp
->dac0in
));
52 iowrite16be(value
, &(regp
->ldac0
));
53 } else if (channel
== 1) {
54 iowrite16be(value
, &(regp
->dac1in
));
55 iowrite16be(value
, &(regp
->ldac1
));
57 printk(KERN_ERR PFX
"invalid channel %d\n", channel
);
63 static int ioctl(struct inode
*inode
,
68 struct vmod_dev
*devp
= fp
->private_data
;
69 struct vmod16a2_convert cvrt
, *cvrtp
= &cvrt
;
74 if (copy_from_user(cvrtp
, (const void __user
*)arg
, sizeof(cvrt
)) != 0)
76 return do_output(devp
, cvrtp
);
85 /* @brief file operations for this driver */
86 static struct file_operations vmod16a2_fops
= {
93 /* module initialization and cleanup */
94 static int __init
init(void)
98 printk(KERN_INFO PFX
"initializing driver");
99 err
= read_params(DRIVER_NAME
, &config
);
103 err
= alloc_chrdev_region(&devno
, 0, VMOD16A2_MAX_MODULES
, DRIVER_NAME
);
106 printk(KERN_INFO PFX
"allocated device %0d\n", MAJOR(devno
));
108 cdev_init(&cdev
, &vmod16a2_fops
);
109 cdev
.owner
= THIS_MODULE
;
110 err
= cdev_add(&cdev
, devno
, VMOD16A2_MAX_MODULES
);
113 "failed to create chardev %d with err %d\n",
119 fail_cdev
: unregister_chrdev_region(devno
, VMOD16A2_MAX_MODULES
);
120 fail_chrdev
: return -1;
123 static void __exit
exit(void)
126 unregister_chrdev_region(devno
, VMOD16A2_MAX_MODULES
);
132 MODULE_LICENSE("GPL");
133 MODULE_AUTHOR("Juan David Gonzalez Cobas <dcobas@cern.ch>");