Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vme_user.c
Go to the documentation of this file.
1 /*
2  * VMEbus User access driver
3  *
4  * Author: Martyn Welch <[email protected]>
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/mutex.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/syscalls.h>
34 #include <linux/types.h>
35 
36 #include <linux/io.h>
37 #include <linux/uaccess.h>
38 #include <linux/vme.h>
39 
40 #include "vme_user.h"
41 
42 static DEFINE_MUTEX(vme_user_mutex);
43 static const char driver_name[] = "vme_user";
44 
45 static int bus[VME_USER_BUS_MAX];
46 static unsigned int bus_num;
47 
48 /* Currently Documentation/devices.txt defines the following for VME:
49  *
50  * 221 char VME bus
51  * 0 = /dev/bus/vme/m0 First master image
52  * 1 = /dev/bus/vme/m1 Second master image
53  * 2 = /dev/bus/vme/m2 Third master image
54  * 3 = /dev/bus/vme/m3 Fourth master image
55  * 4 = /dev/bus/vme/s0 First slave image
56  * 5 = /dev/bus/vme/s1 Second slave image
57  * 6 = /dev/bus/vme/s2 Third slave image
58  * 7 = /dev/bus/vme/s3 Fourth slave image
59  * 8 = /dev/bus/vme/ctl Control
60  *
61  * It is expected that all VME bus drivers will use the
62  * same interface. For interface documentation see
63  * http://www.vmelinux.org/.
64  *
65  * However the VME driver at http://www.vmelinux.org/ is rather old and doesn't
66  * even support the tsi148 chipset (which has 8 master and 8 slave windows).
67  * We'll run with this for now as far as possible, however it probably makes
68  * sense to get rid of the old mappings and just do everything dynamically.
69  *
70  * So for now, we'll restrict the driver to providing 4 masters and 4 slaves as
71  * defined above and try to support at least some of the interface from
72  * http://www.vmelinux.org/ as an alternative the driver can be written
73  * providing a saner interface later.
74  *
75  * The vmelinux.org driver never supported slave images, the devices reserved
76  * for slaves were repurposed to support all 8 master images on the UniverseII!
77  * We shall support 4 masters and 4 slaves with this driver.
78  */
79 #define VME_MAJOR 221 /* VME Major Device Number */
80 #define VME_DEVS 9 /* Number of dev entries */
81 
82 #define MASTER_MINOR 0
83 #define MASTER_MAX 3
84 #define SLAVE_MINOR 4
85 #define SLAVE_MAX 7
86 #define CONTROL_MINOR 8
87 
88 #define PCI_BUF_SIZE 0x20000 /* Size of one slave image buffer */
89 
90 /*
91  * Structure to handle image related parameters.
92  */
93 struct image_desc {
94  void *kern_buf; /* Buffer address in kernel space */
95  dma_addr_t pci_buf; /* Buffer address in PCI address space */
96  unsigned long long size_buf; /* Buffer size */
97  struct mutex mutex; /* Mutex for locking image */
98  struct device *device; /* Sysfs device */
99  struct vme_resource *resource; /* VME resource */
100  int users; /* Number of current users */
101 };
102 static struct image_desc image[VME_DEVS];
103 
104 struct driver_stats {
105  unsigned long reads;
106  unsigned long writes;
107  unsigned long ioctls;
108  unsigned long irqs;
109  unsigned long berrs;
110  unsigned long dmaErrors;
111  unsigned long timeouts;
112  unsigned long external;
113 };
114 static struct driver_stats statistics;
115 
116 static struct cdev *vme_user_cdev; /* Character device */
117 static struct class *vme_user_sysfs_class; /* Sysfs class */
118 static struct vme_dev *vme_user_bridge; /* Pointer to user device */
119 
120 
121 static const int type[VME_DEVS] = { MASTER_MINOR, MASTER_MINOR,
126  };
127 
128 
129 static int vme_user_open(struct inode *, struct file *);
130 static int vme_user_release(struct inode *, struct file *);
131 static ssize_t vme_user_read(struct file *, char __user *, size_t, loff_t *);
132 static ssize_t vme_user_write(struct file *, const char __user *, size_t,
133  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 vme_user_match(struct vme_dev *);
138 static int __devinit vme_user_probe(struct vme_dev *);
139 static int __devexit vme_user_remove(struct vme_dev *);
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  mutex_lock(&image[minor].mutex);
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  mutex_unlock(&image[minor].mutex);
182 
183  return 0;
184 
185 err_res:
186  mutex_unlock(&image[minor].mutex);
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  mutex_lock(&image[minor].mutex);
196 
197  /* Decrement user count */
198  image[minor].users--;
199 
200  mutex_unlock(&image[minor].mutex);
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 to 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  mutex_lock(&image[minor].mutex);
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  mutex_unlock(&image[minor].mutex);
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  mutex_unlock(&image[minor].mutex);
356  if (retval > 0)
357  *ppos += retval;
358 
359  return retval;
360 }
361 
362 static ssize_t vme_user_write(struct file *file, const char __user *buf,
363  size_t count, loff_t *ppos)
364 {
365  unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
366  ssize_t retval;
367  size_t image_size;
368  size_t okcount;
369 
370  if (minor == CONTROL_MINOR)
371  return 0;
372 
373  mutex_lock(&image[minor].mutex);
374 
375  image_size = vme_get_size(image[minor].resource);
376 
377  /* Ensure we are starting at a valid location */
378  if ((*ppos < 0) || (*ppos > (image_size - 1))) {
379  mutex_unlock(&image[minor].mutex);
380  return 0;
381  }
382 
383  /* Ensure not reading past end of the image */
384  if (*ppos + count > image_size)
385  okcount = image_size - *ppos;
386  else
387  okcount = count;
388 
389  switch (type[minor]) {
390  case MASTER_MINOR:
391  retval = resource_from_user(minor, buf, okcount, ppos);
392  break;
393  case SLAVE_MINOR:
394  retval = buffer_from_user(minor, buf, okcount, ppos);
395  break;
396  default:
397  retval = -EINVAL;
398  }
399 
400  mutex_unlock(&image[minor].mutex);
401 
402  if (retval > 0)
403  *ppos += retval;
404 
405  return retval;
406 }
407 
408 static loff_t vme_user_llseek(struct file *file, loff_t off, int whence)
409 {
410  loff_t absolute = -1;
411  unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
412  size_t image_size;
413 
414  if (minor == CONTROL_MINOR)
415  return -EINVAL;
416 
417  mutex_lock(&image[minor].mutex);
418  image_size = vme_get_size(image[minor].resource);
419 
420  switch (whence) {
421  case SEEK_SET:
422  absolute = off;
423  break;
424  case SEEK_CUR:
425  absolute = file->f_pos + off;
426  break;
427  case SEEK_END:
428  absolute = image_size + off;
429  break;
430  default:
431  mutex_unlock(&image[minor].mutex);
432  return -EINVAL;
433  break;
434  }
435 
436  if ((absolute < 0) || (absolute >= image_size)) {
437  mutex_unlock(&image[minor].mutex);
438  return -EINVAL;
439  }
440 
441  file->f_pos = absolute;
442 
443  mutex_unlock(&image[minor].mutex);
444 
445  return absolute;
446 }
447 
448 /*
449  * The ioctls provided by the old VME access method (the one at vmelinux.org)
450  * are most certainly wrong as the effectively push the registers layout
451  * through to user space. Given that the VME core can handle multiple bridges,
452  * with different register layouts this is most certainly not the way to go.
453  *
454  * We aren't using the structures defined in the Motorola driver either - these
455  * are also quite low level, however we should use the definitions that have
456  * already been defined.
457  */
458 static int vme_user_ioctl(struct inode *inode, struct file *file,
459  unsigned int cmd, unsigned long arg)
460 {
461  struct vme_master master;
462  struct vme_slave slave;
463  struct vme_irq_id irq_req;
464  unsigned long copied;
465  unsigned int minor = MINOR(inode->i_rdev);
466  int retval;
468  void __user *argp = (void __user *)arg;
469 
470  statistics.ioctls++;
471 
472  switch (type[minor]) {
473  case CONTROL_MINOR:
474  switch (cmd) {
475  case VME_IRQ_GEN:
476  copied = copy_from_user(&irq_req, argp,
477  sizeof(struct vme_irq_id));
478  if (copied != 0) {
479  printk(KERN_WARNING "Partial copy from userspace\n");
480  return -EFAULT;
481  }
482 
483  retval = vme_irq_generate(vme_user_bridge,
484  irq_req.level,
485  irq_req.statid);
486 
487  return retval;
488  }
489  break;
490  case MASTER_MINOR:
491  switch (cmd) {
492  case VME_GET_MASTER:
493  memset(&master, 0, sizeof(struct vme_master));
494 
495  /* XXX We do not want to push aspace, cycle and width
496  * to userspace as they are
497  */
498  retval = vme_master_get(image[minor].resource,
499  &master.enable, &master.vme_addr,
500  &master.size, &master.aspace,
501  &master.cycle, &master.dwidth);
502 
503  copied = copy_to_user(argp, &master,
504  sizeof(struct vme_master));
505  if (copied != 0) {
506  printk(KERN_WARNING "Partial copy to "
507  "userspace\n");
508  return -EFAULT;
509  }
510 
511  return retval;
512  break;
513 
514  case VME_SET_MASTER:
515 
516  copied = copy_from_user(&master, argp, sizeof(master));
517  if (copied != 0) {
518  printk(KERN_WARNING "Partial copy from "
519  "userspace\n");
520  return -EFAULT;
521  }
522 
523  /* XXX We do not want to push aspace, cycle and width
524  * to userspace as they are
525  */
526  return vme_master_set(image[minor].resource,
527  master.enable, master.vme_addr, master.size,
528  master.aspace, master.cycle, master.dwidth);
529 
530  break;
531  }
532  break;
533  case SLAVE_MINOR:
534  switch (cmd) {
535  case VME_GET_SLAVE:
536  memset(&slave, 0, sizeof(struct vme_slave));
537 
538  /* XXX We do not want to push aspace, cycle and width
539  * to userspace as they are
540  */
541  retval = vme_slave_get(image[minor].resource,
542  &slave.enable, &slave.vme_addr,
543  &slave.size, &pci_addr, &slave.aspace,
544  &slave.cycle);
545 
546  copied = copy_to_user(argp, &slave,
547  sizeof(struct vme_slave));
548  if (copied != 0) {
549  printk(KERN_WARNING "Partial copy to "
550  "userspace\n");
551  return -EFAULT;
552  }
553 
554  return retval;
555  break;
556 
557  case VME_SET_SLAVE:
558 
559  copied = copy_from_user(&slave, argp, sizeof(slave));
560  if (copied != 0) {
561  printk(KERN_WARNING "Partial copy from "
562  "userspace\n");
563  return -EFAULT;
564  }
565 
566  /* XXX We do not want to push aspace, cycle and width
567  * to userspace as they are
568  */
569  return vme_slave_set(image[minor].resource,
570  slave.enable, slave.vme_addr, slave.size,
571  image[minor].pci_buf, slave.aspace,
572  slave.cycle);
573 
574  break;
575  }
576  break;
577  }
578 
579  return -EINVAL;
580 }
581 
582 static long
583 vme_user_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
584 {
585  int ret;
586 
587  mutex_lock(&vme_user_mutex);
588  ret = vme_user_ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
589  mutex_unlock(&vme_user_mutex);
590 
591  return ret;
592 }
593 
594 
595 /*
596  * Unallocate a previously allocated buffer
597  */
598 static void buf_unalloc(int num)
599 {
600  if (image[num].kern_buf) {
601 #ifdef VME_DEBUG
602  printk(KERN_DEBUG "UniverseII:Releasing buffer at %p\n",
603  image[num].pci_buf);
604 #endif
605 
606  vme_free_consistent(image[num].resource, image[num].size_buf,
607  image[num].kern_buf, image[num].pci_buf);
608 
609  image[num].kern_buf = NULL;
610  image[num].pci_buf = 0;
611  image[num].size_buf = 0;
612 
613 #ifdef VME_DEBUG
614  } else {
615  printk(KERN_DEBUG "UniverseII: Buffer not allocated\n");
616 #endif
617  }
618 }
619 
620 static struct vme_driver vme_user_driver = {
621  .name = driver_name,
622  .match = vme_user_match,
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 
632  printk(KERN_INFO "VME User Space Access Driver\n");
633 
634  if (bus_num == 0) {
635  printk(KERN_ERR "%s: No cards, skipping registration\n",
636  driver_name);
637  retval = -ENODEV;
638  goto err_nocard;
639  }
640 
641  /* Let's start by supporting one bus, we can support more than one
642  * in future revisions if that ever becomes necessary.
643  */
644  if (bus_num > VME_USER_BUS_MAX) {
645  printk(KERN_ERR "%s: Driver only able to handle %d buses\n",
647  bus_num = VME_USER_BUS_MAX;
648  }
649 
650  /*
651  * Here we just register the maximum number of devices we can and
652  * leave vme_user_match() to allow only 1 to go through to probe().
653  * This way, if we later want to allow multiple user access devices,
654  * we just change the code in vme_user_match().
655  */
656  retval = vme_register_driver(&vme_user_driver, VME_MAX_SLOTS);
657  if (retval != 0)
658  goto err_reg;
659 
660  return retval;
661 
662 err_reg:
663 err_nocard:
664  return retval;
665 }
666 
667 static int vme_user_match(struct vme_dev *vdev)
668 {
669  if (vdev->num >= VME_USER_BUS_MAX)
670  return 0;
671  return 1;
672 }
673 
674 /*
675  * In this simple access driver, the old behaviour is being preserved as much
676  * as practical. We will therefore reserve the buffers and request the images
677  * here so that we don't have to do it later.
678  */
679 static int __devinit vme_user_probe(struct vme_dev *vdev)
680 {
681  int i, err;
682  char name[12];
683 
684  /* Save pointer to the bridge device */
685  if (vme_user_bridge != NULL) {
686  printk(KERN_ERR "%s: Driver can only be loaded for 1 device\n",
687  driver_name);
688  err = -EINVAL;
689  goto err_dev;
690  }
691  vme_user_bridge = vdev;
692 
693  /* Initialise descriptors */
694  for (i = 0; i < VME_DEVS; i++) {
695  image[i].kern_buf = NULL;
696  image[i].pci_buf = 0;
697  mutex_init(&image[i].mutex);
698  image[i].device = NULL;
699  image[i].resource = NULL;
700  image[i].users = 0;
701  }
702 
703  /* Initialise statistics counters */
704  reset_counters();
705 
706  /* Assign major and minor numbers for the driver */
707  err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS,
708  driver_name);
709  if (err) {
710  printk(KERN_WARNING "%s: Error getting Major Number %d for "
711  "driver.\n", driver_name, VME_MAJOR);
712  goto err_region;
713  }
714 
715  /* Register the driver as a char device */
716  vme_user_cdev = cdev_alloc();
717  vme_user_cdev->ops = &vme_user_fops;
718  vme_user_cdev->owner = THIS_MODULE;
719  err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS);
720  if (err) {
721  printk(KERN_WARNING "%s: cdev_all failed\n", driver_name);
722  goto err_char;
723  }
724 
725  /* Request slave resources and allocate buffers (128kB wide) */
726  for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
727  /* XXX Need to properly request attributes */
728  /* For ca91cx42 bridge there are only two slave windows
729  * supporting A16 addressing, so we request A24 supported
730  * by all windows.
731  */
732  image[i].resource = vme_slave_request(vme_user_bridge,
733  VME_A24, VME_SCT);
734  if (image[i].resource == NULL) {
735  printk(KERN_WARNING "Unable to allocate slave "
736  "resource\n");
737  goto err_slave;
738  }
739  image[i].size_buf = PCI_BUF_SIZE;
740  image[i].kern_buf = vme_alloc_consistent(image[i].resource,
741  image[i].size_buf, &image[i].pci_buf);
742  if (image[i].kern_buf == NULL) {
743  printk(KERN_WARNING "Unable to allocate memory for "
744  "buffer\n");
745  image[i].pci_buf = 0;
747  err = -ENOMEM;
748  goto err_slave;
749  }
750  }
751 
752  /*
753  * Request master resources allocate page sized buffers for small
754  * reads and writes
755  */
756  for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
757  /* XXX Need to properly request attributes */
758  image[i].resource = vme_master_request(vme_user_bridge,
760  if (image[i].resource == NULL) {
761  printk(KERN_WARNING "Unable to allocate master "
762  "resource\n");
763  goto err_master;
764  }
765  image[i].size_buf = PCI_BUF_SIZE;
766  image[i].kern_buf = kmalloc(image[i].size_buf, GFP_KERNEL);
767  if (image[i].kern_buf == NULL) {
768  printk(KERN_WARNING "Unable to allocate memory for "
769  "master window buffers\n");
770  err = -ENOMEM;
771  goto err_master_buf;
772  }
773  }
774 
775  /* Create sysfs entries - on udev systems this creates the dev files */
776  vme_user_sysfs_class = class_create(THIS_MODULE, driver_name);
777  if (IS_ERR(vme_user_sysfs_class)) {
778  printk(KERN_ERR "Error creating vme_user class.\n");
779  err = PTR_ERR(vme_user_sysfs_class);
780  goto err_class;
781  }
782 
783  /* Add sysfs Entries */
784  for (i = 0; i < VME_DEVS; i++) {
785  int num;
786  switch (type[i]) {
787  case MASTER_MINOR:
788  sprintf(name, "bus/vme/m%%d");
789  break;
790  case CONTROL_MINOR:
791  sprintf(name, "bus/vme/ctl");
792  break;
793  case SLAVE_MINOR:
794  sprintf(name, "bus/vme/s%%d");
795  break;
796  default:
797  err = -EINVAL;
798  goto err_sysfs;
799  break;
800  }
801 
802  num = (type[i] == SLAVE_MINOR) ? i - (MASTER_MAX + 1) : i;
803  image[i].device = device_create(vme_user_sysfs_class, NULL,
804  MKDEV(VME_MAJOR, i), NULL, name, num);
805  if (IS_ERR(image[i].device)) {
806  printk(KERN_INFO "%s: Error creating sysfs device\n",
807  driver_name);
808  err = PTR_ERR(image[i].device);
809  goto err_sysfs;
810  }
811  }
812 
813  return 0;
814 
815  /* Ensure counter set correcty to destroy all sysfs devices */
816  i = VME_DEVS;
817 err_sysfs:
818  while (i > 0) {
819  i--;
820  device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
821  }
822  class_destroy(vme_user_sysfs_class);
823 
824  /* Ensure counter set correcty to unalloc all master windows */
825  i = MASTER_MAX + 1;
826 err_master_buf:
827  for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++)
828  kfree(image[i].kern_buf);
829 err_master:
830  while (i > MASTER_MINOR) {
831  i--;
833  }
834 
835  /*
836  * Ensure counter set correcty to unalloc all slave windows and buffers
837  */
838  i = SLAVE_MAX + 1;
839 err_slave:
840  while (i > SLAVE_MINOR) {
841  i--;
842  buf_unalloc(i);
844  }
845 err_class:
846  cdev_del(vme_user_cdev);
847 err_char:
849 err_region:
850 err_dev:
851  return err;
852 }
853 
854 static int __devexit vme_user_remove(struct vme_dev *dev)
855 {
856  int i;
857 
858  /* Remove sysfs Entries */
859  for (i = 0; i < VME_DEVS; i++) {
861  device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
862  }
863  class_destroy(vme_user_sysfs_class);
864 
865  for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
866  kfree(image[i].kern_buf);
868  }
869 
870  for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
871  vme_slave_set(image[i].resource, 0, 0, 0, 0, VME_A32, 0);
872  buf_unalloc(i);
874  }
875 
876  /* Unregister device driver */
877  cdev_del(vme_user_cdev);
878 
879  /* Unregiser the major and minor device numbers */
881 
882  return 0;
883 }
884 
885 static void __exit vme_user_exit(void)
886 {
887  vme_unregister_driver(&vme_user_driver);
888 }
889 
890 
891 MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the driver is connected");
892 module_param_array(bus, int, &bus_num, 0);
893 
894 MODULE_DESCRIPTION("VME User Space Access Driver");
895 MODULE_AUTHOR("Martyn Welch <[email protected]");
896 MODULE_LICENSE("GPL");
897 
898 module_init(vme_user_init);
899 module_exit(vme_user_exit);