vicam.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. /*
  2. * USB ViCam WebCam driver
  3. * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
  4. * Christopher L Cheney (ccheney@cheney.cx),
  5. * Pavel Machek (pavel@suse.cz),
  6. * John Tyner (jtyner@cs.ucr.edu),
  7. * Monroe Williams (monroe@pobox.com)
  8. *
  9. * Supports 3COM HomeConnect PC Digital WebCam
  10. * Supports Compro PS39U WebCam
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * This source code is based heavily on the CPiA webcam driver which was
  27. * written by Peter Pregler, Scott J. Bertin and Johannes Erdfelt
  28. *
  29. * Portions of this code were also copied from usbvideo.c
  30. *
  31. * Special thanks to the whole team at Sourceforge for help making
  32. * this driver become a reality. Notably:
  33. * Andy Armstrong who reverse engineered the color encoding and
  34. * Pavel Machek and Chris Cheney who worked on reverse engineering the
  35. * camera controls and wrote the first generation driver.
  36. */
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/videodev.h>
  41. #include <linux/usb.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/slab.h>
  44. #include <linux/mutex.h>
  45. #include "usbvideo.h"
  46. // #define VICAM_DEBUG
  47. #ifdef VICAM_DEBUG
  48. #define ADBG(lineno,fmt,args...) printk(fmt, jiffies, __FUNCTION__, lineno, ##args)
  49. #define DBG(fmt,args...) ADBG((__LINE__),KERN_DEBUG __FILE__"(%ld):%s (%d):"fmt,##args)
  50. #else
  51. #define DBG(fmn,args...) do {} while(0)
  52. #endif
  53. #define DRIVER_AUTHOR "Joe Burks, jburks@wavicle.org"
  54. #define DRIVER_DESC "ViCam WebCam Driver"
  55. /* Define these values to match your device */
  56. #define USB_VICAM_VENDOR_ID 0x04c1
  57. #define USB_VICAM_PRODUCT_ID 0x009d
  58. #define USB_COMPRO_VENDOR_ID 0x0602
  59. #define USB_COMPRO_PRODUCT_ID 0x1001
  60. #define VICAM_BYTES_PER_PIXEL 3
  61. #define VICAM_MAX_READ_SIZE (512*242+128)
  62. #define VICAM_MAX_FRAME_SIZE (VICAM_BYTES_PER_PIXEL*320*240)
  63. #define VICAM_FRAMES 2
  64. #define VICAM_HEADER_SIZE 64
  65. #define clamp( x, l, h ) max_t( __typeof__( x ), \
  66. ( l ), \
  67. min_t( __typeof__( x ), \
  68. ( h ), \
  69. ( x ) ) )
  70. /* Not sure what all the bytes in these char
  71. * arrays do, but they're necessary to make
  72. * the camera work.
  73. */
  74. static unsigned char setup1[] = {
  75. 0xB6, 0xC3, 0x1F, 0x00, 0x02, 0x64, 0xE7, 0x67,
  76. 0xFD, 0xFF, 0x0E, 0xC0, 0xE7, 0x09, 0xDE, 0x00,
  77. 0x8E, 0x00, 0xC0, 0x09, 0x40, 0x03, 0xC0, 0x17,
  78. 0x44, 0x03, 0x4B, 0xAF, 0xC0, 0x07, 0x00, 0x00,
  79. 0x4B, 0xAF, 0x97, 0xCF, 0x00, 0x00
  80. };
  81. static unsigned char setup2[] = {
  82. 0xB6, 0xC3, 0x03, 0x00, 0x03, 0x64, 0x18, 0x00,
  83. 0x00, 0x00
  84. };
  85. static unsigned char setup3[] = {
  86. 0xB6, 0xC3, 0x01, 0x00, 0x06, 0x64, 0x00, 0x00
  87. };
  88. static unsigned char setup4[] = {
  89. 0xB6, 0xC3, 0x8F, 0x06, 0x02, 0x64, 0xE7, 0x07,
  90. 0x00, 0x00, 0x08, 0xC0, 0xE7, 0x07, 0x00, 0x00,
  91. 0x3E, 0xC0, 0xE7, 0x07, 0x54, 0x01, 0xAA, 0x00,
  92. 0xE7, 0x07, 0xC8, 0x05, 0xB6, 0x00, 0xE7, 0x07,
  93. 0x42, 0x01, 0xD2, 0x00, 0xE7, 0x07, 0x7C, 0x00,
  94. 0x16, 0x00, 0xE7, 0x07, 0x56, 0x00, 0x18, 0x00,
  95. 0xE7, 0x07, 0x06, 0x00, 0x92, 0xC0, 0xE7, 0x07,
  96. 0x00, 0x00, 0x1E, 0xC0, 0xE7, 0x07, 0xFF, 0xFF,
  97. 0x22, 0xC0, 0xE7, 0x07, 0x04, 0x00, 0x24, 0xC0,
  98. 0xE7, 0x07, 0xEC, 0x27, 0x28, 0xC0, 0xE7, 0x07,
  99. 0x16, 0x01, 0x8E, 0x00, 0xE7, 0x87, 0x01, 0x00,
  100. 0x0E, 0xC0, 0x97, 0xCF, 0xD7, 0x09, 0x00, 0xC0,
  101. 0xE7, 0x77, 0x01, 0x00, 0x92, 0xC0, 0x09, 0xC1,
  102. 0xE7, 0x09, 0xFE, 0x05, 0x24, 0x01, 0xE7, 0x09,
  103. 0x04, 0x06, 0x26, 0x01, 0xE7, 0x07, 0x07, 0x00,
  104. 0x92, 0xC0, 0xE7, 0x05, 0x00, 0xC0, 0xC0, 0xDF,
  105. 0x97, 0xCF, 0x17, 0x00, 0x57, 0x00, 0x17, 0x02,
  106. 0xD7, 0x09, 0x00, 0xC0, 0xE7, 0x77, 0x01, 0x00,
  107. 0x92, 0xC0, 0x0A, 0xC1, 0xE7, 0x57, 0xFF, 0xFF,
  108. 0xFA, 0x05, 0x0D, 0xC0, 0xE7, 0x57, 0x00, 0x00,
  109. 0xFA, 0x05, 0x0F, 0xC0, 0x9F, 0xAF, 0xC6, 0x00,
  110. 0xE7, 0x05, 0x00, 0xC0, 0xC8, 0x05, 0xC1, 0x05,
  111. 0xC0, 0x05, 0xC0, 0xDF, 0x97, 0xCF, 0x27, 0xDA,
  112. 0xFA, 0x05, 0xEF, 0x07, 0x01, 0x00, 0x0B, 0x06,
  113. 0x73, 0xCF, 0x9F, 0xAF, 0x78, 0x01, 0x9F, 0xAF,
  114. 0x1A, 0x03, 0x6E, 0xCF, 0xE7, 0x09, 0xFC, 0x05,
  115. 0x24, 0x01, 0xE7, 0x09, 0x02, 0x06, 0x26, 0x01,
  116. 0xE7, 0x07, 0x07, 0x00, 0x92, 0xC0, 0xE7, 0x09,
  117. 0xFC, 0x05, 0xFE, 0x05, 0xE7, 0x09, 0x02, 0x06,
  118. 0x04, 0x06, 0xE7, 0x09, 0x00, 0x06, 0xFC, 0x05,
  119. 0xE7, 0x09, 0xFE, 0x05, 0x00, 0x06, 0x27, 0xDA,
  120. 0xFA, 0x05, 0xE7, 0x57, 0x01, 0x00, 0xFA, 0x05,
  121. 0x02, 0xCA, 0x04, 0xC0, 0x97, 0xCF, 0x9F, 0xAF,
  122. 0x66, 0x05, 0x97, 0xCF, 0xE7, 0x07, 0x40, 0x00,
  123. 0x02, 0x06, 0xC8, 0x09, 0xFC, 0x05, 0x9F, 0xAF,
  124. 0xDA, 0x02, 0x97, 0xCF, 0xCF, 0x17, 0x02, 0x00,
  125. 0xEF, 0x57, 0x81, 0x00, 0x09, 0x06, 0x9F, 0xA0,
  126. 0xB6, 0x01, 0xEF, 0x57, 0x80, 0x00, 0x09, 0x06,
  127. 0x9F, 0xA0, 0x40, 0x02, 0xEF, 0x57, 0x01, 0x00,
  128. 0x0B, 0x06, 0x9F, 0xA0, 0x46, 0x03, 0xE7, 0x07,
  129. 0x01, 0x00, 0x0A, 0xC0, 0x46, 0xAF, 0x47, 0xAF,
  130. 0x9F, 0xAF, 0x40, 0x02, 0xE7, 0x07, 0x2E, 0x00,
  131. 0x0A, 0xC0, 0xEF, 0x87, 0x80, 0x00, 0x09, 0x06,
  132. 0x97, 0xCF, 0x00, 0x0E, 0x01, 0x00, 0xC0, 0x57,
  133. 0x51, 0x00, 0x9F, 0xC0, 0x9E, 0x02, 0xC0, 0x57,
  134. 0x50, 0x00, 0x20, 0xC0, 0xC0, 0x57, 0x55, 0x00,
  135. 0x12, 0xC0, 0xC0, 0x57, 0x56, 0x00, 0x9F, 0xC0,
  136. 0x72, 0x02, 0x9F, 0xCF, 0xD6, 0x02, 0xC1, 0x0B,
  137. 0x08, 0x06, 0x01, 0xD0, 0x6F, 0x90, 0x08, 0x06,
  138. 0xC0, 0x07, 0x08, 0x00, 0xC1, 0x0B, 0x08, 0x06,
  139. 0x9F, 0xAF, 0x28, 0x05, 0x97, 0xCF, 0x2F, 0x0E,
  140. 0x02, 0x00, 0x08, 0x06, 0xC0, 0x07, 0x08, 0x00,
  141. 0xC1, 0x0B, 0x08, 0x06, 0x9F, 0xAF, 0x28, 0x05,
  142. 0x9F, 0xCF, 0xD6, 0x02, 0x2F, 0x0E, 0x02, 0x00,
  143. 0x09, 0x06, 0xEF, 0x87, 0x80, 0x00, 0x09, 0x06,
  144. 0x9F, 0xCF, 0xD6, 0x02, 0xEF, 0x67, 0x7F, 0xFF,
  145. 0x09, 0x06, 0xE7, 0x67, 0xFF, 0xFD, 0x22, 0xC0,
  146. 0xE7, 0x67, 0xEF, 0xFF, 0x24, 0xC0, 0xE7, 0x87,
  147. 0x10, 0x00, 0x28, 0xC0, 0x9F, 0xAF, 0xB8, 0x05,
  148. 0xE7, 0x87, 0xE0, 0x21, 0x24, 0xC0, 0x9F, 0xAF,
  149. 0xA8, 0x05, 0xE7, 0x87, 0x08, 0x00, 0x24, 0xC0,
  150. 0xE7, 0x67, 0xDF, 0xFF, 0x24, 0xC0, 0xC8, 0x07,
  151. 0x0A, 0x00, 0xC0, 0x07, 0x00, 0x00, 0xC1, 0x07,
  152. 0x01, 0x00, 0x9F, 0xAF, 0x28, 0x05, 0x9F, 0xAF,
  153. 0xB8, 0x05, 0xC0, 0x07, 0x9E, 0x00, 0x9F, 0xAF,
  154. 0x44, 0x05, 0xE7, 0x67, 0xFF, 0xFE, 0x24, 0xC0,
  155. 0xC0, 0x09, 0x20, 0xC0, 0xE7, 0x87, 0x00, 0x01,
  156. 0x24, 0xC0, 0xC0, 0x77, 0x00, 0x02, 0x0F, 0xC1,
  157. 0xE7, 0x67, 0xF7, 0xFF, 0x24, 0xC0, 0xE7, 0x67,
  158. 0xF7, 0xFF, 0x24, 0xC0, 0xE7, 0x87, 0x08, 0x00,
  159. 0x24, 0xC0, 0x08, 0xDA, 0x5E, 0xC1, 0xEF, 0x07,
  160. 0x80, 0x00, 0x09, 0x06, 0x97, 0xCF, 0xEF, 0x07,
  161. 0x01, 0x00, 0x0A, 0x06, 0x97, 0xCF, 0xEF, 0x07,
  162. 0x00, 0x00, 0x0B, 0x06, 0xEF, 0x07, 0x00, 0x00,
  163. 0x0A, 0x06, 0xEF, 0x67, 0x7F, 0xFF, 0x09, 0x06,
  164. 0xEF, 0x07, 0x00, 0x00, 0x0D, 0x06, 0xE7, 0x67,
  165. 0xEF, 0xFF, 0x28, 0xC0, 0xE7, 0x67, 0x17, 0xD8,
  166. 0x24, 0xC0, 0xE7, 0x07, 0x00, 0x00, 0x1E, 0xC0,
  167. 0xE7, 0x07, 0xFF, 0xFF, 0x22, 0xC0, 0x97, 0xCF,
  168. 0xC8, 0x07, 0x0E, 0x06, 0x9F, 0xAF, 0xDA, 0x02,
  169. 0xE7, 0x07, 0x00, 0x00, 0xF2, 0x05, 0xE7, 0x07,
  170. 0x10, 0x00, 0xF6, 0x05, 0xE7, 0x07, 0x0E, 0x06,
  171. 0xF4, 0x05, 0xE7, 0x07, 0xD6, 0x02, 0xF8, 0x05,
  172. 0xC8, 0x07, 0xF2, 0x05, 0xC1, 0x07, 0x00, 0x80,
  173. 0x50, 0xAF, 0x97, 0xCF, 0x2F, 0x0C, 0x02, 0x00,
  174. 0x07, 0x06, 0x2F, 0x0C, 0x04, 0x00, 0x06, 0x06,
  175. 0xE7, 0x07, 0x00, 0x00, 0xF2, 0x05, 0xE7, 0x07,
  176. 0x10, 0x00, 0xF6, 0x05, 0xE7, 0x07, 0xE2, 0x05,
  177. 0xF4, 0x05, 0xE7, 0x07, 0xCE, 0x02, 0xF8, 0x05,
  178. 0xC8, 0x07, 0xF2, 0x05, 0xC1, 0x07, 0x00, 0x80,
  179. 0x51, 0xAF, 0x97, 0xCF, 0x9F, 0xAF, 0x66, 0x04,
  180. 0x9F, 0xAF, 0x1A, 0x03, 0x59, 0xAF, 0x97, 0xCF,
  181. 0xC0, 0x07, 0x0E, 0x00, 0xC1, 0x0B, 0x0C, 0x06,
  182. 0x41, 0xD1, 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07,
  183. 0x3C, 0x00, 0x9F, 0xAF, 0x44, 0x05, 0x68, 0x00,
  184. 0xC0, 0x07, 0x3B, 0x00, 0x9F, 0xAF, 0x44, 0x05,
  185. 0x6F, 0x00, 0x0C, 0x06, 0x68, 0x00, 0xE0, 0x07,
  186. 0x04, 0x01, 0xE8, 0x0B, 0x0A, 0x06, 0xE8, 0x07,
  187. 0x00, 0x00, 0xE0, 0x07, 0x00, 0x02, 0xE0, 0x07,
  188. 0xEC, 0x01, 0xE0, 0x07, 0xFC, 0xFF, 0x97, 0xCF,
  189. 0xE7, 0x07, 0xFF, 0xFF, 0xFA, 0x05, 0xEF, 0x07,
  190. 0x00, 0x00, 0x0B, 0x06, 0xE7, 0x07, 0x0E, 0x06,
  191. 0x24, 0x01, 0xE7, 0x07, 0x0E, 0x06, 0xFE, 0x05,
  192. 0xE7, 0x07, 0x40, 0x00, 0x26, 0x01, 0xE7, 0x07,
  193. 0x40, 0x00, 0x04, 0x06, 0xE7, 0x07, 0x07, 0x00,
  194. 0x92, 0xC0, 0x97, 0xCF, 0xEF, 0x07, 0x02, 0x00,
  195. 0x0B, 0x06, 0x9F, 0xAF, 0x78, 0x01, 0xEF, 0x77,
  196. 0x80, 0x00, 0x07, 0x06, 0x9F, 0xC0, 0x14, 0x04,
  197. 0xEF, 0x77, 0x01, 0x00, 0x07, 0x06, 0x37, 0xC0,
  198. 0xEF, 0x77, 0x01, 0x00, 0x0D, 0x06, 0x0F, 0xC1,
  199. 0xEF, 0x07, 0x01, 0x00, 0x0D, 0x06, 0xC0, 0x07,
  200. 0x02, 0x00, 0xC1, 0x07, 0x30, 0x00, 0x9F, 0xAF,
  201. 0x28, 0x05, 0xC0, 0x07, 0x01, 0x00, 0xC1, 0x07,
  202. 0x02, 0x00, 0x9F, 0xAF, 0x28, 0x05, 0xC8, 0x07,
  203. 0xFF, 0x4F, 0x9F, 0xAF, 0xA8, 0x05, 0xC0, 0x07,
  204. 0x38, 0x00, 0x9F, 0xAF, 0x44, 0x05, 0xC1, 0x77,
  205. 0x03, 0x00, 0x02, 0xC1, 0x08, 0xDA, 0x75, 0xC1,
  206. 0xC1, 0x77, 0x01, 0x00, 0x0A, 0xC1, 0xC0, 0x07,
  207. 0x01, 0x00, 0xC1, 0x07, 0x02, 0x00, 0x9F, 0xAF,
  208. 0x28, 0x05, 0xEF, 0x07, 0x01, 0x00, 0x06, 0x06,
  209. 0x2C, 0xCF, 0xC0, 0x07, 0x01, 0x00, 0xC1, 0x07,
  210. 0x04, 0x00, 0x9F, 0xAF, 0x28, 0x05, 0xEF, 0x07,
  211. 0x00, 0x00, 0x06, 0x06, 0x22, 0xCF, 0xEF, 0x07,
  212. 0x00, 0x00, 0x0D, 0x06, 0xEF, 0x57, 0x01, 0x00,
  213. 0x06, 0x06, 0x1B, 0xC0, 0xC0, 0x07, 0x01, 0x00,
  214. 0xC1, 0x07, 0x01, 0x00, 0x9F, 0xAF, 0x28, 0x05,
  215. 0xC0, 0x07, 0x02, 0x00, 0xC1, 0x07, 0x30, 0x00,
  216. 0x9F, 0xAF, 0x28, 0x05, 0xC8, 0x07, 0xFF, 0x4F,
  217. 0x9F, 0xAF, 0xA8, 0x05, 0xC0, 0x07, 0x38, 0x00,
  218. 0x9F, 0xAF, 0x44, 0x05, 0xC1, 0x67, 0x03, 0x00,
  219. 0xC1, 0x57, 0x03, 0x00, 0x02, 0xC0, 0x08, 0xDA,
  220. 0x73, 0xC1, 0xC0, 0x07, 0x02, 0x00, 0xC1, 0x07,
  221. 0x12, 0x00, 0xEF, 0x57, 0x00, 0x00, 0x06, 0x06,
  222. 0x02, 0xC0, 0xC1, 0x07, 0x23, 0x00, 0x9F, 0xAF,
  223. 0x28, 0x05, 0xC0, 0x07, 0x14, 0x00, 0xC1, 0x0B,
  224. 0xEA, 0x05, 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07,
  225. 0x3E, 0x00, 0x9F, 0xAF, 0x0A, 0x05, 0xE7, 0x09,
  226. 0xE4, 0x05, 0xFA, 0x05, 0x27, 0xD8, 0xFA, 0x05,
  227. 0xE7, 0x07, 0x0E, 0x06, 0xFC, 0x05, 0xE7, 0x07,
  228. 0x4E, 0x06, 0x00, 0x06, 0xE7, 0x07, 0x40, 0x00,
  229. 0x02, 0x06, 0x9F, 0xAF, 0x66, 0x05, 0x9F, 0xAF,
  230. 0xC6, 0x00, 0x97, 0xCF, 0xC1, 0x0B, 0xE2, 0x05,
  231. 0x41, 0xD0, 0x01, 0xD2, 0xC1, 0x17, 0x23, 0x00,
  232. 0x9F, 0xAF, 0xDC, 0x04, 0xC0, 0x07, 0x04, 0x00,
  233. 0xC1, 0x0B, 0xE3, 0x05, 0x9F, 0xAF, 0x28, 0x05,
  234. 0xC0, 0x07, 0x06, 0x00, 0xC1, 0x09, 0xE6, 0x05,
  235. 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07, 0x07, 0x00,
  236. 0xC1, 0x09, 0xE6, 0x05, 0xC1, 0xD1, 0x9F, 0xAF,
  237. 0x28, 0x05, 0xC0, 0x07, 0x0B, 0x00, 0xC1, 0x09,
  238. 0xE8, 0x05, 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07,
  239. 0x0C, 0x00, 0xC1, 0x09, 0xE8, 0x05, 0xC1, 0xD1,
  240. 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07, 0x0D, 0x00,
  241. 0xC1, 0x07, 0x09, 0x00, 0x9F, 0xAF, 0x28, 0x05,
  242. 0xC0, 0x07, 0x03, 0x00, 0xC1, 0x07, 0x32, 0x00,
  243. 0x9F, 0xAF, 0x28, 0x05, 0xC0, 0x07, 0x0F, 0x00,
  244. 0xC1, 0x07, 0x00, 0x00, 0x9F, 0xAF, 0x28, 0x05,
  245. 0x97, 0xCF, 0xE7, 0x67, 0xFF, 0xD9, 0x24, 0xC0,
  246. 0xC8, 0x07, 0x0A, 0x00, 0x40, 0x00, 0xC0, 0x67,
  247. 0x00, 0x02, 0x27, 0x80, 0x24, 0xC0, 0xE7, 0x87,
  248. 0x00, 0x04, 0x24, 0xC0, 0xE7, 0x67, 0xFF, 0xF9,
  249. 0x24, 0xC0, 0x01, 0xD2, 0x08, 0xDA, 0x72, 0xC1,
  250. 0xE7, 0x87, 0x00, 0x20, 0x24, 0xC0, 0x97, 0xCF,
  251. 0x27, 0x00, 0x1E, 0xC0, 0xE7, 0x87, 0xFF, 0x00,
  252. 0x22, 0xC0, 0xE7, 0x67, 0x7F, 0xFF, 0x24, 0xC0,
  253. 0xE7, 0x87, 0x80, 0x00, 0x24, 0xC0, 0xE7, 0x87,
  254. 0x80, 0x00, 0x24, 0xC0, 0x97, 0xCF, 0x9F, 0xAF,
  255. 0x0A, 0x05, 0x67, 0x00, 0x1E, 0xC0, 0xE7, 0x67,
  256. 0xBF, 0xFF, 0x24, 0xC0, 0xE7, 0x87, 0x40, 0x00,
  257. 0x24, 0xC0, 0xE7, 0x87, 0x40, 0x00, 0x24, 0xC0,
  258. 0x97, 0xCF, 0x9F, 0xAF, 0x0A, 0x05, 0xE7, 0x67,
  259. 0x00, 0xFF, 0x22, 0xC0, 0xE7, 0x67, 0xFF, 0xFE,
  260. 0x24, 0xC0, 0xE7, 0x67, 0xFF, 0xFE, 0x24, 0xC0,
  261. 0xC1, 0x09, 0x20, 0xC0, 0xE7, 0x87, 0x00, 0x01,
  262. 0x24, 0xC0, 0x97, 0xCF, 0xC0, 0x07, 0x40, 0x00,
  263. 0xC8, 0x09, 0xFC, 0x05, 0xE7, 0x67, 0x00, 0xFF,
  264. 0x22, 0xC0, 0xE7, 0x67, 0xFF, 0xFE, 0x24, 0xC0,
  265. 0xE7, 0x67, 0xBF, 0xFF, 0x24, 0xC0, 0xE7, 0x67,
  266. 0xBF, 0xFF, 0x24, 0xC0, 0x00, 0xDA, 0xE8, 0x09,
  267. 0x20, 0xC0, 0xE7, 0x87, 0x40, 0x00, 0x24, 0xC0,
  268. 0xE7, 0x87, 0x40, 0x00, 0x24, 0xC0, 0x00, 0xDA,
  269. 0xE8, 0x09, 0x20, 0xC0, 0x6D, 0xC1, 0xE7, 0x87,
  270. 0x00, 0x01, 0x24, 0xC0, 0x97, 0xCF, 0xE7, 0x07,
  271. 0x32, 0x00, 0x12, 0xC0, 0xE7, 0x77, 0x00, 0x80,
  272. 0x12, 0xC0, 0x7C, 0xC0, 0x97, 0xCF, 0xE7, 0x07,
  273. 0x20, 0x4E, 0x12, 0xC0, 0xE7, 0x77, 0x00, 0x80,
  274. 0x12, 0xC0, 0x7C, 0xC0, 0x97, 0xCF, 0x09, 0x02,
  275. 0x19, 0x00, 0x01, 0x01, 0x00, 0x80, 0x96, 0x09,
  276. 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  277. 0x07, 0x05, 0x81, 0x02, 0x40, 0x00, 0x00, 0x00,
  278. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  279. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  280. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  281. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  282. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  283. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  284. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  285. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  286. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  287. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  289. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  290. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  291. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  292. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  293. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  294. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  295. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  296. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  297. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  298. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  299. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  300. };
  301. static unsigned char setup5[] = {
  302. 0xB6, 0xC3, 0x2F, 0x01, 0x03, 0x64, 0x0E, 0x00,
  303. 0x14, 0x00, 0x1A, 0x00, 0x20, 0x00, 0x26, 0x00,
  304. 0x4A, 0x00, 0x64, 0x00, 0x6A, 0x00, 0x92, 0x00,
  305. 0x9A, 0x00, 0xA0, 0x00, 0xB2, 0x00, 0xB8, 0x00,
  306. 0xBE, 0x00, 0xC2, 0x00, 0xC8, 0x00, 0xCE, 0x00,
  307. 0xDC, 0x00, 0xDA, 0x00, 0xE2, 0x00, 0xE0, 0x00,
  308. 0xE8, 0x00, 0xE6, 0x00, 0xEE, 0x00, 0xEC, 0x00,
  309. 0xF2, 0x00, 0xF8, 0x00, 0x02, 0x01, 0x0A, 0x01,
  310. 0x0E, 0x01, 0x12, 0x01, 0x1E, 0x01, 0x22, 0x01,
  311. 0x28, 0x01, 0x2C, 0x01, 0x32, 0x01, 0x36, 0x01,
  312. 0x44, 0x01, 0x50, 0x01, 0x5E, 0x01, 0x72, 0x01,
  313. 0x76, 0x01, 0x7A, 0x01, 0x80, 0x01, 0x88, 0x01,
  314. 0x8C, 0x01, 0x94, 0x01, 0x9C, 0x01, 0xA0, 0x01,
  315. 0xA4, 0x01, 0xAA, 0x01, 0xB0, 0x01, 0xB4, 0x01,
  316. 0xBA, 0x01, 0xD0, 0x01, 0xDA, 0x01, 0xF6, 0x01,
  317. 0xFA, 0x01, 0x02, 0x02, 0x34, 0x02, 0x3C, 0x02,
  318. 0x44, 0x02, 0x4A, 0x02, 0x50, 0x02, 0x56, 0x02,
  319. 0x74, 0x02, 0x78, 0x02, 0x7E, 0x02, 0x84, 0x02,
  320. 0x8A, 0x02, 0x88, 0x02, 0x90, 0x02, 0x8E, 0x02,
  321. 0x94, 0x02, 0xA2, 0x02, 0xA8, 0x02, 0xAE, 0x02,
  322. 0xB4, 0x02, 0xBA, 0x02, 0xB8, 0x02, 0xC0, 0x02,
  323. 0xBE, 0x02, 0xC4, 0x02, 0xD0, 0x02, 0xD4, 0x02,
  324. 0xE0, 0x02, 0xE6, 0x02, 0xEE, 0x02, 0xF8, 0x02,
  325. 0xFC, 0x02, 0x06, 0x03, 0x1E, 0x03, 0x24, 0x03,
  326. 0x28, 0x03, 0x30, 0x03, 0x2E, 0x03, 0x3C, 0x03,
  327. 0x4A, 0x03, 0x4E, 0x03, 0x54, 0x03, 0x58, 0x03,
  328. 0x5E, 0x03, 0x66, 0x03, 0x6E, 0x03, 0x7A, 0x03,
  329. 0x86, 0x03, 0x8E, 0x03, 0x96, 0x03, 0xB2, 0x03,
  330. 0xB8, 0x03, 0xC6, 0x03, 0xCC, 0x03, 0xD4, 0x03,
  331. 0xDA, 0x03, 0xE8, 0x03, 0xF4, 0x03, 0xFC, 0x03,
  332. 0x04, 0x04, 0x20, 0x04, 0x2A, 0x04, 0x32, 0x04,
  333. 0x36, 0x04, 0x3E, 0x04, 0x44, 0x04, 0x42, 0x04,
  334. 0x48, 0x04, 0x4E, 0x04, 0x4C, 0x04, 0x54, 0x04,
  335. 0x52, 0x04, 0x5A, 0x04, 0x5E, 0x04, 0x62, 0x04,
  336. 0x68, 0x04, 0x74, 0x04, 0x7C, 0x04, 0x80, 0x04,
  337. 0x88, 0x04, 0x8C, 0x04, 0x94, 0x04, 0x9A, 0x04,
  338. 0xA2, 0x04, 0xA6, 0x04, 0xAE, 0x04, 0xB4, 0x04,
  339. 0xC0, 0x04, 0xCC, 0x04, 0xD8, 0x04, 0x2A, 0x05,
  340. 0x46, 0x05, 0x6C, 0x05, 0x00, 0x00
  341. };
  342. /* rvmalloc / rvfree copied from usbvideo.c
  343. *
  344. * Not sure why these are not yet non-statics which I can reference through
  345. * usbvideo.h the same as it is in 2.4.20. I bet this will get fixed sometime
  346. * in the future.
  347. *
  348. */
  349. static void *rvmalloc(unsigned long size)
  350. {
  351. void *mem;
  352. unsigned long adr;
  353. size = PAGE_ALIGN(size);
  354. mem = vmalloc_32(size);
  355. if (!mem)
  356. return NULL;
  357. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  358. adr = (unsigned long) mem;
  359. while (size > 0) {
  360. SetPageReserved(vmalloc_to_page((void *)adr));
  361. adr += PAGE_SIZE;
  362. size -= PAGE_SIZE;
  363. }
  364. return mem;
  365. }
  366. static void rvfree(void *mem, unsigned long size)
  367. {
  368. unsigned long adr;
  369. if (!mem)
  370. return;
  371. adr = (unsigned long) mem;
  372. while ((long) size > 0) {
  373. ClearPageReserved(vmalloc_to_page((void *)adr));
  374. adr += PAGE_SIZE;
  375. size -= PAGE_SIZE;
  376. }
  377. vfree(mem);
  378. }
  379. struct vicam_camera {
  380. u16 shutter_speed; // capture shutter speed
  381. u16 gain; // capture gain
  382. u8 *raw_image; // raw data captured from the camera
  383. u8 *framebuf; // processed data in RGB24 format
  384. u8 *cntrlbuf; // area used to send control msgs
  385. struct video_device vdev; // v4l video device
  386. struct usb_device *udev; // usb device
  387. /* guard against simultaneous accesses to the camera */
  388. struct mutex cam_lock;
  389. int is_initialized;
  390. u8 open_count;
  391. u8 bulkEndpoint;
  392. int needsDummyRead;
  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. mutex_lock(&cam->cam_lock);
  429. if (cam->udev) {
  430. status = __send_control_msg(cam, request, value,
  431. index, cp, size);
  432. }
  433. mutex_unlock(&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. return -EINVAL;
  673. }
  674. /* the videodev_lock held above us protects us from
  675. * simultaneous opens...for now. we probably shouldn't
  676. * rely on this fact forever.
  677. */
  678. if (cam->open_count > 0) {
  679. printk(KERN_INFO
  680. "vicam_open called on already opened camera");
  681. return -EBUSY;
  682. }
  683. cam->raw_image = kmalloc(VICAM_MAX_READ_SIZE, GFP_KERNEL);
  684. if (!cam->raw_image) {
  685. return -ENOMEM;
  686. }
  687. cam->framebuf = rvmalloc(VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  688. if (!cam->framebuf) {
  689. kfree(cam->raw_image);
  690. return -ENOMEM;
  691. }
  692. cam->cntrlbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  693. if (!cam->cntrlbuf) {
  694. kfree(cam->raw_image);
  695. rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  696. return -ENOMEM;
  697. }
  698. // First upload firmware, then turn the camera on
  699. if (!cam->is_initialized) {
  700. initialize_camera(cam);
  701. cam->is_initialized = 1;
  702. }
  703. set_camera_power(cam, 1);
  704. cam->needsDummyRead = 1;
  705. cam->open_count++;
  706. file->private_data = cam;
  707. return 0;
  708. }
  709. static int
  710. vicam_close(struct inode *inode, struct file *file)
  711. {
  712. struct vicam_camera *cam = file->private_data;
  713. int open_count;
  714. struct usb_device *udev;
  715. DBG("close\n");
  716. /* it's not the end of the world if
  717. * we fail to turn the camera off.
  718. */
  719. set_camera_power(cam, 0);
  720. kfree(cam->raw_image);
  721. rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  722. kfree(cam->cntrlbuf);
  723. mutex_lock(&cam->cam_lock);
  724. cam->open_count--;
  725. open_count = cam->open_count;
  726. udev = cam->udev;
  727. mutex_unlock(&cam->cam_lock);
  728. if (!open_count && !udev) {
  729. kfree(cam);
  730. }
  731. return 0;
  732. }
  733. static void vicam_decode_color(const u8 *data, u8 *rgb)
  734. {
  735. /* vicam_decode_color - Convert from Vicam Y-Cr-Cb to RGB
  736. * Copyright (C) 2002 Monroe Williams (monroe@pobox.com)
  737. */
  738. int i, prevY, nextY;
  739. prevY = 512;
  740. nextY = 512;
  741. data += VICAM_HEADER_SIZE;
  742. for( i = 0; i < 240; i++, data += 512 ) {
  743. const int y = ( i * 242 ) / 240;
  744. int j, prevX, nextX;
  745. int Y, Cr, Cb;
  746. if ( y == 242 - 1 ) {
  747. nextY = -512;
  748. }
  749. prevX = 1;
  750. nextX = 1;
  751. for ( j = 0; j < 320; j++, rgb += 3 ) {
  752. const int x = ( j * 512 ) / 320;
  753. const u8 * const src = &data[x];
  754. if ( x == 512 - 1 ) {
  755. nextX = -1;
  756. }
  757. Cr = ( src[prevX] - src[0] ) +
  758. ( src[nextX] - src[0] );
  759. Cr /= 2;
  760. Cb = ( src[prevY] - src[prevX + prevY] ) +
  761. ( src[prevY] - src[nextX + prevY] ) +
  762. ( src[nextY] - src[prevX + nextY] ) +
  763. ( src[nextY] - src[nextX + nextY] );
  764. Cb /= 4;
  765. Y = 1160 * ( src[0] + ( Cr / 2 ) - 16 );
  766. if ( i & 1 ) {
  767. int Ct = Cr;
  768. Cr = Cb;
  769. Cb = Ct;
  770. }
  771. if ( ( x ^ i ) & 1 ) {
  772. Cr = -Cr;
  773. Cb = -Cb;
  774. }
  775. rgb[0] = clamp( ( ( Y + ( 2017 * Cb ) ) +
  776. 500 ) / 900, 0, 255 );
  777. rgb[1] = clamp( ( ( Y - ( 392 * Cb ) -
  778. ( 813 * Cr ) ) +
  779. 500 ) / 1000, 0, 255 );
  780. rgb[2] = clamp( ( ( Y + ( 1594 * Cr ) ) +
  781. 500 ) / 1300, 0, 255 );
  782. prevX = -1;
  783. }
  784. prevY = -512;
  785. }
  786. }
  787. static void
  788. read_frame(struct vicam_camera *cam, int framenum)
  789. {
  790. unsigned char *request = cam->cntrlbuf;
  791. int realShutter;
  792. int n;
  793. int actual_length;
  794. if (cam->needsDummyRead) {
  795. cam->needsDummyRead = 0;
  796. read_frame(cam, framenum);
  797. }
  798. memset(request, 0, 16);
  799. request[0] = cam->gain; // 0 = 0% gain, FF = 100% gain
  800. request[1] = 0; // 512x242 capture
  801. request[2] = 0x90; // the function of these two bytes
  802. request[3] = 0x07; // is not yet understood
  803. if (cam->shutter_speed > 60) {
  804. // Short exposure
  805. realShutter =
  806. ((-15631900 / cam->shutter_speed) + 260533) / 1000;
  807. request[4] = realShutter & 0xFF;
  808. request[5] = (realShutter >> 8) & 0xFF;
  809. request[6] = 0x03;
  810. request[7] = 0x01;
  811. } else {
  812. // Long exposure
  813. realShutter = 15600 / cam->shutter_speed - 1;
  814. request[4] = 0;
  815. request[5] = 0;
  816. request[6] = realShutter & 0xFF;
  817. request[7] = realShutter >> 8;
  818. }
  819. // Per John Markus Bjørndalen, byte at index 8 causes problems if it isn't 0
  820. request[8] = 0;
  821. // bytes 9-15 do not seem to affect exposure or image quality
  822. mutex_lock(&cam->cam_lock);
  823. if (!cam->udev) {
  824. goto done;
  825. }
  826. n = __send_control_msg(cam, 0x51, 0x80, 0, request, 16);
  827. if (n < 0) {
  828. printk(KERN_ERR
  829. " Problem sending frame capture control message");
  830. goto done;
  831. }
  832. n = usb_bulk_msg(cam->udev,
  833. usb_rcvbulkpipe(cam->udev, cam->bulkEndpoint),
  834. cam->raw_image,
  835. 512 * 242 + 128, &actual_length, 10000);
  836. if (n < 0) {
  837. printk(KERN_ERR "Problem during bulk read of frame data: %d\n",
  838. n);
  839. }
  840. done:
  841. mutex_unlock(&cam->cam_lock);
  842. }
  843. static ssize_t
  844. vicam_read( struct file *file, char __user *buf, size_t count, loff_t *ppos )
  845. {
  846. struct vicam_camera *cam = file->private_data;
  847. DBG("read %d bytes.\n", (int) count);
  848. if (*ppos >= VICAM_MAX_FRAME_SIZE) {
  849. *ppos = 0;
  850. return 0;
  851. }
  852. if (*ppos == 0) {
  853. read_frame(cam, 0);
  854. vicam_decode_color(cam->raw_image,
  855. cam->framebuf +
  856. 0 * VICAM_MAX_FRAME_SIZE);
  857. }
  858. count = min_t(size_t, count, VICAM_MAX_FRAME_SIZE - *ppos);
  859. if (copy_to_user(buf, &cam->framebuf[*ppos], count)) {
  860. count = -EFAULT;
  861. } else {
  862. *ppos += count;
  863. }
  864. if (count == VICAM_MAX_FRAME_SIZE) {
  865. *ppos = 0;
  866. }
  867. return count;
  868. }
  869. static int
  870. vicam_mmap(struct file *file, struct vm_area_struct *vma)
  871. {
  872. // TODO: allocate the raw frame buffer if necessary
  873. unsigned long page, pos;
  874. unsigned long start = vma->vm_start;
  875. unsigned long size = vma->vm_end-vma->vm_start;
  876. struct vicam_camera *cam = file->private_data;
  877. if (!cam)
  878. return -ENODEV;
  879. DBG("vicam_mmap: %ld\n", size);
  880. /* We let mmap allocate as much as it wants because Linux was adding 2048 bytes
  881. * to the size the application requested for mmap and it was screwing apps up.
  882. if (size > VICAM_FRAMES*VICAM_MAX_FRAME_SIZE)
  883. return -EINVAL;
  884. */
  885. pos = (unsigned long)cam->framebuf;
  886. while (size > 0) {
  887. page = vmalloc_to_pfn((void *)pos);
  888. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  889. return -EAGAIN;
  890. start += PAGE_SIZE;
  891. pos += PAGE_SIZE;
  892. if (size > PAGE_SIZE)
  893. size -= PAGE_SIZE;
  894. else
  895. size = 0;
  896. }
  897. return 0;
  898. }
  899. static const struct file_operations vicam_fops = {
  900. .owner = THIS_MODULE,
  901. .open = vicam_open,
  902. .release = vicam_close,
  903. .read = vicam_read,
  904. .mmap = vicam_mmap,
  905. .ioctl = vicam_ioctl,
  906. .compat_ioctl = v4l_compat_ioctl32,
  907. .llseek = no_llseek,
  908. };
  909. static struct video_device vicam_template = {
  910. .owner = THIS_MODULE,
  911. .name = "ViCam-based USB Camera",
  912. .type = VID_TYPE_CAPTURE,
  913. .fops = &vicam_fops,
  914. .minor = -1,
  915. };
  916. /* table of devices that work with this driver */
  917. static struct usb_device_id vicam_table[] = {
  918. {USB_DEVICE(USB_VICAM_VENDOR_ID, USB_VICAM_PRODUCT_ID)},
  919. {USB_DEVICE(USB_COMPRO_VENDOR_ID, USB_COMPRO_PRODUCT_ID)},
  920. {} /* Terminating entry */
  921. };
  922. MODULE_DEVICE_TABLE(usb, vicam_table);
  923. static struct usb_driver vicam_driver = {
  924. .name = "vicam",
  925. .probe = vicam_probe,
  926. .disconnect = vicam_disconnect,
  927. .id_table = vicam_table
  928. };
  929. /**
  930. * vicam_probe
  931. * @intf: the interface
  932. * @id: the device id
  933. *
  934. * Called by the usb core when a new device is connected that it thinks
  935. * this driver might be interested in.
  936. */
  937. static int
  938. vicam_probe( struct usb_interface *intf, const struct usb_device_id *id)
  939. {
  940. struct usb_device *dev = interface_to_usbdev(intf);
  941. int bulkEndpoint = 0;
  942. const struct usb_host_interface *interface;
  943. const struct usb_endpoint_descriptor *endpoint;
  944. struct vicam_camera *cam;
  945. printk(KERN_INFO "ViCam based webcam connected\n");
  946. interface = intf->cur_altsetting;
  947. DBG(KERN_DEBUG "Interface %d. has %u. endpoints!\n",
  948. interface->desc.bInterfaceNumber, (unsigned) (interface->desc.bNumEndpoints));
  949. endpoint = &interface->endpoint[0].desc;
  950. if ((endpoint->bEndpointAddress & 0x80) &&
  951. ((endpoint->bmAttributes & 3) == 0x02)) {
  952. /* we found a bulk in endpoint */
  953. bulkEndpoint = endpoint->bEndpointAddress;
  954. } else {
  955. printk(KERN_ERR
  956. "No bulk in endpoint was found ?! (this is bad)\n");
  957. }
  958. if ((cam =
  959. kzalloc(sizeof (struct vicam_camera), GFP_KERNEL)) == NULL) {
  960. printk(KERN_WARNING
  961. "could not allocate kernel memory for vicam_camera struct\n");
  962. return -ENOMEM;
  963. }
  964. cam->shutter_speed = 15;
  965. mutex_init(&cam->cam_lock);
  966. memcpy(&cam->vdev, &vicam_template,
  967. sizeof (vicam_template));
  968. cam->vdev.priv = cam; // sort of a reverse mapping for those functions that get vdev only
  969. cam->udev = dev;
  970. cam->bulkEndpoint = bulkEndpoint;
  971. if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1) == -1) {
  972. kfree(cam);
  973. printk(KERN_WARNING "video_register_device failed\n");
  974. return -EIO;
  975. }
  976. printk(KERN_INFO "ViCam webcam driver now controlling video device %d\n",cam->vdev.minor);
  977. usb_set_intfdata (intf, cam);
  978. return 0;
  979. }
  980. static void
  981. vicam_disconnect(struct usb_interface *intf)
  982. {
  983. int open_count;
  984. struct vicam_camera *cam = usb_get_intfdata (intf);
  985. usb_set_intfdata (intf, NULL);
  986. /* we must unregister the device before taking its
  987. * cam_lock. This is because the video open call
  988. * holds the same lock as video unregister. if we
  989. * unregister inside of the cam_lock and open also
  990. * uses the cam_lock, we get deadlock.
  991. */
  992. video_unregister_device(&cam->vdev);
  993. /* stop the camera from being used */
  994. mutex_lock(&cam->cam_lock);
  995. /* mark the camera as gone */
  996. cam->udev = NULL;
  997. /* the only thing left to do is synchronize with
  998. * our close/release function on who should release
  999. * the camera memory. if there are any users using the
  1000. * camera, it's their job. if there are no users,
  1001. * it's ours.
  1002. */
  1003. open_count = cam->open_count;
  1004. mutex_unlock(&cam->cam_lock);
  1005. if (!open_count) {
  1006. kfree(cam);
  1007. }
  1008. printk(KERN_DEBUG "ViCam-based WebCam disconnected\n");
  1009. }
  1010. /*
  1011. */
  1012. static int __init
  1013. usb_vicam_init(void)
  1014. {
  1015. int retval;
  1016. DBG(KERN_INFO "ViCam-based WebCam driver startup\n");
  1017. retval = usb_register(&vicam_driver);
  1018. if (retval)
  1019. printk(KERN_WARNING "usb_register failed!\n");
  1020. return retval;
  1021. }
  1022. static void __exit
  1023. usb_vicam_exit(void)
  1024. {
  1025. DBG(KERN_INFO
  1026. "ViCam-based WebCam driver shutdown\n");
  1027. usb_deregister(&vicam_driver);
  1028. }
  1029. module_init(usb_vicam_init);
  1030. module_exit(usb_vicam_exit);
  1031. MODULE_AUTHOR(DRIVER_AUTHOR);
  1032. MODULE_DESCRIPTION(DRIVER_DESC);
  1033. MODULE_LICENSE("GPL");