Subject: [PATCH 27/32] rt2x00: Put Hardware button in generic header

From: Ivo van Doorn <[email-masked]>
Date: Fri, 28 Apr 2006 00:03:17 +0200

From: Ivo van Doorn <[email-masked]>

Put the hardware button handling as much as possible
in the new generic header for PCI.
The individial .c files should now only contain
the polling function to check the state of the hardware button.

Signed-off-by: Ivo van Doorn <[email-masked]>

diff -uprN wireless-dev-rt2x00/drivers/net/wireless/d80211/rt2x00/rt2400pci.c wireless-dev-rt2x00-patch/drivers/net/wireless/d80211/rt2x00/rt2400pci.c
--- wireless-dev-rt2x00/drivers/net/wireless/d80211/rt2x00/rt2400pci.c	2006-04-27 21:57:18.000000000 +0200
+++ wireless-dev-rt2x00-patch/drivers/net/wireless/d80211/rt2x00/rt2400pci.c	2006-04-27 22:03:48.000000000 +0200
@@ -38,10 +38,6 @@
 #include <net/iw_handler.h>
 #include <net/d80211.h>
 
-#ifdef CONFIG_RT2400PCI_BUTTON
-#include <acpi/acpi_bus.h>
-#endif /* CONFIG_RT2400PCI_BUTTON */
-
 #include <asm/io.h>
 
 /*
@@ -364,6 +360,24 @@ rt2x00_eeprom_multiread(
 		rt2x00_eeprom_read(rt2x00pci, word + counter, data++);
 }
 
+#ifdef CONFIG_RT2400PCI_BUTTON
+/*
+ * Hardware button poll handler.
+ */
+static void
+rt2400pci_button_poll(unsigned long data)
+{
+	struct rt2x00_pci	*rt2x00pci = (struct rt2x00_pci*)data;
+	u32			reg;
+
+	rt2x00_register_read(rt2x00pci, GPIOCSR, &reg);
+	rt2x00pci_button_status(
+		rt2x00pci, rt2x00_get_field32(reg, GPIOCSR_BIT0));
+}
+#else /* CONFIG_RT2400PCI_BUTTON */
+static void rt2400pci_button_poll(struct rt2x00_pci *rt2x00pci){}
+#endif /* CONFIG_RT2400PCI_BUTTON */
+
 /*
  * Configuration handlers.
  */
@@ -2258,24 +2272,10 @@ rt2400pci_initialize(struct pci_dev *pci
 	|| rt2400pci_init_hw(rt2x00pci))
 		goto exit_destroy_workqueue;
 
-#ifdef CONFIG_RT2400PCI_BUTTON
-	strcpy(acpi_device_class(&rt2x00pci->acpi_dev), DRV_NAME "_button");
-	strcpy(acpi_device_bid(&rt2x00pci->acpi_dev), DRV_NAME);
-	strcpy(acpi_device_name(&rt2x00pci->acpi_dev), DRV_NAME);
-
-	init_timer(&rt2x00pci->poll_timer);
-
-	if (rt2x00_poll_delay
-	&& rt2x00pci->button_status != BUTTON_STATUS_UNAVAILABLE) {
-		rt2x00pci->button_status = rt2400pci_poll(rt2x00pci);
-		rt2x00pci->poll_delay = rt2x00_poll_delay * (HZ / 10);
-		rt2x00pci->poll_timer.function = rt2400pci_poll_expire;
-		rt2x00pci->poll_timer.data = (unsigned long)rt2x00pci;
-		rt2x00pci->poll_timer.expires =
-			jiffies + rt2x00pci->poll_delay;
-		add_timer(&rt2x00pci->poll_timer);
-	};
-#endif /* CONFIG_RT2400PCI_BUTTON */
+	/*
+	 * If required start hardware button polling.
+	 */
+	rt2x00pci_button_start(rt2x00pci, rt2400pci_button_poll);
 
 	return 0;
 
