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