| 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 |
|
|---|
| 44 | static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall);
|
|---|
| 45 | static interrupt_context_t *create_interrupt_context(void);
|
|---|
| 46 | static void delete_interrupt_context(interrupt_context_t *ctx);
|
|---|
| 47 | static void init_interrupt_context_list(interrupt_context_list_t *list);
|
|---|
| 48 | static void add_interrupt_context(interrupt_context_list_t *list,
|
|---|
| 49 | interrupt_context_t *ctx);
|
|---|
| 50 | static void remove_interrupt_context(interrupt_context_list_t *list,
|
|---|
| 51 | interrupt_context_t *ctx);
|
|---|
| 52 | static interrupt_context_t *find_interrupt_context_by_id(
|
|---|
| 53 | interrupt_context_list_t *list, int id);
|
|---|
| 54 | static interrupt_context_t *find_interrupt_context(
|
|---|
| 55 | interrupt_context_list_t *list, ddf_dev_t *dev, int irq);
|
|---|
| 56 | int register_interrupt_handler(ddf_dev_t *dev, int irq,
|
|---|
| 57 | interrupt_handler_t *handler, irq_code_t *pseudocode);
|
|---|
| 58 | int unregister_interrupt_handler(ddf_dev_t *dev, int irq);
|
|---|
| 59 |
|
|---|
| 60 | /** Interrupts */
|
|---|
| 61 | static interrupt_context_list_t interrupt_contexts;
|
|---|
| 62 |
|
|---|
| 63 | static irq_cmd_t default_cmds[] = {
|
|---|
| 64 | {
|
|---|
| 65 | .cmd = CMD_ACCEPT
|
|---|
| 66 | }
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | static irq_code_t default_pseudocode = {
|
|---|
| 70 | sizeof(default_cmds) / sizeof(irq_cmd_t),
|
|---|
| 71 | default_cmds
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | void interrupt_init(void)
|
|---|
| 75 | {
|
|---|
| 76 | /* Initialize the list of interrupt contexts. */
|
|---|
| 77 | init_interrupt_context_list(&interrupt_contexts);
|
|---|
| 78 |
|
|---|
| 79 | /* Set generic interrupt handler. */
|
|---|
| 80 | async_set_interrupt_received(driver_irq_handler);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 84 | {
|
|---|
| 85 | int id = (int)IPC_GET_IMETHOD(*icall);
|
|---|
| 86 | interrupt_context_t *ctx;
|
|---|
| 87 |
|
|---|
| 88 | ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
|
|---|
| 89 | if (ctx != NULL && ctx->handler != NULL)
|
|---|
| 90 | (*ctx->handler)(ctx->dev, iid, icall);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | static interrupt_context_t *create_interrupt_context(void)
|
|---|
| 94 | {
|
|---|
| 95 | interrupt_context_t *ctx;
|
|---|
| 96 |
|
|---|
| 97 | ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
|
|---|
| 98 | if (ctx != NULL)
|
|---|
| 99 | memset(ctx, 0, sizeof(interrupt_context_t));
|
|---|
| 100 |
|
|---|
| 101 | return ctx;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | static void delete_interrupt_context(interrupt_context_t *ctx)
|
|---|
| 105 | {
|
|---|
| 106 | if (ctx != NULL)
|
|---|
| 107 | free(ctx);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | static void init_interrupt_context_list(interrupt_context_list_t *list)
|
|---|
| 111 | {
|
|---|
| 112 | memset(list, 0, sizeof(interrupt_context_list_t));
|
|---|
| 113 | fibril_mutex_initialize(&list->mutex);
|
|---|
| 114 | list_initialize(&list->contexts);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | static void
|
|---|
| 118 | add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
|
|---|
| 119 | {
|
|---|
| 120 | fibril_mutex_lock(&list->mutex);
|
|---|
| 121 | ctx->id = list->curr_id++;
|
|---|
| 122 | list_append(&ctx->link, &list->contexts);
|
|---|
| 123 | fibril_mutex_unlock(&list->mutex);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | static void remove_interrupt_context(interrupt_context_list_t *list,
|
|---|
| 127 | interrupt_context_t *ctx)
|
|---|
| 128 | {
|
|---|
| 129 | fibril_mutex_lock(&list->mutex);
|
|---|
| 130 | list_remove(&ctx->link);
|
|---|
| 131 | fibril_mutex_unlock(&list->mutex);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | static interrupt_context_t *
|
|---|
| 135 | find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
|
|---|
| 136 | {
|
|---|
| 137 | interrupt_context_t *ctx;
|
|---|
| 138 |
|
|---|
| 139 | fibril_mutex_lock(&list->mutex);
|
|---|
| 140 |
|
|---|
| 141 | list_foreach(list->contexts, link) {
|
|---|
| 142 | ctx = list_get_instance(link, interrupt_context_t, link);
|
|---|
| 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 |
|
|---|
| 153 | static interrupt_context_t *
|
|---|
| 154 | find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
|
|---|
| 155 | {
|
|---|
| 156 | interrupt_context_t *ctx;
|
|---|
| 157 |
|
|---|
| 158 | fibril_mutex_lock(&list->mutex);
|
|---|
| 159 |
|
|---|
| 160 | list_foreach(list->contexts, link) {
|
|---|
| 161 | ctx = list_get_instance(link, interrupt_context_t, link);
|
|---|
| 162 | if (ctx->irq == irq && ctx->dev == dev) {
|
|---|
| 163 | fibril_mutex_unlock(&list->mutex);
|
|---|
| 164 | return ctx;
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | fibril_mutex_unlock(&list->mutex);
|
|---|
| 169 | return NULL;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 | int
|
|---|
| 174 | register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
|
|---|
| 175 | irq_code_t *pseudocode)
|
|---|
| 176 | {
|
|---|
| 177 | interrupt_context_t *ctx = create_interrupt_context();
|
|---|
| 178 |
|
|---|
| 179 | ctx->dev = dev;
|
|---|
| 180 | ctx->irq = irq;
|
|---|
| 181 | ctx->handler = handler;
|
|---|
| 182 |
|
|---|
| 183 | add_interrupt_context(&interrupt_contexts, ctx);
|
|---|
| 184 |
|
|---|
| 185 | if (pseudocode == NULL)
|
|---|
| 186 | pseudocode = &default_pseudocode;
|
|---|
| 187 |
|
|---|
| 188 | int res = irq_register(irq, dev->handle, ctx->id, pseudocode);
|
|---|
| 189 | if (res != EOK) {
|
|---|
| 190 | remove_interrupt_context(&interrupt_contexts, ctx);
|
|---|
| 191 | delete_interrupt_context(ctx);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | return res;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
|
|---|
| 198 | {
|
|---|
| 199 | interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
|
|---|
| 200 | dev, irq);
|
|---|
| 201 | int res = irq_unregister(irq, dev->handle);
|
|---|
| 202 |
|
|---|
| 203 | if (ctx != NULL) {
|
|---|
| 204 | remove_interrupt_context(&interrupt_contexts, ctx);
|
|---|
| 205 | delete_interrupt_context(ctx);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | return res;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | * @}
|
|---|
| 213 | */
|
|---|