source: mainline/uspace/lib/drv/generic/interrupt.c@ 4982d87

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

Simplify use of list_foreach.

  • Property mode set to 100644
File size: 5.8 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 fibril_mutex_lock(&list->mutex);
141
142 list_foreach(list->contexts, link, interrupt_context_t, ctx) {
143 if (ctx->id == id) {
144 fibril_mutex_unlock(&list->mutex);
145 return ctx;
146 }
147 }
148
149 fibril_mutex_unlock(&list->mutex);
150 return NULL;
151}
152
153static interrupt_context_t *find_interrupt_context(
154 interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
155{
156 fibril_mutex_lock(&list->mutex);
157
158 list_foreach(list->contexts, link, interrupt_context_t, ctx) {
159 if (ctx->irq == irq && ctx->dev == dev) {
160 fibril_mutex_unlock(&list->mutex);
161 return ctx;
162 }
163 }
164
165 fibril_mutex_unlock(&list->mutex);
166 return NULL;
167}
168
169
170int register_interrupt_handler(ddf_dev_t *dev, int irq,
171 interrupt_handler_t *handler, irq_code_t *pseudocode)
172{
173 interrupt_context_t *ctx = create_interrupt_context();
174
175 ctx->dev = dev;
176 ctx->irq = irq;
177 ctx->handler = handler;
178
179 add_interrupt_context(&interrupt_contexts, ctx);
180
181 if (pseudocode == NULL)
182 pseudocode = &default_pseudocode;
183
184 int res = irq_register(irq, dev->handle, ctx->id, pseudocode);
185 if (res != EOK) {
186 remove_interrupt_context(&interrupt_contexts, ctx);
187 delete_interrupt_context(ctx);
188 }
189
190 return res;
191}
192
193int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
194{
195 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
196 dev, irq);
197 int res = irq_unregister(irq, dev->handle);
198
199 if (ctx != NULL) {
200 remove_interrupt_context(&interrupt_contexts, ctx);
201 delete_interrupt_context(ctx);
202 }
203
204 return res;
205}
206
207/**
208 * @}
209 */
Note: See TracBrowser for help on using the repository browser.