<?xml version="1.0" encoding="utf-8"?>

<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<title type="text">The Geekess</title>
<subtitle type="html"><![CDATA[
Linux, bicycling, open source, gardening, amateur rockets, and other seemingly unrelated hobbies.
]]></subtitle>
<id>http://sarah.thesharps.us/2009-06-09-13-30.atom</id>
<link rel="alternate" type="text/html" href="http://sarah.thesharps.us" />
<link rel="self" type="application/atom+xml" href="http://sarah.thesharps.us/2009-06-09-13-30.atom" />


<author>
<name>Sarah Sharp</name>
<uri>http://sarah.thesharps.us/2009-06-09-13-30.atom</uri>
<email>sarah@thesharps.us</email>
</author>
<rights>Copyright 2008 Sarah Sharp, under Creative Commons Attribution Non-commercial Share Alike License</rights>
<generator uri="http://pyblosxom.sourceforge.net/" version="1.4.3 01/10/2008">
PyBlosxom http://pyblosxom.sourceforge.net/ 1.4.3 01/10/2008
</generator>

<updated>2009-06-09T20:30:00Z</updated>
<!-- icon?  logo?  -->

<entry>
<title type="html">Installing a custom kernel with USB 3.0 support</title>
<category term="" />
<id>http://sarah.thesharps.us/2009/06/09/2009-06-09-13-30</id>
<updated>2009-06-09T20:30:00Z</updated>
<published>2009-06-09T20:30:00Z</published>
<link rel="alternate" type="text/html" href="http://sarah.thesharps.us/2009-06-09-13-30.cherry" />
<content type="html">&lt;p&gt;This documents my personal flow for downloading and installing a Linux kernel
with my xHCI and USB 3.0 code.  Until the code is in the upstream kernel and
shipping in Linux distributions, you&apos;ll have to follow these directions to get
Linux USB 3.0 support.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h1&gt;Background&lt;/h1&gt;

&lt;p&gt;This tutorial is mostly for vendors who want to test their USB 3.0 devices or
xHCI host controller prototypes.  This assumes you already have a standard
Debian or Ubuntu installation.  There are many helpful guides for installing &lt;a
href=&quot;https://help.ubuntu.com/community/Installation&quot;&gt;Ubuntu&lt;/a&gt; or &lt;a
href=&quot;http://www.linuxdevcenter.com/pub/a/linux/2005/09/29/installing_debian.html&quot;&gt;Debian&lt;/a&gt;,
so I won&apos;t go into detail on that here.&lt;/p&gt;

&lt;p&gt;There are also other guides for creating a custom kernel, but most walk you
through creating a .deb package.  Going through the extra steps of creating the
package adds too much time if you&apos;re updating your kernel a lot in order to
track an upstream git tree.&lt;/p&gt;

&lt;h1&gt;Pre-work&lt;/h1&gt;

&lt;p&gt;First, you need to install some extra software that may not come with a standard
Ubuntu or Debian install.  You can install software by using a gui installer
(like synaptic).  You can also open a terminal (found under the Applications menu in
Gnome and under the System menu in KDE) and install software using the command
line.  I&apos;ll assume you&apos;re using the command line in this tutorial.&lt;/p&gt;

&lt;p&gt;First install the packages necessary to compile the Linux kernel.  If you&apos;re on
Debian, type the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo aptitude install wget make git-core libncurses5-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you&apos;re on Ubuntu, use this command instead:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo aptitude install wget make git-core lib32ncurses5-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next, you&apos;ll need to add a script to install the Linux kernel.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir ~/bin/
cd ~/bin/
wget http://minilop.net/~sarah/installkernel
sudo chmod a+x installkernel
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A standard Debian or Ubuntu install should add the bin directory in your homedir
to your path automatically whenever you start a new terminal shell.  To make
sure you can run that script, close your current terminal, restart a new
terminal, and type&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;echo $PATH
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should see /home/username/bin in the output (replace username with whatever
username you picked).  You can also make sure the script is in your path by
typing &quot;installker&quot; and hitting tab.  If the command expands to &quot;installkernel&quot;,
then your path is set correctly and the script permissions are right.  If not,
follow the next instructions.&lt;/p&gt;

