Skip to contentSkip to conversation

Input/Joystick Driver: add support AD7142 joystick driver

PATCH In review Bryan Wu 5 participants 10 messages Oct 15, 2007 – Oct 16, 2007

Subsystems (2)
lore
Share & tools
lore

Bryan Wu

author
#0 lore raw

PATCH 1/1 Input/Joystick Driver: add support AD7142 joystick driver

Subject: [PATCH try #3] Input/Joystick Driver: add support AD7142 joystick driver

[try #2] Changelog:
- Coding style issues fixed, passed checkpatch.pl
- Kill uselss "ad7142_used"
- Move request_irq to probe
- Move i2c_check_functionality to probe
- Error handling added

[try #3] Changelog:
- Use kzalloc
- Use strlcpy
- Use completion instead of wait queue
- Move input_allocate_device and input_register_device to ad7142_probe()
- Fix some issues pointed by LKML

Cc Andrey Panin <[email protected]>
Cc Roel Kluin <[email protected]>
Cc Ahmed S. Darwish <[email protected]>
Cc Dmitry Torokhov <[email protected]>
---
 drivers/input/joystick/Kconfig  |   16 ++
 drivers/input/joystick/Makefile |    1 +
 drivers/input/joystick/ad7142.c |  477 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 494 insertions(+), 0 deletions(-)
 create mode 100644 drivers/input/joystick/ad7142.c
drivers/input/joystick/Kconfig +16 -0
index 7c662ee..aeb7cc9 100644
@@ -282,4 +282,20 @@ config JOYSTICK_XPAD_LEDS
 	  This option enables support for the LED which surrounds the Big X on
 	  XBox 360 controller.
 
+config JOYSTICK_AD7142
+         tristate "Analog Devices AD7142 Joystick support"
+         depends on BFIN && I2C
+         help
+           Say Y here if you want to support an AD7142 joystick
+
+
+ config BFIN_JOYSTICK_IRQ_PFX
+         int "GPIO for Interrupt"
+         depends on (BFIN && JOYSTICK_AD7142)
+         range 33 120
+         default "55" if BFIN537_STAMP
+         default "39" if BFIN533_STAMP
+         help
+           Choose an GPIO as Keypad interrupt.[0..15]
+
 endif
drivers/input/joystick/Makefile +1 -0
index e855abb..8df388f 100644
@@ -5,6 +5,7 @@
 # Each configuration option enables a list of files.
 
 obj-$(CONFIG_JOYSTICK_A3D)		+= a3d.o
+obj-$(CONFIG_JOYSTICK_AD7142)		+= ad7142.o
 obj-$(CONFIG_JOYSTICK_ADI)		+= adi.o
 obj-$(CONFIG_JOYSTICK_AMIGA)		+= amijoy.o
 obj-$(CONFIG_JOYSTICK_ANALOG)		+= analog.o
drivers/input/joystick/ad7142.c new file +477 -0

477 lines not on this page · raw message

Signature · 1 line
1.5.3.4
maintainer
#1 lore raw

Hi Bryan,

drivers/input/joystick/ad7142.c line 398

Quoted 9 lines · Bryan Wu
drivers/input/joystick/ad7142.c new file +9 -0
+
+static int ad7142_thread(void *nothing)
+{
+       do {
+               wait_for_completion(&ad7142_completion);
+               ad7142_decode();
+               enable_irq(CONFIG_BFIN_JOYSTICK_IRQ_PFX);
+       } while (!kthread_should_stop());
+

No, this is not going to work well:
- you at least need to reinitialize the completion before enabling IRQ, otherwise you will spin in a very tight loop
- if noone would touch the joystick ad7142_clsoe would() block infinitely because noone would signal the completion and ad7142_thread() would never stop.

Completion is just not a good abstraction here... Please use work abstraction and possibly a separate workqueue.

drivers/input/joystick/ad7142.c line 453

Quoted 4 lines
drivers/input/joystick/ad7142.c new file +4 -0
+
+       ad7142_task = kthread_run(ad7142_thread, NULL, "ad7142_task");
+       if (IS_ERR(ad7142_task)) {
+               printk(KERN_ERR "serio: Failed to start kseriod\n");

kseriod?

drivers/input/joystick/ad7142.c line 460

Quoted 7 lines
drivers/input/joystick/ad7142.c new file +7 -0
+               return PTR_ERR(ad7142_task);
+       }
+       return 0;
+}
+
+static void ad7142_close(struct input_dev *dev)
+{

Don't you need to write something over i2c to tell the device to shut down? As it is now I expect the device to continue raising its IRQ until kernel decides that it is unhandled and should be ignored.

Quoted 1 line

+ free_irq(CONFIG_BFIN_JOYSTICK_IRQ_PFX, ad7142_interrupt);

Ok, so you freeing IRQ here, but it is allocated in ad7142_probe(). What happen if you try to open device after it was closed?

drivers/input/joystick/ad7142.c line 473

Quoted 12 lines
drivers/input/joystick/ad7142.c new file +12 -0
+       kthread_stop(ad7142_task);
+}
+
+static int __init ad7142_init(void)
+{
+       return i2c_add_driver(&ad7142_driver);
+}
+
+static void __exit ad7142_exit(void)
+{
+       i2c_del_driver(&ad7142_driver);
+       input_unregister_device(ad7142_dev);

input_unregister_device() should be in ad7142_detach_client? I am not sure i2c - there seems to be 2 interface styles and you probably need to use the new one. I am CC-inj Jean on this.

Quoted 7 lines
drivers/input/joystick/ad7142.c new file +4 -1
+}
+
+module_init(ad7142_init);
+module_exit(ad7142_exit);
--

1.5.3.4

Signature · 1 line
Dmitry
#2 lore raw
Quoted 20 lines · Dmitry Torokhov

Hi Bryan,

drivers/input/joystick/ad7142.c line 398

Quoted 9 lines · Bryan Wu
drivers/input/joystick/ad7142.c new file +9 -0
+
+static int ad7142_thread(void *nothing)
+{
+       do {
+               wait_for_completion(&ad7142_completion);
+               ad7142_decode();
+               enable_irq(CONFIG_BFIN_JOYSTICK_IRQ_PFX);
+       } while (!kthread_should_stop());
+

No, this is not going to work well:
- you at least need to reinitialize the completion before enabling IRQ, otherwise you will spin in a very tight loop
- if noone would touch the joystick ad7142_clsoe would() block infinitely because noone would signal the completion and ad7142_thread() would never stop.

Quoted 2 lines

Completion is just not a good abstraction here... Please use work abstraction and possibly a separate workqueue.

Yes, I agree with you now, although I have a little concern about the possibility of big delay introduced by workqueue.

Quoted 7 lines

drivers/input/joystick/ad7142.c line 453

Quoted 5 lines
drivers/input/joystick/ad7142.c new file +4 -0
+
+       ad7142_task = kthread_run(ad7142_thread, NULL, "ad7142_task");
+       if (IS_ERR(ad7142_task)) {
+               printk(KERN_ERR "serio: Failed to start kseriod\n");

kseriod?

My fault, I did't notice this copy words from other driver.

Quoted 13 lines

drivers/input/joystick/ad7142.c line 460

Quoted 8 lines
drivers/input/joystick/ad7142.c new file +7 -0
+               return PTR_ERR(ad7142_task);
+       }
+       return 0;
+}
+
+static void ad7142_close(struct input_dev *dev)
+{

Don't you need to write something over i2c to tell the device to shut down? As it is now I expect the device to continue raising its IRQ until kernel decides that it is unhandled and should be ignored.

I am not sure here, should do some investigate of the hardware datasheet.

Quoted 5 lines
Quoted 1 line

+ free_irq(CONFIG_BFIN_JOYSTICK_IRQ_PFX, ad7142_interrupt);

Ok, so you freeing IRQ here, but it is allocated in ad7142_probe(). What happen if you try to open device after it was closed?

Yes, it should be move to ad7142_detach_client()

Quoted 8 lines
Quoted 8 lines
drivers/input/joystick/ad7142.c new file +8 -0
+       kthread_stop(ad7142_task);
+}
+
+static int __init ad7142_init(void)
+{
+       return i2c_add_driver(&ad7142_driver);
+}
+
Quoted 9 lines

drivers/input/joystick/ad7142.c line 473

Quoted 4 lines
drivers/input/joystick/ad7142.c new file +4 -0
+static void __exit ad7142_exit(void)
+{
+       i2c_del_driver(&ad7142_driver);
+       input_unregister_device(ad7142_dev);

input_unregister_device() should be in ad7142_detach_client? I am not sure i2c - there seems to be 2 interface styles and you probably need to use the new one. I am CC-inj Jean on this.

Yes, no need input_unregister_device() here.

Thanks a lot for you kindly review.
I will resend update patch later.

Best Regards,
-Bryan Wu

maintainer
#3 lore raw
Quoted 8 lines · Bryan Wu
Quoted 3 lines · Dmitry Torokhov

Completion is just not a good abstraction here... Please use work abstraction and possibly a separate workqueue.

Yes, I agree with you now, although I have a little concern about the possibility of big delay introduced by workqueue.

Having a separate workqueue should isolate the driver from users hogging keventd. Otherwise the speed should be pretty much the same as with a kthread.

Quoted 3 lines

Thanks a lot for you kindly review.
I will resend update patch later.

Thank you for not getting frustrated with all my change requests. Btw, blackfin keypad driver is in my tree and should be in mainline once Linus does the pull I requested.

Signature · 1 line
Dmitry
#4 lore raw
Quoted 6 lines · Dmitry Torokhov
Quoted 5 lines · Bryan Wu
Quoted 3 lines · Dmitry Torokhov

Completion is just not a good abstraction here... Please use work abstraction and possibly a separate workqueue.

Quoted 8 lines
Quoted 3 lines

Yes, I agree with you now, although I have a little concern about the possibility of big delay introduced by workqueue.

Having a separate workqueue should isolate the driver from users hogging keventd. Otherwise the speed should be pretty much the same as with a kthread.

Does this driver need the create a new kthread instead of keventd? I think keventd might be sufficient for this driver.

Quoted 5 lines
Quoted 3 lines

Thanks a lot for you kindly review.
I will resend update patch later.

Thank you for not getting frustrated with all my change requests.

Oh, your help is very useful. It encourages us to send out our drivers to LKML.

Quoted 4 lines

Btw,
blackfin keypad driver is in my tree and should be in mainline once Linus does the pull I requested.

Thanks again, I noticed it was merged already.

-Bryan Wu

maintainer
#5 lore raw
Quoted 11 lines · Bryan Wu
Quoted 10 lines · Dmitry Torokhov
Quoted 8 lines · Bryan Wu
Quoted 3 lines · Dmitry Torokhov

Completion is just not a good abstraction here... Please use work abstraction and possibly a separate workqueue.

Yes, I agree with you now, although I have a little concern about the possibility of big delay introduced by workqueue.

Quoted 8 lines
Quoted 4 lines

Having a separate workqueue should isolate the driver from users hogging keventd. Otherwise the speed should be pretty much the same as with a kthread.

Does this driver need the create a new kthread instead of keventd? I think keventd might be sufficient for this driver.

No it does not have to start a new workqueue. I'd start with keventd and only implement a separate workqueue later if I saw the driver being starved by other keventd users.

Signature · 1 line
Dmitry
#6 lore raw
In reply to Dmitry Torokhov
Quoted 20 lines · Dmitry Torokhov

Hi Bryan,

drivers/input/joystick/ad7142.c line 398

Quoted 9 lines · Bryan Wu
drivers/input/joystick/ad7142.c new file +9 -0
+
+static int ad7142_thread(void *nothing)
+{
+       do {
+               wait_for_completion(&ad7142_completion);
+               ad7142_decode();
+               enable_irq(CONFIG_BFIN_JOYSTICK_IRQ_PFX);
+       } while (!kthread_should_stop());
+

No, this is not going to work well:
- you at least need to reinitialize the completion before enabling IRQ, otherwise you will spin in a very tight loop
- if noone would touch the joystick ad7142_clsoe would() block infinitely because noone would signal the completion and ad7142_thread() would never stop.

Quoted 3 lines

Completion is just not a good abstraction here... Please use work abstraction and possibly a separate workqueue.

Bryan, I'm very interested in the technical advantage of using a completion here.

In my _not-experienced_ opinion, I remember completions was created mainly for "create_task, wait till task got finished, go on" case. Why using it in a different context while workqueues was created for a similar situation to ad7142 one (non-irq context bottom-half) ?

Regards,

Signature · 3 lines
Ahmed S. Darwish
HomePage: http://darwish.07.googlepages.com
Blog: http://darwish-07.blogspot.com
#7 lore raw
Quoted 21 lines · Ahmed S. Darwish
Quoted 20 lines · Dmitry Torokhov

Hi Bryan,

drivers/input/joystick/ad7142.c line 398

Quoted 9 lines · Bryan Wu
drivers/input/joystick/ad7142.c new file +9 -0
+
+static int ad7142_thread(void *nothing)
+{
+       do {
+               wait_for_completion(&ad7142_completion);
+               ad7142_decode();
+               enable_irq(CONFIG_BFIN_JOYSTICK_IRQ_PFX);
+       } while (!kthread_should_stop());
+

No, this is not going to work well:
- you at least need to reinitialize the completion before enabling IRQ, otherwise you will spin in a very tight loop
- if noone would touch the joystick ad7142_clsoe would() block infinitely because noone would signal the completion and ad7142_thread() would never stop.

Quoted 7 lines
Quoted 3 lines

Completion is just not a good abstraction here... Please use work abstraction and possibly a separate workqueue.

Bryan, I'm very interested in the technical advantage of using a completion here.

You are welcome, I'd like to discuss these things here.

Quoted 4 lines

In my _not-experienced_ opinion, I remember completions was created mainly for "create_task, wait till task got finished, go on" case. Why using it in a different context while workqueues was created for a similar situation to ad7142 one (non-irq context bottom-half) ?

I like completion because it is simple to use and understand. Your understanding is right. But there is no limit for using different context with completion. completion is a wrapper of waitqueue+done flag. For some drivers, in process context call wait_for_completetion(), then schedule out and in irq handler call complete(). This is very simple and helpful for driver design (For example, you call dma function to transfer data, then you schedule out and then DMA IRQ handler will call complete() to wakeup you).

But in this driver, a) can not call ad7142_decode() in IRQ handler, because it will sleep in IRQ context by calling some i2c API, b) in original design, creating a new kthread and using some waitqueue API is the same way as using workqueue, c) cannot use completion as Dmitry said.

I am going to use workqueue here.

Any idea?

Thanks
-Bryan Wu

#8 lore raw
Quoted 36 lines · Bryan Wu
Quoted 21 lines · Ahmed S. Darwish
Quoted 20 lines · Dmitry Torokhov

Hi Bryan,

drivers/input/joystick/ad7142.c line 398

Quoted 9 lines · Bryan Wu
drivers/input/joystick/ad7142.c new file +9 -0
+
+static int ad7142_thread(void *nothing)
+{
+       do {
+               wait_for_completion(&ad7142_completion);
+               ad7142_decode();
+               enable_irq(CONFIG_BFIN_JOYSTICK_IRQ_PFX);
+       } while (!kthread_should_stop());
+

No, this is not going to work well:
- you at least need to reinitialize the completion before enabling IRQ, otherwise you will spin in a very tight loop
- if noone would touch the joystick ad7142_clsoe would() block infinitely because noone would signal the completion and ad7142_thread() would never stop.

Quoted 7 lines
Quoted 3 lines

Completion is just not a good abstraction here... Please use work abstraction and possibly a separate workqueue.

Bryan, I'm very interested in the technical advantage of using a completion here.

You are welcome, I'd like to discuss these things here.

Quoted 4 lines

In my _not-experienced_ opinion, I remember completions was created mainly for "create_task, wait till task got finished, go on" case. Why using it in a different context while workqueues was created for a similar situation to ad7142 one (non-irq context bottom-half) ?

Quoted 9 lines

I like completion because it is simple to use and understand. Your understanding is right. But there is no limit for using different context with completion. completion is a wrapper of waitqueue+done flag. For some drivers, in process context call wait_for_completetion(), then schedule out and in irq handler call complete(). This is very simple and helpful for driver design (For example, you call dma function to transfer data, then you schedule out and then DMA IRQ handler will call complete() to wakeup you).

Thank you for such a useful information.

Quoted 10 lines

But in this driver, a) can not call ad7142_decode() in IRQ handler, because it will sleep in IRQ context by calling some i2c API, b) in original design, creating a new kthread and using some waitqueue API is the same way as using workqueue, c) cannot use completion as Dmitry said.

I am going to use workqueue here.

Any idea?

I have no better thoughts than the ones provided by Dmitry actually.

Quoted 2 lines

Thanks
-Bryan Wu

Regards :),

Signature · 3 lines
Ahmed S. Darwish
HomePage: http://darwish.07.googlepages.com
Blog: http://darwish-07.blogspot.com
#9 lore raw
In reply to Dmitry Torokhov
Quoted 14 lines · Dmitry Torokhov

drivers/input/joystick/ad7142.c line 473

Quoted 9 lines · Bryan Wu
drivers/input/joystick/ad7142.c new file +9 -0
+static int __init ad7142_init(void)
+{
+       return i2c_add_driver(&ad7142_driver);
+}
+
+static void __exit ad7142_exit(void)
+{
+       i2c_del_driver(&ad7142_driver);
+       input_unregister_device(ad7142_dev);

input_unregister_device() should be in ad7142_detach_client? I am not sure i2c - there seems to be 2 interface styles and you probably need to use the new one. I am CC-inj Jean on this.

The new style is preferred, yes.

Signature · 1 line
Jean Delvare