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