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/atomic.h>
21 #include <linux/cdev.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/ioctl.h>
28 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/pagemap.h>
32 #include <linux/pci.h>
33 #include <linux/mutex.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/syscalls.h>
37 #include <linux/types.h>
40 #include <linux/uaccess.h>
41 #include <linux/vme.h>
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 mmap_count; /* Number of current mmap's */
105 static struct image_desc image[VME_DEVS];
107 static struct cdev *vme_user_cdev; /* Character device */
108 static struct class *vme_user_sysfs_class; /* Sysfs class */
109 static struct vme_dev *vme_user_bridge; /* Pointer to user device */
111 static const int type[VME_DEVS] = { MASTER_MINOR, MASTER_MINOR,
112 MASTER_MINOR, MASTER_MINOR,
113 SLAVE_MINOR, SLAVE_MINOR,
114 SLAVE_MINOR, SLAVE_MINOR,
118 struct vme_user_vma_priv {
123 static ssize_t resource_to_user(int minor, char __user *buf, size_t count,
128 if (count > image[minor].size_buf)
129 count = image[minor].size_buf;
131 copied = vme_master_read(image[minor].resource, image[minor].kern_buf,
136 if (__copy_to_user(buf, image[minor].kern_buf, (unsigned long)copied))
142 static ssize_t resource_from_user(unsigned int minor, const char __user *buf,
143 size_t count, loff_t *ppos)
145 if (count > image[minor].size_buf)
146 count = image[minor].size_buf;
148 if (__copy_from_user(image[minor].kern_buf, buf, (unsigned long)count))
151 return vme_master_write(image[minor].resource, image[minor].kern_buf,
155 static ssize_t buffer_to_user(unsigned int minor, char __user *buf,
156 size_t count, loff_t *ppos)
160 image_ptr = image[minor].kern_buf + *ppos;
161 if (__copy_to_user(buf, image_ptr, (unsigned long)count))
167 static ssize_t buffer_from_user(unsigned int minor, const char __user *buf,
168 size_t count, loff_t *ppos)
172 image_ptr = image[minor].kern_buf + *ppos;
173 if (__copy_from_user(image_ptr, buf, (unsigned long)count))
179 static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
182 unsigned int minor = MINOR(file_inode(file)->i_rdev);
186 if (minor == CONTROL_MINOR)
189 mutex_lock(&image[minor].mutex);
191 /* XXX Do we *really* want this helper - we can use vme_*_get ? */
192 image_size = vme_get_size(image[minor].resource);
194 /* Ensure we are starting at a valid location */
195 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
196 mutex_unlock(&image[minor].mutex);
200 /* Ensure not reading past end of the image */
201 if (*ppos + count > image_size)
202 count = image_size - *ppos;
204 switch (type[minor]) {
206 retval = resource_to_user(minor, buf, count, ppos);
209 retval = buffer_to_user(minor, buf, count, ppos);
215 mutex_unlock(&image[minor].mutex);
222 static ssize_t vme_user_write(struct file *file, const char __user *buf,
223 size_t count, loff_t *ppos)
225 unsigned int minor = MINOR(file_inode(file)->i_rdev);
229 if (minor == CONTROL_MINOR)
232 mutex_lock(&image[minor].mutex);
234 image_size = vme_get_size(image[minor].resource);
236 /* Ensure we are starting at a valid location */
237 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
238 mutex_unlock(&image[minor].mutex);
242 /* Ensure not reading past end of the image */
243 if (*ppos + count > image_size)
244 count = image_size - *ppos;
246 switch (type[minor]) {
248 retval = resource_from_user(minor, buf, count, ppos);
251 retval = buffer_from_user(minor, buf, count, ppos);
257 mutex_unlock(&image[minor].mutex);
265 static loff_t vme_user_llseek(struct file *file, loff_t off, int whence)
267 unsigned int minor = MINOR(file_inode(file)->i_rdev);
271 switch (type[minor]) {
274 mutex_lock(&image[minor].mutex);
275 image_size = vme_get_size(image[minor].resource);
276 res = fixed_size_llseek(file, off, whence, image_size);
277 mutex_unlock(&image[minor].mutex);
285 * The ioctls provided by the old VME access method (the one at vmelinux.org)
286 * are most certainly wrong as the effectively push the registers layout
287 * through to user space. Given that the VME core can handle multiple bridges,
288 * with different register layouts this is most certainly not the way to go.
290 * We aren't using the structures defined in the Motorola driver either - these
291 * are also quite low level, however we should use the definitions that have
292 * already been defined.
294 static int vme_user_ioctl(struct inode *inode, struct file *file,
295 unsigned int cmd, unsigned long arg)
297 struct vme_master master;
298 struct vme_slave slave;
299 struct vme_irq_id irq_req;
300 unsigned long copied;
301 unsigned int minor = MINOR(inode->i_rdev);
304 void __user *argp = (void __user *)arg;
306 switch (type[minor]) {
310 copied = copy_from_user(&irq_req, argp,
311 sizeof(struct vme_irq_id));
313 pr_warn("Partial copy from userspace\n");
317 return vme_irq_generate(vme_user_bridge,
325 memset(&master, 0, sizeof(struct vme_master));
327 /* XXX We do not want to push aspace, cycle and width
328 * to userspace as they are
330 retval = vme_master_get(image[minor].resource,
333 &master.size, &master.aspace,
334 &master.cycle, &master.dwidth);
336 copied = copy_to_user(argp, &master,
337 sizeof(struct vme_master));
339 pr_warn("Partial copy to userspace\n");
347 if (image[minor].mmap_count != 0) {
348 pr_warn("Can't adjust mapped window\n");
352 copied = copy_from_user(&master, argp, sizeof(master));
354 pr_warn("Partial copy from userspace\n");
358 /* XXX We do not want to push aspace, cycle and width
359 * to userspace as they are
361 return vme_master_set(image[minor].resource,
362 master.enable, master.vme_addr, master.size,
363 master.aspace, master.cycle, master.dwidth);
371 memset(&slave, 0, sizeof(struct vme_slave));
373 /* XXX We do not want to push aspace, cycle and width
374 * to userspace as they are
376 retval = vme_slave_get(image[minor].resource,
377 &slave.enable, &slave.vme_addr,
378 &slave.size, &pci_addr,
379 &slave.aspace, &slave.cycle);
381 copied = copy_to_user(argp, &slave,
382 sizeof(struct vme_slave));
384 pr_warn("Partial copy to userspace\n");
392 copied = copy_from_user(&slave, argp, sizeof(slave));
394 pr_warn("Partial copy from userspace\n");
398 /* XXX We do not want to push aspace, cycle and width
399 * to userspace as they are
401 return vme_slave_set(image[minor].resource,
402 slave.enable, slave.vme_addr, slave.size,
403 image[minor].pci_buf, slave.aspace,
415 vme_user_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
418 struct inode *inode = file_inode(file);
419 unsigned int minor = MINOR(inode->i_rdev);
421 mutex_lock(&image[minor].mutex);
422 ret = vme_user_ioctl(inode, file, cmd, arg);
423 mutex_unlock(&image[minor].mutex);
428 static void vme_user_vm_open(struct vm_area_struct *vma)
430 struct vme_user_vma_priv *vma_priv = vma->vm_private_data;
432 atomic_inc(&vma_priv->refcnt);
435 static void vme_user_vm_close(struct vm_area_struct *vma)
437 struct vme_user_vma_priv *vma_priv = vma->vm_private_data;
438 unsigned int minor = vma_priv->minor;
440 if (!atomic_dec_and_test(&vma_priv->refcnt))
443 mutex_lock(&image[minor].mutex);
444 image[minor].mmap_count--;
445 mutex_unlock(&image[minor].mutex);
450 static const struct vm_operations_struct vme_user_vm_ops = {
451 .open = vme_user_vm_open,
452 .close = vme_user_vm_close,
455 static int vme_user_master_mmap(unsigned int minor, struct vm_area_struct *vma)
458 struct vme_user_vma_priv *vma_priv;
460 mutex_lock(&image[minor].mutex);
462 err = vme_master_mmap(image[minor].resource, vma);
464 mutex_unlock(&image[minor].mutex);
468 vma_priv = kmalloc(sizeof(*vma_priv), GFP_KERNEL);
470 mutex_unlock(&image[minor].mutex);
474 vma_priv->minor = minor;
475 atomic_set(&vma_priv->refcnt, 1);
476 vma->vm_ops = &vme_user_vm_ops;
477 vma->vm_private_data = vma_priv;
479 image[minor].mmap_count++;
481 mutex_unlock(&image[minor].mutex);
486 static int vme_user_mmap(struct file *file, struct vm_area_struct *vma)
488 unsigned int minor = MINOR(file_inode(file)->i_rdev);
490 if (type[minor] == MASTER_MINOR)
491 return vme_user_master_mmap(minor, vma);
496 static const struct file_operations vme_user_fops = {
497 .read = vme_user_read,
498 .write = vme_user_write,
499 .llseek = vme_user_llseek,
500 .unlocked_ioctl = vme_user_unlocked_ioctl,
501 .compat_ioctl = vme_user_unlocked_ioctl,
502 .mmap = vme_user_mmap,
505 static int vme_user_match(struct vme_dev *vdev)
509 int cur_bus = vme_bus_num(vdev);
510 int cur_slot = vme_slot_num(vdev);
512 for (i = 0; i < bus_num; i++)
513 if ((cur_bus == bus[i]) && (cur_slot == vdev->num))
520 * In this simple access driver, the old behaviour is being preserved as much
521 * as practical. We will therefore reserve the buffers and request the images
522 * here so that we don't have to do it later.
524 static int vme_user_probe(struct vme_dev *vdev)
529 /* Save pointer to the bridge device */
530 if (vme_user_bridge) {
531 dev_err(&vdev->dev, "Driver can only be loaded for 1 device\n");
535 vme_user_bridge = vdev;
537 /* Initialise descriptors */
538 for (i = 0; i < VME_DEVS; i++) {
539 image[i].kern_buf = NULL;
540 image[i].pci_buf = 0;
541 mutex_init(&image[i].mutex);
542 image[i].device = NULL;
543 image[i].resource = NULL;
546 /* Assign major and minor numbers for the driver */
547 err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS,
550 dev_warn(&vdev->dev, "Error getting Major Number %d for driver.\n",
555 /* Register the driver as a char device */
556 vme_user_cdev = cdev_alloc();
557 if (!vme_user_cdev) {
561 vme_user_cdev->ops = &vme_user_fops;
562 vme_user_cdev->owner = THIS_MODULE;
563 err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS);
567 /* Request slave resources and allocate buffers (128kB wide) */
568 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
569 /* XXX Need to properly request attributes */
570 /* For ca91cx42 bridge there are only two slave windows
571 * supporting A16 addressing, so we request A24 supported
574 image[i].resource = vme_slave_request(vme_user_bridge,
576 if (!image[i].resource) {
578 "Unable to allocate slave resource\n");
582 image[i].size_buf = PCI_BUF_SIZE;
583 image[i].kern_buf = vme_alloc_consistent(image[i].resource,
584 image[i].size_buf, &image[i].pci_buf);
585 if (!image[i].kern_buf) {
587 "Unable to allocate memory for buffer\n");
588 image[i].pci_buf = 0;
589 vme_slave_free(image[i].resource);
596 * Request master resources allocate page sized buffers for small
599 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
600 /* XXX Need to properly request attributes */
601 image[i].resource = vme_master_request(vme_user_bridge,
602 VME_A32, VME_SCT, VME_D32);
603 if (!image[i].resource) {
605 "Unable to allocate master resource\n");
609 image[i].size_buf = PCI_BUF_SIZE;
610 image[i].kern_buf = kmalloc(image[i].size_buf, GFP_KERNEL);
611 if (!image[i].kern_buf) {
613 vme_master_free(image[i].resource);
618 /* Create sysfs entries - on udev systems this creates the dev files */
619 vme_user_sysfs_class = class_create(THIS_MODULE, driver_name);
620 if (IS_ERR(vme_user_sysfs_class)) {
621 dev_err(&vdev->dev, "Error creating vme_user class.\n");
622 err = PTR_ERR(vme_user_sysfs_class);
626 /* Add sysfs Entries */
627 for (i = 0; i < VME_DEVS; i++) {
632 name = "bus/vme/m%d";
635 name = "bus/vme/ctl";
638 name = "bus/vme/s%d";
645 num = (type[i] == SLAVE_MINOR) ? i - (MASTER_MAX + 1) : i;
646 image[i].device = device_create(vme_user_sysfs_class, NULL,
647 MKDEV(VME_MAJOR, i), NULL, name, num);
648 if (IS_ERR(image[i].device)) {
649 dev_info(&vdev->dev, "Error creating sysfs device\n");
650 err = PTR_ERR(image[i].device);
660 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
662 class_destroy(vme_user_sysfs_class);
664 /* Ensure counter set correcty to unalloc all master windows */
667 while (i > MASTER_MINOR) {
669 kfree(image[i].kern_buf);
670 vme_master_free(image[i].resource);
674 * Ensure counter set correcty to unalloc all slave windows and buffers
678 while (i > SLAVE_MINOR) {
680 vme_free_consistent(image[i].resource, image[i].size_buf,
681 image[i].kern_buf, image[i].pci_buf);
682 vme_slave_free(image[i].resource);
685 cdev_del(vme_user_cdev);
687 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
693 static int vme_user_remove(struct vme_dev *dev)
697 /* Remove sysfs Entries */
698 for (i = 0; i < VME_DEVS; i++) {
699 mutex_destroy(&image[i].mutex);
700 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
702 class_destroy(vme_user_sysfs_class);
704 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
705 kfree(image[i].kern_buf);
706 vme_master_free(image[i].resource);
709 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
710 vme_slave_set(image[i].resource, 0, 0, 0, 0, VME_A32, 0);
711 vme_free_consistent(image[i].resource, image[i].size_buf,
712 image[i].kern_buf, image[i].pci_buf);
713 vme_slave_free(image[i].resource);
716 /* Unregister device driver */
717 cdev_del(vme_user_cdev);
719 /* Unregiser the major and minor device numbers */
720 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
725 static struct vme_driver vme_user_driver = {
727 .match = vme_user_match,
728 .probe = vme_user_probe,
729 .remove = vme_user_remove,
732 static int __init vme_user_init(void)
736 pr_info("VME User Space Access Driver\n");
739 pr_err("No cards, skipping registration\n");
744 /* Let's start by supporting one bus, we can support more than one
745 * in future revisions if that ever becomes necessary.
747 if (bus_num > VME_USER_BUS_MAX) {
748 pr_err("Driver only able to handle %d buses\n",
750 bus_num = VME_USER_BUS_MAX;
754 * Here we just register the maximum number of devices we can and
755 * leave vme_user_match() to allow only 1 to go through to probe().
756 * This way, if we later want to allow multiple user access devices,
757 * we just change the code in vme_user_match().
759 retval = vme_register_driver(&vme_user_driver, VME_MAX_SLOTS);
770 static void __exit vme_user_exit(void)
772 vme_unregister_driver(&vme_user_driver);
775 MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the driver is connected");
776 module_param_array(bus, int, &bus_num, 0);
778 MODULE_DESCRIPTION("VME User Space Access Driver");
779 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
780 MODULE_LICENSE("GPL");
782 module_init(vme_user_init);
783 module_exit(vme_user_exit);