Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / staging / vme / vme.c
blob70722ae523219d5bbf425fdaf1967a9289e59d04
1 /*
2 * VME Bridge Framework
4 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
7 * Based on work by Tom Armistead and Ajit Prem
8 * Copyright 2004 Motorola Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/mm.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/pci.h>
23 #include <linux/poll.h>
24 #include <linux/highmem.h>
25 #include <linux/interrupt.h>
26 #include <linux/pagemap.h>
27 #include <linux/device.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/syscalls.h>
30 #include <linux/mutex.h>
31 #include <linux/spinlock.h>
32 #include <linux/slab.h>
34 #include "vme.h"
35 #include "vme_bridge.h"
37 /* Bitmask and list of registered buses both protected by common mutex */
38 static unsigned int vme_bus_numbers;
39 static LIST_HEAD(vme_bus_list);
40 static DEFINE_MUTEX(vme_buses_lock);
42 static void __exit vme_exit(void);
43 static int __init vme_init(void);
45 static struct vme_dev *dev_to_vme_dev(struct device *dev)
47 return container_of(dev, struct vme_dev, dev);
51 * Find the bridge that the resource is associated with.
53 static struct vme_bridge *find_bridge(struct vme_resource *resource)
55 /* Get list to search */
56 switch (resource->type) {
57 case VME_MASTER:
58 return list_entry(resource->entry, struct vme_master_resource,
59 list)->parent;
60 break;
61 case VME_SLAVE:
62 return list_entry(resource->entry, struct vme_slave_resource,
63 list)->parent;
64 break;
65 case VME_DMA:
66 return list_entry(resource->entry, struct vme_dma_resource,
67 list)->parent;
68 break;
69 case VME_LM:
70 return list_entry(resource->entry, struct vme_lm_resource,
71 list)->parent;
72 break;
73 default:
74 printk(KERN_ERR "Unknown resource type\n");
75 return NULL;
76 break;
81 * Allocate a contiguous block of memory for use by the driver. This is used to
82 * create the buffers for the slave windows.
84 void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
85 dma_addr_t *dma)
87 struct vme_bridge *bridge;
89 if (resource == NULL) {
90 printk(KERN_ERR "No resource\n");
91 return NULL;
94 bridge = find_bridge(resource);
95 if (bridge == NULL) {
96 printk(KERN_ERR "Can't find bridge\n");
97 return NULL;
100 if (bridge->parent == NULL) {
101 printk(KERN_ERR "Dev entry NULL for"
102 " bridge %s\n", bridge->name);
103 return NULL;
106 if (bridge->alloc_consistent == NULL) {
107 printk(KERN_ERR "alloc_consistent not supported by"
108 " bridge %s\n", bridge->name);
109 return NULL;
112 return bridge->alloc_consistent(bridge->parent, size, dma);
114 EXPORT_SYMBOL(vme_alloc_consistent);
117 * Free previously allocated contiguous block of memory.
119 void vme_free_consistent(struct vme_resource *resource, size_t size,
120 void *vaddr, dma_addr_t dma)
122 struct vme_bridge *bridge;
124 if (resource == NULL) {
125 printk(KERN_ERR "No resource\n");
126 return;
129 bridge = find_bridge(resource);
130 if (bridge == NULL) {
131 printk(KERN_ERR "Can't find bridge\n");
132 return;
135 if (bridge->parent == NULL) {
136 printk(KERN_ERR "Dev entry NULL for"
137 " bridge %s\n", bridge->name);
138 return;
141 if (bridge->free_consistent == NULL) {
142 printk(KERN_ERR "free_consistent not supported by"
143 " bridge %s\n", bridge->name);
144 return;
147 bridge->free_consistent(bridge->parent, size, vaddr, dma);
149 EXPORT_SYMBOL(vme_free_consistent);
151 size_t vme_get_size(struct vme_resource *resource)
153 int enabled, retval;
154 unsigned long long base, size;
155 dma_addr_t buf_base;
156 u32 aspace, cycle, dwidth;
158 switch (resource->type) {
159 case VME_MASTER:
160 retval = vme_master_get(resource, &enabled, &base, &size,
161 &aspace, &cycle, &dwidth);
163 return size;
164 break;
165 case VME_SLAVE:
166 retval = vme_slave_get(resource, &enabled, &base, &size,
167 &buf_base, &aspace, &cycle);
169 return size;
170 break;
171 case VME_DMA:
172 return 0;
173 break;
174 default:
175 printk(KERN_ERR "Unknown resource type\n");
176 return 0;
177 break;
180 EXPORT_SYMBOL(vme_get_size);
182 static int vme_check_window(u32 aspace, unsigned long long vme_base,
183 unsigned long long size)
185 int retval = 0;
187 switch (aspace) {
188 case VME_A16:
189 if (((vme_base + size) > VME_A16_MAX) ||
190 (vme_base > VME_A16_MAX))
191 retval = -EFAULT;
192 break;
193 case VME_A24:
194 if (((vme_base + size) > VME_A24_MAX) ||
195 (vme_base > VME_A24_MAX))
196 retval = -EFAULT;
197 break;
198 case VME_A32:
199 if (((vme_base + size) > VME_A32_MAX) ||
200 (vme_base > VME_A32_MAX))
201 retval = -EFAULT;
202 break;
203 case VME_A64:
205 * Any value held in an unsigned long long can be used as the
206 * base
208 break;
209 case VME_CRCSR:
210 if (((vme_base + size) > VME_CRCSR_MAX) ||
211 (vme_base > VME_CRCSR_MAX))
212 retval = -EFAULT;
213 break;
214 case VME_USER1:
215 case VME_USER2:
216 case VME_USER3:
217 case VME_USER4:
218 /* User Defined */
219 break;
220 default:
221 printk(KERN_ERR "Invalid address space\n");
222 retval = -EINVAL;
223 break;
226 return retval;
230 * Request a slave image with specific attributes, return some unique
231 * identifier.
233 struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
234 u32 cycle)
236 struct vme_bridge *bridge;
237 struct list_head *slave_pos = NULL;
238 struct vme_slave_resource *allocated_image = NULL;
239 struct vme_slave_resource *slave_image = NULL;
240 struct vme_resource *resource = NULL;
242 bridge = vdev->bridge;
243 if (bridge == NULL) {
244 printk(KERN_ERR "Can't find VME bus\n");
245 goto err_bus;
248 /* Loop through slave resources */
249 list_for_each(slave_pos, &bridge->slave_resources) {
250 slave_image = list_entry(slave_pos,
251 struct vme_slave_resource, list);
253 if (slave_image == NULL) {
254 printk(KERN_ERR "Registered NULL Slave resource\n");
255 continue;
258 /* Find an unlocked and compatible image */
259 mutex_lock(&slave_image->mtx);
260 if (((slave_image->address_attr & address) == address) &&
261 ((slave_image->cycle_attr & cycle) == cycle) &&
262 (slave_image->locked == 0)) {
264 slave_image->locked = 1;
265 mutex_unlock(&slave_image->mtx);
266 allocated_image = slave_image;
267 break;
269 mutex_unlock(&slave_image->mtx);
272 /* No free image */
273 if (allocated_image == NULL)
274 goto err_image;
276 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
277 if (resource == NULL) {
278 printk(KERN_WARNING "Unable to allocate resource structure\n");
279 goto err_alloc;
281 resource->type = VME_SLAVE;
282 resource->entry = &allocated_image->list;
284 return resource;
286 err_alloc:
287 /* Unlock image */
288 mutex_lock(&slave_image->mtx);
289 slave_image->locked = 0;
290 mutex_unlock(&slave_image->mtx);
291 err_image:
292 err_bus:
293 return NULL;
295 EXPORT_SYMBOL(vme_slave_request);
297 int vme_slave_set(struct vme_resource *resource, int enabled,
298 unsigned long long vme_base, unsigned long long size,
299 dma_addr_t buf_base, u32 aspace, u32 cycle)
301 struct vme_bridge *bridge = find_bridge(resource);
302 struct vme_slave_resource *image;
303 int retval;
305 if (resource->type != VME_SLAVE) {
306 printk(KERN_ERR "Not a slave resource\n");
307 return -EINVAL;
310 image = list_entry(resource->entry, struct vme_slave_resource, list);
312 if (bridge->slave_set == NULL) {
313 printk(KERN_ERR "Function not supported\n");
314 return -ENOSYS;
317 if (!(((image->address_attr & aspace) == aspace) &&
318 ((image->cycle_attr & cycle) == cycle))) {
319 printk(KERN_ERR "Invalid attributes\n");
320 return -EINVAL;
323 retval = vme_check_window(aspace, vme_base, size);
324 if (retval)
325 return retval;
327 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
328 aspace, cycle);
330 EXPORT_SYMBOL(vme_slave_set);
332 int vme_slave_get(struct vme_resource *resource, int *enabled,
333 unsigned long long *vme_base, unsigned long long *size,
334 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
336 struct vme_bridge *bridge = find_bridge(resource);
337 struct vme_slave_resource *image;
339 if (resource->type != VME_SLAVE) {
340 printk(KERN_ERR "Not a slave resource\n");
341 return -EINVAL;
344 image = list_entry(resource->entry, struct vme_slave_resource, list);
346 if (bridge->slave_get == NULL) {
347 printk(KERN_ERR "vme_slave_get not supported\n");
348 return -EINVAL;
351 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
352 aspace, cycle);
354 EXPORT_SYMBOL(vme_slave_get);
356 void vme_slave_free(struct vme_resource *resource)
358 struct vme_slave_resource *slave_image;
360 if (resource->type != VME_SLAVE) {
361 printk(KERN_ERR "Not a slave resource\n");
362 return;
365 slave_image = list_entry(resource->entry, struct vme_slave_resource,
366 list);
367 if (slave_image == NULL) {
368 printk(KERN_ERR "Can't find slave resource\n");
369 return;
372 /* Unlock image */
373 mutex_lock(&slave_image->mtx);
374 if (slave_image->locked == 0)
375 printk(KERN_ERR "Image is already free\n");
377 slave_image->locked = 0;
378 mutex_unlock(&slave_image->mtx);
380 /* Free up resource memory */
381 kfree(resource);
383 EXPORT_SYMBOL(vme_slave_free);
386 * Request a master image with specific attributes, return some unique
387 * identifier.
389 struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
390 u32 cycle, u32 dwidth)
392 struct vme_bridge *bridge;
393 struct list_head *master_pos = NULL;
394 struct vme_master_resource *allocated_image = NULL;
395 struct vme_master_resource *master_image = NULL;
396 struct vme_resource *resource = NULL;
398 bridge = vdev->bridge;
399 if (bridge == NULL) {
400 printk(KERN_ERR "Can't find VME bus\n");
401 goto err_bus;
404 /* Loop through master resources */
405 list_for_each(master_pos, &bridge->master_resources) {
406 master_image = list_entry(master_pos,
407 struct vme_master_resource, list);
409 if (master_image == NULL) {
410 printk(KERN_WARNING "Registered NULL master resource\n");
411 continue;
414 /* Find an unlocked and compatible image */
415 spin_lock(&master_image->lock);
416 if (((master_image->address_attr & address) == address) &&
417 ((master_image->cycle_attr & cycle) == cycle) &&
418 ((master_image->width_attr & dwidth) == dwidth) &&
419 (master_image->locked == 0)) {
421 master_image->locked = 1;
422 spin_unlock(&master_image->lock);
423 allocated_image = master_image;
424 break;
426 spin_unlock(&master_image->lock);
429 /* Check to see if we found a resource */
430 if (allocated_image == NULL) {
431 printk(KERN_ERR "Can't find a suitable resource\n");
432 goto err_image;
435 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
436 if (resource == NULL) {
437 printk(KERN_ERR "Unable to allocate resource structure\n");
438 goto err_alloc;
440 resource->type = VME_MASTER;
441 resource->entry = &allocated_image->list;
443 return resource;
445 err_alloc:
446 /* Unlock image */
447 spin_lock(&master_image->lock);
448 master_image->locked = 0;
449 spin_unlock(&master_image->lock);
450 err_image:
451 err_bus:
452 return NULL;
454 EXPORT_SYMBOL(vme_master_request);
456 int vme_master_set(struct vme_resource *resource, int enabled,
457 unsigned long long vme_base, unsigned long long size, u32 aspace,
458 u32 cycle, u32 dwidth)
460 struct vme_bridge *bridge = find_bridge(resource);
461 struct vme_master_resource *image;
462 int retval;
464 if (resource->type != VME_MASTER) {
465 printk(KERN_ERR "Not a master resource\n");
466 return -EINVAL;
469 image = list_entry(resource->entry, struct vme_master_resource, list);
471 if (bridge->master_set == NULL) {
472 printk(KERN_WARNING "vme_master_set not supported\n");
473 return -EINVAL;
476 if (!(((image->address_attr & aspace) == aspace) &&
477 ((image->cycle_attr & cycle) == cycle) &&
478 ((image->width_attr & dwidth) == dwidth))) {
479 printk(KERN_WARNING "Invalid attributes\n");
480 return -EINVAL;
483 retval = vme_check_window(aspace, vme_base, size);
484 if (retval)
485 return retval;
487 return bridge->master_set(image, enabled, vme_base, size, aspace,
488 cycle, dwidth);
490 EXPORT_SYMBOL(vme_master_set);
492 int vme_master_get(struct vme_resource *resource, int *enabled,
493 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
494 u32 *cycle, u32 *dwidth)
496 struct vme_bridge *bridge = find_bridge(resource);
497 struct vme_master_resource *image;
499 if (resource->type != VME_MASTER) {
500 printk(KERN_ERR "Not a master resource\n");
501 return -EINVAL;
504 image = list_entry(resource->entry, struct vme_master_resource, list);
506 if (bridge->master_get == NULL) {
507 printk(KERN_WARNING "vme_master_set not supported\n");
508 return -EINVAL;
511 return bridge->master_get(image, enabled, vme_base, size, aspace,
512 cycle, dwidth);
514 EXPORT_SYMBOL(vme_master_get);
517 * Read data out of VME space into a buffer.
519 ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
520 loff_t offset)
522 struct vme_bridge *bridge = find_bridge(resource);
523 struct vme_master_resource *image;
524 size_t length;
526 if (bridge->master_read == NULL) {
527 printk(KERN_WARNING "Reading from resource not supported\n");
528 return -EINVAL;
531 if (resource->type != VME_MASTER) {
532 printk(KERN_ERR "Not a master resource\n");
533 return -EINVAL;
536 image = list_entry(resource->entry, struct vme_master_resource, list);
538 length = vme_get_size(resource);
540 if (offset > length) {
541 printk(KERN_WARNING "Invalid Offset\n");
542 return -EFAULT;
545 if ((offset + count) > length)
546 count = length - offset;
548 return bridge->master_read(image, buf, count, offset);
551 EXPORT_SYMBOL(vme_master_read);
554 * Write data out to VME space from a buffer.
556 ssize_t vme_master_write(struct vme_resource *resource, void *buf,
557 size_t count, loff_t offset)
559 struct vme_bridge *bridge = find_bridge(resource);
560 struct vme_master_resource *image;
561 size_t length;
563 if (bridge->master_write == NULL) {
564 printk(KERN_WARNING "Writing to resource not supported\n");
565 return -EINVAL;
568 if (resource->type != VME_MASTER) {
569 printk(KERN_ERR "Not a master resource\n");
570 return -EINVAL;
573 image = list_entry(resource->entry, struct vme_master_resource, list);
575 length = vme_get_size(resource);
577 if (offset > length) {
578 printk(KERN_WARNING "Invalid Offset\n");
579 return -EFAULT;
582 if ((offset + count) > length)
583 count = length - offset;
585 return bridge->master_write(image, buf, count, offset);
587 EXPORT_SYMBOL(vme_master_write);
590 * Perform RMW cycle to provided location.
592 unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
593 unsigned int compare, unsigned int swap, loff_t offset)
595 struct vme_bridge *bridge = find_bridge(resource);
596 struct vme_master_resource *image;
598 if (bridge->master_rmw == NULL) {
599 printk(KERN_WARNING "Writing to resource not supported\n");
600 return -EINVAL;
603 if (resource->type != VME_MASTER) {
604 printk(KERN_ERR "Not a master resource\n");
605 return -EINVAL;
608 image = list_entry(resource->entry, struct vme_master_resource, list);
610 return bridge->master_rmw(image, mask, compare, swap, offset);
612 EXPORT_SYMBOL(vme_master_rmw);
614 void vme_master_free(struct vme_resource *resource)
616 struct vme_master_resource *master_image;
618 if (resource->type != VME_MASTER) {
619 printk(KERN_ERR "Not a master resource\n");
620 return;
623 master_image = list_entry(resource->entry, struct vme_master_resource,
624 list);
625 if (master_image == NULL) {
626 printk(KERN_ERR "Can't find master resource\n");
627 return;
630 /* Unlock image */
631 spin_lock(&master_image->lock);
632 if (master_image->locked == 0)
633 printk(KERN_ERR "Image is already free\n");
635 master_image->locked = 0;
636 spin_unlock(&master_image->lock);
638 /* Free up resource memory */
639 kfree(resource);
641 EXPORT_SYMBOL(vme_master_free);
644 * Request a DMA controller with specific attributes, return some unique
645 * identifier.
647 struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
649 struct vme_bridge *bridge;
650 struct list_head *dma_pos = NULL;
651 struct vme_dma_resource *allocated_ctrlr = NULL;
652 struct vme_dma_resource *dma_ctrlr = NULL;
653 struct vme_resource *resource = NULL;
655 /* XXX Not checking resource attributes */
656 printk(KERN_ERR "No VME resource Attribute tests done\n");
658 bridge = vdev->bridge;
659 if (bridge == NULL) {
660 printk(KERN_ERR "Can't find VME bus\n");
661 goto err_bus;
664 /* Loop through DMA resources */
665 list_for_each(dma_pos, &bridge->dma_resources) {
666 dma_ctrlr = list_entry(dma_pos,
667 struct vme_dma_resource, list);
669 if (dma_ctrlr == NULL) {
670 printk(KERN_ERR "Registered NULL DMA resource\n");
671 continue;
674 /* Find an unlocked and compatible controller */
675 mutex_lock(&dma_ctrlr->mtx);
676 if (((dma_ctrlr->route_attr & route) == route) &&
677 (dma_ctrlr->locked == 0)) {
679 dma_ctrlr->locked = 1;
680 mutex_unlock(&dma_ctrlr->mtx);
681 allocated_ctrlr = dma_ctrlr;
682 break;
684 mutex_unlock(&dma_ctrlr->mtx);
687 /* Check to see if we found a resource */
688 if (allocated_ctrlr == NULL)
689 goto err_ctrlr;
691 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
692 if (resource == NULL) {
693 printk(KERN_WARNING "Unable to allocate resource structure\n");
694 goto err_alloc;
696 resource->type = VME_DMA;
697 resource->entry = &allocated_ctrlr->list;
699 return resource;
701 err_alloc:
702 /* Unlock image */
703 mutex_lock(&dma_ctrlr->mtx);
704 dma_ctrlr->locked = 0;
705 mutex_unlock(&dma_ctrlr->mtx);
706 err_ctrlr:
707 err_bus:
708 return NULL;
710 EXPORT_SYMBOL(vme_dma_request);
713 * Start new list
715 struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
717 struct vme_dma_resource *ctrlr;
718 struct vme_dma_list *dma_list;
720 if (resource->type != VME_DMA) {
721 printk(KERN_ERR "Not a DMA resource\n");
722 return NULL;
725 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
727 dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
728 if (dma_list == NULL) {
729 printk(KERN_ERR "Unable to allocate memory for new dma list\n");
730 return NULL;
732 INIT_LIST_HEAD(&dma_list->entries);
733 dma_list->parent = ctrlr;
734 mutex_init(&dma_list->mtx);
736 return dma_list;
738 EXPORT_SYMBOL(vme_new_dma_list);
741 * Create "Pattern" type attributes
743 struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
745 struct vme_dma_attr *attributes;
746 struct vme_dma_pattern *pattern_attr;
748 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
749 if (attributes == NULL) {
750 printk(KERN_ERR "Unable to allocate memory for attributes "
751 "structure\n");
752 goto err_attr;
755 pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
756 if (pattern_attr == NULL) {
757 printk(KERN_ERR "Unable to allocate memory for pattern "
758 "attributes\n");
759 goto err_pat;
762 attributes->type = VME_DMA_PATTERN;
763 attributes->private = (void *)pattern_attr;
765 pattern_attr->pattern = pattern;
766 pattern_attr->type = type;
768 return attributes;
770 err_pat:
771 kfree(attributes);
772 err_attr:
773 return NULL;
775 EXPORT_SYMBOL(vme_dma_pattern_attribute);
778 * Create "PCI" type attributes
780 struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
782 struct vme_dma_attr *attributes;
783 struct vme_dma_pci *pci_attr;
785 /* XXX Run some sanity checks here */
787 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
788 if (attributes == NULL) {
789 printk(KERN_ERR "Unable to allocate memory for attributes "
790 "structure\n");
791 goto err_attr;
794 pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
795 if (pci_attr == NULL) {
796 printk(KERN_ERR "Unable to allocate memory for pci "
797 "attributes\n");
798 goto err_pci;
803 attributes->type = VME_DMA_PCI;
804 attributes->private = (void *)pci_attr;
806 pci_attr->address = address;
808 return attributes;
810 err_pci:
811 kfree(attributes);
812 err_attr:
813 return NULL;
815 EXPORT_SYMBOL(vme_dma_pci_attribute);
818 * Create "VME" type attributes
820 struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
821 u32 aspace, u32 cycle, u32 dwidth)
823 struct vme_dma_attr *attributes;
824 struct vme_dma_vme *vme_attr;
826 attributes = kmalloc(
827 sizeof(struct vme_dma_attr), GFP_KERNEL);
828 if (attributes == NULL) {
829 printk(KERN_ERR "Unable to allocate memory for attributes "
830 "structure\n");
831 goto err_attr;
834 vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
835 if (vme_attr == NULL) {
836 printk(KERN_ERR "Unable to allocate memory for vme "
837 "attributes\n");
838 goto err_vme;
841 attributes->type = VME_DMA_VME;
842 attributes->private = (void *)vme_attr;
844 vme_attr->address = address;
845 vme_attr->aspace = aspace;
846 vme_attr->cycle = cycle;
847 vme_attr->dwidth = dwidth;
849 return attributes;
851 err_vme:
852 kfree(attributes);
853 err_attr:
854 return NULL;
856 EXPORT_SYMBOL(vme_dma_vme_attribute);
859 * Free attribute
861 void vme_dma_free_attribute(struct vme_dma_attr *attributes)
863 kfree(attributes->private);
864 kfree(attributes);
866 EXPORT_SYMBOL(vme_dma_free_attribute);
868 int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
869 struct vme_dma_attr *dest, size_t count)
871 struct vme_bridge *bridge = list->parent->parent;
872 int retval;
874 if (bridge->dma_list_add == NULL) {
875 printk(KERN_WARNING "Link List DMA generation not supported\n");
876 return -EINVAL;
879 if (!mutex_trylock(&list->mtx)) {
880 printk(KERN_ERR "Link List already submitted\n");
881 return -EINVAL;
884 retval = bridge->dma_list_add(list, src, dest, count);
886 mutex_unlock(&list->mtx);
888 return retval;
890 EXPORT_SYMBOL(vme_dma_list_add);
892 int vme_dma_list_exec(struct vme_dma_list *list)
894 struct vme_bridge *bridge = list->parent->parent;
895 int retval;
897 if (bridge->dma_list_exec == NULL) {
898 printk(KERN_ERR "Link List DMA execution not supported\n");
899 return -EINVAL;
902 mutex_lock(&list->mtx);
904 retval = bridge->dma_list_exec(list);
906 mutex_unlock(&list->mtx);
908 return retval;
910 EXPORT_SYMBOL(vme_dma_list_exec);
912 int vme_dma_list_free(struct vme_dma_list *list)
914 struct vme_bridge *bridge = list->parent->parent;
915 int retval;
917 if (bridge->dma_list_empty == NULL) {
918 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
919 return -EINVAL;
922 if (!mutex_trylock(&list->mtx)) {
923 printk(KERN_ERR "Link List in use\n");
924 return -EINVAL;
928 * Empty out all of the entries from the dma list. We need to go to the
929 * low level driver as dma entries are driver specific.
931 retval = bridge->dma_list_empty(list);
932 if (retval) {
933 printk(KERN_ERR "Unable to empty link-list entries\n");
934 mutex_unlock(&list->mtx);
935 return retval;
937 mutex_unlock(&list->mtx);
938 kfree(list);
940 return retval;
942 EXPORT_SYMBOL(vme_dma_list_free);
944 int vme_dma_free(struct vme_resource *resource)
946 struct vme_dma_resource *ctrlr;
948 if (resource->type != VME_DMA) {
949 printk(KERN_ERR "Not a DMA resource\n");
950 return -EINVAL;
953 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
955 if (!mutex_trylock(&ctrlr->mtx)) {
956 printk(KERN_ERR "Resource busy, can't free\n");
957 return -EBUSY;
960 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
961 printk(KERN_WARNING "Resource still processing transfers\n");
962 mutex_unlock(&ctrlr->mtx);
963 return -EBUSY;
966 ctrlr->locked = 0;
968 mutex_unlock(&ctrlr->mtx);
970 return 0;
972 EXPORT_SYMBOL(vme_dma_free);
974 void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
976 void (*call)(int, int, void *);
977 void *priv_data;
979 call = bridge->irq[level - 1].callback[statid].func;
980 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
982 if (call != NULL)
983 call(level, statid, priv_data);
984 else
985 printk(KERN_WARNING "Spurilous VME interrupt, level:%x, "
986 "vector:%x\n", level, statid);
988 EXPORT_SYMBOL(vme_irq_handler);
990 int vme_irq_request(struct vme_dev *vdev, int level, int statid,
991 void (*callback)(int, int, void *),
992 void *priv_data)
994 struct vme_bridge *bridge;
996 bridge = vdev->bridge;
997 if (bridge == NULL) {
998 printk(KERN_ERR "Can't find VME bus\n");
999 return -EINVAL;
1002 if ((level < 1) || (level > 7)) {
1003 printk(KERN_ERR "Invalid interrupt level\n");
1004 return -EINVAL;
1007 if (bridge->irq_set == NULL) {
1008 printk(KERN_ERR "Configuring interrupts not supported\n");
1009 return -EINVAL;
1012 mutex_lock(&bridge->irq_mtx);
1014 if (bridge->irq[level - 1].callback[statid].func) {
1015 mutex_unlock(&bridge->irq_mtx);
1016 printk(KERN_WARNING "VME Interrupt already taken\n");
1017 return -EBUSY;
1020 bridge->irq[level - 1].count++;
1021 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1022 bridge->irq[level - 1].callback[statid].func = callback;
1024 /* Enable IRQ level */
1025 bridge->irq_set(bridge, level, 1, 1);
1027 mutex_unlock(&bridge->irq_mtx);
1029 return 0;
1031 EXPORT_SYMBOL(vme_irq_request);
1033 void vme_irq_free(struct vme_dev *vdev, int level, int statid)
1035 struct vme_bridge *bridge;
1037 bridge = vdev->bridge;
1038 if (bridge == NULL) {
1039 printk(KERN_ERR "Can't find VME bus\n");
1040 return;
1043 if ((level < 1) || (level > 7)) {
1044 printk(KERN_ERR "Invalid interrupt level\n");
1045 return;
1048 if (bridge->irq_set == NULL) {
1049 printk(KERN_ERR "Configuring interrupts not supported\n");
1050 return;
1053 mutex_lock(&bridge->irq_mtx);
1055 bridge->irq[level - 1].count--;
1057 /* Disable IRQ level if no more interrupts attached at this level*/
1058 if (bridge->irq[level - 1].count == 0)
1059 bridge->irq_set(bridge, level, 0, 1);
1061 bridge->irq[level - 1].callback[statid].func = NULL;
1062 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1064 mutex_unlock(&bridge->irq_mtx);
1066 EXPORT_SYMBOL(vme_irq_free);
1068 int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
1070 struct vme_bridge *bridge;
1072 bridge = vdev->bridge;
1073 if (bridge == NULL) {
1074 printk(KERN_ERR "Can't find VME bus\n");
1075 return -EINVAL;
1078 if ((level < 1) || (level > 7)) {
1079 printk(KERN_WARNING "Invalid interrupt level\n");
1080 return -EINVAL;
1083 if (bridge->irq_generate == NULL) {
1084 printk(KERN_WARNING "Interrupt generation not supported\n");
1085 return -EINVAL;
1088 return bridge->irq_generate(bridge, level, statid);
1090 EXPORT_SYMBOL(vme_irq_generate);
1093 * Request the location monitor, return resource or NULL
1095 struct vme_resource *vme_lm_request(struct vme_dev *vdev)
1097 struct vme_bridge *bridge;
1098 struct list_head *lm_pos = NULL;
1099 struct vme_lm_resource *allocated_lm = NULL;
1100 struct vme_lm_resource *lm = NULL;
1101 struct vme_resource *resource = NULL;
1103 bridge = vdev->bridge;
1104 if (bridge == NULL) {
1105 printk(KERN_ERR "Can't find VME bus\n");
1106 goto err_bus;
1109 /* Loop through DMA resources */
1110 list_for_each(lm_pos, &bridge->lm_resources) {
1111 lm = list_entry(lm_pos,
1112 struct vme_lm_resource, list);
1114 if (lm == NULL) {
1115 printk(KERN_ERR "Registered NULL Location Monitor "
1116 "resource\n");
1117 continue;
1120 /* Find an unlocked controller */
1121 mutex_lock(&lm->mtx);
1122 if (lm->locked == 0) {
1123 lm->locked = 1;
1124 mutex_unlock(&lm->mtx);
1125 allocated_lm = lm;
1126 break;
1128 mutex_unlock(&lm->mtx);
1131 /* Check to see if we found a resource */
1132 if (allocated_lm == NULL)
1133 goto err_lm;
1135 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
1136 if (resource == NULL) {
1137 printk(KERN_ERR "Unable to allocate resource structure\n");
1138 goto err_alloc;
1140 resource->type = VME_LM;
1141 resource->entry = &allocated_lm->list;
1143 return resource;
1145 err_alloc:
1146 /* Unlock image */
1147 mutex_lock(&lm->mtx);
1148 lm->locked = 0;
1149 mutex_unlock(&lm->mtx);
1150 err_lm:
1151 err_bus:
1152 return NULL;
1154 EXPORT_SYMBOL(vme_lm_request);
1156 int vme_lm_count(struct vme_resource *resource)
1158 struct vme_lm_resource *lm;
1160 if (resource->type != VME_LM) {
1161 printk(KERN_ERR "Not a Location Monitor resource\n");
1162 return -EINVAL;
1165 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1167 return lm->monitors;
1169 EXPORT_SYMBOL(vme_lm_count);
1171 int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
1172 u32 aspace, u32 cycle)
1174 struct vme_bridge *bridge = find_bridge(resource);
1175 struct vme_lm_resource *lm;
1177 if (resource->type != VME_LM) {
1178 printk(KERN_ERR "Not a Location Monitor resource\n");
1179 return -EINVAL;
1182 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1184 if (bridge->lm_set == NULL) {
1185 printk(KERN_ERR "vme_lm_set not supported\n");
1186 return -EINVAL;
1189 return bridge->lm_set(lm, lm_base, aspace, cycle);
1191 EXPORT_SYMBOL(vme_lm_set);
1193 int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
1194 u32 *aspace, u32 *cycle)
1196 struct vme_bridge *bridge = find_bridge(resource);
1197 struct vme_lm_resource *lm;
1199 if (resource->type != VME_LM) {
1200 printk(KERN_ERR "Not a Location Monitor resource\n");
1201 return -EINVAL;
1204 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1206 if (bridge->lm_get == NULL) {
1207 printk(KERN_ERR "vme_lm_get not supported\n");
1208 return -EINVAL;
1211 return bridge->lm_get(lm, lm_base, aspace, cycle);
1213 EXPORT_SYMBOL(vme_lm_get);
1215 int vme_lm_attach(struct vme_resource *resource, int monitor,
1216 void (*callback)(int))
1218 struct vme_bridge *bridge = find_bridge(resource);
1219 struct vme_lm_resource *lm;
1221 if (resource->type != VME_LM) {
1222 printk(KERN_ERR "Not a Location Monitor resource\n");
1223 return -EINVAL;
1226 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1228 if (bridge->lm_attach == NULL) {
1229 printk(KERN_ERR "vme_lm_attach not supported\n");
1230 return -EINVAL;
1233 return bridge->lm_attach(lm, monitor, callback);
1235 EXPORT_SYMBOL(vme_lm_attach);
1237 int vme_lm_detach(struct vme_resource *resource, int monitor)
1239 struct vme_bridge *bridge = find_bridge(resource);
1240 struct vme_lm_resource *lm;
1242 if (resource->type != VME_LM) {
1243 printk(KERN_ERR "Not a Location Monitor resource\n");
1244 return -EINVAL;
1247 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1249 if (bridge->lm_detach == NULL) {
1250 printk(KERN_ERR "vme_lm_detach not supported\n");
1251 return -EINVAL;
1254 return bridge->lm_detach(lm, monitor);
1256 EXPORT_SYMBOL(vme_lm_detach);
1258 void vme_lm_free(struct vme_resource *resource)
1260 struct vme_lm_resource *lm;
1262 if (resource->type != VME_LM) {
1263 printk(KERN_ERR "Not a Location Monitor resource\n");
1264 return;
1267 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1269 mutex_lock(&lm->mtx);
1271 /* XXX
1272 * Check to see that there aren't any callbacks still attached, if
1273 * there are we should probably be detaching them!
1276 lm->locked = 0;
1278 mutex_unlock(&lm->mtx);
1280 kfree(resource);
1282 EXPORT_SYMBOL(vme_lm_free);
1284 int vme_slot_get(struct vme_dev *vdev)
1286 struct vme_bridge *bridge;
1288 bridge = vdev->bridge;
1289 if (bridge == NULL) {
1290 printk(KERN_ERR "Can't find VME bus\n");
1291 return -EINVAL;
1294 if (bridge->slot_get == NULL) {
1295 printk(KERN_WARNING "vme_slot_get not supported\n");
1296 return -EINVAL;
1299 return bridge->slot_get(bridge);
1301 EXPORT_SYMBOL(vme_slot_get);
1304 /* - Bridge Registration --------------------------------------------------- */
1306 static void vme_dev_release(struct device *dev)
1308 kfree(dev_to_vme_dev(dev));
1311 int vme_register_bridge(struct vme_bridge *bridge)
1313 int i;
1314 int ret = -1;
1316 mutex_lock(&vme_buses_lock);
1317 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
1318 if ((vme_bus_numbers & (1 << i)) == 0) {
1319 vme_bus_numbers |= (1 << i);
1320 bridge->num = i;
1321 INIT_LIST_HEAD(&bridge->devices);
1322 list_add_tail(&bridge->bus_list, &vme_bus_list);
1323 ret = 0;
1324 break;
1327 mutex_unlock(&vme_buses_lock);
1329 return ret;
1331 EXPORT_SYMBOL(vme_register_bridge);
1333 void vme_unregister_bridge(struct vme_bridge *bridge)
1335 struct vme_dev *vdev;
1336 struct vme_dev *tmp;
1338 mutex_lock(&vme_buses_lock);
1339 vme_bus_numbers &= ~(1 << bridge->num);
1340 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1341 list_del(&vdev->drv_list);
1342 list_del(&vdev->bridge_list);
1343 device_unregister(&vdev->dev);
1345 list_del(&bridge->bus_list);
1346 mutex_unlock(&vme_buses_lock);
1348 EXPORT_SYMBOL(vme_unregister_bridge);
1350 /* - Driver Registration --------------------------------------------------- */
1352 static int __vme_register_driver_bus(struct vme_driver *drv,
1353 struct vme_bridge *bridge, unsigned int ndevs)
1355 int err;
1356 unsigned int i;
1357 struct vme_dev *vdev;
1358 struct vme_dev *tmp;
1360 for (i = 0; i < ndevs; i++) {
1361 vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
1362 if (!vdev) {
1363 err = -ENOMEM;
1364 goto err_devalloc;
1366 vdev->num = i;
1367 vdev->bridge = bridge;
1368 vdev->dev.platform_data = drv;
1369 vdev->dev.release = vme_dev_release;
1370 vdev->dev.parent = bridge->parent;
1371 vdev->dev.bus = &vme_bus_type;
1372 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1373 vdev->num);
1375 err = device_register(&vdev->dev);
1376 if (err)
1377 goto err_reg;
1379 if (vdev->dev.platform_data) {
1380 list_add_tail(&vdev->drv_list, &drv->devices);
1381 list_add_tail(&vdev->bridge_list, &bridge->devices);
1382 } else
1383 device_unregister(&vdev->dev);
1385 return 0;
1387 err_reg:
1388 kfree(vdev);
1389 err_devalloc:
1390 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1391 list_del(&vdev->drv_list);
1392 list_del(&vdev->bridge_list);
1393 device_unregister(&vdev->dev);
1395 return err;
1398 static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1400 struct vme_bridge *bridge;
1401 int err = 0;
1403 mutex_lock(&vme_buses_lock);
1404 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1406 * This cannot cause trouble as we already have vme_buses_lock
1407 * and if the bridge is removed, it will have to go through
1408 * vme_unregister_bridge() to do it (which calls remove() on
1409 * the bridge which in turn tries to acquire vme_buses_lock and
1410 * will have to wait).
1412 err = __vme_register_driver_bus(drv, bridge, ndevs);
1413 if (err)
1414 break;
1416 mutex_unlock(&vme_buses_lock);
1417 return err;
1420 int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1422 int err;
1424 drv->driver.name = drv->name;
1425 drv->driver.bus = &vme_bus_type;
1426 INIT_LIST_HEAD(&drv->devices);
1428 err = driver_register(&drv->driver);
1429 if (err)
1430 return err;
1432 err = __vme_register_driver(drv, ndevs);
1433 if (err)
1434 driver_unregister(&drv->driver);
1436 return err;
1438 EXPORT_SYMBOL(vme_register_driver);
1440 void vme_unregister_driver(struct vme_driver *drv)
1442 struct vme_dev *dev, *dev_tmp;
1444 mutex_lock(&vme_buses_lock);
1445 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1446 list_del(&dev->drv_list);
1447 list_del(&dev->bridge_list);
1448 device_unregister(&dev->dev);
1450 mutex_unlock(&vme_buses_lock);
1452 driver_unregister(&drv->driver);
1454 EXPORT_SYMBOL(vme_unregister_driver);
1456 /* - Bus Registration ------------------------------------------------------ */
1458 static int vme_bus_match(struct device *dev, struct device_driver *drv)
1460 struct vme_driver *vme_drv;
1462 vme_drv = container_of(drv, struct vme_driver, driver);
1464 if (dev->platform_data == vme_drv) {
1465 struct vme_dev *vdev = dev_to_vme_dev(dev);
1467 if (vme_drv->match && vme_drv->match(vdev))
1468 return 1;
1470 dev->platform_data = NULL;
1472 return 0;
1475 static int vme_bus_probe(struct device *dev)
1477 int retval = -ENODEV;
1478 struct vme_driver *driver;
1479 struct vme_dev *vdev = dev_to_vme_dev(dev);
1481 driver = dev->platform_data;
1483 if (driver->probe != NULL)
1484 retval = driver->probe(vdev);
1486 return retval;
1489 static int vme_bus_remove(struct device *dev)
1491 int retval = -ENODEV;
1492 struct vme_driver *driver;
1493 struct vme_dev *vdev = dev_to_vme_dev(dev);
1495 driver = dev->platform_data;
1497 if (driver->remove != NULL)
1498 retval = driver->remove(vdev);
1500 return retval;
1503 struct bus_type vme_bus_type = {
1504 .name = "vme",
1505 .match = vme_bus_match,
1506 .probe = vme_bus_probe,
1507 .remove = vme_bus_remove,
1509 EXPORT_SYMBOL(vme_bus_type);
1511 static int __init vme_init(void)
1513 return bus_register(&vme_bus_type);
1516 static void __exit vme_exit(void)
1518 bus_unregister(&vme_bus_type);
1521 MODULE_DESCRIPTION("VME bridge driver framework");
1522 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
1523 MODULE_LICENSE("GPL");
1525 module_init(vme_init);
1526 module_exit(vme_exit);