2 * HID driver for the apple ir device
4 * Original driver written by James McKenzie
5 * Ported to recent 2.6 kernel versions by Greg Kroah-Hartman <gregkh@suse.de>
6 * Updated to support newer remotes by Bastien Nocera <hadess@hadess.net>
7 * Ported to HID subsystem by Benjamin Tissoires <benjamin.tissoires@gmail.com>
9 * Copyright (C) 2006 James McKenzie
10 * Copyright (C) 2008 Greg Kroah-Hartman <greg@kroah.com>
11 * Copyright (C) 2008 Novell Inc.
12 * Copyright (C) 2010, 2012 Bastien Nocera <hadess@hadess.net>
13 * Copyright (C) 2013 Benjamin Tissoires <benjamin.tissoires@gmail.com>
14 * Copyright (C) 2013 Red Hat Inc. All Rights Reserved
16 * This software is licensed under the terms of the GNU General Public
17 * License version 2, as published by the Free Software Foundation, and
18 * may be copied, distributed, and modified under those terms.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26 #include <linux/device.h>
27 #include <linux/hid.h>
28 #include <linux/module.h>
31 MODULE_AUTHOR("James McKenzie");
32 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>");
33 MODULE_DESCRIPTION("HID Apple IR remote controls");
34 MODULE_LICENSE("GPL");
37 #define TWO_PACKETS_MASK 0x40
40 * James McKenzie has two devices both of which report the following
47 * 26 00 00 00 00 for key repeat
51 * Thomas Glanzmann reports the following responses
58 * 26 00 00 00 00 for key repeat
60 * He also observes the following event sometimes
61 * sent after a key is release, which I interpret
62 * as a flat battery message
63 * 25 87 e0 ca 06 flat battery
67 * Alexandre Karpenko reports the following responses for Device ID 0x8242
74 * 26 87 ee 47 ** for key repeat (** is the code of the key being held)
78 * Bastien Nocera's remote
79 * 25 87 ee 91 5f followed by
80 * 25 87 ee 91 05 gives you >"
82 * 25 87 ee 91 5c followed by
83 * 25 87 ee 91 05 gives you the middle button
87 * Fabien Andre's remote
88 * 25 87 ee a3 5e followed by
89 * 25 87 ee a3 04 gives you >"
91 * 25 87 ee a3 5d followed by
92 * 25 87 ee a3 04 gives you the middle button
95 static const unsigned short appleir_key_table[] = {
116 struct input_dev *input_dev;
117 struct hid_device *hid;
118 unsigned short keymap[ARRAY_SIZE(appleir_key_table)];
119 struct timer_list key_up_timer; /* timer for key up */
120 spinlock_t lock; /* protects .current_key */
121 int current_key; /* the currently pressed key */
122 int prev_key_idx; /* key index in a 2 packets message */
125 static int get_key(int data)
128 * The key is coded accross bits 2..9:
130 * 0x00 or 0x01 ( ) key: 0 -> KEY_RESERVED
131 * 0x02 or 0x03 ( menu ) key: 1 -> KEY_MENU
132 * 0x04 or 0x05 ( >" ) key: 2 -> KEY_PLAYPAUSE
133 * 0x06 or 0x07 ( >> ) key: 3 -> KEY_FORWARD
134 * 0x08 or 0x09 ( << ) key: 4 -> KEY_BACK
135 * 0x0a or 0x0b ( + ) key: 5 -> KEY_VOLUMEUP
136 * 0x0c or 0x0d ( - ) key: 6 -> KEY_VOLUMEDOWN
137 * 0x0e or 0x0f ( ) key: 7 -> KEY_RESERVED
138 * 0x50 or 0x51 ( ) key: 8 -> KEY_RESERVED
139 * 0x52 or 0x53 ( ) key: 9 -> KEY_RESERVED
140 * 0x54 or 0x55 ( ) key: 10 -> KEY_RESERVED
141 * 0x56 or 0x57 ( ) key: 11 -> KEY_RESERVED
142 * 0x58 or 0x59 ( ) key: 12 -> KEY_RESERVED
143 * 0x5a or 0x5b ( ) key: 13 -> KEY_RESERVED
144 * 0x5c or 0x5d ( middle ) key: 14 -> KEY_ENTER
145 * 0x5e or 0x5f ( >" ) key: 15 -> KEY_PLAYPAUSE
147 * Packets starting with 0x5 are part of a two-packets message,
148 * we notify the caller by sending a negative value.
150 int key = (data >> 1) & KEY_MASK;
152 if ((data & TWO_PACKETS_MASK))
153 /* Part of a 2 packets-command */
159 static void key_up(struct hid_device *hid, struct appleir *appleir, int key)
161 input_report_key(appleir->input_dev, key, 0);
162 input_sync(appleir->input_dev);
165 static void key_down(struct hid_device *hid, struct appleir *appleir, int key)
167 input_report_key(appleir->input_dev, key, 1);
168 input_sync(appleir->input_dev);
171 static void battery_flat(struct appleir *appleir)
173 dev_err(&appleir->input_dev->dev, "possible flat battery?\n");
176 static void key_up_tick(unsigned long data)
178 struct appleir *appleir = (struct appleir *)data;
179 struct hid_device *hid = appleir->hid;
182 spin_lock_irqsave(&appleir->lock, flags);
183 if (appleir->current_key) {
184 key_up(hid, appleir, appleir->current_key);
185 appleir->current_key = 0;
187 spin_unlock_irqrestore(&appleir->lock, flags);
190 static int appleir_raw_event(struct hid_device *hid, struct hid_report *report,
193 struct appleir *appleir = hid_get_drvdata(hid);
194 static const u8 keydown[] = { 0x25, 0x87, 0xee };
195 static const u8 keyrepeat[] = { 0x26, };
196 static const u8 flatbattery[] = { 0x25, 0x87, 0xe0 };
202 if (!memcmp(data, keydown, sizeof(keydown))) {
205 spin_lock_irqsave(&appleir->lock, flags);
207 * If we already have a key down, take it up before marking
210 if (appleir->current_key)
211 key_up(hid, appleir, appleir->current_key);
213 /* Handle dual packet commands */
214 if (appleir->prev_key_idx > 0)
215 index = appleir->prev_key_idx;
217 index = get_key(data[4]);
220 appleir->current_key = appleir->keymap[index];
222 key_down(hid, appleir, appleir->current_key);
224 * Remote doesn't do key up, either pull them up, in
225 * the test above, or here set a timer which pulls
226 * them up after 1/8 s
228 mod_timer(&appleir->key_up_timer, jiffies + HZ / 8);
229 appleir->prev_key_idx = 0;
231 /* Remember key for next packet */
232 appleir->prev_key_idx = -index;
233 spin_unlock_irqrestore(&appleir->lock, flags);
237 appleir->prev_key_idx = 0;
239 if (!memcmp(data, keyrepeat, sizeof(keyrepeat))) {
240 key_down(hid, appleir, appleir->current_key);
242 * Remote doesn't do key up, either pull them up, in the test
243 * above, or here set a timer which pulls them up after 1/8 s
245 mod_timer(&appleir->key_up_timer, jiffies + HZ / 8);
249 if (!memcmp(data, flatbattery, sizeof(flatbattery))) {
250 battery_flat(appleir);
255 /* let hidraw and hiddev handle the report */
259 static void appleir_input_configured(struct hid_device *hid,
260 struct hid_input *hidinput)
262 struct input_dev *input_dev = hidinput->input;
263 struct appleir *appleir = hid_get_drvdata(hid);
266 appleir->input_dev = input_dev;
268 input_dev->keycode = appleir->keymap;
269 input_dev->keycodesize = sizeof(unsigned short);
270 input_dev->keycodemax = ARRAY_SIZE(appleir->keymap);
272 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
274 memcpy(appleir->keymap, appleir_key_table, sizeof(appleir->keymap));
275 for (i = 0; i < ARRAY_SIZE(appleir_key_table); i++)
276 set_bit(appleir->keymap[i], input_dev->keybit);
277 clear_bit(KEY_RESERVED, input_dev->keybit);
280 static int appleir_input_mapping(struct hid_device *hid,
281 struct hid_input *hi, struct hid_field *field,
282 struct hid_usage *usage, unsigned long **bit, int *max)
287 static int appleir_probe(struct hid_device *hid, const struct hid_device_id *id)
290 struct appleir *appleir;
292 appleir = kzalloc(sizeof(struct appleir), GFP_KERNEL);
300 spin_lock_init(&appleir->lock);
301 setup_timer(&appleir->key_up_timer,
302 key_up_tick, (unsigned long) appleir);
304 hid_set_drvdata(hid, appleir);
306 ret = hid_parse(hid);
308 hid_err(hid, "parse failed\n");
312 ret = hid_hw_start(hid, HID_CONNECT_DEFAULT | HID_CONNECT_HIDDEV_FORCE);
314 hid_err(hid, "hw start failed\n");
325 static void appleir_remove(struct hid_device *hid)
327 struct appleir *appleir = hid_get_drvdata(hid);
329 del_timer_sync(&appleir->key_up_timer);
333 static const struct hid_device_id appleir_devices[] = {
334 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL) },
335 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL2) },
336 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL3) },
337 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
338 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL5) },
341 MODULE_DEVICE_TABLE(hid, appleir_devices);
343 static struct hid_driver appleir_driver = {
345 .id_table = appleir_devices,
346 .raw_event = appleir_raw_event,
347 .input_configured = appleir_input_configured,
348 .probe = appleir_probe,
349 .remove = appleir_remove,
350 .input_mapping = appleir_input_mapping,
352 module_hid_driver(appleir_driver);