vme.c 53.9 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
 *
 * 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.
 */

16 17
#include <linux/init.h>
#include <linux/export.h>
18 19 20 21 22 23 24 25 26 27 28 29
#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
static int __init vme_init(void);
43

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

/*
 * 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;
68 69 70 71
	case VME_LM:
		return list_entry(resource->entry, struct vme_lm_resource,
			list)->parent;
		break;
72 73 74 75 76 77 78
	default:
		printk(KERN_ERR "Unknown resource type\n");
		return NULL;
		break;
	}
}

79 80 81 82 83 84
/**
 * vme_free_consistent - Allocate contiguous memory.
 * @resource: Pointer to VME resource.
 * @size: Size of allocation required.
 * @dma: Pointer to variable to store physical address of allocation.
 *
85 86
 * Allocate a contiguous block of memory for use by the driver. This is used to
 * create the buffers for the slave windows.
87 88
 *
 * Return: Virtual address of allocation on success, NULL on failure.
89
 */
90
void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
91 92 93 94
	dma_addr_t *dma)
{
	struct vme_bridge *bridge;

95 96
	if (resource == NULL) {
		printk(KERN_ERR "No resource\n");
97 98 99 100
		return NULL;
	}

	bridge = find_bridge(resource);
101 102
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find bridge\n");
103 104 105 106
		return NULL;
	}

	if (bridge->parent == NULL) {
107
		printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
108 109 110 111
		return NULL;
	}

	if (bridge->alloc_consistent == NULL) {
112 113
		printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
		       bridge->name);
114 115 116
		return NULL;
	}

117
	return bridge->alloc_consistent(bridge->parent, size, dma);
118 119 120
}
EXPORT_SYMBOL(vme_alloc_consistent);

121 122 123 124 125 126 127 128
/**
 * vme_free_consistent - Free previously allocated memory.
 * @resource: Pointer to VME resource.
 * @size: Size of allocation to free.
 * @vaddr: Virtual address of allocation.
 * @dma: Physical address of allocation.
 *
 * Free previously allocated block of contiguous memory.
129 130 131 132 133 134
 */
void vme_free_consistent(struct vme_resource *resource, size_t size,
	void *vaddr, dma_addr_t dma)
{
	struct vme_bridge *bridge;

135 136
	if (resource == NULL) {
		printk(KERN_ERR "No resource\n");
137 138 139 140
		return;
	}

	bridge = find_bridge(resource);
141 142
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find bridge\n");
143 144 145
		return;
	}

146
	if (bridge->parent == NULL) {
147
		printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
148 149 150 151
		return;
	}

	if (bridge->free_consistent == NULL) {
152 153
		printk(KERN_ERR "free_consistent not supported by bridge %s\n",
		       bridge->name);
154 155
		return;
	}
156

157
	bridge->free_consistent(bridge->parent, size, vaddr, dma);
158 159 160
}
EXPORT_SYMBOL(vme_free_consistent);

161 162 163 164 165 166 167 168 169 170
/**
 * vme_get_size - Helper function returning size of a VME window
 * @resource: Pointer to VME slave or master resource.
 *
 * Determine the size of the VME window provided. This is a helper
 * function, wrappering the call to vme_master_get or vme_slave_get
 * depending on the type of window resource handed to it.
 *
 * Return: Size of the window on success, zero on failure.
 */
171 172 173 174 175
size_t vme_get_size(struct vme_resource *resource)
{
	int enabled, retval;
	unsigned long long base, size;
	dma_addr_t buf_base;
Martyn Welch's avatar
Martyn Welch committed
176
	u32 aspace, cycle, dwidth;
177 178 179 180 181

	switch (resource->type) {
	case VME_MASTER:
		retval = vme_master_get(resource, &enabled, &base, &size,
			&aspace, &cycle, &dwidth);
182 183
		if (retval)
			return 0;
184 185 186 187 188 189

		return size;
		break;
	case VME_SLAVE:
		retval = vme_slave_get(resource, &enabled, &base, &size,
			&buf_base, &aspace, &cycle);
190 191
		if (retval)
			return 0;
192 193 194 195 196 197 198 199 200 201 202 203 204 205

		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);

206 207
int vme_check_window(u32 aspace, unsigned long long vme_base,
		     unsigned long long size)
208 209 210
{
	int retval = 0;

211 212 213
	if (vme_base + size < size)
		return -EINVAL;

214 215
	switch (aspace) {
	case VME_A16:
216
		if (vme_base + size > VME_A16_MAX)
217 218 219
			retval = -EFAULT;
		break;
	case VME_A24:
220
		if (vme_base + size > VME_A24_MAX)
221 222 223
			retval = -EFAULT;
		break;
	case VME_A32:
224
		if (vme_base + size > VME_A32_MAX)
225 226 227
			retval = -EFAULT;
		break;
	case VME_A64:
228
		/* The VME_A64_MAX limit is actually U64_MAX + 1 */
229 230
		break;
	case VME_CRCSR:
231
		if (vme_base + size > VME_CRCSR_MAX)
232 233 234 235 236 237 238 239 240
			retval = -EFAULT;
		break;
	case VME_USER1:
	case VME_USER2:
	case VME_USER3:
	case VME_USER4:
		/* User Defined */
		break;
	default:
241
		printk(KERN_ERR "Invalid address space\n");
242 243 244 245 246 247
		retval = -EINVAL;
		break;
	}

	return retval;
}
248
EXPORT_SYMBOL(vme_check_window);
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
static u32 vme_get_aspace(int am)
{
	switch (am) {
	case 0x29:
	case 0x2D:
		return VME_A16;
	case 0x38:
	case 0x39:
	case 0x3A:
	case 0x3B:
	case 0x3C:
	case 0x3D:
	case 0x3E:
	case 0x3F:
		return VME_A24;
	case 0x8:
	case 0x9:
	case 0xA:
	case 0xB:
	case 0xC:
	case 0xD:
	case 0xE:
	case 0xF:
		return VME_A32;
	case 0x0:
	case 0x1:
	case 0x3:
		return VME_A64;
	}

	return 0;
}

283 284 285 286 287 288 289 290 291 292
/**
 * vme_slave_request - Request a VME slave window resource.
 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
 * @address: Required VME address space.
 * @cycle: Required VME data transfer cycle type.
 *
 * Request use of a VME window resource capable of being set for the requested
 * address space and data transfer cycle.
 *
 * Return: Pointer to VME resource on success, NULL on failure.
293
 */
Martyn Welch's avatar
Martyn Welch committed
294 295
struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
	u32 cycle)
296 297 298 299 300 301 302
{
	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;

303
	bridge = vdev->bridge;
304 305 306 307 308 309
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		goto err_bus;
	}

	/* Loop through slave resources */
