2012-08-10 19:58:34

by Hans de Goede

[permalink] [raw]
Subject: [PATCH 1/2] radio-shark: Only compile led support when CONFIG_LED_CLASS is set

Reported-by: Dadiv Rientjes <[email protected]>
Signed-off-by: Hans de Goede <[email protected]>
---
drivers/media/radio/radio-shark.c | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/drivers/media/radio/radio-shark.c b/drivers/media/radio/radio-shark.c
index c2ead23..f746ed0 100644
--- a/drivers/media/radio/radio-shark.c
+++ b/drivers/media/radio/radio-shark.c
@@ -27,7 +27,6 @@

#include <linux/init.h>
#include <linux/kernel.h>
-#include <linux/leds.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb.h>
@@ -35,6 +34,12 @@
#include <media/v4l2-device.h>
#include <sound/tea575x-tuner.h>

+#if defined(CONFIG_LEDS_CLASS) || \
+ (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK_MODULE))
+#include <linux/leds.h>
+#define SHARK_USE_LEDS 1
+#endif
+
/*
* Version Information
*/
@@ -54,6 +59,7 @@ MODULE_LICENSE("GPL");

#define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev)

+#ifdef SHARK_USE_LEDS
enum { BLUE_LED, BLUE_PULSE_LED, RED_LED, NO_LEDS };

static void shark_led_set_blue(struct led_classdev *led_cdev,
@@ -83,17 +89,20 @@ static const struct led_classdev shark_led_templates[NO_LEDS] = {
.brightness_set = shark_led_set_red,
},
};
+#endif

struct shark_device {
struct usb_device *usbdev;
struct v4l2_device v4l2_dev;
struct snd_tea575x tea;

+#ifdef SHARK_USE_LEDS
struct work_struct led_work;
struct led_classdev leds[NO_LEDS];
char led_names[NO_LEDS][32];
atomic_t brightness[NO_LEDS];
unsigned long brightness_new;
+#endif

u8 *transfer_buffer;
u32 last_val;
@@ -175,6 +184,7 @@ static struct snd_tea575x_ops shark_tea_ops = {
.read_val = shark_read_val,
};

+#ifdef SHARK_USE_LEDS
static void shark_led_work(struct work_struct *work)
{
struct shark_device *shark =
@@ -244,20 +254,25 @@ static void shark_led_set_red(struct led_classdev *led_cdev,
set_bit(RED_LED, &shark->brightness_new);
schedule_work(&shark->led_work);
}
+#endif

static void usb_shark_disconnect(struct usb_interface *intf)
{
struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
+#ifdef SHARK_USE_LEDS
int i;
+#endif

mutex_lock(&shark->tea.mutex);
v4l2_device_disconnect(&shark->v4l2_dev);
snd_tea575x_exit(&shark->tea);
mutex_unlock(&shark->tea.mutex);

+#ifdef SHARK_USE_LEDS
for (i = 0; i < NO_LEDS; i++)
led_classdev_unregister(&shark->leds[i]);
+#endif

v4l2_device_put(&shark->v4l2_dev);
}
@@ -266,7 +281,9 @@ static void usb_shark_release(struct v4l2_device *v4l2_dev)
{
struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);

+#ifdef SHARK_USE_LEDS
cancel_work_sync(&shark->led_work);
+#endif
v4l2_device_unregister(&shark->v4l2_dev);
kfree(shark->transfer_buffer);
kfree(shark);
@@ -276,7 +293,10 @@ static int usb_shark_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct shark_device *shark;
- int i, retval = -ENOMEM;
+ int retval = -ENOMEM;
+#ifdef SHARK_USE_LEDS
+ int i;
+#endif

shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
if (!shark)
@@ -321,6 +341,7 @@ static int usb_shark_probe(struct usb_interface *intf,
goto err_init_tea;
}

+#ifdef SHARK_USE_LEDS
INIT_WORK(&shark->led_work, shark_led_work);
for (i = 0; i < NO_LEDS; i++) {
shark->leds[i] = shark_led_templates[i];
@@ -341,6 +362,7 @@ static int usb_shark_probe(struct usb_interface *intf,
"couldn't register led: %s\n",
shark->led_names[i]);
}
+#endif

return 0;

--
1.7.11.4


2012-08-10 19:58:31

by Hans de Goede

[permalink] [raw]
Subject: [PATCH 2/2] radio-shark2: Only compile led support when CONFIG_LED_CLASS is set

Reported-by: Dadiv Rientjes <[email protected]>
Signed-off-by: Hans de Goede <[email protected]>
---
drivers/media/radio/radio-shark2.c | 27 ++++++++++++++++++++++++---
1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/drivers/media/radio/radio-shark2.c b/drivers/media/radio/radio-shark2.c
index b9575de..e593d5a 100644
--- a/drivers/media/radio/radio-shark2.c
+++ b/drivers/media/radio/radio-shark2.c
@@ -27,7 +27,6 @@

#include <linux/init.h>
#include <linux/kernel.h>
-#include <linux/leds.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb.h>
@@ -35,6 +34,12 @@
#include <media/v4l2-device.h>
#include "radio-tea5777.h"

+#if defined(CONFIG_LEDS_CLASS) || \
+ (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK2_MODULE))
+#include <linux/leds.h>
+#define SHARK_USE_LEDS 1
+#endif
+
MODULE_AUTHOR("Hans de Goede <[email protected]>");
MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver");
MODULE_LICENSE("GPL");
@@ -43,7 +48,6 @@ static int debug;
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "Debug level (0-1)");

