sn9c102.txt 19 KB

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