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