Skip to contentSkip to conversation

Samsung backlight driver

PATCH In review Greg KH 2 participants 7 messages Aug 14, 2009 – Aug 16, 2009 x86 platform drivers

lore
Share & tools
lore

Greg KH

author
#0 lore raw

PATCH 1/1 Samsung backlight driver

From: Greg Kroah-Hartman <[email protected]>

This driver implements backlight controls for Samsung laptops that currently do not have ACPI support for this control.

It has been tested on the N130 laptop and properly works there.

---
 drivers/platform/x86/Kconfig             |   12 ++
 drivers/platform/x86/Makefile            |    1 
 drivers/platform/x86/samsung-backlight.c |  146 +++++++++++++++++++++++++++++++
 3 files changed, 159 insertions(+)
drivers/platform/x86/Kconfig +12 -0
@@ -425,4 +425,16 @@ config ACPI_TOSHIBA
 
 	  If you have a legacy free Toshiba laptop (such as the Libretto L1
 	  series), say Y.
+
+config SAMSUNG_BACKLIGHT
+	tristate "Samsung Backlight driver"
+	depends on BACKLIGHT_CLASS_DEVICE
+	depends on DMI
+	---help---
+	  This driver adds support to control the backlight on a number of
+	  Samsung laptops, like the N130.
+
+	  It will only be loaded on laptops that properly need it, so it is
+	  safe to say Y here.
+
 endif # X86_PLATFORM_DEVICES
drivers/platform/x86/Makefile +1 -0
@@ -20,3 +20,4 @@ obj-$(CONFIG_INTEL_MENLOW)	+= intel_menl
 obj-$(CONFIG_ACPI_WMI)		+= wmi.o
 obj-$(CONFIG_ACPI_ASUS)		+= asus_acpi.o
 obj-$(CONFIG_ACPI_TOSHIBA)	+= toshiba_acpi.o
+obj-$(CONFIG_SAMSUNG_BACKLIGHT)	+= samsung-backlight.o
drivers/platform/x86/samsung-backlight.c new file +146 -0

146 lines not on this page · raw message

author
#1 lore raw

PATCH 1/1 samsung-backlight: Added support for Samsung NC10 laptop

Info was provided by Soeren Sonnenburg <[email protected]>

drivers/platform/x86/samsung-backlight.c +10 -0
@@ -119,6 +119,15 @@ static struct dmi_system_id __initdata samsung_dmi_table[] = {
 		},
 		.callback = dmi_check_cb,
 	},
+	{
+		.ident = "NC10",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
+			DMI_MATCH(DMI_PRODUCT_NAME, "NC10"),
+			DMI_MATCH(DMI_BOARD_NAME, "NC10"),
+		},
+		.callback = dmi_check_cb,
+	},
 	{ },
 };
 
@@ -144,3 +153,4 @@ MODULE_LICENSE("GPL");
 module_param(offset, int, S_IRUGO | S_IWUSR);
 MODULE_PARM_DESC(offset, "The offset into the PCI device for the brightness control");
 MODULE_ALIAS("dmi:*:svnSAMSUNGELECTRONICSCO.,LTD.:pnN130:*:rnN130:*");
+MODULE_ALIAS("dmi:*:svnSAMSUNGELECTRONICSCO.,LTD.:pnNC10:*:rnNC10:*");
#2 lore raw
In reply to Greg KH

Hi Greg,

drivers/platform/x86/samsung-backlight.c line 52