310
	list_for_each(slave_pos, &bridge->slave_resources) {
311 312 313 314
		slave_image = list_entry(slave_pos,
			struct vme_slave_resource, list);

		if (slave_image == NULL) {
315
			printk(KERN_ERR "Registered NULL Slave resource\n");
316 317 318 319
			continue;
		}

		/* Find an unlocked and compatible image */
320
		mutex_lock(&slave_image->mtx);
321
		if (((slave_image->address_attr & address) == address) &&
322 323 324 325
			((slave_image->cycle_attr & cycle) == cycle) &&
			(slave_image->locked == 0)) {

			slave_image->locked = 1;
326
			mutex_unlock(&slave_image->mtx);
327 328 329
			allocated_image = slave_image;
			break;
		}
330
		mutex_unlock(&slave_image->mtx);
331 332 333 334 335 336 337 338 339 340 341 342
	}

	/* 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;
343
	resource->entry = &allocated_image->list;
344 345 346 347 348

	return resource;

err_alloc:
	/* Unlock image */
349
	mutex_lock(&slave_image->mtx);
350
	slave_image->locked = 0;
351
	mutex_unlock(&slave_image->mtx);
352 353 354 355 356 357
err_image:
err_bus:
	return NULL;
}
EXPORT_SYMBOL(vme_slave_request);

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
/**
 * vme_slave_set - Set VME slave window configuration.
 * @resource: Pointer to VME slave resource.
 * @enabled: State to which the window should be configured.
 * @vme_base: Base address for the window.
 * @size: Size of the VME window.
 * @buf_base: Based address of buffer used to provide VME slave window storage.
 * @aspace: VME address space for the VME window.
 * @cycle: VME data transfer cycle type for the VME window.
 *
 * Set configuration for provided VME slave window.
 *
 * Return: Zero on success, -EINVAL if operation is not supported on this
 *         device, if an invalid resource has been provided or invalid
 *         attributes are provided. Hardware specific errors may also be
 *         returned.
 */
375
int vme_slave_set(struct vme_resource *resource, int enabled,
376
	unsigned long long vme_base, unsigned long long size,
Martyn Welch's avatar
Martyn Welch committed
377
	dma_addr_t buf_base, u32 aspace, u32 cycle)
378 379 380 381 382 383
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_slave_resource *image;
	int retval;

	if (resource->type != VME_SLAVE) {
384
		printk(KERN_ERR "Not a slave resource\n");
385 386 387 388 389 390
		return -EINVAL;
	}

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

	if (bridge->slave_set == NULL) {
391
		printk(KERN_ERR "Function not supported\n");
392 393 394
		return -ENOSYS;
	}

395
	if (!(((image->address_attr & aspace) == aspace) &&
396
		((image->cycle_attr & cycle) == cycle))) {
397
		printk(KERN_ERR "Invalid attributes\n");
398 399 400 401
		return -EINVAL;
	}

	retval = vme_check_window(aspace, vme_base, size);
402
	if (retval)
403 404 405 406 407 408 409
		return retval;

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

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
/**
 * vme_slave_get - Retrieve VME slave window configuration.
 * @resource: Pointer to VME slave resource.
 * @enabled: Pointer to variable for storing state.
 * @vme_base: Pointer to variable for storing window base address.
 * @size: Pointer to variable for storing window size.
 * @buf_base: Pointer to variable for storing slave buffer base address.
 * @aspace: Pointer to variable for storing VME address space.
 * @cycle: Pointer to variable for storing VME data transfer cycle type.
 *
 * Return configuration for provided VME slave window.
 *
 * Return: Zero on success, -EINVAL if operation is not supported on this
 *         device or if an invalid resource has been provided.
 */
425
int vme_slave_get(struct vme_resource *resource, int *enabled,
426
	unsigned long long *vme_base, unsigned long long *size,
Martyn Welch's avatar
Martyn Welch committed
427
	dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
428 429 430 431 432
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_slave_resource *image;

	if (resource->type != VME_SLAVE) {
433
		printk(KERN_ERR "Not a slave resource\n");
434 435 436 437 438
		return -EINVAL;
	}

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

439
	if (bridge->slave_get == NULL) {
440
		printk(KERN_ERR "vme_slave_get not supported\n");
441 442 443 444 445 446 447 448
		return -EINVAL;
	}

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

449 450 451 452 453 454
/**
 * vme_slave_free - Free VME slave window
 * @resource: Pointer to VME slave resource.
 *
 * Free the provided slave resource so that it may be reallocated.
 */
455 456 457 458 459
void vme_slave_free(struct vme_resource *resource)
{
	struct vme_slave_resource *slave_image;

	if (resource->type != VME_SLAVE) {
460
		printk(KERN_ERR "Not a slave resource\n");
461 462 463 464 465 466
		return;
	}

	slave_image = list_entry(resource->entry, struct vme_slave_resource,
		list);
	if (slave_image == NULL) {
467
		printk(KERN_ERR "Can't find slave resource\n");
468 469 470 471
		return;
	}

	/* Unlock image */
472
	mutex_lock(&slave_image->mtx);
473 474 475 476
	if (slave_image->locked == 0)
		printk(KERN_ERR "Image is already free\n");

	slave_image->locked = 0;
477
	mutex_unlock(&slave_image->mtx);
478 479 480 481 482 483

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

484 485 486 487 488 489 490 491 492 493 494
/**
 * vme_master_request - Request a VME master window resource.
 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
 * @address: Required VME address space.
 * @cycle: Required VME data transfer cycle type.
 * @dwidth: Required VME data transfer width.
 *
 * Request use of a VME window resource capable of being set for the requested
 * address space, data transfer cycle and width.
 *
 * Return: Pointer to VME resource on success, NULL on failure.
495
 */
Martyn Welch's avatar
Martyn Welch committed
496 497
struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
	u32 cycle, u32 dwidth)
498 499 500 501 502 503 504
{
	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;

505
	bridge = vdev->bridge;
506 507 508 509 510 511
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		goto err_bus;
	}

	/* Loop through master resources */
512
	list_for_each(master_pos, &bridge->master_resources) {
513 514 515 516 517 518 519 520 521
		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 */
522
		spin_lock(&master_image->lock);
523
		if (((master_image->address_attr & address) == address) &&
524 525 526 527 528
			((master_image->cycle_attr & cycle) == cycle) &&
			((master_image->width_attr & dwidth) == dwidth) &&
			(master_image->locked == 0)) {

			master_image->locked = 1;
529
			spin_unlock(&master_image->lock);
530 531 532
			allocated_image = master_image;
			break;
		}
533
		spin_unlock(&master_image->lock);
534 535 536 537 538 539 540 541 542 543 544 545 546 547
	}

	/* 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;
548
	resource->entry = &allocated_image->list;
549 550 551 552 553

	return resource;

err_alloc:
	/* Unlock image */
554
	spin_lock(&master_image->lock);
555
	master_image->locked = 0;
556
	spin_unlock(&master_image->lock);
557 558 559 560 561 562
err_image:
err_bus:
	return NULL;
}
EXPORT_SYMBOL(vme_master_request);

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
/**
 * vme_master_set - Set VME master window configuration.
 * @resource: Pointer to VME master resource.
 * @enabled: State to which the window should be configured.
 * @vme_base: Base address for the window.
 * @size: Size of the VME window.
 * @aspace: VME address space for the VME window.
 * @cycle: VME data transfer cycle type for the VME window.
 * @dwidth: VME data transfer width for the VME window.
 *
 * Set configuration for provided VME master window.
 *
 * Return: Zero on success, -EINVAL if operation is not supported on this
 *         device, if an invalid resource has been provided or invalid
 *         attributes are provided. Hardware specific errors may also be
 *         returned.
 */
