Jesse Keating 2f82dda
 Documentation/input/appleir.txt |   46 ++++
Jesse Keating 2f82dda
 drivers/hid/hid-apple.c         |    4 -
Jesse Keating 2f82dda
 drivers/hid/hid-core.c          |    5 +-
Jesse Keating 2f82dda
 drivers/hid/hid-ids.h           |    1 +
Jesse Keating 2f82dda
 drivers/input/misc/Kconfig      |   13 +
Jesse Keating 2f82dda
 drivers/input/misc/Makefile     |    1 +
Jesse Keating 2f82dda
 drivers/input/misc/appleir.c    |  477 +++++++++++++++++++++++++++++++++++++++
Jesse Keating 2f82dda
 7 files changed, 541 insertions(+), 6 deletions(-)
Jesse Keating 2f82dda
 create mode 100644 Documentation/input/appleir.txt
Jesse Keating 2f82dda
 create mode 100644 drivers/input/misc/appleir.c
Jesse Keating 2f82dda
Jesse Keating 2f82dda
diff --git a/Documentation/input/appleir.txt b/Documentation/input/appleir.txt
Jesse Keating 2f82dda
new file mode 100644
Jesse Keating 2f82dda
index 0000000..0aaf5fe
Jesse Keating 2f82dda
--- /dev/null
Jesse Keating 2f82dda
+++ b/Documentation/input/appleir.txt
Jesse Keating 2f82dda
@@ -0,0 +1,46 @@
Jesse Keating 2f82dda
+Apple IR receiver Driver (appleir)
Jesse Keating 2f82dda
+----------------------------------
Jesse Keating 2f82dda
+	Copyright (C) 2009 Bastien Nocera <hadess@hadess.net>
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+The appleir driver is a kernel input driver to handle Apple's IR
Jesse Keating 2f82dda
+receivers (and associated remotes) in the kernel.
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+The driver is an input driver which only handles "official" remotes
Jesse Keating 2f82dda
+as built and sold by Apple.
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+Authors
Jesse Keating 2f82dda
+-------
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+James McKenzie (original driver)
Jesse Keating 2f82dda
+Alex Karpenko (05ac:8242 support)
Jesse Keating 2f82dda
+Greg Kroah-Hartman (cleanups and original submission)
Jesse Keating 2f82dda
+Bastien Nocera (further cleanups and suspend support)
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+Supported hardware
Jesse Keating 2f82dda
+------------------
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+- All Apple laptops and desktops from 2005 onwards, except:
Jesse Keating 2f82dda
+  - the unibody Macbook (2009)
Jesse Keating 2f82dda
+  - Mac Pro (all versions)
Jesse Keating 2f82dda
+- Apple TV (all revisions)
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+The remote will only support the 6 buttons of the original remotes
Jesse Keating 2f82dda
+as sold by Apple. See the next section if you want to use other remotes
Jesse Keating 2f82dda
+or want to use lirc with the device instead of the kernel driver.
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+Using lirc (native) instead of the kernel driver
Jesse Keating 2f82dda
+------------------------------------------------
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+First, you will need to disable the kernel driver for the receiver.
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+This can be achieved by passing quirks to the usbhid driver.
Jesse Keating 2f82dda
+The quirk line would be:
Jesse Keating 2f82dda
+usbhid.quirks=0x05ac:0x8242:0x08
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+With 0x05ac being the vendor ID (Apple, you shouldn't need to change this)
Jesse Keating 2f82dda
+With 0x8242 being the product ID (check the output of lsusb for your hardware)
Jesse Keating 2f82dda
+And 0x08 being "HID_CONNECT_HIDDEV"
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+This should force the creation of a hiddev device for the receiver, and
Jesse Keating 2f82dda
+make it usable under lirc.
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
Jesse Keating 2f82dda
index 4b96e7a..d1fdcd0 100644
Jesse Keating 2f82dda
--- a/drivers/hid/hid-apple.c
Jesse Keating 2f82dda
+++ b/drivers/hid/hid-apple.c
Jesse Keating 2f82dda
@@ -353,10 +353,6 @@ static void apple_remove(struct hid_device *hdev)
Jesse Keating 2f82dda
 }
