2 * VMEbus User access driver
4 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
8 * Tom Armistead and Ajit Prem
9 * Copyright 2004 Motorola Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #include <linux/cdev.h>
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/ioctl.h>
25 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pagemap.h>
29 #include <linux/pci.h>
30 #include <linux/semaphore.h>
31 #include <linux/spinlock.h>
32 #include <linux/syscalls.h>
33 #include <linux/types.h>
36 #include <asm/uaccess.h>
41 static char driver_name[] = "vme_user";
43 static int bus[USER_BUS_MAX];
46 /* Currently Documentation/devices.txt defines the following for VME:
49 * 0 = /dev/bus/vme/m0 First master image
50 * 1 = /dev/bus/vme/m1 Second master image
51 * 2 = /dev/bus/vme/m2 Third master image
52 * 3 = /dev/bus/vme/m3 Fourth master image
53 * 4 = /dev/bus/vme/s0 First slave image
54 * 5 = /dev/bus/vme/s1 Second slave image
55 * 6 = /dev/bus/vme/s2 Third slave image
56 * 7 = /dev/bus/vme/s3 Fourth slave image
57 * 8 = /dev/bus/vme/ctl Control
59 * It is expected that all VME bus drivers will use the
60 * same interface. For interface documentation see
61 * http://www.vmelinux.org/.
63 * However the VME driver at http://www.vmelinux.org/ is rather old and doesn't
64 * even support the tsi148 chipset (which has 8 master and 8 slave windows).
65 * We'll run with this or now as far as possible, however it probably makes
66 * sense to get rid of the old mappings and just do everything dynamically.
68 * So for now, we'll restrict the driver to providing 4 masters and 4 slaves as
69 * defined above and try to support at least some of the interface from
70 * http://www.vmelinux.org/ as an alternative drive can be written providing a
71 * saner interface later.
73 * The vmelinux.org driver never supported slave images, the devices reserved
74 * for slaves were repurposed to support all 8 master images on the UniverseII!
75 * We shall support 4 masters and 4 slaves with this driver.
77 #define VME_MAJOR 221 /* VME Major Device Number */
78 #define VME_DEVS 9 /* Number of dev entries */
80 #define MASTER_MINOR 0
84 #define CONTROL_MINOR 8
86 #define PCI_BUF_SIZE 0x20000 /* Size of one slave image buffer */
89 * Structure to handle image related parameters.
92 void __iomem *kern_buf; /* Buffer address in kernel space */
93 dma_addr_t pci_buf; /* Buffer address in PCI address space */
94 unsigned long long size_buf; /* Buffer size */
95 struct semaphore sem; /* Semaphore for locking image */
96 struct device *device; /* Sysfs device */
97 struct vme_resource *resource; /* VME resource */
98 int users; /* Number of current users */
100 static image_desc_t image[VME_DEVS];
104 unsigned long writes;
105 unsigned long ioctls;
108 unsigned long dmaErrors;
109 unsigned long timeouts;
110 unsigned long external;
112 static driver_stats_t statistics;
114 struct cdev *vme_user_cdev; /* Character device */
115 struct class *vme_user_sysfs_class; /* Sysfs class */
116 struct device *vme_user_bridge; /* Pointer to the bridge device */
119 static const int type[VME_DEVS] = { MASTER_MINOR, MASTER_MINOR,
120 MASTER_MINOR, MASTER_MINOR,
121 SLAVE_MINOR, SLAVE_MINOR,
122 SLAVE_MINOR, SLAVE_MINOR,
127 static int vme_user_open(struct inode *, struct file *);
128 static int vme_user_release(struct inode *, struct file *);
129 static ssize_t vme_user_read(struct file *, char *, size_t, loff_t *);
130 static ssize_t vme_user_write(struct file *, const char *, size_t, loff_t *);
131 static loff_t vme_user_llseek(struct file *, loff_t, int);
132 static int vme_user_ioctl(struct inode *, struct file *, unsigned int,
135 static int __init vme_user_probe(struct device *, int, int);
136 static int __exit vme_user_remove(struct device *, int, int);
138 static struct file_operations vme_user_fops = {
139 .open = vme_user_open,
140 .release = vme_user_release,
141 .read = vme_user_read,
142 .write = vme_user_write,
143 .llseek = vme_user_llseek,
144 .ioctl = vme_user_ioctl,
149 * Reset all the statistic counters
151 static void reset_counters(void)
153 statistics.reads = 0;
154 statistics.writes = 0;
155 statistics.ioctls = 0;
157 statistics.berrs = 0;
158 statistics.dmaErrors = 0;
159 statistics.timeouts = 0;
162 static int vme_user_open(struct inode *inode, struct file *file)
165 unsigned int minor = MINOR(inode->i_rdev);
167 down(&image[minor].sem);
168 /* Only allow device to be opened if a resource is allocated */
169 if (image[minor].resource == NULL) {
170 printk(KERN_ERR "No resources allocated for device\n");
175 /* Increment user count */
176 image[minor].users++;
178 up(&image[minor].sem);
183 up(&image[minor].sem);
188 static int vme_user_release(struct inode *inode, struct file *file)
190 unsigned int minor = MINOR(inode->i_rdev);
192 down(&image[minor].sem);
194 /* Decrement user count */
195 image[minor].users--;
197 up(&image[minor].sem);
203 * We are going ot alloc a page during init per window for small transfers.
204 * Small transfers will go VME -> buffer -> user space. Larger (more than a
205 * page) transfers will lock the user space buffer into memory and then
206 * transfer the data directly into the user space buffers.
208 static ssize_t resource_to_user(int minor, char __user *buf, size_t count,
214 if (count <= image[minor].size_buf) {
215 /* We copy to kernel buffer */
216 copied = vme_master_read(image[minor].resource,
217 image[minor].kern_buf, count, *ppos);
222 retval = __copy_to_user(buf, image[minor].kern_buf,
223 (unsigned long)copied);
225 copied = (copied - retval);
226 printk("User copy failed\n");
231 /* XXX Need to write this */
232 printk("Currently don't support large transfers\n");
233 /* Map in pages from userspace */
235 /* Call vme_master_read to do the transfer */
243 * We are going ot alloc a page during init per window for small transfers.
244 * Small transfers will go user space -> buffer -> VME. Larger (more than a
245 * page) transfers will lock the user space buffer into memory and then
246 * transfer the data directly from the user space buffers out to VME.
248 static ssize_t resource_from_user(unsigned int minor, const char *buf,
249 size_t count, loff_t *ppos)
254 if (count <= image[minor].size_buf) {
255 retval = __copy_from_user(image[minor].kern_buf, buf,
256 (unsigned long)count);
258 copied = (copied - retval);
262 copied = vme_master_write(image[minor].resource,
263 image[minor].kern_buf, copied, *ppos);
265 /* XXX Need to write this */
266 printk("Currently don't support large transfers\n");
267 /* Map in pages from userspace */
269 /* Call vme_master_write to do the transfer */
276 static ssize_t buffer_to_user(unsigned int minor, char __user *buf,
277 size_t count, loff_t *ppos)
279 void __iomem *image_ptr;
282 image_ptr = image[minor].kern_buf + *ppos;
284 retval = __copy_to_user(buf, image_ptr, (unsigned long)count);
286 retval = (count - retval);
287 printk(KERN_WARNING "Partial copy to userspace\n");
291 /* Return number of bytes successfully read */
295 static ssize_t buffer_from_user(unsigned int minor, const char *buf,
296 size_t count, loff_t *ppos)
298 void __iomem *image_ptr;
301 image_ptr = image[minor].kern_buf + *ppos;
303 retval = __copy_from_user(image_ptr, buf, (unsigned long)count);
305 retval = (count - retval);
306 printk(KERN_WARNING "Partial copy to userspace\n");
310 /* Return number of bytes successfully read */
314 static ssize_t vme_user_read(struct file *file, char *buf, size_t count,
317 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
322 down(&image[minor].sem);
324 /* XXX Do we *really* want this helper - we can use vme_*_get ? */
325 image_size = vme_get_size(image[minor].resource);
327 /* Ensure we are starting at a valid location */
328 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
329 up(&image[minor].sem);
333 /* Ensure not reading past end of the image */
334 if (*ppos + count > image_size)
335 okcount = image_size - *ppos;
339 switch (type[minor]){
341 retval = resource_to_user(minor, buf, okcount, ppos);
344 retval = buffer_to_user(minor, buf, okcount, ppos);
350 up(&image[minor].sem);
358 static ssize_t vme_user_write(struct file *file, const char *buf, size_t count,
361 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
366 down(&image[minor].sem);
368 image_size = vme_get_size(image[minor].resource);
370 /* Ensure we are starting at a valid location */
371 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
372 up(&image[minor].sem);
376 /* Ensure not reading past end of the image */
377 if (*ppos + count > image_size)
378 okcount = image_size - *ppos;
382 switch (type[minor]){
384 retval = resource_from_user(minor, buf, okcount, ppos);
387 retval = buffer_from_user(minor, buf, okcount, ppos);
393 up(&image[minor].sem);
401 static loff_t vme_user_llseek(struct file *file, loff_t off, int whence)
403 loff_t absolute = -1;
404 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
407 down(&image[minor].sem);
408 image_size = vme_get_size(image[minor].resource);
415 absolute = file->f_pos + off;
418 absolute = image_size + off;
421 up(&image[minor].sem);
426 if ((absolute < 0) || (absolute >= image_size)) {
427 up(&image[minor].sem);
431 file->f_pos = absolute;
433 up(&image[minor].sem);
439 * The ioctls provided by the old VME access method (the one at vmelinux.org)
440 * are most certainly wrong as the effectively push the registers layout
441 * through to user space. Given that the VME core can handle multiple bridges,
442 * with different register layouts this is most certainly not the way to go.
444 * We aren't using the structures defined in the Motorola driver either - these
445 * are also quite low level, however we should use the definitions that have
446 * already been defined.
448 static int vme_user_ioctl(struct inode *inode, struct file *file,
449 unsigned int cmd, unsigned long arg)
451 struct vme_master master;
452 struct vme_slave slave;
453 unsigned long copied;
454 unsigned int minor = MINOR(inode->i_rdev);
460 switch (type[minor]) {
466 memset(&master, 0, sizeof(struct vme_master));
468 /* XXX We do not want to push aspace, cycle and width
469 * to userspace as they are
471 retval = vme_master_get(image[minor].resource,
472 &(master.enable), &(master.vme_addr),
473 &(master.size), &(master.aspace),
474 &(master.cycle), &(master.dwidth));
476 copied = copy_to_user((char *)arg, &master,
477 sizeof(struct vme_master));
479 printk(KERN_WARNING "Partial copy to "
489 copied = copy_from_user(&master, (char *)arg,
492 printk(KERN_WARNING "Partial copy from "
497 /* XXX We do not want to push aspace, cycle and width
498 * to userspace as they are
500 return vme_master_set(image[minor].resource,
501 master.enable, master.vme_addr, master.size,
502 master.aspace, master.cycle, master.dwidth);
510 memset(&slave, 0, sizeof(struct vme_slave));
512 /* XXX We do not want to push aspace, cycle and width
513 * to userspace as they are
515 retval = vme_slave_get(image[minor].resource,
516 &(slave.enable), &(slave.vme_addr),
517 &(slave.size), &pci_addr, &(slave.aspace),
520 copied = copy_to_user((char *)arg, &slave,
521 sizeof(struct vme_slave));
523 printk(KERN_WARNING "Partial copy to "
533 copied = copy_from_user(&slave, (char *)arg,
536 printk(KERN_WARNING "Partial copy from "
541 /* XXX We do not want to push aspace, cycle and width
542 * to userspace as they are
544 return vme_slave_set(image[minor].resource,
545 slave.enable, slave.vme_addr, slave.size,
546 image[minor].pci_buf, slave.aspace,
559 * Unallocate a previously allocated buffer
561 static void buf_unalloc (int num)
563 if (image[num].kern_buf) {
565 printk(KERN_DEBUG "UniverseII:Releasing buffer at %p\n",
569 vme_free_consistent(image[num].resource, image[num].size_buf,
570 image[num].kern_buf, image[num].pci_buf);
572 image[num].kern_buf = NULL;
573 image[num].pci_buf = 0;
574 image[num].size_buf = 0;
578 printk(KERN_DEBUG "UniverseII: Buffer not allocated\n");
583 static struct vme_driver vme_user_driver = {
585 .probe = vme_user_probe,
586 .remove = vme_user_remove,
590 static int __init vme_user_init(void)
594 struct vme_device_id *ids;
596 printk(KERN_INFO "VME User Space Access Driver\n");
599 printk(KERN_ERR "%s: No cards, skipping registration\n",
604 /* Let's start by supporting one bus, we can support more than one
605 * in future revisions if that ever becomes necessary.
607 if (bus_num > USER_BUS_MAX) {
608 printk(KERN_ERR "%s: Driver only able to handle %d buses\n",
609 driver_name, USER_BUS_MAX);
610 bus_num = USER_BUS_MAX;
614 /* Dynamically create the bind table based on module parameters */
615 ids = kmalloc(sizeof(struct vme_device_id) * (bus_num + 1), GFP_KERNEL);
617 printk(KERN_ERR "%s: Unable to allocate ID table\n",
622 memset(ids, 0, (sizeof(struct vme_device_id) * (bus_num + 1)));
624 for (i = 0; i < bus_num; i++) {
627 * We register the driver against the slot occupied by *this*
628 * card, since it's really a low level way of controlling
631 ids[i].slot = VME_SLOT_CURRENT;
634 vme_user_driver.bind_table = ids;
636 retval = vme_register_driver(&vme_user_driver);
642 vme_unregister_driver(&vme_user_driver);
651 * In this simple access driver, the old behaviour is being preserved as much
652 * as practical. We will therefore reserve the buffers and request the images
653 * here so that we don't have to do it later.
655 static int __init vme_user_probe(struct device *dev, int cur_bus, int cur_slot)
660 /* Save pointer to the bridge device */
661 if (vme_user_bridge != NULL) {
662 printk(KERN_ERR "%s: Driver can only be loaded for 1 device\n",
667 vme_user_bridge = dev;
669 /* Initialise descriptors */
670 for (i = 0; i < VME_DEVS; i++) {
671 image[i].kern_buf = NULL;
672 image[i].pci_buf = 0;
673 init_MUTEX(&(image[i].sem));
674 image[i].device = NULL;
675 image[i].resource = NULL;
679 /* Initialise statistics counters */
682 /* Assign major and minor numbers for the driver */
683 err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS,
686 printk(KERN_WARNING "%s: Error getting Major Number %d for "
687 "driver.\n", driver_name, VME_MAJOR);
691 /* Register the driver as a char device */
692 vme_user_cdev = cdev_alloc();
693 vme_user_cdev->ops = &vme_user_fops;
694 vme_user_cdev->owner = THIS_MODULE;
695 err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS);
697 printk(KERN_WARNING "%s: cdev_all failed\n", driver_name);
701 /* Request slave resources and allocate buffers (128kB wide) */
702 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
703 /* XXX Need to properly request attributes */
704 /* For ca91cx42 bridge there are only two slave windows
705 * supporting A16 addressing, so we request A24 supported
708 image[i].resource = vme_slave_request(vme_user_bridge,
710 if (image[i].resource == NULL) {
711 printk(KERN_WARNING "Unable to allocate slave "
715 image[i].size_buf = PCI_BUF_SIZE;
716 image[i].kern_buf = vme_alloc_consistent(image[i].resource,
717 image[i].size_buf, &(image[i].pci_buf));
718 if (image[i].kern_buf == NULL) {
719 printk(KERN_WARNING "Unable to allocate memory for "
721 image[i].pci_buf = 0;
722 vme_slave_free(image[i].resource);
729 * Request master resources allocate page sized buffers for small
732 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
733 /* XXX Need to properly request attributes */
734 image[i].resource = vme_master_request(vme_user_bridge,
735 VME_A32, VME_SCT, VME_D32);
736 if (image[i].resource == NULL) {
737 printk(KERN_WARNING "Unable to allocate master "
741 image[i].size_buf = PCI_BUF_SIZE;
742 image[i].kern_buf = kmalloc(image[i].size_buf, GFP_KERNEL);
743 if (image[i].kern_buf == NULL) {
744 printk(KERN_WARNING "Unable to allocate memory for "
745 "master window buffers\n");
751 /* Create sysfs entries - on udev systems this creates the dev files */
752 vme_user_sysfs_class = class_create(THIS_MODULE, driver_name);
753 if (IS_ERR(vme_user_sysfs_class)) {
754 printk(KERN_ERR "Error creating vme_user class.\n");
755 err = PTR_ERR(vme_user_sysfs_class);
759 /* Add sysfs Entries */
760 for (i=0; i<VME_DEVS; i++) {
763 sprintf(name,"bus/vme/m%%d");
766 sprintf(name,"bus/vme/ctl");
769 sprintf(name,"bus/vme/s%%d");
778 device_create(vme_user_sysfs_class, NULL,
779 MKDEV(VME_MAJOR, i), NULL, name,
780 (type[i] == SLAVE_MINOR)? i - (MASTER_MAX + 1) : i);
781 if (IS_ERR(image[i].device)) {
782 printk("%s: Error creating sysfs device\n",
784 err = PTR_ERR(image[i].device);
791 /* Ensure counter set correcty to destroy all sysfs devices */
796 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
798 class_destroy(vme_user_sysfs_class);
800 /* Ensure counter set correcty to unalloc all master windows */
803 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++)
804 kfree(image[i].kern_buf);
806 while (i > MASTER_MINOR) {
808 vme_master_free(image[i].resource);
812 * Ensure counter set correcty to unalloc all slave windows and buffers
816 while (i > SLAVE_MINOR) {
818 vme_slave_free(image[i].resource);
822 cdev_del(vme_user_cdev);
824 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
830 static int __exit vme_user_remove(struct device *dev, int cur_bus, int cur_slot)
834 /* Remove sysfs Entries */
835 for(i=0; i<VME_DEVS; i++) {
836 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
838 class_destroy(vme_user_sysfs_class);
840 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++)
841 kfree(image[i].kern_buf);
843 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
844 vme_slave_set(image[i].resource, 0, 0, 0, 0, VME_A32, 0);
845 vme_slave_free(image[i].resource);
849 /* Unregister device driver */
850 cdev_del(vme_user_cdev);
852 /* Unregiser the major and minor device numbers */
853 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
858 static void __exit vme_user_exit(void)
860 vme_unregister_driver(&vme_user_driver);
862 kfree(vme_user_driver.bind_table);
866 MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the driver is connected");
867 module_param_array(bus, int, &bus_num, 0);
869 MODULE_DESCRIPTION("VME User Space Access Driver");
870 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
871 MODULE_LICENSE("GPL");
873 module_init(vme_user_init);
874 module_exit(vme_user_exit);