Bug 26584 - aiptek driver crash xorg server on XkbApplyMappingChange
Summary: aiptek driver crash xorg server on XkbApplyMappingChange
Status: RESOLVED FIXED
Alias: None
Product: xorg
Classification: Unclassified
Component: Input/aiptek (show other bugs)
Version: unspecified
Hardware: All All
: medium normal
Assignee: Xorg Project Team
QA Contact: Xorg Project Team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-02-16 01:35 UTC by Olivier Samyn
Modified: 2011-10-17 01:59 UTC (History)
2 users (show)

See Also:
i915 platform:
i915 features:


Attachments
Patch solving the issue. (3.30 KB, patch)
2010-02-16 01:35 UTC, Olivier Samyn
no flags Details | Splinter Review
git formatted diff against a7460d35743b7343a6dcce13f14cfd28428385bf (3.94 KB, patch)
2010-02-16 10:11 UTC, Olivier Samyn
no flags Details | Splinter Review
Uses event-code instead of event->value (992 bytes, patch)
2010-02-17 15:23 UTC, Olivier Samyn
no flags Details | Splinter Review
Uses direct event code to keycode mapping for XI ABI >=7 (2.47 KB, patch)
2010-02-17 15:24 UTC, Olivier Samyn
no flags Details | Splinter Review
Simplify keycode to keysym mapping (5.22 KB, patch)
2010-02-17 15:24 UTC, Olivier Samyn
no flags Details | Splinter Review
Correct linux keymap to keysym index loop (2.50 KB, patch)
2010-02-20 15:35 UTC, Olivier Samyn
no flags Details | Splinter Review
Change index offset for generated key events (1.34 KB, patch)
2010-02-20 15:41 UTC, Olivier Samyn
no flags Details | Splinter Review
Initialize the keymap with 256 items (1.10 KB, patch)
2010-02-23 15:28 UTC, Olivier Samyn
no flags Details | Splinter Review
Add keyboard feedback struct initialization (1.60 KB, patch)
2010-02-23 15:30 UTC, Olivier Samyn
no flags Details | Splinter Review
Generate only key press or key release depending on kernel event. (4.40 KB, patch)
2010-02-23 15:45 UTC, Olivier Samyn
no flags Details | Splinter Review

Description Olivier Samyn 2010-02-16 01:35:04 UTC
Created attachment 33325 [details] [review]
Patch solving the issue.

Trying to get my tablet working on my Ubuntu 9.10 (xorg 7.4; xorg-input-aiptek 1.3.0), my X server crashes.

After some investigations, I got a xorg backtrace showing XkbApplyMappingChange as the last called method.

According to some old list threads and forum posts here is a patch that should solve this problem.

Some references:
http://ubuntuforums.org/showpost.php?p=5523512&postcount=79
http://lists.freedesktop.org/archives/xorg/2007-November/030108.html

Same problem solved in wacom driver since 2007-11-28:
http://linuxwacom.cvs.sourceforge.net/viewvc/linuxwacom/linuxwacom-prod/ChangeLog?revision=1.79&view=markup
http://linuxwacom.cvs.sourceforge.net/viewvc/linuxwacom/linuxwacom-prod/src/xdrv/xf86Wacom.c?r1=1.20&r2=1.21&view=patch
Comment 1 Alan Coopersmith 2010-02-16 07:28:23 UTC
Patches tend to be more quickly processed when mailed to xorg-devel
as the output from git format-patch, as described at:
http://www.x.org/wiki/Development/Documentation/SubmittingPatches

Comment 2 Olivier Samyn 2010-02-16 10:11:50 UTC
Created attachment 33338 [details] [review]
git formatted diff against a7460d35743b7343a6dcce13f14cfd28428385bf
Comment 3 Olivier Samyn 2010-02-16 10:12:27 UTC
Comment on attachment 33325 [details] [review]
Patch solving the issue.

Diff against source release 1.3.0
Comment 4 Peter Hutterer 2010-02-16 16:19:16 UTC
Thanks for the patch, looks good. I do wonder if it would be simpler to do something like this:

static KeySym aiptek_map[256] = {
 // all already defined keysyms
}

Since NoSymbol is equivalent to 0L and that won't change this would give us the same result but make the patch and the code easier to read by not having lines of NoSymbol.
Comment 5 Olivier Samyn 2010-02-16 23:16:14 UTC
Yes it's really simpler (but I was also somewhat mirroring the wacom driver behaviour).

But, after some further testing, I found another bug, around the macroKey assignment.Line 915:
/*
 * Any other EV_KEY event is assumed to be
 * the pressing of a macro key from the tablet.
 */
default:
{
    ++eventsInMessage;
    common->currentValues.macroKey = event->value;
}
break;

I suppose macroKey should be equal to event->code (and not value).

Also, using the 256 entries table, we can probably directly map linux kernel key code to X.Org values. (and not looping over possible kernel values and assigning them to X.Org values).

