vme.c 34.4 KB
Newer Older
1 2 3
/*
 * VME Bridge Framework
 *
4 5
 * Author: Martyn Welch <martyn.welch@ge.com>
 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Based on work by Tom Armistead and Ajit Prem
 * Copyright 2004 Motorola Inc.
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/mm.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/pci.h>
#include <linux/poll.h>
#include <linux/highmem.h>
#include <linux/interrupt.h>
#include <linux/pagemap.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/syscalls.h>
30
#include <linux/mutex.h>
31
#include <linux/spinlock.h>
32
#include <linux/slab.h>
33
#include <linux/vme.h>
34 35 36

#include "vme_bridge.h"

37
/* Bitmask and list of registered buses both protected by common mutex */
38
static unsigned int vme_bus_numbers;
39 40
static LIST_HEAD(vme_bus_list);
static DEFINE_MUTEX(vme_buses_lock);
41

42 43
static void __exit vme_exit(void);
static int __init vme_init(void);
44

45
static struct vme_dev *dev_to_vme_dev(struct device *dev)
46
{
47
	return container_of(dev, struct vme_dev, dev);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
}

/*
 * Find the bridge that the resource is associated with.
 */
static struct vme_bridge *find_bridge(struct vme_resource *resource)
{
	/* Get list to search */
	switch (resource->type) {
	case VME_MASTER:
		return list_entry(resource->entry, struct vme_master_resource,
			list)->parent;
		break;
	case VME_SLAVE:
		return list_entry(resource->entry, struct vme_slave_resource,
			list)->parent;
		break;
	case VME_DMA:
		return list_entry(resource->entry, struct vme_dma_resource,
			list)->parent;
		break;
69 70 71 72
	case VME_LM:
		return list_entry(resource->entry, struct vme_lm_resource,
			list)->parent;
		break;
73 74 75 76 77 78 79 80 81 82 83
	default:
		printk(KERN_ERR "Unknown resource type\n");
		return NULL;
		break;
	}
}

/*
 * Allocate a contiguous block of memory for use by the driver. This is used to
 * create the buffers for the slave windows.
 */
84
void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
85 86 87 88
	dma_addr_t *dma)
{
	struct vme_bridge *bridge;

89 90
	if (resource == NULL) {
		printk(KERN_ERR "No resource\n");
91 92 93 94
		return NULL;
	}

	bridge = find_bridge(resource);
95 96
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find bridge\n");
97 98 99 100
		return NULL;
	}

	if (bridge->parent == NULL) {
101
		printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
102 103 104 105
		return NULL;
	}

	if (bridge->alloc_consistent == NULL) {
106 107
		printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
		       bridge->name);
108 109 110
		return NULL;
	}

111
	return bridge->alloc_consistent(bridge->parent, size, dma);
112 113 114 115 116 117 118 119 120 121 122
}
EXPORT_SYMBOL(vme_alloc_consistent);

/*
 * Free previously allocated contiguous block of memory.
 */
void vme_free_consistent(struct vme_resource *resource, size_t size,
	void *vaddr, dma_addr_t dma)
{
	struct vme_bridge *bridge;

123 124
	if (resource == NULL) {
		printk(KERN_ERR "No resource\n");
125 126 127 128
		return;
	}

	bridge = find_bridge(resource);
129 130
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find bridge\n");
131 132 133
		return;
	}

134
	if (bridge->parent == NULL) {
135
		printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
136 137 138 139
		return;
	}

	if (bridge->free_consistent == NULL) {
140 141
		printk(KERN_ERR "free_consistent not supported by bridge %s\n",
		       bridge->name);
142 143
		return;
	}
144

145
	bridge->free_consistent(bridge->parent, size, vaddr, dma);
146 147 148 149 150 151 152 153
}
EXPORT_SYMBOL(vme_free_consistent);

size_t vme_get_size(struct vme_resource *resource)
{
	int enabled, retval;
	unsigned long long base, size;
	dma_addr_t buf_base;
M
Martyn Welch 已提交
154
	u32 aspace, cycle, dwidth;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

	switch (resource->type) {
	case VME_MASTER:
		retval = vme_master_get(resource, &enabled, &base, &size,
			&aspace, &cycle, &dwidth);

		return size;
		break;
	case VME_SLAVE:
		retval = vme_slave_get(resource, &enabled, &base, &size,
			&buf_base, &aspace, &cycle);

		return size;
		break;
	case VME_DMA:
		return 0;
		break;
	default:
		printk(KERN_ERR "Unknown resource type\n");
		return 0;
		break;
	}
}
EXPORT_SYMBOL(vme_get_size);

M
Martyn Welch 已提交
180
static int vme_check_window(u32 aspace, unsigned long long vme_base,
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	unsigned long long size)
{
	int retval = 0;

	switch (aspace) {
	case VME_A16:
		if (((vme_base + size) > VME_A16_MAX) ||
				(vme_base > VME_A16_MAX))
			retval = -EFAULT;
		break;
	case VME_A24:
		if (((vme_base + size) > VME_A24_MAX) ||
				(vme_base > VME_A24_MAX))
			retval = -EFAULT;
		break;
	case VME_A32:
		if (((vme_base + size) > VME_A32_MAX) ||
				(vme_base > VME_A32_MAX))
			retval = -EFAULT;
		break;
	case VME_A64:
		/*
		 * Any value held in an unsigned long long can be used as the
		 * base
		 */
		break;
	case VME_CRCSR:
		if (((vme_base + size) > VME_CRCSR_MAX) ||
				(vme_base > VME_CRCSR_MAX))
			retval = -EFAULT;
		break;
	case VME_USER1:
	case VME_USER2:
	case VME_USER3:
	case VME_USER4:
		/* User Defined */
		break;
	default:
219
		printk(KERN_ERR "Invalid address space\n");
220 221 222 223 224 225 226 227 228 229 230
		retval = -EINVAL;
		break;
	}

	return retval;
}

