source: mainline/uspace/drv/block/pc-floppy/main.c@ 0dab4850

Last change on this file since 0dab4850 was 705b65ea, checked in by Jiri Svoboda <jiri@…>, 14 months ago

Do not automatically mount floppy. Reduce driver verbosity.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 * Copyright (c) 2024 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
46static errno_t pc_fdc_dev_add(ddf_dev_t *dev);
47static errno_t pc_fdc_dev_remove(ddf_dev_t *dev);
48static errno_t pc_fdc_dev_gone(ddf_dev_t *dev);
49static errno_t pc_fdc_fun_online(ddf_fun_t *fun);
50static errno_t pc_fdc_fun_offline(ddf_fun_t *fun);
51
52static 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
60static 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 */
71static 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 hw_res_flags_t flags;
76 errno_t rc;
77
78 parent_sess = ddf_dev_parent_sess_get(dev);
79 if (parent_sess == NULL)
80 return ENOMEM;
81
82 rc = hw_res_get_flags(parent_sess, &flags);
83 if (rc != EOK)
84 return rc;
85
86 hw_res_list_parsed_init(&hw_res);
87 rc = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
88 if (rc != EOK)
89 return rc;
90
91 if (hw_res.io_ranges.count != 1) {
92 rc = EINVAL;
93 goto error;
94 }
95
96 /* I/O range */
97
98 addr_range_t *regs_rng = &hw_res.io_ranges.ranges[0];
99 res->regs = RNGABS(*regs_rng);
100
101 if (RNGSZ(*regs_rng) < sizeof(pc_fdc_regs_t)) {
102 rc = EINVAL;
103 goto error;
104 }
105
106 /* IRQ */
107 if (hw_res.irqs.count > 0) {
108 res->irq = hw_res.irqs.irqs[0];
109 } else {
110 res->irq = -1;
111 }
112
113 /* DMA channel */
114 if (hw_res.dma_channels.count > 0) {
115 res->dma = hw_res.dma_channels.channels[0];
116 ddf_msg(LVL_NOTE, "DMA channel %u", res->dma);
117 } else {
118 res->dma = -1;
119 }
120
121 return EOK;
122error:
123 hw_res_list_parsed_clean(&hw_res);
124 return rc;
125}
126
127/** Add new FDC device
128 *
129 * @param dev New device
130 * @return EOK on success or an error code.
131 */
132static errno_t pc_fdc_dev_add(ddf_dev_t *dev)
133{
134 pc_fdc_t *fdc;
135 pc_fdc_hwres_t res;
136 errno_t rc;
137
138 rc = pc_fdc_get_res(dev, &res);
139 if (rc != EOK) {
140 ddf_msg(LVL_ERROR, "Invalid HW resource configuration.");
141 return EINVAL;
142 }
143
144 rc = pc_fdc_create(dev, &res, &fdc);
145 if (rc == ENOENT)
146 goto error;
147
148 if (rc != EOK) {
149 ddf_msg(LVL_ERROR, "Failed initializing floppy drive "
150 "controller.");
151 rc = EIO;
152 goto error;
153 }
154
155 return EOK;
156error:
157 return rc;
158}
159
160/** Remove FDC device.
161 *
162 * @param dev Device
163 * @return EOK on success or an error code
164 */
165static errno_t pc_fdc_dev_remove(ddf_dev_t *dev)
166{
167 pc_fdc_t *fdc = (pc_fdc_t *)ddf_dev_data_get(dev);
168 errno_t rc;
169
170 ddf_msg(LVL_DEBUG, "pc_fdc_dev_remove(%p)", dev);
171
172 rc = pc_fdc_destroy(fdc);
173 if (rc != EOK)
174 return rc;
175
176 return EOK;
177}
178
179/** Suprise removal of FDC device.
180 *
181 * @param dev Device
182 * @return EOK on success or an error code
183 */
184static errno_t pc_fdc_dev_gone(ddf_dev_t *dev)
185{
186 (void)dev;
187 return ENOTSUP;
188}
189
190/** Online FDC function.
191 *
192 * @param fun DDF function
193 * @return EOK on success or an error code
194 */
195static errno_t pc_fdc_fun_online(ddf_fun_t *fun)
196{
197 ddf_msg(LVL_DEBUG, "pc_fdc_fun_online()");
198 return ddf_fun_online(fun);
199}
200
201/** Offline FDC function.
202 *
203 * @param fun DDF function
204 * @return EOK on success or an error code
205 */
206static errno_t pc_fdc_fun_offline(ddf_fun_t *fun)
207{
208 ddf_msg(LVL_DEBUG, "pc_fdc_fun_offline()");
209 return ddf_fun_offline(fun);
210}
211
212int main(int argc, char *argv[])
213{
214 printf(NAME ": HelenOS PC floppy disk driver\n");
215 ddf_log_init(NAME);
216 return ddf_driver_main(&pc_fdc_driver);
217}
218
219/**
220 * @}
221 */
Note: See TracBrowser for help on using the repository browser.