| 1 | /*
|
|---|
| 2 | * Copyright (c) 2018 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 |
|
|---|
| 45 | static void pc_lpt_connection(ipc_call_t *, void *);
|
|---|
| 46 |
|
|---|
| 47 | static errno_t pc_lpt_read(chardev_srv_t *, void *, size_t, size_t *,
|
|---|
| 48 | chardev_flags_t);
|
|---|
| 49 | static errno_t pc_lpt_write(chardev_srv_t *, const void *, size_t, size_t *);
|
|---|
| 50 |
|
|---|
| 51 | static chardev_ops_t pc_lpt_chardev_ops = {
|
|---|
| 52 | .read = pc_lpt_read,
|
|---|
| 53 | .write = pc_lpt_write
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | static 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 | */
|
|---|
| 71 | static 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. */
|
|---|
| 79 | errno_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;
|
|---|
| 174 | error:
|
|---|
| 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 */
|
|---|
| 187 | errno_t pc_lpt_remove(pc_lpt_t *lpt)
|
|---|
| 188 | {
|
|---|
| 189 | return ENOTSUP;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /** Pc-lpt device gone */
|
|---|
| 193 | errno_t pc_lpt_gone(pc_lpt_t *lpt)
|
|---|
| 194 | {
|
|---|
| 195 | return ENOTSUP;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /** Write a single byte to the parallel port.
|
|---|
| 199 | *
|
|---|
| 200 | * @param lpt Parallel port (locked)
|
|---|
| 201 | * @param ch Byte
|
|---|
| 202 | */
|
|---|
| 203 | static void pc_lpt_putchar(pc_lpt_t *lpt, uint8_t ch)
|
|---|
| 204 | {
|
|---|
| 205 | uint8_t status;
|
|---|
| 206 | uint8_t control;
|
|---|
| 207 |
|
|---|
| 208 | assert(fibril_mutex_is_locked(&lpt->hw_lock));
|
|---|
| 209 |
|
|---|
| 210 | /* Write data */
|
|---|
| 211 | pio_write_8(&lpt->regs->data, ch);
|
|---|
| 212 |
|
|---|
| 213 | /* Wait for S7/nbusy to become 1 */
|
|---|
| 214 | do {
|
|---|
| 215 | status = pio_read_8(&lpt->regs->status);
|
|---|
| 216 | // FIXME Need to time out with an error after a while
|
|---|
| 217 | } while ((status & BIT_V(uint8_t, sts_nbusy)) == 0);
|
|---|
| 218 |
|
|---|
| 219 | control = pio_read_8(&lpt->regs->control);
|
|---|
| 220 | pio_write_8(&lpt->regs->control, control | BIT_V(uint8_t, ctl_strobe));
|
|---|
| 221 | fibril_usleep(5);
|
|---|
| 222 | pio_write_8(&lpt->regs->control, control & ~BIT_V(uint8_t, ctl_strobe));
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /** Read from pc-lpt device */
|
|---|
| 226 | static errno_t pc_lpt_read(chardev_srv_t *srv, void *buf, size_t size,
|
|---|
| 227 | size_t *nread, chardev_flags_t flags)
|
|---|
| 228 | {
|
|---|
| 229 | pc_lpt_t *lpt = (pc_lpt_t *) srv->srvs->sarg;
|
|---|
| 230 | (void) lpt;
|
|---|
| 231 | return ENOTSUP;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /** Write to pc-lpt device */
|
|---|
| 235 | static errno_t pc_lpt_write(chardev_srv_t *srv, const void *data, size_t size,
|
|---|
| 236 | size_t *nwr)
|
|---|
| 237 | {
|
|---|
| 238 | pc_lpt_t *lpt = (pc_lpt_t *) srv->srvs->sarg;
|
|---|
| 239 | size_t i;
|
|---|
| 240 | uint8_t *dp = (uint8_t *) data;
|
|---|
| 241 |
|
|---|
| 242 | fibril_mutex_lock(&lpt->hw_lock);
|
|---|
| 243 |
|
|---|
| 244 | for (i = 0; i < size; i++)
|
|---|
| 245 | pc_lpt_putchar(lpt, dp[i]);
|
|---|
| 246 |
|
|---|
| 247 | fibril_mutex_unlock(&lpt->hw_lock);
|
|---|
| 248 |
|
|---|
| 249 | *nwr = size;
|
|---|
| 250 | return EOK;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /** Character device connection handler. */
|
|---|
| 254 | static void pc_lpt_connection(ipc_call_t *icall, void *arg)
|
|---|
| 255 | {
|
|---|
| 256 | pc_lpt_t *lpt = (pc_lpt_t *) ddf_dev_data_get(
|
|---|
| 257 | ddf_fun_get_dev((ddf_fun_t *) arg));
|
|---|
| 258 |
|
|---|
| 259 | chardev_conn(icall, &lpt->cds);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /** @}
|
|---|
| 263 | */
|
|---|