libmetal
device.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of Xilinx nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without
16  * specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * @file device.h
33  * @brief Bus abstraction for libmetal.
34  */
35 
36 #ifndef __METAL_BUS__H__
37 #define __METAL_BUS__H__
38 
39 #include <stdint.h>
40 #include "metal/io.h"
41 #include "metal/list.h"
42 #include "metal/dma.h"
43 #include "metal/sys.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
52 #ifndef METAL_MAX_DEVICE_REGIONS
53 #define METAL_MAX_DEVICE_REGIONS 32
54 #endif
55 
56 struct metal_bus;
57 struct metal_device;
58 
60 struct metal_bus_ops {
61  void (*bus_close)(struct metal_bus *bus);
62  int (*dev_open)(struct metal_bus *bus,
63  const char *dev_name,
64  struct metal_device **device);
65  void (*dev_close)(struct metal_bus *bus,
66  struct metal_device *device);
67  void (*dev_irq_ack)(struct metal_bus *bus,
68  struct metal_device *device,
69  int irq);
70  int (*dev_dma_map)(struct metal_bus *bus,
71  struct metal_device *device,
72  uint32_t dir,
73  struct metal_sg *sg_in,
74  int nents_in,
75  struct metal_sg *sg_out);
76  void (*dev_dma_unmap)(struct metal_bus *bus,
77  struct metal_device *device,
78  uint32_t dir,
79  struct metal_sg *sg,
80  int nents);
81 };
82 
84 struct metal_bus {
85  const char *name;
88  struct metal_list node;
89 };
90 
92 extern struct metal_bus metal_generic_bus;
93 
95 struct metal_device {
96  const char *name;
97  struct metal_bus *bus;
98  unsigned num_regions;
102  struct metal_list node;
103  int irq_num;
104  void *irq_info;
105 };
106 
112 extern int metal_bus_register(struct metal_bus *bus);
113 
119 extern int metal_bus_unregister(struct metal_bus *bus);
120 
127 extern int metal_bus_find(const char *name, struct metal_bus **bus);
128 
144 extern int metal_register_generic_device(struct metal_device *device);
145 
153 extern int metal_device_open(const char *bus_name, const char *dev_name,
154  struct metal_device **device);
155 
160 extern void metal_device_close(struct metal_device *device);
161 
169 static inline struct metal_io_region *
170 metal_device_io_region(struct metal_device *device, unsigned index)
171 {
172  return (index < device->num_regions
173  ? &device->regions[index]
174  : NULL);
175 }
176 
179 #ifdef METAL_INTERNAL
180 extern int metal_generic_dev_sys_open(struct metal_device *dev);
181 #endif /* METAL_INTERNAL */
182 
183 #ifdef __cplusplus
184 }
185 #endif
186 
187 #endif /* __METAL_BUS__H__ */
void(* dev_dma_unmap)(struct metal_bus *bus, struct metal_device *device, uint32_t dir, struct metal_sg *sg, int nents)
Definition: device.h:76
int(* dev_open)(struct metal_bus *bus, const char *dev_name, struct metal_device **device)
Definition: device.h:62
int(* dev_dma_map)(struct metal_bus *bus, struct metal_device *device, uint32_t dir, struct metal_sg *sg_in, int nents_in, struct metal_sg *sg_out)
Definition: device.h:70
void(* bus_close)(struct metal_bus *bus)
Definition: device.h:61
unsigned num_regions
Definition: device.h:98
struct metal_io_region regions[METAL_MAX_DEVICE_REGIONS]
Definition: device.h:100
struct metal_list node
Definition: device.h:102
void(* dev_irq_ack)(struct metal_bus *bus, struct metal_device *device, int irq)
Definition: device.h:67
int metal_bus_find(const char *name, struct metal_bus **bus)
Find a libmetal bus by name.
Definition: device.c:70
Definition: device.h:95
void metal_device_close(struct metal_device *device)
Close a libmetal device.
Definition: device.c:114
struct metal_bus_ops ops
Definition: device.h:86
const char * name
Definition: device.h:85
struct metal_bus metal_generic_bus
Definition: device.c:193
Definition: list.h:48
const char * name
Definition: device.h:96
#define METAL_MAX_DEVICE_REGIONS
Definition: device.h:53
Definition: device.h:60
void(* dev_close)(struct metal_bus *bus, struct metal_device *device)
Definition: device.h:65
int metal_generic_dev_sys_open(struct metal_device *dev)
Definition: device.c:40
int metal_device_open(const char *bus_name, const char *dev_name, struct metal_device **device)
Open a libmetal device by name.
Definition: device.c:86
int irq_num
Definition: device.h:103
Definition: device.h:84
struct metal_list devices
Definition: device.h:87
static struct metal_io_region * metal_device_io_region(struct metal_device *device, unsigned index)
Get an I/O region accessor for a device region.
Definition: device.h:170
Definition: io.h:86
int metal_bus_unregister(struct metal_bus *bus)
Unregister a libmetal bus.
Definition: device.c:53
void * irq_info
Definition: device.h:104
int metal_bus_register(struct metal_bus *bus)
Register a libmetal bus.
Definition: device.c:41
scatter/gather list element structure
Definition: dma.h:56
struct metal_bus * bus
Definition: device.h:97
struct metal_list node
Definition: device.h:88
int metal_register_generic_device(struct metal_device *device)
Statically register a generic libmetal device.
Definition: device.c:123