I will try to come with a better patch today.
Comment 6 Peter Hutterer 2010-02-16 23:32:33 UTC
> --- Comment #5 from Olivier Samyn <olivier@oleastre.be>  2010-02-16 23:16:14 PST ---
> Yes it's really simpler (but I was also somewhat mirroring the wacom driver
> behaviour).

that should probably get fixed in the wacom driver too.

> But, after some further testing, I found another bug, around the macroKey
> assignment.Line 915:
> /*
>  * Any other EV_KEY event is assumed to be
>  * the pressing of a macro key from the tablet.
>  */
> default:
> {
>     ++eventsInMessage;
>     common->currentValues.macroKey = event->value;
> }
> break;
> 
> I suppose macroKey should be equal to event->code (and not value).

yes, that makes sense. please provide a separate patch for this though to
ease bisectability and traceability.

> Also, using the 256 entries table, we can probably directly map linux kernel
> key code to X.Org values. (and not looping over possible kernel values and
> assigning them to X.Org values).

well, it's not quite that easy. the kernel sends keycodes that are then
mapped into keysyms by the client. X keycodes are essentially kernel keycode
+ 8, so that's easy enough provided the user has the evdev ruleset (which is
standard these days on linux).

where it does become tricky though is that users configure thinking of
keysyms, not keycodes. so a user says "I want to map Q to K", even though
"Q" is only KEY_Q on a qwerty layout. so some keycode to keysym lookup may
be needed (the wacom driver still does that for this reason).
tbh, I don't know enough about the aiptek driver to know if that applies in
this case, but please be aware of the above - you'd risk wasting time
otherwise.
Comment 7 Olivier Samyn 2010-02-17 02:14:57 UTC
From the kernel source code, the aiptek driver translates some status bits to a fixed set of kernel KEY_* key events.

What I plan to do is:
- have two static key arrays that defines the direct key mapping (keep current linux_inputDevice_keyMap and a slightly modified aiptek_map)
- add a new static 256 elements array (call it aiptek_key_map) that will define the real key, all elements initialized to 0.
- Modify xf86AiptekPlug and add a loop that initialize aiptek_key_map with 
the following rule: aiptek_key_map[linux_inputDevice_keyMap[i]]=aiptek_map[i]
- Modify xf86AiptekSendEvents to map directly kernel event to X.Org one. Event keysym = aiptek_key_map[common->currentValues.macroKey]

Patch will follow...
Comment 8 Olivier Samyn 2010-02-17 15:23:32 UTC
Created attachment 33376 [details] [review]
Uses event-code instead of event->value

Patch series to make the tablet keys working.
Comment 9 Olivier Samyn 2010-02-17 15:24:19 UTC
Created attachment 33377 [details] [review]
Uses direct event code to keycode mapping for XI ABI >=7
Comment 10 Olivier Samyn 2010-02-17 15:24:47 UTC
Created attachment 33378 [details] [review]
Simplify keycode to keysym mapping
Comment 11 Olivier Samyn 2010-02-17 15:28:19 UTC
I added three separate patches to simplify the current kernel keycode to keysym mapping as discussed before.

Everything seems to work, except I do not get any keyboard event in xev. I'm probably missing something, but can not figure it out.

