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