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