Thursday, 31 July 2014

Going Wireless on Raspbmc with MT7601

$35 for a PC, that's Raspberry Pi. You can hook it to a monitor, speakers, USB peripherals as well as the Internet; there are pretty lots of ways you can use it for. I am using it for a menial task though - as a HTPC in my living room.

Raspberry Pi Model B
Raspberry Pi Model B

Raspberry Pi runs on Linux, which is a scary OS for the majority of computer users (including me) who are unfamiliar with it. For me, the biggest challenge was installing the driver for my USB wi-fi dongle, which is not supported by Raspbian out-of-the-box. It took me a weekend of googling and hours of trial and error to finally get it working. (There were numerous times when I almost threw the dongle out of the window because of frustration.) Therefore, I am writing this post for my future reference. Perhaps it might also benefit any other readers are having trouble setting up their wi-fi dongle.

TP-Link TL-WN727N
TL-WN727N - The wi-fi dongle that I almost threw out of the window

Step 1: Boot into Commandline Mode
Before getting started, connect the Raspberry Pi to a keyboard, a monitor and the Internet through the ethernet port. There are other ways to access the Raspberry Pi (eg. via direct connection) but they are beyond the scope of this post.

After Raspbmc loads, click on the power icon at the bottom-left corner of the screen. Then select Exit. While Raspbmc is exiting, press Esc key to go into commandline mode.

Step 2: Verify Kernel Version and Wi-Fi Dongle Chipset
To download the appropriate source code, you will have to find out the kernel version and the model of the wi-fi dongle chipset.
uname -r
lsusb

This should return the kernel version (3.12.21 for my case), and Bus 001 Device 004: ID 148f:7601 Ralink Technology, Corp. if your dongle is using MT7601 chipset.

Step 3: Download and Prepare Kernel Source Kernel Source
Download kernel source and module symbols to the Raspberry Pi using wget command. Place your downloads in any desired directory. Here, the kernel source is downloaded to /home/pi/kernel for clarity purposes.
mkdir /home/pi/kernel
cd /home/pi/kernel
#Download kernel source
wget https://googledrive.com/host/0BzPG3h6ewHsLeG5SQWJfS2xZWWM/rpi-3.12.21.tar.gz
#Unpack kernel source
tar -xzf rpi-3.12.21.tar.gz
#[Optional] Remove tar after unpacking
rm rpi-3.10.21.tar.gz
#Go into unpacked folder
cd linux-rpi*
#Create files required for compiling external modules
make mrproper
zcat /proc/config.gz > .config
cp .config .config.org
sed -i 's/^CONFIG_CROSS_COMPILE.*/CONFIG_CROSS_COMPILE=""/' .config
make modules_prepare
#Download module symbols
pi@raspbmc:~$ wget https://googledrive.com/host/0BzPG3h6ewHsLeG5SQWJfS2xZWWM/Module.symvers

Step 4: Download and Prepare Driver Source
Download driver source to the Raspberry Pi using wget command. Place your downloads in any desired directory. Here, the kernel source is downloaded to /home/pi/driver for clarity purposes.
cd ../..
#Driver source (version 3.0.0.4) downloaded from Mediatek
wget https://googledrive.com/host/0BzPG3h6ewHsLeG5SQWJfS2xZWWM/DPO_MT7601U_LinuxSTA_3.0.0.4_20130913.tar.bz2
tar -xvjpf DPO_MT7601*
rm DPO_MT7601U_LinuxSTA_3.0.0.4_20130913.tar.bz2

Step 5: Compile and Install Driver
#Go into the unpacked folder and edit Makefile
cd DPO*
sudo Makefile

Look for the line:
LINUX_SRC = /lib/modules/$(shell uname -r)/build

Change it to:
LINUX_SRC = /tmp/kernel/linux-rpi-3.12.21

You are finally ready to compile the driver.
#Compile driver
make all
#Copy files to relevant directories and deploy module
sudo mkdir -p /etc/Wireless/RT2870STA
sudo cp RT2870STA.dat /etc/Wireless/RT2870STA
sudo cp os/linux/mt7601Usta.ko /lib/modules/3.12.21/kernel/drivers/net/wireless/
depmod -a

If you are compiling from driver version 3.0.0.3, there will be two more files in addition to mt7601Usta.ko, namely
mtutil7601Usta.ko and mtnet7601Usta.ko.

To make sure the module has been deployed correctly, enter the following command.
lsmod

If you see mt7601Usta in the list, congratulations. Your driver has been successfully installed.

Step 5: Change ra0 to wlan0
The interface name for your wi-fi dongle should be ra0. However, if you type in ifconfig, you will probably only see l0 and wlan0 listed. This is because ra0 is not recognized by Raspbmc.

To change the name of the interface, do the following:
sudo nano /etc/udev/rules.d/95-ralink.rules

Add the following line to the file:
SUBSYSTEM=="net", ACTION=="add", ATTR{type}=="1", KERNEL=="ra*", NAME="wlan0"

Step 6: Configure Wi-Fi SSID and Password
To set up your wifi SSID and password, you will need to edit the /etc/network/interfaces file.
sudo nano /etc/network/interfaces

This file is probably empty. Type in the following lines:
allow-hotplug wlan0
iface wlan0 inet wlan0
wpa-ssid YOUR_WIFI_SSID
wpa-psk YOUR_WIFI_PASSWORD
wireless-power off
up iwpriv $IFACE set Debug=0

Step 6: Reboot
Finally, reboot your Raspberry Pi! Your wi-fi dongle should be working now!
sudo reboot

Sources:
HSMM Mesh on Raspberry Pi
STM Labs Forum - Help Compile Driver for MT7601 Wi-Fi Dongle (Post 12)
Setting up a MediaTek Ralink WiFi Adapter in Raspbmc 3.12.21-Raspberry PI [mt7601]
Change ra0 to wlan0 in Ubuntu 10.10

0 persons flung their shoes: