]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/staging/vme/vme.c
staging: vme: add struct vme_dev for VME devices
[mirror_ubuntu-hirsute-kernel.git] / drivers / staging / vme / vme.c
CommitLineData
a17a75e2
MW
1/*
2 * VME Bridge Framework
3 *
66bd8db5
MW
4 * Author: Martyn Welch <martyn.welch@ge.com>
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
a17a75e2
MW
16#include <linux/module.h>
17#include <linux/moduleparam.h>
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>
a17a75e2
MW
33
34#include "vme.h"
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
MW
42static void __exit vme_exit(void);
43static int __init vme_init(void);
a17a75e2 44
8f966dc4 45static struct vme_dev *dev_to_vme_dev(struct device *dev)
a17a75e2 46{
8f966dc4 47 return container_of(dev, struct vme_dev, dev);
a17a75e2
MW
48}
49
50/*
51 * Find the bridge that the resource is associated with.
52 */
53static struct vme_bridge *find_bridge(struct vme_resource *resource)
54{
55 /* Get list to search */
56 switch (resource->type) {
57 case VME_MASTER:
58 return list_entry(resource->entry, struct vme_master_resource,
59 list)->parent;
60 break;
61 case VME_SLAVE:
62 return list_entry(resource->entry, struct vme_slave_resource,
63 list)->parent;
64 break;
65 case VME_DMA:
66 return list_entry(resource->entry, struct vme_dma_resource,
67 list)->parent;
68 break;
42fb5031
MW
69 case VME_LM:
70 return list_entry(resource->entry, struct vme_lm_resource,
71 list)->parent;
72 break;
a17a75e2
MW
73 default:
74 printk(KERN_ERR "Unknown resource type\n");
75 return NULL;
76 break;
77 }
78}
79
80/*
81 * Allocate a contiguous block of memory for use by the driver. This is used to
82 * create the buffers for the slave windows.
a17a75e2 83 */
ead1f3e3 84void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
a17a75e2
MW
85 dma_addr_t *dma)
86{
87 struct vme_bridge *bridge;
a17a75e2 88
ead1f3e3
MW
89 if (resource == NULL) {
90 printk(KERN_ERR "No resource\n");
a17a75e2
MW
91 return NULL;
92 }
93
94 bridge = find_bridge(resource);
ead1f3e3
MW
95 if (bridge == NULL) {
96 printk(KERN_ERR "Can't find bridge\n");
a17a75e2
MW
97 return NULL;
98 }
99
a17a75e2 100 if (bridge->parent == NULL) {
7f58f025
MV
101 printk(KERN_ERR "Dev entry NULL for"
102 " bridge %s\n", bridge->name);
103 return NULL;
104 }
105
106 if (bridge->alloc_consistent == NULL) {
107 printk(KERN_ERR "alloc_consistent not supported by"
108 " bridge %s\n", bridge->name);
a17a75e2
MW
109 return NULL;
110 }
a17a75e2 111
7f58f025 112 return bridge->alloc_consistent(bridge->parent, size, dma);
a17a75e2
MW
113}
114EXPORT_SYMBOL(vme_alloc_consistent);
115
116/*
117 * Free previously allocated contiguous block of memory.
a17a75e2
MW
118 */
119void vme_free_consistent(struct vme_resource *resource, size_t size,
120 void *vaddr, dma_addr_t dma)
121{
122 struct vme_bridge *bridge;
a17a75e2 123
ead1f3e3
MW
124 if (resource == NULL) {
125 printk(KERN_ERR "No resource\n");
a17a75e2
MW
126 return;
127 }
128
129 bridge = find_bridge(resource);
ead1f3e3
MW
130 if (bridge == NULL) {
131 printk(KERN_ERR "Can't find bridge\n");
a17a75e2
MW
132 return;
133 }
134
7f58f025
MV
135 if (bridge->parent == NULL) {
136 printk(KERN_ERR "Dev entry NULL for"
137 " bridge %s\n", bridge->name);
138 return;
139 }
140
141 if (bridge->free_consistent == NULL) {
142 printk(KERN_ERR "free_consistent not supported by"
143 " bridge %s\n", bridge->name);
144 return;
145 }
a17a75e2 146
7f58f025 147 bridge->free_consistent(bridge->parent, size, vaddr, dma);
a17a75e2
MW
148}
149EXPORT_SYMBOL(vme_free_consistent);
150
151size_t vme_get_size(struct vme_resource *resource)
152{
153 int enabled, retval;
154 unsigned long long base, size;
155 dma_addr_t buf_base;
156 vme_address_t aspace;
157 vme_cycle_t cycle;
158 vme_width_t dwidth;
159
160 switch (resource->type) {
161 case VME_MASTER:
162 retval = vme_master_get(resource, &enabled, &base, &size,
163 &aspace, &cycle, &dwidth);
164
165 return size;
166 break;
167 case VME_SLAVE:
168 retval = vme_slave_get(resource, &enabled, &base, &size,
169 &buf_base, &aspace, &cycle);
170
171 return size;
172 break;
173 case VME_DMA:
174 return 0;
175 break;
176 default:
177 printk(KERN_ERR "Unknown resource type\n");
178 return 0;
179 break;
180 }
181}
182EXPORT_SYMBOL(vme_get_size);
183
184static int vme_check_window(vme_address_t aspace, unsigned long long vme_base,
185 unsigned long long size)
186{
187 int retval = 0;
188
189 switch (aspace) {
190 case VME_A16:
191 if (((vme_base + size) > VME_A16_MAX) ||
192 (vme_base > VME_A16_MAX))
193 retval = -EFAULT;
194 break;
195 case VME_A24:
196 if (((vme_base + size) > VME_A24_MAX) ||
197 (vme_base > VME_A24_MAX))
198 retval = -EFAULT;
199 break;
200 case VME_A32:
201 if (((vme_base + size) > VME_A32_MAX) ||
202 (vme_base > VME_A32_MAX))
203 retval = -EFAULT;
204 break;
205 case VME_A64:
206 /*
207 * Any value held in an unsigned long long can be used as the
208 * base
209 */
210 break;
211 case VME_CRCSR:
212 if (((vme_base + size) > VME_CRCSR_MAX) ||
213 (vme_base > VME_CRCSR_MAX))
214 retval = -EFAULT;
215 break;
216 case VME_USER1:
217 case VME_USER2:
218 case VME_USER3:
219 case VME_USER4:
220 /* User Defined */
221 break;
222 default:
ead1f3e3 223 printk(KERN_ERR "Invalid address space\n");
a17a75e2
MW
224 retval = -EINVAL;
225 break;
226 }
227
228 return retval;
229}
230
231/*
232 * Request a slave image with specific attributes, return some unique
233 * identifier.
234 */
8f966dc4 235struct vme_resource *vme_slave_request(struct vme_dev *vdev,
a17a75e2
MW
236 vme_address_t address, vme_cycle_t cycle)
237{
238 struct vme_bridge *bridge;
239 struct list_head *slave_pos = NULL;
240 struct vme_slave_resource *allocated_image = NULL;
241 struct vme_slave_resource *slave_image = NULL;
242 struct vme_resource *resource = NULL;
243
8f966dc4 244 bridge = vdev->bridge;
a17a75e2
MW
245 if (bridge == NULL) {
246 printk(KERN_ERR "Can't find VME bus\n");
247 goto err_bus;
248 }
249
250 /* Loop through slave resources */
886953e9 251 list_for_each(slave_pos, &bridge->slave_resources) {
a17a75e2
MW
252 slave_image = list_entry(slave_pos,
253 struct vme_slave_resource, list);
254
255 if (slave_image == NULL) {
ead1f3e3 256 printk(KERN_ERR "Registered NULL Slave resource\n");
a17a75e2
MW
257 continue;
258 }
259
260 /* Find an unlocked and compatible image */
886953e9 261 mutex_lock(&slave_image->mtx);
ead1f3e3 262 if (((slave_image->address_attr & address) == address) &&
a17a75e2
MW
263 ((slave_image->cycle_attr & cycle) == cycle) &&
264 (slave_image->locked == 0)) {
265
266 slave_image->locked = 1;
886953e9 267 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
268 allocated_image = slave_image;
269 break;
270 }
886953e9 271 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
272 }
273
274 /* No free image */
275 if (allocated_image == NULL)
276 goto err_image;
277
278 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
279 if (resource == NULL) {
280 printk(KERN_WARNING "Unable to allocate resource structure\n");
281 goto err_alloc;
282 }
283 resource->type = VME_SLAVE;
886953e9 284 resource->entry = &allocated_image->list;
a17a75e2
MW
285
286 return resource;
287
288err_alloc:
289 /* Unlock image */
886953e9 290 mutex_lock(&slave_image->mtx);
a17a75e2 291 slave_image->locked = 0;
886953e9 292 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
293err_image:
294err_bus:
295 return NULL;
296}
297EXPORT_SYMBOL(vme_slave_request);
298
ead1f3e3 299int vme_slave_set(struct vme_resource *resource, int enabled,
a17a75e2
MW
300 unsigned long long vme_base, unsigned long long size,
301 dma_addr_t buf_base, vme_address_t aspace, vme_cycle_t cycle)
302{
303 struct vme_bridge *bridge = find_bridge(resource);
304 struct vme_slave_resource *image;
305 int retval;
306
307 if (resource->type != VME_SLAVE) {
ead1f3e3 308 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
309 return -EINVAL;
310 }
311
312 image = list_entry(resource->entry, struct vme_slave_resource, list);
313
314 if (bridge->slave_set == NULL) {
ead1f3e3 315 printk(KERN_ERR "Function not supported\n");
a17a75e2
MW
316 return -ENOSYS;
317 }
318
ead1f3e3 319 if (!(((image->address_attr & aspace) == aspace) &&
a17a75e2 320 ((image->cycle_attr & cycle) == cycle))) {
ead1f3e3 321 printk(KERN_ERR "Invalid attributes\n");
a17a75e2
MW
322 return -EINVAL;
323 }
324
325 retval = vme_check_window(aspace, vme_base, size);
ead1f3e3 326 if (retval)
a17a75e2
MW
327 return retval;
328
329 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
330 aspace, cycle);
331}
332EXPORT_SYMBOL(vme_slave_set);
333
ead1f3e3 334int vme_slave_get(struct vme_resource *resource, int *enabled,
a17a75e2
MW
335 unsigned long long *vme_base, unsigned long long *size,
336 dma_addr_t *buf_base, vme_address_t *aspace, vme_cycle_t *cycle)
337{
338 struct vme_bridge *bridge = find_bridge(resource);
339 struct vme_slave_resource *image;
340
341 if (resource->type != VME_SLAVE) {
ead1f3e3 342 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
343 return -EINVAL;
344 }
345
346 image = list_entry(resource->entry, struct vme_slave_resource, list);
347
51a569f7 348 if (bridge->slave_get == NULL) {
ead1f3e3 349 printk(KERN_ERR "vme_slave_get not supported\n");
a17a75e2
MW
350 return -EINVAL;
351 }
352
353 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
354 aspace, cycle);
355}
356EXPORT_SYMBOL(vme_slave_get);
357
358void vme_slave_free(struct vme_resource *resource)
359{
360 struct vme_slave_resource *slave_image;
361
362 if (resource->type != VME_SLAVE) {
ead1f3e3 363 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
364 return;
365 }
366
367 slave_image = list_entry(resource->entry, struct vme_slave_resource,
368 list);
369 if (slave_image == NULL) {
ead1f3e3 370 printk(KERN_ERR "Can't find slave resource\n");
a17a75e2
MW
371 return;
372 }
373
374 /* Unlock image */
886953e9 375 mutex_lock(&slave_image->mtx);
a17a75e2
MW
376 if (slave_image->locked == 0)
377 printk(KERN_ERR "Image is already free\n");
378
379 slave_image->locked = 0;
886953e9 380 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
381
382 /* Free up resource memory */
383 kfree(resource);
384}
385EXPORT_SYMBOL(vme_slave_free);
386
387/*
388 * Request a master image with specific attributes, return some unique
389 * identifier.
390 */
8f966dc4 391struct vme_resource *vme_master_request(struct vme_dev *vdev,
a17a75e2
MW
392 vme_address_t address, vme_cycle_t cycle, vme_width_t dwidth)
393{
394 struct vme_bridge *bridge;
395 struct list_head *master_pos = NULL;
396 struct vme_master_resource *allocated_image = NULL;
397 struct vme_master_resource *master_image = NULL;
398 struct vme_resource *resource = NULL;
399
8f966dc4 400 bridge = vdev->bridge;
a17a75e2
MW
401 if (bridge == NULL) {
402 printk(KERN_ERR "Can't find VME bus\n");
403 goto err_bus;
404 }
405
406 /* Loop through master resources */
886953e9 407 list_for_each(master_pos, &bridge->master_resources) {
a17a75e2
MW
408 master_image = list_entry(master_pos,
409 struct vme_master_resource, list);
410
411 if (master_image == NULL) {
412 printk(KERN_WARNING "Registered NULL master resource\n");
413 continue;
414 }
415
416 /* Find an unlocked and compatible image */
886953e9 417 spin_lock(&master_image->lock);
ead1f3e3 418 if (((master_image->address_attr & address) == address) &&
a17a75e2
MW
419 ((master_image->cycle_attr & cycle) == cycle) &&
420 ((master_image->width_attr & dwidth) == dwidth) &&
421 (master_image->locked == 0)) {
422
423 master_image->locked = 1;
886953e9 424 spin_unlock(&master_image->lock);
a17a75e2
MW
425 allocated_image = master_image;
426 break;
427 }
886953e9 428 spin_unlock(&master_image->lock);
a17a75e2
MW
429 }
430
431 /* Check to see if we found a resource */
432 if (allocated_image == NULL) {
433 printk(KERN_ERR "Can't find a suitable resource\n");
434 goto err_image;
435 }
436
437 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
438 if (resource == NULL) {
439 printk(KERN_ERR "Unable to allocate resource structure\n");
440 goto err_alloc;
441 }
442 resource->type = VME_MASTER;
886953e9 443 resource->entry = &allocated_image->list;
a17a75e2
MW
444
445 return resource;
446
a17a75e2
MW
447err_alloc:
448 /* Unlock image */
886953e9 449 spin_lock(&master_image->lock);
a17a75e2 450 master_image->locked = 0;
886953e9 451 spin_unlock(&master_image->lock);
a17a75e2
MW
452err_image:
453err_bus:
454 return NULL;
455}
456EXPORT_SYMBOL(vme_master_request);
457
ead1f3e3 458int vme_master_set(struct vme_resource *resource, int enabled,
a17a75e2
MW
459 unsigned long long vme_base, unsigned long long size,
460 vme_address_t aspace, vme_cycle_t cycle, vme_width_t dwidth)
461{
462 struct vme_bridge *bridge = find_bridge(resource);
463 struct vme_master_resource *image;
464 int retval;
465
466 if (resource->type != VME_MASTER) {
ead1f3e3 467 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
468 return -EINVAL;
469 }
470
471 image = list_entry(resource->entry, struct vme_master_resource, list);
472
473 if (bridge->master_set == NULL) {
ead1f3e3 474 printk(KERN_WARNING "vme_master_set not supported\n");
a17a75e2
MW
475 return -EINVAL;
476 }
477
ead1f3e3 478 if (!(((image->address_attr & aspace) == aspace) &&
a17a75e2
MW
479 ((image->cycle_attr & cycle) == cycle) &&
480 ((image->width_attr & dwidth) == dwidth))) {
ead1f3e3 481 printk(KERN_WARNING "Invalid attributes\n");
a17a75e2
MW
482 return -EINVAL;
483 }
484
485 retval = vme_check_window(aspace, vme_base, size);
ead1f3e3 486 if (retval)
a17a75e2
MW
487 return retval;
488
489 return bridge->master_set(image, enabled, vme_base, size, aspace,
490 cycle, dwidth);
491}
492EXPORT_SYMBOL(vme_master_set);
493
ead1f3e3 494int vme_master_get(struct vme_resource *resource, int *enabled,
a17a75e2
MW
495 unsigned long long *vme_base, unsigned long long *size,
496 vme_address_t *aspace, vme_cycle_t *cycle, vme_width_t *dwidth)
497{
498 struct vme_bridge *bridge = find_bridge(resource);
499 struct vme_master_resource *image;
500
501 if (resource->type != VME_MASTER) {
ead1f3e3 502 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
503 return -EINVAL;
504 }
505
506 image = list_entry(resource->entry, struct vme_master_resource, list);
507
51a569f7 508 if (bridge->master_get == NULL) {
ead1f3e3 509 printk(KERN_WARNING "vme_master_set not supported\n");
a17a75e2
MW
510 return -EINVAL;
511 }
512
513 return bridge->master_get(image, enabled, vme_base, size, aspace,
514 cycle, dwidth);
515}
516EXPORT_SYMBOL(vme_master_get);
517
518/*
519 * Read data out of VME space into a buffer.
520 */
ead1f3e3 521ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
a17a75e2
MW
522 loff_t offset)
523{
524 struct vme_bridge *bridge = find_bridge(resource);
525 struct vme_master_resource *image;
526 size_t length;
527
528 if (bridge->master_read == NULL) {
ead1f3e3 529 printk(KERN_WARNING "Reading from resource not supported\n");
a17a75e2
MW
530 return -EINVAL;
531 }
532
533 if (resource->type != VME_MASTER) {
ead1f3e3 534 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
535 return -EINVAL;
536 }
537
538 image = list_entry(resource->entry, struct vme_master_resource, list);
539
540 length = vme_get_size(resource);
541
542 if (offset > length) {
ead1f3e3 543 printk(KERN_WARNING "Invalid Offset\n");
a17a75e2
MW
544 return -EFAULT;
545 }
546
547 if ((offset + count) > length)
548 count = length - offset;
549
550 return bridge->master_read(image, buf, count, offset);
551
552}
553EXPORT_SYMBOL(vme_master_read);
554
555/*
556 * Write data out to VME space from a buffer.
557 */
ead1f3e3 558ssize_t vme_master_write(struct vme_resource *resource, void *buf,
a17a75e2
MW
559 size_t count, loff_t offset)
560{
561 struct vme_bridge *bridge = find_bridge(resource);
562 struct vme_master_resource *image;
563 size_t length;
564
565 if (bridge->master_write == NULL) {
ead1f3e3 566 printk(KERN_WARNING "Writing to resource not supported\n");
a17a75e2
MW
567 return -EINVAL;
568 }
569
570 if (resource->type != VME_MASTER) {
ead1f3e3 571 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
572 return -EINVAL;
573 }
574
575 image = list_entry(resource->entry, struct vme_master_resource, list);
576
577 length = vme_get_size(resource);
578
579 if (offset > length) {
ead1f3e3 580 printk(KERN_WARNING "Invalid Offset\n");
a17a75e2
MW
581 return -EFAULT;
582 }
583
584 if ((offset + count) > length)
585 count = length - offset;
586
587 return bridge->master_write(image, buf, count, offset);
588}
589EXPORT_SYMBOL(vme_master_write);
590
591/*
592 * Perform RMW cycle to provided location.
593 */
ead1f3e3 594unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
a17a75e2
MW
595 unsigned int compare, unsigned int swap, loff_t offset)
596{
597 struct vme_bridge *bridge = find_bridge(resource);
598 struct vme_master_resource *image;
599
600 if (bridge->master_rmw == NULL) {
ead1f3e3 601 printk(KERN_WARNING "Writing to resource not supported\n");
a17a75e2
MW
602 return -EINVAL;
603 }
604
605 if (resource->type != VME_MASTER) {
ead1f3e3 606 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
607 return -EINVAL;
608 }
609
610 image = list_entry(resource->entry, struct vme_master_resource, list);
611
612 return bridge->master_rmw(image, mask, compare, swap, offset);
613}
614EXPORT_SYMBOL(vme_master_rmw);
615
616void vme_master_free(struct vme_resource *resource)
617{
618 struct vme_master_resource *master_image;
619
620 if (resource->type != VME_MASTER) {
ead1f3e3 621 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
622 return;
623 }
624
625 master_image = list_entry(resource->entry, struct vme_master_resource,
626 list);
627 if (master_image == NULL) {
ead1f3e3 628 printk(KERN_ERR "Can't find master resource\n");
a17a75e2
MW
629 return;
630 }
631
632 /* Unlock image */
886953e9 633 spin_lock(&master_image->lock);
a17a75e2
MW
634 if (master_image->locked == 0)
635 printk(KERN_ERR "Image is already free\n");
636
637 master_image->locked = 0;
886953e9 638 spin_unlock(&master_image->lock);
a17a75e2
MW
639
640 /* Free up resource memory */
641 kfree(resource);
642}
643EXPORT_SYMBOL(vme_master_free);
644
645/*
646 * Request a DMA controller with specific attributes, return some unique
647 * identifier.
648 */
8f966dc4
MV
649struct vme_resource *vme_dma_request(struct vme_dev *vdev,
650 vme_dma_route_t route)
a17a75e2
MW
651{
652 struct vme_bridge *bridge;
653 struct list_head *dma_pos = NULL;
654 struct vme_dma_resource *allocated_ctrlr = NULL;
655 struct vme_dma_resource *dma_ctrlr = NULL;
656 struct vme_resource *resource = NULL;
657
658 /* XXX Not checking resource attributes */
659 printk(KERN_ERR "No VME resource Attribute tests done\n");
660
8f966dc4 661 bridge = vdev->bridge;
a17a75e2
MW
662 if (bridge == NULL) {
663 printk(KERN_ERR "Can't find VME bus\n");
664 goto err_bus;
665 }
666
667 /* Loop through DMA resources */
886953e9 668 list_for_each(dma_pos, &bridge->dma_resources) {
a17a75e2
MW
669 dma_ctrlr = list_entry(dma_pos,
670 struct vme_dma_resource, list);
671
672 if (dma_ctrlr == NULL) {
ead1f3e3 673 printk(KERN_ERR "Registered NULL DMA resource\n");
a17a75e2
MW
674 continue;
675 }
676
4f723df4 677 /* Find an unlocked and compatible controller */
886953e9 678 mutex_lock(&dma_ctrlr->mtx);
4f723df4
MW
679 if (((dma_ctrlr->route_attr & route) == route) &&
680 (dma_ctrlr->locked == 0)) {
681
a17a75e2 682 dma_ctrlr->locked = 1;
886953e9 683 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
684 allocated_ctrlr = dma_ctrlr;
685 break;
686 }
886953e9 687 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
688 }
689
690 /* Check to see if we found a resource */
691 if (allocated_ctrlr == NULL)
692 goto err_ctrlr;
693
694 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
695 if (resource == NULL) {
696 printk(KERN_WARNING "Unable to allocate resource structure\n");
697 goto err_alloc;
698 }
699 resource->type = VME_DMA;
886953e9 700 resource->entry = &allocated_ctrlr->list;
a17a75e2
MW
701
702 return resource;
703
704err_alloc:
705 /* Unlock image */
886953e9 706 mutex_lock(&dma_ctrlr->mtx);
a17a75e2 707 dma_ctrlr->locked = 0;
886953e9 708 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
709err_ctrlr:
710err_bus:
711 return NULL;
712}
58e50798 713EXPORT_SYMBOL(vme_dma_request);
a17a75e2
MW
714
715/*
716 * Start new list
717 */
718struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
719{
720 struct vme_dma_resource *ctrlr;
721 struct vme_dma_list *dma_list;
722
723 if (resource->type != VME_DMA) {
ead1f3e3 724 printk(KERN_ERR "Not a DMA resource\n");
a17a75e2
MW
725 return NULL;
726 }
727
728 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
729
ead1f3e3
MW
730 dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
731 if (dma_list == NULL) {
732 printk(KERN_ERR "Unable to allocate memory for new dma list\n");
a17a75e2
MW
733 return NULL;
734 }
886953e9 735 INIT_LIST_HEAD(&dma_list->entries);
a17a75e2 736 dma_list->parent = ctrlr;
886953e9 737 mutex_init(&dma_list->mtx);
a17a75e2
MW
738
739 return dma_list;
740}
741EXPORT_SYMBOL(vme_new_dma_list);
742
743/*
744 * Create "Pattern" type attributes
745 */
746struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern,
747 vme_pattern_t type)
748{
749 struct vme_dma_attr *attributes;
750 struct vme_dma_pattern *pattern_attr;
751
ead1f3e3
MW
752 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
753 if (attributes == NULL) {
754 printk(KERN_ERR "Unable to allocate memory for attributes "
755 "structure\n");
a17a75e2
MW
756 goto err_attr;
757 }
758
ead1f3e3
MW
759 pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
760 if (pattern_attr == NULL) {
761 printk(KERN_ERR "Unable to allocate memory for pattern "
762 "attributes\n");
a17a75e2
MW
763 goto err_pat;
764 }
765
766 attributes->type = VME_DMA_PATTERN;
767 attributes->private = (void *)pattern_attr;
768
769 pattern_attr->pattern = pattern;
770 pattern_attr->type = type;
771
772 return attributes;
773
a17a75e2
MW
774err_pat:
775 kfree(attributes);
776err_attr:
777 return NULL;
778}
779EXPORT_SYMBOL(vme_dma_pattern_attribute);
780
781/*
782 * Create "PCI" type attributes
783 */
784struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
785{
786 struct vme_dma_attr *attributes;
787 struct vme_dma_pci *pci_attr;
788
789 /* XXX Run some sanity checks here */
790
ead1f3e3
MW
791 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
792 if (attributes == NULL) {
793 printk(KERN_ERR "Unable to allocate memory for attributes "
794 "structure\n");
a17a75e2
MW
795 goto err_attr;
796 }
797
ead1f3e3
MW
798 pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
799 if (pci_attr == NULL) {
800 printk(KERN_ERR "Unable to allocate memory for pci "
801 "attributes\n");
a17a75e2
MW
802 goto err_pci;
803 }
804
805
806
807 attributes->type = VME_DMA_PCI;
808 attributes->private = (void *)pci_attr;
809
810 pci_attr->address = address;
811
812 return attributes;
813
a17a75e2
MW
814err_pci:
815 kfree(attributes);
816err_attr:
817 return NULL;
818}
819EXPORT_SYMBOL(vme_dma_pci_attribute);
820
821/*
822 * Create "VME" type attributes
823 */
824struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
825 vme_address_t aspace, vme_cycle_t cycle, vme_width_t dwidth)
826{
827 struct vme_dma_attr *attributes;
828 struct vme_dma_vme *vme_attr;
829
ead1f3e3 830 attributes = kmalloc(
a17a75e2 831 sizeof(struct vme_dma_attr), GFP_KERNEL);
ead1f3e3
MW
832 if (attributes == NULL) {
833 printk(KERN_ERR "Unable to allocate memory for attributes "
834 "structure\n");
a17a75e2
MW
835 goto err_attr;
836 }
837
ead1f3e3
MW
838 vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
839 if (vme_attr == NULL) {
840 printk(KERN_ERR "Unable to allocate memory for vme "
841 "attributes\n");
a17a75e2
MW
842 goto err_vme;
843 }
844
845 attributes->type = VME_DMA_VME;
846 attributes->private = (void *)vme_attr;
847
848 vme_attr->address = address;
849 vme_attr->aspace = aspace;
850 vme_attr->cycle = cycle;
851 vme_attr->dwidth = dwidth;
852
853 return attributes;
854
a17a75e2
MW
855err_vme:
856 kfree(attributes);
857err_attr:
858 return NULL;
859}
860EXPORT_SYMBOL(vme_dma_vme_attribute);
861
862/*
863 * Free attribute
864 */
865void vme_dma_free_attribute(struct vme_dma_attr *attributes)
866{
867 kfree(attributes->private);
868 kfree(attributes);
869}
870EXPORT_SYMBOL(vme_dma_free_attribute);
871
872int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
873 struct vme_dma_attr *dest, size_t count)
874{
875 struct vme_bridge *bridge = list->parent->parent;
876 int retval;
877
878 if (bridge->dma_list_add == NULL) {
ead1f3e3 879 printk(KERN_WARNING "Link List DMA generation not supported\n");
a17a75e2
MW
880 return -EINVAL;
881 }
882
886953e9 883 if (!mutex_trylock(&list->mtx)) {
ead1f3e3 884 printk(KERN_ERR "Link List already submitted\n");
a17a75e2
MW
885 return -EINVAL;
886 }
887
888 retval = bridge->dma_list_add(list, src, dest, count);
889
886953e9 890 mutex_unlock(&list->mtx);
a17a75e2
MW
891
892 return retval;
893}
894EXPORT_SYMBOL(vme_dma_list_add);
895
896int vme_dma_list_exec(struct vme_dma_list *list)
897{
898 struct vme_bridge *bridge = list->parent->parent;
899 int retval;
900
901 if (bridge->dma_list_exec == NULL) {
ead1f3e3 902 printk(KERN_ERR "Link List DMA execution not supported\n");
a17a75e2
MW
903 return -EINVAL;
904 }
905
886953e9 906 mutex_lock(&list->mtx);
a17a75e2
MW
907
908 retval = bridge->dma_list_exec(list);
909
886953e9 910 mutex_unlock(&list->mtx);
a17a75e2
MW
911
912 return retval;
913}
914EXPORT_SYMBOL(vme_dma_list_exec);
915
916int vme_dma_list_free(struct vme_dma_list *list)
917{
918 struct vme_bridge *bridge = list->parent->parent;
919 int retval;
920
921 if (bridge->dma_list_empty == NULL) {
ead1f3e3 922 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
a17a75e2
MW
923 return -EINVAL;
924 }
925
886953e9 926 if (!mutex_trylock(&list->mtx)) {
ead1f3e3 927 printk(KERN_ERR "Link List in use\n");
a17a75e2
MW
928 return -EINVAL;
929 }
930
931 /*
932 * Empty out all of the entries from the dma list. We need to go to the
933 * low level driver as dma entries are driver specific.
934 */
935 retval = bridge->dma_list_empty(list);
936 if (retval) {
ead1f3e3 937 printk(KERN_ERR "Unable to empty link-list entries\n");
886953e9 938 mutex_unlock(&list->mtx);
a17a75e2
MW
939 return retval;
940 }
886953e9 941 mutex_unlock(&list->mtx);
a17a75e2
MW
942 kfree(list);
943
944 return retval;
945}
946EXPORT_SYMBOL(vme_dma_list_free);
947
948int vme_dma_free(struct vme_resource *resource)
949{
950 struct vme_dma_resource *ctrlr;
951
952 if (resource->type != VME_DMA) {
ead1f3e3 953 printk(KERN_ERR "Not a DMA resource\n");
a17a75e2
MW
954 return -EINVAL;
955 }
956
957 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
958
886953e9 959 if (!mutex_trylock(&ctrlr->mtx)) {
ead1f3e3 960 printk(KERN_ERR "Resource busy, can't free\n");
a17a75e2
MW
961 return -EBUSY;
962 }
963
886953e9 964 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
ead1f3e3 965 printk(KERN_WARNING "Resource still processing transfers\n");
886953e9 966 mutex_unlock(&ctrlr->mtx);
a17a75e2
MW
967 return -EBUSY;
968 }
969
970 ctrlr->locked = 0;
971
886953e9 972 mutex_unlock(&ctrlr->mtx);
a17a75e2
MW
973
974 return 0;
975}
976EXPORT_SYMBOL(vme_dma_free);
977
c813f592
MW
978void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
979{
980 void (*call)(int, int, void *);
981 void *priv_data;
982
983 call = bridge->irq[level - 1].callback[statid].func;
984 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
985
986 if (call != NULL)
987 call(level, statid, priv_data);
988 else
989 printk(KERN_WARNING "Spurilous VME interrupt, level:%x, "
990 "vector:%x\n", level, statid);
991}
992EXPORT_SYMBOL(vme_irq_handler);
993
8f966dc4 994int vme_irq_request(struct vme_dev *vdev, int level, int statid,
29848ac9 995 void (*callback)(int, int, void *),
a17a75e2
MW
996 void *priv_data)
997{
998 struct vme_bridge *bridge;
999
8f966dc4 1000 bridge = vdev->bridge;
a17a75e2
MW
1001 if (bridge == NULL) {
1002 printk(KERN_ERR "Can't find VME bus\n");
1003 return -EINVAL;
1004 }
1005
ead1f3e3 1006 if ((level < 1) || (level > 7)) {
c813f592 1007 printk(KERN_ERR "Invalid interrupt level\n");
a17a75e2
MW
1008 return -EINVAL;
1009 }
1010
c813f592
MW
1011 if (bridge->irq_set == NULL) {
1012 printk(KERN_ERR "Configuring interrupts not supported\n");
a17a75e2
MW
1013 return -EINVAL;
1014 }
1015
886953e9 1016 mutex_lock(&bridge->irq_mtx);
c813f592
MW
1017
1018 if (bridge->irq[level - 1].callback[statid].func) {
886953e9 1019 mutex_unlock(&bridge->irq_mtx);
c813f592
MW
1020 printk(KERN_WARNING "VME Interrupt already taken\n");
1021 return -EBUSY;
1022 }
1023
1024 bridge->irq[level - 1].count++;
1025 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1026 bridge->irq[level - 1].callback[statid].func = callback;
1027
1028 /* Enable IRQ level */
29848ac9 1029 bridge->irq_set(bridge, level, 1, 1);
c813f592 1030
886953e9 1031 mutex_unlock(&bridge->irq_mtx);
c813f592
MW
1032
1033 return 0;
a17a75e2 1034}
c813f592 1035EXPORT_SYMBOL(vme_irq_request);
a17a75e2 1036
8f966dc4 1037void vme_irq_free(struct vme_dev *vdev, int level, int statid)
a17a75e2
MW
1038{
1039 struct vme_bridge *bridge;
1040
8f966dc4 1041 bridge = vdev->bridge;
a17a75e2
MW
1042 if (bridge == NULL) {
1043 printk(KERN_ERR "Can't find VME bus\n");
1044 return;
1045 }
1046
ead1f3e3 1047 if ((level < 1) || (level > 7)) {
c813f592 1048 printk(KERN_ERR "Invalid interrupt level\n");
a17a75e2
MW
1049 return;
1050 }
1051
c813f592
MW
1052 if (bridge->irq_set == NULL) {
1053 printk(KERN_ERR "Configuring interrupts not supported\n");
a17a75e2
MW
1054 return;
1055 }
1056
886953e9 1057 mutex_lock(&bridge->irq_mtx);
c813f592
MW
1058
1059 bridge->irq[level - 1].count--;
1060
1061 /* Disable IRQ level if no more interrupts attached at this level*/
1062 if (bridge->irq[level - 1].count == 0)
29848ac9 1063 bridge->irq_set(bridge, level, 0, 1);
c813f592
MW
1064
1065 bridge->irq[level - 1].callback[statid].func = NULL;
1066 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1067
886953e9 1068 mutex_unlock(&bridge->irq_mtx);
a17a75e2 1069}
c813f592 1070EXPORT_SYMBOL(vme_irq_free);
a17a75e2 1071
8f966dc4 1072int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
a17a75e2
MW
1073{
1074 struct vme_bridge *bridge;
1075
8f966dc4 1076 bridge = vdev->bridge;
a17a75e2
MW
1077 if (bridge == NULL) {
1078 printk(KERN_ERR "Can't find VME bus\n");
1079 return -EINVAL;
1080 }
1081
ead1f3e3 1082 if ((level < 1) || (level > 7)) {
a17a75e2
MW
1083 printk(KERN_WARNING "Invalid interrupt level\n");
1084 return -EINVAL;
1085 }
1086
c813f592 1087 if (bridge->irq_generate == NULL) {
ead1f3e3 1088 printk(KERN_WARNING "Interrupt generation not supported\n");
a17a75e2
MW
1089 return -EINVAL;
1090 }
1091
29848ac9 1092 return bridge->irq_generate(bridge, level, statid);
a17a75e2 1093}
c813f592 1094EXPORT_SYMBOL(vme_irq_generate);
a17a75e2 1095
42fb5031
MW
1096/*
1097 * Request the location monitor, return resource or NULL
1098 */
8f966dc4 1099struct vme_resource *vme_lm_request(struct vme_dev *vdev)
a17a75e2
MW
1100{
1101 struct vme_bridge *bridge;
42fb5031
MW
1102 struct list_head *lm_pos = NULL;
1103 struct vme_lm_resource *allocated_lm = NULL;
1104 struct vme_lm_resource *lm = NULL;
1105 struct vme_resource *resource = NULL;
a17a75e2 1106
8f966dc4 1107 bridge = vdev->bridge;
a17a75e2
MW
1108 if (bridge == NULL) {
1109 printk(KERN_ERR "Can't find VME bus\n");
42fb5031
MW
1110 goto err_bus;
1111 }
1112
1113 /* Loop through DMA resources */
886953e9 1114 list_for_each(lm_pos, &bridge->lm_resources) {
42fb5031
MW
1115 lm = list_entry(lm_pos,
1116 struct vme_lm_resource, list);
1117
1118 if (lm == NULL) {
1119 printk(KERN_ERR "Registered NULL Location Monitor "
1120 "resource\n");
1121 continue;
1122 }
1123
1124 /* Find an unlocked controller */
886953e9 1125 mutex_lock(&lm->mtx);
42fb5031
MW
1126 if (lm->locked == 0) {
1127 lm->locked = 1;
886953e9 1128 mutex_unlock(&lm->mtx);
42fb5031
MW
1129 allocated_lm = lm;
1130 break;
1131 }
886953e9 1132 mutex_unlock(&lm->mtx);
42fb5031
MW
1133 }
1134
1135 /* Check to see if we found a resource */
1136 if (allocated_lm == NULL)
1137 goto err_lm;
1138
1139 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
1140 if (resource == NULL) {
1141 printk(KERN_ERR "Unable to allocate resource structure\n");
1142 goto err_alloc;
1143 }
1144 resource->type = VME_LM;
886953e9 1145 resource->entry = &allocated_lm->list;
42fb5031
MW
1146
1147 return resource;
1148
1149err_alloc:
1150 /* Unlock image */
886953e9 1151 mutex_lock(&lm->mtx);
42fb5031 1152 lm->locked = 0;
886953e9 1153 mutex_unlock(&lm->mtx);
42fb5031
MW
1154err_lm:
1155err_bus:
1156 return NULL;
1157}
1158EXPORT_SYMBOL(vme_lm_request);
1159
1160int vme_lm_count(struct vme_resource *resource)
1161{
1162 struct vme_lm_resource *lm;
1163
1164 if (resource->type != VME_LM) {
1165 printk(KERN_ERR "Not a Location Monitor resource\n");
1166 return -EINVAL;
1167 }
1168
1169 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1170
1171 return lm->monitors;
1172}
1173EXPORT_SYMBOL(vme_lm_count);
1174
1175int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
1176 vme_address_t aspace, vme_cycle_t cycle)
1177{
1178 struct vme_bridge *bridge = find_bridge(resource);
1179 struct vme_lm_resource *lm;
1180
1181 if (resource->type != VME_LM) {
1182 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1183 return -EINVAL;
1184 }
1185
42fb5031
MW
1186 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1187
a17a75e2 1188 if (bridge->lm_set == NULL) {
42fb5031 1189 printk(KERN_ERR "vme_lm_set not supported\n");
a17a75e2
MW
1190 return -EINVAL;
1191 }
1192
8be9226c 1193 return bridge->lm_set(lm, lm_base, aspace, cycle);
a17a75e2
MW
1194}
1195EXPORT_SYMBOL(vme_lm_set);
1196
42fb5031
MW
1197int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
1198 vme_address_t *aspace, vme_cycle_t *cycle)
a17a75e2 1199{
42fb5031
MW
1200 struct vme_bridge *bridge = find_bridge(resource);
1201 struct vme_lm_resource *lm;
a17a75e2 1202
42fb5031
MW
1203 if (resource->type != VME_LM) {
1204 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1205 return -EINVAL;
1206 }
1207
42fb5031
MW
1208 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1209
a17a75e2 1210 if (bridge->lm_get == NULL) {
42fb5031 1211 printk(KERN_ERR "vme_lm_get not supported\n");
a17a75e2
MW
1212 return -EINVAL;
1213 }
1214
42fb5031 1215 return bridge->lm_get(lm, lm_base, aspace, cycle);
a17a75e2
MW
1216}
1217EXPORT_SYMBOL(vme_lm_get);
1218
42fb5031
MW
1219int vme_lm_attach(struct vme_resource *resource, int monitor,
1220 void (*callback)(int))
a17a75e2 1221{
42fb5031
MW
1222 struct vme_bridge *bridge = find_bridge(resource);
1223 struct vme_lm_resource *lm;
a17a75e2 1224
42fb5031
MW
1225 if (resource->type != VME_LM) {
1226 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1227 return -EINVAL;
1228 }
1229
42fb5031
MW
1230 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1231
a17a75e2 1232 if (bridge->lm_attach == NULL) {
42fb5031 1233 printk(KERN_ERR "vme_lm_attach not supported\n");
a17a75e2
MW
1234 return -EINVAL;
1235 }
1236
42fb5031 1237 return bridge->lm_attach(lm, monitor, callback);
a17a75e2
MW
1238}
1239EXPORT_SYMBOL(vme_lm_attach);
1240
42fb5031 1241int vme_lm_detach(struct vme_resource *resource, int monitor)
a17a75e2 1242{
42fb5031
MW
1243 struct vme_bridge *bridge = find_bridge(resource);
1244 struct vme_lm_resource *lm;
a17a75e2 1245
42fb5031
MW
1246 if (resource->type != VME_LM) {
1247 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1248 return -EINVAL;
1249 }
1250
42fb5031
MW
1251 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1252
a17a75e2 1253 if (bridge->lm_detach == NULL) {
42fb5031 1254 printk(KERN_ERR "vme_lm_detach not supported\n");
a17a75e2
MW
1255 return -EINVAL;
1256 }
1257
42fb5031 1258 return bridge->lm_detach(lm, monitor);
a17a75e2
MW
1259}
1260EXPORT_SYMBOL(vme_lm_detach);
1261
42fb5031
MW
1262void vme_lm_free(struct vme_resource *resource)
1263{
1264 struct vme_lm_resource *lm;
1265
1266 if (resource->type != VME_LM) {
1267 printk(KERN_ERR "Not a Location Monitor resource\n");
1268 return;
1269 }
1270
1271 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1272
886953e9 1273 mutex_lock(&lm->mtx);
42fb5031 1274
8be9226c
MW
1275 /* XXX
1276 * Check to see that there aren't any callbacks still attached, if
1277 * there are we should probably be detaching them!
1278 */
42fb5031
MW
1279
1280 lm->locked = 0;
1281
886953e9 1282 mutex_unlock(&lm->mtx);
8be9226c
MW
1283
1284 kfree(resource);
42fb5031
MW
1285}
1286EXPORT_SYMBOL(vme_lm_free);
1287
8f966dc4 1288int vme_slot_get(struct vme_dev *vdev)
a17a75e2
MW
1289{
1290 struct vme_bridge *bridge;
1291
8f966dc4 1292 bridge = vdev->bridge;
a17a75e2
MW
1293 if (bridge == NULL) {
1294 printk(KERN_ERR "Can't find VME bus\n");
1295 return -EINVAL;
1296 }
1297
1298 if (bridge->slot_get == NULL) {
ead1f3e3 1299 printk(KERN_WARNING "vme_slot_get not supported\n");
a17a75e2
MW
1300 return -EINVAL;
1301 }
1302
29848ac9 1303 return bridge->slot_get(bridge);
a17a75e2
MW
1304}
1305EXPORT_SYMBOL(vme_slot_get);
1306
1307
1308/* - Bridge Registration --------------------------------------------------- */
1309
733e3ef0 1310static int vme_add_bus(struct vme_bridge *bridge)
a17a75e2
MW
1311{
1312 int i;
733e3ef0 1313 int ret = -1;
a17a75e2 1314
733e3ef0 1315 mutex_lock(&vme_buses_lock);
a17a75e2 1316 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
733e3ef0
MV
1317 if ((vme_bus_numbers & (1 << i)) == 0) {
1318 vme_bus_numbers |= (1 << i);
1319 bridge->num = i;
1320 list_add_tail(&bridge->bus_list, &vme_bus_list);
1321 ret = 0;
a17a75e2
MW
1322 break;
1323 }
1324 }
733e3ef0 1325 mutex_unlock(&vme_buses_lock);
a17a75e2 1326
733e3ef0 1327 return ret;
a17a75e2
MW
1328}
1329
733e3ef0 1330static void vme_remove_bus(struct vme_bridge *bridge)
a17a75e2 1331{
733e3ef0
MV
1332 mutex_lock(&vme_buses_lock);
1333 vme_bus_numbers &= ~(1 << bridge->num);
1334 list_del(&bridge->bus_list);
1335 mutex_unlock(&vme_buses_lock);
a17a75e2
MW
1336}
1337
f6c39d4f
MV
1338static void vme_dev_release(struct device *dev)
1339{
8f966dc4 1340 kfree(dev_to_vme_dev(dev));
f6c39d4f
MV
1341}
1342
ead1f3e3 1343int vme_register_bridge(struct vme_bridge *bridge)
a17a75e2 1344{
8f966dc4 1345 struct vme_dev *vdev;
a17a75e2
MW
1346 int retval;
1347 int i;
1348
733e3ef0
MV
1349 retval = vme_add_bus(bridge);
1350 if (retval)
1351 return retval;
a17a75e2
MW
1352
1353 /* This creates 32 vme "slot" devices. This equates to a slot for each
1354 * ID available in a system conforming to the ANSI/VITA 1-1994
1355 * specification.
1356 */
1357 for (i = 0; i < VME_SLOTS_MAX; i++) {
8f966dc4 1358 bridge->dev[i] = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
f6c39d4f
MV
1359 if (!bridge->dev[i]) {
1360 retval = -ENOMEM;
1361 goto err_devalloc;
1362 }
8f966dc4
MV
1363 vdev = bridge->dev[i];
1364 memset(vdev, 0, sizeof(struct vme_dev));
1365
1366 vdev->id.bus = bridge->num;
1367 vdev->id.slot = i + 1;
1368 vdev->bridge = bridge;
1369 vdev->dev.parent = bridge->parent;
1370 vdev->dev.bus = &vme_bus_type;
1371 vdev->dev.release = vme_dev_release;
a17a75e2
MW
1372 /*
1373 * We save a pointer to the bridge in platform_data so that we
1374 * can get to it later. We keep driver_data for use by the
1375 * driver that binds against the slot
1376 */
8f966dc4
MV
1377 vdev->dev.platform_data = bridge;
1378 dev_set_name(&vdev->dev, "vme-%x.%x", bridge->num, i + 1);
a17a75e2 1379
8f966dc4 1380 retval = device_register(&vdev->dev);
ead1f3e3 1381 if (retval)
a17a75e2
MW
1382 goto err_reg;
1383 }
1384
1385 return retval;
1386
a17a75e2 1387err_reg:
8f966dc4 1388 kfree(vdev);
f6c39d4f 1389err_devalloc:
da1bbd1d 1390 while (--i >= 0) {
8f966dc4
MV
1391 vdev = bridge->dev[i];
1392 device_unregister(&vdev->dev);
a17a75e2 1393 }
733e3ef0 1394 vme_remove_bus(bridge);
a17a75e2
MW
1395 return retval;
1396}
1397EXPORT_SYMBOL(vme_register_bridge);
1398
ead1f3e3 1399void vme_unregister_bridge(struct vme_bridge *bridge)
a17a75e2
MW
1400{
1401 int i;
8f966dc4 1402 struct vme_dev *vdev;
a17a75e2
MW
1403
1404
1405 for (i = 0; i < VME_SLOTS_MAX; i++) {
8f966dc4
MV
1406 vdev = bridge->dev[i];
1407 device_unregister(&vdev->dev);
a17a75e2 1408 }
733e3ef0 1409 vme_remove_bus(bridge);
a17a75e2
MW
1410}
1411EXPORT_SYMBOL(vme_unregister_bridge);
1412
1413
1414/* - Driver Registration --------------------------------------------------- */
1415
ead1f3e3 1416int vme_register_driver(struct vme_driver *drv)
a17a75e2
MW
1417{
1418 drv->driver.name = drv->name;
1419 drv->driver.bus = &vme_bus_type;
1420
1421 return driver_register(&drv->driver);
1422}
1423EXPORT_SYMBOL(vme_register_driver);
1424
ead1f3e3 1425void vme_unregister_driver(struct vme_driver *drv)
a17a75e2
MW
1426{
1427 driver_unregister(&drv->driver);
1428}
1429EXPORT_SYMBOL(vme_unregister_driver);
1430
1431/* - Bus Registration ------------------------------------------------------ */
1432
a17a75e2
MW
1433static struct vme_driver *dev_to_vme_driver(struct device *dev)
1434{
ead1f3e3
MW
1435 if (dev->driver == NULL)
1436 printk(KERN_ERR "Bugger dev->driver is NULL\n");
a17a75e2
MW
1437
1438 return container_of(dev->driver, struct vme_driver, driver);
1439}
1440
1441static int vme_bus_match(struct device *dev, struct device_driver *drv)
1442{
8f966dc4 1443 struct vme_dev *vdev;
a17a75e2
MW
1444 struct vme_bridge *bridge;
1445 struct vme_driver *driver;
1446 int i, num;
1447
8f966dc4
MV
1448 vdev = dev_to_vme_dev(dev);
1449 bridge = vdev->bridge;
a17a75e2
MW
1450 driver = container_of(drv, struct vme_driver, driver);
1451
8f966dc4
MV
1452 num = vdev->id.slot;
1453 if (num <= 0 || num >= VME_SLOTS_MAX)
a17a75e2
MW
1454 goto err_dev;
1455
1456 if (driver->bind_table == NULL) {
1457 dev_err(dev, "Bind table NULL\n");
1458 goto err_table;
1459 }
1460
1461 i = 0;
ead1f3e3 1462 while ((driver->bind_table[i].bus != 0) ||
a17a75e2
MW
1463 (driver->bind_table[i].slot != 0)) {
1464
a37b0dad
MW
1465 if (bridge->num == driver->bind_table[i].bus) {
1466 if (num == driver->bind_table[i].slot)
1467 return 1;
1468
1469 if (driver->bind_table[i].slot == VME_SLOT_ALL)
1470 return 1;
1471
1472 if ((driver->bind_table[i].slot == VME_SLOT_CURRENT) &&
8f966dc4 1473 (num == vme_slot_get(vdev)))
a37b0dad
MW
1474 return 1;
1475 }
a17a75e2
MW
1476 i++;
1477 }
1478
1479err_dev:
1480err_table:
1481 return 0;
1482}
1483
1484static int vme_bus_probe(struct device *dev)
1485{
a17a75e2 1486 struct vme_driver *driver;
8f966dc4 1487 struct vme_dev *vdev;
a17a75e2
MW
1488 int retval = -ENODEV;
1489
1490 driver = dev_to_vme_driver(dev);
8f966dc4 1491 vdev = dev_to_vme_dev(dev);
a17a75e2 1492
ead1f3e3 1493 if (driver->probe != NULL)
8f966dc4 1494 retval = driver->probe(vdev);
a17a75e2
MW
1495
1496 return retval;
1497}
1498
1499static int vme_bus_remove(struct device *dev)
1500{
a17a75e2 1501 struct vme_driver *driver;
8f966dc4 1502 struct vme_dev *vdev;
a17a75e2
MW
1503 int retval = -ENODEV;
1504
1505 driver = dev_to_vme_driver(dev);
8f966dc4 1506 vdev = dev_to_vme_dev(dev);
a17a75e2 1507
ead1f3e3 1508 if (driver->remove != NULL)
8f966dc4 1509 retval = driver->remove(vdev);
a17a75e2
MW
1510
1511 return retval;
1512}
1513
1514struct bus_type vme_bus_type = {
1515 .name = "vme",
1516 .match = vme_bus_match,
1517 .probe = vme_bus_probe,
1518 .remove = vme_bus_remove,
1519};
1520EXPORT_SYMBOL(vme_bus_type);
1521
ead1f3e3 1522static int __init vme_init(void)
a17a75e2
MW
1523{
1524 return bus_register(&vme_bus_type);
1525}
1526
ead1f3e3 1527static void __exit vme_exit(void)
a17a75e2
MW
1528{
1529 bus_unregister(&vme_bus_type);
1530}
1531
1532MODULE_DESCRIPTION("VME bridge driver framework");
66bd8db5 1533MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
a17a75e2
MW
1534MODULE_LICENSE("GPL");
1535
1536module_init(vme_init);
1537module_exit(vme_exit);