All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Machek <pavel@ucw.cz>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Dmitry Torokhov <dtor@insightbb.com>, Andi Kleen <ak@suse.de>,
	Indan Zupancic <indan@nul.nu>,
	Stephen Hemminger <shemminger@linux-foundation.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org, bwalle@suse.de, rpurdie@rpsys.net
Subject: Re: blink driver power saving
Date: Thu, 5 Jul 2007 00:59:20 +0200	[thread overview]
Message-ID: <20070704225920.GA1687@elf.ucw.cz> (raw)
In-Reply-To: <alpine.LFD.0.98.0707041545490.9434@woody.linux-foundation.org>

Hi!

> > Actually here's one that does not immediately oops when I plug USB
> > keyboard in.
> 
> Why do you use a delayed workqueue and then always use it without a delay? 
> That seems silly.

I copied code from Dmitry :-); guess I copied too much. Here's updated
version:

Signed-off-by: Pavel Machek <pavel@suse.cz>

diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index 87d2046..716620c 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -95,6 +95,13 @@ config LEDS_COBALT
 	help
 	  This option enables support for the front LED on Cobalt Server
 
+config LEDS_INPUT
+	tristate "LED Support for input layer keyboards"
+	depends on LEDS_CLASS
+	help
+	  This option enables support for LEDs on keyboards handled by
+	  input layer.
+
 comment "LED Triggers"
 
 config LEDS_TRIGGERS
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index aa2c18e..ea58020 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -16,8 +16,10 @@ obj-$(CONFIG_LEDS_NET48XX)		+= leds-net4
 obj-$(CONFIG_LEDS_WRAP)			+= leds-wrap.o
 obj-$(CONFIG_LEDS_H1940)		+= leds-h1940.o
 obj-$(CONFIG_LEDS_COBALT)		+= leds-cobalt.o
+obj-$(CONFIG_LEDS_INPUT)		+= leds-input.o
 
 # LED Triggers
 obj-$(CONFIG_LEDS_TRIGGER_TIMER)	+= ledtrig-timer.o
 obj-$(CONFIG_LEDS_TRIGGER_IDE_DISK)	+= ledtrig-ide-disk.o
 obj-$(CONFIG_LEDS_TRIGGER_HEARTBEAT)	+= ledtrig-heartbeat.o