Jesse Keating 2f82dda
 
Jesse Keating 2f82dda
 static const struct hid_device_id apple_devices[] = {
Jesse Keating 2f82dda
-	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL),
Jesse Keating 2f82dda
-		.driver_data = APPLE_HIDDEV | APPLE_IGNORE_HIDINPUT },
Jesse Keating 2f82dda
-	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4),
Jesse Keating 2f82dda
-		.driver_data = APPLE_HIDDEV | APPLE_IGNORE_HIDINPUT },
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE),
Jesse Keating 2f82dda
 		.driver_data = APPLE_MIGHTYMOUSE | APPLE_INVERT_HWHEEL },
Jesse Keating 2f82dda
 
Jesse Keating 2f82dda
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
Jesse Keating 2f82dda
index 7d05c4b..3efb0fa 100644
Jesse Keating 2f82dda
--- a/drivers/hid/hid-core.c
Jesse Keating 2f82dda
+++ b/drivers/hid/hid-core.c
Jesse Keating 2f82dda
@@ -1252,8 +1252,6 @@ EXPORT_SYMBOL_GPL(hid_disconnect);
Jesse Keating 2f82dda
 static const struct hid_device_id hid_blacklist[] = {
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
Jesse Keating 2f82dda
-	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
Jesse Keating 2f82dda
-	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
Jesse Keating 2f82dda
@@ -1539,6 +1537,9 @@ static const struct hid_device_id hid_ignore_list[] = {
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
Jesse Keating 2f82dda
+	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL) },
Jesse Keating 2f82dda
+	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
Jesse Keating 2f82dda
+	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2)},
Jesse Keating 2f82dda
 	{ HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
Jesse Keating 2f82dda
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
Jesse Keating 2f82dda
index adbef5d..c399110 100644
Jesse Keating 2f82dda
--- a/drivers/hid/hid-ids.h
Jesse Keating 2f82dda
+++ b/drivers/hid/hid-ids.h
Jesse Keating 2f82dda
@@ -90,6 +90,7 @@
Jesse Keating 2f82dda
 #define USB_DEVICE_ID_APPLE_WELLSPRING3_JIS	0x0238
Jesse Keating 2f82dda
 #define USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY	0x030a
Jesse Keating 2f82dda
 #define USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY	0x030b
Jesse Keating 2f82dda
+#define USB_DEVICE_ID_APPLE_IRCONTROL	0x8240
Jesse Keating 2f82dda
 #define USB_DEVICE_ID_APPLE_ATV_IRCONTROL	0x8241
Jesse Keating 2f82dda
 #define USB_DEVICE_ID_APPLE_IRCONTROL4	0x8242
Jesse Keating 2f82dda
 
Jesse Keating 2f82dda
diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
Jesse Keating 2f82dda
index a9bb254..51b6684 100644
Jesse Keating 2f82dda
--- a/drivers/input/misc/Kconfig
Jesse Keating 2f82dda
+++ b/drivers/input/misc/Kconfig
Jesse Keating 2f82dda
@@ -148,6 +148,19 @@ config INPUT_KEYSPAN_REMOTE
Jesse Keating 2f82dda
 	  To compile this driver as a module, choose M here: the module will
Jesse Keating 2f82dda
 	  be called keyspan_remote.
Jesse Keating 2f82dda
 
Jesse Keating 2f82dda
+config INPUT_APPLEIR
Jesse Keating 2f82dda
+	tristate "Apple infrared receiver (built in)"
Jesse Keating 2f82dda
+	depends on USB_ARCH_HAS_HCD
Jesse Keating 2f82dda
+	select USB
Jesse Keating 2f82dda
+	help
Jesse Keating 2f82dda
+	  Say Y here if you want to use a Apple infrared remote control. All
Jesse Keating 2f82dda
+	  the Apple computers from 2005 onwards include such a port, except
Jesse Keating 2f82dda
+	  the unibody Macbook (2009), and Mac Pros. This receiver is also
Jesse Keating 2f82dda
+	  used in the Apple TV set-top box.
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	  To compile this driver as a module, choose M here: the module will
Jesse Keating 2f82dda
+	  be called appleir.
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
 config INPUT_POWERMATE
Jesse Keating 2f82dda
 	tristate "Griffin PowerMate and Contour Jog support"
Jesse Keating 2f82dda
 	depends on USB_ARCH_HAS_HCD
Jesse Keating 2f82dda
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
Jesse Keating 2f82dda
index a8b8485..041e6f5 100644
Jesse Keating 2f82dda
--- a/drivers/input/misc/Makefile
Jesse Keating 2f82dda
+++ b/drivers/input/misc/Makefile
Jesse Keating 2f82dda
@@ -5,6 +5,7 @@
Jesse Keating 2f82dda
 # Each configuration option enables a list of files.
Jesse Keating 2f82dda
 
Jesse Keating 2f82dda
 obj-$(CONFIG_INPUT_APANEL)		+= apanel.o
Jesse Keating 2f82dda
+obj-$(CONFIG_INPUT_APPLEIR)		+= appleir.o
Jesse Keating 2f82dda
 obj-$(CONFIG_INPUT_ATI_REMOTE)		+= ati_remote.o
Jesse Keating 2f82dda
 obj-$(CONFIG_INPUT_ATI_REMOTE2)		+= ati_remote2.o
Jesse Keating 2f82dda
 obj-$(CONFIG_INPUT_ATLAS_BTNS)		+= atlas_btns.o
Jesse Keating 2f82dda
diff --git a/drivers/input/misc/appleir.c b/drivers/input/misc/appleir.c
Jesse Keating 2f82dda
new file mode 100644
Jesse Keating 2f82dda
index 0000000..6e332ab
Jesse Keating 2f82dda
--- /dev/null
Jesse Keating 2f82dda
+++ b/drivers/input/misc/appleir.c
Jesse Keating 2f82dda
@@ -0,0 +1,477 @@
Jesse Keating 2f82dda
+/*
Jesse Keating 2f82dda
+ * appleir: USB driver for the apple ir device
Jesse Keating 2f82dda
+ *
Jesse Keating 2f82dda
+ * Original driver written by James McKenzie
Jesse Keating 2f82dda
+ * Ported to recent 2.6 kernel versions by Greg Kroah-Hartman <gregkh@suse.de>
Jesse Keating 2f82dda
+ *
Jesse Keating 2f82dda
+ * Copyright (C) 2006 James McKenzie
Jesse Keating 2f82dda
+ * Copyright (C) 2008 Greg Kroah-Hartman <greg@kroah.com>
Jesse Keating 2f82dda
+ * Copyright (C) 2008 Novell Inc.
Jesse Keating 2f82dda
+ *
Jesse Keating 2f82dda
+ * This program is free software; you can redistribute it and/or modify it
Jesse Keating 2f82dda
+ * under the terms of the GNU General Public License as published by the Free
Jesse Keating 2f82dda
+ * Software Foundation, version 2.
Jesse Keating 2f82dda
+ *
Jesse Keating 2f82dda
+ */
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+#include <linux/kernel.h>
Jesse Keating 2f82dda
+#include <linux/slab.h>
Jesse Keating 2f82dda
+#include <linux/input.h>
Jesse Keating 2f82dda
+#include <linux/usb/input.h>
Jesse Keating 2f82dda
+#include <linux/module.h>
Jesse Keating 2f82dda
+#include <linux/init.h>
Jesse Keating 2f82dda
+#include <linux/usb.h>
Jesse Keating 2f82dda
+#include <linux/usb/input.h>
Jesse Keating 2f82dda
+#include <asm/unaligned.h>
Jesse Keating 2f82dda
+#include <asm/byteorder.h>
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+#define DRIVER_VERSION "v1.2"
Jesse Keating 2f82dda
+#define DRIVER_AUTHOR "James McKenzie"
Jesse Keating 2f82dda
+#define DRIVER_DESC "Apple infrared receiver driver"
Jesse Keating 2f82dda
+#define DRIVER_LICENSE "GPL"
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+MODULE_AUTHOR(DRIVER_AUTHOR);
Jesse Keating 2f82dda
+MODULE_DESCRIPTION(DRIVER_DESC);
Jesse Keating 2f82dda
+MODULE_LICENSE(DRIVER_LICENSE);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+#define USB_VENDOR_ID_APPLE			0x05ac
Jesse Keating 2f82dda
+#define USB_DEVICE_ID_APPLE_IRCONTROL		0x8240
Jesse Keating 2f82dda
+#define USB_DEVICE_ID_APPLE_ATV_IRCONTROL	0x8241
Jesse Keating 2f82dda
+#define USB_DEVICE_ID_APPLE_IRCONTROL4		0x8242
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+#define URB_SIZE	32
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+#define MAX_KEYS	8
Jesse Keating 2f82dda
+#define MAX_KEYS_MASK	(MAX_KEYS - 1)
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+#define dbginfo(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static int debug;
Jesse Keating 2f82dda
+module_param(debug, int, 0644);
Jesse Keating 2f82dda
+MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+struct appleir {
Jesse Keating 2f82dda
+	struct input_dev *input_dev;
Jesse Keating 2f82dda
+	u8 *data;
Jesse Keating 2f82dda
+	dma_addr_t dma_buf;
Jesse Keating 2f82dda
+	struct usb_device *usbdev;
Jesse Keating 2f82dda
+	unsigned int flags;
Jesse Keating 2f82dda
+	struct urb *urb;
Jesse Keating 2f82dda
+	int timer_initted;
Jesse Keating 2f82dda
+	struct timer_list key_up_timer;
Jesse Keating 2f82dda
+	int current_key;
Jesse Keating 2f82dda
+	char phys[32];
Jesse Keating 2f82dda
+};
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static DEFINE_MUTEX(appleir_mutex);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+enum {
Jesse Keating 2f82dda
+	APPLEIR_OPENED = 0x1,
Jesse Keating 2f82dda
+	APPLEIR_SUSPENDED = 0x2,
Jesse Keating 2f82dda
+};
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static struct usb_device_id appleir_ids[] = {
Jesse Keating 2f82dda
+	{ USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL) },
Jesse Keating 2f82dda
+	{ USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
Jesse Keating 2f82dda
+	{ USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
Jesse Keating 2f82dda
+	{}
Jesse Keating 2f82dda
+};
Jesse Keating 2f82dda
+MODULE_DEVICE_TABLE(usb, appleir_ids);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+/* I have two devices both of which report the following */
Jesse Keating 2f82dda
+/* 25 87 ee 83 0a  	+  */
Jesse Keating 2f82dda
+/* 25 87 ee 83 0c  	-  */
Jesse Keating 2f82dda
+/* 25 87 ee 83 09	<< */
Jesse Keating 2f82dda
+/* 25 87 ee 83 06	>> */
Jesse Keating 2f82dda
+/* 25 87 ee 83 05	>" */
Jesse Keating 2f82dda
+/* 25 87 ee 83 03	menu */
Jesse Keating 2f82dda
+/* 26 00 00 00 00	for key repeat*/
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+/* Thomas Glanzmann reports the following responses */
Jesse Keating 2f82dda
+/* 25 87 ee ca 0b	+  */
Jesse Keating 2f82dda
+/* 25 87 ee ca 0d	-  */
Jesse Keating 2f82dda
+/* 25 87 ee ca 08	<< */
Jesse Keating 2f82dda
+/* 25 87 ee ca 07	>> */
Jesse Keating 2f82dda
+/* 25 87 ee ca 04	>" */
Jesse Keating 2f82dda
+/* 25 87 ee ca 02 	menu */
Jesse Keating 2f82dda
+/* 26 00 00 00 00       for key repeat*/
Jesse Keating 2f82dda
+/* He also observes the following event sometimes */
Jesse Keating 2f82dda
+/* sent after a key is release, which I interpret */
Jesse Keating 2f82dda
+/* as a flat battery message */
Jesse Keating 2f82dda
+/* 25 87 e0 ca 06	flat battery */
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+/* Alexandre Karpenko reports the following responses for Device ID 0x8242 */
Jesse Keating 2f82dda
+/* 25 87 ee 47 0b	+  */
Jesse Keating 2f82dda
+/* 25 87 ee 47 0d	-  */
Jesse Keating 2f82dda
+/* 25 87 ee 47 08	<< */
Jesse Keating 2f82dda
+/* 25 87 ee 47 07	>> */
Jesse Keating 2f82dda
+/* 25 87 ee 47 04	>" */
Jesse Keating 2f82dda
+/* 25 87 ee 47 02 	menu */
Jesse Keating 2f82dda
+/* 26 87 ee 47 ** 	for key repeat (** is the code of the key being held) */
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static int keymap[MAX_KEYS] = {
Jesse Keating 2f82dda
+	KEY_RESERVED,
Jesse Keating 2f82dda
+	KEY_MENU,
Jesse Keating 2f82dda
+	KEY_PLAYPAUSE,
Jesse Keating 2f82dda
+	KEY_FORWARD,
Jesse Keating 2f82dda
+	KEY_BACK,
Jesse Keating 2f82dda
+	KEY_VOLUMEUP,
Jesse Keating 2f82dda
+	KEY_VOLUMEDOWN,
Jesse Keating 2f82dda
+	KEY_RESERVED,
Jesse Keating 2f82dda
+};
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static void dump_packet(struct appleir *appleir, char *msg, u8 *data, int len)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	int i;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	printk(KERN_ERR "appleir: %s (%d bytes)", msg, len);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	for (i = 0; i < len; ++i)
Jesse Keating 2f82dda
+		printk(" %02x", data[i]);
Jesse Keating 2f82dda
+	printk("\n");
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static void key_up(struct appleir *appleir, int key)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	dbginfo (&appleir->input_dev->dev, "key %d up\n", key);
Jesse Keating 2f82dda
+	input_report_key(appleir->input_dev, key, 0);
Jesse Keating 2f82dda
+	input_sync(appleir->input_dev);
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static void key_down(struct appleir *appleir, int key)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	dbginfo (&appleir->input_dev->dev, "key %d down\n", key);
Jesse Keating 2f82dda
+	input_report_key(appleir->input_dev, key, 1);
Jesse Keating 2f82dda
+	input_sync(appleir->input_dev);
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static void battery_flat(struct appleir *appleir)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	dev_err(&appleir->input_dev->dev, "possible flat battery?\n");
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static void key_up_tick(unsigned long data)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	struct appleir *appleir = (struct appleir *)data;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	if (appleir->current_key) {
Jesse Keating 2f82dda
+		key_up(appleir, appleir->current_key);
Jesse Keating 2f82dda
+		appleir->current_key = 0;
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static void new_data(struct appleir *appleir, u8 *data, int len)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	static const u8 keydown[] = { 0x25, 0x87, 0xee };
Jesse Keating 2f82dda
+	static const u8 keyrepeat[] = { 0x26, };
Jesse Keating 2f82dda
+	static const u8 flatbattery[] = { 0x25, 0x87, 0xe0 };
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	if (debug)
Jesse Keating 2f82dda
+		dump_packet(appleir, "received", data, len);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	if (len != 5)
Jesse Keating 2f82dda
+		return;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	if (!memcmp(data, keydown, sizeof(keydown))) {
Jesse Keating 2f82dda
+		/*If we already have a key down, take it up before marking */
Jesse Keating 2f82dda
+		/*this one down */
Jesse Keating 2f82dda
+		if (appleir->current_key)
Jesse Keating 2f82dda
+			key_up(appleir, appleir->current_key);
Jesse Keating 2f82dda
+		appleir->current_key = keymap[(data[4] >> 1) & MAX_KEYS_MASK];
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+		key_down(appleir, appleir->current_key);
Jesse Keating 2f82dda
+		/*remote doesn't do key up, either pull them up, in the test */
Jesse Keating 2f82dda
+		/*above, or here set a timer which pulls them up after 1/8 s */
Jesse Keating 2f82dda
+		mod_timer(&appleir->key_up_timer, jiffies + HZ / 8);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+		return;
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	if (!memcmp(data, keyrepeat, sizeof(keyrepeat))) {
Jesse Keating 2f82dda
+		key_down(appleir, appleir->current_key);
Jesse Keating 2f82dda
+		/*remote doesn't do key up, either pull them up, in the test */
Jesse Keating 2f82dda
+		/*above, or here set a timer which pulls them up after 1/8 s */
Jesse Keating 2f82dda
+		mod_timer(&appleir->key_up_timer, jiffies + HZ / 8);
Jesse Keating 2f82dda
+		return;
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	if (!memcmp(data, flatbattery, sizeof(flatbattery))) {
Jesse Keating 2f82dda
+		battery_flat(appleir);
Jesse Keating 2f82dda
+		/* Fall through */
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	dump_packet(appleir, "unknown packet", data, len);
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static void appleir_urb(struct urb *urb)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	struct appleir *appleir = urb->context;
Jesse Keating 2f82dda
+	int status = urb->status;
Jesse Keating 2f82dda
+	int retval;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	switch (status) {
Jesse Keating 2f82dda
+	case 0:
Jesse Keating 2f82dda
+		new_data(appleir, urb->transfer_buffer, urb->actual_length);
Jesse Keating 2f82dda
+		break;
Jesse Keating 2f82dda
+	case -ECONNRESET:
Jesse Keating 2f82dda
+	case -ENOENT:
Jesse Keating 2f82dda
+	case -ESHUTDOWN:
Jesse Keating 2f82dda
+		/* this urb is terminated, clean up */
Jesse Keating 2f82dda
+		dbginfo(&appleir->input_dev->dev, "%s - urb shutting down with status: %d", __func__,
Jesse Keating 2f82dda
+			urb->status);
Jesse Keating 2f82dda
+		return;
Jesse Keating 2f82dda
+	default:
Jesse Keating 2f82dda
+		dbginfo(&appleir->input_dev->dev, "%s - nonzero urb status received: %d", __func__,
Jesse Keating 2f82dda
+			urb->status);
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	retval = usb_submit_urb(urb, GFP_ATOMIC);
Jesse Keating 2f82dda
+	if (retval)
Jesse Keating 2f82dda
+		err("%s - usb_submit_urb failed with result %d", __func__,
Jesse Keating 2f82dda
+		    retval);
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static int appleir_open(struct input_dev *dev)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	struct appleir *appleir = input_get_drvdata(dev);
Jesse Keating 2f82dda
+	struct usb_interface *intf = usb_ifnum_to_if(appleir->usbdev, 0);
Jesse Keating 2f82dda
+	int r;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	r = usb_autopm_get_interface(intf);
Jesse Keating 2f82dda
+	if (r) {
Jesse Keating 2f82dda
+		dev_err(&intf->dev,
Jesse Keating 2f82dda
+			"%s(): usb_autopm_get_interface() = %d\n", __func__, r);
Jesse Keating 2f82dda
+		return r;
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	mutex_lock(&appleir_mutex);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	if (usb_submit_urb(appleir->urb, GFP_KERNEL)) {
Jesse Keating 2f82dda
+		r = -EIO;
Jesse Keating 2f82dda
+		goto fail;
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->flags |= APPLEIR_OPENED;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	mutex_unlock(&appleir_mutex);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	usb_autopm_put_interface(intf);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	return 0;
Jesse Keating 2f82dda
+fail:
Jesse Keating 2f82dda
+	mutex_unlock(&appleir_mutex);
Jesse Keating 2f82dda
+	usb_autopm_put_interface(intf);
Jesse Keating 2f82dda
+	return r;
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static void appleir_close(struct input_dev *dev)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	struct appleir *appleir = input_get_drvdata(dev);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	mutex_lock(&appleir_mutex);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	if (!(appleir->flags & APPLEIR_SUSPENDED)) {
Jesse Keating 2f82dda
+		usb_kill_urb(appleir->urb);
Jesse Keating 2f82dda
+		del_timer_sync(&appleir->key_up_timer);
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->flags &= ~APPLEIR_OPENED;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	mutex_unlock(&appleir_mutex);
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static int appleir_probe(struct usb_interface *intf,
Jesse Keating 2f82dda
+			 const struct usb_device_id *id)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	struct usb_device *dev = interface_to_usbdev(intf);
Jesse Keating 2f82dda
+	struct usb_endpoint_descriptor *endpoint;
Jesse Keating 2f82dda
+	struct appleir *appleir = NULL;
Jesse Keating 2f82dda
+	struct input_dev *input_dev;
Jesse Keating 2f82dda
+	int retval = -ENOMEM;
Jesse Keating 2f82dda
+	int i;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir = kzalloc(sizeof(struct appleir), GFP_KERNEL);
Jesse Keating 2f82dda
+	if (!appleir)
Jesse Keating 2f82dda
+		goto fail;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->data = usb_buffer_alloc(dev, URB_SIZE, GFP_KERNEL,
Jesse Keating 2f82dda
+					 &appleir->dma_buf);
Jesse Keating 2f82dda
+	if (!appleir->data)
Jesse Keating 2f82dda
+		goto fail;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->urb = usb_alloc_urb(0, GFP_KERNEL);
Jesse Keating 2f82dda
+	if (!appleir->urb)
Jesse Keating 2f82dda
+		goto fail;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->usbdev = dev;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	input_dev = input_allocate_device();
Jesse Keating 2f82dda
+	if (!input_dev)
Jesse Keating 2f82dda
+		goto fail;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->input_dev = input_dev;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	usb_make_path(dev, appleir->phys, sizeof(appleir->phys));
Jesse Keating 2f82dda
+	strlcpy(appleir->phys, "/input0", sizeof(appleir->phys));
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	input_dev->name = "Apple infrared remote control driver";
Jesse Keating 2f82dda
+	input_dev->phys = appleir->phys;
Jesse Keating 2f82dda
+	usb_to_input_id(dev, &input_dev->id);
Jesse Keating 2f82dda
+	input_dev->dev.parent = &intf->dev;
Jesse Keating 2f82dda
+	input_set_drvdata(input_dev, appleir);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
Jesse Keating 2f82dda
+	input_dev->ledbit[0] = 0;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	for (i = 0; i < MAX_KEYS; i++)
Jesse Keating 2f82dda
+		set_bit(keymap[i], input_dev->keybit);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	clear_bit(0, input_dev->keybit);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	input_dev->open = appleir_open;
Jesse Keating 2f82dda
+	input_dev->close = appleir_close;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	endpoint = &intf->cur_altsetting->endpoint[0].desc;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	usb_fill_int_urb(appleir->urb, dev,
Jesse Keating 2f82dda
+			 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
Jesse Keating 2f82dda
+			 appleir->data, 8,
Jesse Keating 2f82dda
+			 appleir_urb, appleir, endpoint->bInterval);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->urb->transfer_dma = appleir->dma_buf;
Jesse Keating 2f82dda
+	appleir->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	usb_set_intfdata(intf, appleir);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	init_timer(&appleir->key_up_timer);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->key_up_timer.function = key_up_tick;
Jesse Keating 2f82dda
+	appleir->key_up_timer.data = (unsigned long)appleir;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->timer_initted++;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	retval = input_register_device(appleir->input_dev);
Jesse Keating 2f82dda
+	if (retval)
Jesse Keating 2f82dda
+		goto fail;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	return 0;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+fail:
Jesse Keating 2f82dda
+	printk(KERN_WARNING "Failed to load appleir\n");
Jesse Keating 2f82dda
+	if (appleir) {
Jesse Keating 2f82dda
+		if (appleir->data)
Jesse Keating 2f82dda
+			usb_buffer_free(dev, URB_SIZE, appleir->data,
Jesse Keating 2f82dda
+					appleir->dma_buf);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+		if (appleir->timer_initted)
Jesse Keating 2f82dda
+			del_timer_sync(&appleir->key_up_timer);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+		if (appleir->input_dev)
Jesse Keating 2f82dda
+			input_free_device(appleir->input_dev);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+		kfree(appleir);
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	return retval;
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static void appleir_disconnect(struct usb_interface *intf)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	struct appleir *appleir = usb_get_intfdata(intf);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	usb_set_intfdata(intf, NULL);
Jesse Keating 2f82dda
+	if (appleir) {
Jesse Keating 2f82dda
+		input_unregister_device(appleir->input_dev);
Jesse Keating 2f82dda
+		if (appleir->timer_initted)
Jesse Keating 2f82dda
+			del_timer_sync(&appleir->key_up_timer);
Jesse Keating 2f82dda
+		usb_kill_urb(appleir->urb);
Jesse Keating 2f82dda
+		usb_free_urb(appleir->urb);
Jesse Keating 2f82dda
+		usb_buffer_free(interface_to_usbdev(intf), URB_SIZE,
Jesse Keating 2f82dda
+				appleir->data, appleir->dma_buf);
Jesse Keating 2f82dda
+		kfree(appleir);
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static int appleir_suspend(struct usb_interface *interface,
Jesse Keating 2f82dda
+			   pm_message_t message)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	struct appleir *appleir;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir = usb_get_intfdata(interface);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	mutex_lock(&appleir_mutex);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	if (appleir->flags & APPLEIR_OPENED) {
Jesse Keating 2f82dda
+		usb_kill_urb(appleir->urb);
Jesse Keating 2f82dda
+		del_timer_sync(&appleir->key_up_timer);
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->flags |= APPLEIR_SUSPENDED;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	mutex_unlock(&appleir_mutex);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	return 0;
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static int appleir_resume(struct usb_interface *interface)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	struct appleir *appleir;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir = usb_get_intfdata(interface);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	mutex_lock(&appleir_mutex);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	if (appleir->flags & APPLEIR_OPENED) {
Jesse Keating 2f82dda
+		struct usb_endpoint_descriptor *endpoint;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+		endpoint = &interface->cur_altsetting->endpoint[0].desc;
Jesse Keating 2f82dda
+		usb_fill_int_urb(appleir->urb, appleir->usbdev,
Jesse Keating 2f82dda
+				 usb_rcvintpipe(appleir->usbdev, endpoint->bEndpointAddress),
Jesse Keating 2f82dda
+				 appleir->data, 8,
Jesse Keating 2f82dda
+				 appleir_urb, appleir, endpoint->bInterval);
Jesse Keating 2f82dda
+		appleir->urb->transfer_dma = appleir->dma_buf;
Jesse Keating 2f82dda
+		appleir->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+		init_timer(&appleir->key_up_timer);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+		appleir->key_up_timer.function = key_up_tick;
Jesse Keating 2f82dda
+		appleir->key_up_timer.data = (unsigned long)appleir;
Jesse Keating 2f82dda
+	}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	appleir->flags &= ~APPLEIR_SUSPENDED;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	mutex_unlock(&appleir_mutex);
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	return 0;
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static struct usb_driver appleir_driver = {
Jesse Keating 2f82dda
+	.name                 = "appleir",
Jesse Keating 2f82dda
+	.probe                = appleir_probe,
Jesse Keating 2f82dda
+	.disconnect           = appleir_disconnect,
Jesse Keating 2f82dda
+	.suspend              = appleir_suspend,
Jesse Keating 2f82dda
+	.resume               = appleir_resume,
Jesse Keating 2f82dda
+	.reset_resume         = appleir_resume,
Jesse Keating 2f82dda
+	.id_table             = appleir_ids,
Jesse Keating 2f82dda
+	.supports_autosuspend = 1,
Jesse Keating 2f82dda
+};
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static int __init appleir_init(void)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	int retval;
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+	retval = usb_register(&appleir_driver);
Jesse Keating 2f82dda
+	if (retval)
Jesse Keating 2f82dda
+		goto out;
Jesse Keating 2f82dda
+	printk(KERN_INFO DRIVER_VERSION ":" DRIVER_DESC);
Jesse Keating 2f82dda
+out:
Jesse Keating 2f82dda
+	return retval;
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+static void __exit appleir_exit(void)
Jesse Keating 2f82dda
+{
Jesse Keating 2f82dda
+	usb_deregister(&appleir_driver);
Jesse Keating 2f82dda
+}
Jesse Keating 2f82dda
+
Jesse Keating 2f82dda
+module_init(appleir_init);
Jesse Keating 2f82dda
+module_exit(appleir_exit);
Jesse Keating 2f82dda
-- 
Jesse Keating 2f82dda
1.6.5.2
Jesse Keating 2f82dda