vicam.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /*
  2. * USB ViCam WebCam driver
  3. * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
  4. * Christopher L Cheney (ccheney@cheney.cx),
  5. * Pavel Machek (pavel@suse.cz),
  6. * John Tyner (jtyner@cs.ucr.edu),
  7. * Monroe Williams (monroe@pobox.com)
  8. *
  9. * Supports 3COM HomeConnect PC Digital WebCam
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * This source code is based heavily on the CPiA webcam driver which was
  26. * written by Peter Pregler, Scott J. Bertin and Johannes Erdfelt
  27. *
  28. * Portions of this code were also copied from usbvideo.c
  29. *
  30. * Special thanks to the the whole team at Sourceforge for help making
  31. * this driver become a reality. Notably:
  32. * Andy Armstrong who reverse engineered the color encoding and
  33. * Pavel Machek and Chris Cheney who worked on reverse engineering the
  34. * camera controls and wrote the first generation driver.
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/videodev.h>
  40. #include <linux/usb.h>
  41. #include <linux/vmalloc.h>
  42. #include <linux/slab.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/mutex.h>
  45. #include "usbvideo.h"
  46. // #define VICAM_DEBUG
  47. #ifdef VICAM_DEBUG
  48. #define ADBG(lineno,fmt,args...) printk(fmt, jiffies, __FUNCTION__, lineno, ##args)
  49. #define DBG(fmt,args...) ADBG((__LINE__),KERN_DEBUG __FILE__"(%ld):%s (%d):"fmt,##args)
  50. #else
  51. #define DBG(fmn,args...) do {} while(0)
  52. #endif
  53. #define DRIVER_AUTHOR "Joe Burks, jburks@wavicle.org"
  54. #define DRIVER_DESC "ViCam WebCam Driver"
  55. /* Define these values to match your device */
  56. #define USB_VICAM_VENDOR_ID 0x04c1
  57. #define USB_VICAM_PRODUCT_ID 0x009d
  58. #define VICAM_BYTES_PER_PIXEL 3
  59. #define VICAM_MAX_READ_SIZE (512*242+128)
  60. #define VICAM_MAX_FRAME_SIZE (VICAM_BYTES_PER_PIXEL*320*240)
  61. #define VICAM_FRAMES 2
  62. #define VICAM_HEADER_SIZE 64
  63. #define clamp( x, l, h ) max_t( __typeof__( x ), \
  64. ( l ), \
  65. min_t( __typeof__( x ), \
  66. ( h ), \
  67. ( x ) ) )
  68. /* Not sure what all the bytes in these char
  69. * arrays do, but they're necessary to make
  70. * the camera work.
  71. */
  72. static unsigned char setup1[] = {
  73. 0xB6, 0xC3, 0x1F, 0x00, 0x02, 0x64, 0xE7, 0x67,
  74. 0xFD, 0xFF, 0x0E, 0xC0, 0xE7, 0x09, 0xDE, 0x00,
  75. 0x8E, 0x00, 0xC0, 0x09, 0x40, 0x03, 0xC0, 0x17,
  76. 0x44, 0x03, 0x4B, 0xAF, 0xC0, 0x07, 0x00, 0x00,
  77. 0x4B, 0xAF, 0x97, 0xCF, 0x00, 0x00
  78. };
  79. static unsigned char setup2[] = {
  80. 0xB6, 0xC3, 0x03, 0x00, 0x03, 0x64, 0x18, 0x00,
  81. 0x00, 0x00
  82. };
  83. static unsigned char setup3[] = {
  84. 0xB6, 0xC3, 0x01, 0x00, 0x06, 0x64, 0x00, 0x00
  85. };
  86. static unsigned char setup4[] = {
  87. 0xB6, 0xC3, 0x8F, 0x06, 0x02, 0x64, 0xE7, 0x07,
  88. 0x00, 0x00, 0x08, 0xC0, 0xE7, 0x07, 0x00, 0x00,
  89. 0x3E, 0xC0, 0xE7, 0x07, 0x54, 0x01, 0xAA, 0x00,
  90. 0xE7, 0x07, 0xC8, 0x05, 0xB6, 0x00, 0xE7, 0x07,
  91. 0x42, 0x01, 0xD2, 0x00, 0xE7, 0x07, 0x7C, 0x00,
  92. 0x16, 0x00, 0xE7, 0x07, 0x56, 0x00, 0x18, 0x00,
  93. 0xE7, 0x07, 0x06, 0x00, 0x92, 0xC0, 0xE7, 0x07,
  94. 0x00, 0x00, 0x1E, 0xC0, 0xE7, 0x07, 0xFF, 0xFF,
  95. 0x22, 0xC0, 0xE7, 0x07, 0x04, 0x00, 0x24, 0xC0,
  96. 0xE7, 0x07, 0xEC, 0x27, 0x28, 0xC0, 0xE7, 0x07,
  97. 0x16, 0x01, 0x8E, 0x00, 0xE7, 0x87, 0x01, 0x00,
  98. 0x0E, 0xC0, 0x97, 0xCF, 0xD7, 0x09, 0x00, 0xC0,
  99. 0xE7, 0x77, 0x01, 0x00, 0x92, 0xC0, 0x09, 0xC1,
  100. 0xE7, 0x09, 0xFE, 0x05, 0x24, 0x01, 0xE7, 0x09,
  101. 0x04, 0x06, 0x26, 0x01, 0xE7, 0x07, 0x07, 0x00,
  102. 0x92, 0xC0, 0xE7, 0x05, 0x00, 0xC0, 0xC0, 0xDF,
  103. 0x97, 0xCF, 0x17, 0x00, 0x57, 0x00, 0x17, 0x02,
  104. 0xD7, 0x09, 0x00, 0xC0, 0xE7, 0x77, 0x01, 0x00,
  105. 0x92, 0xC0, 0x0A, 0xC1, 0xE7, 0x57, 0xFF, 0xFF,
  106. 0xFA, 0x05, 0x0D, 0xC0, 0xE7, 0x57, 0x00, 0x00,
  107. 0xFA, 0x05, 0x0F, 0xC0, 0x9F, 0xAF, 0xC6, 0x00,
  108. 0xE7, 0x05, 0x00, 0xC0, 0xC8, 0x05, 0xC1, 0x05,
  109. 0xC0, 0x05, 0xC0, 0xDF, 0x97, 0xCF, 0x27, 0xDA,
  110. 0xFA, 0x05, 0xEF, 0x07, 0x01, 0x00, 0x0B, 0x06,
  111. 0x73, 0xCF, 0x9F, 0xAF, 0x78, 0x01, 0x9F, 0xAF,
  112. 0x1A, 0x03, 0x6E, 0xCF, 0xE7, 0x09, 0xFC, 0x05,
  113. 0x24, 0x01, 0xE7, 0x09, 0x02, 0x06, 0x26, 0x01,
  114. 0xE7, 0x07, 0x07, 0x00, 0x92, 0xC0, 0xE7, 0x09,
  115. 0xFC, 0x05, 0xFE, 0x05, 0xE7, 0x09, 0x02, 0x06,
  116. 0x04, 0x06, 0xE7, 0x09, 0x00, 0x06, 0xFC, 0x05,
  117. 0xE7, 0x09, 0xFE, 0x05, 0x00, 0x06, 0x27, 0xDA,
  118. 0xFA, 0x05, 0xE7, 0x57, 0x01, 0x00, 0xFA, 0x05,
  119. 0x02, 0xCA, 0x04, 0xC0, 0x97, 0xCF, 0x9F, 0xAF,
  120. 0x66, 0x05, 0x97, 0xCF, 0xE7, 0x07, 0x40, 0x00,
  121. 0x02, 0x06, 0xC8, 0x09, 0xFC, 0x05, 0x9F, 0xAF,
  122. 0xDA, 0x02, 0x97, 0xCF, 0xCF, 0x17, 0x02, 0x00,
  123. 0xEF, 0x57, 0x81, 0x00, 0x09, 0x06, 0x9F, 0xA0,
  124. 0xB6, 0x01, 0xEF, 0x57, 0x80, 0x00, 0x09, 0x06,
  125. 0x9F, 0xA0, 0x40, 0x02, 0xEF, 0x57, 0x01, 0x00,
  126. 0x0B, 0x06, 0x9F, 0xA0, 0x46, 0x03, 0xE7, 0x07,
  127. 0x01, 0x00, 0x0A, 0xC0, 0x46, 0xAF, 0x47, 0xAF,
  128. 0x9F, 0xAF, 0x40, 0x02, 0xE7, 0x07, 0x2E, 0x00,
  129. 0x0A, 0xC0, 0xEF, 0x87, 0x80, 0x00, 0x09, 0x06,
  130. 0x97, 0xCF, 0x00, 0x0E, 0x01, 0x00, 0xC0, 0x57,
  131. 0x51, 0x00, 0x9F, 0xC0, 0x9E, 0x02, 0xC0, 0x57,
  132. 0x50, 0x00, 0x20, 0xC0, 0xC0, 0x57, 0x55, 0x00,
  133. 0x12, 0xC0, 0xC0, 0x57, 0x56, 0x00, 0x9F, 0xC0,
  134. 0x72, 0x02, 0x9F, 0xCF, 0xD6, 0x02, 0xC1, 0x0B,
  135. 0x08, 0x06, 0x01, 0xD0, 0x6F, 0x90, 0x08, 0x06,
  136. 0xC0, 0x07, 0x08, 0x00, 0xC1, 0x0B, 0x08, 0x06,
  137. 0x9F, 0xAF, 0x28, 0x05, 0x97, 0xCF, 0x2F, 0x0E,
  138. 0x02, 0x00, 0x08, 0x06, 0xC0, 0x07, 0x08, 0x00,
  139. 0xC1, 0x0B, 0x08, 0x06, 0x9F, 0xAF, 0x28, 0x05,
  140. 0x9F, 0xCF, 0xD6, 0x02, 0x2F, 0x0E, 0x02, 0x00,
  141. 0x09, 0x06, 0xEF, 0x87, 0x80, 0x00, 0x09, 0x06,
  142. 0x9F, 0xCF, 0xD6, 0x02, 0xEF, 0x67, 0x7F, 0xFF,
  143. 0x09, 0x06, 0xE7, 0x67, 0xFF, 0xFD, 0x22, 0xC0,
  144. 0xE7, 0x67, 0xEF, 0xFF, 0x24, 0xC0, 0xE7, 0x87,
  145. 0x10, 0x00, 0x28, 0xC0, 0x9F, 0xAF, 0xB8, 0x05,
  146. 0xE7, 0x87, 0xE0, 0x21, 0x24, 0xC0, 0x9F, 0xAF,
  147. 0xA8, 0x05, 0xE7, 0x87, 0x08, 0x00, 0x24, 0xC0,
  148. 0xE7, 0x67, 0xDF, 0xFF, 0x24, 0xC0, 0xC8, 0x07,
  149. 0x0A, 0x00, 0xC0, 0x07, 0x00, 0x00, 0xC1, 0x07,
  150. 0x01, 0x00, 0x9F, 0xAF, 0x28, 0x05, 0x9F, 0xAF,
  151. 0xB8, 0x05, 0xC0, 0x07, 0x9E, 0x00, 0x9F, 0xAF,
  152. 0x44, 0x05, 0xE7, 0x67, 0xFF, 0xFE, 0x24, 0xC0,
  153. 0xC0, 0x09, 0x20, 0xC0, 0xE7, 0x87, 0x00, 0x01,
  154. 0x24, 0xC0, 0xC0, 0x77, 0x00, 0x02, 0x0F, 0xC1,
  155. 0xE7, 0x67, 0xF7, 0xFF, 0x24, 0xC0, 0xE7, 0x67,
  156. 0xF7, 0xFF, 0x24, 0xC0, 0xE7, 0x87, 0x08, 0x00,
  157. 0x24, 0xC0, 0x08, 0xDA, 0x5E, 0xC1, 0xEF, 0x07,
  158. 0x80, 0x00, 0x09, 0x06, 0x97, 0xCF, 0xEF, 0x07,
  159. 0x01, 0x00, 0x0A, 0x06, 0x97, 0xCF, 0xEF, 0x07,
  160. 0x00, 0x00, 0x0B, 0x06, 0xEF, 0x07, 0x00, 0x00,
  161. 0x0A, 0x06, 0xEF, 0x67, 0x7F, 0xFF, 0x09, 0x06,
  162. 0xEF, 0x07, 0x00, 0x00, 0x0D, 0x06, 0xE7, 0x67,
  163. 0xEF, 0xFF, 0x28, 0xC0, 0xE7, 0x67, 0x17, 0xD8,
  164. 0x24, 0xC0, 0xE7, 0x07, 0x00, 0x00, 0x1E, 0xC0,
  165. 0xE7, 0x07, 0xFF, 0xFF, 0x22, 0xC0, 0x97, 0xCF,
  166. 0xC8, 0x07, 0x0E, 0x06, 0x9F, 0xAF, 0xDA, 0x02,
  167. 0xE7, 0x07, 0x00, 0x00, 0xF2, 0x05, 0xE7, 0x07,
  168. 0x10, 0x00, 0xF6, 0x05, 0xE7, 0x07, 0x0E, 0x06,
  169. 0xF4, 0x05, 0xE7, 0x07, 0xD6, 0x02, 0xF8, 0x05,
  170. 0xC8, 0x07, 0xF2, 0x05, 0xC1, 0x07, 0x00, 0x80,
  171. 0x50, 0xAF, 0x97, 0xCF, 0x2F, 0x0C, 0x02, 0x00,
  172. 0x07, 0x06, 0x2F, 0x0C, 0x04, 0x00, 0x06, 0x06,
  173. 0xE7, 0x07, 0x00, 0x00, 0xF2, 0x05, 0xE7, 0x07,
  174. 0x10, 0x00, 0xF6, 0x05, 0xE7, 0x07, 0xE2, 0x05,
  175. 0xF4, 0x05, 0xE7, 0x07, 0xCE, 0x02, 0xF8, 0x05,
  176. 0xC8, 0x07, 0xF2, 0x05, 0xC1, 0x07, 0x00, 0x80,
  177. 0x51, 0xAF, 0x97, 0xCF, 0x9F, 0xAF, 0x66, 0x04,
  178. 0x9F, 0xAF, 0x1A, 0x03, 0x59, 0xAF, 0x97, 0xCF,
  179. 0xC0, 0x07, 0x0E, 0x00, 0xC1, 0x0B, 0x0C, 0x06,
  180. 0x41, 0xD1, 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07,
  181. 0x3C, 0x00, 0x9F, 0xAF, 0x44, 0x05, 0x68, 0x00,
  182. 0xC0, 0x07, 0x3B, 0x00, 0x9F, 0xAF, 0x44, 0x05,
  183. 0x6F, 0x00, 0x0C, 0x06, 0x68, 0x00, 0xE0, 0x07,
  184. 0x04, 0x01, 0xE8, 0x0B, 0x0A, 0x06, 0xE8, 0x07,
  185. 0x00, 0x00, 0xE0, 0x07, 0x00, 0x02, 0xE0, 0x07,
  186. 0xEC, 0x01, 0xE0, 0x07, 0xFC, 0xFF, 0x97, 0xCF,
  187. 0xE7, 0x07, 0xFF, 0xFF, 0xFA, 0x05, 0xEF, 0x07,
  188. 0x00, 0x00, 0x0B, 0x06, 0xE7, 0x07, 0x0E, 0x06,
  189. 0x24, 0x01, 0xE7, 0x07, 0x0E, 0x06, 0xFE, 0x05,
  190. 0xE7, 0x07, 0x40, 0x00, 0x26, 0x01, 0xE7, 0x07,
  191. 0x40, 0x00, 0x04, 0x06, 0xE7, 0x07, 0x07, 0x00,
  192. 0x92, 0xC0, 0x97, 0xCF, 0xEF, 0x07, 0x02, 0x00,
  193. 0x0B, 0x06, 0x9F, 0xAF, 0x78, 0x01, 0xEF, 0x77,
  194. 0x80, 0x00, 0x07, 0x06, 0x9F, 0xC0, 0x14, 0x04,
  195. 0xEF, 0x77, 0x01, 0x00, 0x07, 0x06, 0x37, 0xC0,
  196. 0xEF, 0x77, 0x01, 0x00, 0x0D, 0x06, 0x0F, 0xC1,
  197. 0xEF, 0x07, 0x01, 0x00, 0x0D, 0x06, 0xC0, 0x07,
  198. 0x02, 0x00, 0xC1, 0x07, 0x30, 0x00, 0x9F, 0xAF,
  199. 0x28, 0x05, 0xC0, 0x07, 0x01, 0x00, 0xC1, 0x07,
  200. 0x02, 0x00, 0x9F, 0xAF, 0x28, 0x05, 0xC8, 0x07,
  201. 0xFF, 0x4F, 0x9F, 0xAF, 0xA8, 0x05, 0xC0, 0x07,
  202. 0x38, 0x00, 0x9F, 0xAF, 0x44, 0x05, 0xC1, 0x77,
  203. 0x03, 0x00, 0x02, 0xC1, 0x08, 0xDA, 0x75, 0xC1,
  204. 0xC1, 0x77, 0x01, 0x00, 0x0A, 0xC1, 0xC0, 0x07,
  205. 0x01, 0x00, 0xC1, 0x07, 0x02, 0x00, 0x9F, 0xAF,
  206. 0x28, 0x05, 0xEF, 0x07, 0x01, 0x00, 0x06, 0x06,
  207. 0x2C, 0xCF, 0xC0, 0x07, 0x01, 0x00, 0xC1, 0x07,
  208. 0x04, 0x00, 0x9F, 0xAF, 0x28, 0x05, 0xEF, 0x07,
  209. 0x00, 0x00, 0x06, 0x06, 0x22, 0xCF, 0xEF, 0x07,
  210. 0x00, 0x00, 0x0D, 0x06, 0xEF, 0x57, 0x01, 0x00,
  211. 0x06, 0x06, 0x1B, 0xC0, 0xC0, 0x07, 0x01, 0x00,
  212. 0xC1, 0x07, 0x01, 0x00, 0x9F, 0xAF, 0x28, 0x05,
  213. 0xC0, 0x07, 0x02, 0x00, 0xC1, 0x07, 0x30, 0x00,
  214. 0x9F, 0xAF, 0x28, 0x05, 0xC8, 0x07, 0xFF, 0x4F,
  215. 0x9F, 0xAF, 0xA8, 0x05, 0xC0, 0x07, 0x38, 0x00,
  216. 0x9F, 0xAF, 0x44, 0x05, 0xC1, 0x67, 0x03, 0x00,
  217. 0xC1, 0x57, 0x03, 0x00, 0x02, 0xC0, 0x08, 0xDA,
  218. 0x73, 0xC1, 0xC0, 0x07, 0x02, 0x00, 0xC1, 0x07,
  219. 0x12, 0x00, 0xEF, 0x57, 0x00, 0x00, 0x06, 0x06,
  220. 0x02, 0xC0, 0xC1, 0x07, 0x23, 0x00, 0x9F, 0xAF,
  221. 0x28, 0x05, 0xC0, 0x07, 0x14, 0x00, 0xC1, 0x0B,
  222. 0xEA, 0x05, 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07,
  223. 0x3E, 0x00, 0x9F, 0xAF, 0x0A, 0x05, 0xE7, 0x09,
  224. 0xE4, 0x05, 0xFA, 0x05, 0x27, 0xD8, 0xFA, 0x05,
  225. 0xE7, 0x07, 0x0E, 0x06, 0xFC, 0x05, 0xE7, 0x07,
  226. 0x4E, 0x06, 0x00, 0x06, 0xE7, 0x07, 0x40, 0x00,
  227. 0x02, 0x06, 0x9F, 0xAF, 0x66, 0x05, 0x9F, 0xAF,
  228. 0xC6, 0x00, 0x97, 0xCF, 0xC1, 0x0B, 0xE2, 0x05,
  229. 0x41, 0xD0, 0x01, 0xD2, 0xC1, 0x17, 0x23, 0x00,
  230. 0x9F, 0xAF, 0xDC, 0x04, 0xC0, 0x07, 0x04, 0x00,
  231. 0xC1, 0x0B, 0xE3, 0x05, 0x9F, 0xAF, 0x28, 0x05,
  232. 0xC0, 0x07, 0x06, 0x00, 0xC1, 0x09, 0xE6, 0x05,
  233. 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07, 0x07, 0x00,
  234. 0xC1, 0x09, 0xE6, 0x05, 0xC1, 0xD1, 0x9F, 0xAF,
  235. 0x28, 0x05, 0xC0, 0x07, 0x0B, 0x00, 0xC1, 0x09,
  236. 0xE8, 0x05, 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07,
  237. 0x0C, 0x00, 0xC1, 0x09, 0xE8, 0x05, 0xC1, 0xD1,
  238. 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07, 0x0D, 0x00,
  239. 0xC1, 0x07, 0x09, 0x00, 0x9F, 0xAF, 0x28, 0x05,
  240. 0xC0, 0x07, 0x03, 0x00, 0xC1, 0x07, 0x32, 0x00,
  241. 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07, 0x0F, 0x00,
  242. 0xC1, 0x07, 0x00, 0x00, 0x9F, 0xAF, 0x28, 0x05,
  243. 0x97, 0xCF, 0xE7, 0x67, 0xFF, 0xD9, 0x24, 0xC0,
  244. 0xC8, 0x07, 0x0A, 0x00, 0x40, 0x00, 0xC0, 0x67,
  245. 0x00, 0x02, 0x27, 0x80, 0x24, 0xC0, 0xE7, 0x87,
  246. 0x00, 0x04, 0x24, 0xC0, 0xE7, 0x67, 0xFF, 0xF9,
  247. 0x24, 0xC0, 0x01, 0xD2, 0x08, 0xDA, 0x72, 0xC1,
  248. 0xE7, 0x87, 0x00, 0x20, 0x24, 0xC0, 0x97, 0xCF,
  249. 0x27, 0x00, 0x1E, 0xC0, 0xE7, 0x87, 0xFF, 0x00,
  250. 0x22, 0xC0, 0xE7, 0x67, 0x7F, 0xFF, 0x24, 0xC0,
  251. 0xE7, 0x87, 0x80, 0x00, 0x24, 0xC0, 0xE7, 0x87,
  252. 0x80, 0x00, 0x24, 0xC0, 0x97, 0xCF, 0x9F, 0xAF,
  253. 0x0A, 0x05, 0x67, 0x00, 0x1E, 0xC0, 0xE7, 0x67,
  254. 0xBF, 0xFF, 0x24, 0xC0, 0xE7, 0x87, 0x40, 0x00,
  255. 0x24, 0xC0, 0xE7, 0x87, 0x40, 0x00, 0x24, 0xC0,
  256. 0x97, 0xCF, 0x9F, 0xAF, 0x0A, 0x05, 0xE7, 0x67,
  257. 0x00, 0xFF, 0x22, 0xC0, 0xE7, 0x67, 0xFF, 0xFE,
  258. 0x24, 0xC0, 0xE7, 0x67, 0xFF, 0xFE, 0x24, 0xC0,
  259. 0xC1, 0x09, 0x20, 0xC0, 0xE7, 0x87, 0x00, 0x01,
  260. 0x24, 0xC0, 0x97, 0xCF, 0xC0, 0x07, 0x40, 0x00,
  261. 0xC8, 0x09, 0xFC, 0x05, 0xE7, 0x67, 0x00, 0xFF,
  262. 0x22, 0xC0, 0xE7, 0x67, 0xFF, 0xFE, 0x24, 0xC0,
  263. 0xE7, 0x67, 0xBF, 0xFF, 0x24, 0xC0, 0xE7, 0x67,
  264. 0xBF, 0xFF, 0x24, 0xC0, 0x00, 0xDA, 0xE8, 0x09,
  265. 0x20, 0xC0, 0xE7, 0x87, 0x40, 0x00, 0x24, 0xC0,
  266. 0xE7, 0x87, 0x40, 0x00, 0x24, 0xC0, 0x00, 0xDA,
  267. 0xE8, 0x09, 0x20, 0xC0, 0x6D, 0xC1, 0xE7, 0x87,
  268. 0x00, 0x01, 0x24, 0xC0, 0x97, 0xCF, 0xE7, 0x07,
  269. 0x32, 0x00, 0x12, 0xC0, 0xE7, 0x77, 0x00, 0x80,
  270. 0x12, 0xC0, 0x7C, 0xC0, 0x97, 0xCF, 0xE7, 0x07,
  271. 0x20, 0x4E, 0x12, 0xC0, 0xE7, 0x77, 0x00, 0x80,
  272. 0x12, 0xC0, 0x7C, 0xC0, 0x97, 0xCF, 0x09, 0x02,
  273. 0x19, 0x00, 0x01, 0x01, 0x00, 0x80, 0x96, 0x09,
  274. 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  275. 0x07, 0x05, 0x81, 0x02, 0x40, 0x00, 0x00, 0x00,
  276. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  277. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  278. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  279. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  280. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  281. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  282. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  283. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  284. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  285. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  286. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  287. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  289. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  290. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  291. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  292. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  293. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  294. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  295. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  296. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  297. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  298. };
  299. static unsigned char setup5[] = {
  300. 0xB6, 0xC3, 0x2F, 0x01, 0x03, 0x64, 0x0E, 0x00,
  301. 0x14, 0x00, 0x1A, 0x00, 0x20, 0x00, 0x26, 0x00,
  302. 0x4A, 0x00, 0x64, 0x00, 0x6A, 0x00, 0x92, 0x00,
  303. 0x9A, 0x00, 0xA0, 0x00, 0xB2, 0x00, 0xB8, 0x00,
  304. 0xBE, 0x00, 0xC2, 0x00, 0xC8, 0x00, 0xCE, 0x00,
  305. 0xDC, 0x00, 0xDA, 0x00, 0xE2, 0x00, 0xE0, 0x00,
  306. 0xE8, 0x00, 0xE6, 0x00, 0xEE, 0x00, 0xEC, 0x00,
  307. 0xF2, 0x00, 0xF8, 0x00, 0x02, 0x01, 0x0A, 0x01,
  308. 0x0E, 0x01, 0x12, 0x01, 0x1E, 0x01, 0x22, 0x01,
  309. 0x28, 0x01, 0x2C, 0x01, 0x32, 0x01, 0x36, 0x01,
  310. 0x44, 0x01, 0x50, 0x01, 0x5E, 0x01, 0x72, 0x01,
  311. 0x76, 0x01, 0x7A, 0x01, 0x80, 0x01, 0x88, 0x01,
  312. 0x8C, 0x01, 0x94, 0x01, 0x9C, 0x01, 0xA0, 0x01,
  313. 0xA4, 0x01, 0xAA, 0x01, 0xB0, 0x01, 0xB4, 0x01,
  314. 0xBA, 0x01, 0xD0, 0x01, 0xDA, 0x01, 0xF6, 0x01,
  315. 0xFA, 0x01, 0x02, 0x02, 0x34, 0x02, 0x3C, 0x02,
  316. 0x44, 0x02, 0x4A, 0x02, 0x50, 0x02, 0x56, 0x02,
  317. 0x74, 0x02, 0x78, 0x02, 0x7E, 0x02, 0x84, 0x02,
  318. 0x8A, 0x02, 0x88, 0x02, 0x90, 0x02, 0x8E, 0x02,
  319. 0x94, 0x02, 0xA2, 0x02, 0xA8, 0x02, 0xAE, 0x02,
  320. 0xB4, 0x02, 0xBA, 0x02, 0xB8, 0x02, 0xC0, 0x02,
  321. 0xBE, 0x02, 0xC4, 0x02, 0xD0, 0x02, 0xD4, 0x02,
  322. 0xE0, 0x02, 0xE6, 0x02, 0xEE, 0x02, 0xF8, 0x02,
  323. 0xFC, 0x02, 0x06, 0x03, 0x1E, 0x03, 0x24, 0x03,
  324. 0x28, 0x03, 0x30, 0x03, 0x2E, 0x03, 0x3C, 0x03,
  325. 0x4A, 0x03, 0x4E, 0x03, 0x54, 0x03, 0x58, 0x03,
  326. 0x5E, 0x03, 0x66, 0x03, 0x6E, 0x03, 0x7A, 0x03,
  327. 0x86, 0x03, 0x8E, 0x03, 0x96, 0x03, 0xB2, 0x03,
  328. 0xB8, 0x03, 0xC6, 0x03, 0xCC, 0x03, 0xD4, 0x03,
  329. 0xDA, 0x03, 0xE8, 0x03, 0xF4, 0x03, 0xFC, 0x03,
  330. 0x04, 0x04, 0x20, 0x04, 0x2A, 0x04, 0x32, 0x04,
  331. 0x36, 0x04, 0x3E, 0x04, 0x44, 0x04, 0x42, 0x04,
  332. 0x48, 0x04, 0x4E, 0x04, 0x4C, 0x04, 0x54, 0x04,
  333. 0x52, 0x04, 0x5A, 0x04, 0x5E, 0x04, 0x62, 0x04,
  334. 0x68, 0x04, 0x74, 0x04, 0x7C, 0x04, 0x80, 0x04,
  335. 0x88, 0x04, 0x8C, 0x04, 0x94, 0x04, 0x9A, 0x04,
  336. 0xA2, 0x04, 0xA6, 0x04, 0xAE, 0x04, 0xB4, 0x04,
  337. 0xC0, 0x04, 0xCC, 0x04, 0xD8, 0x04, 0x2A, 0x05,
  338. 0x46, 0x05, 0x6C, 0x05, 0x00, 0x00
  339. };
  340. /* rvmalloc / rvfree copied from usbvideo.c
  341. *
  342. * Not sure why these are not yet non-statics which I can reference through
  343. * usbvideo.h the same as it is in 2.4.20. I bet this will get fixed sometime
  344. * in the future.
  345. *
  346. */
  347. static void *rvmalloc(unsigned long size)
  348. {
  349. void *mem;
  350. unsigned long adr;
  351. size = PAGE_ALIGN(size);
  352. mem = vmalloc_32(size);
  353. if (!mem)
  354. return NULL;
  355. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  356. adr = (unsigned long) mem;
  357. while (size > 0) {
  358. SetPageReserved(vmalloc_to_page((void *)adr));
  359. adr += PAGE_SIZE;
  360. size -= PAGE_SIZE;
  361. }
  362. return mem;
  363. }
  364. static void rvfree(void *mem, unsigned long size)
  365. {
  366. unsigned long adr;
  367. if (!mem)
  368. return;
  369. adr = (unsigned long) mem;
  370. while ((long) size > 0) {
  371. ClearPageReserved(vmalloc_to_page((void *)adr));
  372. adr += PAGE_SIZE;
  373. size -= PAGE_SIZE;
  374. }
  375. vfree(mem);
  376. }
  377. struct vicam_camera {
  378. u16 shutter_speed; // capture shutter speed
  379. u16 gain; // capture gain
  380. u8 *raw_image; // raw data captured from the camera
  381. u8 *framebuf; // processed data in RGB24 format
  382. u8 *cntrlbuf; // area used to send control msgs
  383. struct video_device vdev; // v4l video device
  384. struct usb_device *udev; // usb device
  385. /* guard against simultaneous accesses to the camera */
  386. struct mutex cam_lock;
  387. int is_initialized;
  388. u8 open_count;
  389. u8 bulkEndpoint;
  390. int needsDummyRead;
  391. #if defined(CONFIG_VIDEO_PROC_FS)
  392. struct proc_dir_entry *proc_dir;
  393. #endif
  394. };
  395. static int vicam_probe( struct usb_interface *intf, const struct usb_device_id *id);
  396. static void vicam_disconnect(struct usb_interface *intf);
  397. static void read_frame(struct vicam_camera *cam, int framenum);
  398. static void vicam_decode_color(const u8 *, u8 *);
  399. static int __send_control_msg(struct vicam_camera *cam,
  400. u8 request,
  401. u16 value,
  402. u16 index,
  403. unsigned char *cp,
  404. u16 size)
  405. {
  406. int status;
  407. /* cp must be memory that has been allocated by kmalloc */
  408. status = usb_control_msg(cam->udev,
  409. usb_sndctrlpipe(cam->udev, 0),
  410. request,
  411. USB_DIR_OUT | USB_TYPE_VENDOR |
  412. USB_RECIP_DEVICE, value, index,
  413. cp, size, 1000);
  414. status = min(status, 0);
  415. if (status < 0) {
  416. printk(KERN_INFO "Failed sending control message, error %d.\n",
  417. status);
  418. }
  419. return status;
  420. }
  421. static int send_control_msg(struct vicam_camera *cam,
  422. u8 request,
  423. u16 value,
  424. u16 index,
  425. unsigned char *cp,
  426. u16 size)
  427. {
  428. int status = -ENODEV;
  429. mutex_lock(&cam->cam_lock);
  430. if (cam->udev) {
  431. status = __send_control_msg(cam, request, value,
  432. index, cp, size);
  433. }
  434. mutex_unlock(&cam->cam_lock);
  435. return status;
  436. }
  437. static int
  438. initialize_camera(struct vicam_camera *cam)
  439. {
  440. const struct {
  441. u8 *data;
  442. u32 size;
  443. } firmware[] = {
  444. { .data = setup1, .size = sizeof(setup1) },
  445. { .data = setup2, .size = sizeof(setup2) },
  446. { .data = setup3, .size = sizeof(setup3) },
  447. { .data = setup4, .size = sizeof(setup4) },
  448. { .data = setup5, .size = sizeof(setup5) },
  449. { .data = setup3, .size = sizeof(setup3) },
  450. { .data = NULL, .size = 0 }
  451. };
  452. int err, i;
  453. for (i = 0, err = 0; firmware[i].data && !err; i++) {
  454. memcpy(cam->cntrlbuf, firmware[i].data, firmware[i].size);
  455. err = send_control_msg(cam, 0xff, 0, 0,
  456. cam->cntrlbuf, firmware[i].size);
  457. }
  458. return err;
  459. }
  460. static int
  461. set_camera_power(struct vicam_camera *cam, int state)
  462. {
  463. int status;
  464. if ((status = send_control_msg(cam, 0x50, state, 0, NULL, 0)) < 0)
  465. return status;
  466. if (state) {
  467. send_control_msg(cam, 0x55, 1, 0, NULL, 0);
  468. }
  469. return 0;
  470. }
  471. static int
  472. vicam_ioctl(struct inode *inode, struct file *file, unsigned int ioctlnr, unsigned long arg)
  473. {
  474. void __user *user_arg = (void __user *)arg;
  475. struct vicam_camera *cam = file->private_data;
  476. int retval = 0;
  477. if (!cam)
  478. return -ENODEV;
  479. switch (ioctlnr) {
  480. /* query capabilities */
  481. case VIDIOCGCAP:
  482. {
  483. struct video_capability b;
  484. DBG("VIDIOCGCAP\n");
  485. memset(&b, 0, sizeof(b));
  486. strcpy(b.name, "ViCam-based Camera");
  487. b.type = VID_TYPE_CAPTURE;
  488. b.channels = 1;
  489. b.audios = 0;
  490. b.maxwidth = 320; /* VIDEOSIZE_CIF */
  491. b.maxheight = 240;
  492. b.minwidth = 320; /* VIDEOSIZE_48_48 */
  493. b.minheight = 240;
  494. if (copy_to_user(user_arg, &b, sizeof(b)))
  495. retval = -EFAULT;
  496. break;
  497. }
  498. /* get/set video source - we are a camera and nothing else */
  499. case VIDIOCGCHAN:
  500. {
  501. struct video_channel v;
  502. DBG("VIDIOCGCHAN\n");
  503. if (copy_from_user(&v, user_arg, sizeof(v))) {
  504. retval = -EFAULT;
  505. break;
  506. }
  507. if (v.channel != 0) {
  508. retval = -EINVAL;
  509. break;
  510. }
  511. v.channel = 0;
  512. strcpy(v.name, "Camera");
  513. v.tuners = 0;
  514. v.flags = 0;
  515. v.type = VIDEO_TYPE_CAMERA;
  516. v.norm = 0;
  517. if (copy_to_user(user_arg, &v, sizeof(v)))
  518. retval = -EFAULT;
  519. break;
  520. }
  521. case VIDIOCSCHAN:
  522. {
  523. int v;
  524. if (copy_from_user(&v, user_arg, sizeof(v)))
  525. retval = -EFAULT;
  526. DBG("VIDIOCSCHAN %d\n", v);
  527. if (retval == 0 && v != 0)
  528. retval = -EINVAL;
  529. break;
  530. }
  531. /* image properties */
  532. case VIDIOCGPICT:
  533. {
  534. struct video_picture vp;
  535. DBG("VIDIOCGPICT\n");
  536. memset(&vp, 0, sizeof (struct video_picture));
  537. vp.brightness = cam->gain << 8;
  538. vp.depth = 24;
  539. vp.palette = VIDEO_PALETTE_RGB24;
  540. if (copy_to_user(user_arg, &vp, sizeof (struct video_picture)))
  541. retval = -EFAULT;
  542. break;
  543. }
  544. case VIDIOCSPICT:
  545. {
  546. struct video_picture vp;
  547. if (copy_from_user(&vp, user_arg, sizeof(vp))) {
  548. retval = -EFAULT;
  549. break;
  550. }
  551. DBG("VIDIOCSPICT depth = %d, pal = %d\n", vp.depth,
  552. vp.palette);
  553. cam->gain = vp.brightness >> 8;
  554. if (vp.depth != 24
  555. || vp.palette != VIDEO_PALETTE_RGB24)
  556. retval = -EINVAL;
  557. break;
  558. }
  559. /* get/set capture window */
  560. case VIDIOCGWIN:
  561. {
  562. struct video_window vw;
  563. vw.x = 0;
  564. vw.y = 0;
  565. vw.width = 320;
  566. vw.height = 240;
  567. vw.chromakey = 0;
  568. vw.flags = 0;
  569. vw.clips = NULL;
  570. vw.clipcount = 0;
  571. DBG("VIDIOCGWIN\n");
  572. if (copy_to_user(user_arg, (void *)&vw, sizeof(vw)))
  573. retval = -EFAULT;
  574. // I'm not sure what the deal with a capture window is, it is very poorly described
  575. // in the doc. So I won't support it now.
  576. break;
  577. }
  578. case VIDIOCSWIN:
  579. {
  580. struct video_window vw;
  581. if (copy_from_user(&vw, user_arg, sizeof(vw))) {
  582. retval = -EFAULT;
  583. break;
  584. }
  585. DBG("VIDIOCSWIN %d x %d\n", vw.width, vw.height);
  586. if ( vw.width != 320 || vw.height != 240 )
  587. retval = -EFAULT;
  588. break;
  589. }
  590. /* mmap interface */
  591. case VIDIOCGMBUF:
  592. {
  593. struct video_mbuf vm;
  594. int i;
  595. DBG("VIDIOCGMBUF\n");
  596. memset(&vm, 0, sizeof (vm));
  597. vm.size =
  598. VICAM_MAX_FRAME_SIZE * VICAM_FRAMES;
  599. vm.frames = VICAM_FRAMES;
  600. for (i = 0; i < VICAM_FRAMES; i++)
  601. vm.offsets[i] = VICAM_MAX_FRAME_SIZE * i;
  602. if (copy_to_user(user_arg, (void *)&vm, sizeof(vm)))
  603. retval = -EFAULT;
  604. break;
  605. }
  606. case VIDIOCMCAPTURE:
  607. {
  608. struct video_mmap vm;
  609. // int video_size;
  610. if (copy_from_user((void *)&vm, user_arg, sizeof(vm))) {
  611. retval = -EFAULT;
  612. break;
  613. }
  614. DBG("VIDIOCMCAPTURE frame=%d, height=%d, width=%d, format=%d.\n",vm.frame,vm.width,vm.height,vm.format);
  615. if ( vm.frame >= VICAM_FRAMES || vm.format != VIDEO_PALETTE_RGB24 )
  616. retval = -EINVAL;
  617. // in theory right here we'd start the image capturing
  618. // (fill in a bulk urb and submit it asynchronously)
  619. //
  620. // Instead we're going to do a total hack job for now and
  621. // retrieve the frame in VIDIOCSYNC
  622. break;
  623. }
  624. case VIDIOCSYNC:
  625. {
  626. int frame;
  627. if (copy_from_user((void *)&frame, user_arg, sizeof(int))) {
  628. retval = -EFAULT;
  629. break;
  630. }
  631. DBG("VIDIOCSYNC: %d\n", frame);
  632. read_frame(cam, frame);
  633. vicam_decode_color(cam->raw_image,
  634. cam->framebuf +
  635. frame * VICAM_MAX_FRAME_SIZE );
  636. break;
  637. }
  638. /* pointless to implement overlay with this camera */
  639. case VIDIOCCAPTURE:
  640. case VIDIOCGFBUF:
  641. case VIDIOCSFBUF:
  642. case VIDIOCKEY:
  643. retval = -EINVAL;
  644. break;
  645. /* tuner interface - we have none */
  646. case VIDIOCGTUNER:
  647. case VIDIOCSTUNER:
  648. case VIDIOCGFREQ:
  649. case VIDIOCSFREQ:
  650. retval = -EINVAL;
  651. break;
  652. /* audio interface - we have none */
  653. case VIDIOCGAUDIO:
  654. case VIDIOCSAUDIO:
  655. retval = -EINVAL;
  656. break;
  657. default:
  658. retval = -ENOIOCTLCMD;
  659. break;
  660. }
  661. return retval;
  662. }
  663. static int
  664. vicam_open(struct inode *inode, struct file *file)
  665. {
  666. struct video_device *dev = video_devdata(file);
  667. struct vicam_camera *cam =
  668. (struct vicam_camera *) dev->priv;
  669. DBG("open\n");
  670. if (!cam) {
  671. printk(KERN_ERR
  672. "vicam video_device improperly initialized");
  673. return -EINVAL;
  674. }
  675. /* the videodev_lock held above us protects us from
  676. * simultaneous opens...for now. we probably shouldn't
  677. * rely on this fact forever.
  678. */
  679. if (cam->open_count > 0) {
  680. printk(KERN_INFO
  681. "vicam_open called on already opened camera");
  682. return -EBUSY;
  683. }
  684. cam->raw_image = kmalloc(VICAM_MAX_READ_SIZE, GFP_KERNEL);
  685. if (!cam->raw_image) {
  686. return -ENOMEM;
  687. }
  688. cam->framebuf = rvmalloc(VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  689. if (!cam->framebuf) {
  690. kfree(cam->raw_image);
  691. return -ENOMEM;
  692. }
  693. cam->cntrlbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  694. if (!cam->cntrlbuf) {
  695. kfree(cam->raw_image);
  696. rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  697. return -ENOMEM;
  698. }
  699. // First upload firmware, then turn the camera on
  700. if (!cam->is_initialized) {
  701. initialize_camera(cam);
  702. cam->is_initialized = 1;
  703. }
  704. set_camera_power(cam, 1);
  705. cam->needsDummyRead = 1;
  706. cam->open_count++;
  707. file->private_data = cam;
  708. return 0;
  709. }
  710. static int
  711. vicam_close(struct inode *inode, struct file *file)
  712. {
  713. struct vicam_camera *cam = file->private_data;
  714. int open_count;
  715. struct usb_device *udev;
  716. DBG("close\n");
  717. /* it's not the end of the world if
  718. * we fail to turn the camera off.
  719. */
  720. set_camera_power(cam, 0);
  721. kfree(cam->raw_image);
  722. rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  723. kfree(cam->cntrlbuf);
  724. mutex_lock(&cam->cam_lock);
  725. cam->open_count--;
  726. open_count = cam->open_count;
  727. udev = cam->udev;
  728. mutex_unlock(&cam->cam_lock);
  729. if (!open_count && !udev) {
  730. kfree(cam);
  731. }
  732. return 0;
  733. }
  734. static void vicam_decode_color(const u8 *data, u8 *rgb)
  735. {
  736. /* vicam_decode_color - Convert from Vicam Y-Cr-Cb to RGB
  737. * Copyright (C) 2002 Monroe Williams (monroe@pobox.com)
  738. */
  739. int i, prevY, nextY;
  740. prevY = 512;
  741. nextY = 512;
  742. data += VICAM_HEADER_SIZE;
  743. for( i = 0; i < 240; i++, data += 512 ) {
  744. const int y = ( i * 242 ) / 240;
  745. int j, prevX, nextX;
  746. int Y, Cr, Cb;
  747. if ( y == 242 - 1 ) {
  748. nextY = -512;
  749. }
  750. prevX = 1;
  751. nextX = 1;
  752. for ( j = 0; j < 320; j++, rgb += 3 ) {
  753. const int x = ( j * 512 ) / 320;
  754. const u8 * const src = &data[x];
  755. if ( x == 512 - 1 ) {
  756. nextX = -1;
  757. }
  758. Cr = ( src[prevX] - src[0] ) +
  759. ( src[nextX] - src[0] );
  760. Cr /= 2;
  761. Cb = ( src[prevY] - src[prevX + prevY] ) +
  762. ( src[prevY] - src[nextX + prevY] ) +
  763. ( src[nextY] - src[prevX + nextY] ) +
  764. ( src[nextY] - src[nextX + nextY] );
  765. Cb /= 4;
  766. Y = 1160 * ( src[0] + ( Cr / 2 ) - 16 );
  767. if ( i & 1 ) {
  768. int Ct = Cr;
  769. Cr = Cb;
  770. Cb = Ct;
  771. }
  772. if ( ( x ^ i ) & 1 ) {
  773. Cr = -Cr;
  774. Cb = -Cb;
  775. }
  776. rgb[0] = clamp( ( ( Y + ( 2017 * Cb ) ) +
  777. 500 ) / 900, 0, 255 );
  778. rgb[1] = clamp( ( ( Y - ( 392 * Cb ) -
  779. ( 813 * Cr ) ) +
  780. 500 ) / 1000, 0, 255 );
  781. rgb[2] = clamp( ( ( Y + ( 1594 * Cr ) ) +
  782. 500 ) / 1300, 0, 255 );
  783. prevX = -1;
  784. }
  785. prevY = -512;
  786. }
  787. }
  788. static void
  789. read_frame(struct vicam_camera *cam, int framenum)
  790. {
  791. unsigned char *request = cam->cntrlbuf;
  792. int realShutter;
  793. int n;
  794. int actual_length;
  795. if (cam->needsDummyRead) {
  796. cam->needsDummyRead = 0;
  797. read_frame(cam, framenum);
  798. }
  799. memset(request, 0, 16);
  800. request[0] = cam->gain; // 0 = 0% gain, FF = 100% gain
  801. request[1] = 0; // 512x242 capture
  802. request[2] = 0x90; // the function of these two bytes
  803. request[3] = 0x07; // is not yet understood
  804. if (cam->shutter_speed > 60) {
  805. // Short exposure
  806. realShutter =
  807. ((-15631900 / cam->shutter_speed) + 260533) / 1000;
  808. request[4] = realShutter & 0xFF;
  809. request[5] = (realShutter >> 8) & 0xFF;
  810. request[6] = 0x03;
  811. request[7] = 0x01;
  812. } else {
  813. // Long exposure
  814. realShutter = 15600 / cam->shutter_speed - 1;
  815. request[4] = 0;
  816. request[5] = 0;
  817. request[6] = realShutter & 0xFF;
  818. request[7] = realShutter >> 8;
  819. }
  820. // Per John Markus Bjørndalen, byte at index 8 causes problems if it isn't 0
  821. request[8] = 0;
  822. // bytes 9-15 do not seem to affect exposure or image quality
  823. mutex_lock(&cam->cam_lock);
  824. if (!cam->udev) {
  825. goto done;
  826. }
  827. n = __send_control_msg(cam, 0x51, 0x80, 0, request, 16);
  828. if (n < 0) {
  829. printk(KERN_ERR
  830. " Problem sending frame capture control message");
  831. goto done;
  832. }
  833. n = usb_bulk_msg(cam->udev,
  834. usb_rcvbulkpipe(cam->udev, cam->bulkEndpoint),
  835. cam->raw_image,
  836. 512 * 242 + 128, &actual_length, 10000);
  837. if (n < 0) {
  838. printk(KERN_ERR "Problem during bulk read of frame data: %d\n",
  839. n);
  840. }
  841. done:
  842. mutex_unlock(&cam->cam_lock);
  843. }
  844. static ssize_t
  845. vicam_read( struct file *file, char __user *buf, size_t count, loff_t *ppos )
  846. {
  847. struct vicam_camera *cam = file->private_data;
  848. DBG("read %d bytes.\n", (int) count);
  849. if (*ppos >= VICAM_MAX_FRAME_SIZE) {
  850. *ppos = 0;
  851. return 0;
  852. }
  853. if (*ppos == 0) {
  854. read_frame(cam, 0);
  855. vicam_decode_color(cam->raw_image,
  856. cam->framebuf +
  857. 0 * VICAM_MAX_FRAME_SIZE);
  858. }
  859. count = min_t(size_t, count, VICAM_MAX_FRAME_SIZE - *ppos);
  860. if (copy_to_user(buf, &cam->framebuf[*ppos], count)) {
  861. count = -EFAULT;
  862. } else {
  863. *ppos += count;
  864. }
  865. if (count == VICAM_MAX_FRAME_SIZE) {
  866. *ppos = 0;
  867. }
  868. return count;
  869. }
  870. static int
  871. vicam_mmap(struct file *file, struct vm_area_struct *vma)
  872. {
  873. // TODO: allocate the raw frame buffer if necessary
  874. unsigned long page, pos;
  875. unsigned long start = vma->vm_start;
  876. unsigned long size = vma->vm_end-vma->vm_start;
  877. struct vicam_camera *cam = file->private_data;
  878. if (!cam)
  879. return -ENODEV;
  880. DBG("vicam_mmap: %ld\n", size);
  881. /* We let mmap allocate as much as it wants because Linux was adding 2048 bytes
  882. * to the size the application requested for mmap and it was screwing apps up.
  883. if (size > VICAM_FRAMES*VICAM_MAX_FRAME_SIZE)
  884. return -EINVAL;
  885. */
  886. pos = (unsigned long)cam->framebuf;
  887. while (size > 0) {
  888. page = vmalloc_to_pfn((void *)pos);
  889. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  890. return -EAGAIN;
  891. start += PAGE_SIZE;
  892. pos += PAGE_SIZE;
  893. if (size > PAGE_SIZE)
  894. size -= PAGE_SIZE;
  895. else
  896. size = 0;
  897. }
  898. return 0;
  899. }
  900. #if defined(CONFIG_VIDEO_PROC_FS)
  901. static struct proc_dir_entry *vicam_proc_root = NULL;
  902. static int vicam_read_helper(char *page, char **start, off_t off,
  903. int count, int *eof, int value)
  904. {
  905. char *out = page;
  906. int len;
  907. out += sprintf(out, "%d",value);
  908. len = out - page;
  909. len -= off;
  910. if (len < count) {
  911. *eof = 1;
  912. if (len <= 0)
  913. return 0;
  914. } else
  915. len = count;
  916. *start = page + off;
  917. return len;
  918. }
  919. static int vicam_read_proc_shutter(char *page, char **start, off_t off,
  920. int count, int *eof, void *data)
  921. {
  922. return vicam_read_helper(page,start,off,count,eof,
  923. ((struct vicam_camera *)data)->shutter_speed);
  924. }
  925. static int vicam_read_proc_gain(char *page, char **start, off_t off,
  926. int count, int *eof, void *data)
  927. {
  928. return vicam_read_helper(page,start,off,count,eof,
  929. ((struct vicam_camera *)data)->gain);
  930. }
  931. static int
  932. vicam_write_proc_shutter(struct file *file, const char *buffer,
  933. unsigned long count, void *data)
  934. {
  935. u16 stmp;
  936. char kbuf[8];
  937. struct vicam_camera *cam = (struct vicam_camera *) data;
  938. if (count > 6)
  939. return -EINVAL;
  940. if (copy_from_user(kbuf, buffer, count))
  941. return -EFAULT;
  942. stmp = (u16) simple_strtoul(kbuf, NULL, 10);
  943. if (stmp < 4 || stmp > 32000)
  944. return -EINVAL;
  945. cam->shutter_speed = stmp;
  946. return count;
  947. }
  948. static int
  949. vicam_write_proc_gain(struct file *file, const char *buffer,
  950. unsigned long count, void *data)
  951. {
  952. u16 gtmp;
  953. char kbuf[8];
  954. struct vicam_camera *cam = (struct vicam_camera *) data;
  955. if (count > 4)
  956. return -EINVAL;
  957. if (copy_from_user(kbuf, buffer, count))
  958. return -EFAULT;
  959. gtmp = (u16) simple_strtoul(kbuf, NULL, 10);
  960. if (gtmp > 255)
  961. return -EINVAL;
  962. cam->gain = gtmp;
  963. return count;
  964. }
  965. static void
  966. vicam_create_proc_root(void)
  967. {
  968. vicam_proc_root = proc_mkdir("video/vicam", NULL);
  969. if (vicam_proc_root)
  970. vicam_proc_root->owner = THIS_MODULE;
  971. else
  972. printk(KERN_ERR
  973. "could not create /proc entry for vicam!");
  974. }
  975. static void
  976. vicam_destroy_proc_root(void)
  977. {
  978. if (vicam_proc_root)
  979. remove_proc_entry("video/vicam", 0);
  980. }
  981. static void
  982. vicam_create_proc_entry(struct vicam_camera *cam)
  983. {
  984. char name[64];
  985. struct proc_dir_entry *ent;
  986. DBG(KERN_INFO "vicam: creating proc entry\n");
  987. if (!vicam_proc_root || !cam) {
  988. printk(KERN_INFO
  989. "vicam: could not create proc entry, %s pointer is null.\n",
  990. (!cam ? "camera" : "root"));
  991. return;
  992. }
  993. sprintf(name, "video%d", cam->vdev.minor);
  994. cam->proc_dir = proc_mkdir(name, vicam_proc_root);
  995. if ( !cam->proc_dir )
  996. return; // FIXME: We should probably return an error here
  997. ent = create_proc_entry("shutter", S_IFREG | S_IRUGO | S_IWUSR,
  998. cam->proc_dir);
  999. if (ent) {
  1000. ent->data = cam;
  1001. ent->read_proc = vicam_read_proc_shutter;
  1002. ent->write_proc = vicam_write_proc_shutter;
  1003. ent->size = 64;
  1004. }
  1005. ent = create_proc_entry("gain", S_IFREG | S_IRUGO | S_IWUSR,
  1006. cam->proc_dir);
  1007. if (ent) {
  1008. ent->data = cam;
  1009. ent->read_proc = vicam_read_proc_gain;
  1010. ent->write_proc = vicam_write_proc_gain;
  1011. ent->size = 64;
  1012. }
  1013. }
  1014. static void
  1015. vicam_destroy_proc_entry(void *ptr)
  1016. {
  1017. struct vicam_camera *cam = (struct vicam_camera *) ptr;
  1018. char name[16];
  1019. if ( !cam->proc_dir )
  1020. return;
  1021. sprintf(name, "video%d", cam->vdev.minor);
  1022. remove_proc_entry("shutter", cam->proc_dir);
  1023. remove_proc_entry("gain", cam->proc_dir);
  1024. remove_proc_entry(name,vicam_proc_root);
  1025. cam->proc_dir = NULL;
  1026. }
  1027. #else
  1028. static inline void vicam_create_proc_root(void) { }
  1029. static inline void vicam_destroy_proc_root(void) { }
  1030. static inline void vicam_create_proc_entry(struct vicam_camera *cam) { }
  1031. static inline void vicam_destroy_proc_entry(void *ptr) { }
  1032. #endif
  1033. static struct file_operations vicam_fops = {
  1034. .owner = THIS_MODULE,
  1035. .open = vicam_open,
  1036. .release = vicam_close,
  1037. .read = vicam_read,
  1038. .mmap = vicam_mmap,
  1039. .ioctl = vicam_ioctl,
  1040. .compat_ioctl = v4l_compat_ioctl32,
  1041. .llseek = no_llseek,
  1042. };
  1043. static struct video_device vicam_template = {
  1044. .owner = THIS_MODULE,
  1045. .name = "ViCam-based USB Camera",
  1046. .type = VID_TYPE_CAPTURE,
  1047. .hardware = VID_HARDWARE_VICAM,
  1048. .fops = &vicam_fops,
  1049. .minor = -1,
  1050. };
  1051. /* table of devices that work with this driver */
  1052. static struct usb_device_id vicam_table[] = {
  1053. {USB_DEVICE(USB_VICAM_VENDOR_ID, USB_VICAM_PRODUCT_ID)},
  1054. {} /* Terminating entry */
  1055. };
  1056. MODULE_DEVICE_TABLE(usb, vicam_table);
  1057. static struct usb_driver vicam_driver = {
  1058. .name = "vicam",
  1059. .probe = vicam_probe,
  1060. .disconnect = vicam_disconnect,
  1061. .id_table = vicam_table
  1062. };
  1063. /**
  1064. * vicam_probe
  1065. * @intf: the interface
  1066. * @id: the device id
  1067. *
  1068. * Called by the usb core when a new device is connected that it thinks
  1069. * this driver might be interested in.
  1070. */
  1071. static int
  1072. vicam_probe( struct usb_interface *intf, const struct usb_device_id *id)
  1073. {
  1074. struct usb_device *dev = interface_to_usbdev(intf);
  1075. int bulkEndpoint = 0;
  1076. const struct usb_host_interface *interface;
  1077. const struct usb_endpoint_descriptor *endpoint;
  1078. struct vicam_camera *cam;
  1079. printk(KERN_INFO "ViCam based webcam connected\n");
  1080. interface = intf->cur_altsetting;
  1081. DBG(KERN_DEBUG "Interface %d. has %u. endpoints!\n",
  1082. interface->desc.bInterfaceNumber, (unsigned) (interface->desc.bNumEndpoints));
  1083. endpoint = &interface->endpoint[0].desc;
  1084. if ((endpoint->bEndpointAddress & 0x80) &&
  1085. ((endpoint->bmAttributes & 3) == 0x02)) {
  1086. /* we found a bulk in endpoint */
  1087. bulkEndpoint = endpoint->bEndpointAddress;
  1088. } else {
  1089. printk(KERN_ERR
  1090. "No bulk in endpoint was found ?! (this is bad)\n");
  1091. }
  1092. if ((cam =
  1093. kmalloc(sizeof (struct vicam_camera), GFP_KERNEL)) == NULL) {
  1094. printk(KERN_WARNING
  1095. "could not allocate kernel memory for vicam_camera struct\n");
  1096. return -ENOMEM;
  1097. }
  1098. memset(cam, 0, sizeof (struct vicam_camera));
  1099. cam->shutter_speed = 15;
  1100. mutex_init(&cam->cam_lock);
  1101. memcpy(&cam->vdev, &vicam_template,
  1102. sizeof (vicam_template));
  1103. cam->vdev.priv = cam; // sort of a reverse mapping for those functions that get vdev only
  1104. cam->udev = dev;
  1105. cam->bulkEndpoint = bulkEndpoint;
  1106. if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1) == -1) {
  1107. kfree(cam);
  1108. printk(KERN_WARNING "video_register_device failed\n");
  1109. return -EIO;
  1110. }
  1111. vicam_create_proc_entry(cam);
  1112. printk(KERN_INFO "ViCam webcam driver now controlling video device %d\n",cam->vdev.minor);
  1113. usb_set_intfdata (intf, cam);
  1114. return 0;
  1115. }
  1116. static void
  1117. vicam_disconnect(struct usb_interface *intf)
  1118. {
  1119. int open_count;
  1120. struct vicam_camera *cam = usb_get_intfdata (intf);
  1121. usb_set_intfdata (intf, NULL);
  1122. /* we must unregister the device before taking its
  1123. * cam_lock. This is because the video open call
  1124. * holds the same lock as video unregister. if we
  1125. * unregister inside of the cam_lock and open also
  1126. * uses the cam_lock, we get deadlock.
  1127. */
  1128. video_unregister_device(&cam->vdev);
  1129. /* stop the camera from being used */
  1130. mutex_lock(&cam->cam_lock);
  1131. /* mark the camera as gone */
  1132. cam->udev = NULL;
  1133. vicam_destroy_proc_entry(cam);
  1134. /* the only thing left to do is synchronize with
  1135. * our close/release function on who should release
  1136. * the camera memory. if there are any users using the
  1137. * camera, it's their job. if there are no users,
  1138. * it's ours.
  1139. */
  1140. open_count = cam->open_count;
  1141. mutex_unlock(&cam->cam_lock);
  1142. if (!open_count) {
  1143. kfree(cam);
  1144. }
  1145. printk(KERN_DEBUG "ViCam-based WebCam disconnected\n");
  1146. }
  1147. /*
  1148. */
  1149. static int __init
  1150. usb_vicam_init(void)
  1151. {
  1152. int retval;
  1153. DBG(KERN_INFO "ViCam-based WebCam driver startup\n");
  1154. vicam_create_proc_root();
  1155. retval = usb_register(&vicam_driver);
  1156. if (retval)
  1157. printk(KERN_WARNING "usb_register failed!\n");
  1158. return retval;
  1159. }
  1160. static void __exit
  1161. usb_vicam_exit(void)
  1162. {
  1163. DBG(KERN_INFO
  1164. "ViCam-based WebCam driver shutdown\n");
  1165. usb_deregister(&vicam_driver);
  1166. vicam_destroy_proc_root();
  1167. }
  1168. module_init(usb_vicam_init);
  1169. module_exit(usb_vicam_exit);
  1170. MODULE_AUTHOR(DRIVER_AUTHOR);
  1171. MODULE_DESCRIPTION(DRIVER_DESC);
  1172. MODULE_LICENSE("GPL");