/*
 * Request a slave image with specific attributes, return some unique
 * identifier.
 */
M
Martyn Welch 已提交
231 232
struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
	u32 cycle)
233 234 235 236 237 238 239
{
	struct vme_bridge *bridge;
	struct list_head *slave_pos = NULL;
	struct vme_slave_resource *allocated_image = NULL;
	struct vme_slave_resource *slave_image = NULL;
	struct vme_resource *resource = NULL;

240
	bridge = vdev->bridge;
241 242 243 244 245 246
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		goto err_bus;
	}

	/* Loop through slave resources */
247
	list_for_each(slave_pos, &bridge->slave_resources) {
248 249 250 251
		slave_image = list_entry(slave_pos,
			struct vme_slave_resource, list);

		if (slave_image == NULL) {
252
			printk(KERN_ERR "Registered NULL Slave resource\n");
253 254 255 256
			continue;
		}

		/* Find an unlocked and compatible image */
257
		mutex_lock(&slave_image->mtx);
258
		if (((slave_image->address_attr & address) == address) &&
259 260 261 262
			((slave_image->cycle_attr & cycle) == cycle) &&
			(slave_image->locked == 0)) {

			slave_image->locked = 1;
263
			mutex_unlock(&slave_image->mtx);
264 265 266
			allocated_image = slave_image;
			break;
		}
267
		mutex_unlock(&slave_image->mtx);
268 269 270 271 272 273 274 275 276 277 278 279
	}

	/* No free image */
	if (allocated_image == NULL)
		goto err_image;

	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
	if (resource == NULL) {
		printk(KERN_WARNING "Unable to allocate resource structure\n");
		goto err_alloc;
	}
	resource->type = VME_SLAVE;
280
	resource->entry = &allocated_image->list;
281 282 283 284 285

	return resource;

err_alloc:
	/* Unlock image */
286
	mutex_lock(&slave_image->mtx);
287
	slave_image->locked = 0;
288
	mutex_unlock(&slave_image->mtx);
289 290 291 292 293 294
err_image:
err_bus:
	return NULL;
}
EXPORT_SYMBOL(vme_slave_request);

295
int vme_slave_set(struct vme_resource *resource, int enabled,
296
	unsigned long long vme_base, unsigned long long size,
M
Martyn Welch 已提交
297
	dma_addr_t buf_base, u32 aspace, u32 cycle)
298 299 300 301 302 303
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_slave_resource *image;
	int retval;

	if (resource->type != VME_SLAVE) {
304
		printk(KERN_ERR "Not a slave resource\n");
305 306 307 308 309 310
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_slave_resource, list);

	if (bridge->slave_set == NULL) {
311
		printk(KERN_ERR "Function not supported\n");
312 313 314
		return -ENOSYS;
	}

315
	if (!(((image->address_attr & aspace) == aspace) &&
316
		((image->cycle_attr & cycle) == cycle))) {
317
		printk(KERN_ERR "Invalid attributes\n");
318 319 320 321
		return -EINVAL;
	}

	retval = vme_check_window(aspace, vme_base, size);
322
	if (retval)
323 324 325 326 327 328 329
		return retval;

	return bridge->slave_set(image, enabled, vme_base, size, buf_base,
		aspace, cycle);
}
EXPORT_SYMBOL(vme_slave_set);

330
int vme_slave_get(struct vme_resource *resource, int *enabled,
331
	unsigned long long *vme_base, unsigned long long *size,
M
Martyn Welch 已提交
332
	dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
333 334 335 336 337
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_slave_resource *image;

	if (resource->type != VME_SLAVE) {
338
		printk(KERN_ERR "Not a slave resource\n");
339 340 341 342 343
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_slave_resource, list);

344
	if (bridge->slave_get == NULL) {
345
		printk(KERN_ERR "vme_slave_get not supported\n");
346 347 348 349 350 351 352 353 354 355 356 357 358
		return -EINVAL;
	}

	return bridge->slave_get(image, enabled, vme_base, size, buf_base,
		aspace, cycle);
}
EXPORT_SYMBOL(vme_slave_get);

void vme_slave_free(struct vme_resource *resource)
{
	struct vme_slave_resource *slave_image;

	if (resource->type != VME_SLAVE) {
359
		printk(KERN_ERR "Not a slave resource\n");
360 361 362 363 364 365
		return;
	}

	slave_image = list_entry(resource->entry, struct vme_slave_resource,
		list);
	if (slave_image == NULL) {
366
		printk(KERN_ERR "Can't find slave resource\n");
367 368 369 370
		return;
	}

	/* Unlock image */
371
	mutex_lock(&slave_image->mtx);
372 373 374 375
	if (slave_image->locked == 0)
		printk(KERN_ERR "Image is already free\n");

	slave_image->locked = 0;
376
	mutex_unlock(&slave_image->mtx);
377 378 379 380 381 382 383 384 385 386

	/* Free up resource memory */
	kfree(resource);
}
EXPORT_SYMBOL(vme_slave_free);

/*
 * Request a master image with specific attributes, return some unique
 * identifier.
 */
M
Martyn Welch 已提交
387 388
struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
	u32 cycle, u32 dwidth)