580
int vme_master_set(struct vme_resource *resource, int enabled,
Martyn Welch's avatar
Martyn Welch committed
581 582
	unsigned long long vme_base, unsigned long long size, u32 aspace,
	u32 cycle, u32 dwidth)
583 584 585 586 587 588
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;
	int retval;

	if (resource->type != VME_MASTER) {
589
		printk(KERN_ERR "Not a master resource\n");
590 591 592 593 594 595
		return -EINVAL;
	}

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

	if (bridge->master_set == NULL) {
596
		printk(KERN_WARNING "vme_master_set not supported\n");
597 598 599
		return -EINVAL;
	}

600
	if (!(((image->address_attr & aspace) == aspace) &&
601 602
		((image->cycle_attr & cycle) == cycle) &&
		((image->width_attr & dwidth) == dwidth))) {
603
		printk(KERN_WARNING "Invalid attributes\n");
604 605 606 607
		return -EINVAL;
	}

	retval = vme_check_window(aspace, vme_base, size);
608
	if (retval)
609 610 611 612 613 614 615
		return retval;

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

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
/**
 * vme_master_get - Retrieve VME master window configuration.
 * @resource: Pointer to VME master resource.
 * @enabled: Pointer to variable for storing state.
 * @vme_base: Pointer to variable for storing window base address.
 * @size: Pointer to variable for storing window size.
 * @aspace: Pointer to variable for storing VME address space.
 * @cycle: Pointer to variable for storing VME data transfer cycle type.
 * @dwidth: Pointer to variable for storing VME data transfer width.
 *
 * Return configuration for provided VME master window.
 *
 * Return: Zero on success, -EINVAL if operation is not supported on this
 *         device or if an invalid resource has been provided.
 */
631
int vme_master_get(struct vme_resource *resource, int *enabled,
Martyn Welch's avatar
Martyn Welch committed
632 633
	unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
	u32 *cycle, u32 *dwidth)
634 635 636 637 638
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;

	if (resource->type != VME_MASTER) {
639
		printk(KERN_ERR "Not a master resource\n");
640 641 642 643 644
		return -EINVAL;
	}

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

645
	if (bridge->master_get == NULL) {
646
		printk(KERN_WARNING "%s not supported\n", __func__);
647 648 649 650 651 652 653 654
		return -EINVAL;
	}

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

655 656 657 658 659 660 661 662 663 664 665 666 667 668
/**
 * vme_master_write - Read data from VME space into a buffer.
 * @resource: Pointer to VME master resource.
 * @buf: Pointer to buffer where data should be transferred.
 * @count: Number of bytes to transfer.
 * @offset: Offset into VME master window at which to start transfer.
 *
 * Perform read of count bytes of data from location on VME bus which maps into
 * the VME master window at offset to buf.
 *
 * Return: Number of bytes read, -EINVAL if resource is not a VME master
 *         resource or read operation is not supported. -EFAULT returned if
 *         invalid offset is provided. Hardware specific errors may also be
 *         returned.
669
 */
670
ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
671 672 673 674 675 676 677
	loff_t offset)
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;
	size_t length;

	if (bridge->master_read == NULL) {
678
		printk(KERN_WARNING "Reading from resource not supported\n");
679 680 681 682
		return -EINVAL;
	}

	if (resource->type != VME_MASTER) {
683
		printk(KERN_ERR "Not a master resource\n");
684 685 686 687 688 689 690 691
		return -EINVAL;
	}

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

	length = vme_get_size(resource);

	if (offset > length) {
692
		printk(KERN_WARNING "Invalid Offset\n");
693 694 695 696 697 698 699 700 701 702 703
		return -EFAULT;
	}

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

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

}
EXPORT_SYMBOL(vme_master_read);

704 705 706 707 708 709 710 711 712 713 714 715 716 717
/**
 * vme_master_write - Write data out to VME space from a buffer.
 * @resource: Pointer to VME master resource.
 * @buf: Pointer to buffer holding data to transfer.
 * @count: Number of bytes to transfer.
 * @offset: Offset into VME master window at which to start transfer.
 *
 * Perform write of count bytes of data from buf to location on VME bus which
 * maps into the VME master window at offset.
 *
 * Return: Number of bytes written, -EINVAL if resource is not a VME master
 *         resource or write operation is not supported. -EFAULT returned if
 *         invalid offset is provided. Hardware specific errors may also be
 *         returned.
718
 */
719
ssize_t vme_master_write(struct vme_resource *resource, void *buf,
720 721 722 723 724 725 726
	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) {
727
		printk(KERN_WARNING "Writing to resource not supported\n");
728 729 730 731
		return -EINVAL;
	}

	if (resource->type != VME_MASTER) {
732
		printk(KERN_ERR "Not a master resource\n");
733 734 735 736 737 738 739 740
		return -EINVAL;
	}

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

	length = vme_get_size(resource);

	if (offset > length) {
741
		printk(KERN_WARNING "Invalid Offset\n");
742 743 744 745 746 747 748 749 750 751
		return -EFAULT;
	}

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

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

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
/**
 * vme_master_rmw - Perform read-modify-write cycle.
 * @resource: Pointer to VME master resource.
 * @mask: Bits to be compared and swapped in operation.
 * @compare: Bits to be compared with data read from offset.
 * @swap: Bits to be swapped in data read from offset.
 * @offset: Offset into VME master window at which to perform operation.
 *
 * Perform read-modify-write cycle on provided location:
 * - Location on VME bus is read.
 * - Bits selected by mask are compared with compare.
 * - Where a selected bit matches that in compare and are selected in swap,
 * the bit is swapped.
 * - Result written back to location on VME bus.
 *
 * Return: Bytes written on success, -EINVAL if resource is not a VME master
 *         resource or RMW operation is not supported. Hardware specific
 *         errors may also be returned.
770
 */
771
unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
772 773 774 775 776 777
	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) {
778
		printk(KERN_WARNING "Writing to resource not supported\n");
779 780 781 782
		return -EINVAL;
	}

	if (resource->type != VME_MASTER) {
783
		printk(KERN_ERR "Not a master resource\n");
784 785 786 787 788 789 790 791 792
		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);

