vicam.c 33 KB

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