389 390 391 392 393 394 395
{
	struct vme_bridge *bridge;
	struct list_head *master_pos = NULL;
	struct vme_master_resource *allocated_image = NULL;
	struct vme_master_resource *master_image = NULL;
	struct vme_resource *resource = NULL;

396
	bridge = vdev->bridge;
397 398 399 400 401 402
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		goto err_bus;
	}

	/* Loop through master resources */
403
	list_for_each(master_pos, &bridge->master_resources) {
404 405 406 407 408 409 410 411 412
		master_image = list_entry(master_pos,
			struct vme_master_resource, list);

		if (master_image == NULL) {
			printk(KERN_WARNING "Registered NULL master resource\n");
			continue;
		}

		/* Find an unlocked and compatible image */
413
		spin_lock(&master_image->lock);
414
		if (((master_image->address_attr & address) == address) &&
415 416 417 418 419
			((master_image->cycle_attr & cycle) == cycle) &&
			((master_image->width_attr & dwidth) == dwidth) &&
			(master_image->locked == 0)) {

			master_image->locked = 1;
420
			spin_unlock(&master_image->lock);
421 422 423
			allocated_image = master_image;
			break;
		}
424
		spin_unlock(&master_image->lock);
425 426 427 428 429 430 431 432 433 434 435 436 437 438
	}

	/* Check to see if we found a resource */
	if (allocated_image == NULL) {
		printk(KERN_ERR "Can't find a suitable resource\n");
		goto err_image;
	}

	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
	if (resource == NULL) {
		printk(KERN_ERR "Unable to allocate resource structure\n");
		goto err_alloc;
	}
	resource->type = VME_MASTER;
439
	resource->entry = &allocated_image->list;
440 441 442 443 444

	return resource;

err_alloc:
	/* Unlock image */
445
	spin_lock(&master_image->lock);
446
	master_image->locked = 0;
447
	spin_unlock(&master_image->lock);
448 449 450 451 452 453
err_image:
err_bus:
	return NULL;
}
EXPORT_SYMBOL(vme_master_request);

454
int vme_master_set(struct vme_resource *resource, int enabled,
M
Martyn Welch 已提交
455 456
	unsigned long long vme_base, unsigned long long size, u32 aspace,
	u32 cycle, u32 dwidth)
457 458 459 460 461 462
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;
	int retval;

	if (resource->type != VME_MASTER) {
463
		printk(KERN_ERR "Not a master resource\n");
464 465 466 467 468 469
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);

	if (bridge->master_set == NULL) {
470
		printk(KERN_WARNING "vme_master_set not supported\n");
471 472 473
		return -EINVAL;
	}

474
	if (!(((image->address_attr & aspace) == aspace) &&
475 476
		((image->cycle_attr & cycle) == cycle) &&
		((image->width_attr & dwidth) == dwidth))) {
477
		printk(KERN_WARNING "Invalid attributes\n");
478 479 480 481
		return -EINVAL;
	}

	retval = vme_check_window(aspace, vme_base, size);
482
	if (retval)
483 484 485 486 487 488 489
		return retval;

	return bridge->master_set(image, enabled, vme_base, size, aspace,
		cycle, dwidth);
}
EXPORT_SYMBOL(vme_master_set);

490
int vme_master_get(struct vme_resource *resource, int *enabled,
M
Martyn Welch 已提交
491 492
	unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
	u32 *cycle, u32 *dwidth)
493 494 495 496 497
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;

	if (resource->type != VME_MASTER) {
498
		printk(KERN_ERR "Not a master resource\n");
499 500 501 502 503
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);

504
	if (bridge->master_get == NULL) {
505
		printk(KERN_WARNING "vme_master_set not supported\n");
506 507 508 509 510 511 512 513 514 515 516
		return -EINVAL;
	}

	return bridge->master_get(image, enabled, vme_base, size, aspace,
		cycle, dwidth);
}
EXPORT_SYMBOL(vme_master_get);

/*
 * Read data out of VME space into a buffer.
 */
517
ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
518 519 520 521 522 523 524
	loff_t offset)
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;
	size_t length;

	if (bridge->master_read == NULL) {
525
		printk(KERN_WARNING "Reading from resource not supported\n");
526 527 528 529
		return -EINVAL;
	}

	if (resource->type != VME_MASTER) {
530
		printk(KERN_ERR "Not a master resource\n");
531 532 533 534 535 536 537 538
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);

	length = vme_get_size(resource);

	if (offset > length) {
539
		printk(KERN_WARNING "Invalid Offset\n");
540 541 542 543 544 545 546 547 548 549 550 551 552 553
		return -EFAULT;
	}

	if ((offset + count) > length)
		count = length - offset;

	return bridge->master_read(image, buf, count, offset);

}
EXPORT_SYMBOL(vme_master_read);

/*
 * Write data out to VME space from a buffer.
 */
554
ssize_t vme_master_write(struct vme_resource *resource, void *buf,
555 556 557 558 559 560 561
	size_t count, loff_t offset)
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;
	size_t length;

	if (bridge->master_write == NULL) {
562
		printk(KERN_WARNING "Writing to resource not supported\n");
563 564 565 566
		return -EINVAL;
	}

	if (resource->type != VME_MASTER) {
567
		printk(KERN_ERR "Not a master resource\n");
568 569 570 571 572 573 574 575
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);

	length = vme_get_size(resource);

	if (offset > length) {
576
		printk(KERN_WARNING "Invalid Offset\n");
577 578 579 580 581 582 583 584 585 586 587 588 589
		return -EFAULT;
	}

	if ((offset + count) > length)
		count = length - offset;

	return bridge->master_write(image, buf, count, offset);
}
EXPORT_SYMBOL(vme_master_write);

