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