&lt;h2&gt;Using the installkernel script&lt;/h2&gt;

&lt;p&gt;Now add that script to your PATH in your rc file for your terminal shell of choice.
Bash is the default option, so edit (or create) the .bashrc file in your
home directory.  You can use vim, emacs, or even OpenOffice Writer to edit this
file.  If you must use OpenOffice Writer, make sure you save the file with the
name &quot;.bashrc&quot; with a Text encoded (txt) file type, and uncheck the &quot;Automatic
file name extension&quot; box.&lt;/p&gt;

&lt;p&gt;Add this line to your .bashrc:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;PATH=&quot;~/bin:$PATH&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you already have a line that starts with PATH, simply add the ~/bin: after
the first double quote.&lt;/p&gt;

&lt;h1&gt;Downloading a custom kernel&lt;/h1&gt;

&lt;p&gt;Most custom kernels are hosted on &lt;a
href=&quot;http://git.kernel.org&quot;&gt;git.kernel.org&lt;/a&gt;.  They are kept under revision
control using the VCS called &quot;git&quot;.  This means the kernels are updated often,
and you may need to use git to update to the latest version.  That&apos;s explained
later.&lt;/p&gt;

&lt;p&gt;In this tutorial, I&apos;ll assume
you want to download &lt;a
href=&quot;http://git.kernel.org/?p=linux/kernel/git/sarah/xhci.git;a=summary&quot;&gt;my
custom kernel&lt;/a&gt; to test USB 3.0 host controllers and devices.&lt;/p&gt;

&lt;p&gt;First, move to your homedirectory and &quot;clone&quot; the git repository so you have a
local copy checked out:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd
git clone git://git.kernel.org/pub/scm/linux/kernel/git/sarah/xhci.git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Downloading the kernel may take a while.  If you&apos;re behind a firewall, you may
need to set the $http_proxy environment variable (by adding a &quot;http_proxy=url&quot;
line to your .bashrc) and use http to clone the repository.  In this case,
replace the last command with&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone http://www.kernel.org/pub/scm/linux/kernel/git/sarah/xhci.git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should see output like &quot;Initialized empty Git repository in
/home/username/xhci/.git/&quot;.  If nothing else happens for a couple minutes after
that output, then you probably have firewall problems.&lt;/p&gt;

&lt;h1&gt;Choosing your kernel configuration&lt;/h1&gt;

&lt;p&gt;First, change directories into the newly created repository clone:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd xhci/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we need to choose the configuration options for the new kernel, and compile
it.  A configuration file describes your system configuration, and whether you
want drivers compiled into the kernel or if you want the drivers to be
dynamically loadable as a module.&lt;/p&gt;

&lt;p&gt;You can use the configuration file that the default distribution install
created by typing:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cp /boot/config-`uname -r` .config
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we need to change or add new configuration options:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;make menuconfig
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will present a text-based GUI of all the configuration options for the
Linux kernel.  This is everything from what file systems to support (like FAT or
EXT3) to what drivers to load (like SCSI, USB, or networking cards).  A driver
is built into the kernel if it has a star &quot;*&quot; by it.  The driver is a
dynamically loadable module if it has an &quot;M&quot; by it.  If the option has nothing
in the brackets, it won&apos;t be compiled.  If you want a driver to be a module,
just hit the &quot;m&quot; key.  If you don&apos;t want a driver, hit &quot;n&quot;; if you want to
compile a module into the kernel or say yes to a configuration option, hit &quot;y&quot;.&lt;/p&gt;

&lt;p&gt;To be able to use my USB 3.0 code, navigate to Device Drivers and onto the USB
support menu.  Make sure &quot;Support for Host-side USB&quot; is a dynamically loadable
module by moving the cursor to that item and hitting the &apos;m&apos; key.  You may get a
message that other drivers depend on host-side USB being built
into the kernel.  If so, you need to make sure all the drivers under this menu
with a star by them (&quot;*&quot;) are changed to be modules (with an &quot;M&quot;) by them.&lt;/p&gt;

