Adam's "Blog"

That's all, really.

Switching the microSD Card Between SoC and USB on a Zsun Wifi Card Reader Running OpenWRT

When I wrote my previous post about installing OpenWRT on the Zsun Wifi card reader, I didn’t know how to switch the SD card from being controlled by OpenWRT to being connected directly to the USB port for use by a different device. The Hackerspace page said:

The device contains a WAS7227Q USB switch, which connects the sd card reader chip to either the USB plug, or the AR9331 SoC. The switch is controlled with GPIO21. Set pin to LOW to connect card reader to SoC.

I thought GPIOs were pins you’d have to connect to in order to change this. But from this PirateBox post I learned that they can easily be changed from software under Linux:

echo 1 /sys/devices/virtual/gpio/gpio21/value

toggles the sdcard for use as a thumbdrive in a computer to transfer files
0=attached to zsun
1=attached as usb drive

And it’s just that easy. Setting it to 1 makes it disappear from the list of available USB devices:

root@MicroWrt:~# lsusb
Bus 001 Device 002: ID 05e3:0723 Genesys Logic, Inc. GL827L SD/MMC/MS Flash Card Reader
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
root@MicroWrt:~# echo 1 > /sys/devices/virtual/gpio/gpio21/value
root@MicroWrt:~# lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
root@MicroWrt:~#

And logs a message about its removal:

Fri Apr 15 16:34:54 2016 kern.info kernel: [  297.010000] usb 1-1: USB disconnect, device number 2

And setting it to 0 again brings it back:

Fri Apr 15 16:37:45 2016 kern.info kernel: [  467.980000] usb 1-1: new high-speed USB device number 3 using ehci-platform
Fri Apr 15 16:37:45 2016 kern.info kernel: [  468.130000] usb-storage 1-1:1.0: USB Mass Storage device detected
Fri Apr 15 16:37:45 2016 kern.info kernel: [  468.150000] scsi host1: usb-storage 1-1:1.0
Fri Apr 15 16:37:46 2016 kern.notice kernel: [  469.150000] scsi 1:0:0:0: Direct-Access     Generic  STORAGE DEVICE   9454 PQ: 0 ANSI: 0
Fri Apr 15 16:37:46 2016 kern.notice kernel: [  469.210000] sd 1:0:0:0: [sda] 121856 512-byte logical blocks: (62.3 MB/59.5 MiB)
Fri Apr 15 16:37:46 2016 kern.notice kernel: [  469.220000] sd 1:0:0:0: [sda] Write Protect is off
Fri Apr 15 16:37:46 2016 kern.debug kernel: [  469.220000] sd 1:0:0:0: [sda] Mode Sense: 03 00 00 00
Fri Apr 15 16:37:46 2016 kern.err kernel: [  469.230000] sd 1:0:0:0: [sda] No Caching mode page found
Fri Apr 15 16:37:46 2016 kern.err kernel: [  469.230000] sd 1:0:0:0: [sda] Assuming drive cache: write through
Fri Apr 15 16:37:46 2016 kern.info kernel: [  469.240000]  sda: sda1
Fri Apr 15 16:37:46 2016 kern.notice kernel: [  469.250000] sd 1:0:0:0: [sda] Attached SCSI removable disk

So it can be mounted under OpenWRT again:

root@MicroWrt:~# mount /dev/sda1 /mnt
root@MicroWrt:~# ls /mnt
Test from Windows.txt   Test.txt                test from microwrt.txt
root@MicroWrt:~# cat /mnt/Test\ from\ Windows.txt
All I had to do was: echo 1 > /sys/devices/virtual/gpio/gpio21/value
And bam, it's a drive.
Nice!root@MicroWrt:~# umount /mnt
root@MicroWrt:~#