drm/radeon/kms: enable use of unmappable VRAM V2
[pandora-kernel.git] / drivers / staging / vme / devices / vme_user.c
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/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         loff_t absolute = -1;
404         unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
405         size_t image_size;
406
407         down(&image[minor].sem);
408         image_size = vme_get_size(image[minor].resource);
409
410         switch (whence) {
411         case SEEK_SET:
412                 absolute = off;
413                 break;
414         case SEEK_CUR:
415                 absolute = file->f_pos + off;
416                 break;
417         case SEEK_END:
418                 absolute = image_size + off;
419                 break;
420         default:
421                 up(&image[minor].sem);
422                 return -EINVAL;
423                 break;
424         }
425
426         if ((absolute < 0) || (absolute >= image_size)) {
427                 up(&image[minor].sem);
428                 return -EINVAL;
429         }
430
431         file->f_pos = absolute;
432
433         up(&image[minor].sem);
434
435         return absolute;
436 }
437
438 /*
439  * The ioctls provided by the old VME access method (the one at vmelinux.org)
440  * are most certainly wrong as the effectively push the registers layout
441  * through to user space. Given that the VME core can handle multiple bridges,
442  * with different register layouts this is most certainly not the way to go.
443  *
444  * We aren't using the structures defined in the Motorola driver either - these
445  * are also quite low level, however we should use the definitions that have
446  * already been defined.
447  */
448 static int vme_user_ioctl(struct inode *inode, struct file *file,
449         unsigned int cmd, unsigned long arg)
450 {
451         struct vme_master master;
452         struct vme_slave slave;
453         unsigned long copied;
454         unsigned int minor = MINOR(inode->i_rdev);
455         int retval;
456         dma_addr_t pci_addr;
457
458         statistics.ioctls++;
459
460         switch (type[minor]) {
461         case CONTROL_MINOR:
462                 break;
463         case MASTER_MINOR:
464                 switch (cmd) {
465                 case VME_GET_MASTER:
466                         memset(&master, 0, sizeof(struct vme_master));
467
468                         /* XXX  We do not want to push aspace, cycle and width
469                          *      to userspace as they are
470                          */
471                         retval = vme_master_get(image[minor].resource,
472                                 &(master.enable), &(master.vme_addr),
473                                 &(master.size), &(master.aspace),
474                                 &(master.cycle), &(master.dwidth));
475
476                         copied = copy_to_user((char *)arg, &master,
477                                 sizeof(struct vme_master));
478                         if (copied != 0) {
479                                 printk(KERN_WARNING "Partial copy to "
480                                         "userspace\n");
481                                 return -EFAULT;
482                         }
483
484                         return retval;
485                         break;
486
487                 case VME_SET_MASTER:
488
489                         copied = copy_from_user(&master, (char *)arg,
490                                 sizeof(master));
491                         if (copied != 0) {
492                                 printk(KERN_WARNING "Partial copy from "
493                                         "userspace\n");
494                                 return -EFAULT;
495                         }
496
497                         /* XXX  We do not want to push aspace, cycle and width
498                          *      to userspace as they are
499                          */
500                         return vme_master_set(image[minor].resource,
501                                 master.enable, master.vme_addr, master.size,
502                                 master.aspace, master.cycle, master.dwidth);
503
504                         break;
505                 }
506                 break;
507         case SLAVE_MINOR:
508                 switch (cmd) {
509                 case VME_GET_SLAVE:
510                         memset(&slave, 0, sizeof(struct vme_slave));
511
512                         /* XXX  We do not want to push aspace, cycle and width
513                          *      to userspace as they are
514                          */
515                         retval = vme_slave_get(image[minor].resource,
516                                 &(slave.enable), &(slave.vme_addr),
517                                 &(slave.size), &pci_addr, &(slave.aspace),
518                                 &(slave.cycle));
519
520                         copied = copy_to_user((char *)arg, &slave,
521                                 sizeof(struct vme_slave));
522                         if (copied != 0) {
523                                 printk(KERN_WARNING "Partial copy to "
524                                         "userspace\n");
525                                 return -EFAULT;
526                         }
527
528                         return retval;
529                         break;
530
531                 case VME_SET_SLAVE:
532
533                         copied = copy_from_user(&slave, (char *)arg,
534                                 sizeof(slave));
535                         if (copied != 0) {
536                                 printk(KERN_WARNING "Partial copy from "
537                                         "userspace\n");
538                                 return -EFAULT;
539                         }
540
541                         /* XXX  We do not want to push aspace, cycle and width
542                          *      to userspace as they are
543                          */
544                         return vme_slave_set(image[minor].resource,
545                                 slave.enable, slave.vme_addr, slave.size,
546                                 image[minor].pci_buf, slave.aspace,
547                                 slave.cycle);
548
549                         break;
550                 }
551                 break;
552         }
553
554         return -EINVAL;
555 }
556
557
558 /*
559  * Unallocate a previously allocated buffer
560  */
561 static void buf_unalloc (int num)
562 {
563         if (image[num].kern_buf) {
564 #ifdef VME_DEBUG
565                 printk(KERN_DEBUG "UniverseII:Releasing buffer at %p\n",
566                         image[num].pci_buf);
567 #endif
568
569                 vme_free_consistent(image[num].resource, image[num].size_buf,
570                         image[num].kern_buf, image[num].pci_buf);
571
572                 image[num].kern_buf = NULL;
573                 image[num].pci_buf = 0;
574                 image[num].size_buf = 0;
575
576 #ifdef VME_DEBUG
577         } else {
578                 printk(KERN_DEBUG "UniverseII: Buffer not allocated\n");
579 #endif
580         }
581 }
582
583 static struct vme_driver vme_user_driver = {
584         .name = driver_name,
585         .probe = vme_user_probe,
586         .remove = vme_user_remove,
587 };
588
589
590 static int __init vme_user_init(void)
591 {
592         int retval = 0;
593         int i;
594         struct vme_device_id *ids;
595
596         printk(KERN_INFO "VME User Space Access Driver\n");
597
598         if (bus_num == 0) {
599                 printk(KERN_ERR "%s: No cards, skipping registration\n",
600                         driver_name);
601                 goto err_nocard;
602         }
603
604         /* Let's start by supporting one bus, we can support more than one
605          * in future revisions if that ever becomes necessary.
606          */
607         if (bus_num > USER_BUS_MAX) {
608                 printk(KERN_ERR "%s: Driver only able to handle %d buses\n",
609                         driver_name, USER_BUS_MAX);
610                 bus_num = USER_BUS_MAX;
611         }
612
613
614         /* Dynamically create the bind table based on module parameters */
615         ids = kmalloc(sizeof(struct vme_device_id) * (bus_num + 1), GFP_KERNEL);
616         if (ids == NULL) {
617                 printk(KERN_ERR "%s: Unable to allocate ID table\n",
618                         driver_name);
619                 goto err_id;
620         }
621
622         memset(ids, 0, (sizeof(struct vme_device_id) * (bus_num + 1)));
623
624         for (i = 0; i < bus_num; i++) {
625                 ids[i].bus = bus[i];
626                 /*
627                  * We register the driver against the slot occupied by *this*
628                  * card, since it's really a low level way of controlling
629                  * the VME bridge
630                  */
631                 ids[i].slot = VME_SLOT_CURRENT;
632         }
633
634         vme_user_driver.bind_table = ids;
635
636         retval = vme_register_driver(&vme_user_driver);
637         if (retval != 0)
638                 goto err_reg;
639
640         return retval;
641
642         vme_unregister_driver(&vme_user_driver);
643 err_reg:
644         kfree(ids);
645 err_id:
646 err_nocard:
647         return retval;
648 }
649
650 /*
651  * In this simple access driver, the old behaviour is being preserved as much
652  * as practical. We will therefore reserve the buffers and request the images
653  * here so that we don't have to do it later.
654  */
655 static int __init vme_user_probe(struct device *dev, int cur_bus, int cur_slot)
656 {
657         int i, err;
658         char name[12];
659
660         /* Save pointer to the bridge device */
661         if (vme_user_bridge != NULL) {
662                 printk(KERN_ERR "%s: Driver can only be loaded for 1 device\n",
663                         driver_name);
664                 err = -EINVAL;
665                 goto err_dev;
666         }
667         vme_user_bridge = dev;
668
669         /* Initialise descriptors */
670         for (i = 0; i < VME_DEVS; i++) {
671                 image[i].kern_buf = NULL;
672                 image[i].pci_buf = 0;
673                 init_MUTEX(&(image[i].sem));
674                 image[i].device = NULL;
675                 image[i].resource = NULL;
676                 image[i].users = 0;
677         }
678
679         /* Initialise statistics counters */
680         reset_counters();
681
682         /* Assign major and minor numbers for the driver */
683         err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS,
684                 driver_name);
685         if (err) {
686                 printk(KERN_WARNING "%s: Error getting Major Number %d for "
687                 "driver.\n", driver_name, VME_MAJOR);
688                 goto err_region;
689         }
690
691         /* Register the driver as a char device */
692         vme_user_cdev = cdev_alloc();
693         vme_user_cdev->ops = &vme_user_fops;
694         vme_user_cdev->owner = THIS_MODULE;
695         err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS);
696         if (err) {
697                 printk(KERN_WARNING "%s: cdev_all failed\n", driver_name);
698                 goto err_char;
699         }
700
701         /* Request slave resources and allocate buffers (128kB wide) */
702         for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
703                 /* XXX Need to properly request attributes */
704                 /* For ca91cx42 bridge there are only two slave windows
705                  * supporting A16 addressing, so we request A24 supported
706                  * by all windows.
707                  */
708                 image[i].resource = vme_slave_request(vme_user_bridge,
709                         VME_A24, VME_SCT);
710                 if (image[i].resource == NULL) {
711                         printk(KERN_WARNING "Unable to allocate slave "
712                                 "resource\n");
713                         goto err_slave;
714                 }
715                 image[i].size_buf = PCI_BUF_SIZE;
716                 image[i].kern_buf = vme_alloc_consistent(image[i].resource,
717                         image[i].size_buf, &(image[i].pci_buf));
718                 if (image[i].kern_buf == NULL) {
719                         printk(KERN_WARNING "Unable to allocate memory for "
720                                 "buffer\n");
721                         image[i].pci_buf = 0;
722                         vme_slave_free(image[i].resource);
723                         err = -ENOMEM;
724                         goto err_slave;
725                 }
726         }
727
728         /*
729          * Request master resources allocate page sized buffers for small
730          * reads and writes
731          */
732         for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
733                 /* XXX Need to properly request attributes */
734                 image[i].resource = vme_master_request(vme_user_bridge,
735                         VME_A32, VME_SCT, VME_D32);
736                 if (image[i].resource == NULL) {
737                         printk(KERN_WARNING "Unable to allocate master "
738                                 "resource\n");
739                         goto err_master;
740                 }
741                 image[i].size_buf = PCI_BUF_SIZE;
742                 image[i].kern_buf = kmalloc(image[i].size_buf, GFP_KERNEL);
743                 if (image[i].kern_buf == NULL) {
744                         printk(KERN_WARNING "Unable to allocate memory for "
745                                 "master window buffers\n");
746                         err = -ENOMEM;
747                         goto err_master_buf;
748                 }
749         }
750
751         /* Create sysfs entries - on udev systems this creates the dev files */
752         vme_user_sysfs_class = class_create(THIS_MODULE, driver_name);
753         if (IS_ERR(vme_user_sysfs_class)) {
754                 printk(KERN_ERR "Error creating vme_user class.\n");
755                 err = PTR_ERR(vme_user_sysfs_class);
756                 goto err_class;
757         }
758
759         /* Add sysfs Entries */
760         for (i=0; i<VME_DEVS; i++) {
761                 switch (type[i]) {
762                 case MASTER_MINOR:
763                         sprintf(name,"bus/vme/m%%d");
764                         break;
765                 case CONTROL_MINOR:
766                         sprintf(name,"bus/vme/ctl");
767                         break;
768                 case SLAVE_MINOR:
769                         sprintf(name,"bus/vme/s%%d");
770                         break;
771                 default:
772                         err = -EINVAL;
773                         goto err_sysfs;
774                         break;
775                 }
776
777                 image[i].device =
778                         device_create(vme_user_sysfs_class, NULL,
779                                 MKDEV(VME_MAJOR, i), NULL, name,
780                                 (type[i] == SLAVE_MINOR)? i - (MASTER_MAX + 1) : i);
781                 if (IS_ERR(image[i].device)) {
782                         printk("%s: Error creating sysfs device\n",
783                                 driver_name);
784                         err = PTR_ERR(image[i].device);
785                         goto err_sysfs;
786                 }
787         }
788
789         return 0;
790
791         /* Ensure counter set correcty to destroy all sysfs devices */
792         i = VME_DEVS;
793 err_sysfs:
794         while (i > 0){
795                 i--;
796                 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
797         }
798         class_destroy(vme_user_sysfs_class);
799
800         /* Ensure counter set correcty to unalloc all master windows */
801         i = MASTER_MAX + 1;
802 err_master_buf:
803         for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++)
804                 kfree(image[i].kern_buf);
805 err_master:
806         while (i > MASTER_MINOR) {
807                 i--;
808                 vme_master_free(image[i].resource);
809         }
810
811         /*
812          * Ensure counter set correcty to unalloc all slave windows and buffers
813          */
814         i = SLAVE_MAX + 1;
815 err_slave:
816         while (i > SLAVE_MINOR) {
817                 i--;
818                 vme_slave_free(image[i].resource);
819                 buf_unalloc(i);
820         }
821 err_class:
822         cdev_del(vme_user_cdev);
823 err_char:
824         unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
825 err_region:
826 err_dev:
827         return err;
828 }
829
830 static int __exit vme_user_remove(struct device *dev, int cur_bus, int cur_slot)
831 {
832         int i;
833
834         /* Remove sysfs Entries */
835         for(i=0; i<VME_DEVS; i++) {
836                 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
837         }
838         class_destroy(vme_user_sysfs_class);
839
840         for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++)
841                 kfree(image[i].kern_buf);
842
843         for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
844                 vme_slave_set(image[i].resource, 0, 0, 0, 0, VME_A32, 0);
845                 vme_slave_free(image[i].resource);
846                 buf_unalloc(i);
847         }
848
849         /* Unregister device driver */
850         cdev_del(vme_user_cdev);
851
852         /* Unregiser the major and minor device numbers */
853         unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
854
855         return 0;
856 }
857
858 static void __exit vme_user_exit(void)
859 {
860         vme_unregister_driver(&vme_user_driver);
861
862         kfree(vme_user_driver.bind_table);
863 }
864
865
866 MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the driver is connected");
867 module_param_array(bus, int, &bus_num, 0);
868
869 MODULE_DESCRIPTION("VME User Space Access Driver");
870 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
871 MODULE_LICENSE("GPL");
872
873 module_init(vme_user_init);
874 module_exit(vme_user_exit);