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