I will continue investigating, but some help is welcome.
Comment 12 Peter Hutterer 2010-02-18 18:08:23 UTC
(In reply to comment #8)
> Created an attachment (id=33376) [details]
> Uses event-code instead of event->value

applied, thanks.(In reply to comment #9)

> Created an attachment (id=33377) [details]
> Uses direct event code to keycode mapping for XI ABI >=7

this one isn't correct, if you change to 8+i instead of 7+i you offset pre-ABI 7 mapping by one too, don't you?(In reply to comment #10)

> Created an attachment (id=33378) [details]
> Simplify keycode to keysym mapping

i think this one needs a few more comments to explain what it does.

+            for (loop = 0; loop < linux_inputDevice_keyMap_len; ++loop)

this block - couldn't you just memcpy this? you can also use ARRAY_LENGTH (copy from evdev) to avoid the _len variable. I'm not sure how needed this approach is anyway - I think you could just leave the current array in place but fix it at [256] in size so the remainers are filled with NoSymbol.


also, the map itself looks quite suspicious - XK_F2 for keycode 9 (KEY_ESC), this is rather odd and would likely return weird results. what are the actual keycodes you get from the kernel? Are they standardised through the event api? if so, you could leave the map as empty anyway, because the actual map is kept by xkb.
Comment 13 Olivier Samyn 2010-02-20 15:35:04 UTC
Created attachment 33465 [details] [review]
Correct linux keymap to keysym index loop

This one corrects the loop that was iterating over the linux key map but testing for a match only with the first map element ([0] instead of [i]).
Comment 14 Olivier Samyn 2010-02-20 15:41:20 UTC
Created attachment 33466 [details] [review]
Change index offset for generated key events

(In reply to comment #12)
> this one isn't correct, if you change to 8+i instead of 7+i you offset pre-ABI
> 7 mapping by one too, don't you?(In reply to comment #10)

The internal linux map starts with KEY_F1 (index 0 in the map), that maps to XK_F1 that's the 9th element of the aiptek_map (index 8 in the map).

So, I suppose adding 8 is the intended behavior (or is there some magic trick in the xf86PostKeyEvent function that adds one ? ).
Comment 15 Peter Hutterer 2010-02-21 22:31:00 UTC
(In reply to comment #14)
> The internal linux map starts with KEY_F1 (index 0 in the map), that maps to
> XK_F1 that's the 9th element of the aiptek_map (index 8 in the map).

oh, right, sorry. I went with the assumption that the event we get has the value from the kernel (e.g. KEY_ESC) not the index into the array.

both patches merged and pushed, thanks.

Comment 16 Olivier Samyn 2010-02-23 15:28:17 UTC
Created attachment 33514 [details] [review]
Initialize the keymap with 256 items

This solves the actual bug in the simplest way. 

Xorg does not crash anymore, but no key event is received by xev.
Comment 17 Olivier Samyn 2010-02-23 15:30:11 UTC
Created attachment 33515 [details] [review]
Add keyboard feedback struct initialization

A simple call to InitKbdFeedbackClassDeviceStruct seems to make the aiptek driver recognized as a keyboard event emitter.

So this makes aiptek generated keyboard events visible to xev.
Comment 18 Peter Hutterer 2010-02-23 15:42:32 UTC
Thanks, applied and pushed.
As I've added in the commit message for the second patch, this only fixes the issue for X servers 1.6 and earlier. X server 1.7 has a different initialization method that - afaict - is currently not employed at all by the aiptek driver. So under 1.7, the driver has no key symbols at all.

Just letting you know about this so you're not surprised when you update :)
Please feel free to send fixes for this case as well, I really appreciate your work here.

Given that this bug is fixed now (I think?), it's probably best to close it and open another one for the 1.7 issue though.
Comment 19 Olivier Samyn 2010-02-23 15:45:39 UTC
Created attachment 33516 [details] [review]
Generate only key press or key release depending on kernel event.

Without this patch, the driver generates two key events (hard coded press and release events) per physical key press on the tablet. 

This patch keeps track of the kernel event value that contains 1 if key pressed and 0 if released; and reuse that value to generate the X event.
Comment 20 Olivier Samyn 2010-02-23 15:51:05 UTC
Cross posting, I just added a small patch for double event generation.

And yes, it solves issues with xserver 1.6 (as I'm still running ubuntu 9.10).

But I plan to install this week the next version, and makes the driver working with xserver 1.7 (and I hope the patches will be merged downstream).

Also, there is still an issue with the generated keysyms.

Although the keycode is the one emitted by the driver, the received keysym seems to come from the evdev keymap (and not the specific one installed by the aiptek driver).

Basically this closes this bug, and I will open a new one for the keysym issue and another one for xserver 1.7 support.
Comment 21 Peter Hutterer 2010-02-23 21:42:46 UTC
(In reply to comment #19)
> Created an attachment (id=33516) [details]
> Generate only key press or key release depending on kernel event.
> 
> Without this patch, the driver generates two key events (hard coded press and
> release events) per physical key press on the tablet. 
> 
> This patch keeps track of the kernel event value that contains 1 if key pressed
> and 0 if released; and reuse that value to generate the X event.

for server 1.6 and later you also need to filter out autorepeat - the server will do that for you. See the evdev code at 
http://cgit.freedesktop.org/xorg/driver/xf86-input-evdev/tree/src/evdev.c#n299

or does the driver do its own filtering anyway? i noticed the previousValues.pressed so if you have the automatic filtering based on the event value, you might be able to get rid of the previousValues.pressed part? (just guessing again)


(In reply to comment #20)
> Basically this closes this bug, and I will open a new one for the keysym issue
> and another one for xserver 1.7 support.

thanks, I'll comment on that bug then, because atm I'm confused what server version this issue occurs ;) 

Comment 22 Olivier Samyn 2010-08-15 11:26:59 UTC
Follow up for xinput ABI > 7 posted as bug #29543
Comment 23 Peter Hutterer 2011-07-07 15:18:51 UTC
sorry for the late answer. Is this still a problem, do you still have this device and are you still using it?
aiptek is barely maintained and tested, it might be beneficial to get the driver supported properly by the kernel and then use evdev or the wacom driver for this device
Comment 24 Jeremy Huddleston Sequoia 2011-10-17 01:58:04 UTC
Short of getting this into kernel-land for evdev, is there anything wrong with 
the attached patch?  If not, let's just get the patch committed and close this 
out.
Comment 25 Jeremy Huddleston Sequoia 2011-10-17 01:59:27 UTC
Closing per #29543


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.