@@ -2294,13 +2294,10 @@ rt2400pci_uninitialize(struct net_device
 {
 	struct rt2x00_pci	*rt2x00pci = ieee80211_dev_hw_data(net_dev);
 
-#ifdef CONFIG_RT2400PCI_BUTTON
 	/*
 	 * Shutdown poll_timer for hardware button.
 	 */
-	rt2x00pci->button_status = BUTTON_STATUS_UNAVAILABLE;
-	del_timer_sync(&rt2x00pci->poll_timer);
-#endif /* CONFIG_RT2400PCI_BUTTON */
+	rt2x00pci_button_stop(rt2x00pci);
 
 	kfree(rt2x00pci->eeprom);
 
@@ -2506,40 +2503,6 @@ rt2400pci_resume(struct pci_dev *pci_dev
 }
 #endif /* CONFIG_PM */
 
-#ifdef CONFIG_RT2400PCI_BUTTON
-static int
-rt2400pci_poll(struct rt2x00_pci *rt2x00pci)
-{
-	u32			reg;
-
-	rt2x00_register_read(rt2x00pci, GPIOCSR, &reg);
-	return rt2x00_get_field32(reg, GPIOCSR_BIT0);
-}
-
-static void
-rt2400pci_poll_expire(unsigned long data)
-{
-	struct rt2x00_pci	*rt2x00pci = (struct rt2x00_pci*)data;
-	u16			status;
-
-	status = rt2400pci_poll(rt2x00pci);
-
-	if (rt2x00pci->button_status != BUTTON_STATUS_UNAVAILABLE)
-		return;
-
-	if (status != rt2x00pci->button_status) {
-		rt2x00pci->button_status = status;
-		acpi_bus_generate_event(&rt2x00pci->acpi_dev,
-			ACPI_TYPE_EVENT, rt2x00pci->button_status);
-	}
-
-	rt2x00pci->poll_timer.expires = jiffies + rt2x00pci->poll_delay;
-
-	if (rt2x00pci->button_status != BUTTON_STATUS_UNAVAILABLE)
-		add_timer(&rt2x00pci->poll_timer);
-}
-#endif /* CONFIG_RT2400PCI_BUTTON */
-
 /*
  * RT2400pci module information.
  */
diff -uprN wireless-dev-rt2x00/drivers/net/wireless/d80211/rt2x00/rt2400pci.h wireless-dev-rt2x00-patch/drivers/net/wireless/d80211/rt2x00/rt2400pci.h
--- wireless-dev-rt2x00/drivers/net/wireless/d80211/rt2x00/rt2400pci.h	2006-04-27 21:57:18.000000000 +0200
+++ wireless-dev-rt2x00-patch/drivers/net/wireless/d80211/rt2x00/rt2400pci.h	2006-04-27 22:03:48.000000000 +0200
@@ -863,21 +863,4 @@ static int rt2400pci_beacon_update(struc
 	struct sk_buff *skb, struct ieee80211_tx_control *control);
 static int rt2400pci_tx_last_beacon(struct net_device *net_dev);
 
-/*
- * HW button support structures and variables.
- * The delay between each poll is set by the module parameter.
- */
-#ifdef CONFIG_RT2400PCI_BUTTON
-/*
- * Module parameter.
- */
-static int rt2x00_poll_delay = 100;
-
-/*
- * Declaration of several required functions.
- */
-static int rt2400pci_poll(struct rt2x00_pci *rt2x00pci);
-static void rt2400pci_poll_expire(unsigned long data);
-#endif /* CONFIG_RT2400PCI_BUTTON */
-
 #endif /* RT2400PCI_H */
diff -uprN wireless-dev-rt2x00/drivers/net/wireless/d80211/rt2x00/rt2500pci.c wireless-dev-rt2x00-patch/drivers/net/wireless/d80211/rt2x00/rt2500pci.c
--- wireless-dev-rt2x00/drivers/net/wireless/d80211/rt2x00/rt2500pci.c	2006-04-27 21:57:18.000000000 +0200
+++ wireless-dev-rt2x00-patch/drivers/net/wireless/d80211/rt2x00/rt2500pci.c	2006-04-27 22:03:48.000000000 +0200
@@ -38,10 +38,6 @@
 #include <net/iw_handler.h>
 #include <net/d80211.h>
 
-#ifdef CONFIG_RT2500PCI_BUTTON
-#include <acpi/acpi_bus.h>
-#endif /* CONFIG_RT2500PCI_BUTTON */
-
 #include <asm/io.h>
 
 /*
@@ -364,6 +360,24 @@ rt2x00_eeprom_multiread(
 		rt2x00_eeprom_read(rt2x00pci, word + counter, data++);
 }
 
+#ifdef CONFIG_RT2500PCI_BUTTON
+/*
+ * Hardware button poll handler.
+ */
+static void
+rt2500pci_button_poll(unsigned long data)
+{
+	struct rt2x00_pci	*rt2x00pci = (struct rt2x00_pci*)data;
+	u32			reg;
+
+	rt2x00_register_read(rt2x00pci, GPIOCSR, &reg);
+	rt2x00pci_button_status(
+		rt2x00pci, rt2x00_get_field32(reg, GPIOCSR_BIT0));
+}
+#else /* CONFIG_RT2500PCI_BUTTON */
+static void rt2500pci_button_poll(struct rt2x00_pci *rt2x00pci){}
+#endif /* CONFIG_RT2500PCI_BUTTON */
+
 /*
  * Configuration handlers.
  */
@@ -2547,24 +2561,10 @@ rt2500pci_initialize(struct pci_dev *pci
 	|| rt2500pci_init_hw(rt2x00pci))
 		goto exit_destroy_workqueue;
 
-#ifdef CONFIG_RT2500PCI_BUTTON
-	strcpy(acpi_device_class(&rt2x00pci->acpi_dev), DRV_NAME "_button");
-	strcpy(acpi_device_bid(&rt2x00pci->acpi_dev), DRV_NAME);
-	strcpy(acpi_device_name(&rt2x00pci->acpi_dev), DRV_NAME);
-
-	init_timer(&rt2x00pci->poll_timer);
-
-	if (rt2x00_poll_delay
-	&& rt2x00pci->button_status != BUTTON_STATUS_UNAVAILABLE) {
-		rt2x00pci->button_status = rt2500pci_poll(rt2x00pci);
-		rt2x00pci->poll_delay = rt2x00_poll_delay * (HZ / 10);
-		rt2x00pci->poll_timer.function = rt2500pci_poll_expire;
-		rt2x00pci->poll_timer.data = (unsigned long)rt2x00pci;
-		rt2x00pci->poll_timer.expires =
-			jiffies + rt2x00pci->poll_delay;
-		add_timer(&rt2x00pci->poll_timer);
-	};
-#endif /* CONFIG_RT2500PCI_BUTTON */
+	/*
+	 * If required start hardware button polling.
+	 */
+	rt2x00pci_button_start(rt2x00pci, rt2500pci_button_poll);
 
 	return 0;
 
@@ -2583,13 +2583,10 @@ rt2500pci_uninitialize(struct net_device
 {
 	struct rt2x00_pci	*rt2x00pci = ieee80211_dev_hw_data(net_dev);
 
-#ifdef CONFIG_RT2500PCI_BUTTON
 	/*
 	 * Shutdown poll_timer for hardware button.
 	 */
-	rt2x00pci->button_status = BUTTON_STATUS_UNAVAILABLE;
-	del_timer_sync(&rt2x00pci->poll_timer);
-#endif /* CONFIG_RT2500PCI_BUTTON */
+	rt2x00pci_button_stop(rt2x00pci);
 
 	kfree(rt2x00pci->eeprom);
 
@@ -2791,40 +2788,6 @@ rt2500pci_resume(struct pci_dev *pci_dev
 }
 #endif /* CONFIG_PM */
 
-#ifdef CONFIG_RT2500PCI_BUTTON
-static int
-rt2500pci_poll(struct rt2x00_pci *rt2x00pci)
-{
-	u32			reg;
-
-	rt2x00_register_read(rt2x00pci, GPIOCSR, &reg);
-	return rt2x00_get_field32(reg, GPIOCSR_BIT0);
-}
-
-static void
-rt2500pci_poll_expire(unsigned long data)
-{
-	struct rt2x00_pci	*rt2x00pci = (struct rt2x00_pci*)data;
-	u16			status;
-
-	status = rt2500pci_poll(rt2x00pci);
-
-	if (rt2x00pci->button_status != BUTTON_STATUS_UNAVAILABLE)
-		return;
-
-	if (status != rt2x00pci->button_status) {
-		rt2x00pci->button_status = status;
-		acpi_bus_generate_event(&rt2x00pci->acpi_dev,
-			ACPI_TYPE_EVENT, rt2x00pci->button_status);
-	}
-
-	rt2x00pci->poll_timer.expires = jiffies + rt2x00pci->poll_delay;
-
-	if (rt2x00pci->button_status != BUTTON_STATUS_UNAVAILABLE)
-		add_timer(&rt2x00pci->poll_timer);
-}
-#endif /* CONFIG_RT2500PCI_BUTTON */
-
 /*
  * RT2500pci module information.
  */
diff -uprN wireless-dev-rt2x00/drivers/net/wireless/d80211/rt2x00/rt2500pci.h wireless-dev-rt2x00-patch/drivers/net/wireless/d80211/rt2x00/rt2500pci.h
--- wireless-dev-rt2x00/drivers/net/wireless/d80211/rt2x00/rt2500pci.h	2006-04-27 21:57:18.000000000 +0200
+++ wireless-dev-rt2x00-patch/drivers/net/wireless/d80211/rt2x00/rt2500pci.h	2006-04-27 22:03:48.000000000 +0200
@@ -1132,21 +1132,4 @@ static int rt2500pci_beacon_update(struc
 	struct sk_buff *skb, struct ieee80211_tx_control *control);
 static int rt2500pci_tx_last_beacon(struct net_device *net_dev);
 
-/*
- * HW button support structures and variables.
- * The delay between each poll is set by the module parameter.
- */
-#ifdef CONFIG_RT2500PCI_BUTTON
-/*
- * Module parameter.
- */
-static int rt2x00_poll_delay = 100;
-
-/*
- * Declaration of several required functions.
- */
-static int rt2500pci_poll(struct rt2x00_pci *rt2x00pci);
-static void rt2500pci_poll_expire(unsigned long data);
-#endif /* CONFIG_RT2500PCI_BUTTON */
-
 #endif /* RT2500PCI_H */
diff -uprN wireless-dev-rt2x00/drivers/net/wireless/d80211/rt2x00/rt2x00pci.h wireless-dev-rt2x00-patch/drivers/net/wireless/d80211/rt2x00/rt2x00pci.h
--- wireless-dev-rt2x00/drivers/net/wireless/d80211/rt2x00/rt2x00pci.h	2006-04-27 22:00:04.000000000 +0200
+++ wireless-dev-rt2x00-patch/drivers/net/wireless/d80211/rt2x00/rt2x00pci.h	2006-04-27 22:03:48.000000000 +0200
@@ -45,6 +45,37 @@
 #define EEPROM_READ_OPCODE		0x06
 
 /*
+ * HW button structure.
+ */
+#ifdef CONFIG_RT2X00_BUTTON
+#include <acpi/acpi_bus.h>
+
+struct rt2x00_button{
+	/*
+	 * ACPI device for generation of ACPI events.
+	 */
+	struct acpi_device			acpi_dev;
+
+	/*
+	 * Timer for register polling.
+	 */
+	struct timer_list			poll_timer;
+
+	/*
+	 * Timer delay.
+	 */
+	short					poll_delay;
+
+	/*
+	 * Current status of button.
+	 */
+	short					button_status:1;
+	short					active_poll:1;
+	short					__pad:14;
+} __attribute__ ((packed));
+#endif /* CONFIG_RT2X00_BUTTON */
+
+/*
  * data_entry
  * The data ring is a list of data entries.
  * Each entry holds a reference to the descriptor
@@ -92,28 +123,7 @@ struct rt2x00_pci{
 	struct pci_dev				*pci_dev;
 
 #ifdef CONFIG_RT2X00_BUTTON
-	/*
-	 * ACPI device for generation of ACPI events.
-	 */
-	struct acpi_device			acpi_dev;
-
-	/*
-	 * Timer for register polling.
-	 */
-	struct timer_list			poll_timer;
-
-	/*
-	 * Timer delay.
-	 */
-	u16					poll_delay;
-
-	/*
-	 * Current status of button.
-	 */
-	u16					button_status;
-#define BUTTON_STATUS_UNAVAILABLE		2
-#define BUTTON_STATUS_ON			1
-#define BUTTON_STATUS_OFF			0
+	struct rt2x00_button			button;
 #endif /* CONFIG_RT2X00_BUTTON */
 
 	/*
@@ -257,4 +267,85 @@ rt2x00pci_get_ring(struct rt2x00_pci *rt
 	return NULL;
 }
 
+/*
+ * HW button variables & functions.
+ * The delay between each poll is set by the module parameter.
+ */
+#ifdef CONFIG_RT2X00_BUTTON
+/*
+ * Module parameter.
+ */
+static short rt2x00_poll_delay = 0;
+
+static inline void
+rt2x00pci_button_status(struct rt2x00_pci *rt2x00pci, char status)
+{
+	struct rt2x00_button	*button = &rt2x00pci->button;
+
+	if (!button->active_poll)
+		return;
+
+	if (status != button->button_status) {
+		button->button_status = status;
+		acpi_bus_generate_event(
+			&button->acpi_dev, ACPI_TYPE_EVENT, status);
+	}
+
+	button->poll_timer.expires = jiffies + button->poll_delay;
+
+	if (button->active_poll)
+		add_timer(&button->poll_timer);
+}
+
+static inline void
+rt2x00pci_button_start(struct rt2x00_pci *rt2x00pci,
+	void (*handler)(unsigned long data))
+{
+	struct rt2x00_button	*button = &rt2x00pci->button;
+
+	/*
+	 * Only enable polling when the user has
+	 * set the poll delay module parameter,
+	 * and the device contains a hardware button.
+	 */
+	if(!GET_FLAG(rt2x00pci, HARDWARE_BUTTON) || !rt2x00_poll_delay)
+		return;
+
+	strcpy(acpi_device_class(&button->acpi_dev), DRV_NAME "_button");
+	strcpy(acpi_device_bid(&button->acpi_dev), DRV_NAME);
+	strcpy(acpi_device_name(&button->acpi_dev), DRV_NAME);
+
+	init_timer(&button->poll_timer);
+
+	button->poll_delay = rt2x00_poll_delay * (HZ / 10);
+	button->button_status = 0;
+	button->active_poll = 1;
+
+	button->poll_timer.function = handler;
+	button->poll_timer.data = (unsigned long)rt2x00pci;
+	button->poll_timer.expires = jiffies + button->poll_delay;
+
+	add_timer(&button->poll_timer);
+}
+
+static inline void
+rt2x00pci_button_stop(struct rt2x00_pci *rt2x00pci)
+{
+	/*
+	 * Shutdown poll_timer for hardware button,
+	 * make sure only to disable polling when
+	 * it was enabled in the first place.
+	 */
+	if(!rt2x00pci->button.active_poll)
+		return;
+
+	rt2x00pci->button.active_poll = 0;
+	del_timer_sync(&rt2x00pci->button.poll_timer);
+}
+#else /* CONFIG_RT2X00_BUTTON */
+static inline void rt2x00pci_button_start(struct rt2x00_pci *rt2x00pci,
+	int (*handler)(struct rt2x00_pci *rt2x00pci)){}
+static inline void rt2x00pci_button_stop(struct rt2x00_pci *rt2x00pci){}
+#endif /* CONFIG_RT2X00_BUTTON */
+
 #endif /* RT2X00PCI_H */