vicam.c 37 KB

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