/*
 * Perform RMW cycle to provided location.
 */
590
unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
591 592 593 594 595 596
	unsigned int compare, unsigned int swap, loff_t offset)
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;

	if (bridge->master_rmw == NULL) {
597
		printk(KERN_WARNING "Writing to resource not supported\n");
598 599 600 601
		return -EINVAL;
	}

	if (resource->type != VME_MASTER) {
602
		printk(KERN_ERR "Not a master resource\n");
603 604 605 606 607 608 609 610 611 612 613 614 615 616
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);

	return bridge->master_rmw(image, mask, compare, swap, offset);
}
EXPORT_SYMBOL(vme_master_rmw);

void vme_master_free(struct vme_resource *resource)
{
	struct vme_master_resource *master_image;

	if (resource->type != VME_MASTER) {
617
		printk(KERN_ERR "Not a master resource\n");
618 619 620 621 622 623
		return;
	}

	master_image = list_entry(resource->entry, struct vme_master_resource,
		list);
	if (master_image == NULL) {
624
		printk(KERN_ERR "Can't find master resource\n");
625 626 627 628
		return;
	}

	/* Unlock image */
629
	spin_lock(&master_image->lock);
630 631 632 633
	if (master_image->locked == 0)
		printk(KERN_ERR "Image is already free\n");

	master_image->locked = 0;
634
	spin_unlock(&master_image->lock);
635 636 637 638 639 640 641 642 643 644

	/* Free up resource memory */
	kfree(resource);
}
EXPORT_SYMBOL(vme_master_free);

/*
 * Request a DMA controller with specific attributes, return some unique
 * identifier.
 */
M
Martyn Welch 已提交
645
struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
646 647 648 649 650 651 652 653 654 655
{
	struct vme_bridge *bridge;
	struct list_head *dma_pos = NULL;
	struct vme_dma_resource *allocated_ctrlr = NULL;
	struct vme_dma_resource *dma_ctrlr = NULL;
	struct vme_resource *resource = NULL;

	/* XXX Not checking resource attributes */
	printk(KERN_ERR "No VME resource Attribute tests done\n");

656
	bridge = vdev->bridge;
657 658 659 660 661 662
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		goto err_bus;
	}

	/* Loop through DMA resources */
663
	list_for_each(dma_pos, &bridge->dma_resources) {
664 665 666 667
		dma_ctrlr = list_entry(dma_pos,
			struct vme_dma_resource, list);

		if (dma_ctrlr == NULL) {
668
			printk(KERN_ERR "Registered NULL DMA resource\n");
669 670 671
			continue;
		}

672
		/* Find an unlocked and compatible controller */
673
		mutex_lock(&dma_ctrlr->mtx);
674 675 676
		if (((dma_ctrlr->route_attr & route) == route) &&
			(dma_ctrlr->locked == 0)) {

677
			dma_ctrlr->locked = 1;
678
			mutex_unlock(&dma_ctrlr->mtx);
679 680 681
			allocated_ctrlr = dma_ctrlr;
			break;
		}
682
		mutex_unlock(&dma_ctrlr->mtx);
683 684 685 686 687 688 689 690 691 692 693 694
	}

	/* Check to see if we found a resource */
	if (allocated_ctrlr == NULL)
		goto err_ctrlr;

	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
	if (resource == NULL) {
		printk(KERN_WARNING "Unable to allocate resource structure\n");
		goto err_alloc;
	}
	resource->type = VME_DMA;
695
	resource->entry = &allocated_ctrlr->list;
696 697 698 699 700

	return resource;

err_alloc:
	/* Unlock image */
701
	mutex_lock(&dma_ctrlr->mtx);
702
	dma_ctrlr->locked = 0;
703
	mutex_unlock(&dma_ctrlr->mtx);
704 705 706 707
err_ctrlr:
err_bus:
	return NULL;
}
708
EXPORT_SYMBOL(vme_dma_request);
709 710 711 712 713 714 715 716 717 718

/*
 * Start new list
 */
struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
{
	struct vme_dma_resource *ctrlr;
	struct vme_dma_list *dma_list;

	if (resource->type != VME_DMA) {
719
		printk(KERN_ERR "Not a DMA resource\n");
720 721 722 723 724
		return NULL;
	}

	ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);

725 726 727
	dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
	if (dma_list == NULL) {
		printk(KERN_ERR "Unable to allocate memory for new dma list\n");
728 729
		return NULL;
	}
730
	INIT_LIST_HEAD(&dma_list->entries);
731
	dma_list->parent = ctrlr;
732
	mutex_init(&dma_list->mtx);
733 734 735 736 737 738 739 740

	return dma_list;
}
EXPORT_SYMBOL(vme_new_dma_list);

/*
 * Create "Pattern" type attributes
 */
M
Martyn Welch 已提交
741
struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
742 743 744 745
{
	struct vme_dma_attr *attributes;
	struct vme_dma_pattern *pattern_attr;

746 747
	attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
	if (attributes == NULL) {
748
		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
749 750 751
		goto err_attr;
	}

752 753
	pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
	if (pattern_attr == NULL) {
754
		printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
		goto err_pat;
	}

	attributes->type = VME_DMA_PATTERN;
	attributes->private = (void *)pattern_attr;

	pattern_attr->pattern = pattern;
	pattern_attr->type = type;

	return attributes;

err_pat:
	kfree(attributes);
err_attr:
	return NULL;
}
EXPORT_SYMBOL(vme_dma_pattern_attribute);

/*
 * Create "PCI" type attributes
 */
struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
{
	struct vme_dma_attr *attributes;
	struct vme_dma_pci *pci_attr;

	/* XXX Run some sanity checks here */

783 784
	attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
	if (attributes == NULL) {
785
		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
786 787 788
		goto err_attr;
	}

789 790
	pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
	if (pci_attr == NULL) {
791
		printk(KERN_ERR "Unable to allocate memory for pci attributes\n");
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
		goto err_pci;
	}



	attributes->type = VME_DMA_PCI;
	attributes->private = (void *)pci_attr;

	pci_attr->address = address;

	return attributes;

err_pci:
	kfree(attributes);
err_attr:
	return NULL;
}
EXPORT_SYMBOL(vme_dma_pci_attribute);

/*
 * Create "VME" type attributes
 */
struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
M
Martyn Welch 已提交
815
	u32 aspace, u32 cycle, u32 dwidth)
816 817 818 819
{
	struct vme_dma_attr *attributes;
	struct vme_dma_vme *vme_attr;

820
	attributes = kmalloc(
821
		sizeof(struct vme_dma_attr), GFP_KERNEL);
822
	if (attributes == NULL) {
823
		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
824 825 826
		goto err_attr;
	}

827 828
	vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
	if (vme_attr == NULL) {
829
		printk(KERN_ERR "Unable to allocate memory for vme attributes\n");
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
		goto err_vme;
	}

	attributes->type = VME_DMA_VME;
	attributes->private = (void *)vme_attr;

	vme_attr->address = address;
	vme_attr->aspace = aspace;
	vme_attr->cycle = cycle;
	vme_attr->dwidth = dwidth;

	return attributes;

err_vme:
	kfree(attributes);
err_attr:
	return NULL;
}
EXPORT_SYMBOL(vme_dma_vme_attribute);

/*
 * Free attribute
 */
void vme_dma_free_attribute(struct vme_dma_attr *attributes)
{
	kfree(attributes->private);
	kfree(attributes);
}
EXPORT_SYMBOL(vme_dma_free_attribute);

int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
	struct vme_dma_attr *dest, size_t count)
{
	struct vme_bridge *bridge = list->parent->parent;
	int retval;

	if (bridge->dma_list_add == NULL) {
867
		printk(KERN_WARNING "Link List DMA generation not supported\n");
868 869 870
		return -EINVAL;
	}

871
	if (!mutex_trylock(&list->mtx)) {
872
		printk(KERN_ERR "Link List already submitted\n");
873 874 875 876 877
		return -EINVAL;
	}

	retval = bridge->dma_list_add(list, src, dest, count);

878
	mutex_unlock(&list->mtx);
879 880 881 882 883 884 885 886 887 888 889

	return retval;
}
EXPORT_SYMBOL(vme_dma_list_add);

int vme_dma_list_exec(struct vme_dma_list *list)
{
	struct vme_bridge *bridge = list->parent->parent;
	int retval;

	if (bridge->dma_list_exec == NULL) {
890
		printk(KERN_ERR "Link List DMA execution not supported\n");
891 892 893
		return -EINVAL;
	}

894
	mutex_lock(&list->mtx);
895 896 897

	retval = bridge->dma_list_exec(list);

898
	mutex_unlock(&list->mtx);
899 900 901 902 903 904 905 906 907 908 909

	return retval;
}
EXPORT_SYMBOL(vme_dma_list_exec);

int vme_dma_list_free(struct vme_dma_list *list)
{
	struct vme_bridge *bridge = list->parent->parent;
	int retval;

	if (bridge->dma_list_empty == NULL) {
910
		printk(KERN_WARNING "Emptying of Link Lists not supported\n");
911 912 913
		return -EINVAL;
	}

914
	if (!mutex_trylock(&list->mtx)) {
915
		printk(KERN_ERR "Link List in use\n");
916 917 918 919 920 921 922 923 924
		return -EINVAL;
	}

	/*
	 * Empty out all of the entries from the dma list. We need to go to the
	 * low level driver as dma entries are driver specific.
	 */
	retval = bridge->dma_list_empty(list);
	if (retval) {
925
		printk(KERN_ERR "Unable to empty link-list entries\n");
926
		mutex_unlock(&list->mtx);
927 928
		return retval;
	}
929
	mutex_unlock(&list->mtx);
930 931 932 933 934 935 936 937 938 939 940
	kfree(list);

	return retval;
}
EXPORT_SYMBOL(vme_dma_list_free);

int vme_dma_free(struct vme_resource *resource)
{
	struct vme_dma_resource *ctrlr;

	if (resource->type != VME_DMA) {
941
		printk(KERN_ERR "Not a DMA resource\n");
942 943 944 945 946
		return -EINVAL;
	}

	ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);

947
	if (!mutex_trylock(&ctrlr->mtx)) {
948
		printk(KERN_ERR "Resource busy, can't free\n");
949 950 951
		return -EBUSY;
	}

952
	if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
953
		printk(KERN_WARNING "Resource still processing transfers\n");
954
		mutex_unlock(&ctrlr->mtx);
955 956 957 958 959
		return -EBUSY;
	}

	ctrlr->locked = 0;

960
	mutex_unlock(&ctrlr->mtx);
961

962 963
	kfree(resource);

964 965 966 967
	return 0;
}
EXPORT_SYMBOL(vme_dma_free);

968 969 970 971 972 973 974 975 976 977 978
void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
{
	void (*call)(int, int, void *);
	void *priv_data;

	call = bridge->irq[level - 1].callback[statid].func;
	priv_data = bridge->irq[level - 1].callback[statid].priv_data;

	if (call != NULL)
		call(level, statid, priv_data);
	else
979 980
		printk(KERN_WARNING "Spurilous VME interrupt, level:%x, vector:%x\n",
		       level, statid);
981 982 983
}
EXPORT_SYMBOL(vme_irq_handler);