&lt;p&gt;You need to change a few more things in this menu.  Choose to compile the xHCI
(USB 3.0) host controller driver as a module by hitting &quot;m&quot;.  Turn off (hit &quot;n&quot;)
on the &quot;USB selective suspend/resume and wakeup&quot; option, since that isn&apos;t
supported by the driver yet.  Turn on debugging for the USB subsystem by hitting
&quot;y&quot; on &quot;USB verbose debug messages&quot; and &quot;USB announce new devices&quot;.  Also turn
on the &quot;Debugging for the xHCI host controller&quot; option.&lt;/p&gt;

&lt;h1&gt;Compiling and installing your kernel&lt;/h1&gt;

&lt;p&gt;Now that you have your kernel configured, compile your kernel by typing&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;make -j4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This may take a while.  Go get a cup of tea or wander off for lunch.  Once that
compile finishes, you&apos;re ready to install your kernel by typing&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo make modules_install install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That will take a bit, but not as long as the compile.&lt;/p&gt;

&lt;h1&gt;Controlling when the xHCI driver is loaded&lt;/h1&gt;

&lt;p&gt;This next step is optional.  If you follow the above steps exactly, the xHCI
driver will be loaded automatically when you boot the new kernel.  It&apos;s much
easier for me to look at debug output from the driver if it&apos;s not interspersed
with boot code.  To do that, you should dynamically load the driver after the
machine finishes booting.&lt;/p&gt;

&lt;p&gt;First, we have to prevent the driver from being loaded automatically at boot
time by black-listing the module.  (Thanks to GotenXiao for the tip!)  Just
execute this command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;echo &quot;blacklist xhci&quot; &amp;gt; /etc/modprobe.d/xhci
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now whenever you want to load the xHCI driver, type the following (after you&apos;ve
rebooted into the new kernel):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo modprobe xhci
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When you want to unload the module, type this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo modprobe -r xhci
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is equivalent to enabling and then disabling the driver in the Windows
system manager.&lt;/p&gt;

&lt;h1&gt;Updating your kernel&lt;/h1&gt;

&lt;p&gt;If the kernel.org tree gets patches added to it, or it&apos;s updated to a newer
kernel, you&apos;ll need to update your local checkout.  There are a couple ways to
this, depending on if you have local changes to the files.  If you haven&apos;t
modified any files, it&apos;s safe to type these two commands from within your xhci
directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git fetch
git reset --hard origin/master
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This resets the local checkout to exactly what&apos;s in the remote repository.  If
you have local changes, you&apos;ll need to do something more complicated in order to
merge in or rebase against the remote repository changes.&lt;/p&gt;

&lt;p&gt;You&apos;ll need compile and install the new code using the steps above, and then
reboot.&lt;/p&gt;

&lt;h1&gt;Debugging&lt;/h1&gt;

&lt;p&gt;I suggest that when you run the xHCI driver, you have two tabs in your terminal
window open.  In one tab, redirect the kernel debugging messages to a file, and
display the output using the tee command (thanks to Mike for the tip!):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tail -f /var/log/kern.log | tee ~/xhci-log-`date +%Y-%m-%d-%H-%M`.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The date part adds the current year-month-day-hour-minute to the logfile name so
you don&apos;t overwrite an old log file.&lt;/p&gt;

&lt;p&gt;In the second tab, start the xHCI driver after 5 seconds.  The slight delay
allows you time to switch back to the first tab to watch the driver debug
messages.  In the second tab, type:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sleep 5 &amp;amp;&amp;amp; sudo modprobe xhci
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Send the logfile to me if you have issues.  You may need to use netconsole to
capture the kernel debug messages if your system is hanging because of a driver
bug.&lt;/p&gt;
</content>
</entry>
</feed>

