vicam.c 37 KB

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