Lines Matching refs:ps2dev

43 static int ps2_do_sendbyte(struct ps2dev *ps2dev, u8 byte,  in ps2_do_sendbyte()  argument
45 __releases(&ps2dev->serio->lock) __acquires(&ps2dev->serio->lock) in ps2_do_sendbyte()
50 lockdep_assert_held(&ps2dev->serio->lock); in ps2_do_sendbyte()
53 ps2dev->nak = 1; in ps2_do_sendbyte()
54 ps2dev->flags |= PS2_FLAG_ACK; in ps2_do_sendbyte()
56 serio_continue_rx(ps2dev->serio); in ps2_do_sendbyte()
58 error = serio_write(ps2dev->serio, byte); in ps2_do_sendbyte()
60 dev_dbg(&ps2dev->serio->dev, in ps2_do_sendbyte()
63 wait_event_timeout(ps2dev->wait, in ps2_do_sendbyte()
64 !(ps2dev->flags & PS2_FLAG_ACK), in ps2_do_sendbyte()
67 serio_pause_rx(ps2dev->serio); in ps2_do_sendbyte()
68 } while (ps2dev->nak == PS2_RET_NAK && ++attempt < max_attempts); in ps2_do_sendbyte()
70 ps2dev->flags &= ~PS2_FLAG_ACK; in ps2_do_sendbyte()
73 switch (ps2dev->nak) { in ps2_do_sendbyte()
89 dev_dbg(&ps2dev->serio->dev, in ps2_do_sendbyte()
91 byte, error, ps2dev->nak, attempt); in ps2_do_sendbyte()
107 int ps2_sendbyte(struct ps2dev *ps2dev, u8 byte, unsigned int timeout) in ps2_sendbyte() argument
111 guard(serio_pause_rx)(ps2dev->serio); in ps2_sendbyte()
113 retval = ps2_do_sendbyte(ps2dev, byte, timeout, 1); in ps2_sendbyte()
114 dev_dbg(&ps2dev->serio->dev, "%02x - %x\n", byte, ps2dev->nak); in ps2_sendbyte()
127 void ps2_begin_command(struct ps2dev *ps2dev) in ps2_begin_command() argument
129 struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex; in ps2_begin_command()
139 void ps2_end_command(struct ps2dev *ps2dev) in ps2_end_command() argument
141 struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex; in ps2_end_command()
154 void ps2_drain(struct ps2dev *ps2dev, size_t maxbytes, unsigned int timeout) in ps2_drain() argument
156 if (maxbytes > sizeof(ps2dev->cmdbuf)) { in ps2_drain()
158 maxbytes = sizeof(ps2dev->cmdbuf); in ps2_drain()
161 ps2_begin_command(ps2dev); in ps2_drain()
163 scoped_guard(serio_pause_rx, ps2dev->serio) { in ps2_drain()
164 ps2dev->flags = PS2_FLAG_CMD; in ps2_drain()
165 ps2dev->cmdcnt = maxbytes; in ps2_drain()
168 wait_event_timeout(ps2dev->wait, in ps2_drain()
169 !(ps2dev->flags & PS2_FLAG_CMD), in ps2_drain()
172 ps2_end_command(ps2dev); in ps2_drain()
201 static int ps2_adjust_timeout(struct ps2dev *ps2dev, in ps2_adjust_timeout() argument
224 if (ps2dev->cmdbuf[1] == 0xaa) { in ps2_adjust_timeout()
225 scoped_guard(serio_pause_rx, ps2dev->serio) in ps2_adjust_timeout()
226 ps2dev->flags = 0; in ps2_adjust_timeout()
235 if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) { in ps2_adjust_timeout()
236 scoped_guard(serio_pause_rx, ps2dev->serio) in ps2_adjust_timeout()
237 ps2dev->flags = ps2dev->cmdcnt = 0; in ps2_adjust_timeout()
263 int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command) in __ps2_command() argument
272 if (receive > sizeof(ps2dev->cmdbuf)) { in __ps2_command()
288 serio_pause_rx(ps2dev->serio); in __ps2_command()
290 ps2dev->cmdcnt = receive; in __ps2_command()
298 ps2dev->flags = PS2_FLAG_WAITID; in __ps2_command()
304 ps2dev->flags = PS2_FLAG_PASS_NOACK; in __ps2_command()
308 ps2dev->flags = 0; in __ps2_command()
314 ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1; in __ps2_command()
317 ps2dev->cmdbuf[(receive - 1) - i] = param[i]; in __ps2_command()
327 rc = ps2_do_sendbyte(ps2dev, command & 0xff, timeout, 2); in __ps2_command()
333 rc = ps2_do_sendbyte(ps2dev, param[i], 200, 2); in __ps2_command()
338 serio_continue_rx(ps2dev->serio); in __ps2_command()
345 timeout = wait_event_timeout(ps2dev->wait, in __ps2_command()
346 !(ps2dev->flags & PS2_FLAG_CMD1), timeout); in __ps2_command()
348 if (ps2dev->cmdcnt && !(ps2dev->flags & PS2_FLAG_CMD1)) { in __ps2_command()
350 timeout = ps2_adjust_timeout(ps2dev, command, timeout); in __ps2_command()
351 wait_event_timeout(ps2dev->wait, in __ps2_command()
352 !(ps2dev->flags & PS2_FLAG_CMD), timeout); in __ps2_command()
355 serio_pause_rx(ps2dev->serio); in __ps2_command()
359 param[i] = ps2dev->cmdbuf[(receive - 1) - i]; in __ps2_command()
363 if (ps2dev->cmdcnt && in __ps2_command()
364 (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1)) { in __ps2_command()
372 ps2dev->flags = 0; in __ps2_command()
373 serio_continue_rx(ps2dev->serio); in __ps2_command()
375 dev_dbg(&ps2dev->serio->dev, in __ps2_command()
378 ps2dev->nak, ps2dev->flags, in __ps2_command()
403 int ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command) in ps2_command() argument
407 ps2_begin_command(ps2dev); in ps2_command()
408 rc = __ps2_command(ps2dev, param, command); in ps2_command()
409 ps2_end_command(ps2dev); in ps2_command()
425 int ps2_sliced_command(struct ps2dev *ps2dev, u8 command) in ps2_sliced_command() argument
430 ps2_begin_command(ps2dev); in ps2_sliced_command()
432 retval = __ps2_command(ps2dev, NULL, PS2_CMD_SETSCALE11); in ps2_sliced_command()
438 retval = __ps2_command(ps2dev, &d, PS2_CMD_SETRES); in ps2_sliced_command()
444 dev_dbg(&ps2dev->serio->dev, "%02x - %d\n", command, retval); in ps2_sliced_command()
445 ps2_end_command(ps2dev); in ps2_sliced_command()
459 void ps2_init(struct ps2dev *ps2dev, struct serio *serio, in ps2_init() argument
463 ps2dev->pre_receive_handler = pre_receive_handler; in ps2_init()
464 ps2dev->receive_handler = receive_handler; in ps2_init()
466 mutex_init(&ps2dev->cmd_mutex); in ps2_init()
467 lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth); in ps2_init()
468 init_waitqueue_head(&ps2dev->wait); in ps2_init()
469 ps2dev->serio = serio; in ps2_init()
470 serio_set_drvdata(serio, ps2dev); in ps2_init()
481 static void ps2_handle_response(struct ps2dev *ps2dev, u8 data) in ps2_handle_response() argument
483 if (ps2dev->cmdcnt) in ps2_handle_response()
484 ps2dev->cmdbuf[--ps2dev->cmdcnt] = data; in ps2_handle_response()
486 if (ps2dev->flags & PS2_FLAG_CMD1) { in ps2_handle_response()
487 ps2dev->flags &= ~PS2_FLAG_CMD1; in ps2_handle_response()
488 if (ps2dev->cmdcnt) in ps2_handle_response()
489 wake_up(&ps2dev->wait); in ps2_handle_response()
492 if (!ps2dev->cmdcnt) { in ps2_handle_response()
493 ps2dev->flags &= ~PS2_FLAG_CMD; in ps2_handle_response()
494 wake_up(&ps2dev->wait); in ps2_handle_response()
503 static void ps2_handle_ack(struct ps2dev *ps2dev, u8 data) in ps2_handle_ack() argument
507 ps2dev->nak = 0; in ps2_handle_ack()
511 ps2dev->flags |= PS2_FLAG_NAK; in ps2_handle_ack()
512 ps2dev->nak = PS2_RET_NAK; in ps2_handle_ack()
516 if (ps2dev->flags & PS2_FLAG_NAK) { in ps2_handle_ack()
517 ps2dev->flags &= ~PS2_FLAG_NAK; in ps2_handle_ack()
518 ps2dev->nak = PS2_RET_ERR; in ps2_handle_ack()
530 if (ps2dev->flags & PS2_FLAG_WAITID) { in ps2_handle_ack()
531 ps2dev->nak = 0; in ps2_handle_ack()
549 dev_dbg(&ps2dev->serio->dev, "unexpected %#02x\n", data); in ps2_handle_ack()
550 if (ps2dev->flags & PS2_FLAG_PASS_NOACK) in ps2_handle_ack()
551 ps2dev->receive_handler(ps2dev, data); in ps2_handle_ack()
552 ps2dev->flags &= ~(PS2_FLAG_WAITID | PS2_FLAG_PASS_NOACK); in ps2_handle_ack()
556 if (!ps2dev->nak) in ps2_handle_ack()
557 ps2dev->flags &= ~PS2_FLAG_NAK; in ps2_handle_ack()
559 ps2dev->flags &= ~PS2_FLAG_ACK; in ps2_handle_ack()
561 if (!ps2dev->nak && data != PS2_RET_ACK) in ps2_handle_ack()
562 ps2_handle_response(ps2dev, data); in ps2_handle_ack()
564 wake_up(&ps2dev->wait); in ps2_handle_ack()
571 static void ps2_cleanup(struct ps2dev *ps2dev) in ps2_cleanup() argument
573 unsigned long old_flags = ps2dev->flags; in ps2_cleanup()
576 ps2dev->flags &= PS2_FLAG_NAK; in ps2_cleanup()
579 ps2dev->nak = 1; in ps2_cleanup()
582 wake_up(&ps2dev->wait); in ps2_cleanup()
597 struct ps2dev *ps2dev = serio_get_drvdata(serio); in ps2_interrupt() local
600 rc = ps2dev->pre_receive_handler(ps2dev, data, flags); in ps2_interrupt()
603 ps2_cleanup(ps2dev); in ps2_interrupt()
610 if (ps2dev->flags & PS2_FLAG_ACK) in ps2_interrupt()
611 ps2_handle_ack(ps2dev, data); in ps2_interrupt()
612 else if (ps2dev->flags & PS2_FLAG_CMD) in ps2_interrupt()
613 ps2_handle_response(ps2dev, data); in ps2_interrupt()
615 ps2dev->receive_handler(ps2dev, data); in ps2_interrupt()