984
int vme_irq_request(struct vme_dev *vdev, int level, int statid,
985
	void (*callback)(int, int, void *),
986 987 988 989
	void *priv_data)
{
	struct vme_bridge *bridge;

990
	bridge = vdev->bridge;
991 992 993 994 995
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return -EINVAL;
	}

996
	if ((level < 1) || (level > 7)) {
997
		printk(KERN_ERR "Invalid interrupt level\n");
998 999 1000
		return -EINVAL;
	}

1001 1002
	if (bridge->irq_set == NULL) {
		printk(KERN_ERR "Configuring interrupts not supported\n");
1003 1004 1005
		return -EINVAL;
	}

1006
	mutex_lock(&bridge->irq_mtx);
1007 1008

	if (bridge->irq[level - 1].callback[statid].func) {
1009
		mutex_unlock(&bridge->irq_mtx);
1010 1011 1012 1013 1014 1015 1016 1017 1018
		printk(KERN_WARNING "VME Interrupt already taken\n");
		return -EBUSY;
	}

	bridge->irq[level - 1].count++;
	bridge->irq[level - 1].callback[statid].priv_data = priv_data;
	bridge->irq[level - 1].callback[statid].func = callback;

	/* Enable IRQ level */
1019
	bridge->irq_set(bridge, level, 1, 1);
1020

1021
	mutex_unlock(&bridge->irq_mtx);
1022 1023

	return 0;
1024
}
1025
EXPORT_SYMBOL(vme_irq_request);
1026

1027
void vme_irq_free(struct vme_dev *vdev, int level, int statid)
1028 1029 1030
{
	struct vme_bridge *bridge;

1031
	bridge = vdev->bridge;
1032 1033 1034 1035 1036
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return;
	}

1037
	if ((level < 1) || (level > 7)) {
1038
		printk(KERN_ERR "Invalid interrupt level\n");
1039 1040 1041
		return;
	}

1042 1043
	if (bridge->irq_set == NULL) {
		printk(KERN_ERR "Configuring interrupts not supported\n");
1044 1045 1046
		return;
	}

1047
	mutex_lock(&bridge->irq_mtx);
1048 1049 1050 1051 1052

	bridge->irq[level - 1].count--;

	/* Disable IRQ level if no more interrupts attached at this level*/
	if (bridge->irq[level - 1].count == 0)
1053
		bridge->irq_set(bridge, level, 0, 1);
1054 1055 1056 1057

	bridge->irq[level - 1].callback[statid].func = NULL;
	bridge->irq[level - 1].callback[statid].priv_data = NULL;

1058
	mutex_unlock(&bridge->irq_mtx);
1059
}
1060
EXPORT_SYMBOL(vme_irq_free);
1061

1062
int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
1063 1064 1065
{
	struct vme_bridge *bridge;

1066
	bridge = vdev->bridge;
1067 1068 1069 1070 1071
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return -EINVAL;
	}

1072
	if ((level < 1) || (level > 7)) {
1073 1074 1075 1076
		printk(KERN_WARNING "Invalid interrupt level\n");
		return -EINVAL;
	}

1077
	if (bridge->irq_generate == NULL) {
1078
		printk(KERN_WARNING "Interrupt generation not supported\n");
1079 1080 1081
		return -EINVAL;
	}

1082
	return bridge->irq_generate(bridge, level, statid);
1083
}
1084
EXPORT_SYMBOL(vme_irq_generate);
1085

1086 1087 1088
/*
 * Request the location monitor, return resource or NULL
 */
1089
struct vme_resource *vme_lm_request(struct vme_dev *vdev)
1090 1091
{
	struct vme_bridge *bridge;
1092 1093 1094 1095
	struct list_head *lm_pos = NULL;
	struct vme_lm_resource *allocated_lm = NULL;
	struct vme_lm_resource *lm = NULL;
	struct vme_resource *resource = NULL;
1096

1097
	bridge = vdev->bridge;
1098 1099
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
1100 1101 1102 1103
		goto err_bus;
	}

	/* Loop through DMA resources */
1104
	list_for_each(lm_pos, &bridge->lm_resources) {
1105 1106 1107 1108
		lm = list_entry(lm_pos,
			struct vme_lm_resource, list);

		if (lm == NULL) {
1109
			printk(KERN_ERR "Registered NULL Location Monitor resource\n");
1110 1111 1112 1113
			continue;
		}

		/* Find an unlocked controller */
1114
		mutex_lock(&lm->mtx);
1115 1116
		if (lm->locked == 0) {
			lm->locked = 1;
1117
			mutex_unlock(&lm->mtx);
1118 1119 1120
			allocated_lm = lm;
			break;
		}
1121
		mutex_unlock(&lm->mtx);
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
	}

	/* Check to see if we found a resource */
	if (allocated_lm == NULL)
		goto err_lm;

	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
	if (resource == NULL) {
		printk(KERN_ERR "Unable to allocate resource structure\n");
		goto err_alloc;
	}
	resource->type = VME_LM;
1134
	resource->entry = &allocated_lm->list;
1135 1136 1137 1138 1139

	return resource;

err_alloc:
	/* Unlock image */
1140
	mutex_lock(&lm->mtx);
1141
	lm->locked = 0;
1142
	mutex_unlock(&lm->mtx);
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
err_lm:
err_bus:
	return NULL;
}
EXPORT_SYMBOL(vme_lm_request);