-
#define SHARK_IN_EP 0x83
#define SHARK_OUT_EP 0x05

@@ -52,6 +56,7 @@ MODULE_PARM_DESC(debug, "Debug level (0-1)");

#define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev)

+#ifdef SHARK_USE_LEDS
enum { BLUE_LED, RED_LED, NO_LEDS };

static void shark_led_set_blue(struct led_classdev *led_cdev,
@@ -73,17 +78,20 @@ static const struct led_classdev shark_led_templates[NO_LEDS] = {
.brightness_set = shark_led_set_red,
},
};
+#endif

struct shark_device {
struct usb_device *usbdev;
struct v4l2_device v4l2_dev;
struct radio_tea5777 tea;

+#ifdef SHARK_USE_LEDS
struct work_struct led_work;
struct led_classdev leds[NO_LEDS];
char led_names[NO_LEDS][32];
atomic_t brightness[NO_LEDS];
unsigned long brightness_new;
+#endif

u8 *transfer_buffer;
};
@@ -161,6 +169,7 @@ static struct radio_tea5777_ops shark_tea_ops = {
.read_reg = shark_read_reg,
};

+#ifdef SHARK_USE_LEDS
static void shark_led_work(struct work_struct *work)
{
struct shark_device *shark =
@@ -216,20 +225,25 @@ static void shark_led_set_red(struct led_classdev *led_cdev,
set_bit(RED_LED, &shark->brightness_new);
schedule_work(&shark->led_work);
}
+#endif

static void usb_shark_disconnect(struct usb_interface *intf)
{
struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
+#ifdef SHARK_USE_LEDS
int i;
+#endif

mutex_lock(&shark->tea.mutex);
v4l2_device_disconnect(&shark->v4l2_dev);
radio_tea5777_exit(&shark->tea);
mutex_unlock(&shark->tea.mutex);

+#ifdef SHARK_USE_LEDS
for (i = 0; i < NO_LEDS; i++)
led_classdev_unregister(&shark->leds[i]);
+#endif

v4l2_device_put(&shark->v4l2_dev);
}
@@ -238,7 +252,9 @@ static void usb_shark_release(struct v4l2_device *v4l2_dev)
{
struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);

+#ifdef SHARK_USE_LEDS
cancel_work_sync(&shark->led_work);
+#endif
v4l2_device_unregister(&shark->v4l2_dev);
kfree(shark->transfer_buffer);
kfree(shark);
@@ -248,7 +264,10 @@ static int usb_shark_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct shark_device *shark;
- int i, retval = -ENOMEM;
+ int retval = -ENOMEM;
+#ifdef SHARK_USE_LEDS
+ int i;
+#endif

shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
if (!shark)
@@ -292,6 +311,7 @@ static int usb_shark_probe(struct usb_interface *intf,
goto err_init_tea;
}

+#ifdef SHARK_USE_LEDS
INIT_WORK(&shark->led_work, shark_led_work);
for (i = 0; i < NO_LEDS; i++) {
shark->leds[i] = shark_led_templates[i];
@@ -312,6 +332,7 @@ static int usb_shark_probe(struct usb_interface *intf,
"couldn't register led: %s\n",
shark->led_names[i]);
}
+#endif

return 0;

--
1.7.11.4

2012-08-10 20:15:55

by Mauro Carvalho Chehab

[permalink] [raw]
Subject: Re: [PATCH 1/2] radio-shark: Only compile led support when CONFIG_LED_CLASS is set

Em 10-08-2012 16:58, Hans de Goede escreveu:
> Reported-by: Dadiv Rientjes <[email protected]>
> Signed-off-by: Hans de Goede <[email protected]>
> ---
> drivers/media/radio/radio-shark.c | 26 ++++++++++++++++++++++++--
> 1 file changed, 24 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/media/radio/radio-shark.c b/drivers/media/radio/radio-shark.c
> index c2ead23..f746ed0 100644
> --- a/drivers/media/radio/radio-shark.c
> +++ b/drivers/media/radio/radio-shark.c
> @@ -27,7 +27,6 @@
>
> #include <linux/init.h>
> #include <linux/kernel.h>
> -#include <linux/leds.h>
> #include <linux/module.h>
> #include <linux/slab.h>
> #include <linux/usb.h>
> @@ -35,6 +34,12 @@
> #include <media/v4l2-device.h>
> #include <sound/tea575x-tuner.h>
>
> +#if defined(CONFIG_LEDS_CLASS) || \
> + (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK_MODULE))
> +#include <linux/leds.h>

Conditionally including headers is not a good thing.

