Changes in uspace/drv/char/i8042/i8042.c [cccdb8b7:876f6463] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/char/i8042/i8042.c
rcccdb8b7 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 static const irq_pio_range_t i8042_ranges[] = {78 {79 .base = 0,80 .size = sizeof(i8042_regs_t)81 }82 };83 84 107 /** i8042 Interrupt pseudo-code. */ 85 108 static const irq_cmd_t i8042_cmds[] = { 86 109 { 87 110 .cmd = CMD_PIO_READ_8, 88 .addr = NULL, 111 .addr = NULL, /* will be patched in run-time */ 89 112 .dstarg = 1 90 113 }, … … 102 125 { 103 126 .cmd = CMD_PIO_READ_8, 104 .addr = NULL, 127 .addr = NULL, /* will be patched in run-time */ 105 128 .dstarg = 2 106 129 }, … … 118 141 119 142 /** Interrupt handler routine. 120 * Writes new data to the corresponding buffer. 121 * @param dev Device that caued the interrupt. 122 * @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. 123 148 * @param call pointerr to call data. 124 */ 125 static void i8042_irq_handler( 126 ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call) 127 { 128 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)) 129 155 return; 156 130 157 i8042_t *controller = dev->driver_data; 131 158 132 159 const uint8_t status = IPC_GET_ARG1(*call); 133 160 const uint8_t data = IPC_GET_ARG2(*call); 161 134 162 buffer_t *buffer = (status & i8042_AUX_DATA) ? 135 163 &controller->aux_buffer : &controller->kbd_buffer; 164 136 165 buffer_write(buffer, data); 137 166 } 138 167 139 168 /** Initialize i8042 driver structure. 140 * @param dev Driver structure to initialize. 141 * @param regs I/O address of registers. 142 * @param reg_size size of the reserved I/O address space. 143 * @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. 144 174 * @param irq_mouse IRQ for aux port. 145 * @param ddf_dev DDF device structure of the device. 175 * @param ddf_dev DDF device structure of the device. 176 * 146 177 * @return Error code. 178 * 147 179 */ 148 180 int i8042_init(i8042_t *dev, void *regs, size_t reg_size, int irq_kbd, … … 151 183 assert(ddf_dev); 152 184 assert(dev); 153 185 154 186 if (reg_size < sizeof(i8042_regs_t)) 155 187 return EINVAL; 156 157 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) 158 190 return -1; 159 191 160 192 dev->kbd_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2a"); 161 193 if (!dev->kbd_fun) 162 194 return ENOMEM; 195 163 196 int ret = ddf_fun_add_match_id(dev->kbd_fun, "char/xtkbd", 90); 164 197 if (ret != EOK) { … … 166 199 return ret; 167 200 } 168 201 169 202 dev->aux_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2b"); 170 203 if (!dev->aux_fun) { … … 172 205 return ENOMEM; 173 206 } 174 207 175 208 ret = ddf_fun_add_match_id(dev->aux_fun, "char/ps2mouse", 90); 176 209 if (ret != EOK) { … … 179 212 return ret; 180 213 } 181 214 182 215 dev->kbd_fun->ops = &ops; 183 216 dev->aux_fun->ops = &ops; 184 217 dev->kbd_fun->driver_data = dev; 185 218 dev->aux_fun->driver_data = dev; 186 219 187 220 buffer_init(&dev->kbd_buffer, dev->kbd_data, BUFFER_SIZE); 188 221 buffer_init(&dev->aux_buffer, dev->aux_data, BUFFER_SIZE); 189 222 fibril_mutex_initialize(&dev->write_guard); 190 191 #define CHECK_RET_DESTROY(ret, msg...) \ 192 if (ret != EOK) { \ 193 ddf_msg(LVL_ERROR, msg); \ 194 if (dev->kbd_fun) { \ 195 dev->kbd_fun->driver_data = NULL; \ 196 ddf_fun_destroy(dev->kbd_fun); \ 197 } \ 198 if (dev->aux_fun) { \ 199 dev->aux_fun->driver_data = NULL; \ 200 ddf_fun_destroy(dev->aux_fun); \ 201 } \ 202 } else (void)0 203 223 204 224 ret = ddf_fun_bind(dev->kbd_fun); 205 CHECK_RET_DESTROY(ret, 206 "Failed to bind keyboard function: %s.",str_error(ret));207 225 CHECK_RET_DESTROY(ret, "Failed to bind keyboard function: %s.", 226 str_error(ret)); 227 208 228 ret = ddf_fun_bind(dev->aux_fun); 209 CHECK_RET_DESTROY(ret, 210 "Failed to bind mouse function: %s.",str_error(ret));211 229 CHECK_RET_DESTROY(ret, "Failed to bind mouse function: %s.", 230 str_error(ret)); 231 212 232 /* Disable kbd and aux */ 213 233 wait_ready(dev); … … 215 235 wait_ready(dev); 216 236 pio_write_8(&dev->regs->data, i8042_KBD_DISABLE | i8042_AUX_DISABLE); 217 237 218 238 /* Flush all current IO */ 219 239 while (pio_read_8(&dev->regs->status) & i8042_OUTPUT_FULL) 220 240 (void) pio_read_8(&dev->regs->data); 221 222 #define CHECK_RET_UNBIND_DESTROY(ret, msg...) \ 223 if (ret != EOK) { \ 224 ddf_msg(LVL_ERROR, msg); \ 225 if (dev->kbd_fun) { \ 226 ddf_fun_unbind(dev->kbd_fun); \ 227 dev->kbd_fun->driver_data = NULL; \ 228 ddf_fun_destroy(dev->kbd_fun); \ 229 } \ 230 if (dev->aux_fun) { \ 231 ddf_fun_unbind(dev->aux_fun); \ 232 dev->aux_fun->driver_data = NULL; \ 233 ddf_fun_destroy(dev->aux_fun); \ 234 } \ 235 } else (void)0 236 237 const size_t range_count = sizeof(i8042_ranges) / 238 sizeof(irq_pio_range_t); 239 irq_pio_range_t ranges[range_count]; 240 memcpy(ranges, i8042_ranges, sizeof(i8042_ranges)); 241 ranges[0].base = (uintptr_t) regs; 242 241 243 242 const size_t cmd_count = sizeof(i8042_cmds) / sizeof(irq_cmd_t); 244 243 irq_cmd_t cmds[cmd_count]; 245 244 memcpy(cmds, i8042_cmds, sizeof(i8042_cmds)); 246 cmds[0].addr = (void *) & (((i8042_regs_t *) regs)->status);247 cmds[3].addr = (void *) & (((i8042_regs_t *) regs)->data);248 245 cmds[0].addr = (void *) &dev->regs->status; 246 cmds[3].addr = (void *) &dev->regs->data; 247 249 248 irq_code_t irq_code = { 250 .rangecount = range_count,251 .ranges = ranges,252 249 .cmdcount = cmd_count, 253 250 .cmds = cmds 254 251 }; 252 255 253 ret = register_interrupt_handler(ddf_dev, irq_kbd, i8042_irq_handler, 256 254 &irq_code); 257 CHECK_RET_UNBIND_DESTROY(ret, 258 "Failed set handler for kbd: %s.",str_error(ret));259 255 CHECK_RET_UNBIND_DESTROY(ret, "Failed set handler for kbd: %s.", 256 str_error(ret)); 257 260 258 ret = register_interrupt_handler(ddf_dev, irq_mouse, i8042_irq_handler, 261 259 &irq_code); 262 CHECK_RET_UNBIND_DESTROY(ret, 263 "Failed set handler for mouse: %s.",str_error(ret));264 260 CHECK_RET_UNBIND_DESTROY(ret, "Failed set handler for mouse: %s.", 261 str_error(ret)); 262 265 263 /* Enable interrupts */ 266 264 async_sess_t *parent_sess = … … 269 267 ret = parent_sess ? EOK : ENOMEM; 270 268 CHECK_RET_UNBIND_DESTROY(ret, "Failed to create parent connection."); 271 269 272 270 const bool enabled = hw_res_enable_interrupt(parent_sess); 273 271 async_hangup(parent_sess); 274 272 ret = enabled ? EOK : EIO; 275 273 CHECK_RET_UNBIND_DESTROY(ret, "Failed to enable interrupts: %s."); 276 274 277 275 /* Enable port interrupts. */ 278 276 wait_ready(dev); … … 281 279 pio_write_8(&dev->regs->data, i8042_KBD_IE | i8042_KBD_TRANSLATE | 282 280 i8042_AUX_IE); 283 281 284 282 return EOK; 285 283 } 286 284 287 // TODO use shared instead this285 // FIXME TODO use shared instead this 288 286 enum { 289 287 IPC_CHAR_READ = DEV_FIRST_CUSTOM_METHOD, … … 292 290 293 291 /** Write data to i8042 port. 294 * @param fun DDF function. 292 * 293 * @param fun DDF function. 295 294 * @param buffer Data source. 296 * @param size Data size. 295 * @param size Data size. 296 * 297 297 * @return Bytes written. 298 * 298 299 */ 299 300 static int i8042_write(ddf_fun_t *fun, char *buffer, size_t size) … … 301 302 assert(fun); 302 303 assert(fun->driver_data); 304 303 305 i8042_t *controller = fun->driver_data; 304 306 fibril_mutex_lock(&controller->write_guard); 307 305 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 306 315 wait_ready(controller); 307 if (controller->aux_fun == fun)308 pio_write_8(309 &controller->regs->status, i8042_CMD_WRITE_AUX);310 316 pio_write_8(&controller->regs->data, buffer[i]); 311 317 } 318 312 319 fibril_mutex_unlock(&controller->write_guard); 313 320 return size; … … 315 322 316 323 /** Read data from i8042 port. 317 * @param fun DDF function. 324 * 325 * @param fun DDF function. 318 326 * @param buffer Data place. 319 * @param size Data place size. 327 * @param size Data place size. 328 * 320 329 * @return Bytes read. 330 * 321 331 */ 322 332 static int i8042_read(ddf_fun_t *fun, char *data, size_t size) … … 324 334 assert(fun); 325 335 assert(fun->driver_data); 326 336 327 337 i8042_t *controller = fun->driver_data; 328 338 buffer_t *buffer = (fun == controller->aux_fun) ? 329 339 &controller->aux_buffer : &controller->kbd_buffer; 330 for (size_t i = 0; i < size; ++i) { 340 341 for (size_t i = 0; i < size; ++i) 331 342 *data++ = buffer_read(buffer); 332 }343 333 344 return size; 334 345 } 335 346 336 347 /** Handle data requests. 337 * @param fun ddf_fun_t function. 338 * @param id callid 348 * 349 * @param fun ddf_fun_t function. 350 * @param id callid 339 351 * @param call IPC request. 352 * 340 353 */ 341 354 void default_handler(ddf_fun_t *fun, ipc_callid_t id, ipc_call_t *call) … … 343 356 const sysarg_t method = IPC_GET_IMETHOD(*call); 344 357 const size_t size = IPC_GET_ARG1(*call); 358 345 359 switch (method) { 346 360 case IPC_CHAR_READ: 347 361 if (size <= 4 * sizeof(sysarg_t)) { 348 362 sysarg_t message[4] = {}; 349 i8042_read(fun, (char*)message, size); 363 364 i8042_read(fun, (char *) message, size); 350 365 async_answer_4(id, size, message[0], message[1], 351 366 message[2], message[3]); 352 } else {367 } else 353 368 async_answer_0(id, ELIMIT); 354 }355 369 break; 356 370 357 371 case IPC_CHAR_WRITE: 358 372 if (size <= 3 * sizeof(sysarg_t)) { 359 373 const sysarg_t message[3] = { 360 IPC_GET_ARG2(*call), IPC_GET_ARG3(*call), 361 IPC_GET_ARG4(*call) }; 362 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); 363 380 async_answer_0(id, size); 364 } else {381 } else 365 382 async_answer_0(id, ELIMIT); 366 } 367 383 368 384 default: 369 385 async_answer_0(id, EINVAL); 370 386 } 371 387 } 388 372 389 /** 373 390 * @}
Note:
See TracChangeset
for help on using the changeset viewer.