vicam.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. }
  674. /* the videodev_lock held above us protects us from
  675. * simultaneous opens...for now. we probably shouldn't
  676. * rely on this fact forever.
  677. */
  678. if (cam->open_count > 0) {
  679. printk(KERN_INFO
  680. "vicam_open called on already opened camera");
  681. return -EBUSY;
  682. }
  683. cam->raw_image = kmalloc(VICAM_MAX_READ_SIZE, GFP_KERNEL);
  684. if (!cam->raw_image) {
  685. return -ENOMEM;
  686. }
  687. cam->framebuf = rvmalloc(VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  688. if (!cam->framebuf) {
  689. kfree(cam->raw_image);
  690. return -ENOMEM;
  691. }
  692. cam->cntrlbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  693. if (!cam->cntrlbuf) {
  694. kfree(cam->raw_image);
  695. rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  696. return -ENOMEM;
  697. }
  698. // First upload firmware, then turn the camera on
  699. if (!cam->is_initialized) {
  700. initialize_camera(cam);
  701. cam->is_initialized = 1;
  702. }
  703. set_camera_power(cam, 1);
  704. cam->needsDummyRead = 1;
  705. cam->open_count++;
  706. file->private_data = cam;
  707. return 0;
  708. }
  709. static int
  710. vicam_close(struct inode *inode, struct file *file)
  711. {
  712. struct vicam_camera *cam = file->private_data;
  713. int open_count;
  714. struct usb_device *udev;
  715. DBG("close\n");
  716. /* it's not the end of the world if
  717. * we fail to turn the camera off.
  718. */
  719. set_camera_power(cam, 0);
  720. kfree(cam->raw_image);
  721. rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  722. kfree(cam->cntrlbuf);
  723. mutex_lock(&cam->cam_lock);
  724. cam->open_count--;
  725. open_count = cam->open_count;
  726. udev = cam->udev;
  727. mutex_unlock(&cam->cam_lock);
  728. if (!open_count && !udev) {
  729. kfree(cam);
  730. }
  731. return 0;
  732. }
  733. static void vicam_decode_color(const u8 *data, u8 *rgb)
  734. {
  735. /* vicam_decode_color - Convert from Vicam Y-Cr-Cb to RGB
  736. * Copyright (C) 2002 Monroe Williams (monroe@pobox.com)
  737. */
  738. int i, prevY, nextY;
  739. prevY = 512;
  740. nextY = 512;
  741. data += VICAM_HEADER_SIZE;
  742. for( i = 0; i < 240; i++, data += 512 ) {
  743. const int y = ( i * 242 ) / 240;
  744. int j, prevX, nextX;
  745. int Y, Cr, Cb;
  746. if ( y == 242 - 1 ) {
  747. nextY = -512;
  748. }
  749. prevX = 1;
  750. nextX = 1;
  751. for ( j = 0; j < 320; j++, rgb += 3 ) {
  752. const int x = ( j * 512 ) / 320;
  753. const u8 * const src = &data[x];
  754. if ( x == 512 - 1 ) {
  755. nextX = -1;
  756. }
  757. Cr = ( src[prevX] - src[0] ) +
  758. ( src[nextX] - src[0] );
  759. Cr /= 2;
  760. Cb = ( src[prevY] - src[prevX + prevY] ) +
  761. ( src[prevY] - src[nextX + prevY] ) +
  762. ( src[nextY] - src[prevX + nextY] ) +
  763. ( src[nextY] - src[nextX + nextY] );
  764. Cb /= 4;
  765. Y = 1160 * ( src[0] + ( Cr / 2 ) - 16 );
  766. if ( i & 1 ) {
  767. int Ct = Cr;
  768. Cr = Cb;
  769. Cb = Ct;
  770. }
  771. if ( ( x ^ i ) & 1 ) {
  772. Cr = -Cr;
  773. Cb = -Cb;
  774. }
  775. rgb[0] = clamp( ( ( Y + ( 2017 * Cb ) ) +
  776. 500 ) / 900, 0, 255 );
  777. rgb[1] = clamp( ( ( Y - ( 392 * Cb ) -
  778. ( 813 * Cr ) ) +
  779. 500 ) / 1000, 0, 255 );
  780. rgb[2] = clamp( ( ( Y + ( 1594 * Cr ) ) +
  781. 500 ) / 1300, 0, 255 );
  782. prevX = -1;
  783. }
  784. prevY = -512;
  785. }
  786. }
  787. static void
  788. read_frame(struct vicam_camera *cam, int framenum)
  789. {
  790. unsigned char *request = cam->cntrlbuf;
  791. int realShutter;
  792. int n;
  793. int actual_length;
  794. if (cam->needsDummyRead) {
  795. cam->needsDummyRead = 0;
  796. read_frame(cam, framenum);
  797. }
  798. memset(request, 0, 16);
  799. request[0] = cam->gain; // 0 = 0% gain, FF = 100% gain
  800. request[1] = 0; // 512x242 capture
  801. request[2] = 0x90; // the function of these two bytes
  802. request[3] = 0x07; // is not yet understood
  803. if (cam->shutter_speed > 60) {
  804. // Short exposure
  805. realShutter =
  806. ((-15631900 / cam->shutter_speed) + 260533) / 1000;
  807. request[4] = realShutter & 0xFF;
  808. request[5] = (realShutter >> 8) & 0xFF;
  809. request[6] = 0x03;
  810. request[7] = 0x01;
  811. } else {
  812. // Long exposure
  813. realShutter = 15600 / cam->shutter_speed - 1;
  814. request[4] = 0;
  815. request[5] = 0;
  816. request[6] = realShutter & 0xFF;
  817. request[7] = realShutter >> 8;
  818. }
  819. // Per John Markus Bjørndalen, byte at index 8 causes problems if it isn't 0
  820. request[8] = 0;
  821. // bytes 9-15 do not seem to affect exposure or image quality
  822. mutex_lock(&cam->cam_lock);
  823. if (!cam->udev) {
  824. goto done;
  825. }
  826. n = __send_control_msg(cam, 0x51, 0x80, 0, request, 16);
  827. if (n < 0) {
  828. printk(KERN_ERR
  829. " Problem sending frame capture control message");
  830. goto done;
  831. }
  832. n = usb_bulk_msg(cam->udev,
  833. usb_rcvbulkpipe(cam->udev, cam->bulkEndpoint),
  834. cam->raw_image,
  835. 512 * 242 + 128, &actual_length, 10000);
  836. if (n < 0) {
  837. printk(KERN_ERR "Problem during bulk read of frame data: %d\n",
  838. n);
  839. }
  840. done:
  841. mutex_unlock(&cam->cam_lock);
  842. }
  843. static ssize_t
  844. vicam_read( struct file *file, char __user *buf, size_t count, loff_t *ppos )
  845. {
  846. struct vicam_camera *cam = file->private_data;
  847. DBG("read %d bytes.\n", (int) count);
  848. if (*ppos >= VICAM_MAX_FRAME_SIZE) {
  849. *ppos = 0;
  850. return 0;
  851. }
  852. if (*ppos == 0) {
  853. read_frame(cam, 0);
  854. vicam_decode_color(cam->raw_image,
  855. cam->framebuf +
  856. 0 * VICAM_MAX_FRAME_SIZE);
  857. }
  858. count = min_t(size_t, count, VICAM_MAX_FRAME_SIZE - *ppos);
  859. if (copy_to_user(buf, &cam->framebuf[*ppos], count)) {
  860. count = -EFAULT;
  861. } else {
  862. *ppos += count;
  863. }
  864. if (count == VICAM_MAX_FRAME_SIZE) {
  865. *ppos = 0;
  866. }
  867. return count;
  868. }
  869. static int
  870. vicam_mmap(struct file *file, struct vm_area_struct *vma)
  871. {
  872. // TODO: allocate the raw frame buffer if necessary
  873. unsigned long page, pos;
  874. unsigned long start = vma->vm_start;
  875. unsigned long size = vma->vm_end-vma->vm_start;
  876. struct vicam_camera *cam = file->private_data;
  877. if (!cam)
  878. return -ENODEV;
  879. DBG("vicam_mmap: %ld\n", size);
  880. /* We let mmap allocate as much as it wants because Linux was adding 2048 bytes
  881. * to the size the application requested for mmap and it was screwing apps up.
  882. if (size > VICAM_FRAMES*VICAM_MAX_FRAME_SIZE)
  883. return -EINVAL;
  884. */
  885. pos = (unsigned long)cam->framebuf;
  886. while (size > 0) {
  887. page = vmalloc_to_pfn((void *)pos);
  888. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  889. return -EAGAIN;
  890. start += PAGE_SIZE;
  891. pos += PAGE_SIZE;
  892. if (size > PAGE_SIZE)
  893. size -= PAGE_SIZE;
  894. else
  895. size = 0;
  896. }
  897. return 0;
  898. }
  899. #if defined(CONFIG_VIDEO_PROC_FS)
  900. static struct proc_dir_entry *vicam_proc_root = NULL;
  901. static int vicam_read_helper(char *page, char **start, off_t off,
  902. int count, int *eof, int value)
  903. {
  904. char *out = page;
  905. int len;
  906. out += sprintf(out, "%d",value);
  907. len = out - page;
  908. len -= off;
  909. if (len < count) {
  910. *eof = 1;
  911. if (len <= 0)
  912. return 0;
  913. } else
  914. len = count;
  915. *start = page + off;
  916. return len;
  917. }
  918. static int vicam_read_proc_shutter(char *page, char **start, off_t off,
  919. int count, int *eof, void *data)
  920. {
  921. return vicam_read_helper(page,start,off,count,eof,
  922. ((struct vicam_camera *)data)->shutter_speed);
  923. }
  924. static int vicam_read_proc_gain(char *page, char **start, off_t off,
  925. int count, int *eof, void *data)
  926. {
  927. return vicam_read_helper(page,start,off,count,eof,
  928. ((struct vicam_camera *)data)->gain);
  929. }
  930. static int
  931. vicam_write_proc_shutter(struct file *file, const char *buffer,
  932. unsigned long count, void *data)
  933. {
  934. u16 stmp;
  935. char kbuf[8];
  936. struct vicam_camera *cam = (struct vicam_camera *) data;
  937. if (count > 6)
  938. return -EINVAL;
  939. if (copy_from_user(kbuf, buffer, count))
  940. return -EFAULT;
  941. stmp = (u16) simple_strtoul(kbuf, NULL, 10);
  942. if (stmp < 4 || stmp > 32000)
  943. return -EINVAL;
  944. cam->shutter_speed = stmp;
  945. return count;
  946. }
  947. static int
  948. vicam_write_proc_gain(struct file *file, const char *buffer,
  949. unsigned long count, void *data)
  950. {
  951. u16 gtmp;
  952. char kbuf[8];
  953. struct vicam_camera *cam = (struct vicam_camera *) data;
  954. if (count > 4)
  955. return -EINVAL;
  956. if (copy_from_user(kbuf, buffer, count))
  957. return -EFAULT;
  958. gtmp = (u16) simple_strtoul(kbuf, NULL, 10);
  959. if (gtmp > 255)
  960. return -EINVAL;
  961. cam->gain = gtmp;
  962. return count;
  963. }
  964. static void
  965. vicam_create_proc_root(void)
  966. {
  967. vicam_proc_root = proc_mkdir("video/vicam", NULL);
  968. if (vicam_proc_root)
  969. vicam_proc_root->owner = THIS_MODULE;
  970. else
  971. printk(KERN_ERR
  972. "could not create /proc entry for vicam!");
  973. }
  974. static void
  975. vicam_destroy_proc_root(void)
  976. {
  977. if (vicam_proc_root)
  978. remove_proc_entry("video/vicam", 0);
  979. }
  980. static void
  981. vicam_create_proc_entry(struct vicam_camera *cam)
  982. {
  983. char name[64];
  984. struct proc_dir_entry *ent;
  985. DBG(KERN_INFO "vicam: creating proc entry\n");
  986. if (!vicam_proc_root || !cam) {
  987. printk(KERN_INFO
  988. "vicam: could not create proc entry, %s pointer is null.\n",
  989. (!cam ? "camera" : "root"));
  990. return;
  991. }
  992. sprintf(name, "video%d", cam->vdev.minor);
  993. cam->proc_dir = proc_mkdir(name, vicam_proc_root);
  994. if ( !cam->proc_dir )
  995. return; // FIXME: We should probably return an error here
  996. ent = create_proc_entry("shutter", S_IFREG | S_IRUGO | S_IWUSR,
  997. cam->proc_dir);
  998. if (ent) {
  999. ent->data = cam;
  1000. ent->read_proc = vicam_read_proc_shutter;
  1001. ent->write_proc = vicam_write_proc_shutter;
  1002. ent->size = 64;
  1003. }
  1004. ent = create_proc_entry("gain", S_IFREG | S_IRUGO | S_IWUSR,
  1005. cam->proc_dir);
  1006. if (ent) {
  1007. ent->data = cam;
  1008. ent->read_proc = vicam_read_proc_gain;
  1009. ent->write_proc = vicam_write_proc_gain;
  1010. ent->size = 64;
  1011. }
  1012. }
  1013. static void
  1014. vicam_destroy_proc_entry(void *ptr)
  1015. {
  1016. struct vicam_camera *cam = (struct vicam_camera *) ptr;
  1017. char name[16];
  1018. if ( !cam->proc_dir )
  1019. return;
  1020. sprintf(name, "video%d", cam->vdev.minor);
  1021. remove_proc_entry("shutter", cam->proc_dir);
  1022. remove_proc_entry("gain", cam->proc_dir);
  1023. remove_proc_entry(name,vicam_proc_root);
  1024. cam->proc_dir = NULL;
  1025. }
  1026. #else
  1027. static inline void vicam_create_proc_root(void) { }
  1028. static inline void vicam_destroy_proc_root(void) { }
  1029. static inline void vicam_create_proc_entry(struct vicam_camera *cam) { }
  1030. static inline void vicam_destroy_proc_entry(void *ptr) { }
  1031. #endif
  1032. static struct file_operations vicam_fops = {
  1033. .owner = THIS_MODULE,
  1034. .open = vicam_open,
  1035. .release = vicam_close,
  1036. .read = vicam_read,
  1037. .mmap = vicam_mmap,
  1038. .ioctl = vicam_ioctl,
  1039. .compat_ioctl = v4l_compat_ioctl32,
  1040. .llseek = no_llseek,
  1041. };
  1042. static struct video_device vicam_template = {
  1043. .owner = THIS_MODULE,
  1044. .name = "ViCam-based USB Camera",
  1045. .type = VID_TYPE_CAPTURE,
  1046. .hardware = VID_HARDWARE_VICAM,
  1047. .fops = &vicam_fops,
  1048. .minor = -1,
  1049. };
  1050. /* table of devices that work with this driver */
  1051. static struct usb_device_id vicam_table[] = {
  1052. {USB_DEVICE(USB_VICAM_VENDOR_ID, USB_VICAM_PRODUCT_ID)},
  1053. {} /* Terminating entry */
  1054. };
  1055. MODULE_DEVICE_TABLE(usb, vicam_table);
  1056. static struct usb_driver vicam_driver = {
  1057. .name = "vicam",
  1058. .probe = vicam_probe,
  1059. .disconnect = vicam_disconnect,
  1060. .id_table = vicam_table
  1061. };
  1062. /**
  1063. * vicam_probe
  1064. * @intf: the interface
  1065. * @id: the device id
  1066. *
  1067. * Called by the usb core when a new device is connected that it thinks
  1068. * this driver might be interested in.
  1069. */
  1070. static int
  1071. vicam_probe( struct usb_interface *intf, const struct usb_device_id *id)
  1072. {
  1073. struct usb_device *dev = interface_to_usbdev(intf);
  1074. int bulkEndpoint = 0;
  1075. const struct usb_host_interface *interface;
  1076. const struct usb_endpoint_descriptor *endpoint;
  1077. struct vicam_camera *cam;
  1078. printk(KERN_INFO "ViCam based webcam connected\n");
  1079. interface = intf->cur_altsetting;
  1080. DBG(KERN_DEBUG "Interface %d. has %u. endpoints!\n",
  1081. interface->desc.bInterfaceNumber, (unsigned) (interface->desc.bNumEndpoints));
  1082. endpoint = &interface->endpoint[0].desc;
  1083. if ((endpoint->bEndpointAddress & 0x80) &&
  1084. ((endpoint->bmAttributes & 3) == 0x02)) {
  1085. /* we found a bulk in endpoint */
  1086. bulkEndpoint = endpoint->bEndpointAddress;
  1087. } else {
  1088. printk(KERN_ERR
  1089. "No bulk in endpoint was found ?! (this is bad)\n");
  1090. }
  1091. if ((cam =
  1092. kmalloc(sizeof (struct vicam_camera), GFP_KERNEL)) == NULL) {
  1093. printk(KERN_WARNING
  1094. "could not allocate kernel memory for vicam_camera struct\n");
  1095. return -ENOMEM;
  1096. }
  1097. memset(cam, 0, sizeof (struct vicam_camera));
  1098. cam->shutter_speed = 15;
  1099. mutex_init(&cam->cam_lock);
  1100. memcpy(&cam->vdev, &vicam_template,
  1101. sizeof (vicam_template));
  1102. cam->vdev.priv = cam; // sort of a reverse mapping for those functions that get vdev only
  1103. cam->udev = dev;
  1104. cam->bulkEndpoint = bulkEndpoint;
  1105. if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1) == -1) {
  1106. kfree(cam);
  1107. printk(KERN_WARNING "video_register_device failed\n");
  1108. return -EIO;
  1109. }
  1110. vicam_create_proc_entry(cam);
  1111. printk(KERN_INFO "ViCam webcam driver now controlling video device %d\n",cam->vdev.minor);
  1112. usb_set_intfdata (intf, cam);
  1113. return 0;
  1114. }
  1115. static void
  1116. vicam_disconnect(struct usb_interface *intf)
  1117. {
  1118. int open_count;
  1119. struct vicam_camera *cam = usb_get_intfdata (intf);
  1120. usb_set_intfdata (intf, NULL);
  1121. /* we must unregister the device before taking its
  1122. * cam_lock. This is because the video open call
  1123. * holds the same lock as video unregister. if we
  1124. * unregister inside of the cam_lock and open also
  1125. * uses the cam_lock, we get deadlock.
  1126. */
  1127. video_unregister_device(&cam->vdev);
  1128. /* stop the camera from being used */
  1129. mutex_lock(&cam->cam_lock);
  1130. /* mark the camera as gone */
  1131. cam->udev = NULL;
  1132. vicam_destroy_proc_entry(cam);
  1133. /* the only thing left to do is synchronize with
  1134. * our close/release function on who should release
  1135. * the camera memory. if there are any users using the
  1136. * camera, it's their job. if there are no users,
  1137. * it's ours.
  1138. */
  1139. open_count = cam->open_count;
  1140. mutex_unlock(&cam->cam_lock);
  1141. if (!open_count) {
  1142. kfree(cam);
  1143. }
  1144. printk(KERN_DEBUG "ViCam-based WebCam disconnected\n");
  1145. }
  1146. /*
  1147. */
  1148. static int __init
  1149. usb_vicam_init(void)
  1150. {
  1151. int retval;
  1152. DBG(KERN_INFO "ViCam-based WebCam driver startup\n");
  1153. vicam_create_proc_root();
  1154. retval = usb_register(&vicam_driver);
  1155. if (retval)
  1156. printk(KERN_WARNING "usb_register failed!\n");
  1157. return retval;
  1158. }
  1159. static void __exit
  1160. usb_vicam_exit(void)
  1161. {
  1162. DBG(KERN_INFO
  1163. "ViCam-based WebCam driver shutdown\n");
  1164. usb_deregister(&vicam_driver);
  1165. vicam_destroy_proc_root();
  1166. }
  1167. module_init(usb_vicam_init);
  1168. module_exit(usb_vicam_exit);
  1169. MODULE_AUTHOR(DRIVER_AUTHOR);
  1170. MODULE_DESCRIPTION(DRIVER_DESC);
  1171. MODULE_LICENSE("GPL");