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