Friday, 3 September 2010

SN9C202 web camera's (Microdia type)

These little beasties are pretty hard to get working in Ubuntu 10.04 LTS but here is a step by step walk through to get it working within minutes :) just type the highlighted commands in a terminal session....

firstly identify you have an SN9C202 or similar camera by using 

lsusb


which should return something like this 

Bus 002 Device 002: ID 045e:0040 Microsoft Corp. Wheel Mouse Optical
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 004: ID 0c45:628f Microdia PC Camera with Microphone (SN9C202 + OV9650)
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Once you have indeed confirmed you have the offending camera follow the steps below.....


firstly install Git with the following command line

sudo apt-get install git-core

1. Download the source code

From a command-line prompt, cd to a directory where you want to download it, default "home" will do then run the command:

git clone http://repo.or.cz/r/microdia.git

This will create a folder named "microdia" which contains all the source code.



Sometimes the above command doesn't work. In such cases please try this one:

git clone http://repo.or.cz/microdia.git

2. Install bare minimum packages

To be able to compile the driver, you must have kernel sources and the necessary tools to compile it. For most applications you will also need libv4l.

Debian/Ubuntu

sudo apt-get install kernel-package linux-headers build-essential ctags libv4l


(this might take a while depending on how fast your connection is)

Note: linux-source is not necessary, rather linux-headers with the same version of your kernel will install /lib/modules//build.

Attention: Do _NOT_ under any circumstances use "sudo make" or "make". There are no root privileges necessary at this point and using them causes a never ending chain of different problems later on.

3. Setting up the compilation environment

The linux kernel needs to be locally compiled in order to compile the microdia driver.

If the kernel is not compiled, "make" will complain of "modpost" missing.

The Makefile expects the source of linux to be located in /lib/modules/$(KVER)/build, if this is not the case edit the Makefile to set KSRC to point to the linux source location.
 

4. Building Microdia driver from source

After installing Git (command line to get git shown above), you need to download microdia kernel driver source. After installing Kernel packages and necessary tools to compile it, you will be able to compile it by typing:

cd microdia
make

Attention: Do _NOT_ under any circumstances use "sudo make" or "make". There are no root privileges necessary at this point and using them causes a never ending chain of different problems later on.

Troubleshooting MAKE errors

If you get the following error:
make: *** [driver] Error 127
Error 127 simply means that the module is not in the proper location. This is not a major error.

5. Loading the driver

sudo insmod ./sn9c20x.ko

If everything works fine you won't see any message on stdout, but your dmesg will have lines like the following:

sn9c20x: SN9C20X USB 2.0 webcam driver loaded
sn9c20x: SN9C20X USB 2.0 Webcam - XXXX:XXXX plugged-in.
sn9c20x: Detected XXX Sensor
sn9c20x: SN9C20X USB 2.0 Webcam is now controlling video device /dev/video0
usbcore: registered new interface driver usb_sn9c20x_driver
sn9c20x: v2008.10 : SN9C20X USB 2.0 Webcam Driver


Troubleshooting insmod errors

insmod sn9c20x.ko
insmod: error inserting 'sn9c20x.ko': -1 File exists (which is what I experienced)

means a similar module is was already loaded into the kernel and needs to be removed first. To find out which model that is, do:

lsmod | grep sn9c20x

you should see something like this

 gspca_sn9c20x 31616  0
 gspca_main 26816  1 gspca_sn9c20x

From which you can then figure out the module name (gspca_sn9c20x in my case) and replace it with the new module as shown below


sudo rmmod gspca_sn9c20x
sudo insmod snc9c20x.ko


Make sure you type all of this from within the git cloned microdia directory or else it will simply bomb on you. You can now check to see if the module is loaded:

lsmod | grep sn9c20x

and you should see the driver installed.....


Update the dependencies for every module.

sudo depmod -a
sudo m-a update,prepare

if the sudo m-a update,prepare fails you'll need to run the following and repeat the command line above

sudo apt-get install module-assistant

