[ee163b3] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
| 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 | */
|
---|
[2df6f6fe] | 28 |
|
---|
[ee163b3] | 29 | /** @addtogroup drvi8042
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
[2df6f6fe] | 32 |
|
---|
[ee163b3] | 33 | /** @file
|
---|
| 34 | * @brief i8042 driver DDF bits.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <libarch/inttypes.h>
|
---|
| 38 | #include <ddf/driver.h>
|
---|
| 39 | #include <devman.h>
|
---|
| 40 | #include <device/hw_res_parsed.h>
|
---|
| 41 | #include <errno.h>
|
---|
| 42 | #include <str_error.h>
|
---|
| 43 | #include <ddf/log.h>
|
---|
| 44 | #include <stdio.h>
|
---|
| 45 | #include "i8042.h"
|
---|
| 46 |
|
---|
| 47 | #define CHECK_RET_RETURN(ret, message...) \
|
---|
[2df6f6fe] | 48 | do { \
|
---|
| 49 | if (ret != EOK) { \
|
---|
| 50 | ddf_msg(LVL_ERROR, message); \
|
---|
| 51 | return ret; \
|
---|
| 52 | } \
|
---|
| 53 | } while (0)
|
---|
[8bb9540] | 54 |
|
---|
[ee163b3] | 55 | /** Get address of I/O registers.
|
---|
| 56 | *
|
---|
[2df6f6fe] | 57 | * @param[in] dev Device asking for the addresses.
|
---|
[ee163b3] | 58 | * @param[out] io_reg_address Base address of the memory range.
|
---|
[2df6f6fe] | 59 | * @param[out] io_reg_size Size of the memory range.
|
---|
| 60 | * @param[out] kbd_irq Primary port IRQ.
|
---|
| 61 | * @param[out] mouse_irq Auxiliary port IRQ.
|
---|
| 62 | *
|
---|
[ee163b3] | 63 | * @return Error code.
|
---|
[2df6f6fe] | 64 | *
|
---|
[ee163b3] | 65 | */
|
---|
[2df6f6fe] | 66 | static int get_my_registers(const ddf_dev_t *dev, uintptr_t *io_reg_address,
|
---|
[ee163b3] | 67 | size_t *io_reg_size, int *kbd_irq, int *mouse_irq)
|
---|
| 68 | {
|
---|
| 69 | assert(dev);
|
---|
[2df6f6fe] | 70 |
|
---|
[ee163b3] | 71 | async_sess_t *parent_sess =
|
---|
| 72 | devman_parent_device_connect(EXCHANGE_SERIALIZE, dev->handle,
|
---|
| 73 | IPC_FLAG_BLOCKING);
|
---|
| 74 | if (!parent_sess)
|
---|
| 75 | return ENOMEM;
|
---|
[2df6f6fe] | 76 |
|
---|
[ee163b3] | 77 | hw_res_list_parsed_t hw_resources;
|
---|
| 78 | hw_res_list_parsed_init(&hw_resources);
|
---|
| 79 | const int ret = hw_res_get_list_parsed(parent_sess, &hw_resources, 0);
|
---|
| 80 | async_hangup(parent_sess);
|
---|
[2df6f6fe] | 81 | if (ret != EOK)
|
---|
[ee163b3] | 82 | return ret;
|
---|
[2df6f6fe] | 83 |
|
---|
| 84 | if ((hw_resources.irqs.count != 2) ||
|
---|
| 85 | (hw_resources.io_ranges.count != 1)) {
|
---|
[ee163b3] | 86 | hw_res_list_parsed_clean(&hw_resources);
|
---|
| 87 | return EINVAL;
|
---|
| 88 | }
|
---|
[2df6f6fe] | 89 |
|
---|
[ee163b3] | 90 | if (io_reg_address)
|
---|
| 91 | *io_reg_address = hw_resources.io_ranges.ranges[0].address;
|
---|
[2df6f6fe] | 92 |
|
---|
[ee163b3] | 93 | if (io_reg_size)
|
---|
| 94 | *io_reg_size = hw_resources.io_ranges.ranges[0].size;
|
---|
[2df6f6fe] | 95 |
|
---|
[ee163b3] | 96 | if (kbd_irq)
|
---|
| 97 | *kbd_irq = hw_resources.irqs.irqs[0];
|
---|
[2df6f6fe] | 98 |
|
---|
[ee163b3] | 99 | if (mouse_irq)
|
---|
| 100 | *mouse_irq = hw_resources.irqs.irqs[1];
|
---|
[2df6f6fe] | 101 |
|
---|
[ee163b3] | 102 | hw_res_list_parsed_clean(&hw_resources);
|
---|
| 103 | return EOK;
|
---|
| 104 | }
|
---|
[2df6f6fe] | 105 |
|
---|
| 106 | /** Initialize a new ddf driver instance of i8042 driver
|
---|
| 107 | *
|
---|
| 108 | * @param[in] device DDF instance of the device to initialize.
|
---|
| 109 | *
|
---|
| 110 | * @return Error code.
|
---|
| 111 | *
|
---|
| 112 | */
|
---|
| 113 | static int i8042_dev_add(ddf_dev_t *device)
|
---|
| 114 | {
|
---|
| 115 | if (!device)
|
---|
| 116 | return EINVAL;
|
---|
| 117 |
|
---|
| 118 | uintptr_t io_regs = 0;
|
---|
| 119 | size_t io_size = 0;
|
---|
| 120 | int kbd = 0;
|
---|
| 121 | int mouse = 0;
|
---|
| 122 |
|
---|
| 123 | int ret = get_my_registers(device, &io_regs, &io_size, &kbd, &mouse);
|
---|
| 124 | CHECK_RET_RETURN(ret, "Failed to get registers: %s.",
|
---|
| 125 | str_error(ret));
|
---|
| 126 | ddf_msg(LVL_DEBUG, "I/O regs at %p (size %zuB), IRQ kbd %d, IRQ mouse %d.",
|
---|
| 127 | (void *) io_regs, io_size, kbd, mouse);
|
---|
| 128 |
|
---|
| 129 | i8042_t *i8042 = ddf_dev_data_alloc(device, sizeof(i8042_t));
|
---|
| 130 | ret = (i8042 == NULL) ? ENOMEM : EOK;
|
---|
| 131 | CHECK_RET_RETURN(ret, "Failed to allocate i8042 driver instance.");
|
---|
| 132 |
|
---|
| 133 | ret = i8042_init(i8042, (void *) io_regs, io_size, kbd, mouse, device);
|
---|
| 134 | CHECK_RET_RETURN(ret, "Failed to initialize i8042 driver: %s.",
|
---|
| 135 | str_error(ret));
|
---|
| 136 |
|
---|
| 137 | ddf_msg(LVL_NOTE, "Controlling '%s' (%" PRIun ").",
|
---|
| 138 | device->name, device->handle);
|
---|
| 139 | return EOK;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /** DDF driver operations. */
|
---|
| 143 | static driver_ops_t i8042_driver_ops = {
|
---|
| 144 | .dev_add = i8042_dev_add,
|
---|
| 145 | };
|
---|
| 146 |
|
---|
| 147 | /** DDF driver. */
|
---|
| 148 | static driver_t i8042_driver = {
|
---|
| 149 | .name = NAME,
|
---|
| 150 | .driver_ops = &i8042_driver_ops
|
---|
| 151 | };
|
---|
| 152 |
|
---|
| 153 | int main(int argc, char *argv[])
|
---|
| 154 | {
|
---|
| 155 | printf("%s: HelenOS PS/2 driver.\n", NAME);
|
---|
| 156 | ddf_log_init(NAME, LVL_NOTE);
|
---|
| 157 | return ddf_driver_main(&i8042_driver);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[ee163b3] | 160 | /**
|
---|
| 161 | * @}
|
---|
| 162 | */
|
---|