All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: "Éric Piel" <Eric.Piel@tremplin-utc.net>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Jean Delvare <khali@linux-fr.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] lis3: Add axes module parameter for custom axis-mapping
Date: Tue, 07 Sep 2010 21:31:54 +0200	[thread overview]
Message-ID: <s5hmxrtuz85.wl%tiwai@suse.de> (raw)
In-Reply-To: <4C868FC0.3040602@tremplin-utc.net>

At Tue, 07 Sep 2010 21:17:20 +0200,
Éric Piel wrote:
> 
> Op 07-09-10 18:10, Takashi Iwai schreef:
> > At Tue, 07 Sep 2010 17:54:58 +0200,
> :
> >>>  
> >>> +module_param_array_named(axes, lis3_dev.axis_map, int, NULL, 0644);
> >>> +MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
> >> But now there is no check at all about the value being between 1 and 3,
> >> is there?
> >> I think it's still necessary to have a small function which checks for
> >> correct values, otherwise the user could read a bit anywhere in the memory.
> > 
> > Ah, right, it's accessing the array index.  This must be fixed indeed.
> > Another revised version below.
> > 
> It seems to be the same patch as the previous version. You probably
> attached the wrong patch.

Oops, sorry.  Attached below.


Takashi

===

>From 0c5400d8214c9585e8eda18eb0fb8715e3d90e69 Mon Sep 17 00:00:00 2001
From: Takashi Iwai <tiwai@suse.de>
Date: Tue, 7 Sep 2010 16:40:59 +0200
Subject: [PATCH] lis3: Add axes module parameter for custom axis-mapping

The axis-mapping of lis3dev device on many (rather most) HP machines
doesn't follow the standard.  When each new model appears, users need
to adjust again.  Testing this requires the rebuild of kernel, thus
it's not trivial for end-users.

This patch adds a module parameter "axes" to allow a custom
axis-mapping without patching and recompiling the kernel driver.
User can pass the parameter such as axes=3,2,1.  Also it can be
changed via sysfs.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
v2->v3: add the sanity-check of the axes parameter values
v1->v2: change lis3_dev.ac to an int array to be written directly via
        module axes parameter

 drivers/hwmon/hp_accel.c      |   35 ++++++++++++++++++++---------------
 drivers/hwmon/lis3lv02d.c     |   36 ++++++++++++++++++++++++++++++------
 drivers/hwmon/lis3lv02d.h     |    8 +-------
 drivers/hwmon/lis3lv02d_i2c.c |   12 +++++-------
 4 files changed, 56 insertions(+), 35 deletions(-)

diff --git a/drivers/hwmon/hp_accel.c b/drivers/hwmon/hp_accel.c
index 7580f55..b9dd63f 100644
--- a/drivers/hwmon/hp_accel.c
+++ b/drivers/hwmon/hp_accel.c
@@ -146,7 +146,8 @@ int lis3lv02d_acpi_write(struct lis3lv02d *lis3, int reg, u8 val)
 
 static int lis3lv02d_dmi_matched(const struct dmi_system_id *dmi)
 {
-	lis3_dev.ac = *((struct axis_conversion *)dmi->driver_data);
+	memcpy(lis3_dev.axis_map, (int *)dmi->driver_data,
+	       sizeof(lis3_dev.axis_map));
 	printk(KERN_INFO DRIVER_NAME ": hardware type %s found.\n", dmi->ident);
 
 	return 1;
@@ -154,16 +155,16 @@ static int lis3lv02d_dmi_matched(const struct dmi_system_id *dmi)
 
 /* Represents, for each axis seen by userspace, the corresponding hw axis (+1).
  * If the value is negative, the opposite of the hw value is used. */
-static struct axis_conversion lis3lv02d_axis_normal = {1, 2, 3};
-static struct axis_conversion lis3lv02d_axis_y_inverted = {1, -2, 3};
-static struct axis_conversion lis3lv02d_axis_x_inverted = {-1, 2, 3};
-static struct axis_conversion lis3lv02d_axis_z_inverted = {1, 2, -3};
-static struct axis_conversion lis3lv02d_axis_xy_swap = {2, 1, 3};
-static struct axis_conversion lis3lv02d_axis_xy_rotated_left = {-2, 1, 3};
-static struct axis_conversion lis3lv02d_axis_xy_rotated_left_usd = {-2, 1, -3};
-static struct axis_conversion lis3lv02d_axis_xy_swap_inverted = {-2, -1, 3};
-static struct axis_conversion lis3lv02d_axis_xy_rotated_right = {2, -1, 3};
-static struct axis_conversion lis3lv02d_axis_xy_swap_yz_inverted = {2, -1, -3};
+static int lis3lv02d_axis_normal[3] = {1, 2, 3};
+static int lis3lv02d_axis_y_inverted[3] = {1, -2, 3};
+static int lis3lv02d_axis_x_inverted[3] = {-1, 2, 3};
+static int lis3lv02d_axis_z_inverted[3] = {1, 2, -3};
+static int lis3lv02d_axis_xy_swap[3] = {2, 1, 3};
+static int lis3lv02d_axis_xy_rotated_left[3] = {-2, 1, 3};
+static int lis3lv02d_axis_xy_rotated_left_usd[3] = {-2, 1, -3};
+static int lis3lv02d_axis_xy_swap_inverted[3] = {-2, -1, 3};
+static int lis3lv02d_axis_xy_rotated_right[3] = {2, -1, 3};
+static int lis3lv02d_axis_xy_swap_yz_inverted[3] = {2, -1, -3};
 
 #define AXIS_DMI_MATCH(_ident, _name, _axis) {		\
 	.ident = _ident,				\
@@ -171,7 +172,7 @@ static struct axis_conversion lis3lv02d_axis_xy_swap_yz_inverted = {2, -1, -3};
 	.matches = {					\
 		DMI_MATCH(DMI_PRODUCT_NAME, _name)	\
 	},						\
-	.driver_data = &lis3lv02d_axis_##_axis		\
+	.driver_data = lis3lv02d_axis_##_axis		\
 }
 
 #define AXIS_DMI_MATCH2(_ident, _class1, _name1,	\
