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