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