blob: 7516030037a16e18082908b41af76a7308c85cb9 [file] [log] [blame]
Martyn Welcha17a75e2009-07-31 09:28:17 +01001/*
2 * VME Bridge Framework
3 *
Martyn Welch66bd8db2010-02-18 15:12:52 +00004 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
Martyn Welcha17a75e2009-07-31 09:28:17 +01006 *
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
Martyn Welcha17a75e2009-07-31 09:28:17 +010016#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>
Martyn Welch400822f2009-08-11 16:20:22 +010030#include <linux/mutex.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010031#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Greg Kroah-Hartmandb3b9e92012-04-26 12:34:58 -070033#include <linux/vme.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010034
Martyn Welcha17a75e2009-07-31 09:28:17 +010035#include "vme_bridge.h"
36
Manohar Vanga733e3ef2011-08-12 12:30:48 +020037/* Bitmask and list of registered buses both protected by common mutex */
Martyn Welcha17a75e2009-07-31 09:28:17 +010038static unsigned int vme_bus_numbers;
Manohar Vanga733e3ef2011-08-12 12:30:48 +020039static LIST_HEAD(vme_bus_list);
40static DEFINE_MUTEX(vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +010041
Martyn Welchead1f3e2009-12-15 08:43:02 +000042static void __exit vme_exit(void);
43static int __init vme_init(void);
Martyn Welcha17a75e2009-07-31 09:28:17 +010044
Manohar Vanga8f966dc2011-09-26 11:27:15 +020045static struct vme_dev *dev_to_vme_dev(struct device *dev)
Martyn Welcha17a75e2009-07-31 09:28:17 +010046{
Manohar Vanga8f966dc2011-09-26 11:27:15 +020047 return container_of(dev, struct vme_dev, dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +010048}
49
50/*
51 * Find the bridge that the resource is associated with.
52 */
53static struct vme_bridge *find_bridge(struct vme_resource *resource)
54{
55 /* Get list to search */
56 switch (resource->type) {
57 case VME_MASTER:
58 return list_entry(resource->entry, struct vme_master_resource,
59 list)->parent;
60 break;
61 case VME_SLAVE:
62 return list_entry(resource->entry, struct vme_slave_resource,
63 list)->parent;
64 break;
65 case VME_DMA:
66 return list_entry(resource->entry, struct vme_dma_resource,
67 list)->parent;
68 break;
Martyn Welch42fb5032009-08-11 17:44:56 +010069 case VME_LM:
70 return list_entry(resource->entry, struct vme_lm_resource,
71 list)->parent;
72 break;
Martyn Welcha17a75e2009-07-31 09:28:17 +010073 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.
Martyn Welcha17a75e2009-07-31 09:28:17 +010083 */
Martyn Welchead1f3e2009-12-15 08:43:02 +000084void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
Martyn Welcha17a75e2009-07-31 09:28:17 +010085 dma_addr_t *dma)
86{
87 struct vme_bridge *bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +010088
Martyn Welchead1f3e2009-12-15 08:43:02 +000089 if (resource == NULL) {
90 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +010091 return NULL;
92 }
93
94 bridge = find_bridge(resource);
Martyn Welchead1f3e2009-12-15 08:43:02 +000095 if (bridge == NULL) {
96 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +010097 return NULL;
98 }
99
Martyn Welcha17a75e2009-07-31 09:28:17 +0100100 if (bridge->parent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700101 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100102 return NULL;
103 }
Martyn Welcha17a75e2009-07-31 09:28:17 +0100104
Manohar Vanga7f58f022011-08-10 11:33:46 +0200105 if (bridge->alloc_consistent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700106 printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
107 bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200108 return NULL;
109 }
110
111 return bridge->alloc_consistent(bridge->parent, size, dma);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100112}
113EXPORT_SYMBOL(vme_alloc_consistent);
114
115/*
116 * Free previously allocated contiguous block of memory.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100117 */
118void vme_free_consistent(struct vme_resource *resource, size_t size,
119 void *vaddr, dma_addr_t dma)
120{
121 struct vme_bridge *bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100122
Martyn Welchead1f3e2009-12-15 08:43:02 +0000123 if (resource == NULL) {
124 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100125 return;
126 }
127
128 bridge = find_bridge(resource);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000129 if (bridge == NULL) {
130 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100131 return;
132 }
133
Manohar Vanga7f58f022011-08-10 11:33:46 +0200134 if (bridge->parent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700135 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200136 return;
137 }
Martyn Welcha17a75e2009-07-31 09:28:17 +0100138
Manohar Vanga7f58f022011-08-10 11:33:46 +0200139 if (bridge->free_consistent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700140 printk(KERN_ERR "free_consistent not supported by bridge %s\n",
141 bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200142 return;
143 }
144
145 bridge->free_consistent(bridge->parent, size, vaddr, dma);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100146}
147EXPORT_SYMBOL(vme_free_consistent);
148
149size_t vme_get_size(struct vme_resource *resource)
150{
151 int enabled, retval;
152 unsigned long long base, size;
153 dma_addr_t buf_base;
Martyn Welch6af04b02011-12-01 17:06:29 +0000154 u32 aspace, cycle, dwidth;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100155
156 switch (resource->type) {
157 case VME_MASTER:
158 retval = vme_master_get(resource, &enabled, &base, &size,
159 &aspace, &cycle, &dwidth);
160
161 return size;
162 break;
163 case VME_SLAVE:
164 retval = vme_slave_get(resource, &enabled, &base, &size,
165 &buf_base, &aspace, &cycle);
166
167 return size;
168 break;
169 case VME_DMA:
170 return 0;
171 break;
172 default:
173 printk(KERN_ERR "Unknown resource type\n");
174 return 0;
175 break;
176 }
177}
178EXPORT_SYMBOL(vme_get_size);
179
Martyn Welch6af04b02011-12-01 17:06:29 +0000180static int vme_check_window(u32 aspace, unsigned long long vme_base,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100181 unsigned long long size)
182{
183 int retval = 0;
184
185 switch (aspace) {
186 case VME_A16:
187 if (((vme_base + size) > VME_A16_MAX) ||
188 (vme_base > VME_A16_MAX))
189 retval = -EFAULT;
190 break;
191 case VME_A24:
192 if (((vme_base + size) > VME_A24_MAX) ||
193 (vme_base > VME_A24_MAX))
194 retval = -EFAULT;
195 break;
196 case VME_A32:
197 if (((vme_base + size) > VME_A32_MAX) ||
198 (vme_base > VME_A32_MAX))
199 retval = -EFAULT;
200 break;
201 case VME_A64:
202 /*
203 * Any value held in an unsigned long long can be used as the
204 * base
205 */
206 break;
207 case VME_CRCSR:
208 if (((vme_base + size) > VME_CRCSR_MAX) ||
209 (vme_base > VME_CRCSR_MAX))
210 retval = -EFAULT;
211 break;
212 case VME_USER1:
213 case VME_USER2:
214 case VME_USER3:
215 case VME_USER4:
216 /* User Defined */
217 break;
218 default:
Martyn Welchead1f3e2009-12-15 08:43:02 +0000219 printk(KERN_ERR "Invalid address space\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100220 retval = -EINVAL;
221 break;
222 }
223
224 return retval;
225}
226
227/*
228 * Request a slave image with specific attributes, return some unique
229 * identifier.
230 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000231struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
232 u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100233{
234 struct vme_bridge *bridge;
235 struct list_head *slave_pos = NULL;
236 struct vme_slave_resource *allocated_image = NULL;
237 struct vme_slave_resource *slave_image = NULL;
238 struct vme_resource *resource = NULL;
239
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200240 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100241 if (bridge == NULL) {
242 printk(KERN_ERR "Can't find VME bus\n");
243 goto err_bus;
244 }
245
246 /* Loop through slave resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000247 list_for_each(slave_pos, &bridge->slave_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100248 slave_image = list_entry(slave_pos,
249 struct vme_slave_resource, list);
250
251 if (slave_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000252 printk(KERN_ERR "Registered NULL Slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100253 continue;
254 }
255
256 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000257 mutex_lock(&slave_image->mtx);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000258 if (((slave_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100259 ((slave_image->cycle_attr & cycle) == cycle) &&
260 (slave_image->locked == 0)) {
261
262 slave_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000263 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100264 allocated_image = slave_image;
265 break;
266 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000267 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100268 }
269
270 /* No free image */
271 if (allocated_image == NULL)
272 goto err_image;
273
274 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
275 if (resource == NULL) {
276 printk(KERN_WARNING "Unable to allocate resource structure\n");
277 goto err_alloc;
278 }
279 resource->type = VME_SLAVE;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000280 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100281
282 return resource;
283
284err_alloc:
285 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000286 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100287 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000288 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100289err_image:
290err_bus:
291 return NULL;
292}
293EXPORT_SYMBOL(vme_slave_request);
294
Martyn Welchead1f3e2009-12-15 08:43:02 +0000295int vme_slave_set(struct vme_resource *resource, int enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100296 unsigned long long vme_base, unsigned long long size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000297 dma_addr_t buf_base, u32 aspace, u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100298{
299 struct vme_bridge *bridge = find_bridge(resource);
300 struct vme_slave_resource *image;
301 int retval;
302
303 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000304 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100305 return -EINVAL;
306 }
307
308 image = list_entry(resource->entry, struct vme_slave_resource, list);
309
310 if (bridge->slave_set == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000311 printk(KERN_ERR "Function not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100312 return -ENOSYS;
313 }
314
Martyn Welchead1f3e2009-12-15 08:43:02 +0000315 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100316 ((image->cycle_attr & cycle) == cycle))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000317 printk(KERN_ERR "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100318 return -EINVAL;
319 }
320
321 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000322 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100323 return retval;
324
325 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
326 aspace, cycle);
327}
328EXPORT_SYMBOL(vme_slave_set);
329
Martyn Welchead1f3e2009-12-15 08:43:02 +0000330int vme_slave_get(struct vme_resource *resource, int *enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100331 unsigned long long *vme_base, unsigned long long *size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000332 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100333{
334 struct vme_bridge *bridge = find_bridge(resource);
335 struct vme_slave_resource *image;
336
337 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000338 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100339 return -EINVAL;
340 }
341
342 image = list_entry(resource->entry, struct vme_slave_resource, list);
343
Emilio G. Cota51a569f2009-08-10 16:52:42 +0200344 if (bridge->slave_get == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000345 printk(KERN_ERR "vme_slave_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100346 return -EINVAL;
347 }
348
349 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
350 aspace, cycle);
351}
352EXPORT_SYMBOL(vme_slave_get);
353
354void vme_slave_free(struct vme_resource *resource)
355{
356 struct vme_slave_resource *slave_image;
357
358 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000359 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100360 return;
361 }
362
363 slave_image = list_entry(resource->entry, struct vme_slave_resource,
364 list);
365 if (slave_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000366 printk(KERN_ERR "Can't find slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100367 return;
368 }
369
370 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000371 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100372 if (slave_image->locked == 0)
373 printk(KERN_ERR "Image is already free\n");
374
375 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000376 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100377
378 /* Free up resource memory */
379 kfree(resource);
380}
381EXPORT_SYMBOL(vme_slave_free);
382
383/*
384 * Request a master image with specific attributes, return some unique
385 * identifier.
386 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000387struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
388 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100389{
390 struct vme_bridge *bridge;
391 struct list_head *master_pos = NULL;
392 struct vme_master_resource *allocated_image = NULL;
393 struct vme_master_resource *master_image = NULL;
394 struct vme_resource *resource = NULL;
395
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200396 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100397 if (bridge == NULL) {
398 printk(KERN_ERR "Can't find VME bus\n");
399 goto err_bus;
400 }
401
402 /* Loop through master resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000403 list_for_each(master_pos, &bridge->master_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100404 master_image = list_entry(master_pos,
405 struct vme_master_resource, list);
406
407 if (master_image == NULL) {
408 printk(KERN_WARNING "Registered NULL master resource\n");
409 continue;
410 }
411
412 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000413 spin_lock(&master_image->lock);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000414 if (((master_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100415 ((master_image->cycle_attr & cycle) == cycle) &&
416 ((master_image->width_attr & dwidth) == dwidth) &&
417 (master_image->locked == 0)) {
418
419 master_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000420 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100421 allocated_image = master_image;
422 break;
423 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000424 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100425 }
426
427 /* Check to see if we found a resource */
428 if (allocated_image == NULL) {
429 printk(KERN_ERR "Can't find a suitable resource\n");
430 goto err_image;
431 }
432
433 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
434 if (resource == NULL) {
435 printk(KERN_ERR "Unable to allocate resource structure\n");
436 goto err_alloc;
437 }
438 resource->type = VME_MASTER;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000439 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100440
441 return resource;
442
Martyn Welcha17a75e2009-07-31 09:28:17 +0100443err_alloc:
444 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000445 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100446 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000447 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100448err_image:
449err_bus:
450 return NULL;
451}
452EXPORT_SYMBOL(vme_master_request);
453
Martyn Welchead1f3e2009-12-15 08:43:02 +0000454int vme_master_set(struct vme_resource *resource, int enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000455 unsigned long long vme_base, unsigned long long size, u32 aspace,
456 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100457{
458 struct vme_bridge *bridge = find_bridge(resource);
459 struct vme_master_resource *image;
460 int retval;
461
462 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000463 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100464 return -EINVAL;
465 }
466
467 image = list_entry(resource->entry, struct vme_master_resource, list);
468
469 if (bridge->master_set == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000470 printk(KERN_WARNING "vme_master_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100471 return -EINVAL;
472 }
473
Martyn Welchead1f3e2009-12-15 08:43:02 +0000474 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100475 ((image->cycle_attr & cycle) == cycle) &&
476 ((image->width_attr & dwidth) == dwidth))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000477 printk(KERN_WARNING "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100478 return -EINVAL;
479 }
480
481 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000482 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100483 return retval;
484
485 return bridge->master_set(image, enabled, vme_base, size, aspace,
486 cycle, dwidth);
487}
488EXPORT_SYMBOL(vme_master_set);
489
Martyn Welchead1f3e2009-12-15 08:43:02 +0000490int vme_master_get(struct vme_resource *resource, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000491 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
492 u32 *cycle, u32 *dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100493{
494 struct vme_bridge *bridge = find_bridge(resource);
495 struct vme_master_resource *image;
496
497 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000498 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100499 return -EINVAL;
500 }
501
502 image = list_entry(resource->entry, struct vme_master_resource, list);
503
Emilio G. Cota51a569f2009-08-10 16:52:42 +0200504 if (bridge->master_get == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000505 printk(KERN_WARNING "vme_master_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100506 return -EINVAL;
507 }
508
509 return bridge->master_get(image, enabled, vme_base, size, aspace,
510 cycle, dwidth);
511}
512EXPORT_SYMBOL(vme_master_get);
513
514/*
515 * Read data out of VME space into a buffer.
516 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000517ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100518 loff_t offset)
519{
520 struct vme_bridge *bridge = find_bridge(resource);
521 struct vme_master_resource *image;
522 size_t length;
523
524 if (bridge->master_read == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000525 printk(KERN_WARNING "Reading from resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100526 return -EINVAL;
527 }
528
529 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000530 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100531 return -EINVAL;
532 }
533
534 image = list_entry(resource->entry, struct vme_master_resource, list);
535
536 length = vme_get_size(resource);
537
538 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000539 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100540 return -EFAULT;
541 }
542
543 if ((offset + count) > length)
544 count = length - offset;
545
546 return bridge->master_read(image, buf, count, offset);
547
548}
549EXPORT_SYMBOL(vme_master_read);
550
551/*
552 * Write data out to VME space from a buffer.
553 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000554ssize_t vme_master_write(struct vme_resource *resource, void *buf,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100555 size_t count, loff_t offset)
556{
557 struct vme_bridge *bridge = find_bridge(resource);
558 struct vme_master_resource *image;
559 size_t length;
560
561 if (bridge->master_write == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000562 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100563 return -EINVAL;
564 }
565
566 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000567 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100568 return -EINVAL;
569 }
570
571 image = list_entry(resource->entry, struct vme_master_resource, list);
572
573 length = vme_get_size(resource);
574
575 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000576 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100577 return -EFAULT;
578 }
579
580 if ((offset + count) > length)
581 count = length - offset;
582
583 return bridge->master_write(image, buf, count, offset);
584}
585EXPORT_SYMBOL(vme_master_write);
586
587/*
588 * Perform RMW cycle to provided location.
589 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000590unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100591 unsigned int compare, unsigned int swap, loff_t offset)
592{
593 struct vme_bridge *bridge = find_bridge(resource);
594 struct vme_master_resource *image;
595
596 if (bridge->master_rmw == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000597 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100598 return -EINVAL;
599 }
600
601 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000602 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100603 return -EINVAL;
604 }
605
606 image = list_entry(resource->entry, struct vme_master_resource, list);
607
608 return bridge->master_rmw(image, mask, compare, swap, offset);
609}
610EXPORT_SYMBOL(vme_master_rmw);
611
612void vme_master_free(struct vme_resource *resource)
613{
614 struct vme_master_resource *master_image;
615
616 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000617 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100618 return;
619 }
620
621 master_image = list_entry(resource->entry, struct vme_master_resource,
622 list);
623 if (master_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000624 printk(KERN_ERR "Can't find master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100625 return;
626 }
627
628 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000629 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100630 if (master_image->locked == 0)
631 printk(KERN_ERR "Image is already free\n");
632
633 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000634 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100635
636 /* Free up resource memory */
637 kfree(resource);
638}
639EXPORT_SYMBOL(vme_master_free);
640
641/*
642 * Request a DMA controller with specific attributes, return some unique
643 * identifier.
644 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000645struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100646{
647 struct vme_bridge *bridge;
648 struct list_head *dma_pos = NULL;
649 struct vme_dma_resource *allocated_ctrlr = NULL;
650 struct vme_dma_resource *dma_ctrlr = NULL;
651 struct vme_resource *resource = NULL;
652
653 /* XXX Not checking resource attributes */
654 printk(KERN_ERR "No VME resource Attribute tests done\n");
655
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200656 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100657 if (bridge == NULL) {
658 printk(KERN_ERR "Can't find VME bus\n");
659 goto err_bus;
660 }
661
662 /* Loop through DMA resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000663 list_for_each(dma_pos, &bridge->dma_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100664 dma_ctrlr = list_entry(dma_pos,
665 struct vme_dma_resource, list);
666
667 if (dma_ctrlr == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000668 printk(KERN_ERR "Registered NULL DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100669 continue;
670 }
671
Martyn Welch4f723df2010-02-18 15:12:58 +0000672 /* Find an unlocked and compatible controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000673 mutex_lock(&dma_ctrlr->mtx);
Martyn Welch4f723df2010-02-18 15:12:58 +0000674 if (((dma_ctrlr->route_attr & route) == route) &&
675 (dma_ctrlr->locked == 0)) {
676
Martyn Welcha17a75e2009-07-31 09:28:17 +0100677 dma_ctrlr->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000678 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100679 allocated_ctrlr = dma_ctrlr;
680 break;
681 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000682 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100683 }
684
685 /* Check to see if we found a resource */
686 if (allocated_ctrlr == NULL)
687 goto err_ctrlr;
688
689 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
690 if (resource == NULL) {
691 printk(KERN_WARNING "Unable to allocate resource structure\n");
692 goto err_alloc;
693 }
694 resource->type = VME_DMA;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000695 resource->entry = &allocated_ctrlr->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100696
697 return resource;
698
699err_alloc:
700 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000701 mutex_lock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100702 dma_ctrlr->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000703 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100704err_ctrlr:
705err_bus:
706 return NULL;
707}
Martyn Welch58e50792009-10-29 16:35:20 +0000708EXPORT_SYMBOL(vme_dma_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100709
710/*
711 * Start new list
712 */
713struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
714{
715 struct vme_dma_resource *ctrlr;
716 struct vme_dma_list *dma_list;
717
718 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000719 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100720 return NULL;
721 }
722
723 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
724
Martyn Welchead1f3e2009-12-15 08:43:02 +0000725 dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
726 if (dma_list == NULL) {
727 printk(KERN_ERR "Unable to allocate memory for new dma list\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100728 return NULL;
729 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000730 INIT_LIST_HEAD(&dma_list->entries);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100731 dma_list->parent = ctrlr;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000732 mutex_init(&dma_list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100733
734 return dma_list;
735}
736EXPORT_SYMBOL(vme_new_dma_list);
737
738/*
739 * Create "Pattern" type attributes
740 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000741struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100742{
743 struct vme_dma_attr *attributes;
744 struct vme_dma_pattern *pattern_attr;
745
Martyn Welchead1f3e2009-12-15 08:43:02 +0000746 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
747 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700748 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100749 goto err_attr;
750 }
751
Martyn Welchead1f3e2009-12-15 08:43:02 +0000752 pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
753 if (pattern_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700754 printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100755 goto err_pat;
756 }
757
758 attributes->type = VME_DMA_PATTERN;
759 attributes->private = (void *)pattern_attr;
760
761 pattern_attr->pattern = pattern;
762 pattern_attr->type = type;
763
764 return attributes;
765
Martyn Welcha17a75e2009-07-31 09:28:17 +0100766err_pat:
767 kfree(attributes);
768err_attr:
769 return NULL;
770}
771EXPORT_SYMBOL(vme_dma_pattern_attribute);
772
773/*
774 * Create "PCI" type attributes
775 */
776struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
777{
778 struct vme_dma_attr *attributes;
779 struct vme_dma_pci *pci_attr;
780
781 /* XXX Run some sanity checks here */
782
Martyn Welchead1f3e2009-12-15 08:43:02 +0000783 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
784 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700785 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100786 goto err_attr;
787 }
788
Martyn Welchead1f3e2009-12-15 08:43:02 +0000789 pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
790 if (pci_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700791 printk(KERN_ERR "Unable to allocate memory for pci attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100792 goto err_pci;
793 }
794
795
796
797 attributes->type = VME_DMA_PCI;
798 attributes->private = (void *)pci_attr;
799
800 pci_attr->address = address;
801
802 return attributes;
803
Martyn Welcha17a75e2009-07-31 09:28:17 +0100804err_pci:
805 kfree(attributes);
806err_attr:
807 return NULL;
808}
809EXPORT_SYMBOL(vme_dma_pci_attribute);
810
811/*
812 * Create "VME" type attributes
813 */
814struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
Martyn Welch6af04b02011-12-01 17:06:29 +0000815 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100816{
817 struct vme_dma_attr *attributes;
818 struct vme_dma_vme *vme_attr;
819
Martyn Welchead1f3e2009-12-15 08:43:02 +0000820 attributes = kmalloc(
Martyn Welcha17a75e2009-07-31 09:28:17 +0100821 sizeof(struct vme_dma_attr), GFP_KERNEL);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000822 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700823 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100824 goto err_attr;
825 }
826
Martyn Welchead1f3e2009-12-15 08:43:02 +0000827 vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
828 if (vme_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700829 printk(KERN_ERR "Unable to allocate memory for vme attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100830 goto err_vme;
831 }
832
833 attributes->type = VME_DMA_VME;
834 attributes->private = (void *)vme_attr;
835
836 vme_attr->address = address;
837 vme_attr->aspace = aspace;
838 vme_attr->cycle = cycle;
839 vme_attr->dwidth = dwidth;
840
841 return attributes;
842
Martyn Welcha17a75e2009-07-31 09:28:17 +0100843err_vme:
844 kfree(attributes);
845err_attr:
846 return NULL;
847}
848EXPORT_SYMBOL(vme_dma_vme_attribute);
849
850/*
851 * Free attribute
852 */
853void vme_dma_free_attribute(struct vme_dma_attr *attributes)
854{
855 kfree(attributes->private);
856 kfree(attributes);
857}
858EXPORT_SYMBOL(vme_dma_free_attribute);
859
860int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
861 struct vme_dma_attr *dest, size_t count)
862{
863 struct vme_bridge *bridge = list->parent->parent;
864 int retval;
865
866 if (bridge->dma_list_add == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000867 printk(KERN_WARNING "Link List DMA generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100868 return -EINVAL;
869 }
870
Emilio G. Cota886953e2010-11-12 11:14:07 +0000871 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000872 printk(KERN_ERR "Link List already submitted\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100873 return -EINVAL;
874 }
875
876 retval = bridge->dma_list_add(list, src, dest, count);
877
Emilio G. Cota886953e2010-11-12 11:14:07 +0000878 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100879
880 return retval;
881}
882EXPORT_SYMBOL(vme_dma_list_add);
883
884int vme_dma_list_exec(struct vme_dma_list *list)
885{
886 struct vme_bridge *bridge = list->parent->parent;
887 int retval;
888
889 if (bridge->dma_list_exec == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000890 printk(KERN_ERR "Link List DMA execution not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100891 return -EINVAL;
892 }
893
Emilio G. Cota886953e2010-11-12 11:14:07 +0000894 mutex_lock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100895
896 retval = bridge->dma_list_exec(list);
897
Emilio G. Cota886953e2010-11-12 11:14:07 +0000898 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100899
900 return retval;
901}
902EXPORT_SYMBOL(vme_dma_list_exec);
903
904int vme_dma_list_free(struct vme_dma_list *list)
905{
906 struct vme_bridge *bridge = list->parent->parent;
907 int retval;
908
909 if (bridge->dma_list_empty == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000910 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100911 return -EINVAL;
912 }
913
Emilio G. Cota886953e2010-11-12 11:14:07 +0000914 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000915 printk(KERN_ERR "Link List in use\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100916 return -EINVAL;
917 }
918
919 /*
920 * Empty out all of the entries from the dma list. We need to go to the
921 * low level driver as dma entries are driver specific.
922 */
923 retval = bridge->dma_list_empty(list);
924 if (retval) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000925 printk(KERN_ERR "Unable to empty link-list entries\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +0000926 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100927 return retval;
928 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000929 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100930 kfree(list);
931
932 return retval;
933}
934EXPORT_SYMBOL(vme_dma_list_free);
935
936int vme_dma_free(struct vme_resource *resource)
937{
938 struct vme_dma_resource *ctrlr;
939
940 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000941 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100942 return -EINVAL;
943 }
944
945 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
946
Emilio G. Cota886953e2010-11-12 11:14:07 +0000947 if (!mutex_trylock(&ctrlr->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000948 printk(KERN_ERR "Resource busy, can't free\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100949 return -EBUSY;
950 }
951
Emilio G. Cota886953e2010-11-12 11:14:07 +0000952 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000953 printk(KERN_WARNING "Resource still processing transfers\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +0000954 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100955 return -EBUSY;
956 }
957
958 ctrlr->locked = 0;
959
Emilio G. Cota886953e2010-11-12 11:14:07 +0000960 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100961
Martyn Welchfd5c2562013-06-06 12:29:16 +0100962 kfree(resource);
963
Martyn Welcha17a75e2009-07-31 09:28:17 +0100964 return 0;
965}
966EXPORT_SYMBOL(vme_dma_free);
967
Martyn Welchc813f592009-10-29 16:34:54 +0000968void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
969{
970 void (*call)(int, int, void *);
971 void *priv_data;
972
973 call = bridge->irq[level - 1].callback[statid].func;
974 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
975
976 if (call != NULL)
977 call(level, statid, priv_data);
978 else
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700979 printk(KERN_WARNING "Spurilous VME interrupt, level:%x, vector:%x\n",
980 level, statid);
Martyn Welchc813f592009-10-29 16:34:54 +0000981}
982EXPORT_SYMBOL(vme_irq_handler);
983
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200984int vme_irq_request(struct vme_dev *vdev, int level, int statid,
Martyn Welch29848ac2010-02-18 15:13:05 +0000985 void (*callback)(int, int, void *),
Martyn Welcha17a75e2009-07-31 09:28:17 +0100986 void *priv_data)
987{
988 struct vme_bridge *bridge;
989
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200990 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100991 if (bridge == NULL) {
992 printk(KERN_ERR "Can't find VME bus\n");
993 return -EINVAL;
994 }
995
Martyn Welchead1f3e2009-12-15 08:43:02 +0000996 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +0000997 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100998 return -EINVAL;
999 }
1000
Martyn Welchc813f592009-10-29 16:34:54 +00001001 if (bridge->irq_set == NULL) {
1002 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001003 return -EINVAL;
1004 }
1005
Emilio G. Cota886953e2010-11-12 11:14:07 +00001006 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001007
1008 if (bridge->irq[level - 1].callback[statid].func) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001009 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001010 printk(KERN_WARNING "VME Interrupt already taken\n");
1011 return -EBUSY;
1012 }
1013
1014 bridge->irq[level - 1].count++;
1015 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1016 bridge->irq[level - 1].callback[statid].func = callback;
1017
1018 /* Enable IRQ level */
Martyn Welch29848ac2010-02-18 15:13:05 +00001019 bridge->irq_set(bridge, level, 1, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001020
Emilio G. Cota886953e2010-11-12 11:14:07 +00001021 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001022
1023 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001024}
Martyn Welchc813f592009-10-29 16:34:54 +00001025EXPORT_SYMBOL(vme_irq_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001026
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001027void vme_irq_free(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001028{
1029 struct vme_bridge *bridge;
1030
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001031 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001032 if (bridge == NULL) {
1033 printk(KERN_ERR "Can't find VME bus\n");
1034 return;
1035 }
1036
Martyn Welchead1f3e2009-12-15 08:43:02 +00001037 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001038 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001039 return;
1040 }
1041
Martyn Welchc813f592009-10-29 16:34:54 +00001042 if (bridge->irq_set == NULL) {
1043 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001044 return;
1045 }
1046
Emilio G. Cota886953e2010-11-12 11:14:07 +00001047 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001048
1049 bridge->irq[level - 1].count--;
1050
1051 /* Disable IRQ level if no more interrupts attached at this level*/
1052 if (bridge->irq[level - 1].count == 0)
Martyn Welch29848ac2010-02-18 15:13:05 +00001053 bridge->irq_set(bridge, level, 0, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001054
1055 bridge->irq[level - 1].callback[statid].func = NULL;
1056 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1057
Emilio G. Cota886953e2010-11-12 11:14:07 +00001058 mutex_unlock(&bridge->irq_mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001059}
Martyn Welchc813f592009-10-29 16:34:54 +00001060EXPORT_SYMBOL(vme_irq_free);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001061
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001062int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001063{
1064 struct vme_bridge *bridge;
1065
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001066 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001067 if (bridge == NULL) {
1068 printk(KERN_ERR "Can't find VME bus\n");
1069 return -EINVAL;
1070 }
1071
Martyn Welchead1f3e2009-12-15 08:43:02 +00001072 if ((level < 1) || (level > 7)) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001073 printk(KERN_WARNING "Invalid interrupt level\n");
1074 return -EINVAL;
1075 }
1076
Martyn Welchc813f592009-10-29 16:34:54 +00001077 if (bridge->irq_generate == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001078 printk(KERN_WARNING "Interrupt generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001079 return -EINVAL;
1080 }
1081
Martyn Welch29848ac2010-02-18 15:13:05 +00001082 return bridge->irq_generate(bridge, level, statid);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001083}
Martyn Welchc813f592009-10-29 16:34:54 +00001084EXPORT_SYMBOL(vme_irq_generate);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001085
Martyn Welch42fb5032009-08-11 17:44:56 +01001086/*
1087 * Request the location monitor, return resource or NULL
1088 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001089struct vme_resource *vme_lm_request(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001090{
1091 struct vme_bridge *bridge;
Martyn Welch42fb5032009-08-11 17:44:56 +01001092 struct list_head *lm_pos = NULL;
1093 struct vme_lm_resource *allocated_lm = NULL;
1094 struct vme_lm_resource *lm = NULL;
1095 struct vme_resource *resource = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001096
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001097 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001098 if (bridge == NULL) {
1099 printk(KERN_ERR "Can't find VME bus\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001100 goto err_bus;
1101 }
1102
1103 /* Loop through DMA resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001104 list_for_each(lm_pos, &bridge->lm_resources) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001105 lm = list_entry(lm_pos,
1106 struct vme_lm_resource, list);
1107
1108 if (lm == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001109 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001110 continue;
1111 }
1112
1113 /* Find an unlocked controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001114 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001115 if (lm->locked == 0) {
1116 lm->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001117 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001118 allocated_lm = lm;
1119 break;
1120 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001121 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001122 }
1123
1124 /* Check to see if we found a resource */
1125 if (allocated_lm == NULL)
1126 goto err_lm;
1127
1128 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
1129 if (resource == NULL) {
1130 printk(KERN_ERR "Unable to allocate resource structure\n");
1131 goto err_alloc;
1132 }
1133 resource->type = VME_LM;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001134 resource->entry = &allocated_lm->list;
Martyn Welch42fb5032009-08-11 17:44:56 +01001135
1136 return resource;
1137
1138err_alloc:
1139 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001140 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001141 lm->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001142 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001143err_lm:
1144err_bus:
1145 return NULL;
1146}
1147EXPORT_SYMBOL(vme_lm_request);
1148
1149int vme_lm_count(struct vme_resource *resource)
1150{
1151 struct vme_lm_resource *lm;
1152
1153 if (resource->type != VME_LM) {
1154 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001155 return -EINVAL;
1156 }
1157
Martyn Welch42fb5032009-08-11 17:44:56 +01001158 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1159
1160 return lm->monitors;
1161}
1162EXPORT_SYMBOL(vme_lm_count);
1163
1164int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001165 u32 aspace, u32 cycle)
Martyn Welch42fb5032009-08-11 17:44:56 +01001166{
1167 struct vme_bridge *bridge = find_bridge(resource);
1168 struct vme_lm_resource *lm;
1169
1170 if (resource->type != VME_LM) {
1171 printk(KERN_ERR "Not a Location Monitor resource\n");
1172 return -EINVAL;
1173 }
1174
1175 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1176
Martyn Welcha17a75e2009-07-31 09:28:17 +01001177 if (bridge->lm_set == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001178 printk(KERN_ERR "vme_lm_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001179 return -EINVAL;
1180 }
1181
Martyn Welch8be92262009-10-29 16:35:08 +00001182 return bridge->lm_set(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001183}
1184EXPORT_SYMBOL(vme_lm_set);
1185
Martyn Welch42fb5032009-08-11 17:44:56 +01001186int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001187 u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001188{
Martyn Welch42fb5032009-08-11 17:44:56 +01001189 struct vme_bridge *bridge = find_bridge(resource);
1190 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001191
Martyn Welch42fb5032009-08-11 17:44:56 +01001192 if (resource->type != VME_LM) {
1193 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001194 return -EINVAL;
1195 }
1196
Martyn Welch42fb5032009-08-11 17:44:56 +01001197 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1198
Martyn Welcha17a75e2009-07-31 09:28:17 +01001199 if (bridge->lm_get == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001200 printk(KERN_ERR "vme_lm_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001201 return -EINVAL;
1202 }
1203
Martyn Welch42fb5032009-08-11 17:44:56 +01001204 return bridge->lm_get(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001205}
1206EXPORT_SYMBOL(vme_lm_get);
1207
Martyn Welch42fb5032009-08-11 17:44:56 +01001208int vme_lm_attach(struct vme_resource *resource, int monitor,
1209 void (*callback)(int))
Martyn Welcha17a75e2009-07-31 09:28:17 +01001210{
Martyn Welch42fb5032009-08-11 17:44:56 +01001211 struct vme_bridge *bridge = find_bridge(resource);
1212 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001213
Martyn Welch42fb5032009-08-11 17:44:56 +01001214 if (resource->type != VME_LM) {
1215 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001216 return -EINVAL;
1217 }
1218
Martyn Welch42fb5032009-08-11 17:44:56 +01001219 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1220
Martyn Welcha17a75e2009-07-31 09:28:17 +01001221 if (bridge->lm_attach == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001222 printk(KERN_ERR "vme_lm_attach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001223 return -EINVAL;
1224 }
1225
Martyn Welch42fb5032009-08-11 17:44:56 +01001226 return bridge->lm_attach(lm, monitor, callback);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001227}
1228EXPORT_SYMBOL(vme_lm_attach);
1229
Martyn Welch42fb5032009-08-11 17:44:56 +01001230int vme_lm_detach(struct vme_resource *resource, int monitor)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001231{
Martyn Welch42fb5032009-08-11 17:44:56 +01001232 struct vme_bridge *bridge = find_bridge(resource);
1233 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001234
Martyn Welch42fb5032009-08-11 17:44:56 +01001235 if (resource->type != VME_LM) {
1236 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001237 return -EINVAL;
1238 }
1239
Martyn Welch42fb5032009-08-11 17:44:56 +01001240 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1241
Martyn Welcha17a75e2009-07-31 09:28:17 +01001242 if (bridge->lm_detach == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001243 printk(KERN_ERR "vme_lm_detach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001244 return -EINVAL;
1245 }
1246
Martyn Welch42fb5032009-08-11 17:44:56 +01001247 return bridge->lm_detach(lm, monitor);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001248}
1249EXPORT_SYMBOL(vme_lm_detach);
1250
Martyn Welch42fb5032009-08-11 17:44:56 +01001251void vme_lm_free(struct vme_resource *resource)
1252{
1253 struct vme_lm_resource *lm;
1254
1255 if (resource->type != VME_LM) {
1256 printk(KERN_ERR "Not a Location Monitor resource\n");
1257 return;
1258 }
1259
1260 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1261
Emilio G. Cota886953e2010-11-12 11:14:07 +00001262 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001263
Martyn Welch8be92262009-10-29 16:35:08 +00001264 /* XXX
1265 * Check to see that there aren't any callbacks still attached, if
1266 * there are we should probably be detaching them!
1267 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001268
1269 lm->locked = 0;
1270
Emilio G. Cota886953e2010-11-12 11:14:07 +00001271 mutex_unlock(&lm->mtx);
Martyn Welch8be92262009-10-29 16:35:08 +00001272
1273 kfree(resource);
Martyn Welch42fb5032009-08-11 17:44:56 +01001274}
1275EXPORT_SYMBOL(vme_lm_free);
1276
Martyn Welchd7729f02013-11-08 11:58:35 +00001277int vme_slot_num(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001278{
1279 struct vme_bridge *bridge;
1280
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001281 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001282 if (bridge == NULL) {
1283 printk(KERN_ERR "Can't find VME bus\n");
1284 return -EINVAL;
1285 }
1286
1287 if (bridge->slot_get == NULL) {
Martyn Welchd7729f02013-11-08 11:58:35 +00001288 printk(KERN_WARNING "vme_slot_num not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001289 return -EINVAL;
1290 }
1291
Martyn Welch29848ac2010-02-18 15:13:05 +00001292 return bridge->slot_get(bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001293}
Martyn Welchd7729f02013-11-08 11:58:35 +00001294EXPORT_SYMBOL(vme_slot_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001295
Martyn Welch978f47d2013-11-08 11:58:34 +00001296int vme_bus_num(struct vme_dev *vdev)
1297{
1298 struct vme_bridge *bridge;
1299
1300 bridge = vdev->bridge;
1301 if (bridge == NULL) {
1302 pr_err("Can't find VME bus\n");
1303 return -EINVAL;
1304 }
1305
1306 return bridge->num;
1307}
1308EXPORT_SYMBOL(vme_bus_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001309
1310/* - Bridge Registration --------------------------------------------------- */
1311
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001312static void vme_dev_release(struct device *dev)
1313{
1314 kfree(dev_to_vme_dev(dev));
1315}
1316
1317int vme_register_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001318{
1319 int i;
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001320 int ret = -1;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001321
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001322 mutex_lock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001323 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001324 if ((vme_bus_numbers & (1 << i)) == 0) {
1325 vme_bus_numbers |= (1 << i);
1326 bridge->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001327 INIT_LIST_HEAD(&bridge->devices);
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001328 list_add_tail(&bridge->bus_list, &vme_bus_list);
1329 ret = 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001330 break;
1331 }
1332 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001333 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001334
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001335 return ret;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001336}
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001337EXPORT_SYMBOL(vme_register_bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001338
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001339void vme_unregister_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001340{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001341 struct vme_dev *vdev;
1342 struct vme_dev *tmp;
1343
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001344 mutex_lock(&vme_buses_lock);
1345 vme_bus_numbers &= ~(1 << bridge->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001346 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1347 list_del(&vdev->drv_list);
1348 list_del(&vdev->bridge_list);
1349 device_unregister(&vdev->dev);
1350 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001351 list_del(&bridge->bus_list);
1352 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001353}
Martyn Welcha17a75e2009-07-31 09:28:17 +01001354EXPORT_SYMBOL(vme_unregister_bridge);
1355
Martyn Welcha17a75e2009-07-31 09:28:17 +01001356/* - Driver Registration --------------------------------------------------- */
1357
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001358static int __vme_register_driver_bus(struct vme_driver *drv,
1359 struct vme_bridge *bridge, unsigned int ndevs)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001360{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001361 int err;
1362 unsigned int i;
1363 struct vme_dev *vdev;
1364 struct vme_dev *tmp;
1365
1366 for (i = 0; i < ndevs; i++) {
1367 vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
1368 if (!vdev) {
1369 err = -ENOMEM;
1370 goto err_devalloc;
1371 }
Manohar Vangaa916a392011-09-26 11:27:17 +02001372 vdev->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001373 vdev->bridge = bridge;
1374 vdev->dev.platform_data = drv;
1375 vdev->dev.release = vme_dev_release;
1376 vdev->dev.parent = bridge->parent;
1377 vdev->dev.bus = &vme_bus_type;
Manohar Vangaa916a392011-09-26 11:27:17 +02001378 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1379 vdev->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001380
1381 err = device_register(&vdev->dev);
1382 if (err)
1383 goto err_reg;
1384
1385 if (vdev->dev.platform_data) {
1386 list_add_tail(&vdev->drv_list, &drv->devices);
1387 list_add_tail(&vdev->bridge_list, &bridge->devices);
1388 } else
1389 device_unregister(&vdev->dev);
1390 }
1391 return 0;
1392
1393err_reg:
Emilio G. Cotadef18202013-02-13 13:47:54 -05001394 put_device(&vdev->dev);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001395 kfree(vdev);
1396err_devalloc:
1397 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1398 list_del(&vdev->drv_list);
1399 list_del(&vdev->bridge_list);
1400 device_unregister(&vdev->dev);
1401 }
1402 return err;
1403}
1404
1405static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1406{
1407 struct vme_bridge *bridge;
1408 int err = 0;
1409
1410 mutex_lock(&vme_buses_lock);
1411 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1412 /*
1413 * This cannot cause trouble as we already have vme_buses_lock
1414 * and if the bridge is removed, it will have to go through
1415 * vme_unregister_bridge() to do it (which calls remove() on
1416 * the bridge which in turn tries to acquire vme_buses_lock and
Manohar Vangac26f6112011-11-04 11:12:29 +01001417 * will have to wait).
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001418 */
1419 err = __vme_register_driver_bus(drv, bridge, ndevs);
1420 if (err)
1421 break;
1422 }
1423 mutex_unlock(&vme_buses_lock);
1424 return err;
1425}
1426
1427int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1428{
1429 int err;
1430
Martyn Welcha17a75e2009-07-31 09:28:17 +01001431 drv->driver.name = drv->name;
1432 drv->driver.bus = &vme_bus_type;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001433 INIT_LIST_HEAD(&drv->devices);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001434
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001435 err = driver_register(&drv->driver);
1436 if (err)
1437 return err;
1438
1439 err = __vme_register_driver(drv, ndevs);
1440 if (err)
1441 driver_unregister(&drv->driver);
1442
1443 return err;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001444}
1445EXPORT_SYMBOL(vme_register_driver);
1446
Martyn Welchead1f3e2009-12-15 08:43:02 +00001447void vme_unregister_driver(struct vme_driver *drv)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001448{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001449 struct vme_dev *dev, *dev_tmp;
1450
1451 mutex_lock(&vme_buses_lock);
1452 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1453 list_del(&dev->drv_list);
1454 list_del(&dev->bridge_list);
1455 device_unregister(&dev->dev);
1456 }
1457 mutex_unlock(&vme_buses_lock);
1458
Martyn Welcha17a75e2009-07-31 09:28:17 +01001459 driver_unregister(&drv->driver);
1460}
1461EXPORT_SYMBOL(vme_unregister_driver);
1462
1463/* - Bus Registration ------------------------------------------------------ */
1464
Martyn Welcha17a75e2009-07-31 09:28:17 +01001465static int vme_bus_match(struct device *dev, struct device_driver *drv)
1466{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001467 struct vme_driver *vme_drv;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001468
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001469 vme_drv = container_of(drv, struct vme_driver, driver);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001470
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001471 if (dev->platform_data == vme_drv) {
1472 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001473
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001474 if (vme_drv->match && vme_drv->match(vdev))
1475 return 1;
1476
1477 dev->platform_data = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001478 }
Martyn Welcha17a75e2009-07-31 09:28:17 +01001479 return 0;
1480}
1481
1482static int vme_bus_probe(struct device *dev)
1483{
Martyn Welcha17a75e2009-07-31 09:28:17 +01001484 int retval = -ENODEV;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001485 struct vme_driver *driver;
1486 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001487
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001488 driver = dev->platform_data;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001489
Martyn Welchead1f3e2009-12-15 08:43:02 +00001490 if (driver->probe != NULL)
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001491 retval = driver->probe(vdev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001492
1493 return retval;
1494}
1495
1496static int vme_bus_remove(struct device *dev)
1497{
Martyn Welcha17a75e2009-07-31 09:28:17 +01001498 int retval = -ENODEV;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001499 struct vme_driver *driver;
1500 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001501
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001502 driver = dev->platform_data;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001503
Martyn Welchead1f3e2009-12-15 08:43:02 +00001504 if (driver->remove != NULL)
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001505 retval = driver->remove(vdev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001506
1507 return retval;
1508}
1509
1510struct bus_type vme_bus_type = {
1511 .name = "vme",
1512 .match = vme_bus_match,
1513 .probe = vme_bus_probe,
1514 .remove = vme_bus_remove,
1515};
1516EXPORT_SYMBOL(vme_bus_type);
1517
Martyn Welchead1f3e2009-12-15 08:43:02 +00001518static int __init vme_init(void)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001519{
1520 return bus_register(&vme_bus_type);
1521}
1522
Martyn Welchead1f3e2009-12-15 08:43:02 +00001523static void __exit vme_exit(void)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001524{
1525 bus_unregister(&vme_bus_type);
1526}
1527
Aaron Sierrac326cc02013-12-09 09:54:42 -06001528subsys_initcall(vme_init);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001529module_exit(vme_exit);