Changes in uspace/drv/char/i8042/i8042.c [bd87ae0:876f6463] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/char/i8042/i8042.c
rbd87ae0 r876f6463 29 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 30 */ 31 31 32 /** @addtogroup kbd_port 32 33 * @ingroup kbd 33 34 * @{ 34 35 */ 36 35 37 /** @file 36 38 * @brief i8042 PS/2 port driver. … … 44 46 #include <str_error.h> 45 47 #include <inttypes.h> 46 47 48 #include <ddf/log.h> 48 49 #include <ddf/interrupt.h> 49 50 50 #include "i8042.h" 51 51 52 #define NAME "i8042" 52 /* Interesting bits for status register */ 53 #define i8042_OUTPUT_FULL 0x01 54 #define i8042_INPUT_FULL 0x02 55 #define i8042_AUX_DATA 0x20 56 57 /* Command constants */ 58 #define i8042_CMD_WRITE_CMDB 0x60 /**< Write command byte */ 59 #define i8042_CMD_WRITE_AUX 0xd4 /**< Write aux device */ 60 61 /* Command byte fields */ 62 #define i8042_KBD_IE 0x01 63 #define i8042_AUX_IE 0x02 64 #define i8042_KBD_DISABLE 0x10 65 #define i8042_AUX_DISABLE 0x20 66 #define i8042_KBD_TRANSLATE 0x40 /* Use this to switch to XT scancodes */ 67 68 #define CHECK_RET_DESTROY(ret, msg...) \ 69 do { \ 70 if (ret != EOK) { \ 71 ddf_msg(LVL_ERROR, msg); \ 72 if (dev->kbd_fun) { \ 73 dev->kbd_fun->driver_data = NULL; \ 74 ddf_fun_destroy(dev->kbd_fun); \ 75 } \ 76 if (dev->aux_fun) { \ 77 dev->aux_fun->driver_data = NULL; \ 78 ddf_fun_destroy(dev->aux_fun); \ 79 } \ 80 } \ 81 } while (0) 82 83 #define CHECK_RET_UNBIND_DESTROY(ret, msg...) \ 84 do { \ 85 if (ret != EOK) { \ 86 ddf_msg(LVL_ERROR, msg); \ 87 if (dev->kbd_fun) { \ 88 ddf_fun_unbind(dev->kbd_fun); \ 89 dev->kbd_fun->driver_data = NULL; \ 90 ddf_fun_destroy(dev->kbd_fun); \ 91 } \ 92 if (dev->aux_fun) { \ 93 ddf_fun_unbind(dev->aux_fun); \ 94 dev->aux_fun->driver_data = NULL; \ 95 ddf_fun_destroy(dev->aux_fun); \ 96 } \ 97 } \ 98 } while (0) 53 99 54 100 void default_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *); … … 59 105 }; 60 106 61 /* Interesting bits for status register */62 #define i8042_OUTPUT_FULL 0x0163 #define i8042_INPUT_FULL 0x0264 #define i8042_AUX_DATA 0x2065 66 /* Command constants */67 #define i8042_CMD_WRITE_CMDB 0x60 /**< write command byte */68 #define i8042_CMD_WRITE_AUX 0xd4 /**< write aux device */69 70 /* Command byte fields */71 #define i8042_KBD_IE 0x0172 #define i8042_AUX_IE 0x0273 #define i8042_KBD_DISABLE 0x1074 #define i8042_AUX_DISABLE 0x2075 #define i8042_KBD_TRANSLATE 0x40 /* Use this to switch to XT scancodes */76 77 107 /** i8042 Interrupt pseudo-code. */ 78 108 static const irq_cmd_t i8042_cmds[] = { 79 109 { 80 110 .cmd = CMD_PIO_READ_8, 81 .addr = NULL, 111 .addr = NULL, /* will be patched in run-time */ 82 112 .dstarg = 1 83 113 }, … … 95 125 { 96 126 .cmd = CMD_PIO_READ_8, 97 .addr = NULL, 127 .addr = NULL, /* will be patched in run-time */ 98 128 .dstarg = 2 99 129 }, … … 111 141 112 142 /** Interrupt handler routine. 113 * Writes new data to the corresponding buffer. 114 * @param dev Device that caued the interrupt. 115 * @param iid Call id. 143 * 144 * Write new data to the corresponding buffer. 145 * 146 * @param dev Device that caued the interrupt. 147 * @param iid Call id. 116 148 * @param call pointerr to call data. 117 */ 118 static void i8042_irq_handler( 119 ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call) 120 { 121 if (!dev || !dev->driver_data) 149 * 150 */ 151 static void i8042_irq_handler(ddf_dev_t *dev, ipc_callid_t iid, 152 ipc_call_t *call) 153 { 154 if ((!dev) || (!dev->driver_data)) 122 155 return; 156 123 157 i8042_t *controller = dev->driver_data; 124 158 125 159 const uint8_t status = IPC_GET_ARG1(*call); 126 160 const uint8_t data = IPC_GET_ARG2(*call); 161 127 162 buffer_t *buffer = (status & i8042_AUX_DATA) ? 128 163 &controller->aux_buffer : &controller->kbd_buffer; 164 129 165 buffer_write(buffer, data); 130 166 } 131 167 132 168 /** Initialize i8042 driver structure. 133 * @param dev Driver structure to initialize. 134 * @param regs I/O address of registers. 135 * @param reg_size size of the reserved I/O address space. 136 * @param irq_kbd IRQ for primary port. 169 * 170 * @param dev Driver structure to initialize. 171 * @param regs I/O address of registers. 172 * @param reg_size size of the reserved I/O address space. 173 * @param irq_kbd IRQ for primary port. 137 174 * @param irq_mouse IRQ for aux port. 138 * @param ddf_dev DDF device structure of the device. 175 * @param ddf_dev DDF device structure of the device. 176 * 139 177 * @return Error code. 178 * 140 179 */ 141 180 int i8042_init(i8042_t *dev, void *regs, size_t reg_size, int irq_kbd, … … 144 183 assert(ddf_dev); 145 184 assert(dev); 146 185 147 186 if (reg_size < sizeof(i8042_regs_t)) 148 187 return EINVAL; 149 150 if (pio_enable(regs, sizeof(i8042_regs_t), (void **)&dev->regs) != 0)188 189 if (pio_enable(regs, sizeof(i8042_regs_t), (void **) &dev->regs) != 0) 151 190 return -1; 152 191 153 192 dev->kbd_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2a"); 154 193 if (!dev->kbd_fun) 155 194 return ENOMEM; 195 156 196 int ret = ddf_fun_add_match_id(dev->kbd_fun, "char/xtkbd", 90); 157 197 if (ret != EOK) { … … 159 199 return ret; 160 200 } 161 201 162 202 dev->aux_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2b"); 163 203 if (!dev->aux_fun) { … … 165 205 return ENOMEM; 166 206 } 167 207 168 208 ret = ddf_fun_add_match_id(dev->aux_fun, "char/ps2mouse", 90); 169 209 if (ret != EOK) { … … 172 212 return ret; 173 213 } 174 214 175 215 dev->kbd_fun->ops = &ops; 176 216 dev->aux_fun->ops = &ops; 177 217 dev->kbd_fun->driver_data = dev; 178 218 dev->aux_fun->driver_data = dev; 179 219 180 220 buffer_init(&dev->kbd_buffer, dev->kbd_data, BUFFER_SIZE); 181 221 buffer_init(&dev->aux_buffer, dev->aux_data, BUFFER_SIZE); 182 222 fibril_mutex_initialize(&dev->write_guard); 183 184 #define CHECK_RET_DESTROY(ret, msg...) \ 185 if (ret != EOK) { \ 186 ddf_msg(LVL_ERROR, msg); \ 187 if (dev->kbd_fun) { \ 188 dev->kbd_fun->driver_data = NULL; \ 189 ddf_fun_destroy(dev->kbd_fun); \ 190 } \ 191 if (dev->aux_fun) { \ 192 dev->aux_fun->driver_data = NULL; \ 193 ddf_fun_destroy(dev->aux_fun); \ 194 } \ 195 } else (void)0 196 223 197 224 ret = ddf_fun_bind(dev->kbd_fun); 198 CHECK_RET_DESTROY(ret, 199 "Failed to bind keyboard function: %s.",str_error(ret));200 225 CHECK_RET_DESTROY(ret, "Failed to bind keyboard function: %s.", 226 str_error(ret)); 227 201 228 ret = ddf_fun_bind(dev->aux_fun); 202 CHECK_RET_DESTROY(ret, 203 "Failed to bind mouse function: %s.",str_error(ret));204 229 CHECK_RET_DESTROY(ret, "Failed to bind mouse function: %s.", 230 str_error(ret)); 231 205 232 /* Disable kbd and aux */ 206 233 wait_ready(dev); … … 208 235 wait_ready(dev); 209 236 pio_write_8(&dev->regs->data, i8042_KBD_DISABLE | i8042_AUX_DISABLE); 210 237 211 238 /* Flush all current IO */ 212 239 while (pio_read_8(&dev->regs->status) & i8042_OUTPUT_FULL) 213 240 (void) pio_read_8(&dev->regs->data); 214 215 #define CHECK_RET_UNBIND_DESTROY(ret, msg...) \ 216 if (ret != EOK) { \ 217 ddf_msg(LVL_ERROR, msg); \ 218 if (dev->kbd_fun) { \ 219 ddf_fun_unbind(dev->kbd_fun); \ 220 dev->kbd_fun->driver_data = NULL; \ 221 ddf_fun_destroy(dev->kbd_fun); \ 222 } \ 223 if (dev->aux_fun) { \ 224 ddf_fun_unbind(dev->aux_fun); \ 225 dev->aux_fun->driver_data = NULL; \ 226 ddf_fun_destroy(dev->aux_fun); \ 227 } \ 228 } else (void)0 229 241 230 242 const size_t cmd_count = sizeof(i8042_cmds) / sizeof(irq_cmd_t); 231 243 irq_cmd_t cmds[cmd_count]; … … 233 245 cmds[0].addr = (void *) &dev->regs->status; 234 246 cmds[3].addr = (void *) &dev->regs->data; 235 236 irq_code_t irq_code = { .cmdcount = cmd_count, .cmds = cmds }; 247 248 irq_code_t irq_code = { 249 .cmdcount = cmd_count, 250 .cmds = cmds 251 }; 252 237 253 ret = register_interrupt_handler(ddf_dev, irq_kbd, i8042_irq_handler, 238 254 &irq_code); 239 CHECK_RET_UNBIND_DESTROY(ret, 240 "Failed set handler for kbd: %s.",str_error(ret));241 255 CHECK_RET_UNBIND_DESTROY(ret, "Failed set handler for kbd: %s.", 256 str_error(ret)); 257 242 258 ret = register_interrupt_handler(ddf_dev, irq_mouse, i8042_irq_handler, 243 259 &irq_code); 244 CHECK_RET_UNBIND_DESTROY(ret, 245 "Failed set handler for mouse: %s.",str_error(ret));246 260 CHECK_RET_UNBIND_DESTROY(ret, "Failed set handler for mouse: %s.", 261 str_error(ret)); 262 247 263 /* Enable interrupts */ 248 264 async_sess_t *parent_sess = … … 251 267 ret = parent_sess ? EOK : ENOMEM; 252 268 CHECK_RET_UNBIND_DESTROY(ret, "Failed to create parent connection."); 253 269 254 270 const bool enabled = hw_res_enable_interrupt(parent_sess); 255 271 async_hangup(parent_sess); 256 272 ret = enabled ? EOK : EIO; 257 273 CHECK_RET_UNBIND_DESTROY(ret, "Failed to enable interrupts: %s."); 258 274 259 275 /* Enable port interrupts. */ 260 276 wait_ready(dev); … … 263 279 pio_write_8(&dev->regs->data, i8042_KBD_IE | i8042_KBD_TRANSLATE | 264 280 i8042_AUX_IE); 265 281 266 282 return EOK; 267 283 } 268 284 269 // TODO use shared instead this285 // FIXME TODO use shared instead this 270 286 enum { 271 287 IPC_CHAR_READ = DEV_FIRST_CUSTOM_METHOD, … … 274 290 275 291 /** Write data to i8042 port. 276 * @param fun DDF function. 292 * 293 * @param fun DDF function. 277 294 * @param buffer Data source. 278 * @param size Data size. 295 * @param size Data size. 296 * 279 297 * @return Bytes written. 298 * 280 299 */ 281 300 static int i8042_write(ddf_fun_t *fun, char *buffer, size_t size) … … 283 302 assert(fun); 284 303 assert(fun->driver_data); 304 285 305 i8042_t *controller = fun->driver_data; 286 306 fibril_mutex_lock(&controller->write_guard); 307 287 308 for (size_t i = 0; i < size; ++i) { 309 if (controller->aux_fun == fun) { 310 wait_ready(controller); 311 pio_write_8(&controller->regs->status, 312 i8042_CMD_WRITE_AUX); 313 } 314 288 315 wait_ready(controller); 289 if (controller->aux_fun == fun)290 pio_write_8(291 &controller->regs->status, i8042_CMD_WRITE_AUX);292 316 pio_write_8(&controller->regs->data, buffer[i]); 293 317 } 318 294 319 fibril_mutex_unlock(&controller->write_guard); 295 320 return size; … … 297 322 298 323 /** Read data from i8042 port. 299 * @param fun DDF function. 324 * 325 * @param fun DDF function. 300 326 * @param buffer Data place. 301 * @param size Data place size. 327 * @param size Data place size. 328 * 302 329 * @return Bytes read. 330 * 303 331 */ 304 332 static int i8042_read(ddf_fun_t *fun, char *data, size_t size) … … 306 334 assert(fun); 307 335 assert(fun->driver_data); 308 336 309 337 i8042_t *controller = fun->driver_data; 310 338 buffer_t *buffer = (fun == controller->aux_fun) ? 311 339 &controller->aux_buffer : &controller->kbd_buffer; 312 for (size_t i = 0; i < size; ++i) { 340 341 for (size_t i = 0; i < size; ++i) 313 342 *data++ = buffer_read(buffer); 314 }343 315 344 return size; 316 345 } 317 346 318 347 /** Handle data requests. 319 * @param fun ddf_fun_t function. 320 * @param id callid 348 * 349 * @param fun ddf_fun_t function. 350 * @param id callid 321 351 * @param call IPC request. 352 * 322 353 */ 323 354 void default_handler(ddf_fun_t *fun, ipc_callid_t id, ipc_call_t *call) … … 325 356 const sysarg_t method = IPC_GET_IMETHOD(*call); 326 357 const size_t size = IPC_GET_ARG1(*call); 358 327 359 switch (method) { 328 360 case IPC_CHAR_READ: 329 361 if (size <= 4 * sizeof(sysarg_t)) { 330 362 sysarg_t message[4] = {}; 331 i8042_read(fun, (char*)message, size); 363 364 i8042_read(fun, (char *) message, size); 332 365 async_answer_4(id, size, message[0], message[1], 333 366 message[2], message[3]); 334 } else {367 } else 335 368 async_answer_0(id, ELIMIT); 336 }337 369 break; 338 370 339 371 case IPC_CHAR_WRITE: 340 372 if (size <= 3 * sizeof(sysarg_t)) { 341 373 const sysarg_t message[3] = { 342 IPC_GET_ARG2(*call), IPC_GET_ARG3(*call), 343 IPC_GET_ARG4(*call) }; 344 i8042_write(fun, (char*)message, size); 374 IPC_GET_ARG2(*call), 375 IPC_GET_ARG3(*call), 376 IPC_GET_ARG4(*call) 377 }; 378 379 i8042_write(fun, (char *) message, size); 345 380 async_answer_0(id, size); 346 } else {381 } else 347 382 async_answer_0(id, ELIMIT); 348 } 349 383 350 384 default: 351 385 async_answer_0(id, EINVAL); 352 386 } 353 387 } 388 354 389 /** 355 390 * @}
Note:
See TracChangeset
for help on using the changeset viewer.