USB Card Readers

From LQWiki
Jump to navigation Jump to search

USB card readers are commonly available. They generally consist of a unit about the size of a Walkman cassette with several slots for different kinds of memory cards and an LED which flashes to indicate when data is being read or written. Internal card readers are also available, which fit into a 3.5in drive bay and connect to the front USB header on the motherboard, but if you already have USB ports on your front panel you will lose the use of them.

Such readers are invariably supplied with a driver CD for Windows and Macs, giving the impression that they are not usable with Linux. In fact, this is not at all the case. The Linux kernel has included USB support ever since version 2.4, and the USB drivers have been back-ported to Linux 2.2. Devices such as memory card readers, USB Flash drives, and digital cameras are all treated as mass storage devices and use a common protocol which emulates a SCSI disc device.

If you are using a stock kernel you will almost certainly have the necessary modules compiled. I will assume for now that if you know enough to compile your own kernel, then you will be able to sort out any problems with missing modules.

Begin by opening a console and becoming root. Once you have successfully mounted your reader, you can set it up so that ordinary users can mount it.

Type

ls /dev/sd*

Make sure that some devices are listed (they are devices, not regular files, so will be listed in yellow if you have set up coloured directory listings and not modified ~/.dir_colors). For each drive letter such as sda, there will also be several partitions: sda1, sda2 and so on.

Now plug in your reader and enter

dmesg |less

Look for lines that say something like

Attached scsi removable disk sda at scsi1, channel 0, id 0, lun 0

(there will be one for each slot, each with a different drive letter; for example, I see sda, sdb, sdc and sdd when using my four-slot reader). If instead you get a message like

USB device 2 is not claimed by any active driver

then you probably just need to load the relevant kernel module. Enter

modprobe usb-storage

to load it now -- and then update /etc/modules to force the module to be loaded the next time the computer boots. Just add a line with usb-storage on it. (Debian users use modconf rather than hand-editing /etc/modules.)

Make mount points for the memory cards you are going to use, for example /mnt/sd and /mnt/smartmedia.

mkdir /mnt/sd
mkdir /mnt/smartmedia

Insert a memory card. Now begins a little trial-and-error; we know what the drive letters are from dmesg, but not which one is which. The easiest way to find out is simply to try mounting drive letters one at a time, until we find the one with the files on the card -- then we know we have mounted it.

Enter

mount -tvfat -oro /dev/sda1 /mnt/sd

(assuming it was an sd card you inserted). This translates as: mount a vfat (= Windows 95)-type file system, read only, located on the 1st partition of the drive sda, under /mnt/sd. (Specifying -tmsdos will appear to work, but it restricts filenames to 8+3 characters and may cause problems.)

If the mount operation is successful, then congratulations - you have found that sda is the sd slot! Try listing the files with ls /mnt/sd/. If the mount fails, you will get an error message; just try the next drive letter until you find the right one.

Once you have successfully mounted the memory card, you need to unmount it before you can remove the card. So enter

umount /mnt/sd

Repeat the process for each type of memory card you have.

Now you should have a list of which drive letter relates to which card slot. All that remains to do is create an entry in /etc/fstab to allow any ordinary user to mount them. For example, with my reader, the SmartMedia slot is sdc and the SD card slot is sdd, so I added these lines to my fstab:

/dev/sdc1 /mnt/smartmedia vfat user,noauto,noexec,umask=022
/dev/sdd1 /mnt/sd vfat user,noauto,noexec,umask=022

The fields on the line can be separated by spaces or tabs. The first column is the device or partition to be mounted. The second column is the target mountpoint. The third column is the file system type, and the fourth is a comma-separated string of options. user allows any user to mount the device. noauto prevents automatic mounting at boot time, in case there is no card in the slot. noexec means don't allow files beneath the mountpoint to be executed, and umask=022 sets the default file permissions to exclude group and world write.

Once /etc/fstab is modified, any user should be able to mount a card slot with a command such as

mount /mnt/smartmedia

This shorthand form looks up all the missing information from /etc/fstab.

When you are done with the card reader, be sure to unmount it before you remove the card, with the command.

umount /mnt/smartmedia

Wait for the light to stop flashing before you remove the card. If you have written many files, this may take ages, but it is perfectly normal; Linux normally caches all disk writes in spare RAM, and only commits the changes when it has to: usually when the device is unmounted, the sync command is issued or the memory is required by another process.

External links