793 794 795 796 797 798 799 800 801 802 803
/**
 * vme_master_mmap - Mmap region of VME master window.
 * @resource: Pointer to VME master resource.
 * @vma: Pointer to definition of user mapping.
 *
 * Memory map a region of the VME master window into user space.
 *
 * Return: Zero on success, -EINVAL if resource is not a VME master
 *         resource or -EFAULT if map exceeds window size. Other generic mmap
 *         errors may also be returned.
 */
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
{
	struct vme_master_resource *image;
	phys_addr_t phys_addr;
	unsigned long vma_size;

	if (resource->type != VME_MASTER) {
		pr_err("Not a master resource\n");
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);
	phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
	vma_size = vma->vm_end - vma->vm_start;

	if (phys_addr + vma_size > image->bus_resource.end + 1) {
		pr_err("Map size cannot exceed the window size\n");
		return -EFAULT;
	}

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);

	return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
}
EXPORT_SYMBOL(vme_master_mmap);

830 831 832 833 834 835
/**
 * vme_master_free - Free VME master window
 * @resource: Pointer to VME master resource.
 *
 * Free the provided master resource so that it may be reallocated.
 */
836 837 838 839 840
void vme_master_free(struct vme_resource *resource)
{
	struct vme_master_resource *master_image;

	if (resource->type != VME_MASTER) {
841
		printk(KERN_ERR "Not a master resource\n");
842 843 844 845 846 847
		return;
	}

	master_image = list_entry(resource->entry, struct vme_master_resource,
		list);
	if (master_image == NULL) {
848
		printk(KERN_ERR "Can't find master resource\n");
849 850 851 852
		return;
	}

	/* Unlock image */
853
	spin_lock(&master_image->lock);
854 855 856 857
	if (master_image->locked == 0)
		printk(KERN_ERR "Image is already free\n");

	master_image->locked = 0;
858
	spin_unlock(&master_image->lock);
859 860 861 862 863 864

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

865 866 867 868 869 870 871 872 873
/**
 * vme_dma_request - Request a DMA controller.
 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
 * @route: Required src/destination combination.
 *
 * Request a VME DMA controller with capability to perform transfers bewteen
 * requested source/destination combination.
 *
 * Return: Pointer to VME DMA resource on success, NULL on failure.
874
 */
Martyn Welch's avatar
Martyn Welch committed
875
struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
876 877 878 879 880 881 882 883 884 885
{
	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");

886
	bridge = vdev->bridge;
887 888 889 890 891 892
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		goto err_bus;
	}

	/* Loop through DMA resources */
893
	list_for_each(dma_pos, &bridge->dma_resources) {
894 895 896 897
		dma_ctrlr = list_entry(dma_pos,
			struct vme_dma_resource, list);

		if (dma_ctrlr == NULL) {
898
			printk(KERN_ERR "Registered NULL DMA resource\n");
899 900 901
			continue;
		}

902
		/* Find an unlocked and compatible controller */
903
		mutex_lock(&dma_ctrlr->mtx);
904 905 906
		if (((dma_ctrlr->route_attr & route) == route) &&
			(dma_ctrlr->locked == 0)) {

907
			dma_ctrlr->locked = 1;
908
			mutex_unlock(&dma_ctrlr->mtx);
909 910 911
			allocated_ctrlr = dma_ctrlr;
			break;
		}
912
		mutex_unlock(&dma_ctrlr->mtx);
913 914 915 916 917 918 919 920 921 922 923 924
	}

	/* 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;
925
	resource->entry = &allocated_ctrlr->list;
926 927 928 929 930

	return resource;

err_alloc:
	/* Unlock image */
931
	mutex_lock(&dma_ctrlr->mtx);
932
	dma_ctrlr->locked = 0;
933
	mutex_unlock(&dma_ctrlr->mtx);
934 935 936 937
err_ctrlr:
err_bus:
	return NULL;
}
938
EXPORT_SYMBOL(vme_dma_request);
939

940 941 942 943 944 945 946 947 948
/**
 * vme_new_dma_list - Create new VME DMA list.
 * @resource: Pointer to VME DMA resource.
 *
 * Create a new VME DMA list. It is the responsibility of the user to free
 * the list once it is no longer required with vme_dma_list_free().
 *
 * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
 *         VME DMA resource.
949 950 951 952 953 954 955
 */
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) {
956
		printk(KERN_ERR "Not a DMA resource\n");
957 958 959 960 961
		return NULL;
	}

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

962 963
	dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
	if (dma_list == NULL) {
964
		printk(KERN_ERR "Unable to allocate memory for new DMA list\n");
965 966
		return NULL;
	}
967
	INIT_LIST_HEAD(&dma_list->entries);
968
	dma_list->parent = ctrlr;
969
	mutex_init(&dma_list->mtx);
970 971 972 973 974

	return dma_list;
}
EXPORT_SYMBOL(vme_new_dma_list);

975 976 977 978 979 980 981 982 983 984
/**
 * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
 * @pattern: Value to use used as pattern
 * @type: Type of pattern to be written.
 *
 * Create VME DMA list attribute for pattern generation. It is the
 * responsibility of the user to free used attributes using
 * vme_dma_free_attribute().
 *
 * Return: Pointer to VME DMA attribute, NULL on failure.
985
 */
Martyn Welch's avatar
Martyn Welch committed
986
struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
987 988 989 990
{
	struct vme_dma_attr *attributes;
	struct vme_dma_pattern *pattern_attr;

991 992
	attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
	if (attributes == NULL) {
993
		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
994 995 996
		goto err_attr;
	}

997 998
	pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
	if (pattern_attr == NULL) {
999
		printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
		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);

1018 1019 1020 1021 1022 1023 1024 1025 1026
/**
 * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
 * @address: PCI base address for DMA transfer.
 *
 * Create VME DMA list attribute pointing to a location on PCI for DMA
 * transfers. It is the responsibility of the user to free used attributes
 * using vme_dma_free_attribute().
 *
 * Return: Pointer to VME DMA attribute, NULL on failure.
1027 1028 1029 1030 1031 1032 1033 1034
 */
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 */

1035 1036
	attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
	if (attributes == NULL) {
1037
		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
1038 1039 1040
		goto err_attr;
	}

1041 1042
	pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
	if (pci_attr == NULL) {
1043
		printk(KERN_ERR "Unable to allocate memory for PCI attributes\n");
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
		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);

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
/**
 * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
 * @address: VME base address for DMA transfer.
 * @aspace: VME address space to use for DMA transfer.
 * @cycle: VME bus cycle to use for DMA transfer.
 * @dwidth: VME data width to use for DMA transfer.
 *
 * Create VME DMA list attribute pointing to a location on the VME bus for DMA
 * transfers. It is the responsibility of the user to free used attributes
 * using vme_dma_free_attribute().
 *
 * Return: Pointer to VME DMA attribute, NULL on failure.
1075 1076
 */
struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
Martyn Welch's avatar
Martyn Welch committed
1077
	u32 aspace, u32 cycle, u32 dwidth)
1078 1079 1080 1081
{
	struct vme_dma_attr *attributes;
	struct vme_dma_vme *vme_attr;

1082
	attributes = kmalloc(
1083
		sizeof(struct vme_dma_attr), GFP_KERNEL);
1084
	if (attributes == NULL) {
1085
		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
1086 1087 1088
		goto err_attr;
	}

1089 1090
	vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
	if (vme_attr == NULL) {
1091
		printk(KERN_ERR "Unable to allocate memory for VME attributes\n");
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
		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);

1112 1113 1114 1115 1116 1117
/**
 * vme_dma_free_attribute - Free DMA list attribute.
 * @attributes: Pointer to DMA list attribute.
 *
 * Free VME DMA list attribute. VME DMA list attributes can be safely freed
 * once vme_dma_list_add() has returned.
1118 1119 1120 1121 1122 1123 1124 1125
 */
void vme_dma_free_attribute(struct vme_dma_attr *attributes)
{
	kfree(attributes->private);
	kfree(attributes);
}
EXPORT_SYMBOL(vme_dma_free_attribute);

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
/**
 * vme_dma_list_add - Add enty to a VME DMA list.
 * @list: Pointer to VME list.
 * @src: Pointer to DMA list attribute to use as source.
 * @dest: Pointer to DMA list attribute to use as destination.
 * @count: Number of bytes to transfer.
 *
 * Add an entry to the provided VME DMA list. Entry requires pointers to source
 * and destination DMA attributes and a count.
 *
 * Please note, the attributes supported as source and destinations for
 * transfers are hardware dependent.
 *
 * Return: Zero on success, -EINVAL if operation is not supported on this
 *         device or if the link list has already been submitted for execution.
 *         Hardware specific errors also possible.
 */
1143 1144 1145 1146 1147 1148 1149
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) {
1150
		printk(KERN_WARNING "Link List DMA generation not supported\n");
1151 1152 1153
		return -EINVAL;
	}

1154
	if (!mutex_trylock(&list->mtx)) {
1155
		printk(KERN_ERR "Link List already submitted\n");
1156 1157 1158 1159 1160
		return -EINVAL;
	}

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

1161
	mutex_unlock(&list->mtx);
1162 1163 1164 1165 1166

	return retval;
}
EXPORT_SYMBOL(vme_dma_list_add);

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
/**
 * vme_dma_list_exec - Queue a VME DMA list for execution.
 * @list: Pointer to VME list.
 *
 * Queue the provided VME DMA list for execution. The call will return once the
 * list has been executed.
 *
 * Return: Zero on success, -EINVAL if operation is not supported on this
 *         device. Hardware specific errors also possible.
 */
1177 1178 1179 1180 1181 1182
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) {
1183
		printk(KERN_ERR "Link List DMA execution not supported\n");
1184 1185 1186
		return -EINVAL;
	}

1187
	mutex_lock(&list->mtx);
1188 1189 1190

	retval = bridge->dma_list_exec(list);

1191
	mutex_unlock(&list->mtx);
1192 1193 1194 1195 1196

	return retval;
}
EXPORT_SYMBOL(vme_dma_list_exec);

1197 1198 1199 1200 1201 1202 1203 1204 1205
/**
 * vme_dma_list_free - Free a VME DMA list.
 * @list: Pointer to VME list.
 *
 * Free the provided DMA list and all its entries.
 *
 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
 *         is still in use. Hardware specific errors also possible.
 */
1206 1207 1208 1209 1210 1211
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) {
1212
		printk(KERN_WARNING "Emptying of Link Lists not supported\n");
1213 1214 1215
		return -EINVAL;
	}

1216
	if (!mutex_trylock(&list->mtx)) {
1217
		printk(KERN_ERR "Link List in use\n");
1218 1219 1220 1221
		return -EINVAL;
	}

	/*
1222 1223
	 * 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.
1224 1225 1226
	 */
	retval = bridge->dma_list_empty(list);
	if (retval) {
1227
		printk(KERN_ERR "Unable to empty link-list entries\n");
1228
		mutex_unlock(&list->mtx);
1229 1230
		return retval;
	}
1231
	mutex_unlock(&list->mtx);
1232 1233 1234 1235 1236 1237
	kfree(list);

	return retval;
}
EXPORT_SYMBOL(vme_dma_list_free);

1238 1239 1240 1241 1242 1243 1244 1245 1246
/**
 * vme_dma_free - Free a VME DMA resource.
 * @resource: Pointer to VME DMA resource.
 *
 * Free the provided DMA resource so that it may be reallocated.
 *
 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
 *         is still active.
 */
1247 1248 1249 1250 1251
int vme_dma_free(struct vme_resource *resource)
{
	struct vme_dma_resource *ctrlr;

	if (resource->type != VME_DMA) {
1252
		printk(KERN_ERR "Not a DMA resource\n");
1253 1254 1255 1256 1257
		return -EINVAL;
	}

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

1258
	if (!mutex_trylock(&ctrlr->mtx)) {
1259
		printk(KERN_ERR "Resource busy, can't free\n");
1260 1261 1262
		return -EBUSY;
	}

1263
	if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
1264
		printk(KERN_WARNING "Resource still processing transfers\n");
1265
		mutex_unlock(&ctrlr->mtx);
1266 1267 1268 1269 1270
		return -EBUSY;
	}

	ctrlr->locked = 0;

1271
	mutex_unlock(&ctrlr->mtx);
1272

1273 1274
	kfree(resource);

1275 1276 1277 1278
	return 0;
}
EXPORT_SYMBOL(vme_dma_free);

1279
void vme_bus_error_handler(struct vme_bridge *bridge,
1280
			   unsigned long long address, int am)
1281
{
1282 1283
	struct list_head *handler_pos = NULL;
	struct vme_error_handler *handler;
1284
	int handler_triggered = 0;
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
	u32 aspace = vme_get_aspace(am);

	list_for_each(handler_pos, &bridge->vme_error_handlers) {
		handler = list_entry(handler_pos, struct vme_error_handler,
				     list);
		if ((aspace == handler->aspace) &&
		    (address >= handler->start) &&
		    (address < handler->end)) {
			if (!handler->num_errors)
				handler->first_error = address;
			if (handler->num_errors != UINT_MAX)
				handler->num_errors++;
1297
			handler_triggered = 1;
1298
		}
1299
	}
1300 1301 1302 1303 1304

	if (!handler_triggered)
		dev_err(bridge->parent,
			"Unhandled VME access error at address 0x%llx\n",
			address);
1305 1306 1307
}
EXPORT_SYMBOL(vme_bus_error_handler);

