source: mainline/uspace/drv/char/pc-lpt/pc-lpt.c@ ca22536

Last change on this file since ca22536 was b9cc81c6, checked in by Jiri Svoboda <jiri@…>, 5 months ago

Implement quiesce in NS8250 and PC-LPT.

  • Property mode set to 100644
File size: 6.8 KB
Line 
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/** @file
30 * @brief PC parallel port driver.
31 */
32
33#include <async.h>
34#include <bitops.h>
35#include <ddf/driver.h>
36#include <ddf/log.h>
37#include <ddi.h>
38#include <errno.h>
39#include <fibril_synch.h>
40#include <io/chardev_srv.h>
41
42#include "pc-lpt.h"
43#include "pc-lpt_hw.h"
44
45static void pc_lpt_connection(ipc_call_t *, void *);
46
47static errno_t pc_lpt_read(chardev_srv_t *, void *, size_t, size_t *,
48 chardev_flags_t);
49static errno_t pc_lpt_write(chardev_srv_t *, const void *, size_t, size_t *);
50
51static chardev_ops_t pc_lpt_chardev_ops = {
52 .read = pc_lpt_read,
53 .write = pc_lpt_write
54};
55
56static irq_cmd_t pc_lpt_cmds_proto[] = {
57 {
58 .cmd = CMD_DECLINE
59 }
60};
61
62/** PC LPT IRQ handler.
63 *
64 * Note that while the standard PC parallel port supports IRQ, it seems
65 * drivers tend to avoid using them (for a reason?) These IRQs tend
66 * to be used by other HW as well (Sound Blaster) so caution is in order.
67 * Also not sure, if/how the IRQ needs to be cleared.
68 *
69 * Currently we don't enable IRQ and don't handle it in any way.
70 */
71static void pc_lpt_irq_handler(ipc_call_t *call, void *arg)
72{
73 pc_lpt_t *lpt = (pc_lpt_t *) arg;
74
75 (void) lpt;
76}
77
78/** Add pc-lpt device. */
79errno_t pc_lpt_add(pc_lpt_t *lpt, pc_lpt_res_t *res)
80{
81 ddf_fun_t *fun = NULL;
82 bool bound = false;
83 irq_cmd_t *pc_lpt_cmds = NULL;
84 uint8_t control;
85 uint8_t r;
86 errno_t rc;
87
88 lpt->irq_handle = CAP_NIL;
89 fibril_mutex_initialize(&lpt->hw_lock);
90
91 pc_lpt_cmds = malloc(sizeof(pc_lpt_cmds_proto));
92 if (pc_lpt_cmds == NULL) {
93 rc = ENOMEM;
94 goto error;
95 }
96
97 lpt->res = *res;
98
99 fun = ddf_fun_create(lpt->dev, fun_exposed, "a");
100 if (fun == NULL) {
101 ddf_msg(LVL_ERROR, "Error creating function 'a'.");
102 rc = ENOMEM;
103 goto error;
104 }
105
106 rc = pio_enable((void *)res->base, sizeof(pc_lpt_regs_t),
107 (void **) &lpt->regs);
108 if (rc != EOK) {
109 ddf_msg(LVL_ERROR, "Error enabling I/O");
110 goto error;
111 }
112
113 ddf_fun_set_conn_handler(fun, pc_lpt_connection);
114
115 lpt->irq_range[0].base = res->base;
116 lpt->irq_range[0].size = 1;
117
118 memcpy(pc_lpt_cmds, pc_lpt_cmds_proto, sizeof(pc_lpt_cmds_proto));
119 pc_lpt_cmds[0].addr = (void *) res->base;
120
121 lpt->irq_code.rangecount = 1;
122 lpt->irq_code.ranges = lpt->irq_range;
123 lpt->irq_code.cmdcount = sizeof(pc_lpt_cmds_proto) / sizeof(irq_cmd_t);
124 lpt->irq_code.cmds = pc_lpt_cmds;
125
126 rc = async_irq_subscribe(res->irq, pc_lpt_irq_handler, lpt,
127 &lpt->irq_code, &lpt->irq_handle);
128 if (rc != EOK) {
129 ddf_msg(LVL_ERROR, "Error registering IRQ code.");
130 goto error;
131 }
132
133 control = BIT_V(uint8_t, ctl_select) | BIT_V(uint8_t, ctl_ninit);
134 pio_write_8(&lpt->regs->control, control);
135 r = pio_read_8(&lpt->regs->control);
136 if ((r & 0xf) != control) {
137 /* Device not present */
138 rc = EIO;
139 goto error;
140 }
141
142 control |= BIT_V(uint8_t, ctl_autofd);
143 pio_write_8(&lpt->regs->control, control);
144 r = pio_read_8(&lpt->regs->control);
145 if ((r & 0xf) != control) {
146 /* Device not present */
147 rc = EIO;
148 goto error;
149 }
150
151 control &= ~BIT_V(uint8_t, ctl_autofd);
152 pio_write_8(&lpt->regs->control, control);
153
154 chardev_srvs_init(&lpt->cds);
155 lpt->cds.ops = &pc_lpt_chardev_ops;
156 lpt->cds.sarg = lpt;
157
158 rc = ddf_fun_bind(fun);
159 if (rc != EOK) {
160 ddf_msg(LVL_ERROR, "Error binding function 'a'.");
161 goto error;
162 }
163
164 bound = true;
165
166 rc = ddf_fun_add_to_category(fun, "printer-port");
167 if (rc != EOK) {
168 ddf_msg(LVL_ERROR, "Error adding function 'a' to category "
169 "'printer-port'.");
170 goto error;
171 }
172
173 return EOK;
174error:
175 if (cap_handle_valid(lpt->irq_handle))
176 async_irq_unsubscribe(lpt->irq_handle);
177 if (bound)
178 ddf_fun_unbind(fun);
179 if (fun != NULL)
180 ddf_fun_destroy(fun);
181 free(pc_lpt_cmds);
182
183 return rc;
184}
185
186/** Remove pc-lpt device */
187errno_t pc_lpt_remove(pc_lpt_t *lpt)
188{
189 return ENOTSUP;
190}
191
192/** Pc-lpt device gone */
193errno_t pc_lpt_gone(pc_lpt_t *lpt)
194{
195 return ENOTSUP;
196}
197
198/** Quiesce pc-lpt device */
199void pc_lpt_quiesce(pc_lpt_t *lpt)
200{
201 uint8_t control;
202
203 control = 0; /* nINIT=0, IRQ_ENABLE=0 */
204 pio_write_8(&lpt->regs->control, control);
205}
206
207/** Write a single byte to the parallel port.
208 *
209 * @param lpt Parallel port (locked)
210 * @param ch Byte
211 */
212static void pc_lpt_putchar(pc_lpt_t *lpt, uint8_t ch)
213{
214 uint8_t status;
215 uint8_t control;
216
217 assert(fibril_mutex_is_locked(&lpt->hw_lock));
218
219 /* Write data */
220 pio_write_8(&lpt->regs->data, ch);
221
222 /* Wait for S7/nbusy to become 1 */
223 do {
224 status = pio_read_8(&lpt->regs->status);
225 // FIXME Need to time out with an error after a while
226 } while ((status & BIT_V(uint8_t, sts_nbusy)) == 0);
227
228 control = pio_read_8(&lpt->regs->control);
229 pio_write_8(&lpt->regs->control, control | BIT_V(uint8_t, ctl_strobe));
230 fibril_usleep(5);
231 pio_write_8(&lpt->regs->control, control & ~BIT_V(uint8_t, ctl_strobe));
232}
233
234/** Read from pc-lpt device */
235static errno_t pc_lpt_read(chardev_srv_t *srv, void *buf, size_t size,
236 size_t *nread, chardev_flags_t flags)
237{
238 pc_lpt_t *lpt = (pc_lpt_t *) srv->srvs->sarg;
239 (void) lpt;
240 return ENOTSUP;
241}
242
243/** Write to pc-lpt device */
244static errno_t pc_lpt_write(chardev_srv_t *srv, const void *data, size_t size,
245 size_t *nwr)
246{
247 pc_lpt_t *lpt = (pc_lpt_t *) srv->srvs->sarg;
248 size_t i;
249 uint8_t *dp = (uint8_t *) data;
250
251 fibril_mutex_lock(&lpt->hw_lock);
252
253 for (i = 0; i < size; i++)
254 pc_lpt_putchar(lpt, dp[i]);
255
256 fibril_mutex_unlock(&lpt->hw_lock);
257
258 *nwr = size;
259 return EOK;
260}
261
262/** Character device connection handler. */
263static void pc_lpt_connection(ipc_call_t *icall, void *arg)
264{
265 pc_lpt_t *lpt = (pc_lpt_t *) ddf_dev_data_get(
266 ddf_fun_get_dev((ddf_fun_t *) arg));
267
268 chardev_conn(icall, &lpt->cds);
269}
270
271/** @}
272 */
Note: See TracBrowser for help on using the repository browser.