then

sudo m-a update,prepare



FIXME: more possibilities
6. Test it using Skype or the command line shown below
Please note that the test requires libv4l for video decoding. mplayer does not support JPEG compression on its own.

LD_PRELOAD=/usr/lib/libv4l/v4l2convert.so mplayer tv:// \
    -tv driver=v4l2:width=640:height=480:fps=25:device=/dev/video0 -vo x11


7. Installing the Microdia Driver

Once you're happy that it's working as expected ....

open a terminal session and put the following commands in to weld it into the operating system :)

sudo strip -g sn9c20x.ko
sudo mkdir -p /lib/modules/`uname -r`/kernel/drivers/media/video/usbvideo/
sudo cp sn9c20x.ko /lib/modules/`uname -r`/kernel/drivers/media/video/usbvideo/
sudo depmod -a

Just cut and paste the above ... you might need to press the enter key for the last command to execute


voila .... working webcam :)

7 comments:

Anonymous said...

Thanks for posting this guide.
Btw I followed all the steps, and at the end I get no errors, but when I issue the mplayer command, it shows only a green screen with some vertical lines flashing on the top of the window.
Please, could you help me to make my webcam working? I guess I am very close to the solution.

My webcam as reported by lsusb: ID 0c45:62b3 Microdia PC Camera with Microphone (SN9C202 + OV9655)

Thank you
Andrea

Anonymous said...

Thanks for posting this guide.
Btw I followed all the steps, and at the end I get no errors, but when I issue the mplayer command, it shows only a green screen with some vertical lines flashing on the top of the window.
Please, could you help me to make my webcam working? I guess I am very close to the solution.

My webcam, as reported by lsusb: ID 0c45:62b3 Microdia PC Camera with Microphone (SN9C202 + OV9655)
OS: Ubuntu 10.04 64bit

Thank you
Andrea

Digamy said...

Hi Andrea,
I think you'll have problems with the 64 bit version of Ubuntu... I initially tried to get this camera working with the 64 bit version and failed miserably, I don't know why it didn't work..... have you tried using cheese to view your webcam ? or gucview ?

Anonymous said...

Hello Digamy! Thank you for your reply, that pushed me in the right direction: I tried guvcview and it works!
Actually, I still have some "minor" problems:
- image is clipped to a 480x480 square, so I loose a slice on the right;
- colors are not rendered correctly, so it's like a monocrhome image with a color dominant;
- skype doesn't recognize it
...but it works, captures images and videos (with audio), and that's nice for me.

I already tried cheese yesterday, and it crashed, but now I have tried it again and it works (!) (?), despite it has the same limits of guvcview and additionally:
- it doesn't work in 640x480 mode, it works only in 320x240 and 160x120 modes;
- it shows a "noise" strip in the bottom, some ten pixels thick

Anyway, thank you again, I hope these additional informations could help someone else one day

Cheers,
Andrea

Anonymous said...

EDIT: the mplayer command works with 320x240 settings, although the output is nearly completely saturated

Andrea

Digamy said...

I have to admit it's a bit flaky even under 32bit Ubuntu... but it does work with skype... at first I could only get it to use the usb microphone part, I found that if I plugged in the camera AFTER skype was loaded it worked !! but it wouldn't do it every time... so not ideal... it just needs a bit of tinkering to get it working properly.... but well done you for getting "something" out of it under 64bit !!! :)

you'll find if you set the camera up under gucview and turn off auto exposure quit gucview THEN load skype it should work

Anonymous said...

Very useful instructions, i was struggling with it. the problem was it was loading the wrong driver and i had to follow the
sudo rmmod gspca_sn9c20x
sudo insmod snc9c20x.ko

i have tried to install the new one permanently but its failing so i had to write a small script to run skype

sudo rmmod gspca_sn9c20x
cd microdia
sudo insmod ./sn9c20x.ko
LD_PRELOAD=/usr/lib/libv4l/v4l2convert.so skype

those who face problem with skype video may try using the last line