1308 1309 1310
struct vme_error_handler *vme_register_error_handler(
	struct vme_bridge *bridge, u32 aspace,
	unsigned long long address, size_t len)
1311
{
1312
	struct vme_error_handler *handler;
1313

1314 1315 1316
	handler = kmalloc(sizeof(*handler), GFP_KERNEL);
	if (!handler)
		return NULL;
1317

1318 1319 1320 1321 1322 1323
	handler->aspace = aspace;
	handler->start = address;
	handler->end = address + len;
	handler->num_errors = 0;
	handler->first_error = 0;
	list_add_tail(&handler->list, &bridge->vme_error_handlers);
1324

1325
	return handler;
1326
}
1327
EXPORT_SYMBOL(vme_register_error_handler);
1328

1329
void vme_unregister_error_handler(struct vme_error_handler *handler)
1330
{
1331 1332
	list_del(&handler->list);
	kfree(handler);
1333
}
1334
EXPORT_SYMBOL(vme_unregister_error_handler);
1335

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
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
1347
		printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
1348
		       level, statid);
1349 1350 1351
}
EXPORT_SYMBOL(vme_irq_handler);

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
/**
 * vme_irq_request - Request a specific VME interrupt.
 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
 * @level: Interrupt priority being requested.
 * @statid: Interrupt vector being requested.
 * @callback: Pointer to callback function called when VME interrupt/vector
 *            received.
 * @priv_data: Generic pointer that will be passed to the callback function.
 *
 * Request callback to be attached as a handler for VME interrupts with provided
 * level and statid.
 *
 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
 *         function is not supported, -EBUSY if the level/statid combination is
 *         already in use. Hardware specific errors also possible.
 */
1368
int vme_irq_request(struct vme_dev *vdev, int level, int statid,
1369
	void (*callback)(int, int, void *),
1370 1371 1372 1373
	void *priv_data)
{
	struct vme_bridge *bridge;

1374
	bridge = vdev->bridge;
1375 1376 1377 1378 1379
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return -EINVAL;
	}

1380
	if ((level < 1) || (level > 7)) {
1381
		printk(KERN_ERR "Invalid interrupt level\n");
1382 1383 1384
		return -EINVAL;
	}

1385 1386
	if (bridge->irq_set == NULL) {
		printk(KERN_ERR "Configuring interrupts not supported\n");
1387 1388 1389
		return -EINVAL;
	}

1390
	mutex_lock(&bridge->irq_mtx);
1391 1392

	if (bridge->irq[level - 1].callback[statid].func) {
1393
		mutex_unlock(&bridge->irq_mtx);
1394 1395 1396 1397 1398 1399 1400 1401 1402
		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 */
1403
	bridge->irq_set(bridge, level, 1, 1);
1404

1405
	mutex_unlock(&bridge->irq_mtx);
1406 1407

	return 0;
1408
}
1409
EXPORT_SYMBOL(vme_irq_request);
1410

1411 1412 1413 1414 1415 1416 1417 1418
/**
 * vme_irq_free - Free a VME interrupt.
 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
 * @level: Interrupt priority of interrupt being freed.
 * @statid: Interrupt vector of interrupt being freed.
 *
 * Remove previously attached callback from VME interrupt priority/vector.
 */
1419
void vme_irq_free(struct vme_dev *vdev, int level, int statid)
1420 1421 1422
{
	struct vme_bridge *bridge;

1423
	bridge = vdev->bridge;
1424 1425 1426 1427 1428
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return;
	}

1429
	if ((level < 1) || (level > 7)) {
1430
		printk(KERN_ERR "Invalid interrupt level\n");
1431 1432 1433
		return;
	}

1434 1435
	if (bridge->irq_set == NULL) {
		printk(KERN_ERR "Configuring interrupts not supported\n");
1436 1437 1438
		return;
	}

1439
	mutex_lock(&bridge->irq_mtx);
1440 1441 1442 1443 1444

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

	/* Disable IRQ level if no more interrupts attached at this level*/
	if (bridge->irq[level - 1].count == 0)
1445
		bridge->irq_set(bridge, level, 0, 1);
1446 1447 1448 1449

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

1450
	mutex_unlock(&bridge->irq_mtx);
1451
}
1452
EXPORT_SYMBOL(vme_irq_free);
1453

1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
/**
 * vme_irq_generate - Generate VME interrupt.
 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
 * @level: Interrupt priority at which to assert the interrupt.
 * @statid: Interrupt vector to associate with the interrupt.
 *
 * Generate a VME interrupt of the provided level and with the provided
 * statid.
 *
 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
 *         function is not supported. Hardware specific errors also possible.
 */
1466
int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
1467 1468 1469
{
	struct vme_bridge *bridge;

1470
	bridge = vdev->bridge;
1471 1472 1473 1474 1475
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return -EINVAL;
	}

1476
	if ((level < 1) || (level > 7)) {
1477 1478 1479 1480
		printk(KERN_WARNING "Invalid interrupt level\n");
		return -EINVAL;
	}

1481
	if (bridge->irq_generate == NULL) {
1482
		printk(KERN_WARNING "Interrupt generation not supported\n");
1483 1484 1485
		return -EINVAL;
	}

1486
	return bridge->irq_generate(bridge, level, statid);
1487
}
1488
EXPORT_SYMBOL(vme_irq_generate);
1489

1490 1491 1492 1493 1494 1495 1496 1497 1498
/**
 * vme_lm_request - Request a VME location monitor
 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
 *
 * Allocate a location monitor resource to the driver. A location monitor
 * allows the driver to monitor accesses to a contiguous number of
 * addresses on the VME bus.
 *
 * Return: Pointer to a VME resource on success or NULL on failure.
1499
 */
1500
struct vme_resource *vme_lm_request(struct vme_dev *vdev)
1501 1502
{
	struct vme_bridge *bridge;
1503 1504 1505 1506
	struct list_head *lm_pos = NULL;
	struct vme_lm_resource *allocated_lm = NULL;
	struct vme_lm_resource *lm = NULL;
	struct vme_resource *resource = NULL;
1507

1508
	bridge = vdev->bridge;
1509 1510
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
1511 1512 1513
		goto err_bus;
	}

1514
	/* Loop through LM resources */
1515
	list_for_each(lm_pos, &bridge->lm_resources) {
1516 1517 1518 1519
		lm = list_entry(lm_pos,
			struct vme_lm_resource, list);

		if (lm == NULL) {
1520
			printk(KERN_ERR "Registered NULL Location Monitor resource\n");
1521 1522 1523 1524
			continue;
		}

		/* Find an unlocked controller */
1525
		mutex_lock(&lm->mtx);
1526 1527
		if (lm->locked == 0) {
			lm->locked = 1;
1528
			mutex_unlock(&lm->mtx);
1529 1530 1531
			allocated_lm = lm;
			break;
		}
1532
		mutex_unlock(&lm->mtx);
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
	}

	/* 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;
1545
	resource->entry = &allocated_lm->list;
1546 1547 1548 1549 1550

	return resource;

err_alloc:
	/* Unlock image */
