Thursday, May 28, 2015

MT7601 on Raspbmc

I brought myself a whole bunch of wifi dongles off aliexpress to sell though the condition on people buying a lot of them was reliant on them being suitable to use on Raspbmc.

Unfortunately they don't work straight out of the box and so I set about trying to get it working. On my desktop it was really easy (mostly).

 sudo apt-get install build-essential linux-headers dkms -y  
 wget http://ppa.launchpad.net/…/mt7601-sta-dkms_3.0.0.4-0~201502…  
 sudo dpkg -i mt7601-sta-dkms_3.0.0.4-0~201502051732~rev18~pkg2~ubuntu14.10.1_all.deb  

So the reason I didn't bother adding the ppa... Well... a dkms package is source code with some configuration files to make it recompile every time a kernel is installed. Which means, it should work regardless of what version of Ubuntu I'm no. Unfortunately, the ppa doesn't contain packages for 12.04 - which is no problem whatsoever. Just download the package and install...

Which means the dongles aren't completely useless. And on to Raspbmc. I started to look for information and was kind of disappointed by a lot of it. People had managed to compile it but hadn't left instructions as to how they managed it - instead providing binaries for particular version.

Others were running into much the same problems that I was and going around in circles not really getting answers. So I decided I needed a script. So the binary could be provided, for quick and easy installation, but also, giving others the ability to compile the binaries themselves should the need arise.

The REALLY neat thing about this script is that it sets up an environment that should be suitable for other modules as well. The first part of this script could be split off into kind of a universal "build an environment for compiling modules" thing...

And yes... I get the chicken and egg thing... You require the Internet in order to run this script... There's no way around this.

 #!/bin/bash  
   
 DRIVER_DOWNLOAD="http://cdn-cw.mediatek.com/Downloads/linux/mt7610u_wifi_sta_v3002_dpo_20130916.tar.bz2"  
 #Might not be needed but... if you don't have enough space on your SD card and you've got a mounted USB drive on the pi,  
 #this makes it use that instead... Comment out the next line if you just want to use pwd.  
 MOUNTPOINT="$( df | grep "^/dev/sd" | sed 's/ */ /g' | cut -f 5,6 -d " " | sort -h | head -n1 | cut -f 2 -d " " )"  
 HEADERS_DOWNLOAD="http://www.mirrorservice.org/sites/raspbmc.com/downloads/bin/kernel/linux-headers-latest.deb.gz"  
   
 CURRENT_KERNEL_VERSION="$( uname -r )"  
 DRIVER_FILENAME="$(basename $DRIVER_DOWNLOAD)"  
 if [ -z "$MOUNTPOINT" ] ; then  
  $MOUNTPOINT="."  
 fi  
   
 if [ "$( whoami )" != "root" ] ; then  
  echo "Must run as root"  
  exit 1  
 fi  
   
 #install the compiler (hopefully this is enough to get what you need).  
 if [ -z "$( dpkg -l | grep build-essential )" ] ; then  
  apt-get install build-essential -y  
 fi  
   
 if [ ! -d "$MOUNTPOINT/mt7601" ] ; then  
  mkdir "$MOUNTPOINT/mt7601"  
 fi  
 cd "$MOUNTPOINT/mt7601"  
   
 #download kernel source. It's just chucking into the current working dir for now... mainly because I'm excessively lazy  
 KERNEL_VERSION="rpi-$( echo $CURRENT_KERNEL_VERSION | cut -d. -f-1,2).y"  
 echo $KERNEL_VERSION  
 wget https://github.com/raspberrypi/linux/archive/"$KERNEL_VERSION".tar.gz  
 if [ ! -d src ] ; then  
  mkdir src  
 fi  
 tar xvf $KERNEL_VERSION.tar.gz -C src  
 KERNEL_DIR="$MOUNTPOINT/mt7601/src/linux-$KERNEL_VERSION"  
   
 #I know.. this next bit seems really dumb.... just for a single file.... Apparently these headers aren't suitable for
 #building wifi drivers BUT has the Module.symvers file that we need...
 if [ ! -d headers ] ; then  
  mkdir headers  
 fi  
 wget $HEADERS_DOWNLOAD -P headers  
 cd headers  
 HEADERS="$(basename "$HEADERS_DOWNLOAD")"  
 ar x "$HEADERS"  
 tar xvf data.tar.gz ./usr/src/linux-headers-$CURRENT_KERNEL_VERSION/Module.symvers  
 cd ..  
 mv headers/usr/src/linux-headers-$CURRENT_KERNEL_VERSION/Module.symvers "$KERNEL_DIR"  
   
 #Prepare...  
 rm "/lib/modules/$CURRENT_KERNEL_VERSION/build"  
 ln -sf "$KERNEL_DIR" "/lib/modules/$CURRENT_KERNEL_VERSION/build"  
 exit 0  
 cd "$MOUNTPOINT/mt7601/src/linux-$KERNEL_VERSION"  
 make mrproper  
 zcat /proc/config.gz | sed 's/CONFIG_CROSS_COMPILE.*/CONFIG_CROSS_COMPILE=""/' > .config  
 make modules_prepare  
   
 #now deal to the drivers... Change this part down if you want to use this script for other wireless drivers 
 cd "$MOUNTPOINT/mt7601"  
 wget $DRIVER_DOWNLOAD  
 tar xvf "$DRIVER_FILENAME" -P src  
 cd src/mt7601*  
 make  
  
 #Make a package so that you can you share the binary with others and to make it easy to remove if necessary.
 mkdir -p package/etc/Wireless/RT2870STA  
 cp RT2870STA.dat package/etc/Wireless/RT2870STA  
 mkdir -p package/lib/modules/$CURRENT_KERNEL_VERSION/kernel/drivers/net/wireless/  
 chmod 755 package/lib/modules/$CURRENT_KERNEL_VERSION/kernel/drivers/net/wireless/  
 cp os/linux/mt7601Usta.ko package/lib/modules/$CURRENT_KERNEL_VERSION/kernel/drivers/net/wireless/  
 chmod 755 package/lib/modules/$CURRENT_KERNEL_VERSION/kernel/drivers/net/wireless/  
 mkdir package/DEBIAN  
 echo '#!/bin/bash  
 depmod -a $CURRENT_KERNEL_VERSION' > package/DEBIAN/postinst  
 chmod 555 package/DEBIAN/postinst  
 echo "Package: mt7601$CURRENT_KERNEL_VERSION  
 Version: $(echo $DRIVER_FILENAME | egrep -o "([0-9]\.){3}[0-9]")  
 Architecture: armhf  
 Maintainer: script by Nevyn Hira <nevynh@gmail.com>  
 Section: admin  
 Priority: optional  
 Description: Ralink MT7601 binary drivers specifically for Raspbmc" > package/DEBIAN/control  
 dpkg -b package "$MOUNTPOINT/mt7601/"
 dpkg -i "$MOUNTPOINT/mt7601/package"

At the moment the script doesn't do any sort of clean up.

No comments:

Post a Comment