[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 |
|
---|
[04ba110] | 37 | #include <inttypes.h>
|
---|
[b4df8db] | 38 | #include <libarch/config.h>
|
---|
[ee163b3] | 39 | #include <ddf/driver.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 | /** Get address of I/O registers.
|
---|
| 48 | *
|
---|
[2df6f6fe] | 49 | * @param[in] dev Device asking for the addresses.
|
---|
[7de1988c] | 50 | * @param[out] p_io_reg Pointer to register range.
|
---|
[2df6f6fe] | 51 | * @param[out] kbd_irq Primary port IRQ.
|
---|
| 52 | * @param[out] mouse_irq Auxiliary port IRQ.
|
---|
| 53 | *
|
---|
[ee163b3] | 54 | * @return Error code.
|
---|
[2df6f6fe] | 55 | *
|
---|
[ee163b3] | 56 | */
|
---|
[7de1988c] | 57 | static int get_my_registers(ddf_dev_t *dev, addr_range_t *p_io_reg,
|
---|
| 58 | int *kbd_irq, int *mouse_irq)
|
---|
[ee163b3] | 59 | {
|
---|
| 60 | assert(dev);
|
---|
[2df6f6fe] | 61 |
|
---|
[2fd26bb] | 62 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(dev);
|
---|
[56fd7cf] | 63 | if (parent_sess == NULL)
|
---|
[ee163b3] | 64 | return ENOMEM;
|
---|
[2df6f6fe] | 65 |
|
---|
[ee163b3] | 66 | hw_res_list_parsed_t hw_resources;
|
---|
| 67 | hw_res_list_parsed_init(&hw_resources);
|
---|
| 68 | const int ret = hw_res_get_list_parsed(parent_sess, &hw_resources, 0);
|
---|
[2df6f6fe] | 69 | if (ret != EOK)
|
---|
[ee163b3] | 70 | return ret;
|
---|
[2df6f6fe] | 71 |
|
---|
| 72 | if ((hw_resources.irqs.count != 2) ||
|
---|
| 73 | (hw_resources.io_ranges.count != 1)) {
|
---|
[ee163b3] | 74 | hw_res_list_parsed_clean(&hw_resources);
|
---|
| 75 | return EINVAL;
|
---|
| 76 | }
|
---|
[2df6f6fe] | 77 |
|
---|
[7de1988c] | 78 | if (p_io_reg)
|
---|
| 79 | *p_io_reg = hw_resources.io_ranges.ranges[0];
|
---|
[2df6f6fe] | 80 |
|
---|
[ee163b3] | 81 | if (kbd_irq)
|
---|
| 82 | *kbd_irq = hw_resources.irqs.irqs[0];
|
---|
[2df6f6fe] | 83 |
|
---|
[ee163b3] | 84 | if (mouse_irq)
|
---|
| 85 | *mouse_irq = hw_resources.irqs.irqs[1];
|
---|
[2df6f6fe] | 86 |
|
---|
[ee163b3] | 87 | hw_res_list_parsed_clean(&hw_resources);
|
---|
| 88 | return EOK;
|
---|
| 89 | }
|
---|
[2df6f6fe] | 90 |
|
---|
| 91 | /** Initialize a new ddf driver instance of i8042 driver
|
---|
| 92 | *
|
---|
| 93 | * @param[in] device DDF instance of the device to initialize.
|
---|
| 94 | *
|
---|
| 95 | * @return Error code.
|
---|
| 96 | *
|
---|
| 97 | */
|
---|
| 98 | static int i8042_dev_add(ddf_dev_t *device)
|
---|
| 99 | {
|
---|
[7de1988c] | 100 | addr_range_t io_regs;
|
---|
[2df6f6fe] | 101 | int kbd = 0;
|
---|
| 102 | int mouse = 0;
|
---|
[ca4730a5] | 103 | int rc;
|
---|
| 104 |
|
---|
| 105 | if (!device)
|
---|
| 106 | return EINVAL;
|
---|
| 107 |
|
---|
[7de1988c] | 108 | rc = get_my_registers(device, &io_regs, &kbd, &mouse);
|
---|
[ca4730a5] | 109 | if (rc != EOK) {
|
---|
| 110 | ddf_msg(LVL_ERROR, "Failed to get registers: %s.",
|
---|
| 111 | str_error(rc));
|
---|
| 112 | return rc;
|
---|
| 113 | }
|
---|
[2df6f6fe] | 114 |
|
---|
[7de1988c] | 115 | ddf_msg(LVL_DEBUG,
|
---|
| 116 | "I/O regs at %p (size %zuB), IRQ kbd %d, IRQ mouse %d.",
|
---|
| 117 | RNGABSPTR(io_regs), RNGSZ(io_regs), kbd, mouse);
|
---|
[2df6f6fe] | 118 |
|
---|
| 119 | i8042_t *i8042 = ddf_dev_data_alloc(device, sizeof(i8042_t));
|
---|
[ca4730a5] | 120 | if (i8042 == NULL) {
|
---|
| 121 | ddf_msg(LVL_ERROR, "Out of memory.");
|
---|
| 122 | return ENOMEM;
|
---|
| 123 | }
|
---|
[2df6f6fe] | 124 |
|
---|
[7de1988c] | 125 | rc = i8042_init(i8042, &io_regs, kbd, mouse, device);
|
---|
[ca4730a5] | 126 | if (rc != EOK) {
|
---|
| 127 | ddf_msg(LVL_ERROR, "Failed to initialize i8042 driver: %s.",
|
---|
| 128 | str_error(rc));
|
---|
| 129 | return rc;
|
---|
| 130 | }
|
---|
[2df6f6fe] | 131 |
|
---|
| 132 | ddf_msg(LVL_NOTE, "Controlling '%s' (%" PRIun ").",
|
---|
[56fd7cf] | 133 | ddf_dev_get_name(device), ddf_dev_get_handle(device));
|
---|
[2df6f6fe] | 134 | return EOK;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** DDF driver operations. */
|
---|
| 138 | static driver_ops_t i8042_driver_ops = {
|
---|
| 139 | .dev_add = i8042_dev_add,
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | /** DDF driver. */
|
---|
| 143 | static driver_t i8042_driver = {
|
---|
| 144 | .name = NAME,
|
---|
| 145 | .driver_ops = &i8042_driver_ops
|
---|
| 146 | };
|
---|
| 147 |
|
---|
| 148 | int main(int argc, char *argv[])
|
---|
| 149 | {
|
---|
| 150 | printf("%s: HelenOS PS/2 driver.\n", NAME);
|
---|
[267f235] | 151 | ddf_log_init(NAME);
|
---|
[b4df8db] | 152 |
|
---|
[2df6f6fe] | 153 | return ddf_driver_main(&i8042_driver);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[ee163b3] | 156 | /**
|
---|
| 157 | * @}
|
---|
| 158 | */
|
---|