@@ -183,7 +184,7 @@ static struct axis_conversion lis3lv02d_axis_xy_swap_yz_inverted = {2, -1, -3};
 		DMI_MATCH(DMI_##_class1, _name1),	\
 		DMI_MATCH(DMI_##_class2, _name2),	\
 	},						\
-	.driver_data = &lis3lv02d_axis_##_axis		\
+	.driver_data = lis3lv02d_axis_##_axis		\
 }
 static struct dmi_system_id lis3lv02d_dmi_ids[] = {
 	/* product names are truncated to match all kinds of a same model */
@@ -297,10 +298,14 @@ static int lis3lv02d_add(struct acpi_device *device)
 	lis3lv02d_enum_resources(device);
 
 	/* If possible use a "standard" axes order */
-	if (dmi_check_system(lis3lv02d_dmi_ids) == 0) {
+	if (lis3_dev.axis_map[0] && lis3_dev.axis_map[1] && lis3_dev.axis_map[2]) {
+		printk(KERN_INFO DRIVER_NAME ": Using custom axes %d,%d,%d\n",
+		       lis3_dev.axis_map[0], lis3_dev.axis_map[1], lis3_dev.axis_map[2]);
+	} else if (dmi_check_system(lis3lv02d_dmi_ids) == 0) {
 		printk(KERN_INFO DRIVER_NAME ": laptop model unknown, "
 				 "using default axes configuration\n");
-		lis3_dev.ac = lis3lv02d_axis_normal;
+		memcpy(lis3_dev.axis_map, lis3lv02d_axis_normal,
+		       sizeof(lis3_dev.axis_map));
 	}
 
 	/* call the core layer do its init */
diff --git a/drivers/hwmon/lis3lv02d.c b/drivers/hwmon/lis3lv02d.c
index 6138f03..5e15967 100644
--- a/drivers/hwmon/lis3lv02d.c
+++ b/drivers/hwmon/lis3lv02d.c
@@ -75,6 +75,30 @@ struct lis3lv02d lis3_dev = {
 
 EXPORT_SYMBOL_GPL(lis3_dev);
 
+/* just like param_set_int() but does sanity-check so that it won't point
+ * over the axis array size
+ */
+static int param_set_axis(const char *val, const struct kernel_param *kp)
+{
+	int ret = param_set_int(val, kp);
+	if (!ret) {
+		int val = *(int *)kp->arg;
+		if (val < 0)
+			val = -val;
+		if (!val || val > 3)
+			return -EINVAL;
+	}
+	return ret;
+}
+
+static struct kernel_param_ops param_ops_axis = {
+	.set = param_set_axis,
+	.get = param_get_int,
+};
+
+module_param_array_named(axes, lis3_dev.axis_map, axis, NULL, 0644);
+MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
+
 static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
 {
 	s8 lo;
@@ -130,9 +154,9 @@ static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
 	for (i = 0; i < 3; i++)
 		position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
 
-	*x = lis3lv02d_get_axis(lis3->ac.x, position);
-	*y = lis3lv02d_get_axis(lis3->ac.y, position);
-	*z = lis3lv02d_get_axis(lis3->ac.z, position);
+	*x = lis3lv02d_get_axis(lis3->axis_map[0], position);
+	*y = lis3lv02d_get_axis(lis3->axis_map[1], position);
+	*z = lis3lv02d_get_axis(lis3->axis_map[2], position);
 }
 
 /* conversion btw sampling rate and the register values */
@@ -479,9 +503,9 @@ int lis3lv02d_joystick_enable(void)
 	input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
 	input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
 
-	lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
-	lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
-	lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
+	lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.axis_map[0]), btns);
+	lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.axis_map[1]), btns);
+	lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.axis_map[2]), btns);
 
 	err = input_register_polled_device(lis3_dev.idev);
 	if (err) {
diff --git a/drivers/hwmon/lis3lv02d.h b/drivers/hwmon/lis3lv02d.h
index 8540913..51b9ba4 100644
--- a/drivers/hwmon/lis3lv02d.h
+++ b/drivers/hwmon/lis3lv02d.h
@@ -206,12 +206,6 @@ enum lis3lv02d_click_src_8b {
 	CLICK_IA	= 0x40,
 };
 
-struct axis_conversion {
-	s8	x;
-	s8	y;
-	s8	z;
-};
-
 struct lis3lv02d {
 	void			*bus_priv; /* used by the bus layer only */
 	int (*init) (struct lis3lv02d *lis3);
@@ -232,7 +226,7 @@ struct lis3lv02d {
 	struct input_polled_dev	*idev;     /* input device */
 	struct platform_device	*pdev;     /* platform device */
 	atomic_t		count;     /* interrupt count after last read */
-	struct axis_conversion	ac;        /* hw -> logical axis */
+	int			axis_map[3];  /* hw -> logical axis */
 	int			mapped_btns[3];
 
 	u32			irq;       /* IRQ number */
diff --git a/drivers/hwmon/lis3lv02d_i2c.c b/drivers/hwmon/lis3lv02d_i2c.c
index dc1f540..76ba42d 100644
--- a/drivers/hwmon/lis3lv02d_i2c.c
+++ b/drivers/hwmon/lis3lv02d_i2c.c
@@ -61,9 +61,7 @@ static int lis3_i2c_init(struct lis3lv02d *lis3)
 }
 
 /* Default axis mapping but it can be overwritten by platform data */
-static struct axis_conversion lis3lv02d_axis_map = { LIS3_DEV_X,
-						     LIS3_DEV_Y,
-						     LIS3_DEV_Z };
+static int lis3lv02d_axis_map[3] = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z };
 
 static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
 					const struct i2c_device_id *id)