...
> static void usb_shark_disconnect(struct usb_interface *intf)
> {
> struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
> struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
> +#ifdef SHARK_USE_LEDS
> int i;
> +#endif
>
> mutex_lock(&shark->tea.mutex);
> v4l2_device_disconnect(&shark->v4l2_dev);
> snd_tea575x_exit(&shark->tea);
> mutex_unlock(&shark->tea.mutex);
>
> +#ifdef SHARK_USE_LEDS
> for (i = 0; i < NO_LEDS; i++)
> led_classdev_unregister(&shark->leds[i]);
> +#endif
>
> v4l2_device_put(&shark->v4l2_dev);
> }

That looks ugly. Maybe you could code it on a different way.

You could be move all shark_use_leds together into the same place at
the code, like:

#if defined(CONFIG_LEDS_CLASS) || \
(defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK_MODULE))

static void shark_led_set_blue(struct led_classdev *led_cdev,
...
.brightness_set = shark_led_set_red,
},
};

static void shark_led_disconnect(...)
{
...
}

static void shark_led_release(...)
{
...
}

static void shark_led_register(...)
{
...
}
#else
static inline void shark_led_disconnect(...) { };
static inline void shark_led_release(...) { };
static inline void shark_led_register(...)
{
printk(KERN_WARN "radio-shark: CONFIG_LED_CLASS not enabled. LEDs won't work\n");
}
#endif

And let the rest of the code to call the shark_led functions, as if LEDS aren't enabled,
the function stubs won't produce any code (well, except for the above error notice).

The same comment also applies to patch 2.

Regards,
Mauro

2012-08-11 07:34:48

by Hans de Goede

[permalink] [raw]
Subject: Re: [PATCH 1/2] radio-shark: Only compile led support when CONFIG_LED_CLASS is set

Hi,

On 08/10/2012 10:15 PM, Mauro Carvalho Chehab wrote:
> Em 10-08-2012 16:58, Hans de Goede escreveu:
>> Reported-by: Dadiv Rientjes <[email protected]>
>> Signed-off-by: Hans de Goede <[email protected]>
>> ---
>> drivers/media/radio/radio-shark.c | 26 ++++++++++++++++++++++++--
>> 1 file changed, 24 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/media/radio/radio-shark.c b/drivers/media/radio/radio-shark.c
>> index c2ead23..f746ed0 100644
>> --- a/drivers/media/radio/radio-shark.c
>> +++ b/drivers/media/radio/radio-shark.c
>> @@ -27,7 +27,6 @@
>>
>> #include <linux/init.h>
>> #include <linux/kernel.h>
>> -#include <linux/leds.h>
>> #include <linux/module.h>
>> #include <linux/slab.h>
>> #include <linux/usb.h>
>> @@ -35,6 +34,12 @@
>> #include <media/v4l2-device.h>
>> #include <sound/tea575x-tuner.h>
>>
>> +#if defined(CONFIG_LEDS_CLASS) || \
>> + (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK_MODULE))
>> +#include <linux/leds.h>
>
> Conditionally including headers is not a good thing.
>

Ok, I will make it unconditional then.

> ...
>> static void usb_shark_disconnect(struct usb_interface *intf)
>> {
>> struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
>> struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
>> +#ifdef SHARK_USE_LEDS
>> int i;
>> +#endif
>>
>> mutex_lock(&shark->tea.mutex);
>> v4l2_device_disconnect(&shark->v4l2_dev);
>> snd_tea575x_exit(&shark->tea);
>> mutex_unlock(&shark->tea.mutex);
>>
>> +#ifdef SHARK_USE_LEDS
>> for (i = 0; i < NO_LEDS; i++)
>> led_classdev_unregister(&shark->leds[i]);
>> +#endif
>>
>> v4l2_device_put(&shark->v4l2_dev);
>> }
>
> That looks ugly.

Agreed.

> Maybe you could code it on a different way.
>
> You could be move all shark_use_leds together into the same place at
> the code, like:
>

Sounds good, although I will still need to set a SHARK_USE_LEDS define at
the top of the file, to avoid having unused members in struct shark_device, not
that the compiler will complain but having them there for no good reason seems
undesirable.


> #if defined(CONFIG_LEDS_CLASS) || \
> (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK_MODULE))
>
> static void shark_led_set_blue(struct led_classdev *led_cdev,
> ...
> .brightness_set = shark_led_set_red,
> },
> };
>
> static void shark_led_disconnect(...)
> {
> ...
> }
>
> static void shark_led_release(...)
> {
> ...
> }
>
> static void shark_led_register(...)
> {
> ...
> }
> #else
> static inline void shark_led_disconnect(...) { };
> static inline void shark_led_release(...) { };
> static inline void shark_led_register(...)
> {
> printk(KERN_WARN "radio-shark: CONFIG_LED_CLASS not enabled. LEDs won't work\n");
> }
> #endif
>
> And let the rest of the code to call the shark_led functions, as if LEDS aren't enabled,
> the function stubs won't produce any code (well, except for the above error notice).
>
> The same comment also applies to patch 2.

Ok new versions of the 2 patches coming up (should be there in 2 hours or so, I want
to both compile and run-time test them both with / without leds).

Regards,

Hans