sn9c102.txt 19 KB

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