@@ -73,13 +71,13 @@ static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
 
 	if (pdata) {
 		if (pdata->axis_x)
-			lis3lv02d_axis_map.x = pdata->axis_x;
+			lis3lv02d_axis_map[0] = pdata->axis_x;
 
 		if (pdata->axis_y)
-			lis3lv02d_axis_map.y = pdata->axis_y;
+			lis3lv02d_axis_map[1] = pdata->axis_y;
 
 		if (pdata->axis_z)
-			lis3lv02d_axis_map.z = pdata->axis_z;
+			lis3lv02d_axis_map[2] = pdata->axis_z;
 
 		if (pdata->setup_resources)
 			ret = pdata->setup_resources();
@@ -94,7 +92,7 @@ static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
 	lis3_dev.read	  = lis3_i2c_read;
 	lis3_dev.write	  = lis3_i2c_write;
 	lis3_dev.irq	  = client->irq;
-	lis3_dev.ac	  = lis3lv02d_axis_map;
+	memcpy(&lis3_dev.axis_map, &lis3lv02d_axis_map, sizeof(lis3_dev.axis_map));
 
 	i2c_set_clientdata(client, &lis3_dev);
 	ret = lis3lv02d_init_device(&lis3_dev);
-- 
1.7.2.1


  reply	other threads:[~2010-09-07 19:31 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-23 12:23 [PATCH] lis3: Add axes module parameter for custom axis-mapping Takashi Iwai
2010-09-07  6:45 ` Takashi Iwai
2010-09-07  9:14   ` Jean Delvare
2010-09-07  9:16     ` Takashi Iwai
2010-09-07 14:25   ` Éric Piel
2010-09-07 14:44     ` Takashi Iwai
2010-09-07 15:54       ` Éric Piel
2010-09-07 16:10         ` Takashi Iwai
2010-09-07 19:17           ` Éric Piel
2010-09-07 19:31             ` Takashi Iwai [this message]
2010-09-08 19:28               ` Éric Piel

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=s5hmxrtuz85.wl%tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=Eric.Piel@tremplin-utc.net \
    --cc=akpm@linux-foundation.org \
    --cc=khali@linux-fr.org \
    --cc=linux-kernel@vger.kernel.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.