1551
	mutex_lock(&lm->mtx);
1552
	lm->locked = 0;
1553
	mutex_unlock(&lm->mtx);
1554 1555 1556 1557 1558 1559
err_lm:
err_bus:
	return NULL;
}
EXPORT_SYMBOL(vme_lm_request);

1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
/**
 * vme_lm_count - Determine number of VME Addresses monitored
 * @resource: Pointer to VME location monitor resource.
 *
 * The number of contiguous addresses monitored is hardware dependent.
 * Return the number of contiguous addresses monitored by the
 * location monitor.
 *
 * Return: Count of addresses monitored or -EINVAL when provided with an
 *	   invalid location monitor resource.
 */
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
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);

1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
/**
 * vme_lm_set - Configure location monitor
 * @resource: Pointer to VME location monitor resource.
 * @lm_base: Base address to monitor.
 * @aspace: VME address space to monitor.
 * @cycle: VME bus cycle type to monitor.
 *
 * Set the base address, address space and cycle type of accesses to be
 * monitored by the location monitor.
 *
 * Return: Zero on success, -EINVAL when provided with an invalid location
 *	   monitor resource or function is not supported. Hardware specific
 *	   errors may also be returned.
 */
1600
int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
Martyn Welch's avatar
Martyn Welch committed
1601
	u32 aspace, u32 cycle)
1602 1603 1604 1605 1606 1607
{
	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");
1608 1609 1610
		return -EINVAL;
	}

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

1613
	if (bridge->lm_set == NULL) {
1614
		printk(KERN_ERR "vme_lm_set not supported\n");
1615 1616 1617
		return -EINVAL;
	}

1618
	return bridge->lm_set(lm, lm_base, aspace, cycle);
1619 1620 1621
}
EXPORT_SYMBOL(vme_lm_set);

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
/**
 * vme_lm_get - Retrieve location monitor settings
 * @resource: Pointer to VME location monitor resource.
 * @lm_base: Pointer used to output the base address monitored.
 * @aspace: Pointer used to output the address space monitored.
 * @cycle: Pointer used to output the VME bus cycle type monitored.
 *
 * Retrieve the base address, address space and cycle type of accesses to
 * be monitored by the location monitor.
 *
 * Return: Zero on success, -EINVAL when provided with an invalid location
 *	   monitor resource or function is not supported. Hardware specific
 *	   errors may also be returned.
 */
1636
int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
Martyn Welch's avatar
Martyn Welch committed
1637
	u32 *aspace, u32 *cycle)
1638
{
1639 1640
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;
1641

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

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

1649
	if (bridge->lm_get == NULL) {
1650
		printk(KERN_ERR "vme_lm_get not supported\n");
1651 1652 1653
		return -EINVAL;
	}

1654
	return bridge->lm_get(lm, lm_base, aspace, cycle);
1655 1656 1657
}
EXPORT_SYMBOL(vme_lm_get);

1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
/**
 * vme_lm_attach - Provide callback for location monitor address
 * @resource: Pointer to VME location monitor resource.
 * @monitor: Offset to which callback should be attached.
 * @callback: Pointer to callback function called when triggered.
 * @data: Generic pointer that will be passed to the callback function.
 *
 * Attach a callback to the specificed offset into the location monitors
 * monitored addresses. A generic pointer is provided to allow data to be
 * passed to the callback when called.
 *
 * Return: Zero on success, -EINVAL when provided with an invalid location
 *	   monitor resource or function is not supported. Hardware specific
 *	   errors may also be returned.
 */
1673
int vme_lm_attach(struct vme_resource *resource, int monitor,
1674
	void (*callback)(void *), void *data)
1675
{
1676 1677
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;
1678

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

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

1686
	if (bridge->lm_attach == NULL) {
1687
		printk(KERN_ERR "vme_lm_attach not supported\n");
1688 1689 1690
		return -EINVAL;
	}

1691
	return bridge->lm_attach(lm, monitor, callback, data);
1692 1693 1694
}
EXPORT_SYMBOL(vme_lm_attach);

1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
/**
 * vme_lm_detach - Remove callback for location monitor address
 * @resource: Pointer to VME location monitor resource.
 * @monitor: Offset to which callback should be removed.
 *
 * Remove the callback associated with the specificed offset into the
 * location monitors monitored addresses.
 *
 * Return: Zero on success, -EINVAL when provided with an invalid location
 *	   monitor resource or function is not supported. Hardware specific
 *	   errors may also be returned.
 */
1707
int vme_lm_detach(struct vme_resource *resource, int monitor)
1708
{
1709 1710
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;
1711

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

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

1719
	if (bridge->lm_detach == NULL) {
1720
		printk(KERN_ERR "vme_lm_detach not supported\n");
1721 1722 1723
		return -EINVAL;
	}

1724
	return bridge->lm_detach(lm, monitor);
1725 1726 1727
}
EXPORT_SYMBOL(vme_lm_detach);

1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
/**
 * vme_lm_free - Free allocated VME location monitor
 * @resource: Pointer to VME location monitor resource.
 *
 * Free allocation of a VME location monitor.
 *
 * WARNING: This function currently expects that any callbacks that have
 *          been attached to the location monitor have been removed.
 *
 * Return: Zero on success, -EINVAL when provided with an invalid location
 *	   monitor resource.
 */
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
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);

1751
	mutex_lock(&lm->mtx);
1752

1753 1754 1755 1756
	/* XXX
	 * Check to see that there aren't any callbacks still attached, if
	 * there are we should probably be detaching them!
	 */
1757 1758 1759

	lm->locked = 0;

1760
	mutex_unlock(&lm->mtx);
1761 1762

	kfree(resource);
1763 1764 1765
}
EXPORT_SYMBOL(vme_lm_free);

1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
/**
 * vme_slot_num - Retrieve slot ID
 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
 *
 * Retrieve the slot ID associated with the provided VME device.
 *
 * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
 *         or the function is not supported. Hardware specific errors may also
 *         be returned.
 */
1776
int vme_slot_num(struct vme_dev *vdev)
1777 1778 1779
{
	struct vme_bridge *bridge;

1780
	bridge = vdev->bridge;
1781 1782 1783 1784 1785 1786
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return -EINVAL;
	}

	if (bridge->slot_get == NULL) {
1787
		printk(KERN_WARNING "vme_slot_num not supported\n");
1788 1789 1790
		return -EINVAL;
	}

1791
	return bridge->slot_get(bridge);
1792
}
1793
EXPORT_SYMBOL(vme_slot_num);
1794

