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/init.h>
17 #include <linux/export.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>
33 #include <linux/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 int __init
vme_init(void);
44 static struct vme_dev
*dev_to_vme_dev(struct device
*dev
)
46 return container_of(dev
, struct vme_dev
, dev
);
50 * Find the bridge that the resource is associated with.
52 static struct vme_bridge
*find_bridge(struct vme_resource
*resource
)
54 /* Get list to search */
55 switch (resource
->type
) {
57 return list_entry(resource
->entry
, struct vme_master_resource
,
61 return list_entry(resource
->entry
, struct vme_slave_resource
,
65 return list_entry(resource
->entry
, struct vme_dma_resource
,
69 return list_entry(resource
->entry
, struct vme_lm_resource
,
73 printk(KERN_ERR
"Unknown resource type\n");
80 * vme_free_consistent - Allocate contiguous memory.
81 * @resource: Pointer to VME resource.
82 * @size: Size of allocation required.
83 * @dma: Pointer to variable to store physical address of allocation.
85 * Allocate a contiguous block of memory for use by the driver. This is used to
86 * create the buffers for the slave windows.
88 * Return: Virtual address of allocation on success, NULL on failure.
90 void *vme_alloc_consistent(struct vme_resource
*resource
, size_t size
,
93 struct vme_bridge
*bridge
;
95 if (resource
== NULL
) {
96 printk(KERN_ERR
"No resource\n");
100 bridge
= find_bridge(resource
);
101 if (bridge
== NULL
) {
102 printk(KERN_ERR
"Can't find bridge\n");
106 if (bridge
->parent
== NULL
) {
107 printk(KERN_ERR
"Dev entry NULL for bridge %s\n", bridge
->name
);
111 if (bridge
->alloc_consistent
== NULL
) {
112 printk(KERN_ERR
"alloc_consistent not supported by bridge %s\n",
117 return bridge
->alloc_consistent(bridge
->parent
, size
, dma
);
119 EXPORT_SYMBOL(vme_alloc_consistent
);
122 * vme_free_consistent - Free previously allocated memory.
123 * @resource: Pointer to VME resource.
124 * @size: Size of allocation to free.
125 * @vaddr: Virtual address of allocation.
126 * @dma: Physical address of allocation.
128 * Free previously allocated block of contiguous memory.
130 void vme_free_consistent(struct vme_resource
*resource
, size_t size
,
131 void *vaddr
, dma_addr_t dma
)
133 struct vme_bridge
*bridge
;
135 if (resource
== NULL
) {
136 printk(KERN_ERR
"No resource\n");
140 bridge
= find_bridge(resource
);
141 if (bridge
== NULL
) {
142 printk(KERN_ERR
"Can't find bridge\n");
146 if (bridge
->parent
== NULL
) {
147 printk(KERN_ERR
"Dev entry NULL for bridge %s\n", bridge
->name
);
151 if (bridge
->free_consistent
== NULL
) {
152 printk(KERN_ERR
"free_consistent not supported by bridge %s\n",
157 bridge
->free_consistent(bridge
->parent
, size
, vaddr
, dma
);
159 EXPORT_SYMBOL(vme_free_consistent
);
162 * vme_get_size - Helper function returning size of a VME window
163 * @resource: Pointer to VME slave or master resource.
165 * Determine the size of the VME window provided. This is a helper
166 * function, wrappering the call to vme_master_get or vme_slave_get
167 * depending on the type of window resource handed to it.
169 * Return: Size of the window on success, zero on failure.
171 size_t vme_get_size(struct vme_resource
*resource
)
174 unsigned long long base
, size
;
176 u32 aspace
, cycle
, dwidth
;
178 switch (resource
->type
) {
180 retval
= vme_master_get(resource
, &enabled
, &base
, &size
,
181 &aspace
, &cycle
, &dwidth
);
188 retval
= vme_slave_get(resource
, &enabled
, &base
, &size
,
189 &buf_base
, &aspace
, &cycle
);
199 printk(KERN_ERR
"Unknown resource type\n");
204 EXPORT_SYMBOL(vme_get_size
);
206 int vme_check_window(u32 aspace
, unsigned long long vme_base
,
207 unsigned long long size
)
213 if (((vme_base
+ size
) > VME_A16_MAX
) ||
214 (vme_base
> VME_A16_MAX
))
218 if (((vme_base
+ size
) > VME_A24_MAX
) ||
219 (vme_base
> VME_A24_MAX
))
223 if (((vme_base
+ size
) > VME_A32_MAX
) ||
224 (vme_base
> VME_A32_MAX
))
228 if ((size
!= 0) && (vme_base
> U64_MAX
+ 1 - size
))
232 if (((vme_base
+ size
) > VME_CRCSR_MAX
) ||
233 (vme_base
> VME_CRCSR_MAX
))
243 printk(KERN_ERR
"Invalid address space\n");
250 EXPORT_SYMBOL(vme_check_window
);
252 static u32
vme_get_aspace(int am
)
286 * vme_slave_request - Request a VME slave window resource.
287 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
288 * @address: Required VME address space.
289 * @cycle: Required VME data transfer cycle type.
291 * Request use of a VME window resource capable of being set for the requested
292 * address space and data transfer cycle.
294 * Return: Pointer to VME resource on success, NULL on failure.
296 struct vme_resource
*vme_slave_request(struct vme_dev
*vdev
, u32 address
,
299 struct vme_bridge
*bridge
;
300 struct list_head
*slave_pos
= NULL
;
301 struct vme_slave_resource
*allocated_image
= NULL
;
302 struct vme_slave_resource
*slave_image
= NULL
;
303 struct vme_resource
*resource
= NULL
;
305 bridge
= vdev
->bridge
;
306 if (bridge
== NULL
) {
307 printk(KERN_ERR
"Can't find VME bus\n");
311 /* Loop through slave resources */
312 list_for_each(slave_pos
, &bridge
->slave_resources
) {
313 slave_image
= list_entry(slave_pos
,
314 struct vme_slave_resource
, list
);
316 if (slave_image
== NULL
) {
317 printk(KERN_ERR
"Registered NULL Slave resource\n");
321 /* Find an unlocked and compatible image */
322 mutex_lock(&slave_image
->mtx
);
323 if (((slave_image
->address_attr
& address
) == address
) &&
324 ((slave_image
->cycle_attr
& cycle
) == cycle
) &&
325 (slave_image
->locked
== 0)) {
327 slave_image
->locked
= 1;
328 mutex_unlock(&slave_image
->mtx
);
329 allocated_image
= slave_image
;
332 mutex_unlock(&slave_image
->mtx
);
336 if (allocated_image
== NULL
)
339 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
340 if (resource
== NULL
) {
341 printk(KERN_WARNING
"Unable to allocate resource structure\n");
344 resource
->type
= VME_SLAVE
;
345 resource
->entry
= &allocated_image
->list
;
351 mutex_lock(&slave_image
->mtx
);
352 slave_image
->locked
= 0;
353 mutex_unlock(&slave_image
->mtx
);
358 EXPORT_SYMBOL(vme_slave_request
);
361 * vme_slave_set - Set VME slave window configuration.
362 * @resource: Pointer to VME slave resource.
363 * @enabled: State to which the window should be configured.
364 * @vme_base: Base address for the window.
365 * @size: Size of the VME window.
366 * @buf_base: Based address of buffer used to provide VME slave window storage.
367 * @aspace: VME address space for the VME window.
368 * @cycle: VME data transfer cycle type for the VME window.
370 * Set configuration for provided VME slave window.
372 * Return: Zero on success, -EINVAL if operation is not supported on this
373 * device, if an invalid resource has been provided or invalid
374 * attributes are provided. Hardware specific errors may also be
377 int vme_slave_set(struct vme_resource
*resource
, int enabled
,
378 unsigned long long vme_base
, unsigned long long size
,
379 dma_addr_t buf_base
, u32 aspace
, u32 cycle
)
381 struct vme_bridge
*bridge
= find_bridge(resource
);
382 struct vme_slave_resource
*image
;
385 if (resource
->type
!= VME_SLAVE
) {
386 printk(KERN_ERR
"Not a slave resource\n");
390 image
= list_entry(resource
->entry
, struct vme_slave_resource
, list
);
392 if (bridge
->slave_set
== NULL
) {
393 printk(KERN_ERR
"Function not supported\n");
397 if (!(((image
->address_attr
& aspace
) == aspace
) &&
398 ((image
->cycle_attr
& cycle
) == cycle
))) {
399 printk(KERN_ERR
"Invalid attributes\n");
403 retval
= vme_check_window(aspace
, vme_base
, size
);
407 return bridge
->slave_set(image
, enabled
, vme_base
, size
, buf_base
,
410 EXPORT_SYMBOL(vme_slave_set
);
413 * vme_slave_get - Retrieve VME slave window configuration.
414 * @resource: Pointer to VME slave resource.
415 * @enabled: Pointer to variable for storing state.
416 * @vme_base: Pointer to variable for storing window base address.
417 * @size: Pointer to variable for storing window size.
418 * @buf_base: Pointer to variable for storing slave buffer base address.
419 * @aspace: Pointer to variable for storing VME address space.
420 * @cycle: Pointer to variable for storing VME data transfer cycle type.
422 * Return configuration for provided VME slave window.
424 * Return: Zero on success, -EINVAL if operation is not supported on this
425 * device or if an invalid resource has been provided.
427 int vme_slave_get(struct vme_resource
*resource
, int *enabled
,
428 unsigned long long *vme_base
, unsigned long long *size
,
429 dma_addr_t
*buf_base
, u32
*aspace
, u32
*cycle
)
431 struct vme_bridge
*bridge
= find_bridge(resource
);
432 struct vme_slave_resource
*image
;
434 if (resource
->type
!= VME_SLAVE
) {
435 printk(KERN_ERR
"Not a slave resource\n");
439 image
= list_entry(resource
->entry
, struct vme_slave_resource
, list
);
441 if (bridge
->slave_get
== NULL
) {
442 printk(KERN_ERR
"vme_slave_get not supported\n");
446 return bridge
->slave_get(image
, enabled
, vme_base
, size
, buf_base
,
449 EXPORT_SYMBOL(vme_slave_get
);
452 * vme_slave_free - Free VME slave window
453 * @resource: Pointer to VME slave resource.
455 * Free the provided slave resource so that it may be reallocated.
457 void vme_slave_free(struct vme_resource
*resource
)
459 struct vme_slave_resource
*slave_image
;
461 if (resource
->type
!= VME_SLAVE
) {
462 printk(KERN_ERR
"Not a slave resource\n");
466 slave_image
= list_entry(resource
->entry
, struct vme_slave_resource
,
468 if (slave_image
== NULL
) {
469 printk(KERN_ERR
"Can't find slave resource\n");
474 mutex_lock(&slave_image
->mtx
);
475 if (slave_image
->locked
== 0)
476 printk(KERN_ERR
"Image is already free\n");
478 slave_image
->locked
= 0;
479 mutex_unlock(&slave_image
->mtx
);
481 /* Free up resource memory */
484 EXPORT_SYMBOL(vme_slave_free
);
487 * vme_master_request - Request a VME master window resource.
488 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
489 * @address: Required VME address space.
490 * @cycle: Required VME data transfer cycle type.
491 * @dwidth: Required VME data transfer width.
493 * Request use of a VME window resource capable of being set for the requested
494 * address space, data transfer cycle and width.
496 * Return: Pointer to VME resource on success, NULL on failure.
498 struct vme_resource
*vme_master_request(struct vme_dev
*vdev
, u32 address
,
499 u32 cycle
, u32 dwidth
)
501 struct vme_bridge
*bridge
;
502 struct list_head
*master_pos
= NULL
;
503 struct vme_master_resource
*allocated_image
= NULL
;
504 struct vme_master_resource
*master_image
= NULL
;
505 struct vme_resource
*resource
= NULL
;
507 bridge
= vdev
->bridge
;
508 if (bridge
== NULL
) {
509 printk(KERN_ERR
"Can't find VME bus\n");
513 /* Loop through master resources */
514 list_for_each(master_pos
, &bridge
->master_resources
) {
515 master_image
= list_entry(master_pos
,
516 struct vme_master_resource
, list
);
518 if (master_image
== NULL
) {
519 printk(KERN_WARNING
"Registered NULL master resource\n");
523 /* Find an unlocked and compatible image */
524 spin_lock(&master_image
->lock
);
525 if (((master_image
->address_attr
& address
) == address
) &&
526 ((master_image
->cycle_attr
& cycle
) == cycle
) &&
527 ((master_image
->width_attr
& dwidth
) == dwidth
) &&
528 (master_image
->locked
== 0)) {
530 master_image
->locked
= 1;
531 spin_unlock(&master_image
->lock
);
532 allocated_image
= master_image
;
535 spin_unlock(&master_image
->lock
);
538 /* Check to see if we found a resource */
539 if (allocated_image
== NULL
) {
540 printk(KERN_ERR
"Can't find a suitable resource\n");
544 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
545 if (resource
== NULL
) {
546 printk(KERN_ERR
"Unable to allocate resource structure\n");
549 resource
->type
= VME_MASTER
;
550 resource
->entry
= &allocated_image
->list
;
556 spin_lock(&master_image
->lock
);
557 master_image
->locked
= 0;
558 spin_unlock(&master_image
->lock
);
563 EXPORT_SYMBOL(vme_master_request
);
566 * vme_master_set - Set VME master window configuration.
567 * @resource: Pointer to VME master resource.
568 * @enabled: State to which the window should be configured.
569 * @vme_base: Base address for the window.
570 * @size: Size of the VME window.
571 * @aspace: VME address space for the VME window.
572 * @cycle: VME data transfer cycle type for the VME window.
573 * @dwidth: VME data transfer width for the VME window.
575 * Set configuration for provided VME master window.
577 * Return: Zero on success, -EINVAL if operation is not supported on this
578 * device, if an invalid resource has been provided or invalid
579 * attributes are provided. Hardware specific errors may also be
582 int vme_master_set(struct vme_resource
*resource
, int enabled
,
583 unsigned long long vme_base
, unsigned long long size
, u32 aspace
,
584 u32 cycle
, u32 dwidth
)
586 struct vme_bridge
*bridge
= find_bridge(resource
);
587 struct vme_master_resource
*image
;
590 if (resource
->type
!= VME_MASTER
) {
591 printk(KERN_ERR
"Not a master resource\n");
595 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
597 if (bridge
->master_set
== NULL
) {
598 printk(KERN_WARNING
"vme_master_set not supported\n");
602 if (!(((image
->address_attr
& aspace
) == aspace
) &&
603 ((image
->cycle_attr
& cycle
) == cycle
) &&
604 ((image
->width_attr
& dwidth
) == dwidth
))) {
605 printk(KERN_WARNING
"Invalid attributes\n");
609 retval
= vme_check_window(aspace
, vme_base
, size
);
613 return bridge
->master_set(image
, enabled
, vme_base
, size
, aspace
,
616 EXPORT_SYMBOL(vme_master_set
);
619 * vme_master_get - Retrieve VME master window configuration.
620 * @resource: Pointer to VME master resource.
621 * @enabled: Pointer to variable for storing state.
622 * @vme_base: Pointer to variable for storing window base address.
623 * @size: Pointer to variable for storing window size.
624 * @aspace: Pointer to variable for storing VME address space.
625 * @cycle: Pointer to variable for storing VME data transfer cycle type.
626 * @dwidth: Pointer to variable for storing VME data transfer width.
628 * Return configuration for provided VME master window.
630 * Return: Zero on success, -EINVAL if operation is not supported on this
631 * device or if an invalid resource has been provided.
633 int vme_master_get(struct vme_resource
*resource
, int *enabled
,
634 unsigned long long *vme_base
, unsigned long long *size
, u32
*aspace
,
635 u32
*cycle
, u32
*dwidth
)
637 struct vme_bridge
*bridge
= find_bridge(resource
);
638 struct vme_master_resource
*image
;
640 if (resource
->type
!= VME_MASTER
) {
641 printk(KERN_ERR
"Not a master resource\n");
645 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
647 if (bridge
->master_get
== NULL
) {
648 printk(KERN_WARNING
"%s not supported\n", __func__
);
652 return bridge
->master_get(image
, enabled
, vme_base
, size
, aspace
,
655 EXPORT_SYMBOL(vme_master_get
);
658 * vme_master_write - Read data from VME space into a buffer.
659 * @resource: Pointer to VME master resource.
660 * @buf: Pointer to buffer where data should be transferred.
661 * @count: Number of bytes to transfer.
662 * @offset: Offset into VME master window at which to start transfer.
664 * Perform read of count bytes of data from location on VME bus which maps into
665 * the VME master window at offset to buf.
667 * Return: Number of bytes read, -EINVAL if resource is not a VME master
668 * resource or read operation is not supported. -EFAULT returned if
669 * invalid offset is provided. Hardware specific errors may also be
672 ssize_t
vme_master_read(struct vme_resource
*resource
, void *buf
, size_t count
,
675 struct vme_bridge
*bridge
= find_bridge(resource
);
676 struct vme_master_resource
*image
;
679 if (bridge
->master_read
== NULL
) {
680 printk(KERN_WARNING
"Reading from resource not supported\n");
684 if (resource
->type
!= VME_MASTER
) {
685 printk(KERN_ERR
"Not a master resource\n");
689 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
691 length
= vme_get_size(resource
);
693 if (offset
> length
) {
694 printk(KERN_WARNING
"Invalid Offset\n");
698 if ((offset
+ count
) > length
)
699 count
= length
- offset
;
701 return bridge
->master_read(image
, buf
, count
, offset
);
704 EXPORT_SYMBOL(vme_master_read
);
707 * vme_master_write - Write data out to VME space from a buffer.
708 * @resource: Pointer to VME master resource.
709 * @buf: Pointer to buffer holding data to transfer.
710 * @count: Number of bytes to transfer.
711 * @offset: Offset into VME master window at which to start transfer.
713 * Perform write of count bytes of data from buf to location on VME bus which
714 * maps into the VME master window at offset.
716 * Return: Number of bytes written, -EINVAL if resource is not a VME master
717 * resource or write operation is not supported. -EFAULT returned if
718 * invalid offset is provided. Hardware specific errors may also be
721 ssize_t
vme_master_write(struct vme_resource
*resource
, void *buf
,
722 size_t count
, loff_t offset
)
724 struct vme_bridge
*bridge
= find_bridge(resource
);
725 struct vme_master_resource
*image
;
728 if (bridge
->master_write
== NULL
) {
729 printk(KERN_WARNING
"Writing to resource not supported\n");
733 if (resource
->type
!= VME_MASTER
) {
734 printk(KERN_ERR
"Not a master resource\n");
738 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
740 length
= vme_get_size(resource
);
742 if (offset
> length
) {
743 printk(KERN_WARNING
"Invalid Offset\n");
747 if ((offset
+ count
) > length
)
748 count
= length
- offset
;
750 return bridge
->master_write(image
, buf
, count
, offset
);
752 EXPORT_SYMBOL(vme_master_write
);
755 * vme_master_rmw - Perform read-modify-write cycle.
756 * @resource: Pointer to VME master resource.
757 * @mask: Bits to be compared and swapped in operation.
758 * @compare: Bits to be compared with data read from offset.
759 * @swap: Bits to be swapped in data read from offset.
760 * @offset: Offset into VME master window at which to perform operation.
762 * Perform read-modify-write cycle on provided location:
763 * - Location on VME bus is read.
764 * - Bits selected by mask are compared with compare.
765 * - Where a selected bit matches that in compare and are selected in swap,
766 * the bit is swapped.
767 * - Result written back to location on VME bus.
769 * Return: Bytes written on success, -EINVAL if resource is not a VME master
770 * resource or RMW operation is not supported. Hardware specific
771 * errors may also be returned.
773 unsigned int vme_master_rmw(struct vme_resource
*resource
, unsigned int mask
,
774 unsigned int compare
, unsigned int swap
, loff_t offset
)
776 struct vme_bridge
*bridge
= find_bridge(resource
);
777 struct vme_master_resource
*image
;
779 if (bridge
->master_rmw
== NULL
) {
780 printk(KERN_WARNING
"Writing to resource not supported\n");
784 if (resource
->type
!= VME_MASTER
) {
785 printk(KERN_ERR
"Not a master resource\n");
789 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
791 return bridge
->master_rmw(image
, mask
, compare
, swap
, offset
);
793 EXPORT_SYMBOL(vme_master_rmw
);
796 * vme_master_mmap - Mmap region of VME master window.
797 * @resource: Pointer to VME master resource.
798 * @vma: Pointer to definition of user mapping.
800 * Memory map a region of the VME master window into user space.
802 * Return: Zero on success, -EINVAL if resource is not a VME master
803 * resource or -EFAULT if map exceeds window size. Other generic mmap
804 * errors may also be returned.
806 int vme_master_mmap(struct vme_resource
*resource
, struct vm_area_struct
*vma
)
808 struct vme_master_resource
*image
;
809 phys_addr_t phys_addr
;
810 unsigned long vma_size
;
812 if (resource
->type
!= VME_MASTER
) {
813 pr_err("Not a master resource\n");
817 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
818 phys_addr
= image
->bus_resource
.start
+ (vma
->vm_pgoff
<< PAGE_SHIFT
);
819 vma_size
= vma
->vm_end
- vma
->vm_start
;
821 if (phys_addr
+ vma_size
> image
->bus_resource
.end
+ 1) {
822 pr_err("Map size cannot exceed the window size\n");
826 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
828 return vm_iomap_memory(vma
, phys_addr
, vma
->vm_end
- vma
->vm_start
);
830 EXPORT_SYMBOL(vme_master_mmap
);
833 * vme_master_free - Free VME master window
834 * @resource: Pointer to VME master resource.
836 * Free the provided master resource so that it may be reallocated.
838 void vme_master_free(struct vme_resource
*resource
)
840 struct vme_master_resource
*master_image
;
842 if (resource
->type
!= VME_MASTER
) {
843 printk(KERN_ERR
"Not a master resource\n");
847 master_image
= list_entry(resource
->entry
, struct vme_master_resource
,
849 if (master_image
== NULL
) {
850 printk(KERN_ERR
"Can't find master resource\n");
855 spin_lock(&master_image
->lock
);
856 if (master_image
->locked
== 0)
857 printk(KERN_ERR
"Image is already free\n");
859 master_image
->locked
= 0;
860 spin_unlock(&master_image
->lock
);
862 /* Free up resource memory */
865 EXPORT_SYMBOL(vme_master_free
);
868 * vme_dma_request - Request a DMA controller.
869 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
870 * @route: Required src/destination combination.
872 * Request a VME DMA controller with capability to perform transfers bewteen
873 * requested source/destination combination.
875 * Return: Pointer to VME DMA resource on success, NULL on failure.
877 struct vme_resource
*vme_dma_request(struct vme_dev
*vdev
, u32 route
)
879 struct vme_bridge
*bridge
;
880 struct list_head
*dma_pos
= NULL
;
881 struct vme_dma_resource
*allocated_ctrlr
= NULL
;
882 struct vme_dma_resource
*dma_ctrlr
= NULL
;
883 struct vme_resource
*resource
= NULL
;
885 /* XXX Not checking resource attributes */
886 printk(KERN_ERR
"No VME resource Attribute tests done\n");
888 bridge
= vdev
->bridge
;
889 if (bridge
== NULL
) {
890 printk(KERN_ERR
"Can't find VME bus\n");
894 /* Loop through DMA resources */
895 list_for_each(dma_pos
, &bridge
->dma_resources
) {
896 dma_ctrlr
= list_entry(dma_pos
,
897 struct vme_dma_resource
, list
);
899 if (dma_ctrlr
== NULL
) {
900 printk(KERN_ERR
"Registered NULL DMA resource\n");
904 /* Find an unlocked and compatible controller */
905 mutex_lock(&dma_ctrlr
->mtx
);
906 if (((dma_ctrlr
->route_attr
& route
) == route
) &&
907 (dma_ctrlr
->locked
== 0)) {
909 dma_ctrlr
->locked
= 1;
910 mutex_unlock(&dma_ctrlr
->mtx
);
911 allocated_ctrlr
= dma_ctrlr
;
914 mutex_unlock(&dma_ctrlr
->mtx
);
917 /* Check to see if we found a resource */
918 if (allocated_ctrlr
== NULL
)
921 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
922 if (resource
== NULL
) {
923 printk(KERN_WARNING
"Unable to allocate resource structure\n");
926 resource
->type
= VME_DMA
;
927 resource
->entry
= &allocated_ctrlr
->list
;
933 mutex_lock(&dma_ctrlr
->mtx
);
934 dma_ctrlr
->locked
= 0;
935 mutex_unlock(&dma_ctrlr
->mtx
);
940 EXPORT_SYMBOL(vme_dma_request
);
943 * vme_new_dma_list - Create new VME DMA list.
944 * @resource: Pointer to VME DMA resource.
946 * Create a new VME DMA list. It is the responsibility of the user to free
947 * the list once it is no longer required with vme_dma_list_free().
949 * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
952 struct vme_dma_list
*vme_new_dma_list(struct vme_resource
*resource
)
954 struct vme_dma_resource
*ctrlr
;
955 struct vme_dma_list
*dma_list
;
957 if (resource
->type
!= VME_DMA
) {
958 printk(KERN_ERR
"Not a DMA resource\n");
962 ctrlr
= list_entry(resource
->entry
, struct vme_dma_resource
, list
);
964 dma_list
= kmalloc(sizeof(struct vme_dma_list
), GFP_KERNEL
);
965 if (dma_list
== NULL
) {
966 printk(KERN_ERR
"Unable to allocate memory for new DMA list\n");
969 INIT_LIST_HEAD(&dma_list
->entries
);
970 dma_list
->parent
= ctrlr
;
971 mutex_init(&dma_list
->mtx
);
975 EXPORT_SYMBOL(vme_new_dma_list
);
978 * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
979 * @pattern: Value to use used as pattern
980 * @type: Type of pattern to be written.
982 * Create VME DMA list attribute for pattern generation. It is the
983 * responsibility of the user to free used attributes using
984 * vme_dma_free_attribute().
986 * Return: Pointer to VME DMA attribute, NULL on failure.
988 struct vme_dma_attr
*vme_dma_pattern_attribute(u32 pattern
, u32 type
)
990 struct vme_dma_attr
*attributes
;
991 struct vme_dma_pattern
*pattern_attr
;
993 attributes
= kmalloc(sizeof(struct vme_dma_attr
), GFP_KERNEL
);
994 if (attributes
== NULL
) {
995 printk(KERN_ERR
"Unable to allocate memory for attributes structure\n");
999 pattern_attr
= kmalloc(sizeof(struct vme_dma_pattern
), GFP_KERNEL
);
1000 if (pattern_attr
== NULL
) {
1001 printk(KERN_ERR
"Unable to allocate memory for pattern attributes\n");
1005 attributes
->type
= VME_DMA_PATTERN
;
1006 attributes
->private = (void *)pattern_attr
;
1008 pattern_attr
->pattern
= pattern
;
1009 pattern_attr
->type
= type
;
1018 EXPORT_SYMBOL(vme_dma_pattern_attribute
);
1021 * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
1022 * @address: PCI base address for DMA transfer.
1024 * Create VME DMA list attribute pointing to a location on PCI for DMA
1025 * transfers. It is the responsibility of the user to free used attributes
1026 * using vme_dma_free_attribute().
1028 * Return: Pointer to VME DMA attribute, NULL on failure.
1030 struct vme_dma_attr
*vme_dma_pci_attribute(dma_addr_t address
)
1032 struct vme_dma_attr
*attributes
;
1033 struct vme_dma_pci
*pci_attr
;
1035 /* XXX Run some sanity checks here */
1037 attributes
= kmalloc(sizeof(struct vme_dma_attr
), GFP_KERNEL
);
1038 if (attributes
== NULL
) {
1039 printk(KERN_ERR
"Unable to allocate memory for attributes structure\n");
1043 pci_attr
= kmalloc(sizeof(struct vme_dma_pci
), GFP_KERNEL
);
1044 if (pci_attr
== NULL
) {
1045 printk(KERN_ERR
"Unable to allocate memory for PCI attributes\n");
1051 attributes
->type
= VME_DMA_PCI
;
1052 attributes
->private = (void *)pci_attr
;
1054 pci_attr
->address
= address
;
1063 EXPORT_SYMBOL(vme_dma_pci_attribute
);
1066 * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
1067 * @address: VME base address for DMA transfer.
1068 * @aspace: VME address space to use for DMA transfer.
1069 * @cycle: VME bus cycle to use for DMA transfer.
1070 * @dwidth: VME data width to use for DMA transfer.
1072 * Create VME DMA list attribute pointing to a location on the VME bus for DMA
1073 * transfers. It is the responsibility of the user to free used attributes
1074 * using vme_dma_free_attribute().
1076 * Return: Pointer to VME DMA attribute, NULL on failure.
1078 struct vme_dma_attr
*vme_dma_vme_attribute(unsigned long long address
,
1079 u32 aspace
, u32 cycle
, u32 dwidth
)
1081 struct vme_dma_attr
*attributes
;
1082 struct vme_dma_vme
*vme_attr
;
1084 attributes
= kmalloc(
1085 sizeof(struct vme_dma_attr
), GFP_KERNEL
);
1086 if (attributes
== NULL
) {
1087 printk(KERN_ERR
"Unable to allocate memory for attributes structure\n");
1091 vme_attr
= kmalloc(sizeof(struct vme_dma_vme
), GFP_KERNEL
);
1092 if (vme_attr
== NULL
) {
1093 printk(KERN_ERR
"Unable to allocate memory for VME attributes\n");
1097 attributes
->type
= VME_DMA_VME
;
1098 attributes
->private = (void *)vme_attr
;
1100 vme_attr
->address
= address
;
1101 vme_attr
->aspace
= aspace
;
1102 vme_attr
->cycle
= cycle
;
1103 vme_attr
->dwidth
= dwidth
;
1112 EXPORT_SYMBOL(vme_dma_vme_attribute
);
1115 * vme_dma_free_attribute - Free DMA list attribute.
1116 * @attributes: Pointer to DMA list attribute.
1118 * Free VME DMA list attribute. VME DMA list attributes can be safely freed
1119 * once vme_dma_list_add() has returned.
1121 void vme_dma_free_attribute(struct vme_dma_attr
*attributes
)
1123 kfree(attributes
->private);
1126 EXPORT_SYMBOL(vme_dma_free_attribute
);
1129 * vme_dma_list_add - Add enty to a VME DMA list.
1130 * @list: Pointer to VME list.
1131 * @src: Pointer to DMA list attribute to use as source.
1132 * @dest: Pointer to DMA list attribute to use as destination.
1133 * @count: Number of bytes to transfer.
1135 * Add an entry to the provided VME DMA list. Entry requires pointers to source
1136 * and destination DMA attributes and a count.
1138 * Please note, the attributes supported as source and destinations for
1139 * transfers are hardware dependent.
1141 * Return: Zero on success, -EINVAL if operation is not supported on this
1142 * device or if the link list has already been submitted for execution.
1143 * Hardware specific errors also possible.
1145 int vme_dma_list_add(struct vme_dma_list
*list
, struct vme_dma_attr
*src
,
1146 struct vme_dma_attr
*dest
, size_t count
)
1148 struct vme_bridge
*bridge
= list
->parent
->parent
;
1151 if (bridge
->dma_list_add
== NULL
) {
1152 printk(KERN_WARNING
"Link List DMA generation not supported\n");
1156 if (!mutex_trylock(&list
->mtx
)) {
1157 printk(KERN_ERR
"Link List already submitted\n");
1161 retval
= bridge
->dma_list_add(list
, src
, dest
, count
);
1163 mutex_unlock(&list
->mtx
);
1167 EXPORT_SYMBOL(vme_dma_list_add
);
1170 * vme_dma_list_exec - Queue a VME DMA list for execution.
1171 * @list: Pointer to VME list.
1173 * Queue the provided VME DMA list for execution. The call will return once the
1174 * list has been executed.
1176 * Return: Zero on success, -EINVAL if operation is not supported on this
1177 * device. Hardware specific errors also possible.
1179 int vme_dma_list_exec(struct vme_dma_list
*list
)
1181 struct vme_bridge
*bridge
= list
->parent
->parent
;
1184 if (bridge
->dma_list_exec
== NULL
) {
1185 printk(KERN_ERR
"Link List DMA execution not supported\n");
1189 mutex_lock(&list
->mtx
);
1191 retval
= bridge
->dma_list_exec(list
);
1193 mutex_unlock(&list
->mtx
);
1197 EXPORT_SYMBOL(vme_dma_list_exec
);
1200 * vme_dma_list_free - Free a VME DMA list.
1201 * @list: Pointer to VME list.
1203 * Free the provided DMA list and all its entries.
1205 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1206 * is still in use. Hardware specific errors also possible.
1208 int vme_dma_list_free(struct vme_dma_list
*list
)
1210 struct vme_bridge
*bridge
= list
->parent
->parent
;
1213 if (bridge
->dma_list_empty
== NULL
) {
1214 printk(KERN_WARNING
"Emptying of Link Lists not supported\n");
1218 if (!mutex_trylock(&list
->mtx
)) {
1219 printk(KERN_ERR
"Link List in use\n");
1224 * Empty out all of the entries from the DMA list. We need to go to the
1225 * low level driver as DMA entries are driver specific.
1227 retval
= bridge
->dma_list_empty(list
);
1229 printk(KERN_ERR
"Unable to empty link-list entries\n");
1230 mutex_unlock(&list
->mtx
);
1233 mutex_unlock(&list
->mtx
);
1238 EXPORT_SYMBOL(vme_dma_list_free
);
1241 * vme_dma_free - Free a VME DMA resource.
1242 * @resource: Pointer to VME DMA resource.
1244 * Free the provided DMA resource so that it may be reallocated.
1246 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1249 int vme_dma_free(struct vme_resource
*resource
)
1251 struct vme_dma_resource
*ctrlr
;
1253 if (resource
->type
!= VME_DMA
) {
1254 printk(KERN_ERR
"Not a DMA resource\n");
1258 ctrlr
= list_entry(resource
->entry
, struct vme_dma_resource
, list
);
1260 if (!mutex_trylock(&ctrlr
->mtx
)) {
1261 printk(KERN_ERR
"Resource busy, can't free\n");
1265 if (!(list_empty(&ctrlr
->pending
) && list_empty(&ctrlr
->running
))) {
1266 printk(KERN_WARNING
"Resource still processing transfers\n");
1267 mutex_unlock(&ctrlr
->mtx
);
1273 mutex_unlock(&ctrlr
->mtx
);
1279 EXPORT_SYMBOL(vme_dma_free
);
1281 void vme_bus_error_handler(struct vme_bridge
*bridge
,
1282 unsigned long long address
, int am
)
1284 struct list_head
*handler_pos
= NULL
;
1285 struct vme_error_handler
*handler
;
1286 int handler_triggered
= 0;
1287 u32 aspace
= vme_get_aspace(am
);
1289 list_for_each(handler_pos
, &bridge
->vme_error_handlers
) {
1290 handler
= list_entry(handler_pos
, struct vme_error_handler
,
1292 if ((aspace
== handler
->aspace
) &&
1293 (address
>= handler
->start
) &&
1294 (address
< handler
->end
)) {
1295 if (!handler
->num_errors
)
1296 handler
->first_error
= address
;
1297 if (handler
->num_errors
!= UINT_MAX
)
1298 handler
->num_errors
++;
1299 handler_triggered
= 1;
1303 if (!handler_triggered
)
1304 dev_err(bridge
->parent
,
1305 "Unhandled VME access error at address 0x%llx\n",
1308 EXPORT_SYMBOL(vme_bus_error_handler
);
1310 struct vme_error_handler
*vme_register_error_handler(
1311 struct vme_bridge
*bridge
, u32 aspace
,
1312 unsigned long long address
, size_t len
)
1314 struct vme_error_handler
*handler
;
1316 handler
= kmalloc(sizeof(*handler
), GFP_KERNEL
);
1320 handler
->aspace
= aspace
;
1321 handler
->start
= address
;
1322 handler
->end
= address
+ len
;
1323 handler
->num_errors
= 0;
1324 handler
->first_error
= 0;
1325 list_add_tail(&handler
->list
, &bridge
->vme_error_handlers
);
1329 EXPORT_SYMBOL(vme_register_error_handler
);
1331 void vme_unregister_error_handler(struct vme_error_handler
*handler
)
1333 list_del(&handler
->list
);
1336 EXPORT_SYMBOL(vme_unregister_error_handler
);
1338 void vme_irq_handler(struct vme_bridge
*bridge
, int level
, int statid
)
1340 void (*call
)(int, int, void *);
1343 call
= bridge
->irq
[level
- 1].callback
[statid
].func
;
1344 priv_data
= bridge
->irq
[level
- 1].callback
[statid
].priv_data
;
1347 call(level
, statid
, priv_data
);
1349 printk(KERN_WARNING
"Spurious VME interrupt, level:%x, vector:%x\n",
1352 EXPORT_SYMBOL(vme_irq_handler
);
1355 * vme_irq_request - Request a specific VME interrupt.
1356 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1357 * @level: Interrupt priority being requested.
1358 * @statid: Interrupt vector being requested.
1359 * @callback: Pointer to callback function called when VME interrupt/vector
1361 * @priv_data: Generic pointer that will be passed to the callback function.
1363 * Request callback to be attached as a handler for VME interrupts with provided
1366 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1367 * function is not supported, -EBUSY if the level/statid combination is
1368 * already in use. Hardware specific errors also possible.
1370 int vme_irq_request(struct vme_dev
*vdev
, int level
, int statid
,
1371 void (*callback
)(int, int, void *),
1374 struct vme_bridge
*bridge
;
1376 bridge
= vdev
->bridge
;
1377 if (bridge
== NULL
) {
1378 printk(KERN_ERR
"Can't find VME bus\n");
1382 if ((level
< 1) || (level
> 7)) {
1383 printk(KERN_ERR
"Invalid interrupt level\n");
1387 if (bridge
->irq_set
== NULL
) {
1388 printk(KERN_ERR
"Configuring interrupts not supported\n");
1392 mutex_lock(&bridge
->irq_mtx
);
1394 if (bridge
->irq
[level
- 1].callback
[statid
].func
) {
1395 mutex_unlock(&bridge
->irq_mtx
);
1396 printk(KERN_WARNING
"VME Interrupt already taken\n");
1400 bridge
->irq
[level
- 1].count
++;
1401 bridge
->irq
[level
- 1].callback
[statid
].priv_data
= priv_data
;
1402 bridge
->irq
[level
- 1].callback
[statid
].func
= callback
;
1404 /* Enable IRQ level */
1405 bridge
->irq_set(bridge
, level
, 1, 1);
1407 mutex_unlock(&bridge
->irq_mtx
);
1411 EXPORT_SYMBOL(vme_irq_request
);
1414 * vme_irq_free - Free a VME interrupt.
1415 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1416 * @level: Interrupt priority of interrupt being freed.
1417 * @statid: Interrupt vector of interrupt being freed.
1419 * Remove previously attached callback from VME interrupt priority/vector.
1421 void vme_irq_free(struct vme_dev
*vdev
, int level
, int statid
)
1423 struct vme_bridge
*bridge
;
1425 bridge
= vdev
->bridge
;
1426 if (bridge
== NULL
) {
1427 printk(KERN_ERR
"Can't find VME bus\n");
1431 if ((level
< 1) || (level
> 7)) {
1432 printk(KERN_ERR
"Invalid interrupt level\n");
1436 if (bridge
->irq_set
== NULL
) {
1437 printk(KERN_ERR
"Configuring interrupts not supported\n");
1441 mutex_lock(&bridge
->irq_mtx
);
1443 bridge
->irq
[level
- 1].count
--;
1445 /* Disable IRQ level if no more interrupts attached at this level*/
1446 if (bridge
->irq
[level
- 1].count
== 0)
1447 bridge
->irq_set(bridge
, level
, 0, 1);
1449 bridge
->irq
[level
- 1].callback
[statid
].func
= NULL
;
1450 bridge
->irq
[level
- 1].callback
[statid
].priv_data
= NULL
;
1452 mutex_unlock(&bridge
->irq_mtx
);
1454 EXPORT_SYMBOL(vme_irq_free
);
1457 * vme_irq_generate - Generate VME interrupt.
1458 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1459 * @level: Interrupt priority at which to assert the interrupt.
1460 * @statid: Interrupt vector to associate with the interrupt.
1462 * Generate a VME interrupt of the provided level and with the provided
1465 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1466 * function is not supported. Hardware specific errors also possible.
1468 int vme_irq_generate(struct vme_dev
*vdev
, int level
, int statid
)
1470 struct vme_bridge
*bridge
;
1472 bridge
= vdev
->bridge
;
1473 if (bridge
== NULL
) {
1474 printk(KERN_ERR
"Can't find VME bus\n");
1478 if ((level
< 1) || (level
> 7)) {
1479 printk(KERN_WARNING
"Invalid interrupt level\n");
1483 if (bridge
->irq_generate
== NULL
) {
1484 printk(KERN_WARNING
"Interrupt generation not supported\n");
1488 return bridge
->irq_generate(bridge
, level
, statid
);
1490 EXPORT_SYMBOL(vme_irq_generate
);
1493 * vme_lm_request - Request a VME location monitor
1494 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1496 * Allocate a location monitor resource to the driver. A location monitor
1497 * allows the driver to monitor accesses to a contiguous number of
1498 * addresses on the VME bus.
1500 * Return: Pointer to a VME resource on success or NULL on failure.
1502 struct vme_resource
*vme_lm_request(struct vme_dev
*vdev
)
1504 struct vme_bridge
*bridge
;
1505 struct list_head
*lm_pos
= NULL
;
1506 struct vme_lm_resource
*allocated_lm
= NULL
;
1507 struct vme_lm_resource
*lm
= NULL
;
1508 struct vme_resource
*resource
= NULL
;
1510 bridge
= vdev
->bridge
;
1511 if (bridge
== NULL
) {
1512 printk(KERN_ERR
"Can't find VME bus\n");
1516 /* Loop through LM resources */
1517 list_for_each(lm_pos
, &bridge
->lm_resources
) {
1518 lm
= list_entry(lm_pos
,
1519 struct vme_lm_resource
, list
);
1522 printk(KERN_ERR
"Registered NULL Location Monitor resource\n");
1526 /* Find an unlocked controller */
1527 mutex_lock(&lm
->mtx
);
1528 if (lm
->locked
== 0) {
1530 mutex_unlock(&lm
->mtx
);
1534 mutex_unlock(&lm
->mtx
);
1537 /* Check to see if we found a resource */
1538 if (allocated_lm
== NULL
)
1541 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
1542 if (resource
== NULL
) {
1543 printk(KERN_ERR
"Unable to allocate resource structure\n");
1546 resource
->type
= VME_LM
;
1547 resource
->entry
= &allocated_lm
->list
;
1553 mutex_lock(&lm
->mtx
);
1555 mutex_unlock(&lm
->mtx
);
1560 EXPORT_SYMBOL(vme_lm_request
);
1563 * vme_lm_count - Determine number of VME Addresses monitored
1564 * @resource: Pointer to VME location monitor resource.
1566 * The number of contiguous addresses monitored is hardware dependent.
1567 * Return the number of contiguous addresses monitored by the
1570 * Return: Count of addresses monitored or -EINVAL when provided with an
1571 * invalid location monitor resource.
1573 int vme_lm_count(struct vme_resource
*resource
)
1575 struct vme_lm_resource
*lm
;
1577 if (resource
->type
!= VME_LM
) {
1578 printk(KERN_ERR
"Not a Location Monitor resource\n");
1582 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1584 return lm
->monitors
;
1586 EXPORT_SYMBOL(vme_lm_count
);
1589 * vme_lm_set - Configure location monitor
1590 * @resource: Pointer to VME location monitor resource.
1591 * @lm_base: Base address to monitor.
1592 * @aspace: VME address space to monitor.
1593 * @cycle: VME bus cycle type to monitor.
1595 * Set the base address, address space and cycle type of accesses to be
1596 * monitored by the location monitor.
1598 * Return: Zero on success, -EINVAL when provided with an invalid location
1599 * monitor resource or function is not supported. Hardware specific
1600 * errors may also be returned.
1602 int vme_lm_set(struct vme_resource
*resource
, unsigned long long lm_base
,
1603 u32 aspace
, u32 cycle
)
1605 struct vme_bridge
*bridge
= find_bridge(resource
);
1606 struct vme_lm_resource
*lm
;
1608 if (resource
->type
!= VME_LM
) {
1609 printk(KERN_ERR
"Not a Location Monitor resource\n");
1613 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1615 if (bridge
->lm_set
== NULL
) {
1616 printk(KERN_ERR
"vme_lm_set not supported\n");
1620 return bridge
->lm_set(lm
, lm_base
, aspace
, cycle
);
1622 EXPORT_SYMBOL(vme_lm_set
);
1625 * vme_lm_get - Retrieve location monitor settings
1626 * @resource: Pointer to VME location monitor resource.
1627 * @lm_base: Pointer used to output the base address monitored.
1628 * @aspace: Pointer used to output the address space monitored.
1629 * @cycle: Pointer used to output the VME bus cycle type monitored.
1631 * Retrieve the base address, address space and cycle type of accesses to
1632 * be monitored by the location monitor.
1634 * Return: Zero on success, -EINVAL when provided with an invalid location
1635 * monitor resource or function is not supported. Hardware specific
1636 * errors may also be returned.
1638 int vme_lm_get(struct vme_resource
*resource
, unsigned long long *lm_base
,
1639 u32
*aspace
, u32
*cycle
)
1641 struct vme_bridge
*bridge
= find_bridge(resource
);
1642 struct vme_lm_resource
*lm
;
1644 if (resource
->type
!= VME_LM
) {
1645 printk(KERN_ERR
"Not a Location Monitor resource\n");
1649 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1651 if (bridge
->lm_get
== NULL
) {
1652 printk(KERN_ERR
"vme_lm_get not supported\n");
1656 return bridge
->lm_get(lm
, lm_base
, aspace
, cycle
);
1658 EXPORT_SYMBOL(vme_lm_get
);
1661 * vme_lm_attach - Provide callback for location monitor address
1662 * @resource: Pointer to VME location monitor resource.
1663 * @monitor: Offset to which callback should be attached.
1664 * @callback: Pointer to callback function called when triggered.
1665 * @data: Generic pointer that will be passed to the callback function.
1667 * Attach a callback to the specificed offset into the location monitors
1668 * monitored addresses. A generic pointer is provided to allow data to be
1669 * passed to the callback when called.
1671 * Return: Zero on success, -EINVAL when provided with an invalid location
1672 * monitor resource or function is not supported. Hardware specific
1673 * errors may also be returned.
1675 int vme_lm_attach(struct vme_resource
*resource
, int monitor
,
1676 void (*callback
)(void *), void *data
)
1678 struct vme_bridge
*bridge
= find_bridge(resource
);
1679 struct vme_lm_resource
*lm
;
1681 if (resource
->type
!= VME_LM
) {
1682 printk(KERN_ERR
"Not a Location Monitor resource\n");
1686 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1688 if (bridge
->lm_attach
== NULL
) {
1689 printk(KERN_ERR
"vme_lm_attach not supported\n");
1693 return bridge
->lm_attach(lm
, monitor
, callback
, data
);
1695 EXPORT_SYMBOL(vme_lm_attach
);
1698 * vme_lm_detach - Remove callback for location monitor address
1699 * @resource: Pointer to VME location monitor resource.
1700 * @monitor: Offset to which callback should be removed.
1702 * Remove the callback associated with the specificed offset into the
1703 * location monitors monitored addresses.
1705 * Return: Zero on success, -EINVAL when provided with an invalid location
1706 * monitor resource or function is not supported. Hardware specific
1707 * errors may also be returned.
1709 int vme_lm_detach(struct vme_resource
*resource
, int monitor
)
1711 struct vme_bridge
*bridge
= find_bridge(resource
);
1712 struct vme_lm_resource
*lm
;
1714 if (resource
->type
!= VME_LM
) {
1715 printk(KERN_ERR
"Not a Location Monitor resource\n");
1719 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1721 if (bridge
->lm_detach
== NULL
) {
1722 printk(KERN_ERR
"vme_lm_detach not supported\n");
1726 return bridge
->lm_detach(lm
, monitor
);
1728 EXPORT_SYMBOL(vme_lm_detach
);
1731 * vme_lm_free - Free allocated VME location monitor
1732 * @resource: Pointer to VME location monitor resource.
1734 * Free allocation of a VME location monitor.
1736 * WARNING: This function currently expects that any callbacks that have
1737 * been attached to the location monitor have been removed.
1739 * Return: Zero on success, -EINVAL when provided with an invalid location
1742 void vme_lm_free(struct vme_resource
*resource
)
1744 struct vme_lm_resource
*lm
;
1746 if (resource
->type
!= VME_LM
) {
1747 printk(KERN_ERR
"Not a Location Monitor resource\n");
1751 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1753 mutex_lock(&lm
->mtx
);
1756 * Check to see that there aren't any callbacks still attached, if
1757 * there are we should probably be detaching them!
1762 mutex_unlock(&lm
->mtx
);
1766 EXPORT_SYMBOL(vme_lm_free
);
1769 * vme_slot_num - Retrieve slot ID
1770 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1772 * Retrieve the slot ID associated with the provided VME device.
1774 * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
1775 * or the function is not supported. Hardware specific errors may also
1778 int vme_slot_num(struct vme_dev
*vdev
)
1780 struct vme_bridge
*bridge
;
1782 bridge
= vdev
->bridge
;
1783 if (bridge
== NULL
) {
1784 printk(KERN_ERR
"Can't find VME bus\n");
1788 if (bridge
->slot_get
== NULL
) {
1789 printk(KERN_WARNING
"vme_slot_num not supported\n");
1793 return bridge
->slot_get(bridge
);
1795 EXPORT_SYMBOL(vme_slot_num
);
1798 * vme_bus_num - Retrieve bus number
1799 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1801 * Retrieve the bus enumeration associated with the provided VME device.
1803 * Return: The bus number on success, -EINVAL if VME bridge cannot be
1806 int vme_bus_num(struct vme_dev
*vdev
)
1808 struct vme_bridge
*bridge
;
1810 bridge
= vdev
->bridge
;
1811 if (bridge
== NULL
) {
1812 pr_err("Can't find VME bus\n");
1818 EXPORT_SYMBOL(vme_bus_num
);
1820 /* - Bridge Registration --------------------------------------------------- */
1822 static void vme_dev_release(struct device
*dev
)
1824 kfree(dev_to_vme_dev(dev
));
1827 /* Common bridge initialization */
1828 struct vme_bridge
*vme_init_bridge(struct vme_bridge
*bridge
)
1830 INIT_LIST_HEAD(&bridge
->vme_error_handlers
);
1831 INIT_LIST_HEAD(&bridge
->master_resources
);
1832 INIT_LIST_HEAD(&bridge
->slave_resources
);
1833 INIT_LIST_HEAD(&bridge
->dma_resources
);
1834 INIT_LIST_HEAD(&bridge
->lm_resources
);
1835 mutex_init(&bridge
->irq_mtx
);
1839 EXPORT_SYMBOL(vme_init_bridge
);
1841 int vme_register_bridge(struct vme_bridge
*bridge
)
1846 mutex_lock(&vme_buses_lock
);
1847 for (i
= 0; i
< sizeof(vme_bus_numbers
) * 8; i
++) {
1848 if ((vme_bus_numbers
& (1 << i
)) == 0) {
1849 vme_bus_numbers
|= (1 << i
);
1851 INIT_LIST_HEAD(&bridge
->devices
);
1852 list_add_tail(&bridge
->bus_list
, &vme_bus_list
);
1857 mutex_unlock(&vme_buses_lock
);
1861 EXPORT_SYMBOL(vme_register_bridge
);
1863 void vme_unregister_bridge(struct vme_bridge
*bridge
)
1865 struct vme_dev
*vdev
;
1866 struct vme_dev
*tmp
;
1868 mutex_lock(&vme_buses_lock
);
1869 vme_bus_numbers
&= ~(1 << bridge
->num
);
1870 list_for_each_entry_safe(vdev
, tmp
, &bridge
->devices
, bridge_list
) {
1871 list_del(&vdev
->drv_list
);
1872 list_del(&vdev
->bridge_list
);
1873 device_unregister(&vdev
->dev
);
1875 list_del(&bridge
->bus_list
);
1876 mutex_unlock(&vme_buses_lock
);
1878 EXPORT_SYMBOL(vme_unregister_bridge
);
1880 /* - Driver Registration --------------------------------------------------- */
1882 static int __vme_register_driver_bus(struct vme_driver
*drv
,
1883 struct vme_bridge
*bridge
, unsigned int ndevs
)
1887 struct vme_dev
*vdev
;
1888 struct vme_dev
*tmp
;
1890 for (i
= 0; i
< ndevs
; i
++) {
1891 vdev
= kzalloc(sizeof(struct vme_dev
), GFP_KERNEL
);
1897 vdev
->bridge
= bridge
;
1898 vdev
->dev
.platform_data
= drv
;
1899 vdev
->dev
.release
= vme_dev_release
;
1900 vdev
->dev
.parent
= bridge
->parent
;
1901 vdev
->dev
.bus
= &vme_bus_type
;
1902 dev_set_name(&vdev
->dev
, "%s.%u-%u", drv
->name
, bridge
->num
,
1905 err
= device_register(&vdev
->dev
);
1909 if (vdev
->dev
.platform_data
) {
1910 list_add_tail(&vdev
->drv_list
, &drv
->devices
);
1911 list_add_tail(&vdev
->bridge_list
, &bridge
->devices
);
1913 device_unregister(&vdev
->dev
);
1918 put_device(&vdev
->dev
);
1921 list_for_each_entry_safe(vdev
, tmp
, &drv
->devices
, drv_list
) {
1922 list_del(&vdev
->drv_list
);
1923 list_del(&vdev
->bridge_list
);
1924 device_unregister(&vdev
->dev
);
1929 static int __vme_register_driver(struct vme_driver
*drv
, unsigned int ndevs
)
1931 struct vme_bridge
*bridge
;
1934 mutex_lock(&vme_buses_lock
);
1935 list_for_each_entry(bridge
, &vme_bus_list
, bus_list
) {
1937 * This cannot cause trouble as we already have vme_buses_lock
1938 * and if the bridge is removed, it will have to go through
1939 * vme_unregister_bridge() to do it (which calls remove() on
1940 * the bridge which in turn tries to acquire vme_buses_lock and
1941 * will have to wait).
1943 err
= __vme_register_driver_bus(drv
, bridge
, ndevs
);
1947 mutex_unlock(&vme_buses_lock
);
1952 * vme_register_driver - Register a VME driver
1953 * @drv: Pointer to VME driver structure to register.
1954 * @ndevs: Maximum number of devices to allow to be enumerated.
1956 * Register a VME device driver with the VME subsystem.
1958 * Return: Zero on success, error value on registration failure.
1960 int vme_register_driver(struct vme_driver
*drv
, unsigned int ndevs
)
1964 drv
->driver
.name
= drv
->name
;
1965 drv
->driver
.bus
= &vme_bus_type
;
1966 INIT_LIST_HEAD(&drv
->devices
);
1968 err
= driver_register(&drv
->driver
);
1972 err
= __vme_register_driver(drv
, ndevs
);
1974 driver_unregister(&drv
->driver
);
1978 EXPORT_SYMBOL(vme_register_driver
);
1981 * vme_unregister_driver - Unregister a VME driver
1982 * @drv: Pointer to VME driver structure to unregister.
1984 * Unregister a VME device driver from the VME subsystem.
1986 void vme_unregister_driver(struct vme_driver
*drv
)
1988 struct vme_dev
*dev
, *dev_tmp
;
1990 mutex_lock(&vme_buses_lock
);
1991 list_for_each_entry_safe(dev
, dev_tmp
, &drv
->devices
, drv_list
) {
1992 list_del(&dev
->drv_list
);
1993 list_del(&dev
->bridge_list
);
1994 device_unregister(&dev
->dev
);
1996 mutex_unlock(&vme_buses_lock
);
1998 driver_unregister(&drv
->driver
);
2000 EXPORT_SYMBOL(vme_unregister_driver
);
2002 /* - Bus Registration ------------------------------------------------------ */
2004 static int vme_bus_match(struct device
*dev
, struct device_driver
*drv
)
2006 struct vme_driver
*vme_drv
;
2008 vme_drv
= container_of(drv
, struct vme_driver
, driver
);
2010 if (dev
->platform_data
== vme_drv
) {
2011 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
2013 if (vme_drv
->match
&& vme_drv
->match(vdev
))
2016 dev
->platform_data
= NULL
;
2021 static int vme_bus_probe(struct device
*dev
)
2023 int retval
= -ENODEV
;
2024 struct vme_driver
*driver
;
2025 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
2027 driver
= dev
->platform_data
;
2029 if (driver
->probe
!= NULL
)
2030 retval
= driver
->probe(vdev
);
2035 static int vme_bus_remove(struct device
*dev
)
2037 int retval
= -ENODEV
;
2038 struct vme_driver
*driver
;
2039 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
2041 driver
= dev
->platform_data
;
2043 if (driver
->remove
!= NULL
)
2044 retval
= driver
->remove(vdev
);
2049 struct bus_type vme_bus_type
= {
2051 .match
= vme_bus_match
,
2052 .probe
= vme_bus_probe
,
2053 .remove
= vme_bus_remove
,
2055 EXPORT_SYMBOL(vme_bus_type
);
2057 static int __init
vme_init(void)
2059 return bus_register(&vme_bus_type
);
2061 subsys_initcall(vme_init
);