source: mainline/uspace/lib/drv/generic/interrupt.c@ 56fd7cf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 56fd7cf was 56fd7cf, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Make ddf_dev_t and ddf_fun_t opaque. This further tighthens the DDF interface.

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * Copyright (c) 2010 Lenka Trochtova
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/**
30 * @defgroup libdrv generic device driver support.
31 * @brief HelenOS generic device driver support.
32 * @{
33 */
34
35/** @file
36 */
37
38#include <async.h>
39#include <errno.h>
40#include <sys/types.h>
41
42#include "ddf/interrupt.h"
43#include "private/driver.h"
44
45static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall);
46static interrupt_context_t *create_interrupt_context(void);
47static void delete_interrupt_context(interrupt_context_t *ctx);
48static void init_interrupt_context_list(interrupt_context_list_t *list);
49static void add_interrupt_context(interrupt_context_list_t *list,
50 interrupt_context_t *ctx);
51static void remove_interrupt_context(interrupt_context_list_t *list,
52 interrupt_context_t *ctx);
53static interrupt_context_t *find_interrupt_context_by_id(
54 interrupt_context_list_t *list, int id);
55static interrupt_context_t *find_interrupt_context(
56 interrupt_context_list_t *list, ddf_dev_t *dev, int irq);
57int register_interrupt_handler(ddf_dev_t *dev, int irq,
58 interrupt_handler_t *handler, irq_code_t *pseudocode);
59int unregister_interrupt_handler(ddf_dev_t *dev, int irq);
60
61/** Interrupts */
62static interrupt_context_list_t interrupt_contexts;
63
64static irq_cmd_t default_cmds[] = {
65 {
66 .cmd = CMD_ACCEPT
67 }
68};
69
70static irq_code_t default_pseudocode = {
71 0,
72 NULL,
73 sizeof(default_cmds) / sizeof(irq_cmd_t),
74 default_cmds
75};
76
77void interrupt_init(void)
78{
79 /* Initialize the list of interrupt contexts. */
80 init_interrupt_context_list(&interrupt_contexts);
81
82 /* Set generic interrupt handler. */
83 async_set_interrupt_received(driver_irq_handler);
84}
85
86static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
87{
88 int id = (int)IPC_GET_IMETHOD(*icall);
89 interrupt_context_t *ctx;
90
91 ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
92 if (ctx != NULL && ctx->handler != NULL)
93 (*ctx->handler)(ctx->dev, iid, icall);
94}
95
96static interrupt_context_t *create_interrupt_context(void)
97{
98 interrupt_context_t *ctx;
99
100 ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
101 if (ctx != NULL)
102 memset(ctx, 0, sizeof(interrupt_context_t));
103
104 return ctx;
105}
106
107static void delete_interrupt_context(interrupt_context_t *ctx)
108{
109 if (ctx != NULL)
110 free(ctx);
111}
112
113static void init_interrupt_context_list(interrupt_context_list_t *list)
114{
115 memset(list, 0, sizeof(interrupt_context_list_t));
116 fibril_mutex_initialize(&list->mutex);
117 list_initialize(&list->contexts);
118}
119
120static void add_interrupt_context(interrupt_context_list_t *list,
121 interrupt_context_t *ctx)
122{
123 fibril_mutex_lock(&list->mutex);
124 ctx->id = list->curr_id++;
125 list_append(&ctx->link, &list->contexts);
126 fibril_mutex_unlock(&list->mutex);
127}
128
129static void remove_interrupt_context(interrupt_context_list_t *list,
130 interrupt_context_t *ctx)
131{
132 fibril_mutex_lock(&list->mutex);
133 list_remove(&ctx->link);
134 fibril_mutex_unlock(&list->mutex);
135}
136
137static interrupt_context_t *find_interrupt_context_by_id(
138 interrupt_context_list_t *list, int id)
139{
140 interrupt_context_t *ctx;
141
142 fibril_mutex_lock(&list->mutex);
143
144 list_foreach(list->contexts, link) {
145 ctx = list_get_instance(link, interrupt_context_t, link);
146 if (ctx->id == id) {
147 fibril_mutex_unlock(&list->mutex);
148 return ctx;
149 }
150 }
151
152 fibril_mutex_unlock(&list->mutex);
153 return NULL;
154}
155
156static interrupt_context_t *find_interrupt_context(
157 interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
158{
159 interrupt_context_t *ctx;
160
161 fibril_mutex_lock(&list->mutex);
162
163 list_foreach(list->contexts, link) {
164 ctx = list_get_instance(link, interrupt_context_t, link);
165 if (ctx->irq == irq && ctx->dev == dev) {
166 fibril_mutex_unlock(&list->mutex);
167 return ctx;
168 }
169 }
170
171 fibril_mutex_unlock(&list->mutex);
172 return NULL;
173}
174
175
176int register_interrupt_handler(ddf_dev_t *dev, int irq,
177 interrupt_handler_t *handler, irq_code_t *pseudocode)
178{
179 interrupt_context_t *ctx = create_interrupt_context();
180
181 ctx->dev = dev;
182 ctx->irq = irq;
183 ctx->handler = handler;
184
185 add_interrupt_context(&interrupt_contexts, ctx);
186
187 if (pseudocode == NULL)
188 pseudocode = &default_pseudocode;
189
190 int res = irq_register(irq, dev->handle, ctx->id, pseudocode);
191 if (res != EOK) {
192 remove_interrupt_context(&interrupt_contexts, ctx);
193 delete_interrupt_context(ctx);
194 }
195
196 return res;
197}
198
199int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
200{
201 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
202 dev, irq);
203 int res = irq_unregister(irq, dev->handle);
204
205 if (ctx != NULL) {
206 remove_interrupt_context(&interrupt_contexts, ctx);
207 delete_interrupt_context(ctx);
208 }
209
210 return res;
211}
212
213/**
214 * @}
215 */
Note: See TracBrowser for help on using the repository browser.