1795 1796 1797 1798 1799 1800 1801 1802 1803
/**
 * vme_bus_num - Retrieve bus number
 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
 *
 * Retrieve the bus enumeration associated with the provided VME device.
 *
 * Return: The bus number on success, -EINVAL if VME bridge cannot be
 *         determined.
 */
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
int vme_bus_num(struct vme_dev *vdev)
{
	struct vme_bridge *bridge;

	bridge = vdev->bridge;
	if (bridge == NULL) {
		pr_err("Can't find VME bus\n");
		return -EINVAL;
	}

	return bridge->num;
}
EXPORT_SYMBOL(vme_bus_num);
1817 1818 1819

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

1820 1821 1822 1823 1824
static void vme_dev_release(struct device *dev)
{
	kfree(dev_to_vme_dev(dev));
}

1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
/* Common bridge initialization */
struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
{
	INIT_LIST_HEAD(&bridge->vme_error_handlers);
	INIT_LIST_HEAD(&bridge->master_resources);
	INIT_LIST_HEAD(&bridge->slave_resources);
	INIT_LIST_HEAD(&bridge->dma_resources);
	INIT_LIST_HEAD(&bridge->lm_resources);
	mutex_init(&bridge->irq_mtx);

	return bridge;
}
EXPORT_SYMBOL(vme_init_bridge);

1839
int vme_register_bridge(struct vme_bridge *bridge)
1840 1841
{
	int i;
1842
	int ret = -1;
1843

1844
	mutex_lock(&vme_buses_lock);
1845
	for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
1846 1847 1848
		if ((vme_bus_numbers & (1 << i)) == 0) {
			vme_bus_numbers |= (1 << i);
			bridge->num = i;
1849
			INIT_LIST_HEAD(&bridge->devices);
1850 1851
			list_add_tail(&bridge->bus_list, &vme_bus_list);
			ret = 0;
1852 1853 1854
			break;
		}
	}
1855
	mutex_unlock(&vme_buses_lock);
1856

1857
	return ret;
1858
}
1859
EXPORT_SYMBOL(vme_register_bridge);
1860

1861
void vme_unregister_bridge(struct vme_bridge *bridge)
1862
{
1863 1864 1865
	struct vme_dev *vdev;
	struct vme_dev *tmp;

1866 1867
	mutex_lock(&vme_buses_lock);
	vme_bus_numbers &= ~(1 << bridge->num);
1868 1869 1870 1871 1872
	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);
	}
1873 1874
	list_del(&bridge->bus_list);
	mutex_unlock(&vme_buses_lock);
1875
}
1876
EXPORT_SYMBOL(vme_unregister_bridge);
1877

1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
/* - 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;
1892 1893
			goto err_devalloc;
		}
1894
		vdev->num = i;
1895
		vdev->bridge = bridge;
1896 1897
		vdev->dev.platform_data = drv;
		vdev->dev.release = vme_dev_release;
1898 1899
		vdev->dev.parent = bridge->parent;
		vdev->dev.bus = &vme_bus_type;
1900 1901
		dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
			vdev->num);
1902

1903 1904
		err = device_register(&vdev->dev);
		if (err)
1905 1906
			goto err_reg;

1907 1908 1909 1910 1911 1912 1913
		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;
1914 1915

err_reg:
1916
	put_device(&vdev->dev);
1917
	kfree(vdev);
1918
err_devalloc:
1919 1920 1921
	list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
		list_del(&vdev->drv_list);
		list_del(&vdev->bridge_list);
1922
		device_unregister(&vdev->dev);
1923
	}
1924
	return err;
1925 1926
}

1927
static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1928
{
1929 1930
	struct vme_bridge *bridge;
	int err = 0;
1931

1932 1933 1934 1935 1936 1937 1938
	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
1939
		 * will have to wait).
1940 1941 1942 1943
		 */
		err = __vme_register_driver_bus(drv, bridge, ndevs);
		if (err)
			break;
1944
	}
1945 1946
	mutex_unlock(&vme_buses_lock);
	return err;
1947 1948
}

1949 1950 1951 1952 1953 1954 1955 1956 1957
/**
 * vme_register_driver - Register a VME driver
 * @drv: Pointer to VME driver structure to register.
 * @ndevs: Maximum number of devices to allow to be enumerated.
 *
 * Register a VME device driver with the VME subsystem.
 *
 * Return: Zero on success, error value on registration failure.
 */
1958
int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1959
{
1960 1961
	int err;

1962 1963
	drv->driver.name = drv->name;
	drv->driver.bus = &vme_bus_type;
1964 1965 1966 1967 1968
	INIT_LIST_HEAD(&drv->devices);

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

1970 1971 1972 1973 1974
	err = __vme_register_driver(drv, ndevs);
	if (err)
		driver_unregister(&drv->driver);

	return err;
1975 1976 1977
}
EXPORT_SYMBOL(vme_register_driver);

1978 1979 1980 1981 1982 1983
/**
 * vme_unregister_driver - Unregister a VME driver
 * @drv: Pointer to VME driver structure to unregister.
 *
 * Unregister a VME device driver from the VME subsystem.
 */
1984
void vme_unregister_driver(struct vme_driver *drv)
1985
{
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
	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);

1996 1997 1998 1999 2000 2001 2002 2003
	driver_unregister(&drv->driver);
}
EXPORT_SYMBOL(vme_unregister_driver);

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

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

2006
	vme_drv = container_of(drv, struct vme_driver, driver);
2007

2008 2009
	if (dev->platform_data == vme_drv) {
		struct vme_dev *vdev = dev_to_vme_dev(dev);
2010

2011 2012
		if (vme_drv->match && vme_drv->match(vdev))
			return 1;
2013

2014
		dev->platform_data = NULL;
2015 2016 2017 2018 2019 2020 2021
	}
	return 0;
}

static int vme_bus_probe(struct device *dev)
{
	int retval = -ENODEV;
2022 2023
	struct vme_driver *driver;
	struct vme_dev *vdev = dev_to_vme_dev(dev);
2024

2025
	driver = dev->platform_data;
2026

2027
	if (driver->probe != NULL)
2028
		retval = driver->probe(vdev);
2029 2030 2031 2032

	return retval;
}

2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
static int vme_bus_remove(struct device *dev)
{
	int retval = -ENODEV;
	struct vme_driver *driver;
	struct vme_dev *vdev = dev_to_vme_dev(dev);

	driver = dev->platform_data;

	if (driver->remove != NULL)
		retval = driver->remove(vdev);

	return retval;
}

2047 2048 2049 2050
struct bus_type vme_bus_type = {
	.name = "vme",
	.match = vme_bus_match,
	.probe = vme_bus_probe,
2051
	.remove = vme_bus_remove,
2052 2053 2054
};
EXPORT_SYMBOL(vme_bus_type);

2055
static int __init vme_init(void)
2056 2057 2058
{
	return bus_register(&vme_bus_type);
}
2059
subsys_initcall(vme_init);