source: mainline/uspace/lib/libdrv/include/driver.h@ 7f8b581

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7f8b581 was 7f8b581, checked in by Lenka Trochtova <trochtova.lenka@…>, 15 years ago

provide a mechanism which enables the driver to register several interrupt handlers for several devices and irqs

  • Property mode set to 100644
File size: 7.1 KB
RevLine 
[c16cf62]1/*
[a1769ee]2 * Copyright (c) 2010 Lenka Trochtova
[c16cf62]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libdrv
30 * @{
31 */
32/** @file
33 */
34#ifndef LIBDRV_DRIVER_H_
35#define LIBDRV_DRIVER_H_
36
[084ff99]37
38#include <adt/list.h>
[7f8b581]39#include <ipc/ipc.h>
[bda60d9]40#include <ipc/devman.h>
[a1769ee]41#include <ipc/dev_iface.h>
[3843ecb]42#include <device/hw_res.h>
[52b7b1bb]43#include <assert.h>
[cfe7716]44#include <ddi.h>
45#include <libarch/ddi.h>
46#include <fibril_synch.h>
47#include <malloc.h>
[084ff99]48
[a1769ee]49struct device;
50typedef struct device device_t;
51
52// device interface
53
54// first two parameters: device and interface structure registered by the devices driver
55typedef void remote_iface_func_t(device_t*, void *, ipc_callid_t, ipc_call_t *);
56typedef remote_iface_func_t *remote_iface_func_ptr_t;
57
58typedef struct {
[52b7b1bb]59 size_t method_count;
[a1769ee]60 remote_iface_func_ptr_t *methods;
61} remote_iface_t;
62
63typedef struct {
64 remote_iface_t * ifaces[DEV_IFACE_COUNT];
65} iface_dipatch_table_t;
66
67
[3843ecb]68static inline bool is_valid_iface_idx(int idx)
[a1769ee]69{
[3843ecb]70 return 0 <= idx && idx < DEV_IFACE_MAX;
[a1769ee]71}
72
[3843ecb]73remote_iface_t* get_remote_iface(int idx);
[52b7b1bb]74remote_iface_func_ptr_t get_remote_method(remote_iface_t *rem_iface, ipcarg_t iface_method_idx);
75
76
[3843ecb]77// device class
78
79/** Devices belonging to the same class should implement the same set of interfaces.*/
80typedef struct device_class {
81 /** Unique identification of the class. */
82 int id;
83 /** The table of interfaces implemented by the device. */
84 void *interfaces[DEV_IFACE_COUNT];
85} device_class_t;
86
87
[a1769ee]88// device
89
[52b7b1bb]90/** The device. */
[a1769ee]91struct device {
[52b7b1bb]92 /** Globally unique device identifier (assigned to the device by the device manager). */
[bda60d9]93 device_handle_t handle;
[5e598e0]94 /** The phone to the parent device driver (if it is different from this driver).*/
[9a66bc2e]95 int parent_phone;
[5e598e0]96 /** Parent device if handled by this driver, NULL otherwise. */
97 device_t *parent;
[52b7b1bb]98 /** The device's name.*/
[bda60d9]99 const char *name;
[52b7b1bb]100 /** The list of device ids for device-to-driver matching.*/
[bda60d9]101 match_id_list_t match_ids;
[52b7b1bb]102 /** The device driver's data associated with this device.*/
[eff1a590]103 void *driver_data;
[3843ecb]104 /** Device class consist of class id and table of interfaces supported by the device.*/
105 device_class_t *class;
[52b7b1bb]106 /** Pointer to the previous and next device in the list of devices handled by the driver */
[084ff99]107 link_t link;
[a1769ee]108};
[c16cf62]109
[a1769ee]110
111// driver
112
[52b7b1bb]113/** Generic device driver operations. */
[a1769ee]114typedef struct driver_ops {
[52b7b1bb]115 /** Callback method for passing a new device to the device driver.*/
[df747b9c]116 int (*add_device)(device_t *dev);
[c16cf62]117 // TODO add other generic driver operations
118} driver_ops_t;
119
[52b7b1bb]120/** The driver structure.*/
[c16cf62]121typedef struct driver {
[52b7b1bb]122 /** The name of the device driver. */
[c16cf62]123 const char *name;
[52b7b1bb]124 /** Generic device driver operations. */
[c16cf62]125 driver_ops_t *driver_ops;
126} driver_t;
127
128int driver_main(driver_t *drv);
129
[52b7b1bb]130/** Create new device structure.
131 *
132 * @return the device structure.
133 */
[7707954]134static inline device_t * create_device()
135{
136 device_t *dev = malloc(sizeof(device_t));
137 if (NULL != dev) {
138 memset(dev, 0, sizeof(device_t));
[5af21c5]139 init_match_ids(&dev->match_ids);
140 }
[7707954]141 return dev;
142}
143
[52b7b1bb]144/** Delete device structure.
145 *
146 * @param dev the device structure.
147 */
[a1769ee]148static inline void delete_device(device_t *dev)
149{
[bda60d9]150 clean_match_ids(&dev->match_ids);
151 if (NULL != dev->name) {
152 free(dev->name);
153 }
154 free(dev);
155}
[7707954]156
[3843ecb]157static inline void * device_get_iface(device_t *dev, dev_inferface_idx_t idx)
[a1769ee]158{
[3843ecb]159 assert(is_valid_iface_idx(idx));
[a1769ee]160
[3843ecb]161 return dev->class->interfaces[idx];
[52b7b1bb]162}
163
[df747b9c]164int child_device_register(device_t *child, device_t *parent);
[7707954]165
[cfe7716]166// interrupts
167
168typedef void interrupt_handler_t(device_t *dev, ipc_callid_t iid, ipc_call_t *icall);
169
170typedef struct interrupt_context {
171 int id;
172 device_t *dev;
173 int irq;
174 interrupt_handler_t *handler;
175 link_t link;
176} interrupt_context_t;
177
178typedef struct interrupt_context_list {
179 int curr_id;
180 link_t contexts;
181 fibril_mutex_t mutex;
182} interrupt_context_list_t;
183
184static inline interrupt_context_t * create_interrupt_context()
185{
186 interrupt_context_t *ctx = (interrupt_context_t *)malloc(sizeof(interrupt_context_t));
187 if (NULL != ctx) {
188 memset(ctx, 0, sizeof(interrupt_context_t));
189 }
190 return ctx;
191}
192
193static inline void delete_interrupt_context(interrupt_context_t *ctx)
194{
195 if (NULL != ctx) {
196 free(ctx);
197 }
198}
199
200static inline void init_interrupt_context_list(interrupt_context_list_t *list)
201{
202 memset(list, 0, sizeof(interrupt_context_list_t));
203 fibril_mutex_initialize(&list->mutex);
204 list_initialize(&list->contexts);
205}
206
207static inline void add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
208{
209 fibril_mutex_lock(&list->mutex);
210
211 ctx->id = list->curr_id++;
212 list_append(&ctx->link, &list->contexts);
213
214 fibril_mutex_unlock(&list->mutex);
215}
216
217static inline void remove_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
218{
219 fibril_mutex_lock(&list->mutex);
220
221 list_remove(&ctx->link);
222
223 fibril_mutex_unlock(&list->mutex);
224}
225
226static inline interrupt_context_t *find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
227{
228 fibril_mutex_lock(&list->mutex);
229 link_t *link = list->contexts.next;
230 interrupt_context_t *ctx;
231
232 while (link != &list->contexts) {
233 ctx = list_get_instance(link, interrupt_context_t, link);
234 if (id == ctx->id) {
235 fibril_mutex_unlock(&list->mutex);
236 return ctx;
237 }
238 link = link->next;
239 }
240 fibril_mutex_unlock(&list->mutex);
241 return NULL;
242}
243
244static inline interrupt_context_t *find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)
245{
246 fibril_mutex_lock(&list->mutex);
247 link_t *link = list->contexts.next;
248 interrupt_context_t *ctx;
249
250 while (link != &list->contexts) {
251 ctx = list_get_instance(link, interrupt_context_t, link);
252 if (irq == ctx->irq && dev == ctx->dev) {
253 fibril_mutex_unlock(&list->mutex);
254 return ctx;
255 }
256 link = link->next;
257 }
258 fibril_mutex_unlock(&list->mutex);
259 return NULL;
260}
[7707954]261
[7f8b581]262int register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler, irq_code_t *pseudocode);
263int unregister_interrupt_handler(device_t *dev, int irq);
264
[c16cf62]265#endif
266
267/**
268 * @}
[a1769ee]269 */
Note: See TracBrowser for help on using the repository browser.