]> Git Repo - linux.git/blame - drivers/vme/vme.c
vme: Move an assignment in vme_new_dma_list()
[linux.git] / drivers / vme / vme.c
CommitLineData
a17a75e2
MW
1/*
2 * VME Bridge Framework
3 *
66bd8db5
MW
4 * Author: Martyn Welch <[email protected]>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
a17a75e2
MW
6 *
7 * Based on work by Tom Armistead and Ajit Prem
8 * Copyright 2004 Motorola Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 */
15
050c3d52
PG
16#include <linux/init.h>
17#include <linux/export.h>
a17a75e2
MW
18#include <linux/mm.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/pci.h>
23#include <linux/poll.h>
24#include <linux/highmem.h>
25#include <linux/interrupt.h>
26#include <linux/pagemap.h>
27#include <linux/device.h>
28#include <linux/dma-mapping.h>
29#include <linux/syscalls.h>
400822fe 30#include <linux/mutex.h>
a17a75e2 31#include <linux/spinlock.h>
5a0e3ad6 32#include <linux/slab.h>
db3b9e99 33#include <linux/vme.h>
a17a75e2 34
a17a75e2
MW
35#include "vme_bridge.h"
36
733e3ef0 37/* Bitmask and list of registered buses both protected by common mutex */
a17a75e2 38static unsigned int vme_bus_numbers;
733e3ef0
MV
39static LIST_HEAD(vme_bus_list);
40static DEFINE_MUTEX(vme_buses_lock);
a17a75e2 41
ead1f3e3 42static int __init vme_init(void);
a17a75e2 43
8f966dc4 44static struct vme_dev *dev_to_vme_dev(struct device *dev)
a17a75e2 45{
8f966dc4 46 return container_of(dev, struct vme_dev, dev);
a17a75e2
MW
47}
48
49/*
50 * Find the bridge that the resource is associated with.
51 */
52static struct vme_bridge *find_bridge(struct vme_resource *resource)
53{
54 /* Get list to search */
55 switch (resource->type) {
56 case VME_MASTER:
57 return list_entry(resource->entry, struct vme_master_resource,
58 list)->parent;
59 break;
60 case VME_SLAVE:
61 return list_entry(resource->entry, struct vme_slave_resource,
62 list)->parent;
63 break;
64 case VME_DMA:
65 return list_entry(resource->entry, struct vme_dma_resource,
66 list)->parent;
67 break;
42fb5031
MW
68 case VME_LM:
69 return list_entry(resource->entry, struct vme_lm_resource,
70 list)->parent;
71 break;
a17a75e2
MW
72 default:
73 printk(KERN_ERR "Unknown resource type\n");
74 return NULL;
75 break;
76 }
77}
78
b5bc980a
MW
79/**
80 * vme_free_consistent - Allocate contiguous memory.
81 * @resource: Pointer to VME resource.
82 * @size: Size of allocation required.
83 * @dma: Pointer to variable to store physical address of allocation.
84 *
a17a75e2
MW
85 * Allocate a contiguous block of memory for use by the driver. This is used to
86 * create the buffers for the slave windows.
b5bc980a
MW
87 *
88 * Return: Virtual address of allocation on success, NULL on failure.
a17a75e2 89 */
ead1f3e3 90void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
a17a75e2
MW
91 dma_addr_t *dma)
92{
93 struct vme_bridge *bridge;
a17a75e2 94
ead1f3e3
MW
95 if (resource == NULL) {
96 printk(KERN_ERR "No resource\n");
a17a75e2
MW
97 return NULL;
98 }
99
100 bridge = find_bridge(resource);
ead1f3e3
MW
101 if (bridge == NULL) {
102 printk(KERN_ERR "Can't find bridge\n");
a17a75e2
MW
103 return NULL;
104 }
105
a17a75e2 106 if (bridge->parent == NULL) {
25958ce3 107 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
7f58f025
MV
108 return NULL;
109 }
110
111 if (bridge->alloc_consistent == NULL) {
25958ce3
GKH
112 printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
113 bridge->name);
a17a75e2
MW
114 return NULL;
115 }
a17a75e2 116
7f58f025 117 return bridge->alloc_consistent(bridge->parent, size, dma);
a17a75e2
MW
118}
119EXPORT_SYMBOL(vme_alloc_consistent);
120
b5bc980a
MW
121/**
122 * vme_free_consistent - Free previously allocated memory.
123 * @resource: Pointer to VME resource.
124 * @size: Size of allocation to free.
125 * @vaddr: Virtual address of allocation.
126 * @dma: Physical address of allocation.
127 *
128 * Free previously allocated block of contiguous memory.
a17a75e2
MW
129 */
130void vme_free_consistent(struct vme_resource *resource, size_t size,
131 void *vaddr, dma_addr_t dma)
132{
133 struct vme_bridge *bridge;
a17a75e2 134
ead1f3e3
MW
135 if (resource == NULL) {
136 printk(KERN_ERR "No resource\n");
a17a75e2
MW
137 return;
138 }
139
140 bridge = find_bridge(resource);
ead1f3e3
MW
141 if (bridge == NULL) {
142 printk(KERN_ERR "Can't find bridge\n");
a17a75e2
MW
143 return;
144 }
145
7f58f025 146 if (bridge->parent == NULL) {
25958ce3 147 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
7f58f025
MV
148 return;
149 }
150
151 if (bridge->free_consistent == NULL) {
25958ce3
GKH
152 printk(KERN_ERR "free_consistent not supported by bridge %s\n",
153 bridge->name);
7f58f025
MV
154 return;
155 }
a17a75e2 156
7f58f025 157 bridge->free_consistent(bridge->parent, size, vaddr, dma);
a17a75e2
MW
158}
159EXPORT_SYMBOL(vme_free_consistent);
160
b5bc980a
MW
161/**
162 * vme_get_size - Helper function returning size of a VME window
163 * @resource: Pointer to VME slave or master resource.
164 *
165 * Determine the size of the VME window provided. This is a helper
166 * function, wrappering the call to vme_master_get or vme_slave_get
167 * depending on the type of window resource handed to it.
168 *
169 * Return: Size of the window on success, zero on failure.
170 */
a17a75e2
MW
171size_t vme_get_size(struct vme_resource *resource)
172{
173 int enabled, retval;
174 unsigned long long base, size;
175 dma_addr_t buf_base;
6af04b06 176 u32 aspace, cycle, dwidth;
a17a75e2
MW
177
178 switch (resource->type) {
179 case VME_MASTER:
180 retval = vme_master_get(resource, &enabled, &base, &size,
181 &aspace, &cycle, &dwidth);
6ad37567
MW
182 if (retval)
183 return 0;
a17a75e2
MW
184
185 return size;
186 break;
187 case VME_SLAVE:
188 retval = vme_slave_get(resource, &enabled, &base, &size,
189 &buf_base, &aspace, &cycle);
6ad37567
MW
190 if (retval)
191 return 0;
a17a75e2
MW
192
193 return size;
194 break;
195 case VME_DMA:
196 return 0;
197 break;
198 default:
199 printk(KERN_ERR "Unknown resource type\n");
200 return 0;
201 break;
202 }
203}
204EXPORT_SYMBOL(vme_get_size);
205
ef73f886
DK
206int vme_check_window(u32 aspace, unsigned long long vme_base,
207 unsigned long long size)
a17a75e2
MW
208{
209 int retval = 0;
210
211 switch (aspace) {
212 case VME_A16:
213 if (((vme_base + size) > VME_A16_MAX) ||
214 (vme_base > VME_A16_MAX))
215 retval = -EFAULT;
216 break;
217 case VME_A24:
218 if (((vme_base + size) > VME_A24_MAX) ||
219 (vme_base > VME_A24_MAX))
220 retval = -EFAULT;
221 break;
222 case VME_A32:
223 if (((vme_base + size) > VME_A32_MAX) ||
224 (vme_base > VME_A32_MAX))
225 retval = -EFAULT;
226 break;
227 case VME_A64:
e7fd80cb
DK
228 if ((size != 0) && (vme_base > U64_MAX + 1 - size))
229 retval = -EFAULT;
a17a75e2
MW
230 break;
231 case VME_CRCSR:
232 if (((vme_base + size) > VME_CRCSR_MAX) ||
233 (vme_base > VME_CRCSR_MAX))
234 retval = -EFAULT;
235 break;
236 case VME_USER1:
237 case VME_USER2:
238 case VME_USER3:
239 case VME_USER4:
240 /* User Defined */
241 break;
242 default:
ead1f3e3 243 printk(KERN_ERR "Invalid address space\n");
a17a75e2
MW
244 retval = -EINVAL;
245 break;
246 }
247
248 return retval;
249}
ef73f886 250EXPORT_SYMBOL(vme_check_window);
a17a75e2 251
472f16f3
DK
252static u32 vme_get_aspace(int am)
253{
254 switch (am) {
255 case 0x29:
256 case 0x2D:
257 return VME_A16;
258 case 0x38:
259 case 0x39:
260 case 0x3A:
261 case 0x3B:
262 case 0x3C:
263 case 0x3D:
264 case 0x3E:
265 case 0x3F:
266 return VME_A24;
267 case 0x8:
268 case 0x9:
269 case 0xA:
270 case 0xB:
271 case 0xC:
272 case 0xD:
273 case 0xE:
274 case 0xF:
275 return VME_A32;
276 case 0x0:
277 case 0x1:
278 case 0x3:
279 return VME_A64;
280 }
281
282 return 0;
283}
284
b5bc980a
MW
285/**
286 * vme_slave_request - Request a VME slave window resource.
287 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
288 * @address: Required VME address space.
289 * @cycle: Required VME data transfer cycle type.
290 *
291 * Request use of a VME window resource capable of being set for the requested
292 * address space and data transfer cycle.
293 *
294 * Return: Pointer to VME resource on success, NULL on failure.
a17a75e2 295 */
6af04b06
MW
296struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
297 u32 cycle)
a17a75e2
MW
298{
299 struct vme_bridge *bridge;
300 struct list_head *slave_pos = NULL;
301 struct vme_slave_resource *allocated_image = NULL;
302 struct vme_slave_resource *slave_image = NULL;
303 struct vme_resource *resource = NULL;
304
8f966dc4 305 bridge = vdev->bridge;
a17a75e2
MW
306 if (bridge == NULL) {
307 printk(KERN_ERR "Can't find VME bus\n");
308 goto err_bus;
309 }
310
311 /* Loop through slave resources */
886953e9 312 list_for_each(slave_pos, &bridge->slave_resources) {
a17a75e2
MW
313 slave_image = list_entry(slave_pos,
314 struct vme_slave_resource, list);
315
316 if (slave_image == NULL) {
ead1f3e3 317 printk(KERN_ERR "Registered NULL Slave resource\n");
a17a75e2
MW
318 continue;
319 }
320
321 /* Find an unlocked and compatible image */
886953e9 322 mutex_lock(&slave_image->mtx);
ead1f3e3 323 if (((slave_image->address_attr & address) == address) &&
a17a75e2
MW
324 ((slave_image->cycle_attr & cycle) == cycle) &&
325 (slave_image->locked == 0)) {
326
327 slave_image->locked = 1;
886953e9 328 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
329 allocated_image = slave_image;
330 break;
331 }
886953e9 332 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
333 }
334
335 /* No free image */
336 if (allocated_image == NULL)
337 goto err_image;
338
1ff0a19c 339 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
94eefcc1 340 if (!resource)
a17a75e2 341 goto err_alloc;
94eefcc1 342
a17a75e2 343 resource->type = VME_SLAVE;
886953e9 344 resource->entry = &allocated_image->list;
a17a75e2
MW
345
346 return resource;
347
348err_alloc:
349 /* Unlock image */
886953e9 350 mutex_lock(&slave_image->mtx);
a17a75e2 351 slave_image->locked = 0;
886953e9 352 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
353err_image:
354err_bus:
355 return NULL;
356}
357EXPORT_SYMBOL(vme_slave_request);
358
b5bc980a
MW
359/**
360 * vme_slave_set - Set VME slave window configuration.
361 * @resource: Pointer to VME slave resource.
362 * @enabled: State to which the window should be configured.
363 * @vme_base: Base address for the window.
364 * @size: Size of the VME window.
365 * @buf_base: Based address of buffer used to provide VME slave window storage.
366 * @aspace: VME address space for the VME window.
367 * @cycle: VME data transfer cycle type for the VME window.
368 *
369 * Set configuration for provided VME slave window.
370 *
371 * Return: Zero on success, -EINVAL if operation is not supported on this
372 * device, if an invalid resource has been provided or invalid
373 * attributes are provided. Hardware specific errors may also be
374 * returned.
375 */
ead1f3e3 376int vme_slave_set(struct vme_resource *resource, int enabled,
a17a75e2 377 unsigned long long vme_base, unsigned long long size,
6af04b06 378 dma_addr_t buf_base, u32 aspace, u32 cycle)
a17a75e2
MW
379{
380 struct vme_bridge *bridge = find_bridge(resource);
381 struct vme_slave_resource *image;
382 int retval;
383
384 if (resource->type != VME_SLAVE) {
ead1f3e3 385 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
386 return -EINVAL;
387 }
388
389 image = list_entry(resource->entry, struct vme_slave_resource, list);
390
391 if (bridge->slave_set == NULL) {
ead1f3e3 392 printk(KERN_ERR "Function not supported\n");
a17a75e2
MW
393 return -ENOSYS;
394 }
395
ead1f3e3 396 if (!(((image->address_attr & aspace) == aspace) &&
a17a75e2 397 ((image->cycle_attr & cycle) == cycle))) {
ead1f3e3 398 printk(KERN_ERR "Invalid attributes\n");
a17a75e2
MW
399 return -EINVAL;
400 }
401
402 retval = vme_check_window(aspace, vme_base, size);
ead1f3e3 403 if (retval)
a17a75e2
MW
404 return retval;
405
406 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
407 aspace, cycle);
408}
409EXPORT_SYMBOL(vme_slave_set);
410
b5bc980a
MW
411/**
412 * vme_slave_get - Retrieve VME slave window configuration.
413 * @resource: Pointer to VME slave resource.
414 * @enabled: Pointer to variable for storing state.
415 * @vme_base: Pointer to variable for storing window base address.
416 * @size: Pointer to variable for storing window size.
417 * @buf_base: Pointer to variable for storing slave buffer base address.
418 * @aspace: Pointer to variable for storing VME address space.
419 * @cycle: Pointer to variable for storing VME data transfer cycle type.
420 *
421 * Return configuration for provided VME slave window.
422 *
423 * Return: Zero on success, -EINVAL if operation is not supported on this
424 * device or if an invalid resource has been provided.
425 */
ead1f3e3 426int vme_slave_get(struct vme_resource *resource, int *enabled,
a17a75e2 427 unsigned long long *vme_base, unsigned long long *size,
6af04b06 428 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
a17a75e2
MW
429{
430 struct vme_bridge *bridge = find_bridge(resource);
431 struct vme_slave_resource *image;
432
433 if (resource->type != VME_SLAVE) {
ead1f3e3 434 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
435 return -EINVAL;
436 }
437
438 image = list_entry(resource->entry, struct vme_slave_resource, list);
439
51a569f7 440 if (bridge->slave_get == NULL) {
ead1f3e3 441 printk(KERN_ERR "vme_slave_get not supported\n");
a17a75e2
MW
442 return -EINVAL;
443 }
444
445 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
446 aspace, cycle);
447}
448EXPORT_SYMBOL(vme_slave_get);
449
b5bc980a
MW
450/**
451 * vme_slave_free - Free VME slave window
452 * @resource: Pointer to VME slave resource.
453 *
454 * Free the provided slave resource so that it may be reallocated.
455 */
a17a75e2
MW
456void vme_slave_free(struct vme_resource *resource)
457{
458 struct vme_slave_resource *slave_image;
459
460 if (resource->type != VME_SLAVE) {
ead1f3e3 461 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
462 return;
463 }
464
465 slave_image = list_entry(resource->entry, struct vme_slave_resource,
466 list);
467 if (slave_image == NULL) {
ead1f3e3 468 printk(KERN_ERR "Can't find slave resource\n");
a17a75e2
MW
469 return;
470 }
471
472 /* Unlock image */
886953e9 473 mutex_lock(&slave_image->mtx);
a17a75e2
MW
474 if (slave_image->locked == 0)
475 printk(KERN_ERR "Image is already free\n");
476
477 slave_image->locked = 0;
886953e9 478 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
479
480 /* Free up resource memory */
481 kfree(resource);
482}
483EXPORT_SYMBOL(vme_slave_free);
484
b5bc980a
MW
485/**
486 * vme_master_request - Request a VME master window resource.
487 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
488 * @address: Required VME address space.
489 * @cycle: Required VME data transfer cycle type.
490 * @dwidth: Required VME data transfer width.
491 *
492 * Request use of a VME window resource capable of being set for the requested
493 * address space, data transfer cycle and width.
494 *
495 * Return: Pointer to VME resource on success, NULL on failure.
a17a75e2 496 */
6af04b06
MW
497struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
498 u32 cycle, u32 dwidth)
a17a75e2
MW
499{
500 struct vme_bridge *bridge;
501 struct list_head *master_pos = NULL;
502 struct vme_master_resource *allocated_image = NULL;
503 struct vme_master_resource *master_image = NULL;
504 struct vme_resource *resource = NULL;
505
8f966dc4 506 bridge = vdev->bridge;
a17a75e2
MW
507 if (bridge == NULL) {
508 printk(KERN_ERR "Can't find VME bus\n");
509 goto err_bus;
510 }
511
512 /* Loop through master resources */
886953e9 513 list_for_each(master_pos, &bridge->master_resources) {
a17a75e2
MW
514 master_image = list_entry(master_pos,
515 struct vme_master_resource, list);
516
517 if (master_image == NULL) {
518 printk(KERN_WARNING "Registered NULL master resource\n");
519 continue;
520 }
521
522 /* Find an unlocked and compatible image */
886953e9 523 spin_lock(&master_image->lock);
ead1f3e3 524 if (((master_image->address_attr & address) == address) &&
a17a75e2
MW
525 ((master_image->cycle_attr & cycle) == cycle) &&
526 ((master_image->width_attr & dwidth) == dwidth) &&
527 (master_image->locked == 0)) {
528
529 master_image->locked = 1;
886953e9 530 spin_unlock(&master_image->lock);
a17a75e2
MW
531 allocated_image = master_image;
532 break;
533 }
886953e9 534 spin_unlock(&master_image->lock);
a17a75e2
MW
535 }
536
537 /* Check to see if we found a resource */
538 if (allocated_image == NULL) {
539 printk(KERN_ERR "Can't find a suitable resource\n");
540 goto err_image;
541 }
542
1ff0a19c 543 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
94eefcc1 544 if (!resource)
a17a75e2 545 goto err_alloc;
94eefcc1 546
a17a75e2 547 resource->type = VME_MASTER;
886953e9 548 resource->entry = &allocated_image->list;
a17a75e2
MW
549
550 return resource;
551
a17a75e2
MW
552err_alloc:
553 /* Unlock image */
886953e9 554 spin_lock(&master_image->lock);
a17a75e2 555 master_image->locked = 0;
886953e9 556 spin_unlock(&master_image->lock);
a17a75e2
MW
557err_image:
558err_bus:
559 return NULL;
560}
561EXPORT_SYMBOL(vme_master_request);
562
b5bc980a
MW
563/**
564 * vme_master_set - Set VME master window configuration.
565 * @resource: Pointer to VME master resource.
566 * @enabled: State to which the window should be configured.
567 * @vme_base: Base address for the window.
568 * @size: Size of the VME window.
569 * @aspace: VME address space for the VME window.
570 * @cycle: VME data transfer cycle type for the VME window.
571 * @dwidth: VME data transfer width for the VME window.
572 *
573 * Set configuration for provided VME master window.
574 *
575 * Return: Zero on success, -EINVAL if operation is not supported on this
576 * device, if an invalid resource has been provided or invalid
577 * attributes are provided. Hardware specific errors may also be
578 * returned.
579 */
ead1f3e3 580int vme_master_set(struct vme_resource *resource, int enabled,
6af04b06
MW
581 unsigned long long vme_base, unsigned long long size, u32 aspace,
582 u32 cycle, u32 dwidth)
a17a75e2
MW
583{
584 struct vme_bridge *bridge = find_bridge(resource);
585 struct vme_master_resource *image;
586 int retval;
587
588 if (resource->type != VME_MASTER) {
ead1f3e3 589 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
590 return -EINVAL;
591 }
592
593 image = list_entry(resource->entry, struct vme_master_resource, list);
594
595 if (bridge->master_set == NULL) {
ead1f3e3 596 printk(KERN_WARNING "vme_master_set not supported\n");
a17a75e2
MW
597 return -EINVAL;
598 }
599
ead1f3e3 600 if (!(((image->address_attr & aspace) == aspace) &&
a17a75e2
MW
601 ((image->cycle_attr & cycle) == cycle) &&
602 ((image->width_attr & dwidth) == dwidth))) {
ead1f3e3 603 printk(KERN_WARNING "Invalid attributes\n");
a17a75e2
MW
604 return -EINVAL;
605 }
606
607 retval = vme_check_window(aspace, vme_base, size);
ead1f3e3 608 if (retval)
a17a75e2
MW
609 return retval;
610
611 return bridge->master_set(image, enabled, vme_base, size, aspace,
612 cycle, dwidth);
613}
614EXPORT_SYMBOL(vme_master_set);
615
b5bc980a
MW
616/**
617 * vme_master_get - Retrieve VME master window configuration.
618 * @resource: Pointer to VME master resource.
619 * @enabled: Pointer to variable for storing state.
620 * @vme_base: Pointer to variable for storing window base address.
621 * @size: Pointer to variable for storing window size.
622 * @aspace: Pointer to variable for storing VME address space.
623 * @cycle: Pointer to variable for storing VME data transfer cycle type.
624 * @dwidth: Pointer to variable for storing VME data transfer width.
625 *
626 * Return configuration for provided VME master window.
627 *
628 * Return: Zero on success, -EINVAL if operation is not supported on this
629 * device or if an invalid resource has been provided.
630 */
ead1f3e3 631int vme_master_get(struct vme_resource *resource, int *enabled,
6af04b06
MW
632 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
633 u32 *cycle, u32 *dwidth)
a17a75e2
MW
634{
635 struct vme_bridge *bridge = find_bridge(resource);
636 struct vme_master_resource *image;
637
638 if (resource->type != VME_MASTER) {
ead1f3e3 639 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
640 return -EINVAL;
641 }
642
643 image = list_entry(resource->entry, struct vme_master_resource, list);
644
51a569f7 645 if (bridge->master_get == NULL) {
c1038307 646 printk(KERN_WARNING "%s not supported\n", __func__);
a17a75e2
MW
647 return -EINVAL;
648 }
649
650 return bridge->master_get(image, enabled, vme_base, size, aspace,
651 cycle, dwidth);
652}
653EXPORT_SYMBOL(vme_master_get);
654
b5bc980a
MW
655/**
656 * vme_master_write - Read data from VME space into a buffer.
657 * @resource: Pointer to VME master resource.
658 * @buf: Pointer to buffer where data should be transferred.
659 * @count: Number of bytes to transfer.
660 * @offset: Offset into VME master window at which to start transfer.
661 *
662 * Perform read of count bytes of data from location on VME bus which maps into
663 * the VME master window at offset to buf.
664 *
665 * Return: Number of bytes read, -EINVAL if resource is not a VME master
666 * resource or read operation is not supported. -EFAULT returned if
667 * invalid offset is provided. Hardware specific errors may also be
668 * returned.
a17a75e2 669 */
ead1f3e3 670ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
a17a75e2
MW
671 loff_t offset)
672{
673 struct vme_bridge *bridge = find_bridge(resource);
674 struct vme_master_resource *image;
675 size_t length;
676
677 if (bridge->master_read == NULL) {
ead1f3e3 678 printk(KERN_WARNING "Reading from resource not supported\n");
a17a75e2
MW
679 return -EINVAL;
680 }
681
682 if (resource->type != VME_MASTER) {
ead1f3e3 683 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
684 return -EINVAL;
685 }
686
687 image = list_entry(resource->entry, struct vme_master_resource, list);
688
689 length = vme_get_size(resource);
690
691 if (offset > length) {
ead1f3e3 692 printk(KERN_WARNING "Invalid Offset\n");
a17a75e2
MW
693 return -EFAULT;
694 }
695
696 if ((offset + count) > length)
697 count = length - offset;
698
699 return bridge->master_read(image, buf, count, offset);
700
701}
702EXPORT_SYMBOL(vme_master_read);
703
b5bc980a
MW
704/**
705 * vme_master_write - Write data out to VME space from a buffer.
706 * @resource: Pointer to VME master resource.
707 * @buf: Pointer to buffer holding data to transfer.
708 * @count: Number of bytes to transfer.
709 * @offset: Offset into VME master window at which to start transfer.
710 *
711 * Perform write of count bytes of data from buf to location on VME bus which
712 * maps into the VME master window at offset.
713 *
714 * Return: Number of bytes written, -EINVAL if resource is not a VME master
715 * resource or write operation is not supported. -EFAULT returned if
716 * invalid offset is provided. Hardware specific errors may also be
717 * returned.
a17a75e2 718 */
ead1f3e3 719ssize_t vme_master_write(struct vme_resource *resource, void *buf,
a17a75e2
MW
720 size_t count, loff_t offset)
721{
722 struct vme_bridge *bridge = find_bridge(resource);
723 struct vme_master_resource *image;
724 size_t length;
725
726 if (bridge->master_write == NULL) {
ead1f3e3 727 printk(KERN_WARNING "Writing to resource not supported\n");
a17a75e2
MW
728 return -EINVAL;
729 }
730
731 if (resource->type != VME_MASTER) {
ead1f3e3 732 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
733 return -EINVAL;
734 }
735
736 image = list_entry(resource->entry, struct vme_master_resource, list);
737
738 length = vme_get_size(resource);
739
740 if (offset > length) {
ead1f3e3 741 printk(KERN_WARNING "Invalid Offset\n");
a17a75e2
MW
742 return -EFAULT;
743 }
744
745 if ((offset + count) > length)
746 count = length - offset;
747
748 return bridge->master_write(image, buf, count, offset);
749}
750EXPORT_SYMBOL(vme_master_write);
751
b5bc980a
MW
752/**
753 * vme_master_rmw - Perform read-modify-write cycle.
754 * @resource: Pointer to VME master resource.
755 * @mask: Bits to be compared and swapped in operation.
756 * @compare: Bits to be compared with data read from offset.
757 * @swap: Bits to be swapped in data read from offset.
758 * @offset: Offset into VME master window at which to perform operation.
759 *
760 * Perform read-modify-write cycle on provided location:
761 * - Location on VME bus is read.
762 * - Bits selected by mask are compared with compare.
763 * - Where a selected bit matches that in compare and are selected in swap,
764 * the bit is swapped.
765 * - Result written back to location on VME bus.
766 *
767 * Return: Bytes written on success, -EINVAL if resource is not a VME master
768 * resource or RMW operation is not supported. Hardware specific
769 * errors may also be returned.
a17a75e2 770 */
ead1f3e3 771unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
a17a75e2
MW
772 unsigned int compare, unsigned int swap, loff_t offset)
773{
774 struct vme_bridge *bridge = find_bridge(resource);
775 struct vme_master_resource *image;
776
777 if (bridge->master_rmw == NULL) {
ead1f3e3 778 printk(KERN_WARNING "Writing to resource not supported\n");
a17a75e2
MW
779 return -EINVAL;
780 }
781
782 if (resource->type != VME_MASTER) {
ead1f3e3 783 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
784 return -EINVAL;
785 }
786
787 image = list_entry(resource->entry, struct vme_master_resource, list);
788
789 return bridge->master_rmw(image, mask, compare, swap, offset);
790}
791EXPORT_SYMBOL(vme_master_rmw);
792
b5bc980a
MW
793/**
794 * vme_master_mmap - Mmap region of VME master window.
795 * @resource: Pointer to VME master resource.
796 * @vma: Pointer to definition of user mapping.
797 *
798 * Memory map a region of the VME master window into user space.
799 *
800 * Return: Zero on success, -EINVAL if resource is not a VME master
801 * resource or -EFAULT if map exceeds window size. Other generic mmap
802 * errors may also be returned.
803 */
c74a804f
DK
804int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
805{
806 struct vme_master_resource *image;
807 phys_addr_t phys_addr;
808 unsigned long vma_size;
809
810 if (resource->type != VME_MASTER) {
811 pr_err("Not a master resource\n");
812 return -EINVAL;
813 }
814
815 image = list_entry(resource->entry, struct vme_master_resource, list);
816 phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
817 vma_size = vma->vm_end - vma->vm_start;
818
819 if (phys_addr + vma_size > image->bus_resource.end + 1) {
820 pr_err("Map size cannot exceed the window size\n");
821 return -EFAULT;
822 }
823
824 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
825
826 return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
827}
828EXPORT_SYMBOL(vme_master_mmap);
829
b5bc980a
MW
830/**
831 * vme_master_free - Free VME master window
832 * @resource: Pointer to VME master resource.
833 *
834 * Free the provided master resource so that it may be reallocated.
835 */
a17a75e2
MW
836void vme_master_free(struct vme_resource *resource)
837{
838 struct vme_master_resource *master_image;
839
840 if (resource->type != VME_MASTER) {
ead1f3e3 841 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
842 return;
843 }
844
845 master_image = list_entry(resource->entry, struct vme_master_resource,
846 list);
847 if (master_image == NULL) {
ead1f3e3 848 printk(KERN_ERR "Can't find master resource\n");
a17a75e2
MW
849 return;
850 }
851
852 /* Unlock image */
886953e9 853 spin_lock(&master_image->lock);
a17a75e2
MW
854 if (master_image->locked == 0)
855 printk(KERN_ERR "Image is already free\n");
856
857 master_image->locked = 0;
886953e9 858 spin_unlock(&master_image->lock);
a17a75e2
MW
859
860 /* Free up resource memory */
861 kfree(resource);
862}
863EXPORT_SYMBOL(vme_master_free);
864
b5bc980a
MW
865/**
866 * vme_dma_request - Request a DMA controller.
867 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
868 * @route: Required src/destination combination.
869 *
870 * Request a VME DMA controller with capability to perform transfers bewteen
871 * requested source/destination combination.
872 *
873 * Return: Pointer to VME DMA resource on success, NULL on failure.
a17a75e2 874 */
6af04b06 875struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
a17a75e2
MW
876{
877 struct vme_bridge *bridge;
878 struct list_head *dma_pos = NULL;
879 struct vme_dma_resource *allocated_ctrlr = NULL;
880 struct vme_dma_resource *dma_ctrlr = NULL;
881 struct vme_resource *resource = NULL;
882
883 /* XXX Not checking resource attributes */
884 printk(KERN_ERR "No VME resource Attribute tests done\n");
885
8f966dc4 886 bridge = vdev->bridge;
a17a75e2
MW
887 if (bridge == NULL) {
888 printk(KERN_ERR "Can't find VME bus\n");
889 goto err_bus;
890 }
891
892 /* Loop through DMA resources */
886953e9 893 list_for_each(dma_pos, &bridge->dma_resources) {
a17a75e2
MW
894 dma_ctrlr = list_entry(dma_pos,
895 struct vme_dma_resource, list);
896
897 if (dma_ctrlr == NULL) {
ead1f3e3 898 printk(KERN_ERR "Registered NULL DMA resource\n");
a17a75e2
MW
899 continue;
900 }
901
4f723df4 902 /* Find an unlocked and compatible controller */
886953e9 903 mutex_lock(&dma_ctrlr->mtx);
4f723df4
MW
904 if (((dma_ctrlr->route_attr & route) == route) &&
905 (dma_ctrlr->locked == 0)) {
906
a17a75e2 907 dma_ctrlr->locked = 1;
886953e9 908 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
909 allocated_ctrlr = dma_ctrlr;
910 break;
911 }
886953e9 912 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
913 }
914
915 /* Check to see if we found a resource */
916 if (allocated_ctrlr == NULL)
917 goto err_ctrlr;
918
1ff0a19c 919 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
94eefcc1 920 if (!resource)
a17a75e2 921 goto err_alloc;
94eefcc1 922
a17a75e2 923 resource->type = VME_DMA;
886953e9 924 resource->entry = &allocated_ctrlr->list;
a17a75e2
MW
925
926 return resource;
927
928err_alloc:
929 /* Unlock image */
886953e9 930 mutex_lock(&dma_ctrlr->mtx);
a17a75e2 931 dma_ctrlr->locked = 0;
886953e9 932 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
933err_ctrlr:
934err_bus:
935 return NULL;
936}
58e50798 937EXPORT_SYMBOL(vme_dma_request);
a17a75e2 938
b5bc980a
MW
939/**
940 * vme_new_dma_list - Create new VME DMA list.
941 * @resource: Pointer to VME DMA resource.
942 *
943 * Create a new VME DMA list. It is the responsibility of the user to free
944 * the list once it is no longer required with vme_dma_list_free().
945 *
946 * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
947 * VME DMA resource.
a17a75e2
MW
948 */
949struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
950{
a17a75e2
MW
951 struct vme_dma_list *dma_list;
952
953 if (resource->type != VME_DMA) {
ead1f3e3 954 printk(KERN_ERR "Not a DMA resource\n");
a17a75e2
MW
955 return NULL;
956 }
957
1ff0a19c 958 dma_list = kmalloc(sizeof(*dma_list), GFP_KERNEL);
94eefcc1 959 if (!dma_list)
a17a75e2 960 return NULL;
94eefcc1 961
886953e9 962 INIT_LIST_HEAD(&dma_list->entries);
a384b2cc
ME
963 dma_list->parent = list_entry(resource->entry,
964 struct vme_dma_resource,
965 list);
886953e9 966 mutex_init(&dma_list->mtx);
a17a75e2
MW
967
968 return dma_list;
969}
970EXPORT_SYMBOL(vme_new_dma_list);
971
b5bc980a
MW
972/**
973 * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
974 * @pattern: Value to use used as pattern
975 * @type: Type of pattern to be written.
976 *
977 * Create VME DMA list attribute for pattern generation. It is the
978 * responsibility of the user to free used attributes using
979 * vme_dma_free_attribute().
980 *
981 * Return: Pointer to VME DMA attribute, NULL on failure.
a17a75e2 982 */
6af04b06 983struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
a17a75e2
MW
984{
985 struct vme_dma_attr *attributes;
986 struct vme_dma_pattern *pattern_attr;
987
1ff0a19c 988 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
94eefcc1 989 if (!attributes)
a17a75e2 990 goto err_attr;
a17a75e2 991
1ff0a19c 992 pattern_attr = kmalloc(sizeof(*pattern_attr), GFP_KERNEL);
94eefcc1 993 if (!pattern_attr)
a17a75e2 994 goto err_pat;
a17a75e2
MW
995
996 attributes->type = VME_DMA_PATTERN;
997 attributes->private = (void *)pattern_attr;
998
999 pattern_attr->pattern = pattern;
1000 pattern_attr->type = type;
1001
1002 return attributes;
1003
a17a75e2
MW
1004err_pat:
1005 kfree(attributes);
1006err_attr:
1007 return NULL;
1008}
1009EXPORT_SYMBOL(vme_dma_pattern_attribute);
1010
b5bc980a
MW
1011/**
1012 * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
1013 * @address: PCI base address for DMA transfer.
1014 *
1015 * Create VME DMA list attribute pointing to a location on PCI for DMA
1016 * transfers. It is the responsibility of the user to free used attributes
1017 * using vme_dma_free_attribute().
1018 *
1019 * Return: Pointer to VME DMA attribute, NULL on failure.
a17a75e2
MW
1020 */
1021struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
1022{
1023 struct vme_dma_attr *attributes;
1024 struct vme_dma_pci *pci_attr;
1025
1026 /* XXX Run some sanity checks here */
1027
1ff0a19c 1028 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
94eefcc1 1029 if (!attributes)
a17a75e2 1030 goto err_attr;
a17a75e2 1031
1ff0a19c 1032 pci_attr = kmalloc(sizeof(*pci_attr), GFP_KERNEL);
94eefcc1 1033 if (!pci_attr)
a17a75e2 1034 goto err_pci;
a17a75e2
MW
1035
1036 attributes->type = VME_DMA_PCI;
1037 attributes->private = (void *)pci_attr;
1038
1039 pci_attr->address = address;
1040
1041 return attributes;
1042
a17a75e2
MW
1043err_pci:
1044 kfree(attributes);
1045err_attr:
1046 return NULL;
1047}
1048EXPORT_SYMBOL(vme_dma_pci_attribute);
1049
b5bc980a
MW
1050/**
1051 * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
1052 * @address: VME base address for DMA transfer.
1053 * @aspace: VME address space to use for DMA transfer.
1054 * @cycle: VME bus cycle to use for DMA transfer.
1055 * @dwidth: VME data width to use for DMA transfer.
1056 *
1057 * Create VME DMA list attribute pointing to a location on the VME bus for DMA
1058 * transfers. It is the responsibility of the user to free used attributes
1059 * using vme_dma_free_attribute().
1060 *
1061 * Return: Pointer to VME DMA attribute, NULL on failure.
a17a75e2
MW
1062 */
1063struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
6af04b06 1064 u32 aspace, u32 cycle, u32 dwidth)
a17a75e2
MW
1065{
1066 struct vme_dma_attr *attributes;
1067 struct vme_dma_vme *vme_attr;
1068
1ff0a19c 1069 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
94eefcc1 1070 if (!attributes)
a17a75e2 1071 goto err_attr;
a17a75e2 1072
1ff0a19c 1073 vme_attr = kmalloc(sizeof(*vme_attr), GFP_KERNEL);
94eefcc1 1074 if (!vme_attr)
a17a75e2 1075 goto err_vme;
a17a75e2
MW
1076
1077 attributes->type = VME_DMA_VME;
1078 attributes->private = (void *)vme_attr;
1079
1080 vme_attr->address = address;
1081 vme_attr->aspace = aspace;
1082 vme_attr->cycle = cycle;
1083 vme_attr->dwidth = dwidth;
1084
1085 return attributes;
1086
a17a75e2
MW
1087err_vme:
1088 kfree(attributes);
1089err_attr:
1090 return NULL;
1091}
1092EXPORT_SYMBOL(vme_dma_vme_attribute);
1093
b5bc980a
MW
1094/**
1095 * vme_dma_free_attribute - Free DMA list attribute.
1096 * @attributes: Pointer to DMA list attribute.
1097 *
1098 * Free VME DMA list attribute. VME DMA list attributes can be safely freed
1099 * once vme_dma_list_add() has returned.
a17a75e2
MW
1100 */
1101void vme_dma_free_attribute(struct vme_dma_attr *attributes)
1102{
1103 kfree(attributes->private);
1104 kfree(attributes);
1105}
1106EXPORT_SYMBOL(vme_dma_free_attribute);
1107
b5bc980a
MW
1108/**
1109 * vme_dma_list_add - Add enty to a VME DMA list.
1110 * @list: Pointer to VME list.
1111 * @src: Pointer to DMA list attribute to use as source.
1112 * @dest: Pointer to DMA list attribute to use as destination.
1113 * @count: Number of bytes to transfer.
1114 *
1115 * Add an entry to the provided VME DMA list. Entry requires pointers to source
1116 * and destination DMA attributes and a count.
1117 *
1118 * Please note, the attributes supported as source and destinations for
1119 * transfers are hardware dependent.
1120 *
1121 * Return: Zero on success, -EINVAL if operation is not supported on this
1122 * device or if the link list has already been submitted for execution.
1123 * Hardware specific errors also possible.
1124 */
a17a75e2
MW
1125int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
1126 struct vme_dma_attr *dest, size_t count)
1127{
1128 struct vme_bridge *bridge = list->parent->parent;
1129 int retval;
1130
1131 if (bridge->dma_list_add == NULL) {
ead1f3e3 1132 printk(KERN_WARNING "Link List DMA generation not supported\n");
a17a75e2
MW
1133 return -EINVAL;
1134 }
1135
886953e9 1136 if (!mutex_trylock(&list->mtx)) {
ead1f3e3 1137 printk(KERN_ERR "Link List already submitted\n");
a17a75e2
MW
1138 return -EINVAL;
1139 }
1140
1141 retval = bridge->dma_list_add(list, src, dest, count);
1142
886953e9 1143 mutex_unlock(&list->mtx);
a17a75e2
MW
1144
1145 return retval;
1146}
1147EXPORT_SYMBOL(vme_dma_list_add);
1148
b5bc980a
MW
1149/**
1150 * vme_dma_list_exec - Queue a VME DMA list for execution.
1151 * @list: Pointer to VME list.
1152 *
1153 * Queue the provided VME DMA list for execution. The call will return once the
1154 * list has been executed.
1155 *
1156 * Return: Zero on success, -EINVAL if operation is not supported on this
1157 * device. Hardware specific errors also possible.
1158 */
a17a75e2
MW
1159int vme_dma_list_exec(struct vme_dma_list *list)
1160{
1161 struct vme_bridge *bridge = list->parent->parent;
1162 int retval;
1163
1164 if (bridge->dma_list_exec == NULL) {
ead1f3e3 1165 printk(KERN_ERR "Link List DMA execution not supported\n");
a17a75e2
MW
1166 return -EINVAL;
1167 }
1168
886953e9 1169 mutex_lock(&list->mtx);
a17a75e2
MW
1170
1171 retval = bridge->dma_list_exec(list);
1172
886953e9 1173 mutex_unlock(&list->mtx);
a17a75e2
MW
1174
1175 return retval;
1176}
1177EXPORT_SYMBOL(vme_dma_list_exec);
1178
b5bc980a
MW
1179/**
1180 * vme_dma_list_free - Free a VME DMA list.
1181 * @list: Pointer to VME list.
1182 *
1183 * Free the provided DMA list and all its entries.
1184 *
1185 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1186 * is still in use. Hardware specific errors also possible.
1187 */
a17a75e2
MW
1188int vme_dma_list_free(struct vme_dma_list *list)
1189{
1190 struct vme_bridge *bridge = list->parent->parent;
1191 int retval;
1192
1193 if (bridge->dma_list_empty == NULL) {
ead1f3e3 1194 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
a17a75e2
MW
1195 return -EINVAL;
1196 }
1197
886953e9 1198 if (!mutex_trylock(&list->mtx)) {
ead1f3e3 1199 printk(KERN_ERR "Link List in use\n");
a17a75e2
MW
1200 return -EINVAL;
1201 }
1202
1203 /*
f56c3d4f
AS
1204 * Empty out all of the entries from the DMA list. We need to go to the
1205 * low level driver as DMA entries are driver specific.
a17a75e2
MW
1206 */
1207 retval = bridge->dma_list_empty(list);
1208 if (retval) {
ead1f3e3 1209 printk(KERN_ERR "Unable to empty link-list entries\n");
886953e9 1210 mutex_unlock(&list->mtx);
a17a75e2
MW
1211 return retval;
1212 }
886953e9 1213 mutex_unlock(&list->mtx);
a17a75e2
MW
1214 kfree(list);
1215
1216 return retval;
1217}
1218EXPORT_SYMBOL(vme_dma_list_free);
1219
b5bc980a
MW
1220/**
1221 * vme_dma_free - Free a VME DMA resource.
1222 * @resource: Pointer to VME DMA resource.
1223 *
1224 * Free the provided DMA resource so that it may be reallocated.
1225 *
1226 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1227 * is still active.
1228 */
a17a75e2
MW
1229int vme_dma_free(struct vme_resource *resource)
1230{
1231 struct vme_dma_resource *ctrlr;
1232
1233 if (resource->type != VME_DMA) {
ead1f3e3 1234 printk(KERN_ERR "Not a DMA resource\n");
a17a75e2
MW
1235 return -EINVAL;
1236 }
1237
1238 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1239
886953e9 1240 if (!mutex_trylock(&ctrlr->mtx)) {
ead1f3e3 1241 printk(KERN_ERR "Resource busy, can't free\n");
a17a75e2
MW
1242 return -EBUSY;
1243 }
1244
886953e9 1245 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
ead1f3e3 1246 printk(KERN_WARNING "Resource still processing transfers\n");
886953e9 1247 mutex_unlock(&ctrlr->mtx);
a17a75e2
MW
1248 return -EBUSY;
1249 }
1250
1251 ctrlr->locked = 0;
1252
886953e9 1253 mutex_unlock(&ctrlr->mtx);
a17a75e2 1254
fd5c2561
MW
1255 kfree(resource);
1256
a17a75e2
MW
1257 return 0;
1258}
1259EXPORT_SYMBOL(vme_dma_free);
1260
e2c6393f 1261void vme_bus_error_handler(struct vme_bridge *bridge,
472f16f3 1262 unsigned long long address, int am)
e2c6393f 1263{
0b049662
DK
1264 struct list_head *handler_pos = NULL;
1265 struct vme_error_handler *handler;
448535a3 1266 int handler_triggered = 0;
0b049662
DK
1267 u32 aspace = vme_get_aspace(am);
1268
1269 list_for_each(handler_pos, &bridge->vme_error_handlers) {
1270 handler = list_entry(handler_pos, struct vme_error_handler,
1271 list);
1272 if ((aspace == handler->aspace) &&
1273 (address >= handler->start) &&
1274 (address < handler->end)) {
1275 if (!handler->num_errors)
1276 handler->first_error = address;
1277 if (handler->num_errors != UINT_MAX)
1278 handler->num_errors++;
448535a3 1279 handler_triggered = 1;
0b049662 1280 }
e2c6393f 1281 }
448535a3
DK
1282
1283 if (!handler_triggered)
1284 dev_err(bridge->parent,
1285 "Unhandled VME access error at address 0x%llx\n",
1286 address);
e2c6393f
DK
1287}
1288EXPORT_SYMBOL(vme_bus_error_handler);
1289
0b049662
DK
1290struct vme_error_handler *vme_register_error_handler(
1291 struct vme_bridge *bridge, u32 aspace,
1292 unsigned long long address, size_t len)
e2c6393f 1293{
0b049662 1294 struct vme_error_handler *handler;
e2c6393f 1295
0b049662
DK
1296 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
1297 if (!handler)
1298 return NULL;
e2c6393f 1299
0b049662
DK
1300 handler->aspace = aspace;
1301 handler->start = address;
1302 handler->end = address + len;
1303 handler->num_errors = 0;
1304 handler->first_error = 0;
1305 list_add_tail(&handler->list, &bridge->vme_error_handlers);
e2c6393f 1306
0b049662 1307 return handler;
e2c6393f 1308}
0b049662 1309EXPORT_SYMBOL(vme_register_error_handler);
e2c6393f 1310
0b049662 1311void vme_unregister_error_handler(struct vme_error_handler *handler)
e2c6393f 1312{
0b049662
DK
1313 list_del(&handler->list);
1314 kfree(handler);
e2c6393f 1315}
0b049662 1316EXPORT_SYMBOL(vme_unregister_error_handler);
e2c6393f 1317
c813f592
MW
1318void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1319{
1320 void (*call)(int, int, void *);
1321 void *priv_data;
1322
1323 call = bridge->irq[level - 1].callback[statid].func;
1324 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
1325
1326 if (call != NULL)
1327 call(level, statid, priv_data);
1328 else
f56c3d4f 1329 printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
25958ce3 1330 level, statid);
c813f592
MW
1331}
1332EXPORT_SYMBOL(vme_irq_handler);
1333
b5bc980a
MW
1334/**
1335 * vme_irq_request - Request a specific VME interrupt.
1336 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1337 * @level: Interrupt priority being requested.
1338 * @statid: Interrupt vector being requested.
1339 * @callback: Pointer to callback function called when VME interrupt/vector
1340 * received.
1341 * @priv_data: Generic pointer that will be passed to the callback function.
1342 *
1343 * Request callback to be attached as a handler for VME interrupts with provided
1344 * level and statid.
1345 *
1346 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1347 * function is not supported, -EBUSY if the level/statid combination is
1348 * already in use. Hardware specific errors also possible.
1349 */
8f966dc4 1350int vme_irq_request(struct vme_dev *vdev, int level, int statid,
29848ac9 1351 void (*callback)(int, int, void *),
a17a75e2
MW
1352 void *priv_data)
1353{
1354 struct vme_bridge *bridge;
1355
8f966dc4 1356 bridge = vdev->bridge;
a17a75e2
MW
1357 if (bridge == NULL) {
1358 printk(KERN_ERR "Can't find VME bus\n");
1359 return -EINVAL;
1360 }
1361
ead1f3e3 1362 if ((level < 1) || (level > 7)) {
c813f592 1363 printk(KERN_ERR "Invalid interrupt level\n");
a17a75e2
MW
1364 return -EINVAL;
1365 }
1366
c813f592
MW
1367 if (bridge->irq_set == NULL) {
1368 printk(KERN_ERR "Configuring interrupts not supported\n");
a17a75e2
MW
1369 return -EINVAL;
1370 }
1371
886953e9 1372 mutex_lock(&bridge->irq_mtx);
c813f592
MW
1373
1374 if (bridge->irq[level - 1].callback[statid].func) {
886953e9 1375 mutex_unlock(&bridge->irq_mtx);
c813f592
MW
1376 printk(KERN_WARNING "VME Interrupt already taken\n");
1377 return -EBUSY;
1378 }
1379
1380 bridge->irq[level - 1].count++;
1381 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1382 bridge->irq[level - 1].callback[statid].func = callback;
1383
1384 /* Enable IRQ level */
29848ac9 1385 bridge->irq_set(bridge, level, 1, 1);
c813f592 1386
886953e9 1387 mutex_unlock(&bridge->irq_mtx);
c813f592
MW
1388
1389 return 0;
a17a75e2 1390}
c813f592 1391EXPORT_SYMBOL(vme_irq_request);
a17a75e2 1392
b5bc980a
MW
1393/**
1394 * vme_irq_free - Free a VME interrupt.
1395 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1396 * @level: Interrupt priority of interrupt being freed.
1397 * @statid: Interrupt vector of interrupt being freed.
1398 *
1399 * Remove previously attached callback from VME interrupt priority/vector.
1400 */
8f966dc4 1401void vme_irq_free(struct vme_dev *vdev, int level, int statid)
a17a75e2
MW
1402{
1403 struct vme_bridge *bridge;
1404
8f966dc4 1405 bridge = vdev->bridge;
a17a75e2
MW
1406 if (bridge == NULL) {
1407 printk(KERN_ERR "Can't find VME bus\n");
1408 return;
1409 }
1410
ead1f3e3 1411 if ((level < 1) || (level > 7)) {
c813f592 1412 printk(KERN_ERR "Invalid interrupt level\n");
a17a75e2
MW
1413 return;
1414 }
1415
c813f592
MW
1416 if (bridge->irq_set == NULL) {
1417 printk(KERN_ERR "Configuring interrupts not supported\n");
a17a75e2
MW
1418 return;
1419 }
1420
886953e9 1421 mutex_lock(&bridge->irq_mtx);
c813f592
MW
1422
1423 bridge->irq[level - 1].count--;
1424
1425 /* Disable IRQ level if no more interrupts attached at this level*/
1426 if (bridge->irq[level - 1].count == 0)
29848ac9 1427 bridge->irq_set(bridge, level, 0, 1);
c813f592
MW
1428
1429 bridge->irq[level - 1].callback[statid].func = NULL;
1430 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1431
886953e9 1432 mutex_unlock(&bridge->irq_mtx);
a17a75e2 1433}
c813f592 1434EXPORT_SYMBOL(vme_irq_free);
a17a75e2 1435
b5bc980a
MW
1436/**
1437 * vme_irq_generate - Generate VME interrupt.
1438 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1439 * @level: Interrupt priority at which to assert the interrupt.
1440 * @statid: Interrupt vector to associate with the interrupt.
1441 *
1442 * Generate a VME interrupt of the provided level and with the provided
1443 * statid.
1444 *
1445 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1446 * function is not supported. Hardware specific errors also possible.
1447 */
8f966dc4 1448int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
a17a75e2
MW
1449{
1450 struct vme_bridge *bridge;
1451
8f966dc4 1452 bridge = vdev->bridge;
a17a75e2
MW
1453 if (bridge == NULL) {
1454 printk(KERN_ERR "Can't find VME bus\n");
1455 return -EINVAL;
1456 }
1457
ead1f3e3 1458 if ((level < 1) || (level > 7)) {
a17a75e2
MW
1459 printk(KERN_WARNING "Invalid interrupt level\n");
1460 return -EINVAL;
1461 }
1462
c813f592 1463 if (bridge->irq_generate == NULL) {
ead1f3e3 1464 printk(KERN_WARNING "Interrupt generation not supported\n");
a17a75e2
MW
1465 return -EINVAL;
1466 }
1467
29848ac9 1468 return bridge->irq_generate(bridge, level, statid);
a17a75e2 1469}
c813f592 1470EXPORT_SYMBOL(vme_irq_generate);
a17a75e2 1471
b5bc980a
MW
1472/**
1473 * vme_lm_request - Request a VME location monitor
1474 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1475 *
1476 * Allocate a location monitor resource to the driver. A location monitor
1477 * allows the driver to monitor accesses to a contiguous number of
1478 * addresses on the VME bus.
1479 *
1480 * Return: Pointer to a VME resource on success or NULL on failure.
42fb5031 1481 */
8f966dc4 1482struct vme_resource *vme_lm_request(struct vme_dev *vdev)
a17a75e2
MW
1483{
1484 struct vme_bridge *bridge;
42fb5031
MW
1485 struct list_head *lm_pos = NULL;
1486 struct vme_lm_resource *allocated_lm = NULL;
1487 struct vme_lm_resource *lm = NULL;
1488 struct vme_resource *resource = NULL;
a17a75e2 1489
8f966dc4 1490 bridge = vdev->bridge;
a17a75e2
MW
1491 if (bridge == NULL) {
1492 printk(KERN_ERR "Can't find VME bus\n");
42fb5031
MW
1493 goto err_bus;
1494 }
1495
b5bc980a 1496 /* Loop through LM resources */
886953e9 1497 list_for_each(lm_pos, &bridge->lm_resources) {
42fb5031
MW
1498 lm = list_entry(lm_pos,
1499 struct vme_lm_resource, list);
1500
1501 if (lm == NULL) {
25958ce3 1502 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
42fb5031
MW
1503 continue;
1504 }
1505
1506 /* Find an unlocked controller */
886953e9 1507 mutex_lock(&lm->mtx);
42fb5031
MW
1508 if (lm->locked == 0) {
1509 lm->locked = 1;
886953e9 1510 mutex_unlock(&lm->mtx);
42fb5031
MW
1511 allocated_lm = lm;
1512 break;
1513 }
886953e9 1514 mutex_unlock(&lm->mtx);
42fb5031
MW
1515 }
1516
1517 /* Check to see if we found a resource */
1518 if (allocated_lm == NULL)
1519 goto err_lm;
1520
1ff0a19c 1521 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
94eefcc1 1522 if (!resource)
42fb5031 1523 goto err_alloc;
94eefcc1 1524
42fb5031 1525 resource->type = VME_LM;
886953e9 1526 resource->entry = &allocated_lm->list;
42fb5031
MW
1527
1528 return resource;
1529
1530err_alloc:
1531 /* Unlock image */
886953e9 1532 mutex_lock(&lm->mtx);
42fb5031 1533 lm->locked = 0;
886953e9 1534 mutex_unlock(&lm->mtx);
42fb5031
MW
1535err_lm:
1536err_bus:
1537 return NULL;
1538}
1539EXPORT_SYMBOL(vme_lm_request);
1540
b5bc980a
MW
1541/**
1542 * vme_lm_count - Determine number of VME Addresses monitored
1543 * @resource: Pointer to VME location monitor resource.
1544 *
1545 * The number of contiguous addresses monitored is hardware dependent.
1546 * Return the number of contiguous addresses monitored by the
1547 * location monitor.
1548 *
1549 * Return: Count of addresses monitored or -EINVAL when provided with an
1550 * invalid location monitor resource.
1551 */
42fb5031
MW
1552int vme_lm_count(struct vme_resource *resource)
1553{
1554 struct vme_lm_resource *lm;
1555
1556 if (resource->type != VME_LM) {
1557 printk(KERN_ERR "Not a Location Monitor resource\n");
1558 return -EINVAL;
1559 }
1560
1561 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1562
1563 return lm->monitors;
1564}
1565EXPORT_SYMBOL(vme_lm_count);
1566
b5bc980a
MW
1567/**
1568 * vme_lm_set - Configure location monitor
1569 * @resource: Pointer to VME location monitor resource.
1570 * @lm_base: Base address to monitor.
1571 * @aspace: VME address space to monitor.
1572 * @cycle: VME bus cycle type to monitor.
1573 *
1574 * Set the base address, address space and cycle type of accesses to be
1575 * monitored by the location monitor.
1576 *
1577 * Return: Zero on success, -EINVAL when provided with an invalid location
1578 * monitor resource or function is not supported. Hardware specific
1579 * errors may also be returned.
1580 */
42fb5031 1581int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
6af04b06 1582 u32 aspace, u32 cycle)
42fb5031
MW
1583{
1584 struct vme_bridge *bridge = find_bridge(resource);
1585 struct vme_lm_resource *lm;
1586
1587 if (resource->type != VME_LM) {
1588 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1589 return -EINVAL;
1590 }
1591
42fb5031
MW
1592 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1593
a17a75e2 1594 if (bridge->lm_set == NULL) {
42fb5031 1595 printk(KERN_ERR "vme_lm_set not supported\n");
a17a75e2
MW
1596 return -EINVAL;
1597 }
1598
8be9226c 1599 return bridge->lm_set(lm, lm_base, aspace, cycle);
a17a75e2
MW
1600}
1601EXPORT_SYMBOL(vme_lm_set);
1602
b5bc980a
MW
1603/**
1604 * vme_lm_get - Retrieve location monitor settings
1605 * @resource: Pointer to VME location monitor resource.
1606 * @lm_base: Pointer used to output the base address monitored.
1607 * @aspace: Pointer used to output the address space monitored.
1608 * @cycle: Pointer used to output the VME bus cycle type monitored.
1609 *
1610 * Retrieve the base address, address space and cycle type of accesses to
1611 * be monitored by the location monitor.
1612 *
1613 * Return: Zero on success, -EINVAL when provided with an invalid location
1614 * monitor resource or function is not supported. Hardware specific
1615 * errors may also be returned.
1616 */
42fb5031 1617int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
6af04b06 1618 u32 *aspace, u32 *cycle)
a17a75e2 1619{
42fb5031
MW
1620 struct vme_bridge *bridge = find_bridge(resource);
1621 struct vme_lm_resource *lm;
a17a75e2 1622
42fb5031
MW
1623 if (resource->type != VME_LM) {
1624 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1625 return -EINVAL;
1626 }
1627
42fb5031
MW
1628 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1629
a17a75e2 1630 if (bridge->lm_get == NULL) {
42fb5031 1631 printk(KERN_ERR "vme_lm_get not supported\n");
a17a75e2
MW
1632 return -EINVAL;
1633 }
1634
42fb5031 1635 return bridge->lm_get(lm, lm_base, aspace, cycle);
a17a75e2
MW
1636}
1637EXPORT_SYMBOL(vme_lm_get);
1638
b5bc980a
MW
1639/**
1640 * vme_lm_attach - Provide callback for location monitor address
1641 * @resource: Pointer to VME location monitor resource.
1642 * @monitor: Offset to which callback should be attached.
1643 * @callback: Pointer to callback function called when triggered.
1644 * @data: Generic pointer that will be passed to the callback function.
1645 *
1646 * Attach a callback to the specificed offset into the location monitors
1647 * monitored addresses. A generic pointer is provided to allow data to be
1648 * passed to the callback when called.
1649 *
1650 * Return: Zero on success, -EINVAL when provided with an invalid location
1651 * monitor resource or function is not supported. Hardware specific
1652 * errors may also be returned.
1653 */
42fb5031 1654int vme_lm_attach(struct vme_resource *resource, int monitor,
fa54b326 1655 void (*callback)(void *), void *data)
a17a75e2 1656{
42fb5031
MW
1657 struct vme_bridge *bridge = find_bridge(resource);
1658 struct vme_lm_resource *lm;
a17a75e2 1659
42fb5031
MW
1660 if (resource->type != VME_LM) {
1661 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1662 return -EINVAL;
1663 }
1664
42fb5031
MW
1665 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1666
a17a75e2 1667 if (bridge->lm_attach == NULL) {
42fb5031 1668 printk(KERN_ERR "vme_lm_attach not supported\n");
a17a75e2
MW
1669 return -EINVAL;
1670 }
1671
fa54b326 1672 return bridge->lm_attach(lm, monitor, callback, data);
a17a75e2
MW
1673}
1674EXPORT_SYMBOL(vme_lm_attach);
1675
b5bc980a
MW
1676/**
1677 * vme_lm_detach - Remove callback for location monitor address
1678 * @resource: Pointer to VME location monitor resource.
1679 * @monitor: Offset to which callback should be removed.
1680 *
1681 * Remove the callback associated with the specificed offset into the
1682 * location monitors monitored addresses.
1683 *
1684 * Return: Zero on success, -EINVAL when provided with an invalid location
1685 * monitor resource or function is not supported. Hardware specific
1686 * errors may also be returned.
1687 */
42fb5031 1688int vme_lm_detach(struct vme_resource *resource, int monitor)
a17a75e2 1689{
42fb5031
MW
1690 struct vme_bridge *bridge = find_bridge(resource);
1691 struct vme_lm_resource *lm;
a17a75e2 1692
42fb5031
MW
1693 if (resource->type != VME_LM) {
1694 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1695 return -EINVAL;
1696 }
1697
42fb5031
MW
1698 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1699
a17a75e2 1700 if (bridge->lm_detach == NULL) {
42fb5031 1701 printk(KERN_ERR "vme_lm_detach not supported\n");
a17a75e2
MW
1702 return -EINVAL;
1703 }
1704
42fb5031 1705 return bridge->lm_detach(lm, monitor);
a17a75e2
MW
1706}
1707EXPORT_SYMBOL(vme_lm_detach);
1708
b5bc980a
MW
1709/**
1710 * vme_lm_free - Free allocated VME location monitor
1711 * @resource: Pointer to VME location monitor resource.
1712 *
1713 * Free allocation of a VME location monitor.
1714 *
1715 * WARNING: This function currently expects that any callbacks that have
1716 * been attached to the location monitor have been removed.
1717 *
1718 * Return: Zero on success, -EINVAL when provided with an invalid location
1719 * monitor resource.
1720 */
42fb5031
MW
1721void vme_lm_free(struct vme_resource *resource)
1722{
1723 struct vme_lm_resource *lm;
1724
1725 if (resource->type != VME_LM) {
1726 printk(KERN_ERR "Not a Location Monitor resource\n");
1727 return;
1728 }
1729
1730 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1731
886953e9 1732 mutex_lock(&lm->mtx);
42fb5031 1733
8be9226c
MW
1734 /* XXX
1735 * Check to see that there aren't any callbacks still attached, if
1736 * there are we should probably be detaching them!
1737 */
42fb5031
MW
1738
1739 lm->locked = 0;
1740
886953e9 1741 mutex_unlock(&lm->mtx);
8be9226c
MW
1742
1743 kfree(resource);
42fb5031
MW
1744}
1745EXPORT_SYMBOL(vme_lm_free);
1746
b5bc980a
MW
1747/**
1748 * vme_slot_num - Retrieve slot ID
1749 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1750 *
1751 * Retrieve the slot ID associated with the provided VME device.
1752 *
1753 * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
1754 * or the function is not supported. Hardware specific errors may also
1755 * be returned.
1756 */
d7729f0f 1757int vme_slot_num(struct vme_dev *vdev)
a17a75e2
MW
1758{
1759 struct vme_bridge *bridge;
1760
8f966dc4 1761 bridge = vdev->bridge;
a17a75e2
MW
1762 if (bridge == NULL) {
1763 printk(KERN_ERR "Can't find VME bus\n");
1764 return -EINVAL;
1765 }
1766
1767 if (bridge->slot_get == NULL) {
d7729f0f 1768 printk(KERN_WARNING "vme_slot_num not supported\n");
a17a75e2
MW
1769 return -EINVAL;
1770 }
1771
29848ac9 1772 return bridge->slot_get(bridge);
a17a75e2 1773}
d7729f0f 1774EXPORT_SYMBOL(vme_slot_num);
a17a75e2 1775
b5bc980a
MW
1776/**
1777 * vme_bus_num - Retrieve bus number
1778 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1779 *
1780 * Retrieve the bus enumeration associated with the provided VME device.
1781 *
1782 * Return: The bus number on success, -EINVAL if VME bridge cannot be
1783 * determined.
1784 */
978f47d6
MW
1785int vme_bus_num(struct vme_dev *vdev)
1786{
1787 struct vme_bridge *bridge;
1788
1789 bridge = vdev->bridge;
1790 if (bridge == NULL) {
1791 pr_err("Can't find VME bus\n");
1792 return -EINVAL;
1793 }
1794
1795 return bridge->num;
1796}
1797EXPORT_SYMBOL(vme_bus_num);
a17a75e2
MW
1798
1799/* - Bridge Registration --------------------------------------------------- */
1800
5b93c2a2
MV
1801static void vme_dev_release(struct device *dev)
1802{
1803 kfree(dev_to_vme_dev(dev));
1804}
1805
326071b3
AS
1806/* Common bridge initialization */
1807struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
1808{
1809 INIT_LIST_HEAD(&bridge->vme_error_handlers);
1810 INIT_LIST_HEAD(&bridge->master_resources);
1811 INIT_LIST_HEAD(&bridge->slave_resources);
1812 INIT_LIST_HEAD(&bridge->dma_resources);
1813 INIT_LIST_HEAD(&bridge->lm_resources);
1814 mutex_init(&bridge->irq_mtx);
1815
1816 return bridge;
1817}
1818EXPORT_SYMBOL(vme_init_bridge);
1819
5b93c2a2 1820int vme_register_bridge(struct vme_bridge *bridge)
a17a75e2
MW
1821{
1822 int i;
733e3ef0 1823 int ret = -1;
a17a75e2 1824
733e3ef0 1825 mutex_lock(&vme_buses_lock);
a17a75e2 1826 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
733e3ef0
MV
1827 if ((vme_bus_numbers & (1 << i)) == 0) {
1828 vme_bus_numbers |= (1 << i);
1829 bridge->num = i;
5d6abf37 1830 INIT_LIST_HEAD(&bridge->devices);
733e3ef0
MV
1831 list_add_tail(&bridge->bus_list, &vme_bus_list);
1832 ret = 0;
a17a75e2
MW
1833 break;
1834 }
1835 }
733e3ef0 1836 mutex_unlock(&vme_buses_lock);
a17a75e2 1837
733e3ef0 1838 return ret;
a17a75e2 1839}
5b93c2a2 1840EXPORT_SYMBOL(vme_register_bridge);
a17a75e2 1841
5b93c2a2 1842void vme_unregister_bridge(struct vme_bridge *bridge)
a17a75e2 1843{
5d6abf37
MV
1844 struct vme_dev *vdev;
1845 struct vme_dev *tmp;
1846
733e3ef0
MV
1847 mutex_lock(&vme_buses_lock);
1848 vme_bus_numbers &= ~(1 << bridge->num);
5d6abf37
MV
1849 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1850 list_del(&vdev->drv_list);
1851 list_del(&vdev->bridge_list);
1852 device_unregister(&vdev->dev);
1853 }
733e3ef0
MV
1854 list_del(&bridge->bus_list);
1855 mutex_unlock(&vme_buses_lock);
a17a75e2 1856}
5d6abf37 1857EXPORT_SYMBOL(vme_unregister_bridge);
a17a75e2 1858
5d6abf37
MV
1859/* - Driver Registration --------------------------------------------------- */
1860
1861static int __vme_register_driver_bus(struct vme_driver *drv,
1862 struct vme_bridge *bridge, unsigned int ndevs)
1863{
1864 int err;
1865 unsigned int i;
1866 struct vme_dev *vdev;
1867 struct vme_dev *tmp;
1868
1869 for (i = 0; i < ndevs; i++) {
1ff0a19c 1870 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
5d6abf37
MV
1871 if (!vdev) {
1872 err = -ENOMEM;
f6c39d4f
MV
1873 goto err_devalloc;
1874 }
a916a391 1875 vdev->num = i;
8f966dc4 1876 vdev->bridge = bridge;
5d6abf37
MV
1877 vdev->dev.platform_data = drv;
1878 vdev->dev.release = vme_dev_release;
8f966dc4
MV
1879 vdev->dev.parent = bridge->parent;
1880 vdev->dev.bus = &vme_bus_type;
a916a391
MV
1881 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1882 vdev->num);
a17a75e2 1883
5d6abf37
MV
1884 err = device_register(&vdev->dev);
1885 if (err)
a17a75e2 1886 goto err_reg;
a17a75e2 1887
5d6abf37
MV
1888 if (vdev->dev.platform_data) {
1889 list_add_tail(&vdev->drv_list, &drv->devices);
1890 list_add_tail(&vdev->bridge_list, &bridge->devices);
1891 } else
1892 device_unregister(&vdev->dev);
1893 }
1894 return 0;
a17a75e2 1895
a17a75e2 1896err_reg:
def1820d 1897 put_device(&vdev->dev);
8f966dc4 1898 kfree(vdev);
f6c39d4f 1899err_devalloc:
5d6abf37
MV
1900 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1901 list_del(&vdev->drv_list);
1902 list_del(&vdev->bridge_list);
8f966dc4 1903 device_unregister(&vdev->dev);
a17a75e2 1904 }
5d6abf37 1905 return err;
a17a75e2 1906}
a17a75e2 1907
5d6abf37 1908static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
a17a75e2 1909{
5d6abf37
MV
1910 struct vme_bridge *bridge;
1911 int err = 0;
a17a75e2 1912
5d6abf37
MV
1913 mutex_lock(&vme_buses_lock);
1914 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1915 /*
1916 * This cannot cause trouble as we already have vme_buses_lock
1917 * and if the bridge is removed, it will have to go through
1918 * vme_unregister_bridge() to do it (which calls remove() on
1919 * the bridge which in turn tries to acquire vme_buses_lock and
c26f6112 1920 * will have to wait).
5d6abf37
MV
1921 */
1922 err = __vme_register_driver_bus(drv, bridge, ndevs);
1923 if (err)
1924 break;
a17a75e2 1925 }
5d6abf37
MV
1926 mutex_unlock(&vme_buses_lock);
1927 return err;
a17a75e2 1928}
a17a75e2 1929
b5bc980a
MW
1930/**
1931 * vme_register_driver - Register a VME driver
1932 * @drv: Pointer to VME driver structure to register.
1933 * @ndevs: Maximum number of devices to allow to be enumerated.
1934 *
1935 * Register a VME device driver with the VME subsystem.
1936 *
1937 * Return: Zero on success, error value on registration failure.
1938 */
5d6abf37 1939int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
a17a75e2 1940{
5d6abf37
MV
1941 int err;
1942
a17a75e2
MW
1943 drv->driver.name = drv->name;
1944 drv->driver.bus = &vme_bus_type;
5d6abf37
MV
1945 INIT_LIST_HEAD(&drv->devices);
1946
1947 err = driver_register(&drv->driver);
1948 if (err)
1949 return err;
a17a75e2 1950
5d6abf37
MV
1951 err = __vme_register_driver(drv, ndevs);
1952 if (err)
1953 driver_unregister(&drv->driver);
1954
1955 return err;
a17a75e2
MW
1956}
1957EXPORT_SYMBOL(vme_register_driver);
1958
b5bc980a
MW
1959/**
1960 * vme_unregister_driver - Unregister a VME driver
1961 * @drv: Pointer to VME driver structure to unregister.
1962 *
1963 * Unregister a VME device driver from the VME subsystem.
1964 */
ead1f3e3 1965void vme_unregister_driver(struct vme_driver *drv)
a17a75e2 1966{
5d6abf37
MV
1967 struct vme_dev *dev, *dev_tmp;
1968
1969 mutex_lock(&vme_buses_lock);
1970 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1971 list_del(&dev->drv_list);
1972 list_del(&dev->bridge_list);
1973 device_unregister(&dev->dev);
1974 }
1975 mutex_unlock(&vme_buses_lock);
1976
a17a75e2
MW
1977 driver_unregister(&drv->driver);
1978}
1979EXPORT_SYMBOL(vme_unregister_driver);
1980
1981/* - Bus Registration ------------------------------------------------------ */
1982
a17a75e2
MW
1983static int vme_bus_match(struct device *dev, struct device_driver *drv)
1984{
5d6abf37 1985 struct vme_driver *vme_drv;
a17a75e2 1986
5d6abf37 1987 vme_drv = container_of(drv, struct vme_driver, driver);
a17a75e2 1988
5d6abf37
MV
1989 if (dev->platform_data == vme_drv) {
1990 struct vme_dev *vdev = dev_to_vme_dev(dev);
a17a75e2 1991
5d6abf37
MV
1992 if (vme_drv->match && vme_drv->match(vdev))
1993 return 1;
a37b0dad 1994
5d6abf37 1995 dev->platform_data = NULL;
a17a75e2 1996 }
a17a75e2
MW
1997 return 0;
1998}
1999
2000static int vme_bus_probe(struct device *dev)
2001{
a17a75e2 2002 int retval = -ENODEV;
5d6abf37
MV
2003 struct vme_driver *driver;
2004 struct vme_dev *vdev = dev_to_vme_dev(dev);
a17a75e2 2005
5d6abf37 2006 driver = dev->platform_data;
a17a75e2 2007
ead1f3e3 2008 if (driver->probe != NULL)
8f966dc4 2009 retval = driver->probe(vdev);
a17a75e2
MW
2010
2011 return retval;
2012}
2013
9797484b
SB
2014static int vme_bus_remove(struct device *dev)
2015{
2016 int retval = -ENODEV;
2017 struct vme_driver *driver;
2018 struct vme_dev *vdev = dev_to_vme_dev(dev);
2019
2020 driver = dev->platform_data;
2021
2022 if (driver->remove != NULL)
2023 retval = driver->remove(vdev);
2024
2025 return retval;
2026}
2027
a17a75e2
MW
2028struct bus_type vme_bus_type = {
2029 .name = "vme",
2030 .match = vme_bus_match,
2031 .probe = vme_bus_probe,
9797484b 2032 .remove = vme_bus_remove,
a17a75e2
MW
2033};
2034EXPORT_SYMBOL(vme_bus_type);
2035
ead1f3e3 2036static int __init vme_init(void)
a17a75e2
MW
2037{
2038 return bus_register(&vme_bus_type);
2039}
c326cc02 2040subsys_initcall(vme_init);
This page took 1.104621 seconds and 4 git commands to generate.