int vme_lm_count(struct vme_resource *resource)
{
	struct vme_lm_resource *lm;

	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
		return -EINVAL;
	}

	lm = list_entry(resource->entry, struct vme_lm_resource, list);

	return lm->monitors;
}
EXPORT_SYMBOL(vme_lm_count);

int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
M
Martyn Welch 已提交
1165
	u32 aspace, u32 cycle)
1166 1167 1168 1169 1170 1171
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;

	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
1172 1173 1174
		return -EINVAL;
	}

1175 1176
	lm = list_entry(resource->entry, struct vme_lm_resource, list);

1177
	if (bridge->lm_set == NULL) {
1178
		printk(KERN_ERR "vme_lm_set not supported\n");
1179 1180 1181
		return -EINVAL;
	}

1182
	return bridge->lm_set(lm, lm_base, aspace, cycle);
1183 1184 1185
}
EXPORT_SYMBOL(vme_lm_set);

1186
int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
M
Martyn Welch 已提交
1187
	u32 *aspace, u32 *cycle)
1188
{
1189 1190
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;
1191

1192 1193
	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
1194 1195 1196
		return -EINVAL;
	}

1197 1198
	lm = list_entry(resource->entry, struct vme_lm_resource, list);

1199
	if (bridge->lm_get == NULL) {
1200
		printk(KERN_ERR "vme_lm_get not supported\n");
1201 1202 1203
		return -EINVAL;
	}

1204
	return bridge->lm_get(lm, lm_base, aspace, cycle);
1205 1206 1207
}
EXPORT_SYMBOL(vme_lm_get);

1208 1209
int vme_lm_attach(struct vme_resource *resource, int monitor,
	void (*callback)(int))
1210
{
1211 1212
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;
1213

1214 1215
	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
1216 1217 1218
		return -EINVAL;
	}

1219 1220
	lm = list_entry(resource->entry, struct vme_lm_resource, list);

1221
	if (bridge->lm_attach == NULL) {
1222
		printk(KERN_ERR "vme_lm_attach not supported\n");
1223 1224 1225
		return -EINVAL;
	}

1226
	return bridge->lm_attach(lm, monitor, callback);
1227 1228 1229
}
EXPORT_SYMBOL(vme_lm_attach);

1230
int vme_lm_detach(struct vme_resource *resource, int monitor)
1231
{
1232 1233
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;
1234

1235 1236
	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
1237 1238 1239
		return -EINVAL;
	}

1240 1241
	lm = list_entry(resource->entry, struct vme_lm_resource, list);

1242
	if (bridge->lm_detach == NULL) {
1243
		printk(KERN_ERR "vme_lm_detach not supported\n");
1244 1245 1246
		return -EINVAL;
	}

1247
	return bridge->lm_detach(lm, monitor);
1248 1249 1250
}
EXPORT_SYMBOL(vme_lm_detach);

1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
void vme_lm_free(struct vme_resource *resource)
{
	struct vme_lm_resource *lm;

	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
		return;
	}

	lm = list_entry(resource->entry, struct vme_lm_resource, list);

1262
	mutex_lock(&lm->mtx);
1263

1264 1265 1266 1267
	/* XXX
	 * Check to see that there aren't any callbacks still attached, if
	 * there are we should probably be detaching them!
	 */
1268 1269 1270

	lm->locked = 0;

1271
	mutex_unlock(&lm->mtx);
1272 1273

	kfree(resource);
1274 1275 1276
}
EXPORT_SYMBOL(vme_lm_free);

1277
int vme_slot_get(struct vme_dev *vdev)
1278 1279 1280
{
	struct vme_bridge *bridge;

1281
	bridge = vdev->bridge;
1282 1283 1284 1285 1286 1287
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return -EINVAL;
	}

	if (bridge->slot_get == NULL) {
1288
		printk(KERN_WARNING "vme_slot_get not supported\n");
1289 1290 1291
		return -EINVAL;
	}

1292
	return bridge->slot_get(bridge);
1293 1294 1295 1296 1297 1298
}
EXPORT_SYMBOL(vme_slot_get);


/* - Bridge Registration --------------------------------------------------- */

1299 1300 1301 1302 1303 1304
static void vme_dev_release(struct device *dev)
{
	kfree(dev_to_vme_dev(dev));
}

int vme_register_bridge(struct vme_bridge *bridge)
1305 1306
{
	int i;
1307
	int ret = -1;
1308

1309
	mutex_lock(&vme_buses_lock);
1310
	for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
1311 1312 1313
		if ((vme_bus_numbers & (1 << i)) == 0) {
			vme_bus_numbers |= (1 << i);
			bridge->num = i;
1314
			INIT_LIST_HEAD(&bridge->devices);
1315 1316
			list_add_tail(&bridge->bus_list, &vme_bus_list);
			ret = 0;
1317 1318 1319
			break;
		}
	}
1320
	mutex_unlock(&vme_buses_lock);
1321

1322
	return ret;
1323
}
1324
EXPORT_SYMBOL(vme_register_bridge);
1325

1326
void vme_unregister_bridge(struct vme_bridge *bridge)
1327
{
1328 1329 1330
	struct vme_dev *vdev;
	struct vme_dev *tmp;

1331 1332
	mutex_lock(&vme_buses_lock);
	vme_bus_numbers &= ~(1 << bridge->num);
1333 1334 1335 1336 1337
	list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
		list_del(&vdev->drv_list);
		list_del(&vdev->bridge_list);
		device_unregister(&vdev->dev);
	}
1338 1339
	list_del(&bridge->bus_list);
	mutex_unlock(&vme_buses_lock);
1340
}
1341
EXPORT_SYMBOL(vme_unregister_bridge);
1342

