[ee163b3] | 1 | /*
|
---|
[797ab95] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[ee163b3] | 3 | * Copyright (c) 2011 Jan Vesely
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
[2df6f6fe] | 29 |
|
---|
[c8ea6eca] | 30 | /** @addtogroup i8042
|
---|
[ee163b3] | 31 | * @{
|
---|
| 32 | */
|
---|
[2df6f6fe] | 33 |
|
---|
[ee163b3] | 34 | /** @file
|
---|
| 35 | * @brief i8042 driver DDF bits.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[04ba110] | 38 | #include <inttypes.h>
|
---|
[b4df8db] | 39 | #include <libarch/config.h>
|
---|
[ee163b3] | 40 | #include <ddf/driver.h>
|
---|
| 41 | #include <device/hw_res_parsed.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <str_error.h>
|
---|
| 44 | #include <ddf/log.h>
|
---|
| 45 | #include <stdio.h>
|
---|
| 46 | #include "i8042.h"
|
---|
| 47 |
|
---|
| 48 | /** Get address of I/O registers.
|
---|
| 49 | *
|
---|
[2df6f6fe] | 50 | * @param[in] dev Device asking for the addresses.
|
---|
[7de1988c] | 51 | * @param[out] p_io_reg Pointer to register range.
|
---|
[2df6f6fe] | 52 | * @param[out] kbd_irq Primary port IRQ.
|
---|
| 53 | * @param[out] mouse_irq Auxiliary port IRQ.
|
---|
| 54 | *
|
---|
[ee163b3] | 55 | * @return Error code.
|
---|
[2df6f6fe] | 56 | *
|
---|
[ee163b3] | 57 | */
|
---|
[b7fd2a0] | 58 | static errno_t get_my_registers(ddf_dev_t *dev, addr_range_t *p_io_reg,
|
---|
[7de1988c] | 59 | int *kbd_irq, int *mouse_irq)
|
---|
[ee163b3] | 60 | {
|
---|
| 61 | assert(dev);
|
---|
[a35b458] | 62 |
|
---|
[2fd26bb] | 63 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(dev);
|
---|
[56fd7cf] | 64 | if (parent_sess == NULL)
|
---|
[ee163b3] | 65 | return ENOMEM;
|
---|
[a35b458] | 66 |
|
---|
[ee163b3] | 67 | hw_res_list_parsed_t hw_resources;
|
---|
| 68 | hw_res_list_parsed_init(&hw_resources);
|
---|
[b7fd2a0] | 69 | const errno_t ret = hw_res_get_list_parsed(parent_sess, &hw_resources, 0);
|
---|
[2df6f6fe] | 70 | if (ret != EOK)
|
---|
[ee163b3] | 71 | return ret;
|
---|
[a35b458] | 72 |
|
---|
[2df6f6fe] | 73 | if ((hw_resources.irqs.count != 2) ||
|
---|
| 74 | (hw_resources.io_ranges.count != 1)) {
|
---|
[ee163b3] | 75 | hw_res_list_parsed_clean(&hw_resources);
|
---|
| 76 | return EINVAL;
|
---|
| 77 | }
|
---|
[a35b458] | 78 |
|
---|
[7de1988c] | 79 | if (p_io_reg)
|
---|
| 80 | *p_io_reg = hw_resources.io_ranges.ranges[0];
|
---|
[a35b458] | 81 |
|
---|
[ee163b3] | 82 | if (kbd_irq)
|
---|
| 83 | *kbd_irq = hw_resources.irqs.irqs[0];
|
---|
[a35b458] | 84 |
|
---|
[ee163b3] | 85 | if (mouse_irq)
|
---|
| 86 | *mouse_irq = hw_resources.irqs.irqs[1];
|
---|
[a35b458] | 87 |
|
---|
[ee163b3] | 88 | hw_res_list_parsed_clean(&hw_resources);
|
---|
| 89 | return EOK;
|
---|
| 90 | }
|
---|
[2df6f6fe] | 91 |
|
---|
| 92 | /** Initialize a new ddf driver instance of i8042 driver
|
---|
| 93 | *
|
---|
| 94 | * @param[in] device DDF instance of the device to initialize.
|
---|
| 95 | *
|
---|
| 96 | * @return Error code.
|
---|
| 97 | *
|
---|
| 98 | */
|
---|
[b7fd2a0] | 99 | static errno_t i8042_dev_add(ddf_dev_t *device)
|
---|
[2df6f6fe] | 100 | {
|
---|
[7de1988c] | 101 | addr_range_t io_regs;
|
---|
[2df6f6fe] | 102 | int kbd = 0;
|
---|
| 103 | int mouse = 0;
|
---|
[b7fd2a0] | 104 | errno_t rc;
|
---|
[a35b458] | 105 |
|
---|
[ca4730a5] | 106 | if (!device)
|
---|
| 107 | return EINVAL;
|
---|
[a35b458] | 108 |
|
---|
[7de1988c] | 109 | rc = get_my_registers(device, &io_regs, &kbd, &mouse);
|
---|
[ca4730a5] | 110 | if (rc != EOK) {
|
---|
| 111 | ddf_msg(LVL_ERROR, "Failed to get registers: %s.",
|
---|
| 112 | str_error(rc));
|
---|
| 113 | return rc;
|
---|
| 114 | }
|
---|
[a35b458] | 115 |
|
---|
[7de1988c] | 116 | ddf_msg(LVL_DEBUG,
|
---|
| 117 | "I/O regs at %p (size %zuB), IRQ kbd %d, IRQ mouse %d.",
|
---|
| 118 | RNGABSPTR(io_regs), RNGSZ(io_regs), kbd, mouse);
|
---|
[a35b458] | 119 |
|
---|
[2df6f6fe] | 120 | i8042_t *i8042 = ddf_dev_data_alloc(device, sizeof(i8042_t));
|
---|
[ca4730a5] | 121 | if (i8042 == NULL) {
|
---|
| 122 | ddf_msg(LVL_ERROR, "Out of memory.");
|
---|
| 123 | return ENOMEM;
|
---|
| 124 | }
|
---|
[a35b458] | 125 |
|
---|
[7de1988c] | 126 | rc = i8042_init(i8042, &io_regs, kbd, mouse, device);
|
---|
[ca4730a5] | 127 | if (rc != EOK) {
|
---|
| 128 | ddf_msg(LVL_ERROR, "Failed to initialize i8042 driver: %s.",
|
---|
| 129 | str_error(rc));
|
---|
| 130 | return rc;
|
---|
| 131 | }
|
---|
[a35b458] | 132 |
|
---|
[2df6f6fe] | 133 | ddf_msg(LVL_NOTE, "Controlling '%s' (%" PRIun ").",
|
---|
[56fd7cf] | 134 | ddf_dev_get_name(device), ddf_dev_get_handle(device));
|
---|
[2df6f6fe] | 135 | return EOK;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[797ab95] | 138 | /** Initialize a new ddf driver instance of i8042 driver
|
---|
| 139 | *
|
---|
| 140 | * @param[in] device DDF instance of the device to initialize.
|
---|
| 141 | *
|
---|
| 142 | * @return Error code.
|
---|
| 143 | *
|
---|
| 144 | */
|
---|
| 145 | static errno_t i8042_dev_quiesce(ddf_dev_t *device)
|
---|
| 146 | {
|
---|
| 147 | i8042_t *i8042;
|
---|
| 148 |
|
---|
| 149 | ddf_msg(LVL_DEBUG, "i8042_dev_quiesce()");
|
---|
| 150 |
|
---|
| 151 | i8042 = (i8042_t *)ddf_dev_data_get(device);
|
---|
| 152 | i8042_quiesce(i8042);
|
---|
| 153 | return EOK;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[2df6f6fe] | 156 | /** DDF driver operations. */
|
---|
| 157 | static driver_ops_t i8042_driver_ops = {
|
---|
| 158 | .dev_add = i8042_dev_add,
|
---|
[797ab95] | 159 | .dev_quiesce = i8042_dev_quiesce
|
---|
[2df6f6fe] | 160 | };
|
---|
| 161 |
|
---|
| 162 | /** DDF driver. */
|
---|
| 163 | static driver_t i8042_driver = {
|
---|
| 164 | .name = NAME,
|
---|
| 165 | .driver_ops = &i8042_driver_ops
|
---|
| 166 | };
|
---|
| 167 |
|
---|
| 168 | int main(int argc, char *argv[])
|
---|
| 169 | {
|
---|
| 170 | printf("%s: HelenOS PS/2 driver.\n", NAME);
|
---|
[267f235] | 171 | ddf_log_init(NAME);
|
---|
[a35b458] | 172 |
|
---|
[2df6f6fe] | 173 | return ddf_driver_main(&i8042_driver);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[ee163b3] | 176 | /**
|
---|
| 177 | * @}
|
---|
| 178 | */
|
---|