1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Author: Martyn Welch <martyn.welch@ge.com>
6 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
8 * Based on work by Tom Armistead and Ajit Prem
9 * Copyright 2004 Motorola Inc.
12 #include <linux/init.h>
13 #include <linux/export.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/pci.h>
19 #include <linux/poll.h>
20 #include <linux/highmem.h>
21 #include <linux/interrupt.h>
22 #include <linux/pagemap.h>
23 #include <linux/device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/syscalls.h>
26 #include <linux/mutex.h>
27 #include <linux/spinlock.h>
28 #include <linux/slab.h>
31 #include "vme_bridge.h"
33 /* Bitmask and list of registered buses both protected by common mutex */
34 static unsigned int vme_bus_numbers;
35 static LIST_HEAD(vme_bus_list);
36 static DEFINE_MUTEX(vme_buses_lock);
38 static int __init vme_init(void);
40 static struct vme_dev *dev_to_vme_dev(struct device *dev)
42 return container_of(dev, struct vme_dev, dev);
46 * Find the bridge that the resource is associated with.
48 static struct vme_bridge *find_bridge(struct vme_resource *resource)
50 /* Get list to search */
51 switch (resource->type) {
53 return list_entry(resource->entry, struct vme_master_resource,
56 return list_entry(resource->entry, struct vme_slave_resource,
59 return list_entry(resource->entry, struct vme_dma_resource,
62 return list_entry(resource->entry, struct vme_lm_resource,
70 * vme_alloc_consistent - Allocate contiguous memory.
71 * @resource: Pointer to VME resource.
72 * @size: Size of allocation required.
73 * @dma: Pointer to variable to store physical address of allocation.
75 * Allocate a contiguous block of memory for use by the driver. This is used to
76 * create the buffers for the slave windows.
78 * Return: Virtual address of allocation on success, NULL on failure.
80 void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
83 struct vme_bridge *bridge = find_bridge(resource);
85 if (!bridge->alloc_consistent) {
86 dev_err(bridge->parent,
87 "alloc_consistent not supported by bridge %s\n",
92 return bridge->alloc_consistent(bridge->parent, size, dma);
94 EXPORT_SYMBOL(vme_alloc_consistent);
97 * vme_free_consistent - Free previously allocated memory.
98 * @resource: Pointer to VME resource.
99 * @size: Size of allocation to free.
100 * @vaddr: Virtual address of allocation.
101 * @dma: Physical address of allocation.
103 * Free previously allocated block of contiguous memory.
105 void vme_free_consistent(struct vme_resource *resource, size_t size,
106 void *vaddr, dma_addr_t dma)
108 struct vme_bridge *bridge = find_bridge(resource);
110 if (!bridge->free_consistent) {
111 dev_err(bridge->parent,
112 "free_consistent not supported by bridge %s\n",
117 bridge->free_consistent(bridge->parent, size, vaddr, dma);
119 EXPORT_SYMBOL(vme_free_consistent);
122 * vme_get_size - Helper function returning size of a VME window
123 * @resource: Pointer to VME slave or master resource.
125 * Determine the size of the VME window provided. This is a helper
126 * function, wrappering the call to vme_master_get or vme_slave_get
127 * depending on the type of window resource handed to it.
129 * Return: Size of the window on success, zero on failure.
131 size_t vme_get_size(struct vme_resource *resource)
133 struct vme_bridge *bridge = find_bridge(resource);
135 unsigned long long base, size;
137 u32 aspace, cycle, dwidth;
139 switch (resource->type) {
141 retval = vme_master_get(resource, &enabled, &base, &size,
142 &aspace, &cycle, &dwidth);
148 retval = vme_slave_get(resource, &enabled, &base, &size,
149 &buf_base, &aspace, &cycle);
157 dev_err(bridge->parent, "Unknown resource type\n");
161 EXPORT_SYMBOL(vme_get_size);
163 int vme_check_window(struct vme_bridge *bridge, u32 aspace,
164 unsigned long long vme_base, unsigned long long size)
168 if (vme_base + size < size)
173 if (vme_base + size > VME_A16_MAX)
177 if (vme_base + size > VME_A24_MAX)
181 if (vme_base + size > VME_A32_MAX)
185 /* The VME_A64_MAX limit is actually U64_MAX + 1 */
188 if (vme_base + size > VME_CRCSR_MAX)
198 dev_err(bridge->parent, "Invalid address space\n");
205 EXPORT_SYMBOL(vme_check_window);
207 static u32 vme_get_aspace(int am)
241 * vme_slave_request - Request a VME slave window resource.
242 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
243 * @address: Required VME address space.
244 * @cycle: Required VME data transfer cycle type.
246 * Request use of a VME window resource capable of being set for the requested
247 * address space and data transfer cycle.
249 * Return: Pointer to VME resource on success, NULL on failure.
251 struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
254 struct vme_bridge *bridge;
255 struct vme_slave_resource *allocated_image = NULL;
256 struct vme_slave_resource *slave_image = NULL;
257 struct vme_resource *resource = NULL;
259 bridge = vdev->bridge;
261 dev_err(&vdev->dev, "Can't find VME bus\n");
265 /* Loop through slave resources */
266 list_for_each_entry(slave_image, &bridge->slave_resources, list) {
268 dev_err(bridge->parent,
269 "Registered NULL Slave resource\n");
273 /* Find an unlocked and compatible image */
274 mutex_lock(&slave_image->mtx);
275 if (((slave_image->address_attr & address) == address) &&
276 ((slave_image->cycle_attr & cycle) == cycle) &&
277 (slave_image->locked == 0)) {
278 slave_image->locked = 1;
279 mutex_unlock(&slave_image->mtx);
280 allocated_image = slave_image;
283 mutex_unlock(&slave_image->mtx);
287 if (!allocated_image)
290 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
294 resource->type = VME_SLAVE;
295 resource->entry = &allocated_image->list;
301 mutex_lock(&slave_image->mtx);
302 slave_image->locked = 0;
303 mutex_unlock(&slave_image->mtx);
308 EXPORT_SYMBOL(vme_slave_request);
311 * vme_slave_set - Set VME slave window configuration.
312 * @resource: Pointer to VME slave resource.
313 * @enabled: State to which the window should be configured.
314 * @vme_base: Base address for the window.
315 * @size: Size of the VME window.
316 * @buf_base: Based address of buffer used to provide VME slave window storage.
317 * @aspace: VME address space for the VME window.
318 * @cycle: VME data transfer cycle type for the VME window.
320 * Set configuration for provided VME slave window.
322 * Return: Zero on success, -EINVAL if operation is not supported on this
323 * device, if an invalid resource has been provided or invalid
324 * attributes are provided. Hardware specific errors may also be
327 int vme_slave_set(struct vme_resource *resource, int enabled,
328 unsigned long long vme_base, unsigned long long size,
329 dma_addr_t buf_base, u32 aspace, u32 cycle)
331 struct vme_bridge *bridge = find_bridge(resource);
332 struct vme_slave_resource *image;
335 if (resource->type != VME_SLAVE) {
336 dev_err(bridge->parent, "Not a slave resource\n");
340 image = list_entry(resource->entry, struct vme_slave_resource, list);
342 if (!bridge->slave_set) {
343 dev_err(bridge->parent, "%s not supported\n", __func__);
347 if (!(((image->address_attr & aspace) == aspace) &&
348 ((image->cycle_attr & cycle) == cycle))) {
349 dev_err(bridge->parent, "Invalid attributes\n");
353 retval = vme_check_window(bridge, aspace, vme_base, size);
357 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
360 EXPORT_SYMBOL(vme_slave_set);
363 * vme_slave_get - Retrieve VME slave window configuration.
364 * @resource: Pointer to VME slave resource.
365 * @enabled: Pointer to variable for storing state.
366 * @vme_base: Pointer to variable for storing window base address.
367 * @size: Pointer to variable for storing window size.
368 * @buf_base: Pointer to variable for storing slave buffer base address.
369 * @aspace: Pointer to variable for storing VME address space.
370 * @cycle: Pointer to variable for storing VME data transfer cycle type.
372 * Return configuration for provided VME slave window.
374 * Return: Zero on success, -EINVAL if operation is not supported on this
375 * device or if an invalid resource has been provided.
377 int vme_slave_get(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;
384 if (resource->type != VME_SLAVE) {
385 dev_err(bridge->parent, "Not a slave resource\n");
389 image = list_entry(resource->entry, struct vme_slave_resource, list);
391 if (!bridge->slave_get) {
392 dev_err(bridge->parent, "%s not supported\n", __func__);
396 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
399 EXPORT_SYMBOL(vme_slave_get);
402 * vme_slave_free - Free VME slave window
403 * @resource: Pointer to VME slave resource.
405 * Free the provided slave resource so that it may be reallocated.
407 void vme_slave_free(struct vme_resource *resource)
409 struct vme_bridge *bridge = find_bridge(resource);
410 struct vme_slave_resource *slave_image;
412 if (resource->type != VME_SLAVE) {
413 dev_err(bridge->parent, "Not a slave resource\n");
417 slave_image = list_entry(resource->entry, struct vme_slave_resource,
420 dev_err(bridge->parent, "Can't find slave resource\n");
425 mutex_lock(&slave_image->mtx);
426 if (slave_image->locked == 0)
427 dev_err(bridge->parent, "Image is already free\n");
429 slave_image->locked = 0;
430 mutex_unlock(&slave_image->mtx);
432 /* Free up resource memory */
435 EXPORT_SYMBOL(vme_slave_free);
438 * vme_master_request - Request a VME master window resource.
439 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
440 * @address: Required VME address space.
441 * @cycle: Required VME data transfer cycle type.
442 * @dwidth: Required VME data transfer width.
444 * Request use of a VME window resource capable of being set for the requested
445 * address space, data transfer cycle and width.
447 * Return: Pointer to VME resource on success, NULL on failure.
449 struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
450 u32 cycle, u32 dwidth)
452 struct vme_bridge *bridge;
453 struct vme_master_resource *allocated_image = NULL;
454 struct vme_master_resource *master_image = NULL;
455 struct vme_resource *resource = NULL;
457 bridge = vdev->bridge;
459 dev_err(&vdev->dev, "Can't find VME bus\n");
463 /* Loop through master resources */
464 list_for_each_entry(master_image, &bridge->master_resources, list) {
466 dev_warn(bridge->parent,
467 "Registered NULL master resource\n");
471 /* Find an unlocked and compatible image */
472 spin_lock(&master_image->lock);
473 if (((master_image->address_attr & address) == address) &&
474 ((master_image->cycle_attr & cycle) == cycle) &&
475 ((master_image->width_attr & dwidth) == dwidth) &&
476 (master_image->locked == 0)) {
477 master_image->locked = 1;
478 spin_unlock(&master_image->lock);
479 allocated_image = master_image;
482 spin_unlock(&master_image->lock);
485 /* Check to see if we found a resource */
486 if (!allocated_image) {
487 dev_err(&vdev->dev, "Can't find a suitable resource\n");
491 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
495 resource->type = VME_MASTER;
496 resource->entry = &allocated_image->list;
502 spin_lock(&master_image->lock);
503 master_image->locked = 0;
504 spin_unlock(&master_image->lock);
509 EXPORT_SYMBOL(vme_master_request);
512 * vme_master_set - Set VME master window configuration.
513 * @resource: Pointer to VME master resource.
514 * @enabled: State to which the window should be configured.
515 * @vme_base: Base address for the window.
516 * @size: Size of the VME window.
517 * @aspace: VME address space for the VME window.
518 * @cycle: VME data transfer cycle type for the VME window.
519 * @dwidth: VME data transfer width for the VME window.
521 * Set configuration for provided VME master window.
523 * Return: Zero on success, -EINVAL if operation is not supported on this
524 * device, if an invalid resource has been provided or invalid
525 * attributes are provided. Hardware specific errors may also be
528 int vme_master_set(struct vme_resource *resource, int enabled,
529 unsigned long long vme_base, unsigned long long size,
530 u32 aspace, u32 cycle, u32 dwidth)
532 struct vme_bridge *bridge = find_bridge(resource);
533 struct vme_master_resource *image;
536 if (resource->type != VME_MASTER) {
537 dev_err(bridge->parent, "Not a master resource\n");
541 image = list_entry(resource->entry, struct vme_master_resource, list);
543 if (!bridge->master_set) {
544 dev_warn(bridge->parent, "%s not supported\n", __func__);
548 if (!(((image->address_attr & aspace) == aspace) &&
549 ((image->cycle_attr & cycle) == cycle) &&
550 ((image->width_attr & dwidth) == dwidth))) {
551 dev_warn(bridge->parent, "Invalid attributes\n");
555 retval = vme_check_window(bridge, aspace, vme_base, size);
559 return bridge->master_set(image, enabled, vme_base, size, aspace,
562 EXPORT_SYMBOL(vme_master_set);
565 * vme_master_get - Retrieve VME master window configuration.
566 * @resource: Pointer to VME master resource.
567 * @enabled: Pointer to variable for storing state.
568 * @vme_base: Pointer to variable for storing window base address.
569 * @size: Pointer to variable for storing window size.
570 * @aspace: Pointer to variable for storing VME address space.
571 * @cycle: Pointer to variable for storing VME data transfer cycle type.
572 * @dwidth: Pointer to variable for storing VME data transfer width.
574 * Return configuration for provided VME master window.
576 * Return: Zero on success, -EINVAL if operation is not supported on this
577 * device or if an invalid resource has been provided.
579 int vme_master_get(struct vme_resource *resource, int *enabled,
580 unsigned long long *vme_base, unsigned long long *size,
581 u32 *aspace, u32 *cycle, u32 *dwidth)
583 struct vme_bridge *bridge = find_bridge(resource);
584 struct vme_master_resource *image;
586 if (resource->type != VME_MASTER) {
587 dev_err(bridge->parent, "Not a master resource\n");
591 image = list_entry(resource->entry, struct vme_master_resource, list);
593 if (!bridge->master_get) {
594 dev_warn(bridge->parent, "%s not supported\n", __func__);
598 return bridge->master_get(image, enabled, vme_base, size, aspace,
601 EXPORT_SYMBOL(vme_master_get);
604 * vme_master_read - Read data from VME space into a buffer.
605 * @resource: Pointer to VME master resource.
606 * @buf: Pointer to buffer where data should be transferred.
607 * @count: Number of bytes to transfer.
608 * @offset: Offset into VME master window at which to start transfer.
610 * Perform read of count bytes of data from location on VME bus which maps into
611 * the VME master window at offset to buf.
613 * Return: Number of bytes read, -EINVAL if resource is not a VME master
614 * resource or read operation is not supported. -EFAULT returned if
615 * invalid offset is provided. Hardware specific errors may also be
618 ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
621 struct vme_bridge *bridge = find_bridge(resource);
622 struct vme_master_resource *image;
625 if (!bridge->master_read) {
626 dev_warn(bridge->parent,
627 "Reading from resource not supported\n");
631 if (resource->type != VME_MASTER) {
632 dev_err(bridge->parent, "Not a master resource\n");
636 image = list_entry(resource->entry, struct vme_master_resource, list);
638 length = vme_get_size(resource);
640 if (offset > length) {
641 dev_warn(bridge->parent, "Invalid Offset\n");
645 if ((offset + count) > length)
646 count = length - offset;
648 return bridge->master_read(image, buf, count, offset);
650 EXPORT_SYMBOL(vme_master_read);
653 * vme_master_write - Write data out to VME space from a buffer.
654 * @resource: Pointer to VME master resource.
655 * @buf: Pointer to buffer holding data to transfer.
656 * @count: Number of bytes to transfer.
657 * @offset: Offset into VME master window at which to start transfer.
659 * Perform write of count bytes of data from buf to location on VME bus which
660 * maps into the VME master window at offset.
662 * Return: Number of bytes written, -EINVAL if resource is not a VME master
663 * resource or write operation is not supported. -EFAULT returned if
664 * invalid offset is provided. Hardware specific errors may also be
667 ssize_t vme_master_write(struct vme_resource *resource, void *buf,
668 size_t count, loff_t offset)
670 struct vme_bridge *bridge = find_bridge(resource);
671 struct vme_master_resource *image;
674 if (!bridge->master_write) {
675 dev_warn(bridge->parent, "Writing to resource not supported\n");
679 if (resource->type != VME_MASTER) {
680 dev_err(bridge->parent, "Not a master resource\n");
684 image = list_entry(resource->entry, struct vme_master_resource, list);
686 length = vme_get_size(resource);
688 if (offset > length) {
689 dev_warn(bridge->parent, "Invalid Offset\n");
693 if ((offset + count) > length)
694 count = length - offset;
696 return bridge->master_write(image, buf, count, offset);
698 EXPORT_SYMBOL(vme_master_write);
701 * vme_master_rmw - Perform read-modify-write cycle.
702 * @resource: Pointer to VME master resource.
703 * @mask: Bits to be compared and swapped in operation.
704 * @compare: Bits to be compared with data read from offset.
705 * @swap: Bits to be swapped in data read from offset.
706 * @offset: Offset into VME master window at which to perform operation.
708 * Perform read-modify-write cycle on provided location:
709 * - Location on VME bus is read.
710 * - Bits selected by mask are compared with compare.
711 * - Where a selected bit matches that in compare and are selected in swap,
712 * the bit is swapped.
713 * - Result written back to location on VME bus.
715 * Return: Bytes written on success, -EINVAL if resource is not a VME master
716 * resource or RMW operation is not supported. Hardware specific
717 * errors may also be returned.
719 unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
720 unsigned int compare, unsigned int swap, loff_t offset)
722 struct vme_bridge *bridge = find_bridge(resource);
723 struct vme_master_resource *image;
725 if (!bridge->master_rmw) {
726 dev_warn(bridge->parent, "Writing to resource not supported\n");
730 if (resource->type != VME_MASTER) {
731 dev_err(bridge->parent, "Not a master resource\n");
735 image = list_entry(resource->entry, struct vme_master_resource, list);
737 return bridge->master_rmw(image, mask, compare, swap, offset);
739 EXPORT_SYMBOL(vme_master_rmw);
742 * vme_master_mmap - Mmap region of VME master window.
743 * @resource: Pointer to VME master resource.
744 * @vma: Pointer to definition of user mapping.
746 * Memory map a region of the VME master window into user space.
748 * Return: Zero on success, -EINVAL if resource is not a VME master
749 * resource or -EFAULT if map exceeds window size. Other generic mmap
750 * errors may also be returned.
752 int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
754 struct vme_bridge *bridge = find_bridge(resource);
755 struct vme_master_resource *image;
756 phys_addr_t phys_addr;
757 unsigned long vma_size;
759 if (resource->type != VME_MASTER) {
760 dev_err(bridge->parent, "Not a master resource\n");
764 image = list_entry(resource->entry, struct vme_master_resource, list);
765 phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
766 vma_size = vma->vm_end - vma->vm_start;
768 if (phys_addr + vma_size > image->bus_resource.end + 1) {
769 dev_err(bridge->parent, "Map size cannot exceed the window size\n");
773 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
775 return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
777 EXPORT_SYMBOL(vme_master_mmap);
780 * vme_master_free - Free VME master window
781 * @resource: Pointer to VME master resource.
783 * Free the provided master resource so that it may be reallocated.
785 void vme_master_free(struct vme_resource *resource)
787 struct vme_bridge *bridge = find_bridge(resource);
788 struct vme_master_resource *master_image;
790 if (resource->type != VME_MASTER) {
791 dev_err(bridge->parent, "Not a master resource\n");
795 master_image = list_entry(resource->entry, struct vme_master_resource,
798 dev_err(bridge->parent, "Can't find master resource\n");
803 spin_lock(&master_image->lock);
804 if (master_image->locked == 0)
805 dev_err(bridge->parent, "Image is already free\n");
807 master_image->locked = 0;
808 spin_unlock(&master_image->lock);
810 /* Free up resource memory */
813 EXPORT_SYMBOL(vme_master_free);
816 * vme_dma_request - Request a DMA controller.
817 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
818 * @route: Required src/destination combination.
820 * Request a VME DMA controller with capability to perform transfers bewteen
821 * requested source/destination combination.
823 * Return: Pointer to VME DMA resource on success, NULL on failure.
825 struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
827 struct vme_bridge *bridge;
828 struct vme_dma_resource *allocated_ctrlr = NULL;
829 struct vme_dma_resource *dma_ctrlr = NULL;
830 struct vme_resource *resource = NULL;
832 /* XXX Not checking resource attributes */
833 dev_err(&vdev->dev, "No VME resource Attribute tests done\n");
835 bridge = vdev->bridge;
837 dev_err(&vdev->dev, "Can't find VME bus\n");
841 /* Loop through DMA resources */
842 list_for_each_entry(dma_ctrlr, &bridge->dma_resources, list) {
844 dev_err(bridge->parent,
845 "Registered NULL DMA resource\n");
849 /* Find an unlocked and compatible controller */
850 mutex_lock(&dma_ctrlr->mtx);
851 if (((dma_ctrlr->route_attr & route) == route) &&
852 (dma_ctrlr->locked == 0)) {
853 dma_ctrlr->locked = 1;
854 mutex_unlock(&dma_ctrlr->mtx);
855 allocated_ctrlr = dma_ctrlr;
858 mutex_unlock(&dma_ctrlr->mtx);
861 /* Check to see if we found a resource */
862 if (!allocated_ctrlr)
865 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
869 resource->type = VME_DMA;
870 resource->entry = &allocated_ctrlr->list;
876 mutex_lock(&dma_ctrlr->mtx);
877 dma_ctrlr->locked = 0;
878 mutex_unlock(&dma_ctrlr->mtx);
883 EXPORT_SYMBOL(vme_dma_request);
886 * vme_new_dma_list - Create new VME DMA list.
887 * @resource: Pointer to VME DMA resource.
889 * Create a new VME DMA list. It is the responsibility of the user to free
890 * the list once it is no longer required with vme_dma_list_free().
892 * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
895 struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
897 struct vme_bridge *bridge = find_bridge(resource);
898 struct vme_dma_list *dma_list;
900 if (resource->type != VME_DMA) {
901 dev_err(bridge->parent, "Not a DMA resource\n");
905 dma_list = kmalloc(sizeof(*dma_list), GFP_KERNEL);
909 INIT_LIST_HEAD(&dma_list->entries);
910 dma_list->parent = list_entry(resource->entry,
911 struct vme_dma_resource,
913 mutex_init(&dma_list->mtx);
917 EXPORT_SYMBOL(vme_new_dma_list);
920 * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
921 * @pattern: Value to use used as pattern
922 * @type: Type of pattern to be written.
924 * Create VME DMA list attribute for pattern generation. It is the
925 * responsibility of the user to free used attributes using
926 * vme_dma_free_attribute().
928 * Return: Pointer to VME DMA attribute, NULL on failure.
930 struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
932 struct vme_dma_attr *attributes;
933 struct vme_dma_pattern *pattern_attr;
935 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
939 pattern_attr = kmalloc(sizeof(*pattern_attr), GFP_KERNEL);
943 attributes->type = VME_DMA_PATTERN;
944 attributes->private = (void *)pattern_attr;
946 pattern_attr->pattern = pattern;
947 pattern_attr->type = type;
956 EXPORT_SYMBOL(vme_dma_pattern_attribute);
959 * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
960 * @address: PCI base address for DMA transfer.
962 * Create VME DMA list attribute pointing to a location on PCI for DMA
963 * transfers. It is the responsibility of the user to free used attributes
964 * using vme_dma_free_attribute().
966 * Return: Pointer to VME DMA attribute, NULL on failure.
968 struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
970 struct vme_dma_attr *attributes;
971 struct vme_dma_pci *pci_attr;
973 /* XXX Run some sanity checks here */
975 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
979 pci_attr = kmalloc(sizeof(*pci_attr), GFP_KERNEL);
983 attributes->type = VME_DMA_PCI;
984 attributes->private = (void *)pci_attr;
986 pci_attr->address = address;
995 EXPORT_SYMBOL(vme_dma_pci_attribute);
998 * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
999 * @address: VME base address for DMA transfer.
1000 * @aspace: VME address space to use for DMA transfer.
1001 * @cycle: VME bus cycle to use for DMA transfer.
1002 * @dwidth: VME data width to use for DMA transfer.
1004 * Create VME DMA list attribute pointing to a location on the VME bus for DMA
1005 * transfers. It is the responsibility of the user to free used attributes
1006 * using vme_dma_free_attribute().
1008 * Return: Pointer to VME DMA attribute, NULL on failure.
1010 struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
1011 u32 aspace, u32 cycle, u32 dwidth)
1013 struct vme_dma_attr *attributes;
1014 struct vme_dma_vme *vme_attr;
1016 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
1020 vme_attr = kmalloc(sizeof(*vme_attr), GFP_KERNEL);
1024 attributes->type = VME_DMA_VME;
1025 attributes->private = (void *)vme_attr;
1027 vme_attr->address = address;
1028 vme_attr->aspace = aspace;
1029 vme_attr->cycle = cycle;
1030 vme_attr->dwidth = dwidth;
1039 EXPORT_SYMBOL(vme_dma_vme_attribute);
1042 * vme_dma_free_attribute - Free DMA list attribute.
1043 * @attributes: Pointer to DMA list attribute.
1045 * Free VME DMA list attribute. VME DMA list attributes can be safely freed
1046 * once vme_dma_list_add() has returned.
1048 void vme_dma_free_attribute(struct vme_dma_attr *attributes)
1050 kfree(attributes->private);
1053 EXPORT_SYMBOL(vme_dma_free_attribute);
1056 * vme_dma_list_add - Add enty to a VME DMA list.
1057 * @list: Pointer to VME list.
1058 * @src: Pointer to DMA list attribute to use as source.
1059 * @dest: Pointer to DMA list attribute to use as destination.
1060 * @count: Number of bytes to transfer.
1062 * Add an entry to the provided VME DMA list. Entry requires pointers to source
1063 * and destination DMA attributes and a count.
1065 * Please note, the attributes supported as source and destinations for
1066 * transfers are hardware dependent.
1068 * Return: Zero on success, -EINVAL if operation is not supported on this
1069 * device or if the link list has already been submitted for execution.
1070 * Hardware specific errors also possible.
1072 int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
1073 struct vme_dma_attr *dest, size_t count)
1075 struct vme_bridge *bridge = list->parent->parent;
1078 if (!bridge->dma_list_add) {
1079 dev_warn(bridge->parent,
1080 "Link List DMA generation not supported\n");
1084 if (!mutex_trylock(&list->mtx)) {
1085 dev_err(bridge->parent, "Link List already submitted\n");
1089 retval = bridge->dma_list_add(list, src, dest, count);
1091 mutex_unlock(&list->mtx);
1095 EXPORT_SYMBOL(vme_dma_list_add);
1098 * vme_dma_list_exec - Queue a VME DMA list for execution.
1099 * @list: Pointer to VME list.
1101 * Queue the provided VME DMA list for execution. The call will return once the
1102 * list has been executed.
1104 * Return: Zero on success, -EINVAL if operation is not supported on this
1105 * device. Hardware specific errors also possible.
1107 int vme_dma_list_exec(struct vme_dma_list *list)
1109 struct vme_bridge *bridge = list->parent->parent;
1112 if (!bridge->dma_list_exec) {
1113 dev_err(bridge->parent,
1114 "Link List DMA execution not supported\n");
1118 mutex_lock(&list->mtx);
1120 retval = bridge->dma_list_exec(list);
1122 mutex_unlock(&list->mtx);
1126 EXPORT_SYMBOL(vme_dma_list_exec);
1129 * vme_dma_list_free - Free a VME DMA list.
1130 * @list: Pointer to VME list.
1132 * Free the provided DMA list and all its entries.
1134 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1135 * is still in use. Hardware specific errors also possible.
1137 int vme_dma_list_free(struct vme_dma_list *list)
1139 struct vme_bridge *bridge = list->parent->parent;
1142 if (!bridge->dma_list_empty) {
1143 dev_warn(bridge->parent,
1144 "Emptying of Link Lists not supported\n");
1148 if (!mutex_trylock(&list->mtx)) {
1149 dev_err(bridge->parent, "Link List in use\n");
1154 * Empty out all of the entries from the DMA list. We need to go to the
1155 * low level driver as DMA entries are driver specific.
1157 retval = bridge->dma_list_empty(list);
1159 dev_err(bridge->parent, "Unable to empty link-list entries\n");
1160 mutex_unlock(&list->mtx);
1163 mutex_unlock(&list->mtx);
1168 EXPORT_SYMBOL(vme_dma_list_free);
1171 * vme_dma_free - Free a VME DMA resource.
1172 * @resource: Pointer to VME DMA resource.
1174 * Free the provided DMA resource so that it may be reallocated.
1176 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1179 int vme_dma_free(struct vme_resource *resource)
1181 struct vme_bridge *bridge = find_bridge(resource);
1182 struct vme_dma_resource *ctrlr;
1184 if (resource->type != VME_DMA) {
1185 dev_err(bridge->parent, "Not a DMA resource\n");
1189 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1191 if (!mutex_trylock(&ctrlr->mtx)) {
1192 dev_err(bridge->parent, "Resource busy, can't free\n");
1196 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
1197 dev_warn(bridge->parent,
1198 "Resource still processing transfers\n");
1199 mutex_unlock(&ctrlr->mtx);
1205 mutex_unlock(&ctrlr->mtx);
1211 EXPORT_SYMBOL(vme_dma_free);
1213 void vme_bus_error_handler(struct vme_bridge *bridge,
1214 unsigned long long address, int am)
1216 struct vme_error_handler *handler;
1217 int handler_triggered = 0;
1218 u32 aspace = vme_get_aspace(am);
1220 list_for_each_entry(handler, &bridge->vme_error_handlers, list) {
1221 if ((aspace == handler->aspace) &&
1222 (address >= handler->start) &&
1223 (address < handler->end)) {
1224 if (!handler->num_errors)
1225 handler->first_error = address;
1226 if (handler->num_errors != UINT_MAX)
1227 handler->num_errors++;
1228 handler_triggered = 1;
1232 if (!handler_triggered)
1233 dev_err(bridge->parent,
1234 "Unhandled VME access error at address 0x%llx\n",
1237 EXPORT_SYMBOL(vme_bus_error_handler);
1239 struct vme_error_handler *vme_register_error_handler(struct vme_bridge *bridge, u32 aspace,
1240 unsigned long long address, size_t len)
1242 struct vme_error_handler *handler;
1244 handler = kmalloc(sizeof(*handler), GFP_ATOMIC);
1248 handler->aspace = aspace;
1249 handler->start = address;
1250 handler->end = address + len;
1251 handler->num_errors = 0;
1252 handler->first_error = 0;
1253 list_add_tail(&handler->list, &bridge->vme_error_handlers);
1257 EXPORT_SYMBOL(vme_register_error_handler);
1259 void vme_unregister_error_handler(struct vme_error_handler *handler)
1261 list_del(&handler->list);
1264 EXPORT_SYMBOL(vme_unregister_error_handler);
1266 void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1268 void (*call)(int, int, void *);
1271 call = bridge->irq[level - 1].callback[statid].func;
1272 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
1274 call(level, statid, priv_data);
1276 dev_warn(bridge->parent,
1277 "Spurious VME interrupt, level:%x, vector:%x\n", level,
1280 EXPORT_SYMBOL(vme_irq_handler);
1283 * vme_irq_request - Request a specific VME interrupt.
1284 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1285 * @level: Interrupt priority being requested.
1286 * @statid: Interrupt vector being requested.
1287 * @callback: Pointer to callback function called when VME interrupt/vector
1289 * @priv_data: Generic pointer that will be passed to the callback function.
1291 * Request callback to be attached as a handler for VME interrupts with provided
1294 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1295 * function is not supported, -EBUSY if the level/statid combination is
1296 * already in use. Hardware specific errors also possible.
1298 int vme_irq_request(struct vme_dev *vdev, int level, int statid,
1299 void (*callback)(int, int, void *),
1302 struct vme_bridge *bridge;
1304 bridge = vdev->bridge;
1306 dev_err(&vdev->dev, "Can't find VME bus\n");
1310 if ((level < 1) || (level > 7)) {
1311 dev_err(bridge->parent, "Invalid interrupt level\n");
1315 if (!bridge->irq_set) {
1316 dev_err(bridge->parent,
1317 "Configuring interrupts not supported\n");
1321 mutex_lock(&bridge->irq_mtx);
1323 if (bridge->irq[level - 1].callback[statid].func) {
1324 mutex_unlock(&bridge->irq_mtx);
1325 dev_warn(bridge->parent, "VME Interrupt already taken\n");
1329 bridge->irq[level - 1].count++;
1330 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1331 bridge->irq[level - 1].callback[statid].func = callback;
1333 /* Enable IRQ level */
1334 bridge->irq_set(bridge, level, 1, 1);
1336 mutex_unlock(&bridge->irq_mtx);
1340 EXPORT_SYMBOL(vme_irq_request);
1343 * vme_irq_free - Free a VME interrupt.
1344 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1345 * @level: Interrupt priority of interrupt being freed.
1346 * @statid: Interrupt vector of interrupt being freed.
1348 * Remove previously attached callback from VME interrupt priority/vector.
1350 void vme_irq_free(struct vme_dev *vdev, int level, int statid)
1352 struct vme_bridge *bridge;
1354 bridge = vdev->bridge;
1356 dev_err(&vdev->dev, "Can't find VME bus\n");
1360 if ((level < 1) || (level > 7)) {
1361 dev_err(bridge->parent, "Invalid interrupt level\n");
1365 if (!bridge->irq_set) {
1366 dev_err(bridge->parent,
1367 "Configuring interrupts not supported\n");
1371 mutex_lock(&bridge->irq_mtx);
1373 bridge->irq[level - 1].count--;
1375 /* Disable IRQ level if no more interrupts attached at this level*/
1376 if (bridge->irq[level - 1].count == 0)
1377 bridge->irq_set(bridge, level, 0, 1);
1379 bridge->irq[level - 1].callback[statid].func = NULL;
1380 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1382 mutex_unlock(&bridge->irq_mtx);
1384 EXPORT_SYMBOL(vme_irq_free);
1387 * vme_irq_generate - Generate VME interrupt.
1388 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1389 * @level: Interrupt priority at which to assert the interrupt.
1390 * @statid: Interrupt vector to associate with the interrupt.
1392 * Generate a VME interrupt of the provided level and with the provided
1395 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1396 * function is not supported. Hardware specific errors also possible.
1398 int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
1400 struct vme_bridge *bridge;
1402 bridge = vdev->bridge;
1404 dev_err(&vdev->dev, "Can't find VME bus\n");
1408 if ((level < 1) || (level > 7)) {
1409 dev_warn(bridge->parent, "Invalid interrupt level\n");
1413 if (!bridge->irq_generate) {
1414 dev_warn(bridge->parent,
1415 "Interrupt generation not supported\n");
1419 return bridge->irq_generate(bridge, level, statid);
1421 EXPORT_SYMBOL(vme_irq_generate);
1424 * vme_lm_request - Request a VME location monitor
1425 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1427 * Allocate a location monitor resource to the driver. A location monitor
1428 * allows the driver to monitor accesses to a contiguous number of
1429 * addresses on the VME bus.
1431 * Return: Pointer to a VME resource on success or NULL on failure.
1433 struct vme_resource *vme_lm_request(struct vme_dev *vdev)
1435 struct vme_bridge *bridge;
1436 struct vme_lm_resource *allocated_lm = NULL;
1437 struct vme_lm_resource *lm = NULL;
1438 struct vme_resource *resource = NULL;
1440 bridge = vdev->bridge;
1442 dev_err(&vdev->dev, "Can't find VME bus\n");
1446 /* Loop through LM resources */
1447 list_for_each_entry(lm, &bridge->lm_resources, list) {
1449 dev_err(bridge->parent,
1450 "Registered NULL Location Monitor resource\n");
1454 /* Find an unlocked controller */
1455 mutex_lock(&lm->mtx);
1456 if (lm->locked == 0) {
1458 mutex_unlock(&lm->mtx);
1462 mutex_unlock(&lm->mtx);
1465 /* Check to see if we found a resource */
1469 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
1473 resource->type = VME_LM;
1474 resource->entry = &allocated_lm->list;
1480 mutex_lock(&lm->mtx);
1482 mutex_unlock(&lm->mtx);
1487 EXPORT_SYMBOL(vme_lm_request);
1490 * vme_lm_count - Determine number of VME Addresses monitored
1491 * @resource: Pointer to VME location monitor resource.
1493 * The number of contiguous addresses monitored is hardware dependent.
1494 * Return the number of contiguous addresses monitored by the
1497 * Return: Count of addresses monitored or -EINVAL when provided with an
1498 * invalid location monitor resource.
1500 int vme_lm_count(struct vme_resource *resource)
1502 struct vme_bridge *bridge = find_bridge(resource);
1503 struct vme_lm_resource *lm;
1505 if (resource->type != VME_LM) {
1506 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1510 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1512 return lm->monitors;
1514 EXPORT_SYMBOL(vme_lm_count);
1517 * vme_lm_set - Configure location monitor
1518 * @resource: Pointer to VME location monitor resource.
1519 * @lm_base: Base address to monitor.
1520 * @aspace: VME address space to monitor.
1521 * @cycle: VME bus cycle type to monitor.
1523 * Set the base address, address space and cycle type of accesses to be
1524 * monitored by the location monitor.
1526 * Return: Zero on success, -EINVAL when provided with an invalid location
1527 * monitor resource or function is not supported. Hardware specific
1528 * errors may also be returned.
1530 int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
1531 u32 aspace, u32 cycle)
1533 struct vme_bridge *bridge = find_bridge(resource);
1534 struct vme_lm_resource *lm;
1536 if (resource->type != VME_LM) {
1537 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1541 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1543 if (!bridge->lm_set) {
1544 dev_err(bridge->parent, "%s not supported\n", __func__);
1548 return bridge->lm_set(lm, lm_base, aspace, cycle);
1550 EXPORT_SYMBOL(vme_lm_set);
1553 * vme_lm_get - Retrieve location monitor settings
1554 * @resource: Pointer to VME location monitor resource.
1555 * @lm_base: Pointer used to output the base address monitored.
1556 * @aspace: Pointer used to output the address space monitored.
1557 * @cycle: Pointer used to output the VME bus cycle type monitored.
1559 * Retrieve the base address, address space and cycle type of accesses to
1560 * be monitored by the location monitor.
1562 * Return: Zero on success, -EINVAL when provided with an invalid location
1563 * monitor resource or function is not supported. Hardware specific
1564 * errors may also be returned.
1566 int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
1567 u32 *aspace, u32 *cycle)
1569 struct vme_bridge *bridge = find_bridge(resource);
1570 struct vme_lm_resource *lm;
1572 if (resource->type != VME_LM) {
1573 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1577 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1579 if (!bridge->lm_get) {
1580 dev_err(bridge->parent, "%s not supported\n", __func__);
1584 return bridge->lm_get(lm, lm_base, aspace, cycle);
1586 EXPORT_SYMBOL(vme_lm_get);
1589 * vme_lm_attach - Provide callback for location monitor address
1590 * @resource: Pointer to VME location monitor resource.
1591 * @monitor: Offset to which callback should be attached.
1592 * @callback: Pointer to callback function called when triggered.
1593 * @data: Generic pointer that will be passed to the callback function.
1595 * Attach a callback to the specified offset into the location monitors
1596 * monitored addresses. A generic pointer is provided to allow data to be
1597 * passed to the callback when called.
1599 * Return: Zero on success, -EINVAL when provided with an invalid location
1600 * monitor resource or function is not supported. Hardware specific
1601 * errors may also be returned.
1603 int vme_lm_attach(struct vme_resource *resource, int monitor,
1604 void (*callback)(void *), void *data)
1606 struct vme_bridge *bridge = find_bridge(resource);
1607 struct vme_lm_resource *lm;
1609 if (resource->type != VME_LM) {
1610 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1614 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1616 if (!bridge->lm_attach) {
1617 dev_err(bridge->parent, "%s not supported\n", __func__);
1621 return bridge->lm_attach(lm, monitor, callback, data);
1623 EXPORT_SYMBOL(vme_lm_attach);
1626 * vme_lm_detach - Remove callback for location monitor address
1627 * @resource: Pointer to VME location monitor resource.
1628 * @monitor: Offset to which callback should be removed.
1630 * Remove the callback associated with the specified offset into the
1631 * location monitors monitored addresses.
1633 * Return: Zero on success, -EINVAL when provided with an invalid location
1634 * monitor resource or function is not supported. Hardware specific
1635 * errors may also be returned.
1637 int vme_lm_detach(struct vme_resource *resource, int monitor)
1639 struct vme_bridge *bridge = find_bridge(resource);
1640 struct vme_lm_resource *lm;
1642 if (resource->type != VME_LM) {
1643 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1647 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1649 if (!bridge->lm_detach) {
1650 dev_err(bridge->parent, "%s not supported\n", __func__);
1654 return bridge->lm_detach(lm, monitor);
1656 EXPORT_SYMBOL(vme_lm_detach);
1659 * vme_lm_free - Free allocated VME location monitor
1660 * @resource: Pointer to VME location monitor resource.
1662 * Free allocation of a VME location monitor.
1664 * WARNING: This function currently expects that any callbacks that have
1665 * been attached to the location monitor have been removed.
1667 * Return: Zero on success, -EINVAL when provided with an invalid location
1670 void vme_lm_free(struct vme_resource *resource)
1672 struct vme_bridge *bridge = find_bridge(resource);
1673 struct vme_lm_resource *lm;
1675 if (resource->type != VME_LM) {
1676 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1680 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1682 mutex_lock(&lm->mtx);
1685 * Check to see that there aren't any callbacks still attached, if
1686 * there are we should probably be detaching them!
1691 mutex_unlock(&lm->mtx);
1695 EXPORT_SYMBOL(vme_lm_free);
1698 * vme_slot_num - Retrieve slot ID
1699 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1701 * Retrieve the slot ID associated with the provided VME device.
1703 * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
1704 * or the function is not supported. Hardware specific errors may also
1707 int vme_slot_num(struct vme_dev *vdev)
1709 struct vme_bridge *bridge;
1711 bridge = vdev->bridge;
1713 dev_err(&vdev->dev, "Can't find VME bus\n");
1717 if (!bridge->slot_get) {
1718 dev_warn(bridge->parent, "%s not supported\n", __func__);
1722 return bridge->slot_get(bridge);
1724 EXPORT_SYMBOL(vme_slot_num);
1727 * vme_bus_num - Retrieve bus number
1728 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1730 * Retrieve the bus enumeration associated with the provided VME device.
1732 * Return: The bus number on success, -EINVAL if VME bridge cannot be
1735 int vme_bus_num(struct vme_dev *vdev)
1737 struct vme_bridge *bridge;
1739 bridge = vdev->bridge;
1741 dev_err(&vdev->dev, "Can't find VME bus\n");
1747 EXPORT_SYMBOL(vme_bus_num);
1749 /* - Bridge Registration --------------------------------------------------- */
1751 static void vme_dev_release(struct device *dev)
1753 kfree(dev_to_vme_dev(dev));
1756 /* Common bridge initialization */
1757 struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
1759 INIT_LIST_HEAD(&bridge->vme_error_handlers);
1760 INIT_LIST_HEAD(&bridge->master_resources);
1761 INIT_LIST_HEAD(&bridge->slave_resources);
1762 INIT_LIST_HEAD(&bridge->dma_resources);
1763 INIT_LIST_HEAD(&bridge->lm_resources);
1764 mutex_init(&bridge->irq_mtx);
1768 EXPORT_SYMBOL(vme_init_bridge);
1770 int vme_register_bridge(struct vme_bridge *bridge)
1775 mutex_lock(&vme_buses_lock);
1776 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
1777 if ((vme_bus_numbers & (1 << i)) == 0) {
1778 vme_bus_numbers |= (1 << i);
1780 INIT_LIST_HEAD(&bridge->devices);
1781 list_add_tail(&bridge->bus_list, &vme_bus_list);
1786 mutex_unlock(&vme_buses_lock);
1790 EXPORT_SYMBOL(vme_register_bridge);
1792 void vme_unregister_bridge(struct vme_bridge *bridge)
1794 struct vme_dev *vdev;
1795 struct vme_dev *tmp;
1797 mutex_lock(&vme_buses_lock);
1798 vme_bus_numbers &= ~(1 << bridge->num);
1799 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1800 list_del(&vdev->drv_list);
1801 list_del(&vdev->bridge_list);
1802 device_unregister(&vdev->dev);
1804 list_del(&bridge->bus_list);
1805 mutex_unlock(&vme_buses_lock);
1807 EXPORT_SYMBOL(vme_unregister_bridge);
1809 /* - Driver Registration --------------------------------------------------- */
1811 static int __vme_register_driver_bus(struct vme_driver *drv,
1812 struct vme_bridge *bridge,
1817 struct vme_dev *vdev;
1818 struct vme_dev *tmp;
1820 for (i = 0; i < ndevs; i++) {
1821 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1827 vdev->bridge = bridge;
1828 vdev->dev.platform_data = drv;
1829 vdev->dev.release = vme_dev_release;
1830 vdev->dev.parent = bridge->parent;
1831 vdev->dev.bus = &vme_bus_type;
1832 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1835 err = device_register(&vdev->dev);
1839 if (vdev->dev.platform_data) {
1840 list_add_tail(&vdev->drv_list, &drv->devices);
1841 list_add_tail(&vdev->bridge_list, &bridge->devices);
1843 device_unregister(&vdev->dev);
1849 put_device(&vdev->dev);
1851 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1852 list_del(&vdev->drv_list);
1853 list_del(&vdev->bridge_list);
1854 device_unregister(&vdev->dev);
1859 static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1861 struct vme_bridge *bridge;
1864 mutex_lock(&vme_buses_lock);
1865 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1867 * This cannot cause trouble as we already have vme_buses_lock
1868 * and if the bridge is removed, it will have to go through
1869 * vme_unregister_bridge() to do it (which calls remove() on
1870 * the bridge which in turn tries to acquire vme_buses_lock and
1871 * will have to wait).
1873 err = __vme_register_driver_bus(drv, bridge, ndevs);
1877 mutex_unlock(&vme_buses_lock);
1882 * vme_register_driver - Register a VME driver
1883 * @drv: Pointer to VME driver structure to register.
1884 * @ndevs: Maximum number of devices to allow to be enumerated.
1886 * Register a VME device driver with the VME subsystem.
1888 * Return: Zero on success, error value on registration failure.
1890 int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1894 drv->driver.name = drv->name;
1895 drv->driver.bus = &vme_bus_type;
1896 INIT_LIST_HEAD(&drv->devices);
1898 err = driver_register(&drv->driver);
1902 err = __vme_register_driver(drv, ndevs);
1904 driver_unregister(&drv->driver);
1908 EXPORT_SYMBOL(vme_register_driver);
1911 * vme_unregister_driver - Unregister a VME driver
1912 * @drv: Pointer to VME driver structure to unregister.
1914 * Unregister a VME device driver from the VME subsystem.
1916 void vme_unregister_driver(struct vme_driver *drv)
1918 struct vme_dev *dev, *dev_tmp;
1920 mutex_lock(&vme_buses_lock);
1921 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1922 list_del(&dev->drv_list);
1923 list_del(&dev->bridge_list);
1924 device_unregister(&dev->dev);
1926 mutex_unlock(&vme_buses_lock);
1928 driver_unregister(&drv->driver);
1930 EXPORT_SYMBOL(vme_unregister_driver);
1932 /* - Bus Registration ------------------------------------------------------ */
1934 static int vme_bus_match(struct device *dev, struct device_driver *drv)
1936 struct vme_driver *vme_drv;
1938 vme_drv = container_of(drv, struct vme_driver, driver);
1940 if (dev->platform_data == vme_drv) {
1941 struct vme_dev *vdev = dev_to_vme_dev(dev);
1943 if (vme_drv->match && vme_drv->match(vdev))
1946 dev->platform_data = NULL;
1951 static int vme_bus_probe(struct device *dev)
1953 struct vme_driver *driver;
1954 struct vme_dev *vdev = dev_to_vme_dev(dev);
1956 driver = dev->platform_data;
1958 return driver->probe(vdev);
1963 static void vme_bus_remove(struct device *dev)
1965 struct vme_driver *driver;
1966 struct vme_dev *vdev = dev_to_vme_dev(dev);
1968 driver = dev->platform_data;
1970 driver->remove(vdev);
1973 const struct bus_type vme_bus_type = {
1975 .match = vme_bus_match,
1976 .probe = vme_bus_probe,
1977 .remove = vme_bus_remove,
1979 EXPORT_SYMBOL(vme_bus_type);
1981 static int __init vme_init(void)
1983 return bus_register(&vme_bus_type);
1985 subsys_initcall(vme_init);