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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/cdev.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/ioctl.h>
27 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/pagemap.h>
31 #include <linux/pci.h>
32 #include <linux/mutex.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35 #include <linux/syscalls.h>
36 #include <linux/types.h>
39 #include <linux/uaccess.h>
40 #include <linux/vme.h>
44 static DEFINE_MUTEX(vme_user_mutex);
45 static const char driver_name[] = "vme_user";
47 static int bus[VME_USER_BUS_MAX];
48 static unsigned int bus_num;
50 /* Currently Documentation/devices.txt defines the following for VME:
53 * 0 = /dev/bus/vme/m0 First master image
54 * 1 = /dev/bus/vme/m1 Second master image
55 * 2 = /dev/bus/vme/m2 Third master image
56 * 3 = /dev/bus/vme/m3 Fourth master image
57 * 4 = /dev/bus/vme/s0 First slave image
58 * 5 = /dev/bus/vme/s1 Second slave image
59 * 6 = /dev/bus/vme/s2 Third slave image
60 * 7 = /dev/bus/vme/s3 Fourth slave image
61 * 8 = /dev/bus/vme/ctl Control
63 * It is expected that all VME bus drivers will use the
64 * same interface. For interface documentation see
65 * http://www.vmelinux.org/.
67 * However the VME driver at http://www.vmelinux.org/ is rather old and doesn't
68 * even support the tsi148 chipset (which has 8 master and 8 slave windows).
69 * We'll run with this for now as far as possible, however it probably makes
70 * sense to get rid of the old mappings and just do everything dynamically.
72 * So for now, we'll restrict the driver to providing 4 masters and 4 slaves as
73 * defined above and try to support at least some of the interface from
74 * http://www.vmelinux.org/ as an alternative the driver can be written
75 * providing a saner interface later.
77 * The vmelinux.org driver never supported slave images, the devices reserved
78 * for slaves were repurposed to support all 8 master images on the UniverseII!
79 * We shall support 4 masters and 4 slaves with this driver.
81 #define VME_MAJOR 221 /* VME Major Device Number */
82 #define VME_DEVS 9 /* Number of dev entries */
84 #define MASTER_MINOR 0
88 #define CONTROL_MINOR 8
90 #define PCI_BUF_SIZE 0x20000 /* Size of one slave image buffer */
93 * Structure to handle image related parameters.
96 void *kern_buf; /* Buffer address in kernel space */
97 dma_addr_t pci_buf; /* Buffer address in PCI address space */
98 unsigned long long size_buf; /* Buffer size */
99 struct mutex mutex; /* Mutex for locking image */
100 struct device *device; /* Sysfs device */
101 struct vme_resource *resource; /* VME resource */
102 int users; /* Number of current users */
104 static struct image_desc image[VME_DEVS];
106 struct driver_stats {
108 unsigned long writes;
109 unsigned long ioctls;
112 unsigned long dmaerrors;
113 unsigned long timeouts;
114 unsigned long external;
116 static struct driver_stats statistics;
118 static struct cdev *vme_user_cdev; /* Character device */
119 static struct class *vme_user_sysfs_class; /* Sysfs class */
120 static struct vme_dev *vme_user_bridge; /* Pointer to user device */
123 static const int type[VME_DEVS] = { MASTER_MINOR, MASTER_MINOR,
124 MASTER_MINOR, MASTER_MINOR,
125 SLAVE_MINOR, SLAVE_MINOR,
126 SLAVE_MINOR, SLAVE_MINOR,
131 static int vme_user_open(struct inode *, struct file *);
132 static int vme_user_release(struct inode *, struct file *);
133 static ssize_t vme_user_read(struct file *, char __user *, size_t, loff_t *);
134 static ssize_t vme_user_write(struct file *, const char __user *, size_t,
136 static loff_t vme_user_llseek(struct file *, loff_t, int);
137 static long vme_user_unlocked_ioctl(struct file *, unsigned int, unsigned long);
139 static int vme_user_match(struct vme_dev *);
140 static int vme_user_probe(struct vme_dev *);
141 static int vme_user_remove(struct vme_dev *);
143 static const struct file_operations vme_user_fops = {
144 .open = vme_user_open,
145 .release = vme_user_release,
146 .read = vme_user_read,
147 .write = vme_user_write,
148 .llseek = vme_user_llseek,
149 .unlocked_ioctl = vme_user_unlocked_ioctl,
150 .compat_ioctl = vme_user_unlocked_ioctl,
155 * Reset all the statistic counters
157 static void reset_counters(void)
159 statistics.reads = 0;
160 statistics.writes = 0;
161 statistics.ioctls = 0;
163 statistics.berrs = 0;
164 statistics.dmaerrors = 0;
165 statistics.timeouts = 0;
168 static int vme_user_open(struct inode *inode, struct file *file)
171 unsigned int minor = MINOR(inode->i_rdev);
173 mutex_lock(&image[minor].mutex);
174 /* Allow device to be opened if a resource is needed and allocated. */
175 if (minor < CONTROL_MINOR && image[minor].resource == NULL) {
176 pr_err("No resources allocated for device\n");
181 /* Increment user count */
182 image[minor].users++;
184 mutex_unlock(&image[minor].mutex);
189 mutex_unlock(&image[minor].mutex);
194 static int vme_user_release(struct inode *inode, struct file *file)
196 unsigned int minor = MINOR(inode->i_rdev);
198 mutex_lock(&image[minor].mutex);
200 /* Decrement user count */
201 image[minor].users--;
203 mutex_unlock(&image[minor].mutex);
209 * We are going ot alloc a page during init per window for small transfers.
210 * Small transfers will go VME -> buffer -> user space. Larger (more than a
211 * page) transfers will lock the user space buffer into memory and then
212 * transfer the data directly into the user space buffers.
214 static ssize_t resource_to_user(int minor, char __user *buf, size_t count,
220 if (count <= image[minor].size_buf) {
221 /* We copy to kernel buffer */
222 copied = vme_master_read(image[minor].resource,
223 image[minor].kern_buf, count, *ppos);
227 retval = __copy_to_user(buf, image[minor].kern_buf,
228 (unsigned long)copied);
230 copied = (copied - retval);
231 pr_info("User copy failed\n");
236 /* XXX Need to write this */
237 pr_info("Currently don't support large transfers\n");
238 /* Map in pages from userspace */
240 /* Call vme_master_read to do the transfer */
248 * We are going to alloc a page during init per window for small transfers.
249 * Small transfers will go user space -> buffer -> VME. Larger (more than a
250 * page) transfers will lock the user space buffer into memory and then
251 * transfer the data directly from the user space buffers out to VME.
253 static ssize_t resource_from_user(unsigned int minor, const char __user *buf,
254 size_t count, loff_t *ppos)
259 if (count <= image[minor].size_buf) {
260 retval = __copy_from_user(image[minor].kern_buf, buf,
261 (unsigned long)count);
263 copied = (copied - retval);
267 copied = vme_master_write(image[minor].resource,
268 image[minor].kern_buf, copied, *ppos);
270 /* XXX Need to write this */
271 pr_info("Currently don't support large transfers\n");
272 /* Map in pages from userspace */
274 /* Call vme_master_write to do the transfer */
281 static ssize_t buffer_to_user(unsigned int minor, char __user *buf,
282 size_t count, loff_t *ppos)
287 image_ptr = image[minor].kern_buf + *ppos;
289 retval = __copy_to_user(buf, image_ptr, (unsigned long)count);
291 retval = (count - retval);
292 pr_warn("Partial copy to userspace\n");
296 /* Return number of bytes successfully read */
300 static ssize_t buffer_from_user(unsigned int minor, const char __user *buf,
301 size_t count, loff_t *ppos)
306 image_ptr = image[minor].kern_buf + *ppos;
308 retval = __copy_from_user(image_ptr, buf, (unsigned long)count);
310 retval = (count - retval);
311 pr_warn("Partial copy to userspace\n");
315 /* Return number of bytes successfully read */
319 static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
322 unsigned int minor = MINOR(file_inode(file)->i_rdev);
327 if (minor == CONTROL_MINOR)
330 mutex_lock(&image[minor].mutex);
332 /* XXX Do we *really* want this helper - we can use vme_*_get ? */
333 image_size = vme_get_size(image[minor].resource);
335 /* Ensure we are starting at a valid location */
336 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
337 mutex_unlock(&image[minor].mutex);
341 /* Ensure not reading past end of the image */
342 if (*ppos + count > image_size)
343 okcount = image_size - *ppos;
347 switch (type[minor]) {
349 retval = resource_to_user(minor, buf, okcount, ppos);
352 retval = buffer_to_user(minor, buf, okcount, ppos);
358 mutex_unlock(&image[minor].mutex);
365 static ssize_t vme_user_write(struct file *file, const char __user *buf,
366 size_t count, loff_t *ppos)
368 unsigned int minor = MINOR(file_inode(file)->i_rdev);
373 if (minor == CONTROL_MINOR)
376 mutex_lock(&image[minor].mutex);
378 image_size = vme_get_size(image[minor].resource);
380 /* Ensure we are starting at a valid location */
381 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
382 mutex_unlock(&image[minor].mutex);
386 /* Ensure not reading past end of the image */
387 if (*ppos + count > image_size)
388 okcount = image_size - *ppos;
392 switch (type[minor]) {
394 retval = resource_from_user(minor, buf, okcount, ppos);
397 retval = buffer_from_user(minor, buf, okcount, ppos);
403 mutex_unlock(&image[minor].mutex);
411 static loff_t vme_user_llseek(struct file *file, loff_t off, int whence)
413 loff_t absolute = -1;
414 unsigned int minor = MINOR(file_inode(file)->i_rdev);
417 if (minor == CONTROL_MINOR)
420 mutex_lock(&image[minor].mutex);
421 image_size = vme_get_size(image[minor].resource);
428 absolute = file->f_pos + off;
431 absolute = image_size + off;
434 mutex_unlock(&image[minor].mutex);
439 if ((absolute < 0) || (absolute >= image_size)) {
440 mutex_unlock(&image[minor].mutex);
444 file->f_pos = absolute;
446 mutex_unlock(&image[minor].mutex);
452 * The ioctls provided by the old VME access method (the one at vmelinux.org)
453 * are most certainly wrong as the effectively push the registers layout
454 * through to user space. Given that the VME core can handle multiple bridges,
455 * with different register layouts this is most certainly not the way to go.
457 * We aren't using the structures defined in the Motorola driver either - these
458 * are also quite low level, however we should use the definitions that have
459 * already been defined.
461 static int vme_user_ioctl(struct inode *inode, struct file *file,
462 unsigned int cmd, unsigned long arg)
464 struct vme_master master;
465 struct vme_slave slave;
466 struct vme_irq_id irq_req;
467 unsigned long copied;
468 unsigned int minor = MINOR(inode->i_rdev);
471 void __user *argp = (void __user *)arg;
475 switch (type[minor]) {
479 copied = copy_from_user(&irq_req, argp,
480 sizeof(struct vme_irq_id));
482 pr_warn("Partial copy from userspace\n");
486 retval = vme_irq_generate(vme_user_bridge,
496 memset(&master, 0, sizeof(struct vme_master));
498 /* XXX We do not want to push aspace, cycle and width
499 * to userspace as they are
501 retval = vme_master_get(image[minor].resource,
502 &master.enable, &master.vme_addr,
503 &master.size, &master.aspace,
504 &master.cycle, &master.dwidth);
506 copied = copy_to_user(argp, &master,
507 sizeof(struct vme_master));
509 pr_warn("Partial copy to userspace\n");
518 copied = copy_from_user(&master, argp, sizeof(master));
520 pr_warn("Partial copy from userspace\n");
524 /* XXX We do not want to push aspace, cycle and width
525 * to userspace as they are
527 return vme_master_set(image[minor].resource,
528 master.enable, master.vme_addr, master.size,
529 master.aspace, master.cycle, master.dwidth);
537 memset(&slave, 0, sizeof(struct vme_slave));
539 /* XXX We do not want to push aspace, cycle and width
540 * to userspace as they are
542 retval = vme_slave_get(image[minor].resource,
543 &slave.enable, &slave.vme_addr,
544 &slave.size, &pci_addr, &slave.aspace,
547 copied = copy_to_user(argp, &slave,
548 sizeof(struct vme_slave));
550 pr_warn("Partial copy to userspace\n");
559 copied = copy_from_user(&slave, argp, sizeof(slave));
561 pr_warn("Partial copy from userspace\n");
565 /* XXX We do not want to push aspace, cycle and width
566 * to userspace as they are
568 return vme_slave_set(image[minor].resource,
569 slave.enable, slave.vme_addr, slave.size,
570 image[minor].pci_buf, slave.aspace,
582 vme_user_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
586 mutex_lock(&vme_user_mutex);
587 ret = vme_user_ioctl(file_inode(file), file, cmd, arg);
588 mutex_unlock(&vme_user_mutex);
595 * Unallocate a previously allocated buffer
597 static void buf_unalloc(int num)
599 if (image[num].kern_buf) {
601 pr_debug("UniverseII:Releasing buffer at %p\n",
605 vme_free_consistent(image[num].resource, image[num].size_buf,
606 image[num].kern_buf, image[num].pci_buf);
608 image[num].kern_buf = NULL;
609 image[num].pci_buf = 0;
610 image[num].size_buf = 0;
614 pr_debug("UniverseII: Buffer not allocated\n");
619 static struct vme_driver vme_user_driver = {
621 .match = vme_user_match,
622 .probe = vme_user_probe,
623 .remove = vme_user_remove,
627 static int __init vme_user_init(void)
631 pr_info("VME User Space Access Driver\n");
634 pr_err("No cards, skipping registration\n");
639 /* Let's start by supporting one bus, we can support more than one
640 * in future revisions if that ever becomes necessary.
642 if (bus_num > VME_USER_BUS_MAX) {
643 pr_err("Driver only able to handle %d buses\n",
645 bus_num = VME_USER_BUS_MAX;
649 * Here we just register the maximum number of devices we can and
650 * leave vme_user_match() to allow only 1 to go through to probe().
651 * This way, if we later want to allow multiple user access devices,
652 * we just change the code in vme_user_match().
654 retval = vme_register_driver(&vme_user_driver, VME_MAX_SLOTS);
665 static int vme_user_match(struct vme_dev *vdev)
669 int cur_bus = vme_bus_num(vdev);
670 int cur_slot = vme_slot_num(vdev);
672 for (i = 0; i < bus_num; i++)
673 if ((cur_bus == bus[i]) && (cur_slot == vdev->num))
680 * In this simple access driver, the old behaviour is being preserved as much
681 * as practical. We will therefore reserve the buffers and request the images
682 * here so that we don't have to do it later.
684 static int vme_user_probe(struct vme_dev *vdev)
689 /* Save pointer to the bridge device */
690 if (vme_user_bridge != NULL) {
691 dev_err(&vdev->dev, "Driver can only be loaded for 1 device\n");
695 vme_user_bridge = vdev;
697 /* Initialise descriptors */
698 for (i = 0; i < VME_DEVS; i++) {
699 image[i].kern_buf = NULL;
700 image[i].pci_buf = 0;
701 mutex_init(&image[i].mutex);
702 image[i].device = NULL;
703 image[i].resource = NULL;
707 /* Initialise statistics counters */
710 /* Assign major and minor numbers for the driver */
711 err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS,
714 dev_warn(&vdev->dev, "Error getting Major Number %d for driver.\n",
719 /* Register the driver as a char device */
720 vme_user_cdev = cdev_alloc();
721 if (!vme_user_cdev) {
725 vme_user_cdev->ops = &vme_user_fops;
726 vme_user_cdev->owner = THIS_MODULE;
727 err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS);
729 dev_warn(&vdev->dev, "cdev_all failed\n");
733 /* Request slave resources and allocate buffers (128kB wide) */
734 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
735 /* XXX Need to properly request attributes */
736 /* For ca91cx42 bridge there are only two slave windows
737 * supporting A16 addressing, so we request A24 supported
740 image[i].resource = vme_slave_request(vme_user_bridge,
742 if (image[i].resource == NULL) {
744 "Unable to allocate slave resource\n");
748 image[i].size_buf = PCI_BUF_SIZE;
749 image[i].kern_buf = vme_alloc_consistent(image[i].resource,
750 image[i].size_buf, &image[i].pci_buf);
751 if (image[i].kern_buf == NULL) {
753 "Unable to allocate memory for buffer\n");
754 image[i].pci_buf = 0;
755 vme_slave_free(image[i].resource);
762 * Request master resources allocate page sized buffers for small
765 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
766 /* XXX Need to properly request attributes */
767 image[i].resource = vme_master_request(vme_user_bridge,
768 VME_A32, VME_SCT, VME_D32);
769 if (image[i].resource == NULL) {
771 "Unable to allocate master resource\n");
775 image[i].size_buf = PCI_BUF_SIZE;
776 image[i].kern_buf = kmalloc(image[i].size_buf, GFP_KERNEL);
777 if (image[i].kern_buf == NULL) {
783 /* Create sysfs entries - on udev systems this creates the dev files */
784 vme_user_sysfs_class = class_create(THIS_MODULE, driver_name);
785 if (IS_ERR(vme_user_sysfs_class)) {
786 dev_err(&vdev->dev, "Error creating vme_user class.\n");
787 err = PTR_ERR(vme_user_sysfs_class);
791 /* Add sysfs Entries */
792 for (i = 0; i < VME_DEVS; i++) {
796 sprintf(name, "bus/vme/m%%d");
799 sprintf(name, "bus/vme/ctl");
802 sprintf(name, "bus/vme/s%%d");
810 num = (type[i] == SLAVE_MINOR) ? i - (MASTER_MAX + 1) : i;
811 image[i].device = device_create(vme_user_sysfs_class, NULL,
812 MKDEV(VME_MAJOR, i), NULL, name, num);
813 if (IS_ERR(image[i].device)) {
814 dev_info(&vdev->dev, "Error creating sysfs device\n");
815 err = PTR_ERR(image[i].device);
822 /* Ensure counter set correcty to destroy all sysfs devices */
827 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
829 class_destroy(vme_user_sysfs_class);
831 /* Ensure counter set correcty to unalloc all master windows */
834 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++)
835 kfree(image[i].kern_buf);
837 while (i > MASTER_MINOR) {
839 vme_master_free(image[i].resource);
843 * Ensure counter set correcty to unalloc all slave windows and buffers
847 while (i > SLAVE_MINOR) {
850 vme_slave_free(image[i].resource);
853 cdev_del(vme_user_cdev);
855 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
861 static int vme_user_remove(struct vme_dev *dev)
865 /* Remove sysfs Entries */
866 for (i = 0; i < VME_DEVS; i++) {
867 mutex_destroy(&image[i].mutex);
868 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
870 class_destroy(vme_user_sysfs_class);
872 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
873 kfree(image[i].kern_buf);
874 vme_master_free(image[i].resource);
877 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
878 vme_slave_set(image[i].resource, 0, 0, 0, 0, VME_A32, 0);
880 vme_slave_free(image[i].resource);
883 /* Unregister device driver */
884 cdev_del(vme_user_cdev);
886 /* Unregiser the major and minor device numbers */
887 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
892 static void __exit vme_user_exit(void)
894 vme_unregister_driver(&vme_user_driver);
898 MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the driver is connected");
899 module_param_array(bus, int, &bus_num, 0);
901 MODULE_DESCRIPTION("VME User Space Access Driver");
902 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
903 MODULE_LICENSE("GPL");
905 module_init(vme_user_init);
906 module_exit(vme_user_exit);