1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
/* - Driver Registration --------------------------------------------------- */

static int __vme_register_driver_bus(struct vme_driver *drv,
	struct vme_bridge *bridge, unsigned int ndevs)
{
	int err;
	unsigned int i;
	struct vme_dev *vdev;
	struct vme_dev *tmp;

	for (i = 0; i < ndevs; i++) {
		vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
		if (!vdev) {
			err = -ENOMEM;
1357 1358
			goto err_devalloc;
		}
1359
		vdev->num = i;
1360
		vdev->bridge = bridge;
1361 1362
		vdev->dev.platform_data = drv;
		vdev->dev.release = vme_dev_release;
1363 1364
		vdev->dev.parent = bridge->parent;
		vdev->dev.bus = &vme_bus_type;
1365 1366
		dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
			vdev->num);
1367

1368 1369
		err = device_register(&vdev->dev);
		if (err)
1370 1371
			goto err_reg;

1372 1373 1374 1375 1376 1377 1378
		if (vdev->dev.platform_data) {
			list_add_tail(&vdev->drv_list, &drv->devices);
			list_add_tail(&vdev->bridge_list, &bridge->devices);
		} else
			device_unregister(&vdev->dev);
	}
	return 0;
1379 1380

err_reg:
1381
	put_device(&vdev->dev);
1382
	kfree(vdev);
1383
err_devalloc:
1384 1385 1386
	list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
		list_del(&vdev->drv_list);
		list_del(&vdev->bridge_list);
1387
		device_unregister(&vdev->dev);
1388
	}
1389
	return err;
1390 1391
}

1392
static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1393
{
1394 1395
	struct vme_bridge *bridge;
	int err = 0;
1396

1397 1398 1399 1400 1401 1402 1403
	mutex_lock(&vme_buses_lock);
	list_for_each_entry(bridge, &vme_bus_list, bus_list) {
		/*
		 * This cannot cause trouble as we already have vme_buses_lock
		 * and if the bridge is removed, it will have to go through
		 * vme_unregister_bridge() to do it (which calls remove() on
		 * the bridge which in turn tries to acquire vme_buses_lock and
1404
		 * will have to wait).
1405 1406 1407 1408
		 */
		err = __vme_register_driver_bus(drv, bridge, ndevs);
		if (err)
			break;
1409
	}
1410 1411
	mutex_unlock(&vme_buses_lock);
	return err;
1412 1413
}

1414
int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1415
{
1416 1417
	int err;

1418 1419
	drv->driver.name = drv->name;
	drv->driver.bus = &vme_bus_type;
1420 1421 1422 1423 1424
	INIT_LIST_HEAD(&drv->devices);

	err = driver_register(&drv->driver);
	if (err)
		return err;
1425

1426 1427 1428 1429 1430
	err = __vme_register_driver(drv, ndevs);
	if (err)
		driver_unregister(&drv->driver);

	return err;
1431 1432 1433
}
EXPORT_SYMBOL(vme_register_driver);

1434
void vme_unregister_driver(struct vme_driver *drv)
1435
{
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
	struct vme_dev *dev, *dev_tmp;

	mutex_lock(&vme_buses_lock);
	list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
		list_del(&dev->drv_list);
		list_del(&dev->bridge_list);
		device_unregister(&dev->dev);
	}
	mutex_unlock(&vme_buses_lock);

1446 1447 1448 1449 1450 1451 1452 1453
	driver_unregister(&drv->driver);
}
EXPORT_SYMBOL(vme_unregister_driver);

/* - Bus Registration ------------------------------------------------------ */

static int vme_bus_match(struct device *dev, struct device_driver *drv)
{
1454
	struct vme_driver *vme_drv;
1455

1456
	vme_drv = container_of(drv, struct vme_driver, driver);
1457

1458 1459
	if (dev->platform_data == vme_drv) {
		struct vme_dev *vdev = dev_to_vme_dev(dev);
1460

1461 1462
		if (vme_drv->match && vme_drv->match(vdev))
			return 1;
1463

1464
		dev->platform_data = NULL;
1465 1466 1467 1468 1469 1470 1471
	}
	return 0;
}

static int vme_bus_probe(struct device *dev)
{
	int retval = -ENODEV;
1472 1473
	struct vme_driver *driver;
	struct vme_dev *vdev = dev_to_vme_dev(dev);
1474

1475
	driver = dev->platform_data;
1476

1477
	if (driver->probe != NULL)
1478
		retval = driver->probe(vdev);
1479 1480 1481 1482 1483 1484 1485

	return retval;
}

static int vme_bus_remove(struct device *dev)
{
	int retval = -ENODEV;
1486 1487
	struct vme_driver *driver;
	struct vme_dev *vdev = dev_to_vme_dev(dev);
1488

1489
	driver = dev->platform_data;
1490

1491
	if (driver->remove != NULL)
1492
		retval = driver->remove(vdev);
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504

	return retval;
}

struct bus_type vme_bus_type = {
	.name = "vme",
	.match = vme_bus_match,
	.probe = vme_bus_probe,
	.remove = vme_bus_remove,
};
EXPORT_SYMBOL(vme_bus_type);

1505
static int __init vme_init(void)
1506 1507 1508 1509
{
	return bus_register(&vme_bus_type);
}

1510
static void __exit vme_exit(void)
1511 1512 1513 1514 1515
{
	bus_unregister(&vme_bus_type);
}

MODULE_DESCRIPTION("VME bridge driver framework");
1516
MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
1517 1518 1519 1520
MODULE_LICENSE("GPL");

module_init(vme_init);
module_exit(vme_exit);