Commit | Line | Data |
---|---|---|
0057cb5e | 1 | // SPDX-License-Identifier: GPL-2.0+ |
f00a86d9 MW |
2 | /* |
3 | * VMEbus User access driver | |
4 | * | |
66bd8db5 MW |
5 | * Author: Martyn Welch <martyn.welch@ge.com> |
6 | * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc. | |
f00a86d9 MW |
7 | * |
8 | * Based on work by: | |
9 | * Tom Armistead and Ajit Prem | |
10 | * Copyright 2004 Motorola Inc. | |
f00a86d9 MW |
11 | */ |
12 | ||
0093e5f8 YT |
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
14 | ||
d3cfd5b9 | 15 | #include <linux/refcount.h> |
f00a86d9 MW |
16 | #include <linux/cdev.h> |
17 | #include <linux/delay.h> | |
18 | #include <linux/device.h> | |
19 | #include <linux/dma-mapping.h> | |
20 | #include <linux/errno.h> | |
21 | #include <linux/init.h> | |
22 | #include <linux/ioctl.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/mm.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/pagemap.h> | |
27 | #include <linux/pci.h> | |
ecb3b80f | 28 | #include <linux/mutex.h> |
5a0e3ad6 | 29 | #include <linux/slab.h> |
f00a86d9 MW |
30 | #include <linux/spinlock.h> |
31 | #include <linux/syscalls.h> | |
32 | #include <linux/types.h> | |
f00a86d9 | 33 | |
45f9f018 NC |
34 | #include <linux/io.h> |
35 | #include <linux/uaccess.h> | |
db3b9e99 | 36 | #include <linux/vme.h> |
f00a86d9 | 37 | |
f00a86d9 MW |
38 | #include "vme_user.h" |
39 | ||
584721ca | 40 | static const char driver_name[] = "vme_user"; |
238add52 | 41 | |
0a4b6b02 | 42 | static int bus[VME_USER_BUS_MAX]; |
c9492318 | 43 | static unsigned int bus_num; |
238add52 | 44 | |
00e293b9 PT |
45 | /* Currently Documentation/admin-guide/devices.rst defines the |
46 | * following for VME: | |
f00a86d9 MW |
47 | * |
48 | * 221 char VME bus | |
45f9f018 NC |
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 | |
f00a86d9 | 58 | * |
45f9f018 NC |
59 | * It is expected that all VME bus drivers will use the |
60 | * same interface. For interface documentation see | |
61 | * http://www.vmelinux.org/. | |
f00a86d9 MW |
62 | * |
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). | |
95605332 | 65 | * We'll run with this for now as far as possible, however it probably makes |
f00a86d9 MW |
66 | * sense to get rid of the old mappings and just do everything dynamically. |
67 | * | |
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 | |
95605332 JM |
70 | * http://www.vmelinux.org/ as an alternative the driver can be written |
71 | * providing a saner interface later. | |
238add52 MW |
72 | * |
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. | |
f00a86d9 MW |
76 | */ |
77 | #define VME_MAJOR 221 /* VME Major Device Number */ | |
78 | #define VME_DEVS 9 /* Number of dev entries */ | |
79 | ||
80 | #define MASTER_MINOR 0 | |
81 | #define MASTER_MAX 3 | |
82 | #define SLAVE_MINOR 4 | |
83 | #define SLAVE_MAX 7 | |
84 | #define CONTROL_MINOR 8 | |
85 | ||
86 | #define PCI_BUF_SIZE 0x20000 /* Size of one slave image buffer */ | |
87 | ||
88 | /* | |
89 | * Structure to handle image related parameters. | |
90 | */ | |
584721ca | 91 | struct image_desc { |
0a81a0f7 | 92 | void *kern_buf; /* Buffer address in kernel space */ |
f00a86d9 MW |
93 | dma_addr_t pci_buf; /* Buffer address in PCI address space */ |
94 | unsigned long long size_buf; /* Buffer size */ | |
ecb3b80f | 95 | struct mutex mutex; /* Mutex for locking image */ |
f00a86d9 MW |
96 | struct device *device; /* Sysfs device */ |
97 | struct vme_resource *resource; /* VME resource */ | |
c74a804f | 98 | int mmap_count; /* Number of current mmap's */ |
584721ca | 99 | }; |
cd974d35 | 100 | |
584721ca | 101 | static struct image_desc image[VME_DEVS]; |
f00a86d9 | 102 | |
b9cc2934 EC |
103 | static struct cdev *vme_user_cdev; /* Character device */ |
104 | static struct class *vme_user_sysfs_class; /* Sysfs class */ | |
8f966dc4 | 105 | static struct vme_dev *vme_user_bridge; /* Pointer to user device */ |
f00a86d9 | 106 | |
f00a86d9 MW |
107 | static const int type[VME_DEVS] = { MASTER_MINOR, MASTER_MINOR, |
108 | MASTER_MINOR, MASTER_MINOR, | |
109 | SLAVE_MINOR, SLAVE_MINOR, | |
110 | SLAVE_MINOR, SLAVE_MINOR, | |
111 | CONTROL_MINOR | |
112 | }; | |
113 | ||
c74a804f DK |
114 | struct vme_user_vma_priv { |
115 | unsigned int minor; | |
d3cfd5b9 | 116 | refcount_t refcnt; |
c74a804f DK |
117 | }; |
118 | ||
f00a86d9 | 119 | static ssize_t resource_to_user(int minor, char __user *buf, size_t count, |
86eadace | 120 | loff_t *ppos) |
f00a86d9 | 121 | { |
f00a86d9 MW |
122 | ssize_t copied = 0; |
123 | ||
8e4d138c DK |
124 | if (count > image[minor].size_buf) |
125 | count = image[minor].size_buf; | |
126 | ||
8e4d138c DK |
127 | copied = vme_master_read(image[minor].resource, image[minor].kern_buf, |
128 | count, *ppos); | |
129 | if (copied < 0) | |
130 | return (int)copied; | |
131 | ||
7c78e0cd DK |
132 | if (__copy_to_user(buf, image[minor].kern_buf, (unsigned long)copied)) |
133 | return -EFAULT; | |
f00a86d9 MW |
134 | |
135 | return copied; | |
136 | } | |
137 | ||
1a85f207 | 138 | static ssize_t resource_from_user(unsigned int minor, const char __user *buf, |
86eadace | 139 | size_t count, loff_t *ppos) |
f00a86d9 | 140 | { |
8e4d138c DK |
141 | if (count > image[minor].size_buf) |
142 | count = image[minor].size_buf; | |
143 | ||
7c78e0cd DK |
144 | if (__copy_from_user(image[minor].kern_buf, buf, (unsigned long)count)) |
145 | return -EFAULT; | |
8e4d138c | 146 | |
457ab286 DK |
147 | return vme_master_write(image[minor].resource, image[minor].kern_buf, |
148 | count, *ppos); | |
f00a86d9 MW |
149 | } |
150 | ||
151 | static ssize_t buffer_to_user(unsigned int minor, char __user *buf, | |
86eadace | 152 | size_t count, loff_t *ppos) |
f00a86d9 | 153 | { |
0a81a0f7 | 154 | void *image_ptr; |
f00a86d9 MW |
155 | |
156 | image_ptr = image[minor].kern_buf + *ppos; | |
7c78e0cd DK |
157 | if (__copy_to_user(buf, image_ptr, (unsigned long)count)) |
158 | return -EFAULT; | |
f00a86d9 | 159 | |
7c78e0cd | 160 | return count; |
f00a86d9 MW |
161 | } |
162 | ||
1a85f207 | 163 | static ssize_t buffer_from_user(unsigned int minor, const char __user *buf, |
86eadace | 164 | size_t count, loff_t *ppos) |
f00a86d9 | 165 | { |
0a81a0f7 | 166 | void *image_ptr; |
f00a86d9 MW |
167 | |
168 | image_ptr = image[minor].kern_buf + *ppos; | |
7c78e0cd DK |
169 | if (__copy_from_user(image_ptr, buf, (unsigned long)count)) |
170 | return -EFAULT; | |
f00a86d9 | 171 | |
7c78e0cd | 172 | return count; |
f00a86d9 MW |
173 | } |
174 | ||
1a85f207 | 175 | static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count, |
86eadace | 176 | loff_t *ppos) |
f00a86d9 | 177 | { |
496ad9aa | 178 | unsigned int minor = MINOR(file_inode(file)->i_rdev); |
f00a86d9 MW |
179 | ssize_t retval; |
180 | size_t image_size; | |
f00a86d9 | 181 | |
05614fbf VB |
182 | if (minor == CONTROL_MINOR) |
183 | return 0; | |
184 | ||
ecb3b80f | 185 | mutex_lock(&image[minor].mutex); |
f00a86d9 MW |
186 | |
187 | /* XXX Do we *really* want this helper - we can use vme_*_get ? */ | |
188 | image_size = vme_get_size(image[minor].resource); | |
189 | ||
190 | /* Ensure we are starting at a valid location */ | |
191 | if ((*ppos < 0) || (*ppos > (image_size - 1))) { | |
ecb3b80f | 192 | mutex_unlock(&image[minor].mutex); |
f00a86d9 MW |
193 | return 0; |
194 | } | |
195 | ||
196 | /* Ensure not reading past end of the image */ | |
197 | if (*ppos + count > image_size) | |
32491f56 | 198 | count = image_size - *ppos; |
f00a86d9 | 199 | |
45f9f018 | 200 | switch (type[minor]) { |
f00a86d9 | 201 | case MASTER_MINOR: |
32491f56 | 202 | retval = resource_to_user(minor, buf, count, ppos); |
f00a86d9 MW |
203 | break; |
204 | case SLAVE_MINOR: | |
32491f56 | 205 | retval = buffer_to_user(minor, buf, count, ppos); |
f00a86d9 MW |
206 | break; |
207 | default: | |
208 | retval = -EINVAL; | |
209 | } | |
210 | ||
ecb3b80f | 211 | mutex_unlock(&image[minor].mutex); |
f00a86d9 MW |
212 | if (retval > 0) |
213 | *ppos += retval; | |
214 | ||
215 | return retval; | |
216 | } | |
217 | ||
1a85f207 | 218 | static ssize_t vme_user_write(struct file *file, const char __user *buf, |
86eadace | 219 | size_t count, loff_t *ppos) |
f00a86d9 | 220 | { |
496ad9aa | 221 | unsigned int minor = MINOR(file_inode(file)->i_rdev); |
f00a86d9 MW |
222 | ssize_t retval; |
223 | size_t image_size; | |
f00a86d9 | 224 | |
05614fbf VB |
225 | if (minor == CONTROL_MINOR) |
226 | return 0; | |
227 | ||
ecb3b80f | 228 | mutex_lock(&image[minor].mutex); |
f00a86d9 MW |
229 | |
230 | image_size = vme_get_size(image[minor].resource); | |
231 | ||
232 | /* Ensure we are starting at a valid location */ | |
233 | if ((*ppos < 0) || (*ppos > (image_size - 1))) { | |
ecb3b80f | 234 | mutex_unlock(&image[minor].mutex); |
f00a86d9 MW |
235 | return 0; |
236 | } | |
237 | ||
238 | /* Ensure not reading past end of the image */ | |
239 | if (*ppos + count > image_size) | |
32491f56 | 240 | count = image_size - *ppos; |
f00a86d9 | 241 | |
45f9f018 | 242 | switch (type[minor]) { |
f00a86d9 | 243 | case MASTER_MINOR: |
32491f56 | 244 | retval = resource_from_user(minor, buf, count, ppos); |
f00a86d9 MW |
245 | break; |
246 | case SLAVE_MINOR: | |
32491f56 | 247 | retval = buffer_from_user(minor, buf, count, ppos); |
f00a86d9 MW |
248 | break; |
249 | default: | |
250 | retval = -EINVAL; | |
251 | } | |
538a697a | 252 | |
ecb3b80f | 253 | mutex_unlock(&image[minor].mutex); |
f00a86d9 MW |
254 | |
255 | if (retval > 0) | |
256 | *ppos += retval; | |
257 | ||
258 | return retval; | |
259 | } | |
260 | ||
261 | static loff_t vme_user_llseek(struct file *file, loff_t off, int whence) | |
262 | { | |
496ad9aa | 263 | unsigned int minor = MINOR(file_inode(file)->i_rdev); |
877de4b4 | 264 | size_t image_size; |
59482291 | 265 | loff_t res; |
877de4b4 | 266 | |
615c40dd DK |
267 | switch (type[minor]) { |
268 | case MASTER_MINOR: | |
269 | case SLAVE_MINOR: | |
270 | mutex_lock(&image[minor].mutex); | |
271 | image_size = vme_get_size(image[minor].resource); | |
272 | res = fixed_size_llseek(file, off, whence, image_size); | |
273 | mutex_unlock(&image[minor].mutex); | |
274 | return res; | |
275 | } | |
877de4b4 | 276 | |
615c40dd | 277 | return -EINVAL; |
f00a86d9 MW |
278 | } |
279 | ||
238add52 MW |
280 | /* |
281 | * The ioctls provided by the old VME access method (the one at vmelinux.org) | |
282 | * are most certainly wrong as the effectively push the registers layout | |
283 | * through to user space. Given that the VME core can handle multiple bridges, | |
284 | * with different register layouts this is most certainly not the way to go. | |
285 | * | |
286 | * We aren't using the structures defined in the Motorola driver either - these | |
287 | * are also quite low level, however we should use the definitions that have | |
288 | * already been defined. | |
289 | */ | |
f00a86d9 | 290 | static int vme_user_ioctl(struct inode *inode, struct file *file, |
86eadace | 291 | unsigned int cmd, unsigned long arg) |
f00a86d9 | 292 | { |
238add52 MW |
293 | struct vme_master master; |
294 | struct vme_slave slave; | |
dca22184 | 295 | struct vme_irq_id irq_req; |
238add52 | 296 | unsigned long copied; |
f00a86d9 | 297 | unsigned int minor = MINOR(inode->i_rdev); |
238add52 MW |
298 | int retval; |
299 | dma_addr_t pci_addr; | |
1a85f207 | 300 | void __user *argp = (void __user *)arg; |
f00a86d9 | 301 | |
f00a86d9 MW |
302 | switch (type[minor]) { |
303 | case CONTROL_MINOR: | |
dca22184 VB |
304 | switch (cmd) { |
305 | case VME_IRQ_GEN: | |
a7f3943c | 306 | copied = copy_from_user(&irq_req, argp, |
93a28666 | 307 | sizeof(irq_req)); |
59a04f11 | 308 | if (copied) { |
0093e5f8 | 309 | pr_warn("Partial copy from userspace\n"); |
dca22184 VB |
310 | return -EFAULT; |
311 | } | |
312 | ||
fc489a52 | 313 | return vme_irq_generate(vme_user_bridge, |
dca22184 VB |
314 | irq_req.level, |
315 | irq_req.statid); | |
dca22184 | 316 | } |
f00a86d9 MW |
317 | break; |
318 | case MASTER_MINOR: | |
f00a86d9 | 319 | switch (cmd) { |
238add52 | 320 | case VME_GET_MASTER: |
93a28666 | 321 | memset(&master, 0, sizeof(master)); |
238add52 MW |
322 | |
323 | /* XXX We do not want to push aspace, cycle and width | |
324 | * to userspace as they are | |
325 | */ | |
326 | retval = vme_master_get(image[minor].resource, | |
86eadace DK |
327 | &master.enable, |
328 | &master.vme_addr, | |
329 | &master.size, &master.aspace, | |
330 | &master.cycle, &master.dwidth); | |
238add52 | 331 | |
1a85f207 | 332 | copied = copy_to_user(argp, &master, |
93a28666 | 333 | sizeof(master)); |
59a04f11 | 334 | if (copied) { |
0093e5f8 | 335 | pr_warn("Partial copy to userspace\n"); |
238add52 MW |
336 | return -EFAULT; |
337 | } | |
f00a86d9 | 338 | |
238add52 | 339 | return retval; |
238add52 MW |
340 | |
341 | case VME_SET_MASTER: | |
342 | ||
c74a804f DK |
343 | if (image[minor].mmap_count != 0) { |
344 | pr_warn("Can't adjust mapped window\n"); | |
345 | return -EPERM; | |
346 | } | |
347 | ||
1a85f207 | 348 | copied = copy_from_user(&master, argp, sizeof(master)); |
59a04f11 | 349 | if (copied) { |
0093e5f8 | 350 | pr_warn("Partial copy from userspace\n"); |
f00a86d9 MW |
351 | return -EFAULT; |
352 | } | |
353 | ||
238add52 MW |
354 | /* XXX We do not want to push aspace, cycle and width |
355 | * to userspace as they are | |
356 | */ | |
357 | return vme_master_set(image[minor].resource, | |
358 | master.enable, master.vme_addr, master.size, | |
359 | master.aspace, master.cycle, master.dwidth); | |
f00a86d9 MW |
360 | |
361 | break; | |
238add52 MW |
362 | } |
363 | break; | |
364 | case SLAVE_MINOR: | |
365 | switch (cmd) { | |
f00a86d9 | 366 | case VME_GET_SLAVE: |
93a28666 | 367 | memset(&slave, 0, sizeof(slave)); |
238add52 MW |
368 | |
369 | /* XXX We do not want to push aspace, cycle and width | |
370 | * to userspace as they are | |
371 | */ | |
372 | retval = vme_slave_get(image[minor].resource, | |
86eadace DK |
373 | &slave.enable, &slave.vme_addr, |
374 | &slave.size, &pci_addr, | |
375 | &slave.aspace, &slave.cycle); | |
238add52 | 376 | |
1a85f207 | 377 | copied = copy_to_user(argp, &slave, |
93a28666 | 378 | sizeof(slave)); |
59a04f11 | 379 | if (copied) { |
0093e5f8 | 380 | pr_warn("Partial copy to userspace\n"); |
238add52 MW |
381 | return -EFAULT; |
382 | } | |
383 | ||
384 | return retval; | |
f00a86d9 | 385 | |
238add52 | 386 | case VME_SET_SLAVE: |
f00a86d9 | 387 | |
1a85f207 | 388 | copied = copy_from_user(&slave, argp, sizeof(slave)); |
59a04f11 | 389 | if (copied) { |
0093e5f8 | 390 | pr_warn("Partial copy from userspace\n"); |
f00a86d9 MW |
391 | return -EFAULT; |
392 | } | |
393 | ||
238add52 MW |
394 | /* XXX We do not want to push aspace, cycle and width |
395 | * to userspace as they are | |
396 | */ | |
397 | return vme_slave_set(image[minor].resource, | |
398 | slave.enable, slave.vme_addr, slave.size, | |
399 | image[minor].pci_buf, slave.aspace, | |
400 | slave.cycle); | |
401 | ||
f00a86d9 | 402 | break; |
f00a86d9 MW |
403 | } |
404 | break; | |
405 | } | |
406 | ||
407 | return -EINVAL; | |
408 | } | |
409 | ||
b1f2ac07 AB |
410 | static long |
411 | vme_user_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
412 | { | |
413 | int ret; | |
0cd189a4 DK |
414 | struct inode *inode = file_inode(file); |
415 | unsigned int minor = MINOR(inode->i_rdev); | |
b1f2ac07 | 416 | |
0cd189a4 DK |
417 | mutex_lock(&image[minor].mutex); |
418 | ret = vme_user_ioctl(inode, file, cmd, arg); | |
419 | mutex_unlock(&image[minor].mutex); | |
b1f2ac07 AB |
420 | |
421 | return ret; | |
422 | } | |
423 | ||
c74a804f DK |
424 | static void vme_user_vm_open(struct vm_area_struct *vma) |
425 | { | |
426 | struct vme_user_vma_priv *vma_priv = vma->vm_private_data; | |
427 | ||
d3cfd5b9 | 428 | refcount_inc(&vma_priv->refcnt); |
c74a804f DK |
429 | } |
430 | ||
431 | static void vme_user_vm_close(struct vm_area_struct *vma) | |
432 | { | |
433 | struct vme_user_vma_priv *vma_priv = vma->vm_private_data; | |
434 | unsigned int minor = vma_priv->minor; | |
435 | ||
d3cfd5b9 | 436 | if (!refcount_dec_and_test(&vma_priv->refcnt)) |
c74a804f DK |
437 | return; |
438 | ||
439 | mutex_lock(&image[minor].mutex); | |
440 | image[minor].mmap_count--; | |
441 | mutex_unlock(&image[minor].mutex); | |
442 | ||
443 | kfree(vma_priv); | |
444 | } | |
445 | ||
e4aea6aa DK |
446 | static const struct vm_operations_struct vme_user_vm_ops = { |
447 | .open = vme_user_vm_open, | |
448 | .close = vme_user_vm_close, | |
449 | }; | |
450 | ||
c74a804f DK |
451 | static int vme_user_master_mmap(unsigned int minor, struct vm_area_struct *vma) |
452 | { | |
453 | int err; | |
454 | struct vme_user_vma_priv *vma_priv; | |
455 | ||
456 | mutex_lock(&image[minor].mutex); | |
457 | ||
458 | err = vme_master_mmap(image[minor].resource, vma); | |
459 | if (err) { | |
460 | mutex_unlock(&image[minor].mutex); | |
461 | return err; | |
462 | } | |
463 | ||
1f0622de | 464 | vma_priv = kmalloc(sizeof(*vma_priv), GFP_KERNEL); |
f99b71be | 465 | if (!vma_priv) { |
c74a804f DK |
466 | mutex_unlock(&image[minor].mutex); |
467 | return -ENOMEM; | |
468 | } | |
469 | ||
470 | vma_priv->minor = minor; | |
d3cfd5b9 | 471 | refcount_set(&vma_priv->refcnt, 1); |
c74a804f DK |
472 | vma->vm_ops = &vme_user_vm_ops; |
473 | vma->vm_private_data = vma_priv; | |
474 | ||
475 | image[minor].mmap_count++; | |
476 | ||
477 | mutex_unlock(&image[minor].mutex); | |
478 | ||
479 | return 0; | |
480 | } | |
481 | ||
482 | static int vme_user_mmap(struct file *file, struct vm_area_struct *vma) | |
483 | { | |
484 | unsigned int minor = MINOR(file_inode(file)->i_rdev); | |
485 | ||
486 | if (type[minor] == MASTER_MINOR) | |
487 | return vme_user_master_mmap(minor, vma); | |
488 | ||
489 | return -ENODEV; | |
490 | } | |
491 | ||
e4aea6aa | 492 | static const struct file_operations vme_user_fops = { |
e4aea6aa DK |
493 | .read = vme_user_read, |
494 | .write = vme_user_write, | |
495 | .llseek = vme_user_llseek, | |
496 | .unlocked_ioctl = vme_user_unlocked_ioctl, | |
497 | .compat_ioctl = vme_user_unlocked_ioctl, | |
498 | .mmap = vme_user_mmap, | |
499 | }; | |
f00a86d9 | 500 | |
5d6abf37 MV |
501 | static int vme_user_match(struct vme_dev *vdev) |
502 | { | |
978f47d6 MW |
503 | int i; |
504 | ||
505 | int cur_bus = vme_bus_num(vdev); | |
d7729f0f | 506 | int cur_slot = vme_slot_num(vdev); |
978f47d6 MW |
507 | |
508 | for (i = 0; i < bus_num; i++) | |
509 | if ((cur_bus == bus[i]) && (cur_slot == vdev->num)) | |
510 | return 1; | |
511 | ||
512 | return 0; | |
5d6abf37 MV |
513 | } |
514 | ||
f00a86d9 | 515 | /* |
238add52 MW |
516 | * In this simple access driver, the old behaviour is being preserved as much |
517 | * as practical. We will therefore reserve the buffers and request the images | |
518 | * here so that we don't have to do it later. | |
f00a86d9 | 519 | */ |
d7e530d2 | 520 | static int vme_user_probe(struct vme_dev *vdev) |
f00a86d9 MW |
521 | { |
522 | int i, err; | |
f1552cbd | 523 | char *name; |
f00a86d9 | 524 | |
238add52 | 525 | /* Save pointer to the bridge device */ |
f99b71be | 526 | if (vme_user_bridge) { |
0093e5f8 | 527 | dev_err(&vdev->dev, "Driver can only be loaded for 1 device\n"); |
238add52 MW |
528 | err = -EINVAL; |
529 | goto err_dev; | |
530 | } | |
8f966dc4 | 531 | vme_user_bridge = vdev; |
f00a86d9 MW |
532 | |
533 | /* Initialise descriptors */ | |
534 | for (i = 0; i < VME_DEVS; i++) { | |
535 | image[i].kern_buf = NULL; | |
536 | image[i].pci_buf = 0; | |
ecb3b80f | 537 | mutex_init(&image[i].mutex); |
f00a86d9 MW |
538 | image[i].device = NULL; |
539 | image[i].resource = NULL; | |
f00a86d9 MW |
540 | } |
541 | ||
f00a86d9 MW |
542 | /* Assign major and minor numbers for the driver */ |
543 | err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS, | |
86eadace | 544 | driver_name); |
f00a86d9 | 545 | if (err) { |
0093e5f8 YT |
546 | dev_warn(&vdev->dev, "Error getting Major Number %d for driver.\n", |
547 | VME_MAJOR); | |
f00a86d9 MW |
548 | goto err_region; |
549 | } | |
550 | ||
551 | /* Register the driver as a char device */ | |
552 | vme_user_cdev = cdev_alloc(); | |
d4113a69 KAM |
553 | if (!vme_user_cdev) { |
554 | err = -ENOMEM; | |
555 | goto err_char; | |
556 | } | |
f00a86d9 MW |
557 | vme_user_cdev->ops = &vme_user_fops; |
558 | vme_user_cdev->owner = THIS_MODULE; | |
559 | err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS); | |
44ae5443 | 560 | if (err) |
f00a86d9 | 561 | goto err_char; |
f00a86d9 MW |
562 | |
563 | /* Request slave resources and allocate buffers (128kB wide) */ | |
564 | for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) { | |
565 | /* XXX Need to properly request attributes */ | |
5188d74c AB |
566 | /* For ca91cx42 bridge there are only two slave windows |
567 | * supporting A16 addressing, so we request A24 supported | |
568 | * by all windows. | |
569 | */ | |
f00a86d9 | 570 | image[i].resource = vme_slave_request(vme_user_bridge, |
9d4c0c9f | 571 | VME_A24, VME_SCT); |
f99b71be | 572 | if (!image[i].resource) { |
0093e5f8 YT |
573 | dev_warn(&vdev->dev, |
574 | "Unable to allocate slave resource\n"); | |
465ff28d | 575 | err = -ENOMEM; |
238add52 | 576 | goto err_slave; |
f00a86d9 MW |
577 | } |
578 | image[i].size_buf = PCI_BUF_SIZE; | |
579 | image[i].kern_buf = vme_alloc_consistent(image[i].resource, | |
9d4c0c9f PL |
580 | image[i].size_buf, |
581 | &image[i].pci_buf); | |
f99b71be | 582 | if (!image[i].kern_buf) { |
0093e5f8 YT |
583 | dev_warn(&vdev->dev, |
584 | "Unable to allocate memory for buffer\n"); | |
f00a86d9 MW |
585 | image[i].pci_buf = 0; |
586 | vme_slave_free(image[i].resource); | |
587 | err = -ENOMEM; | |
238add52 | 588 | goto err_slave; |
f00a86d9 MW |
589 | } |
590 | } | |
591 | ||
592 | /* | |
593 | * Request master resources allocate page sized buffers for small | |
594 | * reads and writes | |
595 | */ | |
596 | for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) { | |
597 | /* XXX Need to properly request attributes */ | |
598 | image[i].resource = vme_master_request(vme_user_bridge, | |
9d4c0c9f PL |
599 | VME_A32, VME_SCT, |
600 | VME_D32); | |
f99b71be | 601 | if (!image[i].resource) { |
0093e5f8 YT |
602 | dev_warn(&vdev->dev, |
603 | "Unable to allocate master resource\n"); | |
465ff28d | 604 | err = -ENOMEM; |
238add52 | 605 | goto err_master; |
f00a86d9 | 606 | } |
33e920d9 AB |
607 | image[i].size_buf = PCI_BUF_SIZE; |
608 | image[i].kern_buf = kmalloc(image[i].size_buf, GFP_KERNEL); | |
f99b71be | 609 | if (!image[i].kern_buf) { |
33e920d9 | 610 | err = -ENOMEM; |
1a524893 DY |
611 | vme_master_free(image[i].resource); |
612 | goto err_master; | |
33e920d9 | 613 | } |
f00a86d9 MW |
614 | } |
615 | ||
616 | /* Create sysfs entries - on udev systems this creates the dev files */ | |
617 | vme_user_sysfs_class = class_create(THIS_MODULE, driver_name); | |
618 | if (IS_ERR(vme_user_sysfs_class)) { | |
0093e5f8 | 619 | dev_err(&vdev->dev, "Error creating vme_user class.\n"); |
f00a86d9 MW |
620 | err = PTR_ERR(vme_user_sysfs_class); |
621 | goto err_class; | |
622 | } | |
623 | ||
624 | /* Add sysfs Entries */ | |
45f9f018 | 625 | for (i = 0; i < VME_DEVS; i++) { |
584721ca | 626 | int num; |
938acb99 | 627 | |
f00a86d9 MW |
628 | switch (type[i]) { |
629 | case MASTER_MINOR: | |
f1552cbd | 630 | name = "bus/vme/m%d"; |
f00a86d9 MW |
631 | break; |
632 | case CONTROL_MINOR: | |
f1552cbd | 633 | name = "bus/vme/ctl"; |
f00a86d9 MW |
634 | break; |
635 | case SLAVE_MINOR: | |
f1552cbd | 636 | name = "bus/vme/s%d"; |
f00a86d9 MW |
637 | break; |
638 | default: | |
639 | err = -EINVAL; | |
640 | goto err_sysfs; | |
f00a86d9 MW |
641 | } |
642 | ||
584721ca VB |
643 | num = (type[i] == SLAVE_MINOR) ? i - (MASTER_MAX + 1) : i; |
644 | image[i].device = device_create(vme_user_sysfs_class, NULL, | |
9d4c0c9f PL |
645 | MKDEV(VME_MAJOR, i), NULL, |
646 | name, num); | |
f00a86d9 | 647 | if (IS_ERR(image[i].device)) { |
0093e5f8 | 648 | dev_info(&vdev->dev, "Error creating sysfs device\n"); |
f00a86d9 MW |
649 | err = PTR_ERR(image[i].device); |
650 | goto err_sysfs; | |
651 | } | |
652 | } | |
653 | ||
f00a86d9 MW |
654 | return 0; |
655 | ||
f00a86d9 | 656 | err_sysfs: |
45f9f018 | 657 | while (i > 0) { |
f00a86d9 MW |
658 | i--; |
659 | device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i)); | |
660 | } | |
661 | class_destroy(vme_user_sysfs_class); | |
662 | ||
1fede020 | 663 | /* Ensure counter set correctly to unalloc all master windows */ |
238add52 MW |
664 | i = MASTER_MAX + 1; |
665 | err_master: | |
666 | while (i > MASTER_MINOR) { | |
667 | i--; | |
1a524893 | 668 | kfree(image[i].kern_buf); |
238add52 MW |
669 | vme_master_free(image[i].resource); |
670 | } | |
671 | ||
672 | /* | |
1fede020 | 673 | * Ensure counter set correctly to unalloc all slave windows and buffers |
238add52 | 674 | */ |
f00a86d9 | 675 | i = SLAVE_MAX + 1; |
238add52 MW |
676 | err_slave: |
677 | while (i > SLAVE_MINOR) { | |
f00a86d9 | 678 | i--; |
625a9e01 DK |
679 | vme_free_consistent(image[i].resource, image[i].size_buf, |
680 | image[i].kern_buf, image[i].pci_buf); | |
1daa38d3 | 681 | vme_slave_free(image[i].resource); |
f00a86d9 MW |
682 | } |
683 | err_class: | |
684 | cdev_del(vme_user_cdev); | |
685 | err_char: | |
686 | unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS); | |
687 | err_region: | |
238add52 | 688 | err_dev: |
f00a86d9 MW |
689 | return err; |
690 | } | |
691 | ||
f21a8247 | 692 | static int vme_user_remove(struct vme_dev *dev) |
f00a86d9 MW |
693 | { |
694 | int i; | |
695 | ||
696 | /* Remove sysfs Entries */ | |
ecb3b80f SN |
697 | for (i = 0; i < VME_DEVS; i++) { |
698 | mutex_destroy(&image[i].mutex); | |
f00a86d9 | 699 | device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i)); |
ecb3b80f | 700 | } |
f00a86d9 MW |
701 | class_destroy(vme_user_sysfs_class); |
702 | ||
b62c99b1 | 703 | for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) { |
33e920d9 | 704 | kfree(image[i].kern_buf); |
b62c99b1 EC |
705 | vme_master_free(image[i].resource); |
706 | } | |
33e920d9 | 707 | |
f00a86d9 | 708 | for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) { |
238add52 | 709 | vme_slave_set(image[i].resource, 0, 0, 0, 0, VME_A32, 0); |
625a9e01 DK |
710 | vme_free_consistent(image[i].resource, image[i].size_buf, |
711 | image[i].kern_buf, image[i].pci_buf); | |
1daa38d3 | 712 | vme_slave_free(image[i].resource); |
f00a86d9 MW |
713 | } |
714 | ||
715 | /* Unregister device driver */ | |
716 | cdev_del(vme_user_cdev); | |
717 | ||
1fede020 | 718 | /* Unregister the major and minor device numbers */ |
f00a86d9 | 719 | unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS); |
238add52 MW |
720 | |
721 | return 0; | |
f00a86d9 MW |
722 | } |
723 | ||
e4aea6aa DK |
724 | static struct vme_driver vme_user_driver = { |
725 | .name = driver_name, | |
726 | .match = vme_user_match, | |
727 | .probe = vme_user_probe, | |
728 | .remove = vme_user_remove, | |
729 | }; | |
730 | ||
731 | static int __init vme_user_init(void) | |
732 | { | |
733 | int retval = 0; | |
734 | ||
735 | pr_info("VME User Space Access Driver\n"); | |
736 | ||
737 | if (bus_num == 0) { | |
738 | pr_err("No cards, skipping registration\n"); | |
739 | retval = -ENODEV; | |
740 | goto err_nocard; | |
741 | } | |
742 | ||
743 | /* Let's start by supporting one bus, we can support more than one | |
744 | * in future revisions if that ever becomes necessary. | |
745 | */ | |
746 | if (bus_num > VME_USER_BUS_MAX) { | |
747 | pr_err("Driver only able to handle %d buses\n", | |
748 | VME_USER_BUS_MAX); | |
749 | bus_num = VME_USER_BUS_MAX; | |
750 | } | |
751 | ||
752 | /* | |
753 | * Here we just register the maximum number of devices we can and | |
754 | * leave vme_user_match() to allow only 1 to go through to probe(). | |
755 | * This way, if we later want to allow multiple user access devices, | |
756 | * we just change the code in vme_user_match(). | |
757 | */ | |
758 | retval = vme_register_driver(&vme_user_driver, VME_MAX_SLOTS); | |
59a04f11 | 759 | if (retval) |
e4aea6aa DK |
760 | goto err_reg; |
761 | ||
762 | return retval; | |
763 | ||
764 | err_reg: | |
765 | err_nocard: | |
766 | return retval; | |
767 | } | |
768 | ||
238add52 MW |
769 | static void __exit vme_user_exit(void) |
770 | { | |
771 | vme_unregister_driver(&vme_user_driver); | |
238add52 MW |
772 | } |
773 | ||
238add52 | 774 | MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the driver is connected"); |
65f5c3ea | 775 | module_param_array(bus, int, &bus_num, 0000); |
238add52 | 776 | |
f00a86d9 | 777 | MODULE_DESCRIPTION("VME User Space Access Driver"); |
66bd8db5 | 778 | MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com"); |
f00a86d9 MW |
779 | MODULE_LICENSE("GPL"); |
780 | ||
238add52 MW |
781 | module_init(vme_user_init); |
782 | module_exit(vme_user_exit); |