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