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