Lines Matching refs:ps2dev
44 static int ps2_do_sendbyte(struct ps2dev *ps2dev, u8 byte, in ps2_do_sendbyte() argument
46 __releases(&ps2dev->serio->lock) __acquires(&ps2dev->serio->lock) in ps2_do_sendbyte()
51 lockdep_assert_held(&ps2dev->serio->lock); in ps2_do_sendbyte()
54 ps2dev->nak = 1; in ps2_do_sendbyte()
55 ps2dev->flags |= PS2_FLAG_ACK; in ps2_do_sendbyte()
57 serio_continue_rx(ps2dev->serio); in ps2_do_sendbyte()
59 error = serio_write(ps2dev->serio, byte); in ps2_do_sendbyte()
61 dev_dbg(&ps2dev->serio->dev, in ps2_do_sendbyte()
64 wait_event_timeout(ps2dev->wait, in ps2_do_sendbyte()
65 !(ps2dev->flags & PS2_FLAG_ACK), in ps2_do_sendbyte()
68 serio_pause_rx(ps2dev->serio); in ps2_do_sendbyte()
69 } while (ps2dev->nak == PS2_RET_NAK && ++attempt < max_attempts); in ps2_do_sendbyte()
71 ps2dev->flags &= ~PS2_FLAG_ACK; in ps2_do_sendbyte()
74 switch (ps2dev->nak) { in ps2_do_sendbyte()
90 dev_dbg(&ps2dev->serio->dev, in ps2_do_sendbyte()
92 byte, error, ps2dev->nak, attempt); in ps2_do_sendbyte()
108 int ps2_sendbyte(struct ps2dev *ps2dev, u8 byte, unsigned int timeout) in ps2_sendbyte() argument
112 guard(serio_pause_rx)(ps2dev->serio); in ps2_sendbyte()
114 retval = ps2_do_sendbyte(ps2dev, byte, timeout, 1); in ps2_sendbyte()
115 dev_dbg(&ps2dev->serio->dev, "%02x - %x\n", byte, ps2dev->nak); in ps2_sendbyte()
128 void ps2_begin_command(struct ps2dev *ps2dev) in ps2_begin_command() argument
130 struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex; in ps2_begin_command()
140 void ps2_end_command(struct ps2dev *ps2dev) in ps2_end_command() argument
142 struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex; in ps2_end_command()
155 void ps2_drain(struct ps2dev *ps2dev, size_t maxbytes, unsigned int timeout) in ps2_drain() argument
157 if (maxbytes > sizeof(ps2dev->cmdbuf)) { in ps2_drain()
159 maxbytes = sizeof(ps2dev->cmdbuf); in ps2_drain()
162 ps2_begin_command(ps2dev); in ps2_drain()
164 scoped_guard(serio_pause_rx, ps2dev->serio) { in ps2_drain()
165 ps2dev->flags = PS2_FLAG_CMD; in ps2_drain()
166 ps2dev->cmdcnt = maxbytes; in ps2_drain()
169 wait_event_timeout(ps2dev->wait, in ps2_drain()
170 !(ps2dev->flags & PS2_FLAG_CMD), in ps2_drain()
173 ps2_end_command(ps2dev); in ps2_drain()
202 static int ps2_adjust_timeout(struct ps2dev *ps2dev, in ps2_adjust_timeout() argument
225 if (ps2dev->cmdbuf[1] == 0xaa) { in ps2_adjust_timeout()
226 scoped_guard(serio_pause_rx, ps2dev->serio) in ps2_adjust_timeout()
227 ps2dev->flags = 0; in ps2_adjust_timeout()
236 if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) { in ps2_adjust_timeout()
237 scoped_guard(serio_pause_rx, ps2dev->serio) in ps2_adjust_timeout()
238 ps2dev->flags = ps2dev->cmdcnt = 0; in ps2_adjust_timeout()
264 int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command) in __ps2_command() argument
273 if (receive > sizeof(ps2dev->cmdbuf)) { in __ps2_command()
289 serio_pause_rx(ps2dev->serio); in __ps2_command()
291 ps2dev->cmdcnt = receive; in __ps2_command()
299 ps2dev->flags = PS2_FLAG_WAITID; in __ps2_command()
305 ps2dev->flags = PS2_FLAG_PASS_NOACK; in __ps2_command()
309 ps2dev->flags = 0; in __ps2_command()
315 ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1; in __ps2_command()
318 ps2dev->cmdbuf[(receive - 1) - i] = param[i]; in __ps2_command()
328 rc = ps2_do_sendbyte(ps2dev, command & 0xff, timeout, 2); in __ps2_command()
334 rc = ps2_do_sendbyte(ps2dev, param[i], 200, 2); in __ps2_command()
339 serio_continue_rx(ps2dev->serio); in __ps2_command()
346 timeout = wait_event_timeout(ps2dev->wait, in __ps2_command()
347 !(ps2dev->flags & PS2_FLAG_CMD1), timeout); in __ps2_command()
349 if (ps2dev->cmdcnt && !(ps2dev->flags & PS2_FLAG_CMD1)) { in __ps2_command()
351 timeout = ps2_adjust_timeout(ps2dev, command, timeout); in __ps2_command()
352 wait_event_timeout(ps2dev->wait, in __ps2_command()
353 !(ps2dev->flags & PS2_FLAG_CMD), timeout); in __ps2_command()
356 serio_pause_rx(ps2dev->serio); in __ps2_command()
360 param[i] = ps2dev->cmdbuf[(receive - 1) - i]; in __ps2_command()
364 if (ps2dev->cmdcnt && in __ps2_command()
365 (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1)) { in __ps2_command()
373 ps2dev->flags = 0; in __ps2_command()
374 serio_continue_rx(ps2dev->serio); in __ps2_command()
376 dev_dbg(&ps2dev->serio->dev, in __ps2_command()
379 ps2dev->nak, ps2dev->flags, in __ps2_command()
404 int ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command) in ps2_command() argument
408 ps2_begin_command(ps2dev); in ps2_command()
409 rc = __ps2_command(ps2dev, param, command); in ps2_command()
410 ps2_end_command(ps2dev); in ps2_command()
426 int ps2_sliced_command(struct ps2dev *ps2dev, u8 command) in ps2_sliced_command() argument
431 ps2_begin_command(ps2dev); in ps2_sliced_command()
433 retval = __ps2_command(ps2dev, NULL, PS2_CMD_SETSCALE11); in ps2_sliced_command()
439 retval = __ps2_command(ps2dev, &d, PS2_CMD_SETRES); in ps2_sliced_command()
445 dev_dbg(&ps2dev->serio->dev, "%02x - %d\n", command, retval); in ps2_sliced_command()
446 ps2_end_command(ps2dev); in ps2_sliced_command()
460 void ps2_init(struct ps2dev *ps2dev, struct serio *serio, in ps2_init() argument
464 ps2dev->pre_receive_handler = pre_receive_handler; in ps2_init()
465 ps2dev->receive_handler = receive_handler; in ps2_init()
467 mutex_init(&ps2dev->cmd_mutex); in ps2_init()
468 lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth); in ps2_init()
469 init_waitqueue_head(&ps2dev->wait); in ps2_init()
470 ps2dev->serio = serio; in ps2_init()
471 serio_set_drvdata(serio, ps2dev); in ps2_init()
482 static void ps2_handle_response(struct ps2dev *ps2dev, u8 data) in ps2_handle_response() argument
484 if (ps2dev->cmdcnt) in ps2_handle_response()
485 ps2dev->cmdbuf[--ps2dev->cmdcnt] = data; in ps2_handle_response()
487 if (ps2dev->flags & PS2_FLAG_CMD1) { in ps2_handle_response()
488 ps2dev->flags &= ~PS2_FLAG_CMD1; in ps2_handle_response()
489 if (ps2dev->cmdcnt) in ps2_handle_response()
490 wake_up(&ps2dev->wait); in ps2_handle_response()
493 if (!ps2dev->cmdcnt) { in ps2_handle_response()
494 ps2dev->flags &= ~PS2_FLAG_CMD; in ps2_handle_response()
495 wake_up(&ps2dev->wait); in ps2_handle_response()
504 static void ps2_handle_ack(struct ps2dev *ps2dev, u8 data) in ps2_handle_ack() argument
508 ps2dev->nak = 0; in ps2_handle_ack()
512 ps2dev->flags |= PS2_FLAG_NAK; in ps2_handle_ack()
513 ps2dev->nak = PS2_RET_NAK; in ps2_handle_ack()
517 if (ps2dev->flags & PS2_FLAG_NAK) { in ps2_handle_ack()
518 ps2dev->flags &= ~PS2_FLAG_NAK; in ps2_handle_ack()
519 ps2dev->nak = PS2_RET_ERR; in ps2_handle_ack()
531 if (ps2dev->flags & PS2_FLAG_WAITID) { in ps2_handle_ack()
532 ps2dev->nak = 0; in ps2_handle_ack()
550 dev_dbg(&ps2dev->serio->dev, "unexpected %#02x\n", data); in ps2_handle_ack()
551 if (ps2dev->flags & PS2_FLAG_PASS_NOACK) in ps2_handle_ack()
552 ps2dev->receive_handler(ps2dev, data); in ps2_handle_ack()
553 ps2dev->flags &= ~(PS2_FLAG_WAITID | PS2_FLAG_PASS_NOACK); in ps2_handle_ack()
557 if (!ps2dev->nak) in ps2_handle_ack()
558 ps2dev->flags &= ~PS2_FLAG_NAK; in ps2_handle_ack()
560 ps2dev->flags &= ~PS2_FLAG_ACK; in ps2_handle_ack()
562 if (!ps2dev->nak && data != PS2_RET_ACK) in ps2_handle_ack()
563 ps2_handle_response(ps2dev, data); in ps2_handle_ack()
565 wake_up(&ps2dev->wait); in ps2_handle_ack()
572 static void ps2_cleanup(struct ps2dev *ps2dev) in ps2_cleanup() argument
574 unsigned long old_flags = ps2dev->flags; in ps2_cleanup()
577 ps2dev->flags &= PS2_FLAG_NAK; in ps2_cleanup()
580 ps2dev->nak = 1; in ps2_cleanup()
583 wake_up(&ps2dev->wait); in ps2_cleanup()
598 struct ps2dev *ps2dev = serio_get_drvdata(serio); in ps2_interrupt() local
601 rc = ps2dev->pre_receive_handler(ps2dev, data, flags); in ps2_interrupt()
604 ps2_cleanup(ps2dev); in ps2_interrupt()
611 if (ps2dev->flags & PS2_FLAG_ACK) in ps2_interrupt()
612 ps2_handle_ack(ps2dev, data); in ps2_interrupt()
613 else if (ps2dev->flags & PS2_FLAG_CMD) in ps2_interrupt()
614 ps2_handle_response(ps2dev, data); in ps2_interrupt()
616 ps2dev->receive_handler(ps2dev, data); in ps2_interrupt()