^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * HID driver for the apple ir device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Original driver written by James McKenzie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Ported to recent 2.6 kernel versions by Greg Kroah-Hartman <gregkh@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Updated to support newer remotes by Bastien Nocera <hadess@hadess.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Ported to HID subsystem by Benjamin Tissoires <benjamin.tissoires@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (C) 2006 James McKenzie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (C) 2008 Greg Kroah-Hartman <greg@kroah.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Copyright (C) 2008 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright (C) 2010, 2012 Bastien Nocera <hadess@hadess.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Copyright (C) 2013 Benjamin Tissoires <benjamin.tissoires@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Copyright (C) 2013 Red Hat Inc. All Rights Reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) MODULE_AUTHOR("James McKenzie");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) MODULE_DESCRIPTION("HID Apple IR remote controls");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define KEY_MASK 0x0F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define TWO_PACKETS_MASK 0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * James McKenzie has two devices both of which report the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * 25 87 ee 83 0a +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * 25 87 ee 83 0c -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * 25 87 ee 83 09 <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * 25 87 ee 83 06 >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * 25 87 ee 83 05 >"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * 25 87 ee 83 03 menu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * 26 00 00 00 00 for key repeat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Thomas Glanzmann reports the following responses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * 25 87 ee ca 0b +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * 25 87 ee ca 0d -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * 25 87 ee ca 08 <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * 25 87 ee ca 07 >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * 25 87 ee ca 04 >"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * 25 87 ee ca 02 menu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * 26 00 00 00 00 for key repeat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * He also observes the following event sometimes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * sent after a key is release, which I interpret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * as a flat battery message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * 25 87 e0 ca 06 flat battery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Alexandre Karpenko reports the following responses for Device ID 0x8242
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * 25 87 ee 47 0b +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * 25 87 ee 47 0d -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * 25 87 ee 47 08 <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * 25 87 ee 47 07 >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * 25 87 ee 47 04 >"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * 25 87 ee 47 02 menu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * 26 87 ee 47 ** for key repeat (** is the code of the key being held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * Bastien Nocera's remote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * 25 87 ee 91 5f followed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * 25 87 ee 91 05 gives you >"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * 25 87 ee 91 5c followed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * 25 87 ee 91 05 gives you the middle button
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * Fabien Andre's remote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * 25 87 ee a3 5e followed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * 25 87 ee a3 04 gives you >"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * 25 87 ee a3 5d followed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * 25 87 ee a3 04 gives you the middle button
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static const unsigned short appleir_key_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) KEY_MENU,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) KEY_PLAYPAUSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) KEY_FORWARD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) KEY_BACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) KEY_VOLUMEUP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) KEY_VOLUMEDOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) KEY_ENTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) KEY_PLAYPAUSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct appleir {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct hid_device *hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) unsigned short keymap[ARRAY_SIZE(appleir_key_table)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct timer_list key_up_timer; /* timer for key up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) spinlock_t lock; /* protects .current_key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int current_key; /* the currently pressed key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int prev_key_idx; /* key index in a 2 packets message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int get_key(int data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * The key is coded accross bits 2..9:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * 0x00 or 0x01 ( ) key: 0 -> KEY_RESERVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * 0x02 or 0x03 ( menu ) key: 1 -> KEY_MENU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * 0x04 or 0x05 ( >" ) key: 2 -> KEY_PLAYPAUSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * 0x06 or 0x07 ( >> ) key: 3 -> KEY_FORWARD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * 0x08 or 0x09 ( << ) key: 4 -> KEY_BACK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * 0x0a or 0x0b ( + ) key: 5 -> KEY_VOLUMEUP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * 0x0c or 0x0d ( - ) key: 6 -> KEY_VOLUMEDOWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * 0x0e or 0x0f ( ) key: 7 -> KEY_RESERVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * 0x50 or 0x51 ( ) key: 8 -> KEY_RESERVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * 0x52 or 0x53 ( ) key: 9 -> KEY_RESERVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * 0x54 or 0x55 ( ) key: 10 -> KEY_RESERVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * 0x56 or 0x57 ( ) key: 11 -> KEY_RESERVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * 0x58 or 0x59 ( ) key: 12 -> KEY_RESERVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * 0x5a or 0x5b ( ) key: 13 -> KEY_RESERVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * 0x5c or 0x5d ( middle ) key: 14 -> KEY_ENTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * 0x5e or 0x5f ( >" ) key: 15 -> KEY_PLAYPAUSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * Packets starting with 0x5 are part of a two-packets message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * we notify the caller by sending a negative value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int key = (data >> 1) & KEY_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if ((data & TWO_PACKETS_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* Part of a 2 packets-command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) key = -key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static void key_up(struct hid_device *hid, struct appleir *appleir, int key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) input_report_key(appleir->input_dev, key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) input_sync(appleir->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void key_down(struct hid_device *hid, struct appleir *appleir, int key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) input_report_key(appleir->input_dev, key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) input_sync(appleir->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static void battery_flat(struct appleir *appleir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) dev_err(&appleir->input_dev->dev, "possible flat battery?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void key_up_tick(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct appleir *appleir = from_timer(appleir, t, key_up_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct hid_device *hid = appleir->hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) spin_lock_irqsave(&appleir->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (appleir->current_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) key_up(hid, appleir, appleir->current_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) appleir->current_key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) spin_unlock_irqrestore(&appleir->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int appleir_raw_event(struct hid_device *hid, struct hid_report *report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) u8 *data, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct appleir *appleir = hid_get_drvdata(hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static const u8 keydown[] = { 0x25, 0x87, 0xee };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static const u8 keyrepeat[] = { 0x26, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static const u8 flatbattery[] = { 0x25, 0x87, 0xe0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (len != 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!memcmp(data, keydown, sizeof(keydown))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) spin_lock_irqsave(&appleir->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * If we already have a key down, take it up before marking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * this one down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (appleir->current_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) key_up(hid, appleir, appleir->current_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* Handle dual packet commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (appleir->prev_key_idx > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) index = appleir->prev_key_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) index = get_key(data[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (index >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) appleir->current_key = appleir->keymap[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) key_down(hid, appleir, appleir->current_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * Remote doesn't do key up, either pull them up, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * the test above, or here set a timer which pulls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * them up after 1/8 s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) mod_timer(&appleir->key_up_timer, jiffies + HZ / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) appleir->prev_key_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* Remember key for next packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) appleir->prev_key_idx = -index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) spin_unlock_irqrestore(&appleir->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) appleir->prev_key_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (!memcmp(data, keyrepeat, sizeof(keyrepeat))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) key_down(hid, appleir, appleir->current_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * Remote doesn't do key up, either pull them up, in the test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * above, or here set a timer which pulls them up after 1/8 s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) mod_timer(&appleir->key_up_timer, jiffies + HZ / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (!memcmp(data, flatbattery, sizeof(flatbattery))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) battery_flat(appleir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /* Fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /* let hidraw and hiddev handle the report */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static int appleir_input_configured(struct hid_device *hid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct hid_input *hidinput)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct input_dev *input_dev = hidinput->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct appleir *appleir = hid_get_drvdata(hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) appleir->input_dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) input_dev->keycode = appleir->keymap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) input_dev->keycodesize = sizeof(unsigned short);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) input_dev->keycodemax = ARRAY_SIZE(appleir->keymap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) memcpy(appleir->keymap, appleir_key_table, sizeof(appleir->keymap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) for (i = 0; i < ARRAY_SIZE(appleir_key_table); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) set_bit(appleir->keymap[i], input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) clear_bit(KEY_RESERVED, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static int appleir_input_mapping(struct hid_device *hid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct hid_input *hi, struct hid_field *field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct hid_usage *usage, unsigned long **bit, int *max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int appleir_probe(struct hid_device *hid, const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct appleir *appleir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) appleir = devm_kzalloc(&hid->dev, sizeof(struct appleir), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!appleir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) appleir->hid = hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /* force input as some remotes bypass the input registration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) hid->quirks |= HID_QUIRK_HIDINPUT_FORCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) spin_lock_init(&appleir->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) timer_setup(&appleir->key_up_timer, key_up_tick, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) hid_set_drvdata(hid, appleir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) ret = hid_parse(hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) hid_err(hid, "parse failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ret = hid_hw_start(hid, HID_CONNECT_DEFAULT | HID_CONNECT_HIDDEV_FORCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) hid_err(hid, "hw start failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) devm_kfree(&hid->dev, appleir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static void appleir_remove(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct appleir *appleir = hid_get_drvdata(hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) hid_hw_stop(hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) del_timer_sync(&appleir->key_up_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static const struct hid_device_id appleir_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL2) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL3) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL5) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) MODULE_DEVICE_TABLE(hid, appleir_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static struct hid_driver appleir_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) .name = "appleir",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) .id_table = appleir_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) .raw_event = appleir_raw_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) .input_configured = appleir_input_configured,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) .probe = appleir_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) .remove = appleir_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) .input_mapping = appleir_input_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) module_hid_driver(appleir_driver);