| 1 | /*
|
|---|
| 2 | * Copyright (c) 2014 Jiri Svoboda
|
|---|
| 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 libc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <assert.h>
|
|---|
| 36 | #include <async.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <ipc/irc.h>
|
|---|
| 39 | #include <ipc/services.h>
|
|---|
| 40 | #include <irc.h>
|
|---|
| 41 | #include <loc.h>
|
|---|
| 42 | #include <stdlib.h>
|
|---|
| 43 | #include <sysinfo.h>
|
|---|
| 44 |
|
|---|
| 45 | static async_sess_t *irc_sess;
|
|---|
| 46 |
|
|---|
| 47 | /** Connect to IRC service.
|
|---|
| 48 | *
|
|---|
| 49 | * @return EOK on success, EIO on failure
|
|---|
| 50 | */
|
|---|
| 51 | static int irc_init(void)
|
|---|
| 52 | {
|
|---|
| 53 | category_id_t irc_cat;
|
|---|
| 54 | service_id_t *svcs;
|
|---|
| 55 | size_t count;
|
|---|
| 56 | int rc;
|
|---|
| 57 |
|
|---|
| 58 | assert(irc_sess == NULL);
|
|---|
| 59 | rc = loc_category_get_id("irc", &irc_cat, IPC_FLAG_BLOCKING);
|
|---|
| 60 | if (rc != EOK)
|
|---|
| 61 | return EIO;
|
|---|
| 62 |
|
|---|
| 63 | while (true) {
|
|---|
| 64 | rc = loc_category_get_svcs(irc_cat, &svcs, &count);
|
|---|
| 65 | if (rc != EOK)
|
|---|
| 66 | return EIO;
|
|---|
| 67 |
|
|---|
| 68 | if (count > 0)
|
|---|
| 69 | break;
|
|---|
| 70 |
|
|---|
| 71 | free(svcs);
|
|---|
| 72 |
|
|---|
| 73 | // XXX This is just a temporary hack
|
|---|
| 74 | async_usleep(500 * 1000);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | irc_sess = loc_service_connect(svcs[0], INTERFACE_IRC,
|
|---|
| 78 | IPC_FLAG_BLOCKING);
|
|---|
| 79 | free(svcs);
|
|---|
| 80 |
|
|---|
| 81 | if (irc_sess == NULL)
|
|---|
| 82 | return EIO;
|
|---|
| 83 |
|
|---|
| 84 | return EOK;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /** Enable interrupt.
|
|---|
| 88 | *
|
|---|
| 89 | * Allow interrupt delivery.
|
|---|
| 90 | *
|
|---|
| 91 | * @param irq IRQ number
|
|---|
| 92 | */
|
|---|
| 93 | int irc_enable_interrupt(int irq)
|
|---|
| 94 | {
|
|---|
| 95 | int rc;
|
|---|
| 96 |
|
|---|
| 97 | if (irc_sess == NULL) {
|
|---|
| 98 | rc = irc_init();
|
|---|
| 99 | if (rc != EOK)
|
|---|
| 100 | return rc;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | async_exch_t *exch = async_exchange_begin(irc_sess);
|
|---|
| 104 | rc = async_req_1_0(exch, IRC_ENABLE_INTERRUPT, irq);
|
|---|
| 105 | async_exchange_end(exch);
|
|---|
| 106 |
|
|---|
| 107 | return rc;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /** Disable interrupt.
|
|---|
| 111 | *
|
|---|
| 112 | * Disallow interrupt delivery.
|
|---|
| 113 | *
|
|---|
| 114 | * @param irq IRQ number
|
|---|
| 115 | */
|
|---|
| 116 | int irc_disable_interrupt(int irq)
|
|---|
| 117 | {
|
|---|
| 118 | int rc;
|
|---|
| 119 |
|
|---|
| 120 | if (irc_sess == NULL) {
|
|---|
| 121 | rc = irc_init();
|
|---|
| 122 | if (rc != EOK)
|
|---|
| 123 | return rc;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | async_exch_t *exch = async_exchange_begin(irc_sess);
|
|---|
| 127 | rc = async_req_1_0(exch, IRC_DISABLE_INTERRUPT, irq);
|
|---|
| 128 | async_exchange_end(exch);
|
|---|
| 129 |
|
|---|
| 130 | return rc;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /** Clear interrupt.
|
|---|
| 134 | *
|
|---|
| 135 | * Clear/acknowledge interrupt in interrupt controller so that
|
|---|
| 136 | * another interrupt can be delivered.
|
|---|
| 137 | *
|
|---|
| 138 | * @param irq IRQ number
|
|---|
| 139 | */
|
|---|
| 140 | int irc_clear_interrupt(int irq)
|
|---|
| 141 | {
|
|---|
| 142 | int rc;
|
|---|
| 143 |
|
|---|
| 144 | if (irc_sess == NULL) {
|
|---|
| 145 | rc = irc_init();
|
|---|
| 146 | if (rc != EOK)
|
|---|
| 147 | return rc;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | async_exch_t *exch = async_exchange_begin(irc_sess);
|
|---|
| 151 | rc = async_req_1_0(exch, IRC_CLEAR_INTERRUPT, irq);
|
|---|
| 152 | async_exchange_end(exch);
|
|---|
| 153 |
|
|---|
| 154 | return rc;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /** @}
|
|---|
| 158 | */
|
|---|