+
diff --git a/drivers/leds/leds-input.c b/drivers/leds/leds-input.c
new file mode 100644
index 0000000..8caca35
--- /dev/null
+++ b/drivers/leds/leds-input.c
@@ -0,0 +1,153 @@
+/*
+ * LED <-> input subsystem glue
+ *
+ * Copyright 2007 Pavel Machek <pavel@suse.cz>
+ * Copyright 2007 Dmitry Torokhov
+ * Copyright 2005-2006 Openedhand Ltd.
+ *
+ * Author: Pavel Machek <pavel@suse.cz>
+ * Based on code by: Richard Purdie <rpurdie@openedhand.com>
+ * 		     
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/platform_device.h>
+#include <linux/leds.h>
+
+#include <linux/module.h>
+#include <linux/input.h>
+#include <linux/slab.h>
+#include <linux/workqueue.h>
+#include <linux/init.h>
+
+struct blinker {
+	struct delayed_work work;
+	struct input_handle handle;
+	int state;
+
+	struct led_classdev dev;
+};
+
+struct blinker *blinker;
+
+static void inputled_set(struct led_classdev *led_cdev, enum led_brightness value)
+{
+	struct blinker *blinker = container_of(led_cdev, struct blinker, dev);
+	blinker->state = value;
+	schedule_delayed_work(&blinker->work, 0);
+}
+
+static void blink_task_handler(struct work_struct *work)
+{
+	struct blinker *blinker = container_of(work, struct blinker, work.work);
+	printk("Setting led to %d\n", blinker->state);
+	input_inject_event(&blinker->handle, EV_LED, LED_CAPSL, !!blinker->state);
+}
+
+static void blink_event(struct input_handle *handle, unsigned int type,
+		        unsigned int code, int down)
+{
+	/*
+	 * This is a very rare handler that does not process any input
+	 * events; just injects them.
+	 */
+}
+
+static int blink_connect(struct input_handler *handler, struct input_dev *dev,
+			  const struct input_device_id *id)
+{
+	struct input_handle *handle;
+	struct led_classdev *led_dev;
+	static int counter;
+	int error;
+
+	blinker = kzalloc(sizeof(struct blinker), GFP_KERNEL);
+	if (!blinker) {
+		return -ENOMEM;
+	}
+
+	INIT_DELAYED_WORK(&blinker->work, blink_task_handler);
+
+	led_dev = &blinker->dev;
+	led_dev->name = kmalloc(10, GFP_KERNEL);
+	sprintf(led_dev->name, "input%d", counter++);
+	led_dev->brightness_set = inputled_set;
+
+	handle = &blinker->handle;
+	handle->dev = dev;
+	handle->handler = handler;
+	handle->name = "blink";
+	handle->private = blinker;
+
+	error = input_register_handle(handle);
+	if (error)
+		goto err_free_handle;
+
+	error = input_open_device(handle);
+	if (error)
+		goto err_unregister_handle;
+
+	error = led_classdev_register(NULL, led_dev);
+	if (error < 0)
+		goto err_input_close_device;
+
+	return 0;
+
+ err_input_close_device:
+	input_close_device(handle);
+ err_unregister_handle:
+	input_unregister_handle(handle);
+ err_free_handle:
+	kfree(handle);
+	return error;
+}
+
+static void blink_disconnect(struct input_handle *handle)
+{
+	struct blinker *blinker = handle->private;
+
+	led_classdev_unregister(&blinker->dev);
+	cancel_rearming_delayed_work(&blinker->work);
+	input_close_device(handle);
+	input_unregister_handle(handle);
+	kfree(blinker);
+}
+
+static const struct input_device_id blink_ids[] = {
+	{
+		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_LEDBIT,
+		.evbit = { BIT(EV_LED) },
+		.ledbit = { [LONG(LED_CAPSL)] = BIT(LED_CAPSL) },
+	},
+	{ }
+};
+
+static struct input_handler blink_handler = {
+	.event		= blink_event,
+	.connect	= blink_connect,
+	.disconnect	= blink_disconnect,
+	.name		= "blink",
+	.id_table	= blink_ids,
+};
+
+static int __init blink_handler_init(void)
+{
+	return input_register_handler(&blink_handler);
+}
+
+static void __exit blink_handler_exit(void)
+{
+	input_unregister_handler(&blink_handler);
+	flush_scheduled_work();
+}
+
+module_init(blink_handler_init);
+module_exit(blink_handler_exit);
+
+
diff --git a/drivers/leds/leds-spitz.c b/drivers/leds/leds-spitz.c
index 126d09c..8d10274 100644
--- a/drivers/leds/leds-spitz.c
+++ b/drivers/leds/leds-spitz.c
@@ -1,5 +1,5 @@
 /*
- * LED Triggers Core
+ * SPITZ LED Driver
  *
  * Copyright 2005-2006 Openedhand Ltd.
  *


-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

  reply	other threads:[~2007-07-04 22:59 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-02 11:43 blink driver power saving Indan Zupancic
2007-07-02 11:51 ` Andi Kleen
2007-07-02 12:29   ` Indan Zupancic
2007-07-02 12:31   ` Dmitry Torokhov
2007-07-02 12:39     ` Andi Kleen
2007-07-02 12:56       ` Bernhard Walle
2007-07-02 13:42       ` Dmitry Torokhov
2007-07-02 13:43         ` Dmitry Torokhov
2007-07-02 20:19           ` Bernhard Walle
2007-07-04 21:47           ` Pavel Machek
2007-07-05  5:25             ` Dmitry Torokhov
2007-07-02 23:08       ` Pavel Machek
2007-07-03  5:42         ` Dmitry Torokhov
2007-07-04 21:40           ` Pavel Machek
2007-07-04 21:57           ` Pavel Machek
2007-07-04 22:11           ` Pavel Machek
2007-07-05  5:38             ` Dmitry Torokhov
2007-07-12  9:10               ` Pavel Machek
2007-07-13  0:42                 ` Jiri Kosina
2007-07-04 22:32           ` Pavel Machek
2007-07-04 22:46             ` Linus Torvalds
2007-07-04 22:59               ` Pavel Machek [this message]
     [not found]                 ` <alpine.LFD.0.98.0707041610530.9434@woody.linux-foundation.org>
2007-07-04 23:20                   ` Pavel Machek
2007-07-03  7:12         ` Bernhard Walle
2007-07-04 19:37           ` Pavel Machek
2007-07-05 20:30             ` Bill Davidsen
     [not found] <8CaWr-2o4-19@gated-at.bofh.it>
     [not found] ` <8Cfjl-PJ-33@gated-at.bofh.it>
     [not found]   ` <8CfW1-1TX-23@gated-at.bofh.it>
     [not found]     ` <8ChEo-4yy-1@gated-at.bofh.it>
2007-07-02 13:11       ` Bodo Eggert
  -- strict thread matches above, loose matches on Subject: below --
2007-07-01 16:50 Stephen Hemminger
2007-07-01 18:07 ` Linus Torvalds
2007-07-01 21:29   ` Andi Kleen
2007-07-01 22:14     ` Linus Torvalds
2007-07-01 23:59       ` Andi Kleen
2007-07-02 15:51         ` Linus Torvalds
2007-07-02 16:59           ` Alan Cox
2007-07-02 17:50         ` Stephen Hemminger
2007-07-02 19:03           ` Dmitry Torokhov
2007-07-02 19:08           ` Andi Kleen
2007-07-02 23:18             ` Pavel Machek
2007-07-01 21:26 ` Andi Kleen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070704225920.GA1687@elf.ucw.cz \
    --to=pavel@ucw.cz \
    --cc=ak@suse.de \
    --cc=akpm@linux-foundation.org \
    --cc=bwalle@suse.de \
    --cc=dtor@insightbb.com \
    --cc=indan@nul.nu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rpurdie@rpsys.net \
    --cc=shemminger@linux-foundation.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.