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