Quoted 6 lines · Greg KH
drivers/platform/x86/samsung-backlight.c new file +6 -0
+
+static int update_status(struct backlight_device *bd)
+{
+	if (!pci_device)
+		return -ENODEV;
+	if (!backlight_device)

How is this possible?

Quoted 10 lines
drivers/platform/x86/samsung-backlight.c new file +10 -0
+		return -ENODEV;
+
+	current_brightness = bd->props.brightness;
+	set_brightness();
+	return 0;
+}
+
+static struct backlight_ops backlight_ops = {
+	.get_brightness	= get_brightness,
+	.update_status	= update_status,

drivers/platform/x86/samsung-backlight.c line 72

Quoted 10 lines
drivers/platform/x86/samsung-backlight.c new file +10 -0
+};
+
+static int find_video_card(void)
+{
+	struct pci_dev *dev = NULL;
+
+	while ((dev = pci_get_device(0x8086, 0x27ae, dev)) != NULL) {
+		/* Found one, so let's save it off */
+		if (!pci_device)
+			pci_device = pci_dev_get(dev);

If you need to iterate all matching devices you need to save them all or release references to "extras" right here... Or, better yet, just break after you find the first one.

drivers/platform/x86/samsung-backlight.c line 57

Quoted 4 lines
drivers/platform/x86/samsung-backlight.c new file +4 -0
+	}
+
+	if (!pci_device)
+		return 0;

Why not -ENODEV? What is the point of having this driver loaded if the device is not available? Then you could drop all silly "if (!pci_device)" checks.

Signature · 1 line
Dmitry
author
#3 lore raw
Quoted 11 lines · Dmitry Torokhov

Hi Greg,

drivers/platform/x86/samsung-backlight.c line 52

Quoted 6 lines · Greg KH
drivers/platform/x86/samsung-backlight.c new file +6 -0
+
+static int update_status(struct backlight_device *bd)
+{
+	if (!pci_device)
+		return -ENODEV;
+	if (!backlight_device)

How is this possible?

It's probably not, I was just being paranoid. You're right, I can drop all of these checks and rework the find logic a bit.

Quoted 16 lines
Quoted 16 lines
drivers/platform/x86/samsung-backlight.c new file +16 -0
+		return -ENODEV;
+
+	current_brightness = bd->props.brightness;
+	set_brightness();
+	return 0;
+}
+
+static struct backlight_ops backlight_ops = {
+	.get_brightness	= get_brightness,
+	.update_status	= update_status,
+};
+
+static int find_video_card(void)
+{
+	struct pci_dev *dev = NULL;
+
Quoted 8 lines

drivers/platform/x86/samsung-backlight.c line 72

Quoted 4 lines
drivers/platform/x86/samsung-backlight.c new file +4 -0
+	while ((dev = pci_get_device(0x8086, 0x27ae, dev)) != NULL) {
+		/* Found one, so let's save it off */
+		if (!pci_device)
+			pci_device = pci_dev_get(dev);

If you need to iterate all matching devices you need to save them all or release references to "extras" right here... Or, better yet, just break after you find the first one.

It's a trivially small loop, this way I get the "hm, do I need to drop the reference or not" logic correct :)

Quoted 8 lines

drivers/platform/x86/samsung-backlight.c line 57

Quoted 4 lines
drivers/platform/x86/samsung-backlight.c new file +4 -0
+	}
+
+	if (!pci_device)
+		return 0;

Why not -ENODEV? What is the point of having this driver loaded if the device is not available? Then you could drop all silly "if (!pci_device)" checks.

Ah, you are right, thanks, I'll go fix that.

thanks for the review, I appreciate it.

greg k-h

author
#4 lore raw
In reply to Dmitry Torokhov

Here's an updated version, with two more laptop models supported, and the comments from Dmitry added in.

thanks,

greg k-h

----------------

From: Greg Kroah-Hartman <[email protected]>

This driver implements backlight controls for Samsung laptops that currently do not have ACPI support for this control.

It has been tested on the N130 laptop and properly works there.

Info for the NC10 was provided by Soeren Sonnenburg <[email protected]> Info for the NP-Q45 from Jérémie Huchet <[email protected]>

Cc Soeren Sonnenburg <[email protected]>
Cc Jérémie Huchet <[email protected]>
---
 drivers/platform/x86/Kconfig             |   12 ++
 drivers/platform/x86/Makefile            |    1 
 drivers/platform/x86/samsung-backlight.c |  168 +++++++++++++++++++++++++++++++
 3 files changed, 181 insertions(+)
drivers/platform/x86/Kconfig +12 -0
@@ -425,4 +425,16 @@ config ACPI_TOSHIBA
 
 	  If you have a legacy free Toshiba laptop (such as the Libretto L1
 	  series), say Y.
+
+config SAMSUNG_BACKLIGHT
+	tristate "Samsung Backlight driver"
+	depends on BACKLIGHT_CLASS_DEVICE
+	depends on DMI
+	---help---
+	  This driver adds support to control the backlight on a number of
+	  Samsung laptops, like the N130.
+
+	  It will only be loaded on laptops that properly need it, so it is
+	  safe to say Y here.
+
 endif # X86_PLATFORM_DEVICES
drivers/platform/x86/Makefile +1 -0
@@ -20,3 +20,4 @@ obj-$(CONFIG_INTEL_MENLOW)	+= intel_menl
 obj-$(CONFIG_ACPI_WMI)		+= wmi.o
 obj-$(CONFIG_ACPI_ASUS)		+= asus_acpi.o
 obj-$(CONFIG_ACPI_TOSHIBA)	+= toshiba_acpi.o
+obj-$(CONFIG_SAMSUNG_BACKLIGHT)	+= samsung-backlight.o
drivers/platform/x86/samsung-backlight.c new file +168 -0

168 lines not on this page · raw message

#5 lore raw
Quoted 3 lines · Greg KH

Here's an updated version, with two more laptop models supported, and the comments from Dmitry added in.

You are leaking pci device reference in error unwinding path....

Here you go, just fold it together with yours.

Signature · 11 lines
Dmitry

Fix PCI device reference leak in error handling path; move more code
into __init/__exit sections; simplify searching for video card and
use less globals.

Signed-off-by: Dmitry Torokhov <[email protected]>
---

 drivers/platform/x86/samsung-backlight.c |  100 +++++++++++-------------------
 1 files changed, 36 insertions(+), 64 deletions(-)
drivers/platform/x86/samsung-backlight.c +36 -64
index accc20e..8df5fc5 100644
@@ -21,23 +21,24 @@
 #define MAX_BRIGHT	0xff
 #define OFFSET		0xf4
 
+static int offset = OFFSET;
+module_param(offset, int, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(offset, "The offset into the PCI device for the brightness control");
+
 static struct pci_dev *pci_device;
 static struct backlight_device *backlight_device;
-static int offset = OFFSET;
-static u8 current_brightness;
 
-static void read_brightness(void)
+static u8 read_brightness(void)
 {
-	if (!pci_device)
-		return;
-	pci_read_config_byte(pci_device, offset, &current_brightness);
+	u8 brightness;
+
+	pci_read_config_byte(pci_device, offset, &brightness);
+	return brightness;
 }
 
-static void set_brightness(void)
+static void set_brightness(u8 brightness)
 {
-	if (!pci_device)
-		return;
-	pci_write_config_byte(pci_device, offset, current_brightness);
+	pci_write_config_byte(pci_device, offset, brightness);
 }
 
 static int get_brightness(struct backlight_device *bd)
@@ -47,11 +48,7 @@ static int get_brightness(struct backlight_device *bd)
 
 static int update_status(struct backlight_device *bd)
 {
-	if (!pci_device)
-		return -ENODEV;
-
-	current_brightness = bd->props.brightness;
-	set_brightness();
+	set_brightness(bd->props.brightness);
 	return 0;
 }
 
@@ -60,51 +57,7 @@ static struct backlight_ops backlight_ops = {
 	.update_status	= update_status,
 };
 
-static int find_video_card(void)
-{
-	struct pci_dev *dev = NULL;
-
-	while ((dev = pci_get_device(0x8086, 0x27ae, dev)) != NULL) {
-		/*
-		 * Found one, so let's save it off and break
-		 * Note that the reference is still raised on
-		 * the PCI device here.
-		 */
-		pci_device = dev;
-		break;
-	}
-
-	if (!pci_device)
-		return -ENODEV;
-
-	/* create a backlight device to talk to this one */
-	backlight_device = backlight_device_register("samsung",
-						     &pci_device->dev,
-						     NULL, &backlight_ops);
-	if (IS_ERR(backlight_device))
-		return PTR_ERR(backlight_device);
-	read_brightness();
-	backlight_device->props.max_brightness = MAX_BRIGHT;
-	backlight_device->props.brightness = current_brightness;
-	backlight_device->props.power = FB_BLANK_UNBLANK;
-	backlight_update_status(backlight_device);
-	return 0;
-}
-
-static void remove_video_card(void)
-{
-	if (!pci_device)
-		return;
-
-	backlight_device_unregister(backlight_device);
-	backlight_device = NULL;
-
-	/* we are done with the PCI device, put it back */
-	pci_dev_put(pci_device);
-	pci_device = NULL;
-}
-
-static int dmi_check_cb(const struct dmi_system_id *id)
+static int __init dmi_check_cb(const struct dmi_system_id *id)
 {
 	printk(KERN_INFO KBUILD_MODNAME ": found laptop model '%s'\n",
 		id->ident);
@@ -147,12 +100,33 @@ static int __init samsung_init(void)
 	if (!dmi_check_system(samsung_dmi_table))
 		return -ENODEV;
 
-	return find_video_card();
+	pci_device = pci_get_device(0x8086, 0x27ae, NULL);
+	if (!pci_device)
+		return -ENODEV;
+
+	/* create a backlight device to talk to this one */
+	backlight_device = backlight_device_register("samsung",
+						     &pci_device->dev,
+						     NULL, &backlight_ops);
+	if (IS_ERR(backlight_device)) {
+		pci_dev_put(pci_device);
+		return PTR_ERR(backlight_device);
+	}
+
+	backlight_device->props.max_brightness = MAX_BRIGHT;
+	backlight_device->props.brightness = read_brightness();
+	backlight_device->props.power = FB_BLANK_UNBLANK;
+	backlight_update_status(backlight_device);
+
+	return 0;
 }
 
 static void __exit samsung_exit(void)
 {
-	remove_video_card();
+	backlight_device_unregister(backlight_device);
+
+	/* we are done with the PCI device, put it back */
+	pci_dev_put(pci_device);
 }
 
 module_init(samsung_init);
@@ -161,8 +135,6 @@ module_exit(samsung_exit);
 MODULE_AUTHOR("Greg Kroah-Hartman <[email protected]>");
 MODULE_DESCRIPTION("Samsung Backlight driver");
 MODULE_LICENSE("GPL");
-module_param(offset, int, S_IRUGO | S_IWUSR);
-MODULE_PARM_DESC(offset, "The offset into the PCI device for the brightness control");
 MODULE_ALIAS("dmi:*:svnSAMSUNGELECTRONICSCO.,LTD.:pnN130:*:rnN130:*");
 MODULE_ALIAS("dmi:*:svnSAMSUNGELECTRONICSCO.,LTD.:pnNC10:*:rnNC10:*");
 MODULE_ALIAS("dmi:*:svnSAMSUNGELECTRONICSCO.,LTD.:pnSQ45S70S:*:rnSQ45S70S:*");
author
#6 lore raw
Quoted 8 lines · Dmitry Torokhov
Quoted 3 lines · Greg KH

Here's an updated version, with two more laptop models supported, and the comments from Dmitry added in.

You are leaking pci device reference in error unwinding path....

Here you go, just fold it together with yours.

Thank you very much for the patch, I appreciate it.

Jérémie, I've now added the pci id for your device, so the driver should now work for it as well. Let me know if you have any problems with it.

I've included an updated version below that also adds support for the N120 laptop series.

thanks,

greg k-h

-----------------------

From: Greg Kroah-Hartman <[email protected]>
Subject: Samsung backlight driver

This driver implements backlight controls for Samsung laptops that currently do not have ACPI support for this control.

It has been tested on the N130 laptop and properly works there.

Info for the NC10 was provided by Soeren Sonnenburg <[email protected]> Info for the NP-Q45 from Jérémie Huchet <[email protected]>

Many thanks to Dmitry Torokhov <[email protected]> for cleanups and other suggestions on how to make the driver simpler.

Cc Soeren Sonnenburg <[email protected]>
Cc Jérémie Huchet <[email protected]>
Cc Dmitry Torokhov <[email protected]>
---
 drivers/platform/x86/Kconfig             |   12 ++
 drivers/platform/x86/Makefile            |    1 
 drivers/platform/x86/samsung-backlight.c |  157 +++++++++++++++++++++++++++++++
 3 files changed, 170 insertions(+)
drivers/platform/x86/Kconfig +12 -0
@@ -425,4 +425,16 @@ config ACPI_TOSHIBA
 
 	  If you have a legacy free Toshiba laptop (such as the Libretto L1
 	  series), say Y.
+
+config SAMSUNG_BACKLIGHT
+	tristate "Samsung Backlight driver"
+	depends on BACKLIGHT_CLASS_DEVICE
+	depends on DMI
+	---help---
+	  This driver adds support to control the backlight on a number of
+	  Samsung laptops, like the N130.
+
+	  It will only be loaded on laptops that properly need it, so it is
+	  safe to say Y here.
+
 endif # X86_PLATFORM_DEVICES
drivers/platform/x86/Makefile +1 -0
@@ -20,3 +20,4 @@ obj-$(CONFIG_INTEL_MENLOW)	+= intel_menl
 obj-$(CONFIG_ACPI_WMI)		+= wmi.o
 obj-$(CONFIG_ACPI_ASUS)		+= asus_acpi.o
 obj-$(CONFIG_ACPI_TOSHIBA)	+= toshiba_acpi.o
+obj-$(CONFIG_SAMSUNG_BACKLIGHT)	+= samsung-backlight.o
drivers/platform/x86/samsung-backlight.c new file +157 -0

157 lines not on this page · raw message