| 1 | /*
|
|---|
| 2 | * Copyright (c) 2025 Jiri Svoboda
|
|---|
| 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 pc-floppy
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file PC floppy disk driver main
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <str_error.h>
|
|---|
| 40 | #include <ddf/driver.h>
|
|---|
| 41 | #include <ddf/log.h>
|
|---|
| 42 | #include <device/hw_res_parsed.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "pc-floppy.h"
|
|---|
| 45 |
|
|---|
| 46 | static errno_t pc_fdc_dev_add(ddf_dev_t *dev);
|
|---|
| 47 | static errno_t pc_fdc_dev_remove(ddf_dev_t *dev);
|
|---|
| 48 | static errno_t pc_fdc_dev_gone(ddf_dev_t *dev);
|
|---|
| 49 | static errno_t pc_fdc_dev_quiesce(ddf_dev_t *dev);
|
|---|
| 50 | static errno_t pc_fdc_fun_online(ddf_fun_t *fun);
|
|---|
| 51 | static errno_t pc_fdc_fun_offline(ddf_fun_t *fun);
|
|---|
| 52 |
|
|---|
| 53 | static driver_ops_t driver_ops = {
|
|---|
| 54 | .dev_add = pc_fdc_dev_add,
|
|---|
| 55 | .dev_remove = pc_fdc_dev_remove,
|
|---|
| 56 | .dev_gone = pc_fdc_dev_gone,
|
|---|
| 57 | .dev_quiesce = pc_fdc_dev_quiesce,
|
|---|
| 58 | .fun_online = pc_fdc_fun_online,
|
|---|
| 59 | .fun_offline = pc_fdc_fun_offline
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | static driver_t pc_fdc_driver = {
|
|---|
| 63 | .name = NAME,
|
|---|
| 64 | .driver_ops = &driver_ops
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | /** Parse FDC hardware resources.
|
|---|
| 68 | *
|
|---|
| 69 | * @param dev DDF device (from which we get resources)
|
|---|
| 70 | * @param res Place to store FDC hardware resources
|
|---|
| 71 | * @return EOK on success or an error code
|
|---|
| 72 | */
|
|---|
| 73 | static errno_t pc_fdc_get_res(ddf_dev_t *dev, pc_fdc_hwres_t *res)
|
|---|
| 74 | {
|
|---|
| 75 | async_sess_t *parent_sess;
|
|---|
| 76 | hw_res_list_parsed_t hw_res;
|
|---|
| 77 | errno_t rc;
|
|---|
| 78 |
|
|---|
| 79 | parent_sess = ddf_dev_parent_sess_get(dev);
|
|---|
| 80 | if (parent_sess == NULL)
|
|---|
| 81 | return ENOMEM;
|
|---|
| 82 |
|
|---|
| 83 | hw_res_list_parsed_init(&hw_res);
|
|---|
| 84 | rc = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
|
|---|
| 85 | if (rc != EOK)
|
|---|
| 86 | return rc;
|
|---|
| 87 |
|
|---|
| 88 | if (hw_res.io_ranges.count != 1) {
|
|---|
| 89 | rc = EINVAL;
|
|---|
| 90 | goto error;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /* I/O range */
|
|---|
| 94 |
|
|---|
| 95 | addr_range_t *regs_rng = &hw_res.io_ranges.ranges[0];
|
|---|
| 96 | res->regs = RNGABS(*regs_rng);
|
|---|
| 97 |
|
|---|
| 98 | if (RNGSZ(*regs_rng) < sizeof(pc_fdc_regs_t)) {
|
|---|
| 99 | rc = EINVAL;
|
|---|
| 100 | goto error;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /* IRQ */
|
|---|
| 104 | if (hw_res.irqs.count > 0) {
|
|---|
| 105 | res->irq = hw_res.irqs.irqs[0];
|
|---|
| 106 | } else {
|
|---|
| 107 | res->irq = -1;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /* DMA channel */
|
|---|
| 111 | if (hw_res.dma_channels.count > 0) {
|
|---|
| 112 | res->dma = hw_res.dma_channels.channels[0];
|
|---|
| 113 | ddf_msg(LVL_NOTE, "DMA channel %u", res->dma);
|
|---|
| 114 | } else {
|
|---|
| 115 | res->dma = -1;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | return EOK;
|
|---|
| 119 | error:
|
|---|
| 120 | hw_res_list_parsed_clean(&hw_res);
|
|---|
| 121 | return rc;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /** Add new FDC device
|
|---|
| 125 | *
|
|---|
| 126 | * @param dev New device
|
|---|
| 127 | * @return EOK on success or an error code.
|
|---|
| 128 | */
|
|---|
| 129 | static errno_t pc_fdc_dev_add(ddf_dev_t *dev)
|
|---|
| 130 | {
|
|---|
| 131 | pc_fdc_t *fdc;
|
|---|
| 132 | pc_fdc_hwres_t res;
|
|---|
| 133 | errno_t rc;
|
|---|
| 134 |
|
|---|
| 135 | rc = pc_fdc_get_res(dev, &res);
|
|---|
| 136 | if (rc != EOK) {
|
|---|
| 137 | ddf_msg(LVL_ERROR, "Invalid HW resource configuration.");
|
|---|
| 138 | return EINVAL;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | rc = pc_fdc_create(dev, &res, &fdc);
|
|---|
| 142 | if (rc == ENOENT)
|
|---|
| 143 | goto error;
|
|---|
| 144 |
|
|---|
| 145 | if (rc != EOK) {
|
|---|
| 146 | ddf_msg(LVL_ERROR, "Failed initializing floppy drive "
|
|---|
| 147 | "controller.");
|
|---|
| 148 | rc = EIO;
|
|---|
| 149 | goto error;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | return EOK;
|
|---|
| 153 | error:
|
|---|
| 154 | return rc;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /** Remove FDC device.
|
|---|
| 158 | *
|
|---|
| 159 | * @param dev Device
|
|---|
| 160 | * @return EOK on success or an error code
|
|---|
| 161 | */
|
|---|
| 162 | static errno_t pc_fdc_dev_remove(ddf_dev_t *dev)
|
|---|
| 163 | {
|
|---|
| 164 | pc_fdc_t *fdc = (pc_fdc_t *)ddf_dev_data_get(dev);
|
|---|
| 165 | errno_t rc;
|
|---|
| 166 |
|
|---|
| 167 | ddf_msg(LVL_DEBUG, "pc_fdc_dev_remove(%p)", dev);
|
|---|
| 168 |
|
|---|
| 169 | rc = pc_fdc_destroy(fdc);
|
|---|
| 170 | if (rc != EOK)
|
|---|
| 171 | return rc;
|
|---|
| 172 |
|
|---|
| 173 | return EOK;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /** Suprise removal of FDC device.
|
|---|
| 177 | *
|
|---|
| 178 | * @param dev Device
|
|---|
| 179 | * @return EOK on success or an error code
|
|---|
| 180 | */
|
|---|
| 181 | static errno_t pc_fdc_dev_gone(ddf_dev_t *dev)
|
|---|
| 182 | {
|
|---|
| 183 | (void)dev;
|
|---|
| 184 | return ENOTSUP;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /** Quiesce FDC device.
|
|---|
| 188 | *
|
|---|
| 189 | * @param dev Device
|
|---|
| 190 | * @return EOK on success or an error code
|
|---|
| 191 | */
|
|---|
| 192 | static errno_t pc_fdc_dev_quiesce(ddf_dev_t *dev)
|
|---|
| 193 | {
|
|---|
| 194 | pc_fdc_t *fdc = (pc_fdc_t *)ddf_dev_data_get(dev);
|
|---|
| 195 | pc_fdc_quiesce(fdc);
|
|---|
| 196 | return EOK;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /** Online FDC function.
|
|---|
| 200 | *
|
|---|
| 201 | * @param fun DDF function
|
|---|
| 202 | * @return EOK on success or an error code
|
|---|
| 203 | */
|
|---|
| 204 | static errno_t pc_fdc_fun_online(ddf_fun_t *fun)
|
|---|
| 205 | {
|
|---|
| 206 | ddf_msg(LVL_DEBUG, "pc_fdc_fun_online()");
|
|---|
| 207 | return ddf_fun_online(fun);
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /** Offline FDC function.
|
|---|
| 211 | *
|
|---|
| 212 | * @param fun DDF function
|
|---|
| 213 | * @return EOK on success or an error code
|
|---|
| 214 | */
|
|---|
| 215 | static errno_t pc_fdc_fun_offline(ddf_fun_t *fun)
|
|---|
| 216 | {
|
|---|
| 217 | ddf_msg(LVL_DEBUG, "pc_fdc_fun_offline()");
|
|---|
| 218 | return ddf_fun_offline(fun);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | int main(int argc, char *argv[])
|
|---|
| 222 | {
|
|---|
| 223 | printf(NAME ": HelenOS PC floppy disk driver\n");
|
|---|
| 224 | ddf_log_init(NAME);
|
|---|
| 225 | return ddf_driver_main(&pc_fdc_driver);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /**
|
|---|
| 229 | * @}
|
|---|
| 230 | */
|
|---|