sn9c102.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. SN9C10x PC Camera Controllers
  2. Driver for Linux
  3. =============================
  4. - Documentation -
  5. Index
  6. =====
  7. 1. Copyright
  8. 2. Disclaimer
  9. 3. License
  10. 4. Overview and features
  11. 5. Module dependencies
  12. 6. Module loading
  13. 7. Module parameters
  14. 8. Optional device control through "sysfs"
  15. 9. Supported devices
  16. 10. Notes for V4L2 application developers
  17. 11. Video frame formats
  18. 12. Contact information
  19. 13. Credits
  20. 1. Copyright
  21. ============
  22. Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it>
  23. 2. Disclaimer
  24. =============
  25. SONiX is a trademark of SONiX Technology Company Limited, inc.
  26. This software is not sponsored or developed by SONiX.
  27. 3. License
  28. ==========
  29. This program is free software; you can redistribute it and/or modify
  30. it under the terms of the GNU General Public License as published by
  31. the Free Software Foundation; either version 2 of the License, or
  32. (at your option) any later version.
  33. This program is distributed in the hope that it will be useful,
  34. but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. GNU General Public License for more details.
  37. You should have received a copy of the GNU General Public License
  38. along with this program; if not, write to the Free Software
  39. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  40. 4. Overview and features
  41. ========================
  42. This driver attempts to support the video interface of the devices mounting the
  43. SONiX SN9C101, SN9C102 and SN9C103 PC Camera Controllers.
  44. It's worth to note that SONiX has never collaborated with the author during the
  45. development of this project, despite several requests for enough detailed
  46. specifications of the register tables, compression engine and video data format
  47. of the above chips. Nevertheless, these informations are no longer necessary,
  48. because all the aspects related to these chips are known and have been
  49. described in detail in this documentation.
  50. The driver relies on the Video4Linux2 and USB core modules. It has been
  51. designed to run properly on SMP systems as well.
  52. The latest version of the SN9C10x driver can be found at the following URL:
  53. http://www.linux-projects.org/
  54. Some of the features of the driver are:
  55. - full compliance with the Video4Linux2 API (see also "Notes for V4L2
  56. application developers" paragraph);
  57. - available mmap or read/poll methods for video streaming through isochronous
  58. data transfers;
  59. - automatic detection of image sensor;
  60. - support for built-in microphone interface;
  61. - support for any window resolutions and optional panning within the maximum
  62. pixel area of image sensor;
  63. - image downscaling with arbitrary scaling factors from 1, 2 and 4 in both
  64. directions (see "Notes for V4L2 application developers" paragraph);
  65. - two different video formats for uncompressed or compressed data in low or
  66. high compression quality (see also "Notes for V4L2 application developers"
  67. and "Video frame formats" paragraphs);
  68. - full support for the capabilities of many of the possible image sensors that
  69. can be connected to the SN9C10x bridges, including, for instance, red, green,
  70. blue and global gain adjustments and exposure (see "Supported devices"
  71. paragraph for details);
  72. - use of default color settings for sunlight conditions;
  73. - dynamic I/O interface for both SN9C10x and image sensor control and
  74. monitoring (see "Optional device control through 'sysfs'" paragraph);
  75. - dynamic driver control thanks to various module parameters (see "Module
  76. parameters" paragraph);
  77. - up to 64 cameras can be handled at the same time; they can be connected and
  78. disconnected from the host many times without turning off the computer, if
  79. the system supports hotplugging;
  80. - no known bugs.
  81. 5. Module dependencies
  82. ======================
  83. For it to work properly, the driver needs kernel support for Video4Linux and
  84. USB.
  85. The following options of the kernel configuration file must be enabled and
  86. corresponding modules must be compiled:
  87. # Multimedia devices
  88. #
  89. CONFIG_VIDEO_DEV=m
  90. To enable advanced debugging functionality on the device through /sysfs:
  91. # Multimedia devices
  92. #
  93. CONFIG_VIDEO_ADV_DEBUG=y
  94. # USB support
  95. #
  96. CONFIG_USB=m
  97. In addition, depending on the hardware being used, the modules below are
  98. necessary:
  99. # USB Host Controller Drivers
  100. #
  101. CONFIG_USB_EHCI_HCD=m
  102. CONFIG_USB_UHCI_HCD=m
  103. CONFIG_USB_OHCI_HCD=m
  104. The SN9C103 controller also provides a built-in microphone interface. It is
  105. supported by the USB Audio driver thanks to the ALSA API:
  106. # Sound
  107. #
  108. CONFIG_SOUND=y
  109. # Advanced Linux Sound Architecture
  110. #
  111. CONFIG_SND=m
  112. # USB devices
  113. #
  114. CONFIG_SND_USB_AUDIO=m
  115. And finally:
  116. # USB Multimedia devices
  117. #
  118. CONFIG_USB_SN9C102=m
  119. 6. Module loading
  120. =================
  121. To use the driver, it is necessary to load the "sn9c102" module into memory
  122. after every other module required: "videodev", "usbcore" and, depending on
  123. the USB host controller you have, "ehci-hcd", "uhci-hcd" or "ohci-hcd".
  124. Loading can be done as shown below:
  125. [root@localhost home]# modprobe sn9c102
  126. At this point the devices should be recognized. You can invoke "dmesg" to
  127. analyze kernel messages and verify that the loading process has gone well:
  128. [user@localhost home]$ dmesg
  129. 7. Module parameters
  130. ====================
  131. Module parameters are listed below:
  132. -------------------------------------------------------------------------------
  133. Name: video_nr
  134. Type: short array (min = 0, max = 64)
  135. Syntax: <-1|n[,...]>
  136. Description: Specify V4L2 minor mode number:
  137. -1 = use next available
  138. n = use minor number n
  139. You can specify up to 64 cameras this way.
  140. For example:
  141. video_nr=-1,2,-1 would assign minor number 2 to the second
  142. recognized camera and use auto for the first one and for every
  143. other camera.
  144. Default: -1
  145. -------------------------------------------------------------------------------
  146. Name: force_munmap
  147. Type: bool array (min = 0, max = 64)
  148. Syntax: <0|1[,...]>
  149. Description: Force the application to unmap previously mapped buffer memory
  150. before calling any VIDIOC_S_CROP or VIDIOC_S_FMT ioctl's. Not
  151. all the applications support this feature. This parameter is
  152. specific for each detected camera.
  153. 0 = do not force memory unmapping
  154. 1 = force memory unmapping (save memory)
  155. Default: 0
  156. -------------------------------------------------------------------------------
  157. Name: frame_timeout
  158. Type: uint array (min = 0, max = 64)
  159. Syntax: <n[,...]>
  160. Description: Timeout for a video frame in seconds. This parameter is
  161. specific for each detected camera. This parameter can be
  162. changed at runtime thanks to the /sys filesystem interface.
  163. Default: 2
  164. -------------------------------------------------------------------------------
  165. Name: debug
  166. Type: ushort
  167. Syntax: <n>
  168. Description: Debugging information level, from 0 to 3:
  169. 0 = none (use carefully)
  170. 1 = critical errors
  171. 2 = significant informations
  172. 3 = more verbose messages
  173. Level 3 is useful for testing only, when only one device
  174. is used. It also shows some more informations about the
  175. hardware being detected. This parameter can be changed at
  176. runtime thanks to the /sys filesystem interface.
  177. Default: 2
  178. -------------------------------------------------------------------------------
  179. 8. Optional device control through "sysfs" [1]
  180. ==========================================
  181. If the kernel has been compiled with the CONFIG_VIDEO_ADV_DEBUG option enabled,
  182. it is possible to read and write both the SN9C10x and the image sensor
  183. registers by using the "sysfs" filesystem interface.
  184. Every time a supported device is recognized, a write-only file named "green" is
  185. created in the /sys/class/video4linux/videoX directory. You can set the green
  186. channel's gain by writing the desired value to it. The value may range from 0
  187. to 15 for SN9C101 or SN9C102 bridges, from 0 to 127 for SN9C103 bridges.
  188. Similarly, only for SN9C103 controllers, blue and red gain control files are
  189. available in the same directory, for which accepted values may range from 0 to
  190. 127.
  191. There are other four entries in the directory above for each registered camera:
  192. "reg", "val", "i2c_reg" and "i2c_val". The first two files control the
  193. SN9C10x bridge, while the other two control the sensor chip. "reg" and
  194. "i2c_reg" hold the values of the current register index where the following
  195. reading/writing operations are addressed at through "val" and "i2c_val". Their
  196. use is not intended for end-users. Note that "i2c_reg" and "i2c_val" will not
  197. be created if the sensor does not actually support the standard I2C protocol or
  198. its registers are not 8-bit long. Also, remember that you must be logged in as
  199. root before writing to them.
  200. As an example, suppose we were to want to read the value contained in the
  201. register number 1 of the sensor register table - which is usually the product
  202. identifier - of the camera registered as "/dev/video0":
  203. [root@localhost #] cd /sys/class/video4linux/video0
  204. [root@localhost #] echo 1 > i2c_reg
  205. [root@localhost #] cat i2c_val
  206. Note that "cat" will fail if sensor registers cannot be read.
  207. Now let's set the green gain's register of the SN9C101 or SN9C102 chips to 2:
  208. [root@localhost #] echo 0x11 > reg
  209. [root@localhost #] echo 2 > val
  210. Note that the SN9C10x always returns 0 when some of its registers are read.
  211. To avoid race conditions, all the I/O accesses to the above files are
  212. serialized.
  213. The sysfs interface also provides the "frame_header" entry, which exports the
  214. frame header of the most recent requested and captured video frame. The header
  215. is always 18-bytes long and is appended to every video frame by the SN9C10x
  216. controllers. As an example, this additional information can be used by the user
  217. application for implementing auto-exposure features via software.
  218. The following table describes the frame header:
  219. Byte # Value Description
  220. ------ ----- -----------
  221. 0x00 0xFF Frame synchronisation pattern.
  222. 0x01 0xFF Frame synchronisation pattern.
  223. 0x02 0x00 Frame synchronisation pattern.
  224. 0x03 0xC4 Frame synchronisation pattern.
  225. 0x04 0xC4 Frame synchronisation pattern.
  226. 0x05 0x96 Frame synchronisation pattern.
  227. 0x06 0xXX Unknown meaning. The exact value depends on the chip;
  228. possible values are 0x00, 0x01 and 0x20.
  229. 0x07 0xXX Variable value, whose bits are ff00uzzc, where ff is a
  230. frame counter, u is unknown, zz is a size indicator
  231. (00 = VGA, 01 = SIF, 10 = QSIF) and c stands for
  232. "compression enabled" (1 = yes, 0 = no).
  233. 0x08 0xXX Brightness sum inside Auto-Exposure area (low-byte).
  234. 0x09 0xXX Brightness sum inside Auto-Exposure area (high-byte).
  235. For a pure white image, this number will be equal to 500
  236. times the area of the specified AE area. For images
  237. that are not pure white, the value scales down according
  238. to relative whiteness.
  239. 0x0A 0xXX Brightness sum outside Auto-Exposure area (low-byte).
  240. 0x0B 0xXX Brightness sum outside Auto-Exposure area (high-byte).
  241. For a pure white image, this number will be equal to 125
  242. times the area outside of the specified AE area. For
  243. images that are not pure white, the value scales down
  244. according to relative whiteness.
  245. according to relative whiteness.
  246. The following bytes are used by the SN9C103 bridge only:
  247. 0x0C 0xXX Unknown meaning
  248. 0x0D 0xXX Unknown meaning
  249. 0x0E 0xXX Unknown meaning
  250. 0x0F 0xXX Unknown meaning
  251. 0x10 0xXX Unknown meaning
  252. 0x11 0xXX Unknown meaning
  253. The AE area (sx, sy, ex, ey) in the active window can be set by programming the
  254. registers 0x1c, 0x1d, 0x1e and 0x1f of the SN9C10x controllers, where one unit
  255. corresponds to 32 pixels.
  256. [1] Part of the meaning of the frame header has been documented by Bertrik
  257. Sikken.
  258. 9. Supported devices
  259. ====================
  260. None of the names of the companies as well as their products will be mentioned
  261. here. They have never collaborated with the author, so no advertising.
  262. From the point of view of a driver, what unambiguously identify a device are
  263. its vendor and product USB identifiers. Below is a list of known identifiers of
  264. devices mounting the SN9C10x PC camera controllers:
  265. Vendor ID Product ID
  266. --------- ----------
  267. 0x0c45 0x6001
  268. 0x0c45 0x6005
  269. 0x0c45 0x6007
  270. 0x0c45 0x6009
  271. 0x0c45 0x600d
  272. 0x0c45 0x6024
  273. 0x0c45 0x6025
  274. 0x0c45 0x6028
  275. 0x0c45 0x6029
  276. 0x0c45 0x602a
  277. 0x0c45 0x602b
  278. 0x0c45 0x602c
  279. 0x0c45 0x602d
  280. 0x0c45 0x602e
  281. 0x0c45 0x6030
  282. 0x0c45 0x6080
  283. 0x0c45 0x6082
  284. 0x0c45 0x6083
  285. 0x0c45 0x6088
  286. 0x0c45 0x608a
  287. 0x0c45 0x608b
  288. 0x0c45 0x608c
  289. 0x0c45 0x608e
  290. 0x0c45 0x608f
  291. 0x0c45 0x60a0
  292. 0x0c45 0x60a2
  293. 0x0c45 0x60a3
  294. 0x0c45 0x60a8
  295. 0x0c45 0x60aa
  296. 0x0c45 0x60ab
  297. 0x0c45 0x60ac
  298. 0x0c45 0x60ae
  299. 0x0c45 0x60af
  300. 0x0c45 0x60b0
  301. 0x0c45 0x60b2
  302. 0x0c45 0x60b3
  303. 0x0c45 0x60b8
  304. 0x0c45 0x60ba
  305. 0x0c45 0x60bb
  306. 0x0c45 0x60bc
  307. 0x0c45 0x60be
  308. The list above does not imply that all those devices work with this driver: up
  309. until now only the ones that mount the following image sensors are supported;
  310. kernel messages will always tell you whether this is the case:
  311. Model Manufacturer
  312. ----- ------------
  313. HV7131D Hynix Semiconductor, Inc.
  314. MI-0343 Micron Technology, Inc.
  315. OV7630 OmniVision Technologies, Inc.
  316. PAS106B PixArt Imaging, Inc.
  317. PAS202BCA PixArt Imaging, Inc.
  318. PAS202BCB PixArt Imaging, Inc.
  319. TAS5110C1B Taiwan Advanced Sensor Corporation
  320. TAS5130D1B Taiwan Advanced Sensor Corporation
  321. All the available control settings of each image sensor are supported through
  322. the V4L2 interface.
  323. Donations of new models for further testing and support would be much
  324. appreciated. Non-available hardware will not be supported by the author of this
  325. driver.
  326. 10. Notes for V4L2 application developers
  327. =========================================
  328. This driver follows the V4L2 API specifications. In particular, it enforces two
  329. rules:
  330. - exactly one I/O method, either "mmap" or "read", is associated with each
  331. file descriptor. Once it is selected, the application must close and reopen the
  332. device to switch to the other I/O method;
  333. - although it is not mandatory, previously mapped buffer memory should always
  334. be unmapped before calling any "VIDIOC_S_CROP" or "VIDIOC_S_FMT" ioctl's.
  335. The same number of buffers as before will be allocated again to match the size
  336. of the new video frames, so you have to map the buffers again before any I/O
  337. attempts on them.
  338. Consistently with the hardware limits, this driver also supports image
  339. downscaling with arbitrary scaling factors from 1, 2 and 4 in both directions.
  340. However, the V4L2 API specifications don't correctly define how the scaling
  341. factor can be chosen arbitrarily by the "negotiation" of the "source" and
  342. "target" rectangles. To work around this flaw, we have added the convention
  343. that, during the negotiation, whenever the "VIDIOC_S_CROP" ioctl is issued, the
  344. scaling factor is restored to 1.
  345. This driver supports two different video formats: the first one is the "8-bit
  346. Sequential Bayer" format and can be used to obtain uncompressed video data
  347. from the device through the current I/O method, while the second one provides
  348. "raw" compressed video data (without frame headers not related to the
  349. compressed data). The compression quality may vary from 0 to 1 and can be
  350. selected or queried thanks to the VIDIOC_S_JPEGCOMP and VIDIOC_G_JPEGCOMP V4L2
  351. ioctl's. For maximum flexibility, both the default active video format and the
  352. default compression quality depend on how the image sensor being used is
  353. initialized (as described in the documentation of the API for the image sensors
  354. supplied by this driver).
  355. 11. Video frame formats [1]
  356. =======================
  357. The SN9C10x PC Camera Controllers can send images in two possible video
  358. formats over the USB: either native "Sequential RGB Bayer" or Huffman
  359. compressed. The latter is used to achieve high frame rates. The current video
  360. format may be selected or queried from the user application by calling the
  361. VIDIOC_S_FMT or VIDIOC_G_FMT ioctl's, as described in the V4L2 API
  362. specifications.
  363. The name "Sequential Bayer" indicates the organization of the red, green and
  364. blue pixels in one video frame. Each pixel is associated with a 8-bit long
  365. value and is disposed in memory according to the pattern shown below:
  366. B[0] G[1] B[2] G[3] ... B[m-2] G[m-1]
  367. G[m] R[m+1] G[m+2] R[m+2] ... G[2m-2] R[2m-1]
  368. ...
  369. ... B[(n-1)(m-2)] G[(n-1)(m-1)]
  370. ... G[n(m-2)] R[n(m-1)]
  371. The above matrix also represents the sequential or progressive read-out mode of
  372. the (n, m) Bayer color filter array used in many CCD/CMOS image sensors.
  373. One compressed video frame consists of a bitstream that encodes for every R, G,
  374. or B pixel the difference between the value of the pixel itself and some
  375. reference pixel value. Pixels are organised in the Bayer pattern and the Bayer
  376. sub-pixels are tracked individually and alternatingly. For example, in the
  377. first line values for the B and G1 pixels are alternatingly encoded, while in
  378. the second line values for the G2 and R pixels are alternatingly encoded.
  379. The pixel reference value is calculated as follows:
  380. - the 4 top left pixels are encoded in raw uncompressed 8-bit format;
  381. - the value in the top two rows is the value of the pixel left of the current
  382. pixel;
  383. - the value in the left column is the value of the pixel above the current
  384. pixel;
  385. - for all other pixels, the reference value is the average of the value of the
  386. pixel on the left and the value of the pixel above the current pixel;
  387. - there is one code in the bitstream that specifies the value of a pixel
  388. directly (in 4-bit resolution);
  389. - pixel values need to be clamped inside the range [0..255] for proper
  390. decoding.
  391. The algorithm purely describes the conversion from compressed Bayer code used
  392. in the SN9C10x chips to uncompressed Bayer. Additional steps are required to
  393. convert this to a color image (i.e. a color interpolation algorithm).
  394. The following Huffman codes have been found:
  395. 0: +0 (relative to reference pixel value)
  396. 100: +4
  397. 101: -4?
  398. 1110xxxx: set absolute value to xxxx.0000
  399. 1101: +11
  400. 1111: -11
  401. 11001: +20
  402. 110000: -20
  403. 110001: ??? - these codes are apparently not used
  404. [1] The Huffman compression algorithm has been reverse-engineered and
  405. documented by Bertrik Sikken.
  406. 12. Contact information
  407. =======================
  408. The author may be contacted by e-mail at <luca.risolia@studio.unibo.it>.
  409. GPG/PGP encrypted e-mail's are accepted. The GPG key ID of the author is
  410. 'FCE635A4'; the public 1024-bit key should be available at any keyserver;
  411. the fingerprint is: '88E8 F32F 7244 68BA 3958 5D40 99DA 5D2A FCE6 35A4'.
  412. 13. Credits
  413. ===========
  414. Many thanks to following persons for their contribute (listed in alphabetical
  415. order):
  416. - Luca Capello for the donation of a webcam;
  417. - Philippe Coval for having helped testing the PAS202BCA image sensor;
  418. - Joao Rodrigo Fuzaro, Joao Limirio, Claudio Filho and Caio Begotti for the
  419. donation of a webcam;
  420. - Jon Hollstrom for the donation of a webcam;
  421. - Carlos Eduardo Medaglia Dyonisio, who added the support for the PAS202BCB
  422. image sensor;
  423. - Stefano Mozzi, who donated 45 EU;
  424. - Andrew Pearce for the donation of a webcam;
  425. - Bertrik Sikken, who reverse-engineered and documented the Huffman compression
  426. algorithm used in the SN9C10x controllers and implemented the first decoder;
  427. - Mizuno Takafumi for the donation of a webcam;
  428. - an "anonymous" donator (who didn't want his name to be revealed) for the
  429. donation of a webcam.