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