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