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