Monday, March 10, 2008

Hacking xpad kernel module for Mad Catz Beat Pad

I spent three days reading various forums and posts on adding support to xpad for one controller or another. Every post pointed towards downloading the newest CVS source from the Xbox-Linux repository. After unsuccessfully trying to make the new xpad driver detect my controller, I contacted one of the maintainers for the xpad kernel module. He suggested I go back and attempt to add support directly into the kernel version (which is version 0.0.6).

Here is what I did to make this work (you should be working from konsole or similar to run any of the sudo commands - keep this terminal window open and work from it):

1) Install required packages (build environment)

sudo apt-get install linux-headers-`uname -r` build-essential automake1.9

2) Install linux kernel source (where we will get the xpad.c file)

sudo apt-get install linux-source

3) Prepare xpad directory and extract xpad.c from linux-source tarball.

cd
mkdir xpad
cd xpad
tar -jxvf /usr/src/linux-source-`uname -r | awk -F"-" '{print $1}'`.tar.bz2 --strip 4 linux-source-`uname -r | awk -F"-" '{print $1}'`/drivers/input/joystick/xpad.c

Note: The above tar command should be entered all on one line. The `uname -r | awk -F"-" '{print $1}'` is used to detect your kernel version (and hence the source). If different, replace that code with the kernel version (like 2.6.24).

4) Now we need a Makefile to be able to build the module. Open kate (or favourite text editor like nano or vi) and paste the following. There should be a single tab (and not spaces) in the line following all: and install:. These will not paste correctly, so you have to make the change in the file before saving it. Save it as Makefile under the xpad directory.

KERNEL_PATH?=/usr/src/linux-headers-$(shell uname -r)

EXTRA_CFLAGS=-I$(shell pwd)

obj-m:=xpad.o

all:
$(MAKE) modules -C $(KERNEL_PATH) SUBDIRS=$(shell pwd)

install:
cp -f xpad.ko /lib/modules/$(shell uname -r)/kernel/drivers/input/joystick

5) Patching xpad.c for Mad Catz Beat Pad. According to my lsusb output, I have a 0x0738:0x4740 device ID. This is important, and the basis for what we need to hack into the driver. Save the output below to a file called xpad.patch under your xpad directory (using kate, nano, vi, etc).

--- xpad-0.0.6/xpad.c.orig 2008-03-09 23:58:32.000000000 -0230
+++ xpad-0.0.6/xpad.c 2008-03-10 00:39:56.000000000 -0230
@@ -121,6 +121,7 @@
{ 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
{ 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX },
{ 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX360 },
+ { 0x0738, 0x4740, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
{ 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
{ 0x0c12, 0x8802, "Zeroplus Xbox Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX },
{ 0x0c12, 0x8810, "Zeroplus Xbox Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX },
@@ -182,6 +183,7 @@
{ USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
{ USB_DEVICE_INTERFACE_PROTOCOL(0x045e, 0x028e, 1) }, /* X-Box 360 controller */
{ USB_DEVICE_INTERFACE_PROTOCOL(0x1430, 0x4748, 1) }, /* RedOctane Guitar Hero X-plorer */
+ { USB_DEVICE_INTERFACE_PROTOCOL(0x0738, 0x4740, 1) }, /* Mad Catz Beat Pad */
{ }
};

Note: There should be 17 lines in this file. Make sure none of the lines wrapped with a newline, or the patch file will not work.

Now, run this command (from konsole/terminal) to patch the xpad.c file:

patch -p0 < xpad.patch

You now have a patched xpad.c for use with the Mad Catz Beat Pad (0x0738:0x4740). You can alter the patch above to achieve similar for other Xbox 360 controllers (not wireless, which likely require the newer drivers - see reference site listed at end).

6) Compiling and installing the drivers

make && sudo make install && sudo rmmod xpad && sudo depmod -a && modprobe xpad

Note: Pay attention to any output. Do not worry if it complains about no module to remove (maybe it wasn't loaded). If no other errors, then it should now have compiled, installed and loaded.

7) Please see reference below for additional details, troubleshooting, and the general help guide for Xbox controllers under Ubuntu (though the guide specifies using a newer driver - currently it's 0.1.7).

References:

Install and configure Microsoft® Xbox™ and Xbox 360™ controllers under Ubuntu 7.10