Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * VMEbus User access driver |
| 3 | * |
Martyn Welch | 66bd8db | 2010-02-18 15:12:52 +0000 | [diff] [blame] | 4 | * Author: Martyn Welch <martyn.welch@ge.com> |
| 5 | * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc. |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 6 | * |
| 7 | * Based on work by: |
| 8 | * Tom Armistead and Ajit Prem |
| 9 | * Copyright 2004 Motorola Inc. |
| 10 | * |
| 11 | * |
| 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. |
| 16 | */ |
| 17 | |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 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> |
| 28 | #include <linux/mm.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/pagemap.h> |
| 31 | #include <linux/pci.h> |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/syscalls.h> |
| 36 | #include <linux/types.h> |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 37 | |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 38 | #include <linux/io.h> |
| 39 | #include <linux/uaccess.h> |
Greg Kroah-Hartman | db3b9e9 | 2012-04-26 12:34:58 -0700 | [diff] [blame] | 40 | #include <linux/vme.h> |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 41 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 42 | #include "vme_user.h" |
| 43 | |
Arnd Bergmann | 8e2394a | 2010-07-11 23:18:52 +0200 | [diff] [blame] | 44 | static DEFINE_MUTEX(vme_user_mutex); |
Vincent Bossier | 584721c | 2011-06-03 10:07:39 +0100 | [diff] [blame] | 45 | static const char driver_name[] = "vme_user"; |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 46 | |
Manohar Vanga | 0a4b6b0 | 2011-09-26 11:27:18 +0200 | [diff] [blame] | 47 | static int bus[VME_USER_BUS_MAX]; |
Emilio G. Cota | c949231 | 2010-11-12 11:15:21 +0000 | [diff] [blame] | 48 | static unsigned int bus_num; |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 49 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 50 | /* Currently Documentation/devices.txt defines the following for VME: |
| 51 | * |
| 52 | * 221 char VME bus |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 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 |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 62 | * |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 63 | * It is expected that all VME bus drivers will use the |
| 64 | * same interface. For interface documentation see |
| 65 | * http://www.vmelinux.org/. |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 66 | * |
| 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). |
Justin P. Mattock | 9560533 | 2012-08-13 10:28:22 -0700 | [diff] [blame] | 69 | * We'll run with this for now as far as possible, however it probably makes |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 70 | * sense to get rid of the old mappings and just do everything dynamically. |
| 71 | * |
| 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 |
Justin P. Mattock | 9560533 | 2012-08-13 10:28:22 -0700 | [diff] [blame] | 74 | * http://www.vmelinux.org/ as an alternative the driver can be written |
| 75 | * providing a saner interface later. |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 76 | * |
| 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. |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 80 | */ |
| 81 | #define VME_MAJOR 221 /* VME Major Device Number */ |
| 82 | #define VME_DEVS 9 /* Number of dev entries */ |
| 83 | |
| 84 | #define MASTER_MINOR 0 |
| 85 | #define MASTER_MAX 3 |
| 86 | #define SLAVE_MINOR 4 |
| 87 | #define SLAVE_MAX 7 |
| 88 | #define CONTROL_MINOR 8 |
| 89 | |
| 90 | #define PCI_BUF_SIZE 0x20000 /* Size of one slave image buffer */ |
| 91 | |
| 92 | /* |
| 93 | * Structure to handle image related parameters. |
| 94 | */ |
Vincent Bossier | 584721c | 2011-06-03 10:07:39 +0100 | [diff] [blame] | 95 | struct image_desc { |
Emilio G. Cota | 0a81a0f | 2010-11-12 11:15:27 +0000 | [diff] [blame] | 96 | void *kern_buf; /* Buffer address in kernel space */ |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 97 | dma_addr_t pci_buf; /* Buffer address in PCI address space */ |
| 98 | unsigned long long size_buf; /* Buffer size */ |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 99 | struct mutex mutex; /* Mutex for locking image */ |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 100 | struct device *device; /* Sysfs device */ |
| 101 | struct vme_resource *resource; /* VME resource */ |
| 102 | int users; /* Number of current users */ |
Vincent Bossier | 584721c | 2011-06-03 10:07:39 +0100 | [diff] [blame] | 103 | }; |
| 104 | static struct image_desc image[VME_DEVS]; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 105 | |
Vincent Bossier | 584721c | 2011-06-03 10:07:39 +0100 | [diff] [blame] | 106 | struct driver_stats { |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 107 | unsigned long reads; |
| 108 | unsigned long writes; |
| 109 | unsigned long ioctls; |
| 110 | unsigned long irqs; |
| 111 | unsigned long berrs; |
| 112 | unsigned long dmaErrors; |
| 113 | unsigned long timeouts; |
| 114 | unsigned long external; |
Vincent Bossier | 584721c | 2011-06-03 10:07:39 +0100 | [diff] [blame] | 115 | }; |
| 116 | static struct driver_stats statistics; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 117 | |
Emilio G. Cota | b9cc293 | 2010-11-12 11:15:14 +0000 | [diff] [blame] | 118 | static struct cdev *vme_user_cdev; /* Character device */ |
| 119 | static struct class *vme_user_sysfs_class; /* Sysfs class */ |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 120 | static struct vme_dev *vme_user_bridge; /* Pointer to user device */ |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 121 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 122 | |
| 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, |
| 127 | CONTROL_MINOR |
| 128 | }; |
| 129 | |
| 130 | |
| 131 | static int vme_user_open(struct inode *, struct file *); |
| 132 | static int vme_user_release(struct inode *, struct file *); |
Emilio G. Cota | 1a85f20 | 2010-11-12 11:15:34 +0000 | [diff] [blame] | 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, |
| 135 | loff_t *); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 136 | static loff_t vme_user_llseek(struct file *, loff_t, int); |
Arnd Bergmann | b1f2ac0 | 2010-04-27 20:15:07 +0200 | [diff] [blame] | 137 | static long vme_user_unlocked_ioctl(struct file *, unsigned int, unsigned long); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 138 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 139 | static int vme_user_match(struct vme_dev *); |
Bill Pemberton | d7e530d | 2012-11-19 13:21:56 -0500 | [diff] [blame] | 140 | static int vme_user_probe(struct vme_dev *); |
Bill Pemberton | f21a824 | 2012-11-19 13:26:52 -0500 | [diff] [blame] | 141 | static int vme_user_remove(struct vme_dev *); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 142 | |
Vincent Bossier | 584721c | 2011-06-03 10:07:39 +0100 | [diff] [blame] | 143 | static const struct file_operations vme_user_fops = { |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 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, |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | |
| 153 | /* |
| 154 | * Reset all the statistic counters |
| 155 | */ |
| 156 | static void reset_counters(void) |
| 157 | { |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 158 | statistics.reads = 0; |
| 159 | statistics.writes = 0; |
| 160 | statistics.ioctls = 0; |
| 161 | statistics.irqs = 0; |
| 162 | statistics.berrs = 0; |
| 163 | statistics.dmaErrors = 0; |
| 164 | statistics.timeouts = 0; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 167 | static int vme_user_open(struct inode *inode, struct file *file) |
| 168 | { |
| 169 | int err; |
| 170 | unsigned int minor = MINOR(inode->i_rdev); |
| 171 | |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 172 | mutex_lock(&image[minor].mutex); |
Vincent Bossier | 05614fb | 2011-06-09 09:20:31 +0100 | [diff] [blame] | 173 | /* Allow device to be opened if a resource is needed and allocated. */ |
| 174 | if (minor < CONTROL_MINOR && image[minor].resource == NULL) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 175 | pr_err("No resources allocated for device\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 176 | err = -EINVAL; |
| 177 | goto err_res; |
| 178 | } |
| 179 | |
| 180 | /* Increment user count */ |
| 181 | image[minor].users++; |
| 182 | |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 183 | mutex_unlock(&image[minor].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 184 | |
| 185 | return 0; |
| 186 | |
| 187 | err_res: |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 188 | mutex_unlock(&image[minor].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 189 | |
| 190 | return err; |
| 191 | } |
| 192 | |
| 193 | static int vme_user_release(struct inode *inode, struct file *file) |
| 194 | { |
| 195 | unsigned int minor = MINOR(inode->i_rdev); |
| 196 | |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 197 | mutex_lock(&image[minor].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 198 | |
| 199 | /* Decrement user count */ |
| 200 | image[minor].users--; |
| 201 | |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 202 | mutex_unlock(&image[minor].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * We are going ot alloc a page during init per window for small transfers. |
| 209 | * Small transfers will go VME -> buffer -> user space. Larger (more than a |
| 210 | * page) transfers will lock the user space buffer into memory and then |
| 211 | * transfer the data directly into the user space buffers. |
| 212 | */ |
| 213 | static ssize_t resource_to_user(int minor, char __user *buf, size_t count, |
| 214 | loff_t *ppos) |
| 215 | { |
| 216 | ssize_t retval; |
| 217 | ssize_t copied = 0; |
| 218 | |
| 219 | if (count <= image[minor].size_buf) { |
| 220 | /* We copy to kernel buffer */ |
| 221 | copied = vme_master_read(image[minor].resource, |
| 222 | image[minor].kern_buf, count, *ppos); |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 223 | if (copied < 0) |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 224 | return (int)copied; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 225 | |
| 226 | retval = __copy_to_user(buf, image[minor].kern_buf, |
| 227 | (unsigned long)copied); |
| 228 | if (retval != 0) { |
| 229 | copied = (copied - retval); |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 230 | pr_info("User copy failed\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 231 | return -EINVAL; |
| 232 | } |
| 233 | |
| 234 | } else { |
| 235 | /* XXX Need to write this */ |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 236 | pr_info("Currently don't support large transfers\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 237 | /* Map in pages from userspace */ |
| 238 | |
| 239 | /* Call vme_master_read to do the transfer */ |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | |
| 243 | return copied; |
| 244 | } |
| 245 | |
| 246 | /* |
Justin P. Mattock | 9560533 | 2012-08-13 10:28:22 -0700 | [diff] [blame] | 247 | * We are going to alloc a page during init per window for small transfers. |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 248 | * Small transfers will go user space -> buffer -> VME. Larger (more than a |
| 249 | * page) transfers will lock the user space buffer into memory and then |
| 250 | * transfer the data directly from the user space buffers out to VME. |
| 251 | */ |
Emilio G. Cota | 1a85f20 | 2010-11-12 11:15:34 +0000 | [diff] [blame] | 252 | static ssize_t resource_from_user(unsigned int minor, const char __user *buf, |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 253 | size_t count, loff_t *ppos) |
| 254 | { |
| 255 | ssize_t retval; |
| 256 | ssize_t copied = 0; |
| 257 | |
| 258 | if (count <= image[minor].size_buf) { |
| 259 | retval = __copy_from_user(image[minor].kern_buf, buf, |
| 260 | (unsigned long)count); |
| 261 | if (retval != 0) |
| 262 | copied = (copied - retval); |
| 263 | else |
| 264 | copied = count; |
| 265 | |
| 266 | copied = vme_master_write(image[minor].resource, |
| 267 | image[minor].kern_buf, copied, *ppos); |
| 268 | } else { |
| 269 | /* XXX Need to write this */ |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 270 | pr_info("Currently don't support large transfers\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 271 | /* Map in pages from userspace */ |
| 272 | |
| 273 | /* Call vme_master_write to do the transfer */ |
| 274 | return -EINVAL; |
| 275 | } |
| 276 | |
| 277 | return copied; |
| 278 | } |
| 279 | |
| 280 | static ssize_t buffer_to_user(unsigned int minor, char __user *buf, |
| 281 | size_t count, loff_t *ppos) |
| 282 | { |
Emilio G. Cota | 0a81a0f | 2010-11-12 11:15:27 +0000 | [diff] [blame] | 283 | void *image_ptr; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 284 | ssize_t retval; |
| 285 | |
| 286 | image_ptr = image[minor].kern_buf + *ppos; |
| 287 | |
| 288 | retval = __copy_to_user(buf, image_ptr, (unsigned long)count); |
| 289 | if (retval != 0) { |
| 290 | retval = (count - retval); |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 291 | pr_warn("Partial copy to userspace\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 292 | } else |
| 293 | retval = count; |
| 294 | |
| 295 | /* Return number of bytes successfully read */ |
| 296 | return retval; |
| 297 | } |
| 298 | |
Emilio G. Cota | 1a85f20 | 2010-11-12 11:15:34 +0000 | [diff] [blame] | 299 | static ssize_t buffer_from_user(unsigned int minor, const char __user *buf, |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 300 | size_t count, loff_t *ppos) |
| 301 | { |
Emilio G. Cota | 0a81a0f | 2010-11-12 11:15:27 +0000 | [diff] [blame] | 302 | void *image_ptr; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 303 | size_t retval; |
| 304 | |
| 305 | image_ptr = image[minor].kern_buf + *ppos; |
| 306 | |
| 307 | retval = __copy_from_user(image_ptr, buf, (unsigned long)count); |
| 308 | if (retval != 0) { |
| 309 | retval = (count - retval); |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 310 | pr_warn("Partial copy to userspace\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 311 | } else |
| 312 | retval = count; |
| 313 | |
| 314 | /* Return number of bytes successfully read */ |
| 315 | return retval; |
| 316 | } |
| 317 | |
Emilio G. Cota | 1a85f20 | 2010-11-12 11:15:34 +0000 | [diff] [blame] | 318 | static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count, |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 319 | loff_t *ppos) |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 320 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 321 | unsigned int minor = MINOR(file_inode(file)->i_rdev); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 322 | ssize_t retval; |
| 323 | size_t image_size; |
| 324 | size_t okcount; |
| 325 | |
Vincent Bossier | 05614fb | 2011-06-09 09:20:31 +0100 | [diff] [blame] | 326 | if (minor == CONTROL_MINOR) |
| 327 | return 0; |
| 328 | |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 329 | mutex_lock(&image[minor].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 330 | |
| 331 | /* XXX Do we *really* want this helper - we can use vme_*_get ? */ |
| 332 | image_size = vme_get_size(image[minor].resource); |
| 333 | |
| 334 | /* Ensure we are starting at a valid location */ |
| 335 | if ((*ppos < 0) || (*ppos > (image_size - 1))) { |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 336 | mutex_unlock(&image[minor].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* Ensure not reading past end of the image */ |
| 341 | if (*ppos + count > image_size) |
| 342 | okcount = image_size - *ppos; |
| 343 | else |
| 344 | okcount = count; |
| 345 | |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 346 | switch (type[minor]) { |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 347 | case MASTER_MINOR: |
| 348 | retval = resource_to_user(minor, buf, okcount, ppos); |
| 349 | break; |
| 350 | case SLAVE_MINOR: |
| 351 | retval = buffer_to_user(minor, buf, okcount, ppos); |
| 352 | break; |
| 353 | default: |
| 354 | retval = -EINVAL; |
| 355 | } |
| 356 | |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 357 | mutex_unlock(&image[minor].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 358 | if (retval > 0) |
| 359 | *ppos += retval; |
| 360 | |
| 361 | return retval; |
| 362 | } |
| 363 | |
Emilio G. Cota | 1a85f20 | 2010-11-12 11:15:34 +0000 | [diff] [blame] | 364 | static ssize_t vme_user_write(struct file *file, const char __user *buf, |
| 365 | size_t count, loff_t *ppos) |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 366 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 367 | unsigned int minor = MINOR(file_inode(file)->i_rdev); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 368 | ssize_t retval; |
| 369 | size_t image_size; |
| 370 | size_t okcount; |
| 371 | |
Vincent Bossier | 05614fb | 2011-06-09 09:20:31 +0100 | [diff] [blame] | 372 | if (minor == CONTROL_MINOR) |
| 373 | return 0; |
| 374 | |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 375 | mutex_lock(&image[minor].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 376 | |
| 377 | image_size = vme_get_size(image[minor].resource); |
| 378 | |
| 379 | /* Ensure we are starting at a valid location */ |
| 380 | if ((*ppos < 0) || (*ppos > (image_size - 1))) { |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 381 | mutex_unlock(&image[minor].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /* Ensure not reading past end of the image */ |
| 386 | if (*ppos + count > image_size) |
| 387 | okcount = image_size - *ppos; |
| 388 | else |
| 389 | okcount = count; |
| 390 | |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 391 | switch (type[minor]) { |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 392 | case MASTER_MINOR: |
| 393 | retval = resource_from_user(minor, buf, okcount, ppos); |
| 394 | break; |
| 395 | case SLAVE_MINOR: |
| 396 | retval = buffer_from_user(minor, buf, okcount, ppos); |
| 397 | break; |
| 398 | default: |
| 399 | retval = -EINVAL; |
| 400 | } |
Toshiaki Yamane | 538a697 | 2012-08-21 20:12:33 +0900 | [diff] [blame] | 401 | |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 402 | mutex_unlock(&image[minor].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 403 | |
| 404 | if (retval > 0) |
| 405 | *ppos += retval; |
| 406 | |
| 407 | return retval; |
| 408 | } |
| 409 | |
| 410 | static loff_t vme_user_llseek(struct file *file, loff_t off, int whence) |
| 411 | { |
Arthur Benilov | 877de4b | 2010-02-16 15:40:30 +0100 | [diff] [blame] | 412 | loff_t absolute = -1; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 413 | unsigned int minor = MINOR(file_inode(file)->i_rdev); |
Arthur Benilov | 877de4b | 2010-02-16 15:40:30 +0100 | [diff] [blame] | 414 | size_t image_size; |
| 415 | |
Vincent Bossier | 05614fb | 2011-06-09 09:20:31 +0100 | [diff] [blame] | 416 | if (minor == CONTROL_MINOR) |
| 417 | return -EINVAL; |
| 418 | |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 419 | mutex_lock(&image[minor].mutex); |
Arthur Benilov | 877de4b | 2010-02-16 15:40:30 +0100 | [diff] [blame] | 420 | image_size = vme_get_size(image[minor].resource); |
| 421 | |
| 422 | switch (whence) { |
| 423 | case SEEK_SET: |
| 424 | absolute = off; |
| 425 | break; |
| 426 | case SEEK_CUR: |
| 427 | absolute = file->f_pos + off; |
| 428 | break; |
| 429 | case SEEK_END: |
| 430 | absolute = image_size + off; |
| 431 | break; |
| 432 | default: |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 433 | mutex_unlock(&image[minor].mutex); |
Arthur Benilov | 877de4b | 2010-02-16 15:40:30 +0100 | [diff] [blame] | 434 | return -EINVAL; |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | if ((absolute < 0) || (absolute >= image_size)) { |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 439 | mutex_unlock(&image[minor].mutex); |
Arthur Benilov | 877de4b | 2010-02-16 15:40:30 +0100 | [diff] [blame] | 440 | return -EINVAL; |
| 441 | } |
| 442 | |
| 443 | file->f_pos = absolute; |
| 444 | |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 445 | mutex_unlock(&image[minor].mutex); |
Arthur Benilov | 877de4b | 2010-02-16 15:40:30 +0100 | [diff] [blame] | 446 | |
| 447 | return absolute; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 450 | /* |
| 451 | * The ioctls provided by the old VME access method (the one at vmelinux.org) |
| 452 | * are most certainly wrong as the effectively push the registers layout |
| 453 | * through to user space. Given that the VME core can handle multiple bridges, |
| 454 | * with different register layouts this is most certainly not the way to go. |
| 455 | * |
| 456 | * We aren't using the structures defined in the Motorola driver either - these |
| 457 | * are also quite low level, however we should use the definitions that have |
| 458 | * already been defined. |
| 459 | */ |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 460 | static int vme_user_ioctl(struct inode *inode, struct file *file, |
| 461 | unsigned int cmd, unsigned long arg) |
| 462 | { |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 463 | struct vme_master master; |
| 464 | struct vme_slave slave; |
Vincent Bossier | dca2218 | 2011-06-09 15:49:30 +0200 | [diff] [blame] | 465 | struct vme_irq_id irq_req; |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 466 | unsigned long copied; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 467 | unsigned int minor = MINOR(inode->i_rdev); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 468 | int retval; |
| 469 | dma_addr_t pci_addr; |
Emilio G. Cota | 1a85f20 | 2010-11-12 11:15:34 +0000 | [diff] [blame] | 470 | void __user *argp = (void __user *)arg; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 471 | |
| 472 | statistics.ioctls++; |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 473 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 474 | switch (type[minor]) { |
| 475 | case CONTROL_MINOR: |
Vincent Bossier | dca2218 | 2011-06-09 15:49:30 +0200 | [diff] [blame] | 476 | switch (cmd) { |
| 477 | case VME_IRQ_GEN: |
Dan Carpenter | a7f3943 | 2012-07-06 11:21:49 +0300 | [diff] [blame] | 478 | copied = copy_from_user(&irq_req, argp, |
Vincent Bossier | dca2218 | 2011-06-09 15:49:30 +0200 | [diff] [blame] | 479 | sizeof(struct vme_irq_id)); |
| 480 | if (copied != 0) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 481 | pr_warn("Partial copy from userspace\n"); |
Vincent Bossier | dca2218 | 2011-06-09 15:49:30 +0200 | [diff] [blame] | 482 | return -EFAULT; |
| 483 | } |
| 484 | |
| 485 | retval = vme_irq_generate(vme_user_bridge, |
| 486 | irq_req.level, |
| 487 | irq_req.statid); |
| 488 | |
| 489 | return retval; |
| 490 | } |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 491 | break; |
| 492 | case MASTER_MINOR: |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 493 | switch (cmd) { |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 494 | case VME_GET_MASTER: |
| 495 | memset(&master, 0, sizeof(struct vme_master)); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 496 | |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 497 | /* XXX We do not want to push aspace, cycle and width |
| 498 | * to userspace as they are |
| 499 | */ |
| 500 | retval = vme_master_get(image[minor].resource, |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 501 | &master.enable, &master.vme_addr, |
| 502 | &master.size, &master.aspace, |
| 503 | &master.cycle, &master.dwidth); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 504 | |
Emilio G. Cota | 1a85f20 | 2010-11-12 11:15:34 +0000 | [diff] [blame] | 505 | copied = copy_to_user(argp, &master, |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 506 | sizeof(struct vme_master)); |
| 507 | if (copied != 0) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 508 | pr_warn("Partial copy to userspace\n"); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 509 | return -EFAULT; |
| 510 | } |
| 511 | |
| 512 | return retval; |
| 513 | break; |
| 514 | |
| 515 | case VME_SET_MASTER: |
| 516 | |
Emilio G. Cota | 1a85f20 | 2010-11-12 11:15:34 +0000 | [diff] [blame] | 517 | copied = copy_from_user(&master, argp, sizeof(master)); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 518 | if (copied != 0) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 519 | pr_warn("Partial copy from userspace\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 520 | return -EFAULT; |
| 521 | } |
| 522 | |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 523 | /* XXX We do not want to push aspace, cycle and width |
| 524 | * to userspace as they are |
| 525 | */ |
| 526 | return vme_master_set(image[minor].resource, |
| 527 | master.enable, master.vme_addr, master.size, |
| 528 | master.aspace, master.cycle, master.dwidth); |
| 529 | |
| 530 | break; |
| 531 | } |
| 532 | break; |
| 533 | case SLAVE_MINOR: |
| 534 | switch (cmd) { |
| 535 | case VME_GET_SLAVE: |
| 536 | memset(&slave, 0, sizeof(struct vme_slave)); |
| 537 | |
| 538 | /* XXX We do not want to push aspace, cycle and width |
| 539 | * to userspace as they are |
| 540 | */ |
| 541 | retval = vme_slave_get(image[minor].resource, |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 542 | &slave.enable, &slave.vme_addr, |
| 543 | &slave.size, &pci_addr, &slave.aspace, |
| 544 | &slave.cycle); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 545 | |
Emilio G. Cota | 1a85f20 | 2010-11-12 11:15:34 +0000 | [diff] [blame] | 546 | copied = copy_to_user(argp, &slave, |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 547 | sizeof(struct vme_slave)); |
| 548 | if (copied != 0) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 549 | pr_warn("Partial copy to userspace\n"); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 550 | return -EFAULT; |
| 551 | } |
| 552 | |
| 553 | return retval; |
| 554 | break; |
| 555 | |
| 556 | case VME_SET_SLAVE: |
| 557 | |
Emilio G. Cota | 1a85f20 | 2010-11-12 11:15:34 +0000 | [diff] [blame] | 558 | copied = copy_from_user(&slave, argp, sizeof(slave)); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 559 | if (copied != 0) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 560 | pr_warn("Partial copy from userspace\n"); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 561 | return -EFAULT; |
| 562 | } |
| 563 | |
| 564 | /* XXX We do not want to push aspace, cycle and width |
| 565 | * to userspace as they are |
| 566 | */ |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 567 | return vme_slave_set(image[minor].resource, |
| 568 | slave.enable, slave.vme_addr, slave.size, |
| 569 | image[minor].pci_buf, slave.aspace, |
| 570 | slave.cycle); |
| 571 | |
| 572 | break; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 573 | } |
| 574 | break; |
| 575 | } |
| 576 | |
| 577 | return -EINVAL; |
| 578 | } |
| 579 | |
Arnd Bergmann | b1f2ac0 | 2010-04-27 20:15:07 +0200 | [diff] [blame] | 580 | static long |
| 581 | vme_user_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 582 | { |
| 583 | int ret; |
| 584 | |
Arnd Bergmann | 8e2394a | 2010-07-11 23:18:52 +0200 | [diff] [blame] | 585 | mutex_lock(&vme_user_mutex); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 586 | ret = vme_user_ioctl(file_inode(file), file, cmd, arg); |
Arnd Bergmann | 8e2394a | 2010-07-11 23:18:52 +0200 | [diff] [blame] | 587 | mutex_unlock(&vme_user_mutex); |
Arnd Bergmann | b1f2ac0 | 2010-04-27 20:15:07 +0200 | [diff] [blame] | 588 | |
| 589 | return ret; |
| 590 | } |
| 591 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 592 | |
| 593 | /* |
| 594 | * Unallocate a previously allocated buffer |
| 595 | */ |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 596 | static void buf_unalloc(int num) |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 597 | { |
| 598 | if (image[num].kern_buf) { |
| 599 | #ifdef VME_DEBUG |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 600 | pr_debug("UniverseII:Releasing buffer at %p\n", |
| 601 | image[num].pci_buf); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 602 | #endif |
| 603 | |
| 604 | vme_free_consistent(image[num].resource, image[num].size_buf, |
| 605 | image[num].kern_buf, image[num].pci_buf); |
| 606 | |
| 607 | image[num].kern_buf = NULL; |
| 608 | image[num].pci_buf = 0; |
| 609 | image[num].size_buf = 0; |
| 610 | |
| 611 | #ifdef VME_DEBUG |
| 612 | } else { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 613 | pr_debug("UniverseII: Buffer not allocated\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 614 | #endif |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | static struct vme_driver vme_user_driver = { |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 619 | .name = driver_name, |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 620 | .match = vme_user_match, |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 621 | .probe = vme_user_probe, |
Bill Pemberton | 3893075 | 2012-11-19 13:21:03 -0500 | [diff] [blame] | 622 | .remove = vme_user_remove, |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 623 | }; |
| 624 | |
| 625 | |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 626 | static int __init vme_user_init(void) |
| 627 | { |
| 628 | int retval = 0; |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 629 | |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 630 | pr_info("VME User Space Access Driver\n"); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 631 | |
| 632 | if (bus_num == 0) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 633 | pr_err("No cards, skipping registration\n"); |
Emilio G. Cota | 55db502 | 2010-11-12 11:14:14 +0000 | [diff] [blame] | 634 | retval = -ENODEV; |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 635 | goto err_nocard; |
| 636 | } |
| 637 | |
| 638 | /* Let's start by supporting one bus, we can support more than one |
| 639 | * in future revisions if that ever becomes necessary. |
| 640 | */ |
Manohar Vanga | 0a4b6b0 | 2011-09-26 11:27:18 +0200 | [diff] [blame] | 641 | if (bus_num > VME_USER_BUS_MAX) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 642 | pr_err("Driver only able to handle %d buses\n", |
| 643 | VME_USER_BUS_MAX); |
Manohar Vanga | 0a4b6b0 | 2011-09-26 11:27:18 +0200 | [diff] [blame] | 644 | bus_num = VME_USER_BUS_MAX; |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 645 | } |
| 646 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 647 | /* |
| 648 | * Here we just register the maximum number of devices we can and |
| 649 | * leave vme_user_match() to allow only 1 to go through to probe(). |
| 650 | * This way, if we later want to allow multiple user access devices, |
| 651 | * we just change the code in vme_user_match(). |
| 652 | */ |
| 653 | retval = vme_register_driver(&vme_user_driver, VME_MAX_SLOTS); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 654 | if (retval != 0) |
| 655 | goto err_reg; |
| 656 | |
| 657 | return retval; |
| 658 | |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 659 | err_reg: |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 660 | err_nocard: |
| 661 | return retval; |
| 662 | } |
| 663 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 664 | static int vme_user_match(struct vme_dev *vdev) |
| 665 | { |
Manohar Vanga | 0a4b6b0 | 2011-09-26 11:27:18 +0200 | [diff] [blame] | 666 | if (vdev->num >= VME_USER_BUS_MAX) |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 667 | return 0; |
| 668 | return 1; |
| 669 | } |
| 670 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 671 | /* |
| 672 | * In this simple access driver, the old behaviour is being preserved as much |
| 673 | * as practical. We will therefore reserve the buffers and request the images |
| 674 | * here so that we don't have to do it later. |
| 675 | */ |
Bill Pemberton | d7e530d | 2012-11-19 13:21:56 -0500 | [diff] [blame] | 676 | static int vme_user_probe(struct vme_dev *vdev) |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 677 | { |
| 678 | int i, err; |
Martyn Welch | beb9ccc | 2009-10-29 16:34:48 +0000 | [diff] [blame] | 679 | char name[12]; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 680 | |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 681 | /* Save pointer to the bridge device */ |
| 682 | if (vme_user_bridge != NULL) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 683 | dev_err(&vdev->dev, "Driver can only be loaded for 1 device\n"); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 684 | err = -EINVAL; |
| 685 | goto err_dev; |
| 686 | } |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 687 | vme_user_bridge = vdev; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 688 | |
| 689 | /* Initialise descriptors */ |
| 690 | for (i = 0; i < VME_DEVS; i++) { |
| 691 | image[i].kern_buf = NULL; |
| 692 | image[i].pci_buf = 0; |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 693 | mutex_init(&image[i].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 694 | image[i].device = NULL; |
| 695 | image[i].resource = NULL; |
| 696 | image[i].users = 0; |
| 697 | } |
| 698 | |
| 699 | /* Initialise statistics counters */ |
| 700 | reset_counters(); |
| 701 | |
| 702 | /* Assign major and minor numbers for the driver */ |
| 703 | err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS, |
| 704 | driver_name); |
| 705 | if (err) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 706 | dev_warn(&vdev->dev, "Error getting Major Number %d for driver.\n", |
| 707 | VME_MAJOR); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 708 | goto err_region; |
| 709 | } |
| 710 | |
| 711 | /* Register the driver as a char device */ |
| 712 | vme_user_cdev = cdev_alloc(); |
Kumar Amit Mehta | d4113a6 | 2013-03-24 22:37:48 -0700 | [diff] [blame^] | 713 | if (!vme_user_cdev) { |
| 714 | err = -ENOMEM; |
| 715 | goto err_char; |
| 716 | } |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 717 | vme_user_cdev->ops = &vme_user_fops; |
| 718 | vme_user_cdev->owner = THIS_MODULE; |
| 719 | err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS); |
| 720 | if (err) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 721 | dev_warn(&vdev->dev, "cdev_all failed\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 722 | goto err_char; |
| 723 | } |
| 724 | |
| 725 | /* Request slave resources and allocate buffers (128kB wide) */ |
| 726 | for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) { |
| 727 | /* XXX Need to properly request attributes */ |
Arthur Benilov | 5188d74 | 2010-02-16 15:40:58 +0100 | [diff] [blame] | 728 | /* For ca91cx42 bridge there are only two slave windows |
| 729 | * supporting A16 addressing, so we request A24 supported |
| 730 | * by all windows. |
| 731 | */ |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 732 | image[i].resource = vme_slave_request(vme_user_bridge, |
Arthur Benilov | 5188d74 | 2010-02-16 15:40:58 +0100 | [diff] [blame] | 733 | VME_A24, VME_SCT); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 734 | if (image[i].resource == NULL) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 735 | dev_warn(&vdev->dev, |
| 736 | "Unable to allocate slave resource\n"); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 737 | goto err_slave; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 738 | } |
| 739 | image[i].size_buf = PCI_BUF_SIZE; |
| 740 | image[i].kern_buf = vme_alloc_consistent(image[i].resource, |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 741 | image[i].size_buf, &image[i].pci_buf); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 742 | if (image[i].kern_buf == NULL) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 743 | dev_warn(&vdev->dev, |
| 744 | "Unable to allocate memory for buffer\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 745 | image[i].pci_buf = 0; |
| 746 | vme_slave_free(image[i].resource); |
| 747 | err = -ENOMEM; |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 748 | goto err_slave; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Request master resources allocate page sized buffers for small |
| 754 | * reads and writes |
| 755 | */ |
| 756 | for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) { |
| 757 | /* XXX Need to properly request attributes */ |
| 758 | image[i].resource = vme_master_request(vme_user_bridge, |
| 759 | VME_A32, VME_SCT, VME_D32); |
| 760 | if (image[i].resource == NULL) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 761 | dev_warn(&vdev->dev, |
| 762 | "Unable to allocate master resource\n"); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 763 | goto err_master; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 764 | } |
Arthur Benilov | 33e920d | 2010-02-16 15:41:21 +0100 | [diff] [blame] | 765 | image[i].size_buf = PCI_BUF_SIZE; |
| 766 | image[i].kern_buf = kmalloc(image[i].size_buf, GFP_KERNEL); |
| 767 | if (image[i].kern_buf == NULL) { |
Arthur Benilov | 33e920d | 2010-02-16 15:41:21 +0100 | [diff] [blame] | 768 | err = -ENOMEM; |
| 769 | goto err_master_buf; |
| 770 | } |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* Create sysfs entries - on udev systems this creates the dev files */ |
| 774 | vme_user_sysfs_class = class_create(THIS_MODULE, driver_name); |
| 775 | if (IS_ERR(vme_user_sysfs_class)) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 776 | dev_err(&vdev->dev, "Error creating vme_user class.\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 777 | err = PTR_ERR(vme_user_sysfs_class); |
| 778 | goto err_class; |
| 779 | } |
| 780 | |
| 781 | /* Add sysfs Entries */ |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 782 | for (i = 0; i < VME_DEVS; i++) { |
Vincent Bossier | 584721c | 2011-06-03 10:07:39 +0100 | [diff] [blame] | 783 | int num; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 784 | switch (type[i]) { |
| 785 | case MASTER_MINOR: |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 786 | sprintf(name, "bus/vme/m%%d"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 787 | break; |
| 788 | case CONTROL_MINOR: |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 789 | sprintf(name, "bus/vme/ctl"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 790 | break; |
| 791 | case SLAVE_MINOR: |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 792 | sprintf(name, "bus/vme/s%%d"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 793 | break; |
| 794 | default: |
| 795 | err = -EINVAL; |
| 796 | goto err_sysfs; |
| 797 | break; |
| 798 | } |
| 799 | |
Vincent Bossier | 584721c | 2011-06-03 10:07:39 +0100 | [diff] [blame] | 800 | num = (type[i] == SLAVE_MINOR) ? i - (MASTER_MAX + 1) : i; |
| 801 | image[i].device = device_create(vme_user_sysfs_class, NULL, |
| 802 | MKDEV(VME_MAJOR, i), NULL, name, num); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 803 | if (IS_ERR(image[i].device)) { |
YAMANE Toshiaki | 0093e5f | 2012-11-09 12:23:14 +0900 | [diff] [blame] | 804 | dev_info(&vdev->dev, "Error creating sysfs device\n"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 805 | err = PTR_ERR(image[i].device); |
| 806 | goto err_sysfs; |
| 807 | } |
| 808 | } |
| 809 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 810 | return 0; |
| 811 | |
| 812 | /* Ensure counter set correcty to destroy all sysfs devices */ |
| 813 | i = VME_DEVS; |
| 814 | err_sysfs: |
Nanakos Chrysostomos | 45f9f01 | 2010-05-28 10:54:45 +0000 | [diff] [blame] | 815 | while (i > 0) { |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 816 | i--; |
| 817 | device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i)); |
| 818 | } |
| 819 | class_destroy(vme_user_sysfs_class); |
| 820 | |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 821 | /* Ensure counter set correcty to unalloc all master windows */ |
| 822 | i = MASTER_MAX + 1; |
Arthur Benilov | 33e920d | 2010-02-16 15:41:21 +0100 | [diff] [blame] | 823 | err_master_buf: |
| 824 | for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) |
| 825 | kfree(image[i].kern_buf); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 826 | err_master: |
| 827 | while (i > MASTER_MINOR) { |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 828 | i--; |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 829 | vme_master_free(image[i].resource); |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | * Ensure counter set correcty to unalloc all slave windows and buffers |
| 834 | */ |
| 835 | i = SLAVE_MAX + 1; |
| 836 | err_slave: |
| 837 | while (i > SLAVE_MINOR) { |
| 838 | i--; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 839 | buf_unalloc(i); |
Emilio G. Cota | 1daa38d | 2010-12-03 09:05:08 +0000 | [diff] [blame] | 840 | vme_slave_free(image[i].resource); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 841 | } |
| 842 | err_class: |
| 843 | cdev_del(vme_user_cdev); |
| 844 | err_char: |
| 845 | unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS); |
| 846 | err_region: |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 847 | err_dev: |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 848 | return err; |
| 849 | } |
| 850 | |
Bill Pemberton | f21a824 | 2012-11-19 13:26:52 -0500 | [diff] [blame] | 851 | static int vme_user_remove(struct vme_dev *dev) |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 852 | { |
| 853 | int i; |
| 854 | |
| 855 | /* Remove sysfs Entries */ |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 856 | for (i = 0; i < VME_DEVS; i++) { |
| 857 | mutex_destroy(&image[i].mutex); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 858 | device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i)); |
Santosh Nayak | ecb3b80 | 2012-04-03 16:42:51 +0530 | [diff] [blame] | 859 | } |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 860 | class_destroy(vme_user_sysfs_class); |
| 861 | |
Emilio G. Cota | b62c99b | 2010-12-03 14:20:51 +0000 | [diff] [blame] | 862 | for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) { |
Arthur Benilov | 33e920d | 2010-02-16 15:41:21 +0100 | [diff] [blame] | 863 | kfree(image[i].kern_buf); |
Emilio G. Cota | b62c99b | 2010-12-03 14:20:51 +0000 | [diff] [blame] | 864 | vme_master_free(image[i].resource); |
| 865 | } |
Arthur Benilov | 33e920d | 2010-02-16 15:41:21 +0100 | [diff] [blame] | 866 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 867 | for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) { |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 868 | vme_slave_set(image[i].resource, 0, 0, 0, 0, VME_A32, 0); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 869 | buf_unalloc(i); |
Emilio G. Cota | 1daa38d | 2010-12-03 09:05:08 +0000 | [diff] [blame] | 870 | vme_slave_free(image[i].resource); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /* Unregister device driver */ |
| 874 | cdev_del(vme_user_cdev); |
| 875 | |
| 876 | /* Unregiser the major and minor device numbers */ |
| 877 | unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 878 | |
| 879 | return 0; |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 880 | } |
| 881 | |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 882 | static void __exit vme_user_exit(void) |
| 883 | { |
| 884 | vme_unregister_driver(&vme_user_driver); |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | |
| 888 | MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the driver is connected"); |
| 889 | module_param_array(bus, int, &bus_num, 0); |
| 890 | |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 891 | MODULE_DESCRIPTION("VME User Space Access Driver"); |
Martyn Welch | 66bd8db | 2010-02-18 15:12:52 +0000 | [diff] [blame] | 892 | MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com"); |
Martyn Welch | f00a86d | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 893 | MODULE_LICENSE("GPL"); |
| 894 | |
Martyn Welch | 238add5 | 2009-08-11 14:37:15 +0100 | [diff] [blame] | 895 | module_init(vme_user_init); |
| 896 | module_exit(vme_user_exit); |