w9968cf.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620
  1. /***************************************************************************
  2. * Video4Linux driver for W996[87]CF JPEG USB Dual Mode Camera Chip. *
  3. * *
  4. * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it> *
  5. * *
  6. * - Memory management code from bttv driver by Ralph Metzler, *
  7. * Marcus Metzler and Gerd Knorr. *
  8. * - I2C interface to kernel, high-level image sensor control routines and *
  9. * some symbolic names from OV511 driver by Mark W. McClelland. *
  10. * - Low-level I2C fast write function by Piotr Czerczak. *
  11. * - Low-level I2C read function by Frederic Jouault. *
  12. * *
  13. * This program is free software; you can redistribute it and/or modify *
  14. * it under the terms of the GNU General Public License as published by *
  15. * the Free Software Foundation; either version 2 of the License, or *
  16. * (at your option) any later version. *
  17. * *
  18. * This program is distributed in the hope that it will be useful, *
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  21. * GNU General Public License for more details. *
  22. * *
  23. * You should have received a copy of the GNU General Public License *
  24. * along with this program; if not, write to the Free Software *
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
  26. ***************************************************************************/
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/kmod.h>
  30. #include <linux/init.h>
  31. #include <linux/fs.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/slab.h>
  34. #include <linux/mm.h>
  35. #include <linux/string.h>
  36. #include <linux/errno.h>
  37. #include <linux/sched.h>
  38. #include <linux/ioctl.h>
  39. #include <linux/delay.h>
  40. #include <linux/stddef.h>
  41. #include <asm/page.h>
  42. #include <asm/uaccess.h>
  43. #include <linux/page-flags.h>
  44. #include <linux/videodev.h>
  45. #include <media/v4l2-ioctl.h>
  46. #include "w9968cf.h"
  47. #include "w9968cf_decoder.h"
  48. static struct w9968cf_vpp_t* w9968cf_vpp;
  49. static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
  50. static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
  51. static DEFINE_MUTEX(w9968cf_devlist_mutex); /* semaphore for list traversal */
  52. static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
  53. /****************************************************************************
  54. * Module macros and parameters *
  55. ****************************************************************************/
  56. MODULE_DEVICE_TABLE(usb, winbond_id_table);
  57. MODULE_AUTHOR(W9968CF_MODULE_AUTHOR" "W9968CF_AUTHOR_EMAIL);
  58. MODULE_DESCRIPTION(W9968CF_MODULE_NAME);
  59. MODULE_VERSION(W9968CF_MODULE_VERSION);
  60. MODULE_LICENSE(W9968CF_MODULE_LICENSE);
  61. MODULE_SUPPORTED_DEVICE("Video");
  62. static unsigned short simcams = W9968CF_SIMCAMS;
  63. static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
  64. static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
  65. W9968CF_PACKET_SIZE};
  66. static unsigned short max_buffers[] = {[0 ... W9968CF_MAX_DEVICES-1] =
  67. W9968CF_BUFFERS};
  68. static int double_buffer[] = {[0 ... W9968CF_MAX_DEVICES-1] =
  69. W9968CF_DOUBLE_BUFFER};
  70. static int clamping[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLAMPING};
  71. static unsigned short filter_type[]= {[0 ... W9968CF_MAX_DEVICES-1] =
  72. W9968CF_FILTER_TYPE};
  73. static int largeview[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_LARGEVIEW};
  74. static unsigned short decompression[] = {[0 ... W9968CF_MAX_DEVICES-1] =
  75. W9968CF_DECOMPRESSION};
  76. static int upscaling[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_UPSCALING};
  77. static unsigned short force_palette[] = {[0 ... W9968CF_MAX_DEVICES-1] = 0};
  78. static int force_rgb[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_FORCE_RGB};
  79. static int autobright[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOBRIGHT};
  80. static int autoexp[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOEXP};
  81. static unsigned short lightfreq[] = {[0 ... W9968CF_MAX_DEVICES-1] =
  82. W9968CF_LIGHTFREQ};
  83. static int bandingfilter[] = {[0 ... W9968CF_MAX_DEVICES-1]=
  84. W9968CF_BANDINGFILTER};
  85. static short clockdiv[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLOCKDIV};
  86. static int backlight[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_BACKLIGHT};
  87. static int mirror[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_MIRROR};
  88. static int monochrome[] = {[0 ... W9968CF_MAX_DEVICES-1]=W9968CF_MONOCHROME};
  89. static unsigned int brightness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
  90. W9968CF_BRIGHTNESS};
  91. static unsigned int hue[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_HUE};
  92. static unsigned int colour[]={[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_COLOUR};
  93. static unsigned int contrast[] = {[0 ... W9968CF_MAX_DEVICES-1] =
  94. W9968CF_CONTRAST};
  95. static unsigned int whiteness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
  96. W9968CF_WHITENESS};
  97. #ifdef W9968CF_DEBUG
  98. static unsigned short debug = W9968CF_DEBUG_LEVEL;
  99. static int specific_debug = W9968CF_SPECIFIC_DEBUG;
  100. #endif
  101. static unsigned int param_nv[24]; /* number of values per parameter */
  102. module_param(simcams, ushort, 0644);
  103. module_param_array(video_nr, short, &param_nv[0], 0444);
  104. module_param_array(packet_size, uint, &param_nv[1], 0444);
  105. module_param_array(max_buffers, ushort, &param_nv[2], 0444);
  106. module_param_array(double_buffer, bool, &param_nv[3], 0444);
  107. module_param_array(clamping, bool, &param_nv[4], 0444);
  108. module_param_array(filter_type, ushort, &param_nv[5], 0444);
  109. module_param_array(largeview, bool, &param_nv[6], 0444);
  110. module_param_array(decompression, ushort, &param_nv[7], 0444);
  111. module_param_array(upscaling, bool, &param_nv[8], 0444);
  112. module_param_array(force_palette, ushort, &param_nv[9], 0444);
  113. module_param_array(force_rgb, ushort, &param_nv[10], 0444);
  114. module_param_array(autobright, bool, &param_nv[11], 0444);
  115. module_param_array(autoexp, bool, &param_nv[12], 0444);
  116. module_param_array(lightfreq, ushort, &param_nv[13], 0444);
  117. module_param_array(bandingfilter, bool, &param_nv[14], 0444);
  118. module_param_array(clockdiv, short, &param_nv[15], 0444);
  119. module_param_array(backlight, bool, &param_nv[16], 0444);
  120. module_param_array(mirror, bool, &param_nv[17], 0444);
  121. module_param_array(monochrome, bool, &param_nv[18], 0444);
  122. module_param_array(brightness, uint, &param_nv[19], 0444);
  123. module_param_array(hue, uint, &param_nv[20], 0444);
  124. module_param_array(colour, uint, &param_nv[21], 0444);
  125. module_param_array(contrast, uint, &param_nv[22], 0444);
  126. module_param_array(whiteness, uint, &param_nv[23], 0444);
  127. #ifdef W9968CF_DEBUG
  128. module_param(debug, ushort, 0644);
  129. module_param(specific_debug, bool, 0644);
  130. #endif
  131. MODULE_PARM_DESC(simcams,
  132. "\n<n> Number of cameras allowed to stream simultaneously."
  133. "\nn may vary from 0 to "
  134. __MODULE_STRING(W9968CF_MAX_DEVICES)"."
  135. "\nDefault value is "__MODULE_STRING(W9968CF_SIMCAMS)"."
  136. "\n");
  137. MODULE_PARM_DESC(video_nr,
  138. "\n<-1|n[,...]> Specify V4L minor mode number."
  139. "\n -1 = use next available (default)"
  140. "\n n = use minor number n (integer >= 0)"
  141. "\nYou can specify up to "__MODULE_STRING(W9968CF_MAX_DEVICES)
  142. " cameras this way."
  143. "\nFor example:"
  144. "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
  145. "\nthe second camera and use auto for the first"
  146. "\none and for every other camera."
  147. "\n");
  148. MODULE_PARM_DESC(packet_size,
  149. "\n<n[,...]> Specify the maximum data payload"
  150. "\nsize in bytes for alternate settings, for each device."
  151. "\nn is scaled between 63 and 1023 "
  152. "(default is "__MODULE_STRING(W9968CF_PACKET_SIZE)")."
  153. "\n");
  154. MODULE_PARM_DESC(max_buffers,
  155. "\n<n[,...]> For advanced users."
  156. "\nSpecify the maximum number of video frame buffers"
  157. "\nto allocate for each device, from 2 to "
  158. __MODULE_STRING(W9968CF_MAX_BUFFERS)
  159. ". (default is "__MODULE_STRING(W9968CF_BUFFERS)")."
  160. "\n");
  161. MODULE_PARM_DESC(double_buffer,
  162. "\n<0|1[,...]> "
  163. "Hardware double buffering: 0 disabled, 1 enabled."
  164. "\nIt should be enabled if you want smooth video output: if"
  165. "\nyou obtain out of sync. video, disable it, or try to"
  166. "\ndecrease the 'clockdiv' module parameter value."
  167. "\nDefault value is "__MODULE_STRING(W9968CF_DOUBLE_BUFFER)
  168. " for every device."
  169. "\n");
  170. MODULE_PARM_DESC(clamping,
  171. "\n<0|1[,...]> Video data clamping: 0 disabled, 1 enabled."
  172. "\nDefault value is "__MODULE_STRING(W9968CF_CLAMPING)
  173. " for every device."
  174. "\n");
  175. MODULE_PARM_DESC(filter_type,
  176. "\n<0|1|2[,...]> Video filter type."
  177. "\n0 none, 1 (1-2-1) 3-tap filter, "
  178. "2 (2-3-6-3-2) 5-tap filter."
  179. "\nDefault value is "__MODULE_STRING(W9968CF_FILTER_TYPE)
  180. " for every device."
  181. "\nThe filter is used to reduce noise and aliasing artifacts"
  182. "\nproduced by the CCD or CMOS image sensor, and the scaling"
  183. " process."
  184. "\n");
  185. MODULE_PARM_DESC(largeview,
  186. "\n<0|1[,...]> Large view: 0 disabled, 1 enabled."
  187. "\nDefault value is "__MODULE_STRING(W9968CF_LARGEVIEW)
  188. " for every device."
  189. "\n");
  190. MODULE_PARM_DESC(upscaling,
  191. "\n<0|1[,...]> Software scaling (for non-compressed video):"
  192. "\n0 disabled, 1 enabled."
  193. "\nDisable it if you have a slow CPU or you don't have"
  194. " enough memory."
  195. "\nDefault value is "__MODULE_STRING(W9968CF_UPSCALING)
  196. " for every device."
  197. "\nIf 'w9968cf-vpp' is not present, this parameter is"
  198. " set to 0."
  199. "\n");
  200. MODULE_PARM_DESC(decompression,
  201. "\n<0|1|2[,...]> Software video decompression:"
  202. "\n- 0 disables decompression (doesn't allow formats needing"
  203. " decompression)"
  204. "\n- 1 forces decompression (allows formats needing"
  205. " decompression only);"
  206. "\n- 2 allows any permitted formats."
  207. "\nFormats supporting compressed video are YUV422P and"
  208. " YUV420P/YUV420 "
  209. "\nin any resolutions where both width and height are "
  210. "a multiple of 16."
  211. "\nDefault value is "__MODULE_STRING(W9968CF_DECOMPRESSION)
  212. " for every device."
  213. "\nIf 'w9968cf-vpp' is not present, forcing decompression is "
  214. "\nnot allowed; in this case this parameter is set to 2."
  215. "\n");
  216. MODULE_PARM_DESC(force_palette,
  217. "\n<0"
  218. "|" __MODULE_STRING(VIDEO_PALETTE_UYVY)
  219. "|" __MODULE_STRING(VIDEO_PALETTE_YUV420)
  220. "|" __MODULE_STRING(VIDEO_PALETTE_YUV422P)
  221. "|" __MODULE_STRING(VIDEO_PALETTE_YUV420P)
  222. "|" __MODULE_STRING(VIDEO_PALETTE_YUYV)
  223. "|" __MODULE_STRING(VIDEO_PALETTE_YUV422)
  224. "|" __MODULE_STRING(VIDEO_PALETTE_GREY)
  225. "|" __MODULE_STRING(VIDEO_PALETTE_RGB555)
  226. "|" __MODULE_STRING(VIDEO_PALETTE_RGB565)
  227. "|" __MODULE_STRING(VIDEO_PALETTE_RGB24)
  228. "|" __MODULE_STRING(VIDEO_PALETTE_RGB32)
  229. "[,...]>"
  230. " Force picture palette."
  231. "\nIn order:"
  232. "\n- 0 allows any of the following formats:"
  233. "\n- UYVY 16 bpp - Original video, compression disabled"
  234. "\n- YUV420 12 bpp - Original video, compression enabled"
  235. "\n- YUV422P 16 bpp - Original video, compression enabled"
  236. "\n- YUV420P 12 bpp - Original video, compression enabled"
  237. "\n- YUVY 16 bpp - Software conversion from UYVY"
  238. "\n- YUV422 16 bpp - Software conversion from UYVY"
  239. "\n- GREY 8 bpp - Software conversion from UYVY"
  240. "\n- RGB555 16 bpp - Software conversion from UYVY"
  241. "\n- RGB565 16 bpp - Software conversion from UYVY"
  242. "\n- RGB24 24 bpp - Software conversion from UYVY"
  243. "\n- RGB32 32 bpp - Software conversion from UYVY"
  244. "\nWhen not 0, this parameter will override 'decompression'."
  245. "\nDefault value is 0 for every device."
  246. "\nInitial palette is "
  247. __MODULE_STRING(W9968CF_PALETTE_DECOMP_ON)"."
  248. "\nIf 'w9968cf-vpp' is not present, this parameter is"
  249. " set to 9 (UYVY)."
  250. "\n");
  251. MODULE_PARM_DESC(force_rgb,
  252. "\n<0|1[,...]> Read RGB video data instead of BGR:"
  253. "\n 1 = use RGB component ordering."
  254. "\n 0 = use BGR component ordering."
  255. "\nThis parameter has effect when using RGBX palettes only."
  256. "\nDefault value is "__MODULE_STRING(W9968CF_FORCE_RGB)
  257. " for every device."
  258. "\n");
  259. MODULE_PARM_DESC(autobright,
  260. "\n<0|1[,...]> Image sensor automatically changes brightness:"
  261. "\n 0 = no, 1 = yes"
  262. "\nDefault value is "__MODULE_STRING(W9968CF_AUTOBRIGHT)
  263. " for every device."
  264. "\n");
  265. MODULE_PARM_DESC(autoexp,
  266. "\n<0|1[,...]> Image sensor automatically changes exposure:"
  267. "\n 0 = no, 1 = yes"
  268. "\nDefault value is "__MODULE_STRING(W9968CF_AUTOEXP)
  269. " for every device."
  270. "\n");
  271. MODULE_PARM_DESC(lightfreq,
  272. "\n<50|60[,...]> Light frequency in Hz:"
  273. "\n 50 for European and Asian lighting,"
  274. " 60 for American lighting."
  275. "\nDefault value is "__MODULE_STRING(W9968CF_LIGHTFREQ)
  276. " for every device."
  277. "\n");
  278. MODULE_PARM_DESC(bandingfilter,
  279. "\n<0|1[,...]> Banding filter to reduce effects of"
  280. " fluorescent lighting:"
  281. "\n 0 disabled, 1 enabled."
  282. "\nThis filter tries to reduce the pattern of horizontal"
  283. "\nlight/dark bands caused by some (usually fluorescent)"
  284. " lighting."
  285. "\nDefault value is "__MODULE_STRING(W9968CF_BANDINGFILTER)
  286. " for every device."
  287. "\n");
  288. MODULE_PARM_DESC(clockdiv,
  289. "\n<-1|n[,...]> "
  290. "Force pixel clock divisor to a specific value (for experts):"
  291. "\n n may vary from 0 to 127."
  292. "\n -1 for automatic value."
  293. "\nSee also the 'double_buffer' module parameter."
  294. "\nDefault value is "__MODULE_STRING(W9968CF_CLOCKDIV)
  295. " for every device."
  296. "\n");
  297. MODULE_PARM_DESC(backlight,
  298. "\n<0|1[,...]> Objects are lit from behind:"
  299. "\n 0 = no, 1 = yes"
  300. "\nDefault value is "__MODULE_STRING(W9968CF_BACKLIGHT)
  301. " for every device."
  302. "\n");
  303. MODULE_PARM_DESC(mirror,
  304. "\n<0|1[,...]> Reverse image horizontally:"
  305. "\n 0 = no, 1 = yes"
  306. "\nDefault value is "__MODULE_STRING(W9968CF_MIRROR)
  307. " for every device."
  308. "\n");
  309. MODULE_PARM_DESC(monochrome,
  310. "\n<0|1[,...]> Use image sensor as monochrome sensor:"
  311. "\n 0 = no, 1 = yes"
  312. "\nNot all the sensors support monochrome color."
  313. "\nDefault value is "__MODULE_STRING(W9968CF_MONOCHROME)
  314. " for every device."
  315. "\n");
  316. MODULE_PARM_DESC(brightness,
  317. "\n<n[,...]> Set picture brightness (0-65535)."
  318. "\nDefault value is "__MODULE_STRING(W9968CF_BRIGHTNESS)
  319. " for every device."
  320. "\nThis parameter has no effect if 'autobright' is enabled."
  321. "\n");
  322. MODULE_PARM_DESC(hue,
  323. "\n<n[,...]> Set picture hue (0-65535)."
  324. "\nDefault value is "__MODULE_STRING(W9968CF_HUE)
  325. " for every device."
  326. "\n");
  327. MODULE_PARM_DESC(colour,
  328. "\n<n[,...]> Set picture saturation (0-65535)."
  329. "\nDefault value is "__MODULE_STRING(W9968CF_COLOUR)
  330. " for every device."
  331. "\n");
  332. MODULE_PARM_DESC(contrast,
  333. "\n<n[,...]> Set picture contrast (0-65535)."
  334. "\nDefault value is "__MODULE_STRING(W9968CF_CONTRAST)
  335. " for every device."
  336. "\n");
  337. MODULE_PARM_DESC(whiteness,
  338. "\n<n[,...]> Set picture whiteness (0-65535)."
  339. "\nDefault value is "__MODULE_STRING(W9968CF_WHITENESS)
  340. " for every device."
  341. "\n");
  342. #ifdef W9968CF_DEBUG
  343. MODULE_PARM_DESC(debug,
  344. "\n<n> Debugging information level, from 0 to 6:"
  345. "\n0 = none (use carefully)"
  346. "\n1 = critical errors"
  347. "\n2 = significant informations"
  348. "\n3 = configuration or general messages"
  349. "\n4 = warnings"
  350. "\n5 = called functions"
  351. "\n6 = function internals"
  352. "\nLevel 5 and 6 are useful for testing only, when only "
  353. "one device is used."
  354. "\nDefault value is "__MODULE_STRING(W9968CF_DEBUG_LEVEL)"."
  355. "\n");
  356. MODULE_PARM_DESC(specific_debug,
  357. "\n<0|1> Enable or disable specific debugging messages:"
  358. "\n0 = print messages concerning every level"
  359. " <= 'debug' level."
  360. "\n1 = print messages concerning the level"
  361. " indicated by 'debug'."
  362. "\nDefault value is "
  363. __MODULE_STRING(W9968CF_SPECIFIC_DEBUG)"."
  364. "\n");
  365. #endif /* W9968CF_DEBUG */
  366. /****************************************************************************
  367. * Some prototypes *
  368. ****************************************************************************/
  369. /* Video4linux interface */
  370. static const struct v4l2_file_operations w9968cf_fops;
  371. static int w9968cf_open(struct file *);
  372. static int w9968cf_release(struct file *);
  373. static int w9968cf_mmap(struct file *, struct vm_area_struct *);
  374. static long w9968cf_ioctl(struct file *, unsigned, unsigned long);
  375. static ssize_t w9968cf_read(struct file *, char __user *, size_t, loff_t *);
  376. static long w9968cf_v4l_ioctl(struct file *, unsigned int,
  377. void __user *);
  378. /* USB-specific */
  379. static int w9968cf_start_transfer(struct w9968cf_device*);
  380. static int w9968cf_stop_transfer(struct w9968cf_device*);
  381. static int w9968cf_write_reg(struct w9968cf_device*, u16 value, u16 index);
  382. static int w9968cf_read_reg(struct w9968cf_device*, u16 index);
  383. static int w9968cf_write_fsb(struct w9968cf_device*, u16* data);
  384. static int w9968cf_write_sb(struct w9968cf_device*, u16 value);
  385. static int w9968cf_read_sb(struct w9968cf_device*);
  386. static int w9968cf_upload_quantizationtables(struct w9968cf_device*);
  387. static void w9968cf_urb_complete(struct urb *urb);
  388. /* Low-level I2C (SMBus) I/O */
  389. static int w9968cf_smbus_start(struct w9968cf_device*);
  390. static int w9968cf_smbus_stop(struct w9968cf_device*);
  391. static int w9968cf_smbus_write_byte(struct w9968cf_device*, u8 v);
  392. static int w9968cf_smbus_read_byte(struct w9968cf_device*, u8* v);
  393. static int w9968cf_smbus_write_ack(struct w9968cf_device*);
  394. static int w9968cf_smbus_read_ack(struct w9968cf_device*);
  395. static int w9968cf_smbus_refresh_bus(struct w9968cf_device*);
  396. static int w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
  397. u16 address, u8* value);
  398. static int w9968cf_i2c_adap_read_byte_data(struct w9968cf_device*, u16 address,
  399. u8 subaddress, u8* value);
  400. static int w9968cf_i2c_adap_write_byte(struct w9968cf_device*,
  401. u16 address, u8 subaddress);
  402. static int w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device*,
  403. u16 address, u8 subaddress,
  404. u8 value);
  405. /* I2C interface to kernel */
  406. static int w9968cf_i2c_init(struct w9968cf_device*);
  407. static int w9968cf_i2c_smbus_xfer(struct i2c_adapter*, u16 addr,
  408. unsigned short flags, char read_write,
  409. u8 command, int size, union i2c_smbus_data*);
  410. static u32 w9968cf_i2c_func(struct i2c_adapter*);
  411. /* Memory management */
  412. static void* rvmalloc(unsigned long size);
  413. static void rvfree(void *mem, unsigned long size);
  414. static void w9968cf_deallocate_memory(struct w9968cf_device*);
  415. static int w9968cf_allocate_memory(struct w9968cf_device*);
  416. /* High-level image sensor control functions */
  417. static int w9968cf_sensor_set_control(struct w9968cf_device*,int cid,int val);
  418. static int w9968cf_sensor_get_control(struct w9968cf_device*,int cid,int *val);
  419. static int w9968cf_sensor_cmd(struct w9968cf_device*,
  420. unsigned int cmd, void *arg);
  421. static int w9968cf_sensor_init(struct w9968cf_device*);
  422. static int w9968cf_sensor_update_settings(struct w9968cf_device*);
  423. static int w9968cf_sensor_get_picture(struct w9968cf_device*);
  424. static int w9968cf_sensor_update_picture(struct w9968cf_device*,
  425. struct video_picture pict);
  426. /* Other helper functions */
  427. static void w9968cf_configure_camera(struct w9968cf_device*,struct usb_device*,
  428. enum w9968cf_model_id,
  429. const unsigned short dev_nr);
  430. static void w9968cf_adjust_configuration(struct w9968cf_device*);
  431. static int w9968cf_turn_on_led(struct w9968cf_device*);
  432. static int w9968cf_init_chip(struct w9968cf_device*);
  433. static inline u16 w9968cf_valid_palette(u16 palette);
  434. static inline u16 w9968cf_valid_depth(u16 palette);
  435. static inline u8 w9968cf_need_decompression(u16 palette);
  436. static int w9968cf_set_picture(struct w9968cf_device*, struct video_picture);
  437. static int w9968cf_set_window(struct w9968cf_device*, struct video_window);
  438. static int w9968cf_postprocess_frame(struct w9968cf_device*,
  439. struct w9968cf_frame_t*);
  440. static int w9968cf_adjust_window_size(struct w9968cf_device*, u32 *w, u32 *h);
  441. static void w9968cf_init_framelist(struct w9968cf_device*);
  442. static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
  443. static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
  444. static void w9968cf_release_resources(struct w9968cf_device*);
  445. /****************************************************************************
  446. * Symbolic names *
  447. ****************************************************************************/
  448. /* Used to represent a list of values and their respective symbolic names */
  449. struct w9968cf_symbolic_list {
  450. const int num;
  451. const char *name;
  452. };
  453. /*--------------------------------------------------------------------------
  454. Returns the name of the matching element in the symbolic_list array. The
  455. end of the list must be marked with an element that has a NULL name.
  456. --------------------------------------------------------------------------*/
  457. static inline const char *
  458. symbolic(struct w9968cf_symbolic_list list[], const int num)
  459. {
  460. int i;
  461. for (i = 0; list[i].name != NULL; i++)
  462. if (list[i].num == num)
  463. return (list[i].name);
  464. return "Unknown";
  465. }
  466. static struct w9968cf_symbolic_list camlist[] = {
  467. { W9968CF_MOD_GENERIC, "W996[87]CF JPEG USB Dual Mode Camera" },
  468. { W9968CF_MOD_CLVBWGP, "Creative Labs Video Blaster WebCam Go Plus" },
  469. /* Other cameras (having the same descriptors as Generic W996[87]CF) */
  470. { W9968CF_MOD_ADPVDMA, "Aroma Digi Pen VGA Dual Mode ADG-5000" },
  471. { W9986CF_MOD_AAU, "AVerMedia AVerTV USB" },
  472. { W9968CF_MOD_CLVBWG, "Creative Labs Video Blaster WebCam Go" },
  473. { W9968CF_MOD_LL, "Lebon LDC-035A" },
  474. { W9968CF_MOD_EEEMC, "Ezonics EZ-802 EZMega Cam" },
  475. { W9968CF_MOD_OOE, "OmniVision OV8610-EDE" },
  476. { W9968CF_MOD_ODPVDMPC, "OPCOM Digi Pen VGA Dual Mode Pen Camera" },
  477. { W9968CF_MOD_PDPII, "Pretec Digi Pen-II" },
  478. { W9968CF_MOD_PDP480, "Pretec DigiPen-480" },
  479. { -1, NULL }
  480. };
  481. static struct w9968cf_symbolic_list senlist[] = {
  482. { CC_OV76BE, "OV76BE" },
  483. { CC_OV7610, "OV7610" },
  484. { CC_OV7620, "OV7620" },
  485. { CC_OV7620AE, "OV7620AE" },
  486. { CC_OV6620, "OV6620" },
  487. { CC_OV6630, "OV6630" },
  488. { CC_OV6630AE, "OV6630AE" },
  489. { CC_OV6630AF, "OV6630AF" },
  490. { -1, NULL }
  491. };
  492. /* Video4Linux1 palettes */
  493. static struct w9968cf_symbolic_list v4l1_plist[] = {
  494. { VIDEO_PALETTE_GREY, "GREY" },
  495. { VIDEO_PALETTE_HI240, "HI240" },
  496. { VIDEO_PALETTE_RGB565, "RGB565" },
  497. { VIDEO_PALETTE_RGB24, "RGB24" },
  498. { VIDEO_PALETTE_RGB32, "RGB32" },
  499. { VIDEO_PALETTE_RGB555, "RGB555" },
  500. { VIDEO_PALETTE_YUV422, "YUV422" },
  501. { VIDEO_PALETTE_YUYV, "YUYV" },
  502. { VIDEO_PALETTE_UYVY, "UYVY" },
  503. { VIDEO_PALETTE_YUV420, "YUV420" },
  504. { VIDEO_PALETTE_YUV411, "YUV411" },
  505. { VIDEO_PALETTE_RAW, "RAW" },
  506. { VIDEO_PALETTE_YUV422P, "YUV422P" },
  507. { VIDEO_PALETTE_YUV411P, "YUV411P" },
  508. { VIDEO_PALETTE_YUV420P, "YUV420P" },
  509. { VIDEO_PALETTE_YUV410P, "YUV410P" },
  510. { -1, NULL }
  511. };
  512. /* Decoder error codes: */
  513. static struct w9968cf_symbolic_list decoder_errlist[] = {
  514. { W9968CF_DEC_ERR_CORRUPTED_DATA, "Corrupted data" },
  515. { W9968CF_DEC_ERR_BUF_OVERFLOW, "Buffer overflow" },
  516. { W9968CF_DEC_ERR_NO_SOI, "SOI marker not found" },
  517. { W9968CF_DEC_ERR_NO_SOF0, "SOF0 marker not found" },
  518. { W9968CF_DEC_ERR_NO_SOS, "SOS marker not found" },
  519. { W9968CF_DEC_ERR_NO_EOI, "EOI marker not found" },
  520. { -1, NULL }
  521. };
  522. /* URB error codes: */
  523. static struct w9968cf_symbolic_list urb_errlist[] = {
  524. { -ENOMEM, "No memory for allocation of internal structures" },
  525. { -ENOSPC, "The host controller's bandwidth is already consumed" },
  526. { -ENOENT, "URB was canceled by unlink_urb" },
  527. { -EXDEV, "ISO transfer only partially completed" },
  528. { -EAGAIN, "Too match scheduled for the future" },
  529. { -ENXIO, "URB already queued" },
  530. { -EFBIG, "Too much ISO frames requested" },
  531. { -ENOSR, "Buffer error (overrun)" },
  532. { -EPIPE, "Specified endpoint is stalled (device not responding)"},
  533. { -EOVERFLOW, "Babble (too much data)" },
  534. { -EPROTO, "Bit-stuff error (bad cable?)" },
  535. { -EILSEQ, "CRC/Timeout" },
  536. { -ETIME, "Device does not respond to token" },
  537. { -ETIMEDOUT, "Device does not respond to command" },
  538. { -1, NULL }
  539. };
  540. /****************************************************************************
  541. * Memory management functions *
  542. ****************************************************************************/
  543. static void* rvmalloc(unsigned long size)
  544. {
  545. void* mem;
  546. unsigned long adr;
  547. size = PAGE_ALIGN(size);
  548. mem = vmalloc_32(size);
  549. if (!mem)
  550. return NULL;
  551. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  552. adr = (unsigned long) mem;
  553. while (size > 0) {
  554. SetPageReserved(vmalloc_to_page((void *)adr));
  555. adr += PAGE_SIZE;
  556. size -= PAGE_SIZE;
  557. }
  558. return mem;
  559. }
  560. static void rvfree(void* mem, unsigned long size)
  561. {
  562. unsigned long adr;
  563. if (!mem)
  564. return;
  565. adr = (unsigned long) mem;
  566. while ((long) size > 0) {
  567. ClearPageReserved(vmalloc_to_page((void *)adr));
  568. adr += PAGE_SIZE;
  569. size -= PAGE_SIZE;
  570. }
  571. vfree(mem);
  572. }
  573. /*--------------------------------------------------------------------------
  574. Deallocate previously allocated memory.
  575. --------------------------------------------------------------------------*/
  576. static void w9968cf_deallocate_memory(struct w9968cf_device* cam)
  577. {
  578. u8 i;
  579. /* Free the isochronous transfer buffers */
  580. for (i = 0; i < W9968CF_URBS; i++) {
  581. kfree(cam->transfer_buffer[i]);
  582. cam->transfer_buffer[i] = NULL;
  583. }
  584. /* Free temporary frame buffer */
  585. if (cam->frame_tmp.buffer) {
  586. rvfree(cam->frame_tmp.buffer, cam->frame_tmp.size);
  587. cam->frame_tmp.buffer = NULL;
  588. }
  589. /* Free helper buffer */
  590. if (cam->frame_vpp.buffer) {
  591. rvfree(cam->frame_vpp.buffer, cam->frame_vpp.size);
  592. cam->frame_vpp.buffer = NULL;
  593. }
  594. /* Free video frame buffers */
  595. if (cam->frame[0].buffer) {
  596. rvfree(cam->frame[0].buffer, cam->nbuffers*cam->frame[0].size);
  597. cam->frame[0].buffer = NULL;
  598. }
  599. cam->nbuffers = 0;
  600. DBG(5, "Memory successfully deallocated")
  601. }
  602. /*--------------------------------------------------------------------------
  603. Allocate memory buffers for USB transfers and video frames.
  604. This function is called by open() only.
  605. Return 0 on success, a negative number otherwise.
  606. --------------------------------------------------------------------------*/
  607. static int w9968cf_allocate_memory(struct w9968cf_device* cam)
  608. {
  609. const u16 p_size = wMaxPacketSize[cam->altsetting-1];
  610. void* buff = NULL;
  611. unsigned long hw_bufsize, vpp_bufsize;
  612. u8 i, bpp;
  613. /* NOTE: Deallocation is done elsewhere in case of error */
  614. /* Calculate the max amount of raw data per frame from the device */
  615. hw_bufsize = cam->maxwidth*cam->maxheight*2;
  616. /* Calculate the max buf. size needed for post-processing routines */
  617. bpp = (w9968cf_vpp) ? 4 : 2;
  618. if (cam->upscaling)
  619. vpp_bufsize = max(W9968CF_MAX_WIDTH*W9968CF_MAX_HEIGHT*bpp,
  620. cam->maxwidth*cam->maxheight*bpp);
  621. else
  622. vpp_bufsize = cam->maxwidth*cam->maxheight*bpp;
  623. /* Allocate memory for the isochronous transfer buffers */
  624. for (i = 0; i < W9968CF_URBS; i++) {
  625. if (!(cam->transfer_buffer[i] =
  626. kzalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
  627. DBG(1, "Couldn't allocate memory for the isochronous "
  628. "transfer buffers (%u bytes)",
  629. p_size * W9968CF_ISO_PACKETS)
  630. return -ENOMEM;
  631. }
  632. }
  633. /* Allocate memory for the temporary frame buffer */
  634. if (!(cam->frame_tmp.buffer = rvmalloc(hw_bufsize))) {
  635. DBG(1, "Couldn't allocate memory for the temporary "
  636. "video frame buffer (%lu bytes)", hw_bufsize)
  637. return -ENOMEM;
  638. }
  639. cam->frame_tmp.size = hw_bufsize;
  640. cam->frame_tmp.number = -1;
  641. /* Allocate memory for the helper buffer */
  642. if (w9968cf_vpp) {
  643. if (!(cam->frame_vpp.buffer = rvmalloc(vpp_bufsize))) {
  644. DBG(1, "Couldn't allocate memory for the helper buffer"
  645. " (%lu bytes)", vpp_bufsize)
  646. return -ENOMEM;
  647. }
  648. cam->frame_vpp.size = vpp_bufsize;
  649. } else
  650. cam->frame_vpp.buffer = NULL;
  651. /* Allocate memory for video frame buffers */
  652. cam->nbuffers = cam->max_buffers;
  653. while (cam->nbuffers >= 2) {
  654. if ((buff = rvmalloc(cam->nbuffers * vpp_bufsize)))
  655. break;
  656. else
  657. cam->nbuffers--;
  658. }
  659. if (!buff) {
  660. DBG(1, "Couldn't allocate memory for the video frame buffers")
  661. cam->nbuffers = 0;
  662. return -ENOMEM;
  663. }
  664. if (cam->nbuffers != cam->max_buffers)
  665. DBG(2, "Couldn't allocate memory for %u video frame buffers. "
  666. "Only memory for %u buffers has been allocated",
  667. cam->max_buffers, cam->nbuffers)
  668. for (i = 0; i < cam->nbuffers; i++) {
  669. cam->frame[i].buffer = buff + i*vpp_bufsize;
  670. cam->frame[i].size = vpp_bufsize;
  671. cam->frame[i].number = i;
  672. /* Circular list */
  673. if (i != cam->nbuffers-1)
  674. cam->frame[i].next = &cam->frame[i+1];
  675. else
  676. cam->frame[i].next = &cam->frame[0];
  677. cam->frame[i].status = F_UNUSED;
  678. }
  679. DBG(5, "Memory successfully allocated")
  680. return 0;
  681. }
  682. /****************************************************************************
  683. * USB-specific functions *
  684. ****************************************************************************/
  685. /*--------------------------------------------------------------------------
  686. This is an handler function which is called after the URBs are completed.
  687. It collects multiple data packets coming from the camera by putting them
  688. into frame buffers: one or more zero data length data packets are used to
  689. mark the end of a video frame; the first non-zero data packet is the start
  690. of the next video frame; if an error is encountered in a packet, the entire
  691. video frame is discarded and grabbed again.
  692. If there are no requested frames in the FIFO list, packets are collected into
  693. a temporary buffer.
  694. --------------------------------------------------------------------------*/
  695. static void w9968cf_urb_complete(struct urb *urb)
  696. {
  697. struct w9968cf_device* cam = (struct w9968cf_device*)urb->context;
  698. struct w9968cf_frame_t** f;
  699. unsigned int len, status;
  700. void* pos;
  701. u8 i;
  702. int err = 0;
  703. if ((!cam->streaming) || cam->disconnected) {
  704. DBG(4, "Got interrupt, but not streaming")
  705. return;
  706. }
  707. /* "(*f)" will be used instead of "cam->frame_current" */
  708. f = &cam->frame_current;
  709. /* If a frame has been requested and we are grabbing into
  710. the temporary frame, we'll switch to that requested frame */
  711. if ((*f) == &cam->frame_tmp && *cam->requested_frame) {
  712. if (cam->frame_tmp.status == F_GRABBING) {
  713. w9968cf_pop_frame(cam, &cam->frame_current);
  714. (*f)->status = F_GRABBING;
  715. (*f)->length = cam->frame_tmp.length;
  716. memcpy((*f)->buffer, cam->frame_tmp.buffer,
  717. (*f)->length);
  718. DBG(6, "Switched from temp. frame to frame #%d",
  719. (*f)->number)
  720. }
  721. }
  722. for (i = 0; i < urb->number_of_packets; i++) {
  723. len = urb->iso_frame_desc[i].actual_length;
  724. status = urb->iso_frame_desc[i].status;
  725. pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
  726. if (status && len != 0) {
  727. DBG(4, "URB failed, error in data packet "
  728. "(error #%u, %s)",
  729. status, symbolic(urb_errlist, status))
  730. (*f)->status = F_ERROR;
  731. continue;
  732. }
  733. if (len) { /* start of frame */
  734. if ((*f)->status == F_UNUSED) {
  735. (*f)->status = F_GRABBING;
  736. (*f)->length = 0;
  737. }
  738. /* Buffer overflows shouldn't happen, however...*/
  739. if ((*f)->length + len > (*f)->size) {
  740. DBG(4, "Buffer overflow: bad data packets")
  741. (*f)->status = F_ERROR;
  742. }
  743. if ((*f)->status == F_GRABBING) {
  744. memcpy((*f)->buffer + (*f)->length, pos, len);
  745. (*f)->length += len;
  746. }
  747. } else if ((*f)->status == F_GRABBING) { /* end of frame */
  748. DBG(6, "Frame #%d successfully grabbed", (*f)->number)
  749. if (cam->vpp_flag & VPP_DECOMPRESSION) {
  750. err = w9968cf_vpp->check_headers((*f)->buffer,
  751. (*f)->length);
  752. if (err) {
  753. DBG(4, "Skip corrupted frame: %s",
  754. symbolic(decoder_errlist, err))
  755. (*f)->status = F_UNUSED;
  756. continue; /* grab this frame again */
  757. }
  758. }
  759. (*f)->status = F_READY;
  760. (*f)->queued = 0;
  761. /* Take a pointer to the new frame from the FIFO list.
  762. If the list is empty,we'll use the temporary frame*/
  763. if (*cam->requested_frame)
  764. w9968cf_pop_frame(cam, &cam->frame_current);
  765. else {
  766. cam->frame_current = &cam->frame_tmp;
  767. (*f)->status = F_UNUSED;
  768. }
  769. } else if ((*f)->status == F_ERROR)
  770. (*f)->status = F_UNUSED; /* grab it again */
  771. PDBGG("Frame length %lu | pack.#%u | pack.len. %u | state %d",
  772. (unsigned long)(*f)->length, i, len, (*f)->status)
  773. } /* end for */
  774. /* Resubmit this URB */
  775. urb->dev = cam->usbdev;
  776. urb->status = 0;
  777. spin_lock(&cam->urb_lock);
  778. if (cam->streaming)
  779. if ((err = usb_submit_urb(urb, GFP_ATOMIC))) {
  780. cam->misconfigured = 1;
  781. DBG(1, "Couldn't resubmit the URB: error %d, %s",
  782. err, symbolic(urb_errlist, err))
  783. }
  784. spin_unlock(&cam->urb_lock);
  785. /* Wake up the user process */
  786. wake_up_interruptible(&cam->wait_queue);
  787. }
  788. /*---------------------------------------------------------------------------
  789. Setup the URB structures for the isochronous transfer.
  790. Submit the URBs so that the data transfer begins.
  791. Return 0 on success, a negative number otherwise.
  792. ---------------------------------------------------------------------------*/
  793. static int w9968cf_start_transfer(struct w9968cf_device* cam)
  794. {
  795. struct usb_device *udev = cam->usbdev;
  796. struct urb* urb;
  797. const u16 p_size = wMaxPacketSize[cam->altsetting-1];
  798. u16 w, h, d;
  799. int vidcapt;
  800. u32 t_size;
  801. int err = 0;
  802. s8 i, j;
  803. for (i = 0; i < W9968CF_URBS; i++) {
  804. urb = usb_alloc_urb(W9968CF_ISO_PACKETS, GFP_KERNEL);
  805. if (!urb) {
  806. for (j = 0; j < i; j++)
  807. usb_free_urb(cam->urb[j]);
  808. DBG(1, "Couldn't allocate the URB structures")
  809. return -ENOMEM;
  810. }
  811. cam->urb[i] = urb;
  812. urb->dev = udev;
  813. urb->context = (void*)cam;
  814. urb->pipe = usb_rcvisocpipe(udev, 1);
  815. urb->transfer_flags = URB_ISO_ASAP;
  816. urb->number_of_packets = W9968CF_ISO_PACKETS;
  817. urb->complete = w9968cf_urb_complete;
  818. urb->transfer_buffer = cam->transfer_buffer[i];
  819. urb->transfer_buffer_length = p_size*W9968CF_ISO_PACKETS;
  820. urb->interval = 1;
  821. for (j = 0; j < W9968CF_ISO_PACKETS; j++) {
  822. urb->iso_frame_desc[j].offset = p_size*j;
  823. urb->iso_frame_desc[j].length = p_size;
  824. }
  825. }
  826. /* Transfer size per frame, in WORD ! */
  827. d = cam->hw_depth;
  828. w = cam->hw_width;
  829. h = cam->hw_height;
  830. t_size = (w*h*d)/16;
  831. err = w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
  832. err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
  833. /* Transfer size */
  834. err += w9968cf_write_reg(cam, t_size & 0xffff, 0x3d); /* low bits */
  835. err += w9968cf_write_reg(cam, t_size >> 16, 0x3e); /* high bits */
  836. if (cam->vpp_flag & VPP_DECOMPRESSION)
  837. err += w9968cf_upload_quantizationtables(cam);
  838. vidcapt = w9968cf_read_reg(cam, 0x16); /* read picture settings */
  839. err += w9968cf_write_reg(cam, vidcapt|0x8000, 0x16); /* capt. enable */
  840. err += usb_set_interface(udev, 0, cam->altsetting);
  841. err += w9968cf_write_reg(cam, 0x8a05, 0x3c); /* USB FIFO enable */
  842. if (err || (vidcapt < 0)) {
  843. for (i = 0; i < W9968CF_URBS; i++)
  844. usb_free_urb(cam->urb[i]);
  845. DBG(1, "Couldn't tell the camera to start the data transfer")
  846. return err;
  847. }
  848. w9968cf_init_framelist(cam);
  849. /* Begin to grab into the temporary buffer */
  850. cam->frame_tmp.status = F_UNUSED;
  851. cam->frame_tmp.queued = 0;
  852. cam->frame_current = &cam->frame_tmp;
  853. if (!(cam->vpp_flag & VPP_DECOMPRESSION))
  854. DBG(5, "Isochronous transfer size: %lu bytes/frame",
  855. (unsigned long)t_size*2)
  856. DBG(5, "Starting the isochronous transfer...")
  857. cam->streaming = 1;
  858. /* Submit the URBs */
  859. for (i = 0; i < W9968CF_URBS; i++) {
  860. err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
  861. if (err) {
  862. cam->streaming = 0;
  863. for (j = i-1; j >= 0; j--) {
  864. usb_kill_urb(cam->urb[j]);
  865. usb_free_urb(cam->urb[j]);
  866. }
  867. DBG(1, "Couldn't send a transfer request to the "
  868. "USB core (error #%d, %s)", err,
  869. symbolic(urb_errlist, err))
  870. return err;
  871. }
  872. }
  873. return 0;
  874. }
  875. /*--------------------------------------------------------------------------
  876. Stop the isochronous transfer and set alternate setting to 0 (0Mb/s).
  877. Return 0 on success, a negative number otherwise.
  878. --------------------------------------------------------------------------*/
  879. static int w9968cf_stop_transfer(struct w9968cf_device* cam)
  880. {
  881. struct usb_device *udev = cam->usbdev;
  882. unsigned long lock_flags;
  883. int err = 0;
  884. s8 i;
  885. if (!cam->streaming)
  886. return 0;
  887. /* This avoids race conditions with usb_submit_urb()
  888. in the URB completition handler */
  889. spin_lock_irqsave(&cam->urb_lock, lock_flags);
  890. cam->streaming = 0;
  891. spin_unlock_irqrestore(&cam->urb_lock, lock_flags);
  892. for (i = W9968CF_URBS-1; i >= 0; i--)
  893. if (cam->urb[i]) {
  894. usb_kill_urb(cam->urb[i]);
  895. usb_free_urb(cam->urb[i]);
  896. cam->urb[i] = NULL;
  897. }
  898. if (cam->disconnected)
  899. goto exit;
  900. err = w9968cf_write_reg(cam, 0x0a05, 0x3c); /* stop USB transfer */
  901. err += usb_set_interface(udev, 0, 0); /* 0 Mb/s */
  902. err += w9968cf_write_reg(cam, 0x0000, 0x39); /* disable JPEG encoder */
  903. err += w9968cf_write_reg(cam, 0x0000, 0x16); /* stop video capture */
  904. if (err) {
  905. DBG(2, "Failed to tell the camera to stop the isochronous "
  906. "transfer. However this is not a critical error.")
  907. return -EIO;
  908. }
  909. exit:
  910. DBG(5, "Isochronous transfer stopped")
  911. return 0;
  912. }
  913. /*--------------------------------------------------------------------------
  914. Write a W9968CF register.
  915. Return 0 on success, -1 otherwise.
  916. --------------------------------------------------------------------------*/
  917. static int w9968cf_write_reg(struct w9968cf_device* cam, u16 value, u16 index)
  918. {
  919. struct usb_device* udev = cam->usbdev;
  920. int res;
  921. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
  922. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  923. value, index, NULL, 0, W9968CF_USB_CTRL_TIMEOUT);
  924. if (res < 0)
  925. DBG(4, "Failed to write a register "
  926. "(value 0x%04X, index 0x%02X, error #%d, %s)",
  927. value, index, res, symbolic(urb_errlist, res))
  928. return (res >= 0) ? 0 : -1;
  929. }
  930. /*--------------------------------------------------------------------------
  931. Read a W9968CF register.
  932. Return the register value on success, -1 otherwise.
  933. --------------------------------------------------------------------------*/
  934. static int w9968cf_read_reg(struct w9968cf_device* cam, u16 index)
  935. {
  936. struct usb_device* udev = cam->usbdev;
  937. u16* buff = cam->control_buffer;
  938. int res;
  939. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 1,
  940. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  941. 0, index, buff, 2, W9968CF_USB_CTRL_TIMEOUT);
  942. if (res < 0)
  943. DBG(4, "Failed to read a register "
  944. "(index 0x%02X, error #%d, %s)",
  945. index, res, symbolic(urb_errlist, res))
  946. return (res >= 0) ? (int)(*buff) : -1;
  947. }
  948. /*--------------------------------------------------------------------------
  949. Write 64-bit data to the fast serial bus registers.
  950. Return 0 on success, -1 otherwise.
  951. --------------------------------------------------------------------------*/
  952. static int w9968cf_write_fsb(struct w9968cf_device* cam, u16* data)
  953. {
  954. struct usb_device* udev = cam->usbdev;
  955. u16 value;
  956. int res;
  957. value = *data++;
  958. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
  959. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  960. value, 0x06, data, 6, W9968CF_USB_CTRL_TIMEOUT);
  961. if (res < 0)
  962. DBG(4, "Failed to write the FSB registers "
  963. "(error #%d, %s)", res, symbolic(urb_errlist, res))
  964. return (res >= 0) ? 0 : -1;
  965. }
  966. /*--------------------------------------------------------------------------
  967. Write data to the serial bus control register.
  968. Return 0 on success, a negative number otherwise.
  969. --------------------------------------------------------------------------*/
  970. static int w9968cf_write_sb(struct w9968cf_device* cam, u16 value)
  971. {
  972. int err = 0;
  973. err = w9968cf_write_reg(cam, value, 0x01);
  974. udelay(W9968CF_I2C_BUS_DELAY);
  975. return err;
  976. }
  977. /*--------------------------------------------------------------------------
  978. Read data from the serial bus control register.
  979. Return 0 on success, a negative number otherwise.
  980. --------------------------------------------------------------------------*/
  981. static int w9968cf_read_sb(struct w9968cf_device* cam)
  982. {
  983. int v = 0;
  984. v = w9968cf_read_reg(cam, 0x01);
  985. udelay(W9968CF_I2C_BUS_DELAY);
  986. return v;
  987. }
  988. /*--------------------------------------------------------------------------
  989. Upload quantization tables for the JPEG compression.
  990. This function is called by w9968cf_start_transfer().
  991. Return 0 on success, a negative number otherwise.
  992. --------------------------------------------------------------------------*/
  993. static int w9968cf_upload_quantizationtables(struct w9968cf_device* cam)
  994. {
  995. u16 a, b;
  996. int err = 0, i, j;
  997. err += w9968cf_write_reg(cam, 0x0010, 0x39); /* JPEG clock enable */
  998. for (i = 0, j = 0; i < 32; i++, j += 2) {
  999. a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j+1]) << 8);
  1000. b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j+1]) << 8);
  1001. err += w9968cf_write_reg(cam, a, 0x40+i);
  1002. err += w9968cf_write_reg(cam, b, 0x60+i);
  1003. }
  1004. err += w9968cf_write_reg(cam, 0x0012, 0x39); /* JPEG encoder enable */
  1005. return err;
  1006. }
  1007. /****************************************************************************
  1008. * Low-level I2C I/O functions. *
  1009. * The adapter supports the following I2C transfer functions: *
  1010. * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
  1011. * i2c_adap_read_byte_data() *
  1012. * i2c_adap_read_byte() *
  1013. ****************************************************************************/
  1014. static int w9968cf_smbus_start(struct w9968cf_device* cam)
  1015. {
  1016. int err = 0;
  1017. err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
  1018. err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
  1019. return err;
  1020. }
  1021. static int w9968cf_smbus_stop(struct w9968cf_device* cam)
  1022. {
  1023. int err = 0;
  1024. err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
  1025. err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
  1026. return err;
  1027. }
  1028. static int w9968cf_smbus_write_byte(struct w9968cf_device* cam, u8 v)
  1029. {
  1030. u8 bit;
  1031. int err = 0, sda;
  1032. for (bit = 0 ; bit < 8 ; bit++) {
  1033. sda = (v & 0x80) ? 2 : 0;
  1034. v <<= 1;
  1035. /* SDE=1, SDA=sda, SCL=0 */
  1036. err += w9968cf_write_sb(cam, 0x10 | sda);
  1037. /* SDE=1, SDA=sda, SCL=1 */
  1038. err += w9968cf_write_sb(cam, 0x11 | sda);
  1039. /* SDE=1, SDA=sda, SCL=0 */
  1040. err += w9968cf_write_sb(cam, 0x10 | sda);
  1041. }
  1042. return err;
  1043. }
  1044. static int w9968cf_smbus_read_byte(struct w9968cf_device* cam, u8* v)
  1045. {
  1046. u8 bit;
  1047. int err = 0;
  1048. *v = 0;
  1049. for (bit = 0 ; bit < 8 ; bit++) {
  1050. *v <<= 1;
  1051. err += w9968cf_write_sb(cam, 0x0013);
  1052. *v |= (w9968cf_read_sb(cam) & 0x0008) ? 1 : 0;
  1053. err += w9968cf_write_sb(cam, 0x0012);
  1054. }
  1055. return err;
  1056. }
  1057. static int w9968cf_smbus_write_ack(struct w9968cf_device* cam)
  1058. {
  1059. int err = 0;
  1060. err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
  1061. err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
  1062. err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
  1063. return err;
  1064. }
  1065. static int w9968cf_smbus_read_ack(struct w9968cf_device* cam)
  1066. {
  1067. int err = 0, sda;
  1068. err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
  1069. sda = (w9968cf_read_sb(cam) & 0x08) ? 1 : 0; /* sda = SDA */
  1070. err += w9968cf_write_sb(cam, 0x0012); /* SDE=1, SDA=1, SCL=0 */
  1071. if (sda < 0)
  1072. err += sda;
  1073. if (sda == 1) {
  1074. DBG(6, "Couldn't receive the ACK")
  1075. err += -1;
  1076. }
  1077. return err;
  1078. }
  1079. /* This seems to refresh the communication through the serial bus */
  1080. static int w9968cf_smbus_refresh_bus(struct w9968cf_device* cam)
  1081. {
  1082. int err = 0, j;
  1083. for (j = 1; j <= 10; j++) {
  1084. err = w9968cf_write_reg(cam, 0x0020, 0x01);
  1085. err += w9968cf_write_reg(cam, 0x0000, 0x01);
  1086. if (err)
  1087. break;
  1088. }
  1089. return err;
  1090. }
  1091. /* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
  1092. static int
  1093. w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device* cam,
  1094. u16 address, u8 subaddress,u8 value)
  1095. {
  1096. u16* data = cam->data_buffer;
  1097. int err = 0;
  1098. err += w9968cf_smbus_refresh_bus(cam);
  1099. /* Enable SBUS outputs */
  1100. err += w9968cf_write_sb(cam, 0x0020);
  1101. data[0] = 0x082f | ((address & 0x80) ? 0x1500 : 0x0);
  1102. data[0] |= (address & 0x40) ? 0x4000 : 0x0;
  1103. data[1] = 0x2082 | ((address & 0x40) ? 0x0005 : 0x0);
  1104. data[1] |= (address & 0x20) ? 0x0150 : 0x0;
  1105. data[1] |= (address & 0x10) ? 0x5400 : 0x0;
  1106. data[2] = 0x8208 | ((address & 0x08) ? 0x0015 : 0x0);
  1107. data[2] |= (address & 0x04) ? 0x0540 : 0x0;
  1108. data[2] |= (address & 0x02) ? 0x5000 : 0x0;
  1109. data[3] = 0x1d20 | ((address & 0x02) ? 0x0001 : 0x0);
  1110. data[3] |= (address & 0x01) ? 0x0054 : 0x0;
  1111. err += w9968cf_write_fsb(cam, data);
  1112. data[0] = 0x8208 | ((subaddress & 0x80) ? 0x0015 : 0x0);
  1113. data[0] |= (subaddress & 0x40) ? 0x0540 : 0x0;
  1114. data[0] |= (subaddress & 0x20) ? 0x5000 : 0x0;
  1115. data[1] = 0x0820 | ((subaddress & 0x20) ? 0x0001 : 0x0);
  1116. data[1] |= (subaddress & 0x10) ? 0x0054 : 0x0;
  1117. data[1] |= (subaddress & 0x08) ? 0x1500 : 0x0;
  1118. data[1] |= (subaddress & 0x04) ? 0x4000 : 0x0;
  1119. data[2] = 0x2082 | ((subaddress & 0x04) ? 0x0005 : 0x0);
  1120. data[2] |= (subaddress & 0x02) ? 0x0150 : 0x0;
  1121. data[2] |= (subaddress & 0x01) ? 0x5400 : 0x0;
  1122. data[3] = 0x001d;
  1123. err += w9968cf_write_fsb(cam, data);
  1124. data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
  1125. data[0] |= (value & 0x40) ? 0x0540 : 0x0;
  1126. data[0] |= (value & 0x20) ? 0x5000 : 0x0;
  1127. data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
  1128. data[1] |= (value & 0x10) ? 0x0054 : 0x0;
  1129. data[1] |= (value & 0x08) ? 0x1500 : 0x0;
  1130. data[1] |= (value & 0x04) ? 0x4000 : 0x0;
  1131. data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
  1132. data[2] |= (value & 0x02) ? 0x0150 : 0x0;
  1133. data[2] |= (value & 0x01) ? 0x5400 : 0x0;
  1134. data[3] = 0xfe1d;
  1135. err += w9968cf_write_fsb(cam, data);
  1136. /* Disable SBUS outputs */
  1137. err += w9968cf_write_sb(cam, 0x0000);
  1138. if (!err)
  1139. DBG(5, "I2C write byte data done, addr.0x%04X, subaddr.0x%02X "
  1140. "value 0x%02X", address, subaddress, value)
  1141. else
  1142. DBG(5, "I2C write byte data failed, addr.0x%04X, "
  1143. "subaddr.0x%02X, value 0x%02X",
  1144. address, subaddress, value)
  1145. return err;
  1146. }
  1147. /* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */
  1148. static int
  1149. w9968cf_i2c_adap_read_byte_data(struct w9968cf_device* cam,
  1150. u16 address, u8 subaddress,
  1151. u8* value)
  1152. {
  1153. int err = 0;
  1154. /* Serial data enable */
  1155. err += w9968cf_write_sb(cam, 0x0013); /* don't change ! */
  1156. err += w9968cf_smbus_start(cam);
  1157. err += w9968cf_smbus_write_byte(cam, address);
  1158. err += w9968cf_smbus_read_ack(cam);
  1159. err += w9968cf_smbus_write_byte(cam, subaddress);
  1160. err += w9968cf_smbus_read_ack(cam);
  1161. err += w9968cf_smbus_stop(cam);
  1162. err += w9968cf_smbus_start(cam);
  1163. err += w9968cf_smbus_write_byte(cam, address + 1);
  1164. err += w9968cf_smbus_read_ack(cam);
  1165. err += w9968cf_smbus_read_byte(cam, value);
  1166. err += w9968cf_smbus_write_ack(cam);
  1167. err += w9968cf_smbus_stop(cam);
  1168. /* Serial data disable */
  1169. err += w9968cf_write_sb(cam, 0x0000);
  1170. if (!err)
  1171. DBG(5, "I2C read byte data done, addr.0x%04X, "
  1172. "subaddr.0x%02X, value 0x%02X",
  1173. address, subaddress, *value)
  1174. else
  1175. DBG(5, "I2C read byte data failed, addr.0x%04X, "
  1176. "subaddr.0x%02X, wrong value 0x%02X",
  1177. address, subaddress, *value)
  1178. return err;
  1179. }
  1180. /* SMBus protocol: S Addr+1 Rd [A] [Value] NA P */
  1181. static int
  1182. w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
  1183. u16 address, u8* value)
  1184. {
  1185. int err = 0;
  1186. /* Serial data enable */
  1187. err += w9968cf_write_sb(cam, 0x0013);
  1188. err += w9968cf_smbus_start(cam);
  1189. err += w9968cf_smbus_write_byte(cam, address + 1);
  1190. err += w9968cf_smbus_read_ack(cam);
  1191. err += w9968cf_smbus_read_byte(cam, value);
  1192. err += w9968cf_smbus_write_ack(cam);
  1193. err += w9968cf_smbus_stop(cam);
  1194. /* Serial data disable */
  1195. err += w9968cf_write_sb(cam, 0x0000);
  1196. if (!err)
  1197. DBG(5, "I2C read byte done, addr.0x%04X, "
  1198. "value 0x%02X", address, *value)
  1199. else
  1200. DBG(5, "I2C read byte failed, addr.0x%04X, "
  1201. "wrong value 0x%02X", address, *value)
  1202. return err;
  1203. }
  1204. /* SMBus protocol: S Addr Wr [A] Value [A] P */
  1205. static int
  1206. w9968cf_i2c_adap_write_byte(struct w9968cf_device* cam,
  1207. u16 address, u8 value)
  1208. {
  1209. DBG(4, "i2c_write_byte() is an unsupported transfer mode")
  1210. return -EINVAL;
  1211. }
  1212. /****************************************************************************
  1213. * I2C interface to kernel *
  1214. ****************************************************************************/
  1215. static int
  1216. w9968cf_i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
  1217. unsigned short flags, char read_write, u8 command,
  1218. int size, union i2c_smbus_data *data)
  1219. {
  1220. struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
  1221. struct w9968cf_device *cam = to_cam(v4l2_dev);
  1222. u8 i;
  1223. int err = 0;
  1224. if (size == I2C_SMBUS_BYTE) {
  1225. /* Why addr <<= 1? See OVXXX0_SID defines in ovcamchip.h */
  1226. addr <<= 1;
  1227. if (read_write == I2C_SMBUS_WRITE)
  1228. err = w9968cf_i2c_adap_write_byte(cam, addr, command);
  1229. else if (read_write == I2C_SMBUS_READ)
  1230. for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
  1231. err = w9968cf_i2c_adap_read_byte(cam, addr,
  1232. &data->byte);
  1233. if (err) {
  1234. if (w9968cf_smbus_refresh_bus(cam)) {
  1235. err = -EIO;
  1236. break;
  1237. }
  1238. } else
  1239. break;
  1240. }
  1241. } else if (size == I2C_SMBUS_BYTE_DATA) {
  1242. addr <<= 1;
  1243. if (read_write == I2C_SMBUS_WRITE)
  1244. err = w9968cf_i2c_adap_fastwrite_byte_data(cam, addr,
  1245. command, data->byte);
  1246. else if (read_write == I2C_SMBUS_READ) {
  1247. for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
  1248. err = w9968cf_i2c_adap_read_byte_data(cam,addr,
  1249. command, &data->byte);
  1250. if (err) {
  1251. if (w9968cf_smbus_refresh_bus(cam)) {
  1252. err = -EIO;
  1253. break;
  1254. }
  1255. } else
  1256. break;
  1257. }
  1258. } else
  1259. return -EINVAL;
  1260. } else {
  1261. DBG(4, "Unsupported I2C transfer mode (%d)", size)
  1262. return -EINVAL;
  1263. }
  1264. return err;
  1265. }
  1266. static u32 w9968cf_i2c_func(struct i2c_adapter* adap)
  1267. {
  1268. return I2C_FUNC_SMBUS_READ_BYTE |
  1269. I2C_FUNC_SMBUS_READ_BYTE_DATA |
  1270. I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
  1271. }
  1272. static int w9968cf_i2c_init(struct w9968cf_device* cam)
  1273. {
  1274. int err = 0;
  1275. static struct i2c_algorithm algo = {
  1276. .smbus_xfer = w9968cf_i2c_smbus_xfer,
  1277. .functionality = w9968cf_i2c_func,
  1278. };
  1279. static struct i2c_adapter adap = {
  1280. .owner = THIS_MODULE,
  1281. .algo = &algo,
  1282. };
  1283. memcpy(&cam->i2c_adapter, &adap, sizeof(struct i2c_adapter));
  1284. strcpy(cam->i2c_adapter.name, "w9968cf");
  1285. cam->i2c_adapter.dev.parent = &cam->usbdev->dev;
  1286. i2c_set_adapdata(&cam->i2c_adapter, &cam->v4l2_dev);
  1287. DBG(6, "Registering I2C adapter with kernel...")
  1288. err = i2c_add_adapter(&cam->i2c_adapter);
  1289. if (err)
  1290. DBG(1, "Failed to register the I2C adapter")
  1291. else
  1292. DBG(5, "I2C adapter registered")
  1293. return err;
  1294. }
  1295. /****************************************************************************
  1296. * Helper functions *
  1297. ****************************************************************************/
  1298. /*--------------------------------------------------------------------------
  1299. Turn on the LED on some webcams. A beep should be heard too.
  1300. Return 0 on success, a negative number otherwise.
  1301. --------------------------------------------------------------------------*/
  1302. static int w9968cf_turn_on_led(struct w9968cf_device* cam)
  1303. {
  1304. int err = 0;
  1305. err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power-down */
  1306. err += w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
  1307. err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
  1308. err += w9968cf_write_reg(cam, 0x0010, 0x01); /* serial bus, SDS high */
  1309. err += w9968cf_write_reg(cam, 0x0000, 0x01); /* serial bus, SDS low */
  1310. err += w9968cf_write_reg(cam, 0x0010, 0x01); /* ..high 'beep-beep' */
  1311. if (err)
  1312. DBG(2, "Couldn't turn on the LED")
  1313. DBG(5, "LED turned on")
  1314. return err;
  1315. }
  1316. /*--------------------------------------------------------------------------
  1317. Write some registers for the device initialization.
  1318. This function is called once on open().
  1319. Return 0 on success, a negative number otherwise.
  1320. --------------------------------------------------------------------------*/
  1321. static int w9968cf_init_chip(struct w9968cf_device* cam)
  1322. {
  1323. unsigned long hw_bufsize = cam->maxwidth*cam->maxheight*2,
  1324. y0 = 0x0000,
  1325. u0 = y0 + hw_bufsize/2,
  1326. v0 = u0 + hw_bufsize/4,
  1327. y1 = v0 + hw_bufsize/4,
  1328. u1 = y1 + hw_bufsize/2,
  1329. v1 = u1 + hw_bufsize/4;
  1330. int err = 0;
  1331. err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power off */
  1332. err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* power on */
  1333. err += w9968cf_write_reg(cam, 0x405d, 0x03); /* DRAM timings */
  1334. err += w9968cf_write_reg(cam, 0x0030, 0x04); /* SDRAM timings */
  1335. err += w9968cf_write_reg(cam, y0 & 0xffff, 0x20); /* Y buf.0, low */
  1336. err += w9968cf_write_reg(cam, y0 >> 16, 0x21); /* Y buf.0, high */
  1337. err += w9968cf_write_reg(cam, u0 & 0xffff, 0x24); /* U buf.0, low */
  1338. err += w9968cf_write_reg(cam, u0 >> 16, 0x25); /* U buf.0, high */
  1339. err += w9968cf_write_reg(cam, v0 & 0xffff, 0x28); /* V buf.0, low */
  1340. err += w9968cf_write_reg(cam, v0 >> 16, 0x29); /* V buf.0, high */
  1341. err += w9968cf_write_reg(cam, y1 & 0xffff, 0x22); /* Y buf.1, low */
  1342. err += w9968cf_write_reg(cam, y1 >> 16, 0x23); /* Y buf.1, high */
  1343. err += w9968cf_write_reg(cam, u1 & 0xffff, 0x26); /* U buf.1, low */
  1344. err += w9968cf_write_reg(cam, u1 >> 16, 0x27); /* U buf.1, high */
  1345. err += w9968cf_write_reg(cam, v1 & 0xffff, 0x2a); /* V buf.1, low */
  1346. err += w9968cf_write_reg(cam, v1 >> 16, 0x2b); /* V buf.1, high */
  1347. err += w9968cf_write_reg(cam, y1 & 0xffff, 0x32); /* JPEG buf 0 low */
  1348. err += w9968cf_write_reg(cam, y1 >> 16, 0x33); /* JPEG buf 0 high */
  1349. err += w9968cf_write_reg(cam, y1 & 0xffff, 0x34); /* JPEG buf 1 low */
  1350. err += w9968cf_write_reg(cam, y1 >> 16, 0x35); /* JPEG bug 1 high */
  1351. err += w9968cf_write_reg(cam, 0x0000, 0x36);/* JPEG restart interval */
  1352. err += w9968cf_write_reg(cam, 0x0804, 0x37);/*JPEG VLE FIFO threshold*/
  1353. err += w9968cf_write_reg(cam, 0x0000, 0x38);/* disable hw up-scaling */
  1354. err += w9968cf_write_reg(cam, 0x0000, 0x3f); /* JPEG/MCTL test data */
  1355. err += w9968cf_set_picture(cam, cam->picture); /* this before */
  1356. err += w9968cf_set_window(cam, cam->window);
  1357. if (err)
  1358. DBG(1, "Chip initialization failed")
  1359. else
  1360. DBG(5, "Chip successfully initialized")
  1361. return err;
  1362. }
  1363. /*--------------------------------------------------------------------------
  1364. Return non-zero if the palette is supported, 0 otherwise.
  1365. --------------------------------------------------------------------------*/
  1366. static inline u16 w9968cf_valid_palette(u16 palette)
  1367. {
  1368. u8 i = 0;
  1369. while (w9968cf_formatlist[i].palette != 0) {
  1370. if (palette == w9968cf_formatlist[i].palette)
  1371. return palette;
  1372. i++;
  1373. }
  1374. return 0;
  1375. }
  1376. /*--------------------------------------------------------------------------
  1377. Return the depth corresponding to the given palette.
  1378. Palette _must_ be supported !
  1379. --------------------------------------------------------------------------*/
  1380. static inline u16 w9968cf_valid_depth(u16 palette)
  1381. {
  1382. u8 i=0;
  1383. while (w9968cf_formatlist[i].palette != palette)
  1384. i++;
  1385. return w9968cf_formatlist[i].depth;
  1386. }
  1387. /*--------------------------------------------------------------------------
  1388. Return non-zero if the format requires decompression, 0 otherwise.
  1389. --------------------------------------------------------------------------*/
  1390. static inline u8 w9968cf_need_decompression(u16 palette)
  1391. {
  1392. u8 i = 0;
  1393. while (w9968cf_formatlist[i].palette != 0) {
  1394. if (palette == w9968cf_formatlist[i].palette)
  1395. return w9968cf_formatlist[i].compression;
  1396. i++;
  1397. }
  1398. return 0;
  1399. }
  1400. /*--------------------------------------------------------------------------
  1401. Change the picture settings of the camera.
  1402. Return 0 on success, a negative number otherwise.
  1403. --------------------------------------------------------------------------*/
  1404. static int
  1405. w9968cf_set_picture(struct w9968cf_device* cam, struct video_picture pict)
  1406. {
  1407. u16 fmt, hw_depth, hw_palette, reg_v = 0x0000;
  1408. int err = 0;
  1409. /* Make sure we are using a valid depth */
  1410. pict.depth = w9968cf_valid_depth(pict.palette);
  1411. fmt = pict.palette;
  1412. hw_depth = pict.depth; /* depth used by the winbond chip */
  1413. hw_palette = pict.palette; /* palette used by the winbond chip */
  1414. /* VS & HS polarities */
  1415. reg_v = (cam->vs_polarity << 12) | (cam->hs_polarity << 11);
  1416. switch (fmt)
  1417. {
  1418. case VIDEO_PALETTE_UYVY:
  1419. reg_v |= 0x0000;
  1420. cam->vpp_flag = VPP_NONE;
  1421. break;
  1422. case VIDEO_PALETTE_YUV422P:
  1423. reg_v |= 0x0002;
  1424. cam->vpp_flag = VPP_DECOMPRESSION;
  1425. break;
  1426. case VIDEO_PALETTE_YUV420:
  1427. case VIDEO_PALETTE_YUV420P:
  1428. reg_v |= 0x0003;
  1429. cam->vpp_flag = VPP_DECOMPRESSION;
  1430. break;
  1431. case VIDEO_PALETTE_YUYV:
  1432. case VIDEO_PALETTE_YUV422:
  1433. reg_v |= 0x0000;
  1434. cam->vpp_flag = VPP_SWAP_YUV_BYTES;
  1435. hw_palette = VIDEO_PALETTE_UYVY;
  1436. break;
  1437. /* Original video is used instead of RGBX palettes.
  1438. Software conversion later. */
  1439. case VIDEO_PALETTE_GREY:
  1440. case VIDEO_PALETTE_RGB555:
  1441. case VIDEO_PALETTE_RGB565:
  1442. case VIDEO_PALETTE_RGB24:
  1443. case VIDEO_PALETTE_RGB32:
  1444. reg_v |= 0x0000; /* UYVY 16 bit is used */
  1445. hw_depth = 16;
  1446. hw_palette = VIDEO_PALETTE_UYVY;
  1447. cam->vpp_flag = VPP_UYVY_TO_RGBX;
  1448. break;
  1449. }
  1450. /* NOTE: due to memory issues, it is better to disable the hardware
  1451. double buffering during compression */
  1452. if (cam->double_buffer && !(cam->vpp_flag & VPP_DECOMPRESSION))
  1453. reg_v |= 0x0080;
  1454. if (cam->clamping)
  1455. reg_v |= 0x0020;
  1456. if (cam->filter_type == 1)
  1457. reg_v |= 0x0008;
  1458. else if (cam->filter_type == 2)
  1459. reg_v |= 0x000c;
  1460. if ((err = w9968cf_write_reg(cam, reg_v, 0x16)))
  1461. goto error;
  1462. if ((err = w9968cf_sensor_update_picture(cam, pict)))
  1463. goto error;
  1464. /* If all went well, update the device data structure */
  1465. memcpy(&cam->picture, &pict, sizeof(pict));
  1466. cam->hw_depth = hw_depth;
  1467. cam->hw_palette = hw_palette;
  1468. /* Settings changed, so we clear the frame buffers */
  1469. memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
  1470. DBG(4, "Palette is %s, depth is %u bpp",
  1471. symbolic(v4l1_plist, pict.palette), pict.depth)
  1472. return 0;
  1473. error:
  1474. DBG(1, "Failed to change picture settings")
  1475. return err;
  1476. }
  1477. /*--------------------------------------------------------------------------
  1478. Change the capture area size of the camera.
  1479. This function _must_ be called _after_ w9968cf_set_picture().
  1480. Return 0 on success, a negative number otherwise.
  1481. --------------------------------------------------------------------------*/
  1482. static int
  1483. w9968cf_set_window(struct w9968cf_device* cam, struct video_window win)
  1484. {
  1485. u16 x, y, w, h, scx, scy, cw, ch, ax, ay;
  1486. unsigned long fw, fh;
  1487. struct ovcamchip_window s_win;
  1488. int err = 0;
  1489. /* Work around to avoid FP arithmetics */
  1490. #define SC(x) ((x) << 10)
  1491. #define UNSC(x) ((x) >> 10)
  1492. /* Make sure we are using a supported resolution */
  1493. if ((err = w9968cf_adjust_window_size(cam, &win.width, &win.height)))
  1494. goto error;
  1495. /* Scaling factors */
  1496. fw = SC(win.width) / cam->maxwidth;
  1497. fh = SC(win.height) / cam->maxheight;
  1498. /* Set up the width and height values used by the chip */
  1499. if ((win.width > cam->maxwidth) || (win.height > cam->maxheight)) {
  1500. cam->vpp_flag |= VPP_UPSCALE;
  1501. /* Calculate largest w,h mantaining the same w/h ratio */
  1502. w = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
  1503. h = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
  1504. if (w < cam->minwidth) /* just in case */
  1505. w = cam->minwidth;
  1506. if (h < cam->minheight) /* just in case */
  1507. h = cam->minheight;
  1508. } else {
  1509. cam->vpp_flag &= ~VPP_UPSCALE;
  1510. w = win.width;
  1511. h = win.height;
  1512. }
  1513. /* x,y offsets of the cropped area */
  1514. scx = cam->start_cropx;
  1515. scy = cam->start_cropy;
  1516. /* Calculate cropped area manteining the right w/h ratio */
  1517. if (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE)) {
  1518. cw = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
  1519. ch = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
  1520. } else {
  1521. cw = w;
  1522. ch = h;
  1523. }
  1524. /* Setup the window of the sensor */
  1525. s_win.format = VIDEO_PALETTE_UYVY;
  1526. s_win.width = cam->maxwidth;
  1527. s_win.height = cam->maxheight;
  1528. s_win.quarter = 0; /* full progressive video */
  1529. /* Center it */
  1530. s_win.x = (s_win.width - cw) / 2;
  1531. s_win.y = (s_win.height - ch) / 2;
  1532. /* Clock divisor */
  1533. if (cam->clockdiv >= 0)
  1534. s_win.clockdiv = cam->clockdiv; /* manual override */
  1535. else
  1536. switch (cam->sensor) {
  1537. case CC_OV6620:
  1538. s_win.clockdiv = 0;
  1539. break;
  1540. case CC_OV6630:
  1541. s_win.clockdiv = 0;
  1542. break;
  1543. case CC_OV76BE:
  1544. case CC_OV7610:
  1545. case CC_OV7620:
  1546. s_win.clockdiv = 0;
  1547. break;
  1548. default:
  1549. s_win.clockdiv = W9968CF_DEF_CLOCKDIVISOR;
  1550. }
  1551. /* We have to scale win.x and win.y offsets */
  1552. if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
  1553. || (cam->vpp_flag & VPP_UPSCALE) ) {
  1554. ax = SC(win.x)/fw;
  1555. ay = SC(win.y)/fh;
  1556. } else {
  1557. ax = win.x;
  1558. ay = win.y;
  1559. }
  1560. if ((ax + cw) > cam->maxwidth)
  1561. ax = cam->maxwidth - cw;
  1562. if ((ay + ch) > cam->maxheight)
  1563. ay = cam->maxheight - ch;
  1564. /* Adjust win.x, win.y */
  1565. if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
  1566. || (cam->vpp_flag & VPP_UPSCALE) ) {
  1567. win.x = UNSC(ax*fw);
  1568. win.y = UNSC(ay*fh);
  1569. } else {
  1570. win.x = ax;
  1571. win.y = ay;
  1572. }
  1573. /* Offsets used by the chip */
  1574. x = ax + s_win.x;
  1575. y = ay + s_win.y;
  1576. /* Go ! */
  1577. if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_MODE, &s_win)))
  1578. goto error;
  1579. err += w9968cf_write_reg(cam, scx + x, 0x10);
  1580. err += w9968cf_write_reg(cam, scy + y, 0x11);
  1581. err += w9968cf_write_reg(cam, scx + x + cw, 0x12);
  1582. err += w9968cf_write_reg(cam, scy + y + ch, 0x13);
  1583. err += w9968cf_write_reg(cam, w, 0x14);
  1584. err += w9968cf_write_reg(cam, h, 0x15);
  1585. /* JPEG width & height */
  1586. err += w9968cf_write_reg(cam, w, 0x30);
  1587. err += w9968cf_write_reg(cam, h, 0x31);
  1588. /* Y & UV frame buffer strides (in WORD) */
  1589. if (cam->vpp_flag & VPP_DECOMPRESSION) {
  1590. err += w9968cf_write_reg(cam, w/2, 0x2c);
  1591. err += w9968cf_write_reg(cam, w/4, 0x2d);
  1592. } else
  1593. err += w9968cf_write_reg(cam, w, 0x2c);
  1594. if (err)
  1595. goto error;
  1596. /* If all went well, update the device data structure */
  1597. memcpy(&cam->window, &win, sizeof(win));
  1598. cam->hw_width = w;
  1599. cam->hw_height = h;
  1600. /* Settings changed, so we clear the frame buffers */
  1601. memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
  1602. DBG(4, "The capture area is %dx%d, Offset (x,y)=(%u,%u)",
  1603. win.width, win.height, win.x, win.y)
  1604. PDBGG("x=%u ,y=%u, w=%u, h=%u, ax=%u, ay=%u, s_win.x=%u, s_win.y=%u, "
  1605. "cw=%u, ch=%u, win.x=%u, win.y=%u, win.width=%u, win.height=%u",
  1606. x, y, w, h, ax, ay, s_win.x, s_win.y, cw, ch, win.x, win.y,
  1607. win.width, win.height)
  1608. return 0;
  1609. error:
  1610. DBG(1, "Failed to change the capture area size")
  1611. return err;
  1612. }
  1613. /*--------------------------------------------------------------------------
  1614. Adjust the asked values for window width and height.
  1615. Return 0 on success, -1 otherwise.
  1616. --------------------------------------------------------------------------*/
  1617. static int
  1618. w9968cf_adjust_window_size(struct w9968cf_device *cam, u32 *width, u32 *height)
  1619. {
  1620. unsigned int maxw, maxh, align;
  1621. maxw = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
  1622. w9968cf_vpp ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
  1623. : cam->maxwidth;
  1624. maxh = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
  1625. w9968cf_vpp ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
  1626. : cam->maxheight;
  1627. align = (cam->vpp_flag & VPP_DECOMPRESSION) ? 4 : 0;
  1628. v4l_bound_align_image(width, cam->minwidth, maxw, align,
  1629. height, cam->minheight, maxh, align, 0);
  1630. PDBGG("Window size adjusted w=%u, h=%u ", *width, *height)
  1631. return 0;
  1632. }
  1633. /*--------------------------------------------------------------------------
  1634. Initialize the FIFO list of requested frames.
  1635. --------------------------------------------------------------------------*/
  1636. static void w9968cf_init_framelist(struct w9968cf_device* cam)
  1637. {
  1638. u8 i;
  1639. for (i = 0; i < cam->nbuffers; i++) {
  1640. cam->requested_frame[i] = NULL;
  1641. cam->frame[i].queued = 0;
  1642. cam->frame[i].status = F_UNUSED;
  1643. }
  1644. }
  1645. /*--------------------------------------------------------------------------
  1646. Add a frame in the FIFO list of requested frames.
  1647. This function is called in process context.
  1648. --------------------------------------------------------------------------*/
  1649. static void w9968cf_push_frame(struct w9968cf_device* cam, u8 f_num)
  1650. {
  1651. u8 f;
  1652. unsigned long lock_flags;
  1653. spin_lock_irqsave(&cam->flist_lock, lock_flags);
  1654. for (f=0; cam->requested_frame[f] != NULL; f++);
  1655. cam->requested_frame[f] = &cam->frame[f_num];
  1656. cam->frame[f_num].queued = 1;
  1657. cam->frame[f_num].status = F_UNUSED; /* clear the status */
  1658. spin_unlock_irqrestore(&cam->flist_lock, lock_flags);
  1659. DBG(6, "Frame #%u pushed into the FIFO list. Position %u", f_num, f)
  1660. }
  1661. /*--------------------------------------------------------------------------
  1662. Read, store and remove the first pointer in the FIFO list of requested
  1663. frames. This function is called in interrupt context.
  1664. --------------------------------------------------------------------------*/
  1665. static void
  1666. w9968cf_pop_frame(struct w9968cf_device* cam, struct w9968cf_frame_t** framep)
  1667. {
  1668. u8 i;
  1669. spin_lock(&cam->flist_lock);
  1670. *framep = cam->requested_frame[0];
  1671. /* Shift the list of pointers */
  1672. for (i = 0; i < cam->nbuffers-1; i++)
  1673. cam->requested_frame[i] = cam->requested_frame[i+1];
  1674. cam->requested_frame[i] = NULL;
  1675. spin_unlock(&cam->flist_lock);
  1676. DBG(6,"Popped frame #%d from the list", (*framep)->number)
  1677. }
  1678. /*--------------------------------------------------------------------------
  1679. High-level video post-processing routine on grabbed frames.
  1680. Return 0 on success, a negative number otherwise.
  1681. --------------------------------------------------------------------------*/
  1682. static int
  1683. w9968cf_postprocess_frame(struct w9968cf_device* cam,
  1684. struct w9968cf_frame_t* fr)
  1685. {
  1686. void *pIn = fr->buffer, *pOut = cam->frame_vpp.buffer, *tmp;
  1687. u16 w = cam->window.width,
  1688. h = cam->window.height,
  1689. d = cam->picture.depth,
  1690. fmt = cam->picture.palette,
  1691. rgb = cam->force_rgb,
  1692. hw_w = cam->hw_width,
  1693. hw_h = cam->hw_height,
  1694. hw_d = cam->hw_depth;
  1695. int err = 0;
  1696. #define _PSWAP(pIn, pOut) {tmp = (pIn); (pIn) = (pOut); (pOut) = tmp;}
  1697. if (cam->vpp_flag & VPP_DECOMPRESSION) {
  1698. memcpy(pOut, pIn, fr->length);
  1699. _PSWAP(pIn, pOut)
  1700. err = w9968cf_vpp->decode(pIn, fr->length, hw_w, hw_h, pOut);
  1701. PDBGG("Compressed frame length: %lu",(unsigned long)fr->length)
  1702. fr->length = (hw_w*hw_h*hw_d)/8;
  1703. _PSWAP(pIn, pOut)
  1704. if (err) {
  1705. DBG(4, "An error occurred while decoding the frame: "
  1706. "%s", symbolic(decoder_errlist, err))
  1707. return err;
  1708. } else
  1709. DBG(6, "Frame decoded")
  1710. }
  1711. if (cam->vpp_flag & VPP_SWAP_YUV_BYTES) {
  1712. w9968cf_vpp->swap_yuvbytes(pIn, fr->length);
  1713. DBG(6, "Original UYVY component ordering changed")
  1714. }
  1715. if (cam->vpp_flag & VPP_UPSCALE) {
  1716. w9968cf_vpp->scale_up(pIn, pOut, hw_w, hw_h, hw_d, w, h);
  1717. fr->length = (w*h*hw_d)/8;
  1718. _PSWAP(pIn, pOut)
  1719. DBG(6, "Vertical up-scaling done: %u,%u,%ubpp->%u,%u",
  1720. hw_w, hw_h, hw_d, w, h)
  1721. }
  1722. if (cam->vpp_flag & VPP_UYVY_TO_RGBX) {
  1723. w9968cf_vpp->uyvy_to_rgbx(pIn, fr->length, pOut, fmt, rgb);
  1724. fr->length = (w*h*d)/8;
  1725. _PSWAP(pIn, pOut)
  1726. DBG(6, "UYVY-16bit to %s conversion done",
  1727. symbolic(v4l1_plist, fmt))
  1728. }
  1729. if (pOut == fr->buffer)
  1730. memcpy(fr->buffer, cam->frame_vpp.buffer, fr->length);
  1731. return 0;
  1732. }
  1733. /****************************************************************************
  1734. * Image sensor control routines *
  1735. ****************************************************************************/
  1736. static int
  1737. w9968cf_sensor_set_control(struct w9968cf_device* cam, int cid, int val)
  1738. {
  1739. struct ovcamchip_control ctl;
  1740. int err;
  1741. ctl.id = cid;
  1742. ctl.value = val;
  1743. err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ctl);
  1744. return err;
  1745. }
  1746. static int
  1747. w9968cf_sensor_get_control(struct w9968cf_device* cam, int cid, int* val)
  1748. {
  1749. struct ovcamchip_control ctl;
  1750. int err;
  1751. ctl.id = cid;
  1752. err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ctl);
  1753. if (!err)
  1754. *val = ctl.value;
  1755. return err;
  1756. }
  1757. static int
  1758. w9968cf_sensor_cmd(struct w9968cf_device* cam, unsigned int cmd, void* arg)
  1759. {
  1760. int rc;
  1761. rc = v4l2_subdev_call(cam->sensor_sd, core, ioctl, cmd, arg);
  1762. /* The I2C driver returns -EPERM on non-supported controls */
  1763. return (rc < 0 && rc != -EPERM) ? rc : 0;
  1764. }
  1765. /*--------------------------------------------------------------------------
  1766. Update some settings of the image sensor.
  1767. Returns: 0 on success, a negative number otherwise.
  1768. --------------------------------------------------------------------------*/
  1769. static int w9968cf_sensor_update_settings(struct w9968cf_device* cam)
  1770. {
  1771. int err = 0;
  1772. /* Auto brightness */
  1773. err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOBRIGHT,
  1774. cam->auto_brt);
  1775. if (err)
  1776. return err;
  1777. /* Auto exposure */
  1778. err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOEXP,
  1779. cam->auto_exp);
  1780. if (err)
  1781. return err;
  1782. /* Banding filter */
  1783. err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BANDFILT,
  1784. cam->bandfilt);
  1785. if (err)
  1786. return err;
  1787. /* Light frequency */
  1788. err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_FREQ,
  1789. cam->lightfreq);
  1790. if (err)
  1791. return err;
  1792. /* Back light */
  1793. err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BACKLIGHT,
  1794. cam->backlight);
  1795. if (err)
  1796. return err;
  1797. /* Mirror */
  1798. err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_MIRROR,
  1799. cam->mirror);
  1800. if (err)
  1801. return err;
  1802. return 0;
  1803. }
  1804. /*--------------------------------------------------------------------------
  1805. Get some current picture settings from the image sensor and update the
  1806. internal 'picture' structure of the camera.
  1807. Returns: 0 on success, a negative number otherwise.
  1808. --------------------------------------------------------------------------*/
  1809. static int w9968cf_sensor_get_picture(struct w9968cf_device* cam)
  1810. {
  1811. int err, v;
  1812. err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_CONT, &v);
  1813. if (err)
  1814. return err;
  1815. cam->picture.contrast = v;
  1816. err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_BRIGHT, &v);
  1817. if (err)
  1818. return err;
  1819. cam->picture.brightness = v;
  1820. err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_SAT, &v);
  1821. if (err)
  1822. return err;
  1823. cam->picture.colour = v;
  1824. err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_HUE, &v);
  1825. if (err)
  1826. return err;
  1827. cam->picture.hue = v;
  1828. DBG(5, "Got picture settings from the image sensor")
  1829. PDBGG("Brightness, contrast, hue, colour, whiteness are "
  1830. "%u,%u,%u,%u,%u", cam->picture.brightness,cam->picture.contrast,
  1831. cam->picture.hue, cam->picture.colour, cam->picture.whiteness)
  1832. return 0;
  1833. }
  1834. /*--------------------------------------------------------------------------
  1835. Update picture settings of the image sensor.
  1836. Returns: 0 on success, a negative number otherwise.
  1837. --------------------------------------------------------------------------*/
  1838. static int
  1839. w9968cf_sensor_update_picture(struct w9968cf_device* cam,
  1840. struct video_picture pict)
  1841. {
  1842. int err = 0;
  1843. if ((!cam->sensor_initialized)
  1844. || pict.contrast != cam->picture.contrast) {
  1845. err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_CONT,
  1846. pict.contrast);
  1847. if (err)
  1848. goto fail;
  1849. DBG(4, "Contrast changed from %u to %u",
  1850. cam->picture.contrast, pict.contrast)
  1851. cam->picture.contrast = pict.contrast;
  1852. }
  1853. if (((!cam->sensor_initialized) ||
  1854. pict.brightness != cam->picture.brightness) && (!cam->auto_brt)) {
  1855. err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BRIGHT,
  1856. pict.brightness);
  1857. if (err)
  1858. goto fail;
  1859. DBG(4, "Brightness changed from %u to %u",
  1860. cam->picture.brightness, pict.brightness)
  1861. cam->picture.brightness = pict.brightness;
  1862. }
  1863. if ((!cam->sensor_initialized) || pict.colour != cam->picture.colour) {
  1864. err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_SAT,
  1865. pict.colour);
  1866. if (err)
  1867. goto fail;
  1868. DBG(4, "Colour changed from %u to %u",
  1869. cam->picture.colour, pict.colour)
  1870. cam->picture.colour = pict.colour;
  1871. }
  1872. if ((!cam->sensor_initialized) || pict.hue != cam->picture.hue) {
  1873. err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_HUE,
  1874. pict.hue);
  1875. if (err)
  1876. goto fail;
  1877. DBG(4, "Hue changed from %u to %u",
  1878. cam->picture.hue, pict.hue)
  1879. cam->picture.hue = pict.hue;
  1880. }
  1881. return 0;
  1882. fail:
  1883. DBG(4, "Failed to change sensor picture setting")
  1884. return err;
  1885. }
  1886. /****************************************************************************
  1887. * Camera configuration *
  1888. ****************************************************************************/
  1889. /*--------------------------------------------------------------------------
  1890. This function is called when a supported image sensor is detected.
  1891. Return 0 if the initialization succeeds, a negative number otherwise.
  1892. --------------------------------------------------------------------------*/
  1893. static int w9968cf_sensor_init(struct w9968cf_device* cam)
  1894. {
  1895. int err = 0;
  1896. if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_INITIALIZE,
  1897. &cam->monochrome)))
  1898. goto error;
  1899. if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE,
  1900. &cam->sensor)))
  1901. goto error;
  1902. /* NOTE: Make sure width and height are a multiple of 16 */
  1903. switch (v4l2_i2c_subdev_addr(cam->sensor_sd)) {
  1904. case OV6xx0_SID:
  1905. cam->maxwidth = 352;
  1906. cam->maxheight = 288;
  1907. cam->minwidth = 64;
  1908. cam->minheight = 48;
  1909. break;
  1910. case OV7xx0_SID:
  1911. cam->maxwidth = 640;
  1912. cam->maxheight = 480;
  1913. cam->minwidth = 64;
  1914. cam->minheight = 48;
  1915. break;
  1916. default:
  1917. DBG(1, "Not supported image sensor detected for %s",
  1918. symbolic(camlist, cam->id))
  1919. return -EINVAL;
  1920. }
  1921. /* These values depend on the ones in the ovxxx0.c sources */
  1922. switch (cam->sensor) {
  1923. case CC_OV7620:
  1924. cam->start_cropx = 287;
  1925. cam->start_cropy = 35;
  1926. /* Seems to work around a bug in the image sensor */
  1927. cam->vs_polarity = 1;
  1928. cam->hs_polarity = 1;
  1929. break;
  1930. default:
  1931. cam->start_cropx = 320;
  1932. cam->start_cropy = 35;
  1933. cam->vs_polarity = 1;
  1934. cam->hs_polarity = 0;
  1935. }
  1936. if ((err = w9968cf_sensor_update_settings(cam)))
  1937. goto error;
  1938. if ((err = w9968cf_sensor_update_picture(cam, cam->picture)))
  1939. goto error;
  1940. cam->sensor_initialized = 1;
  1941. DBG(2, "%s image sensor initialized", symbolic(senlist, cam->sensor))
  1942. return 0;
  1943. error:
  1944. cam->sensor_initialized = 0;
  1945. cam->sensor = CC_UNKNOWN;
  1946. DBG(1, "Image sensor initialization failed for %s (%s). "
  1947. "Try to detach and attach this device again",
  1948. symbolic(camlist, cam->id), video_device_node_name(cam->v4ldev))
  1949. return err;
  1950. }
  1951. /*--------------------------------------------------------------------------
  1952. Fill some basic fields in the main device data structure.
  1953. This function is called once on w9968cf_usb_probe() for each recognized
  1954. camera.
  1955. --------------------------------------------------------------------------*/
  1956. static void
  1957. w9968cf_configure_camera(struct w9968cf_device* cam,
  1958. struct usb_device* udev,
  1959. enum w9968cf_model_id mod_id,
  1960. const unsigned short dev_nr)
  1961. {
  1962. mutex_init(&cam->fileop_mutex);
  1963. init_waitqueue_head(&cam->open);
  1964. spin_lock_init(&cam->urb_lock);
  1965. spin_lock_init(&cam->flist_lock);
  1966. cam->users = 0;
  1967. cam->disconnected = 0;
  1968. cam->id = mod_id;
  1969. cam->sensor = CC_UNKNOWN;
  1970. cam->sensor_initialized = 0;
  1971. /* Calculate the alternate setting number (from 1 to 16)
  1972. according to the 'packet_size' module parameter */
  1973. if (packet_size[dev_nr] < W9968CF_MIN_PACKET_SIZE)
  1974. packet_size[dev_nr] = W9968CF_MIN_PACKET_SIZE;
  1975. for (cam->altsetting = 1;
  1976. packet_size[dev_nr] < wMaxPacketSize[cam->altsetting-1];
  1977. cam->altsetting++);
  1978. cam->max_buffers = (max_buffers[dev_nr] < 2 ||
  1979. max_buffers[dev_nr] > W9968CF_MAX_BUFFERS)
  1980. ? W9968CF_BUFFERS : (u8)max_buffers[dev_nr];
  1981. cam->double_buffer = (double_buffer[dev_nr] == 0 ||
  1982. double_buffer[dev_nr] == 1)
  1983. ? (u8)double_buffer[dev_nr]:W9968CF_DOUBLE_BUFFER;
  1984. cam->clamping = (clamping[dev_nr] == 0 || clamping[dev_nr] == 1)
  1985. ? (u8)clamping[dev_nr] : W9968CF_CLAMPING;
  1986. cam->filter_type = (filter_type[dev_nr] == 0 ||
  1987. filter_type[dev_nr] == 1 ||
  1988. filter_type[dev_nr] == 2)
  1989. ? (u8)filter_type[dev_nr] : W9968CF_FILTER_TYPE;
  1990. cam->capture = 1;
  1991. cam->largeview = (largeview[dev_nr] == 0 || largeview[dev_nr] == 1)
  1992. ? (u8)largeview[dev_nr] : W9968CF_LARGEVIEW;
  1993. cam->decompression = (decompression[dev_nr] == 0 ||
  1994. decompression[dev_nr] == 1 ||
  1995. decompression[dev_nr] == 2)
  1996. ? (u8)decompression[dev_nr]:W9968CF_DECOMPRESSION;
  1997. cam->upscaling = (upscaling[dev_nr] == 0 ||
  1998. upscaling[dev_nr] == 1)
  1999. ? (u8)upscaling[dev_nr] : W9968CF_UPSCALING;
  2000. cam->auto_brt = (autobright[dev_nr] == 0 || autobright[dev_nr] == 1)
  2001. ? (u8)autobright[dev_nr] : W9968CF_AUTOBRIGHT;
  2002. cam->auto_exp = (autoexp[dev_nr] == 0 || autoexp[dev_nr] == 1)
  2003. ? (u8)autoexp[dev_nr] : W9968CF_AUTOEXP;
  2004. cam->lightfreq = (lightfreq[dev_nr] == 50 || lightfreq[dev_nr] == 60)
  2005. ? (u8)lightfreq[dev_nr] : W9968CF_LIGHTFREQ;
  2006. cam->bandfilt = (bandingfilter[dev_nr] == 0 ||
  2007. bandingfilter[dev_nr] == 1)
  2008. ? (u8)bandingfilter[dev_nr] : W9968CF_BANDINGFILTER;
  2009. cam->backlight = (backlight[dev_nr] == 0 || backlight[dev_nr] == 1)
  2010. ? (u8)backlight[dev_nr] : W9968CF_BACKLIGHT;
  2011. cam->clockdiv = (clockdiv[dev_nr] == -1 || clockdiv[dev_nr] >= 0)
  2012. ? (s8)clockdiv[dev_nr] : W9968CF_CLOCKDIV;
  2013. cam->mirror = (mirror[dev_nr] == 0 || mirror[dev_nr] == 1)
  2014. ? (u8)mirror[dev_nr] : W9968CF_MIRROR;
  2015. cam->monochrome = (monochrome[dev_nr] == 0 || monochrome[dev_nr] == 1)
  2016. ? monochrome[dev_nr] : W9968CF_MONOCHROME;
  2017. cam->picture.brightness = (u16)brightness[dev_nr];
  2018. cam->picture.hue = (u16)hue[dev_nr];
  2019. cam->picture.colour = (u16)colour[dev_nr];
  2020. cam->picture.contrast = (u16)contrast[dev_nr];
  2021. cam->picture.whiteness = (u16)whiteness[dev_nr];
  2022. if (w9968cf_valid_palette((u16)force_palette[dev_nr])) {
  2023. cam->picture.palette = (u16)force_palette[dev_nr];
  2024. cam->force_palette = 1;
  2025. } else {
  2026. cam->force_palette = 0;
  2027. if (cam->decompression == 0)
  2028. cam->picture.palette = W9968CF_PALETTE_DECOMP_OFF;
  2029. else if (cam->decompression == 1)
  2030. cam->picture.palette = W9968CF_PALETTE_DECOMP_FORCE;
  2031. else
  2032. cam->picture.palette = W9968CF_PALETTE_DECOMP_ON;
  2033. }
  2034. cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
  2035. cam->force_rgb = (force_rgb[dev_nr] == 0 || force_rgb[dev_nr] == 1)
  2036. ? (u8)force_rgb[dev_nr] : W9968CF_FORCE_RGB;
  2037. cam->window.x = 0;
  2038. cam->window.y = 0;
  2039. cam->window.width = W9968CF_WIDTH;
  2040. cam->window.height = W9968CF_HEIGHT;
  2041. cam->window.chromakey = 0;
  2042. cam->window.clipcount = 0;
  2043. cam->window.flags = 0;
  2044. DBG(3, "%s configured with settings #%u:",
  2045. symbolic(camlist, cam->id), dev_nr)
  2046. DBG(3, "- Data packet size for USB isochrnous transfer: %u bytes",
  2047. wMaxPacketSize[cam->altsetting-1])
  2048. DBG(3, "- Number of requested video frame buffers: %u",
  2049. cam->max_buffers)
  2050. if (cam->double_buffer)
  2051. DBG(3, "- Hardware double buffering enabled")
  2052. else
  2053. DBG(3, "- Hardware double buffering disabled")
  2054. if (cam->filter_type == 0)
  2055. DBG(3, "- Video filtering disabled")
  2056. else if (cam->filter_type == 1)
  2057. DBG(3, "- Video filtering enabled: type 1-2-1")
  2058. else if (cam->filter_type == 2)
  2059. DBG(3, "- Video filtering enabled: type 2-3-6-3-2")
  2060. if (cam->clamping)
  2061. DBG(3, "- Video data clamping (CCIR-601 format) enabled")
  2062. else
  2063. DBG(3, "- Video data clamping (CCIR-601 format) disabled")
  2064. if (cam->largeview)
  2065. DBG(3, "- Large view enabled")
  2066. else
  2067. DBG(3, "- Large view disabled")
  2068. if ((cam->decompression) == 0 && (!cam->force_palette))
  2069. DBG(3, "- Decompression disabled")
  2070. else if ((cam->decompression) == 1 && (!cam->force_palette))
  2071. DBG(3, "- Decompression forced")
  2072. else if ((cam->decompression) == 2 && (!cam->force_palette))
  2073. DBG(3, "- Decompression allowed")
  2074. if (cam->upscaling)
  2075. DBG(3, "- Software image scaling enabled")
  2076. else
  2077. DBG(3, "- Software image scaling disabled")
  2078. if (cam->force_palette)
  2079. DBG(3, "- Image palette forced to %s",
  2080. symbolic(v4l1_plist, cam->picture.palette))
  2081. if (cam->force_rgb)
  2082. DBG(3, "- RGB component ordering will be used instead of BGR")
  2083. if (cam->auto_brt)
  2084. DBG(3, "- Auto brightness enabled")
  2085. else
  2086. DBG(3, "- Auto brightness disabled")
  2087. if (cam->auto_exp)
  2088. DBG(3, "- Auto exposure enabled")
  2089. else
  2090. DBG(3, "- Auto exposure disabled")
  2091. if (cam->backlight)
  2092. DBG(3, "- Backlight exposure algorithm enabled")
  2093. else
  2094. DBG(3, "- Backlight exposure algorithm disabled")
  2095. if (cam->mirror)
  2096. DBG(3, "- Mirror enabled")
  2097. else
  2098. DBG(3, "- Mirror disabled")
  2099. if (cam->bandfilt)
  2100. DBG(3, "- Banding filter enabled")
  2101. else
  2102. DBG(3, "- Banding filter disabled")
  2103. DBG(3, "- Power lighting frequency: %u", cam->lightfreq)
  2104. if (cam->clockdiv == -1)
  2105. DBG(3, "- Automatic clock divisor enabled")
  2106. else
  2107. DBG(3, "- Clock divisor: %d", cam->clockdiv)
  2108. if (cam->monochrome)
  2109. DBG(3, "- Image sensor used as monochrome")
  2110. else
  2111. DBG(3, "- Image sensor not used as monochrome")
  2112. }
  2113. /*--------------------------------------------------------------------------
  2114. If the video post-processing module is not loaded, some parameters
  2115. must be overridden.
  2116. --------------------------------------------------------------------------*/
  2117. static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
  2118. {
  2119. if (!w9968cf_vpp) {
  2120. if (cam->decompression == 1) {
  2121. cam->decompression = 2;
  2122. DBG(2, "Video post-processing module not found: "
  2123. "'decompression' parameter forced to 2")
  2124. }
  2125. if (cam->upscaling) {
  2126. cam->upscaling = 0;
  2127. DBG(2, "Video post-processing module not found: "
  2128. "'upscaling' parameter forced to 0")
  2129. }
  2130. if (cam->picture.palette != VIDEO_PALETTE_UYVY) {
  2131. cam->force_palette = 0;
  2132. DBG(2, "Video post-processing module not found: "
  2133. "'force_palette' parameter forced to 0")
  2134. }
  2135. cam->picture.palette = VIDEO_PALETTE_UYVY;
  2136. cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
  2137. }
  2138. }
  2139. /*--------------------------------------------------------------------------
  2140. Release the resources used by the driver.
  2141. This function is called on disconnect
  2142. (or on close if deallocation has been deferred)
  2143. --------------------------------------------------------------------------*/
  2144. static void w9968cf_release_resources(struct w9968cf_device* cam)
  2145. {
  2146. mutex_lock(&w9968cf_devlist_mutex);
  2147. DBG(2, "V4L device deregistered: %s",
  2148. video_device_node_name(cam->v4ldev))
  2149. video_unregister_device(cam->v4ldev);
  2150. list_del(&cam->v4llist);
  2151. i2c_del_adapter(&cam->i2c_adapter);
  2152. w9968cf_deallocate_memory(cam);
  2153. kfree(cam->control_buffer);
  2154. kfree(cam->data_buffer);
  2155. v4l2_device_unregister(&cam->v4l2_dev);
  2156. mutex_unlock(&w9968cf_devlist_mutex);
  2157. }
  2158. /****************************************************************************
  2159. * Video4Linux interface *
  2160. ****************************************************************************/
  2161. static int w9968cf_open(struct file *filp)
  2162. {
  2163. struct w9968cf_device* cam;
  2164. int err;
  2165. /* This the only safe way to prevent race conditions with disconnect */
  2166. if (!down_read_trylock(&w9968cf_disconnect))
  2167. return -EAGAIN;
  2168. cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
  2169. mutex_lock(&cam->dev_mutex);
  2170. if (cam->sensor == CC_UNKNOWN) {
  2171. DBG(2, "No supported image sensor has been detected by the "
  2172. "'ovcamchip' module for the %s (%s). Make sure "
  2173. "it is loaded *before* (re)connecting the camera.",
  2174. symbolic(camlist, cam->id),
  2175. video_device_node_name(cam->v4ldev))
  2176. mutex_unlock(&cam->dev_mutex);
  2177. up_read(&w9968cf_disconnect);
  2178. return -ENODEV;
  2179. }
  2180. if (cam->users) {
  2181. DBG(2, "%s (%s) has been already occupied by '%s'",
  2182. symbolic(camlist, cam->id),
  2183. video_device_node_name(cam->v4ldev), cam->command)
  2184. if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
  2185. mutex_unlock(&cam->dev_mutex);
  2186. up_read(&w9968cf_disconnect);
  2187. return -EWOULDBLOCK;
  2188. }
  2189. mutex_unlock(&cam->dev_mutex);
  2190. err = wait_event_interruptible_exclusive(cam->open,
  2191. cam->disconnected ||
  2192. !cam->users);
  2193. if (err) {
  2194. up_read(&w9968cf_disconnect);
  2195. return err;
  2196. }
  2197. if (cam->disconnected) {
  2198. up_read(&w9968cf_disconnect);
  2199. return -ENODEV;
  2200. }
  2201. mutex_lock(&cam->dev_mutex);
  2202. }
  2203. DBG(5, "Opening '%s', %s ...",
  2204. symbolic(camlist, cam->id), video_device_node_name(cam->v4ldev))
  2205. cam->streaming = 0;
  2206. cam->misconfigured = 0;
  2207. w9968cf_adjust_configuration(cam);
  2208. if ((err = w9968cf_allocate_memory(cam)))
  2209. goto deallocate_memory;
  2210. if ((err = w9968cf_init_chip(cam)))
  2211. goto deallocate_memory;
  2212. if ((err = w9968cf_start_transfer(cam)))
  2213. goto deallocate_memory;
  2214. filp->private_data = cam;
  2215. cam->users++;
  2216. strcpy(cam->command, current->comm);
  2217. init_waitqueue_head(&cam->wait_queue);
  2218. DBG(5, "Video device is open")
  2219. mutex_unlock(&cam->dev_mutex);
  2220. up_read(&w9968cf_disconnect);
  2221. return 0;
  2222. deallocate_memory:
  2223. w9968cf_deallocate_memory(cam);
  2224. DBG(2, "Failed to open the video device")
  2225. mutex_unlock(&cam->dev_mutex);
  2226. up_read(&w9968cf_disconnect);
  2227. return err;
  2228. }
  2229. static int w9968cf_release(struct file *filp)
  2230. {
  2231. struct w9968cf_device* cam;
  2232. cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
  2233. mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
  2234. w9968cf_stop_transfer(cam);
  2235. if (cam->disconnected) {
  2236. w9968cf_release_resources(cam);
  2237. mutex_unlock(&cam->dev_mutex);
  2238. kfree(cam);
  2239. return 0;
  2240. }
  2241. cam->users--;
  2242. w9968cf_deallocate_memory(cam);
  2243. wake_up_interruptible_nr(&cam->open, 1);
  2244. DBG(5, "Video device closed")
  2245. mutex_unlock(&cam->dev_mutex);
  2246. return 0;
  2247. }
  2248. static ssize_t
  2249. w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
  2250. {
  2251. struct w9968cf_device* cam;
  2252. struct w9968cf_frame_t* fr;
  2253. int err = 0;
  2254. cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
  2255. if (filp->f_flags & O_NONBLOCK)
  2256. return -EWOULDBLOCK;
  2257. if (mutex_lock_interruptible(&cam->fileop_mutex))
  2258. return -ERESTARTSYS;
  2259. if (cam->disconnected) {
  2260. DBG(2, "Device not present")
  2261. mutex_unlock(&cam->fileop_mutex);
  2262. return -ENODEV;
  2263. }
  2264. if (cam->misconfigured) {
  2265. DBG(2, "The camera is misconfigured. Close and open it again.")
  2266. mutex_unlock(&cam->fileop_mutex);
  2267. return -EIO;
  2268. }
  2269. if (!cam->frame[0].queued)
  2270. w9968cf_push_frame(cam, 0);
  2271. if (!cam->frame[1].queued)
  2272. w9968cf_push_frame(cam, 1);
  2273. err = wait_event_interruptible(cam->wait_queue,
  2274. cam->frame[0].status == F_READY ||
  2275. cam->frame[1].status == F_READY ||
  2276. cam->disconnected);
  2277. if (err) {
  2278. mutex_unlock(&cam->fileop_mutex);
  2279. return err;
  2280. }
  2281. if (cam->disconnected) {
  2282. mutex_unlock(&cam->fileop_mutex);
  2283. return -ENODEV;
  2284. }
  2285. fr = (cam->frame[0].status == F_READY) ? &cam->frame[0]:&cam->frame[1];
  2286. if (w9968cf_vpp)
  2287. w9968cf_postprocess_frame(cam, fr);
  2288. if (count > fr->length)
  2289. count = fr->length;
  2290. if (copy_to_user(buf, fr->buffer, count)) {
  2291. fr->status = F_UNUSED;
  2292. mutex_unlock(&cam->fileop_mutex);
  2293. return -EFAULT;
  2294. }
  2295. *f_pos += count;
  2296. fr->status = F_UNUSED;
  2297. DBG(5, "%zu bytes read", count)
  2298. mutex_unlock(&cam->fileop_mutex);
  2299. return count;
  2300. }
  2301. static int w9968cf_mmap(struct file* filp, struct vm_area_struct *vma)
  2302. {
  2303. struct w9968cf_device* cam = (struct w9968cf_device*)
  2304. video_get_drvdata(video_devdata(filp));
  2305. unsigned long vsize = vma->vm_end - vma->vm_start,
  2306. psize = cam->nbuffers * cam->frame[0].size,
  2307. start = vma->vm_start,
  2308. pos = (unsigned long)cam->frame[0].buffer,
  2309. page;
  2310. if (cam->disconnected) {
  2311. DBG(2, "Device not present")
  2312. return -ENODEV;
  2313. }
  2314. if (cam->misconfigured) {
  2315. DBG(2, "The camera is misconfigured. Close and open it again")
  2316. return -EIO;
  2317. }
  2318. PDBGG("mmapping %lu bytes...", vsize)
  2319. if (vsize > psize - (vma->vm_pgoff << PAGE_SHIFT))
  2320. return -EINVAL;
  2321. while (vsize > 0) {
  2322. page = vmalloc_to_pfn((void *)pos);
  2323. if (remap_pfn_range(vma, start, page + vma->vm_pgoff,
  2324. PAGE_SIZE, vma->vm_page_prot))
  2325. return -EAGAIN;
  2326. start += PAGE_SIZE;
  2327. pos += PAGE_SIZE;
  2328. vsize -= PAGE_SIZE;
  2329. }
  2330. DBG(5, "mmap method successfully called")
  2331. return 0;
  2332. }
  2333. static long
  2334. w9968cf_ioctl(struct file *filp,
  2335. unsigned int cmd, unsigned long arg)
  2336. {
  2337. struct w9968cf_device* cam;
  2338. long err;
  2339. cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
  2340. if (mutex_lock_interruptible(&cam->fileop_mutex))
  2341. return -ERESTARTSYS;
  2342. if (cam->disconnected) {
  2343. DBG(2, "Device not present")
  2344. mutex_unlock(&cam->fileop_mutex);
  2345. return -ENODEV;
  2346. }
  2347. if (cam->misconfigured) {
  2348. DBG(2, "The camera is misconfigured. Close and open it again.")
  2349. mutex_unlock(&cam->fileop_mutex);
  2350. return -EIO;
  2351. }
  2352. err = w9968cf_v4l_ioctl(filp, cmd, (void __user *)arg);
  2353. mutex_unlock(&cam->fileop_mutex);
  2354. return err;
  2355. }
  2356. static long w9968cf_v4l_ioctl(struct file *filp,
  2357. unsigned int cmd, void __user *arg)
  2358. {
  2359. struct w9968cf_device* cam;
  2360. const char* v4l1_ioctls[] = {
  2361. "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER",
  2362. "GPICT", "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF",
  2363. "SFBUF", "KEY", "GFREQ", "SFREQ", "GAUDIO", "SAUDIO",
  2364. "SYNC", "MCAPTURE", "GMBUF", "GUNIT", "GCAPTURE", "SCAPTURE",
  2365. "SPLAYMODE", "SWRITEMODE", "GPLAYINFO", "SMICROCODE",
  2366. "GVBIFMT", "SVBIFMT"
  2367. };
  2368. #define V4L1_IOCTL(cmd) \
  2369. ((_IOC_NR((cmd)) < ARRAY_SIZE(v4l1_ioctls)) ? \
  2370. v4l1_ioctls[_IOC_NR((cmd))] : "?")
  2371. cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
  2372. switch (cmd) {
  2373. case VIDIOCGCAP: /* get video capability */
  2374. {
  2375. struct video_capability cap = {
  2376. .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
  2377. .channels = 1,
  2378. .audios = 0,
  2379. .minwidth = cam->minwidth,
  2380. .minheight = cam->minheight,
  2381. };
  2382. sprintf(cap.name, "W996[87]CF USB Camera");
  2383. cap.maxwidth = (cam->upscaling && w9968cf_vpp)
  2384. ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
  2385. : cam->maxwidth;
  2386. cap.maxheight = (cam->upscaling && w9968cf_vpp)
  2387. ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
  2388. : cam->maxheight;
  2389. if (copy_to_user(arg, &cap, sizeof(cap)))
  2390. return -EFAULT;
  2391. DBG(5, "VIDIOCGCAP successfully called")
  2392. return 0;
  2393. }
  2394. case VIDIOCGCHAN: /* get video channel informations */
  2395. {
  2396. struct video_channel chan;
  2397. if (copy_from_user(&chan, arg, sizeof(chan)))
  2398. return -EFAULT;
  2399. if (chan.channel != 0)
  2400. return -EINVAL;
  2401. strcpy(chan.name, "Camera");
  2402. chan.tuners = 0;
  2403. chan.flags = 0;
  2404. chan.type = VIDEO_TYPE_CAMERA;
  2405. chan.norm = VIDEO_MODE_AUTO;
  2406. if (copy_to_user(arg, &chan, sizeof(chan)))
  2407. return -EFAULT;
  2408. DBG(5, "VIDIOCGCHAN successfully called")
  2409. return 0;
  2410. }
  2411. case VIDIOCSCHAN: /* set active channel */
  2412. {
  2413. struct video_channel chan;
  2414. if (copy_from_user(&chan, arg, sizeof(chan)))
  2415. return -EFAULT;
  2416. if (chan.channel != 0)
  2417. return -EINVAL;
  2418. DBG(5, "VIDIOCSCHAN successfully called")
  2419. return 0;
  2420. }
  2421. case VIDIOCGPICT: /* get image properties of the picture */
  2422. {
  2423. if (w9968cf_sensor_get_picture(cam))
  2424. return -EIO;
  2425. if (copy_to_user(arg, &cam->picture, sizeof(cam->picture)))
  2426. return -EFAULT;
  2427. DBG(5, "VIDIOCGPICT successfully called")
  2428. return 0;
  2429. }
  2430. case VIDIOCSPICT: /* change picture settings */
  2431. {
  2432. struct video_picture pict;
  2433. int err = 0;
  2434. if (copy_from_user(&pict, arg, sizeof(pict)))
  2435. return -EFAULT;
  2436. if ( (cam->force_palette || !w9968cf_vpp)
  2437. && pict.palette != cam->picture.palette ) {
  2438. DBG(4, "Palette %s rejected: only %s is allowed",
  2439. symbolic(v4l1_plist, pict.palette),
  2440. symbolic(v4l1_plist, cam->picture.palette))
  2441. return -EINVAL;
  2442. }
  2443. if (!w9968cf_valid_palette(pict.palette)) {
  2444. DBG(4, "Palette %s not supported. VIDIOCSPICT failed",
  2445. symbolic(v4l1_plist, pict.palette))
  2446. return -EINVAL;
  2447. }
  2448. if (!cam->force_palette) {
  2449. if (cam->decompression == 0) {
  2450. if (w9968cf_need_decompression(pict.palette)) {
  2451. DBG(4, "Decompression disabled: palette %s is not "
  2452. "allowed. VIDIOCSPICT failed",
  2453. symbolic(v4l1_plist, pict.palette))
  2454. return -EINVAL;
  2455. }
  2456. } else if (cam->decompression == 1) {
  2457. if (!w9968cf_need_decompression(pict.palette)) {
  2458. DBG(4, "Decompression forced: palette %s is not "
  2459. "allowed. VIDIOCSPICT failed",
  2460. symbolic(v4l1_plist, pict.palette))
  2461. return -EINVAL;
  2462. }
  2463. }
  2464. }
  2465. if (pict.depth != w9968cf_valid_depth(pict.palette)) {
  2466. DBG(4, "Requested depth %u bpp is not valid for %s "
  2467. "palette: ignored and changed to %u bpp",
  2468. pict.depth, symbolic(v4l1_plist, pict.palette),
  2469. w9968cf_valid_depth(pict.palette))
  2470. pict.depth = w9968cf_valid_depth(pict.palette);
  2471. }
  2472. if (pict.palette != cam->picture.palette) {
  2473. if(*cam->requested_frame
  2474. || cam->frame_current->queued) {
  2475. err = wait_event_interruptible
  2476. ( cam->wait_queue,
  2477. cam->disconnected ||
  2478. (!*cam->requested_frame &&
  2479. !cam->frame_current->queued) );
  2480. if (err)
  2481. return err;
  2482. if (cam->disconnected)
  2483. return -ENODEV;
  2484. }
  2485. if (w9968cf_stop_transfer(cam))
  2486. goto ioctl_fail;
  2487. if (w9968cf_set_picture(cam, pict))
  2488. goto ioctl_fail;
  2489. if (w9968cf_start_transfer(cam))
  2490. goto ioctl_fail;
  2491. } else if (w9968cf_sensor_update_picture(cam, pict))
  2492. return -EIO;
  2493. DBG(5, "VIDIOCSPICT successfully called")
  2494. return 0;
  2495. }
  2496. case VIDIOCSWIN: /* set capture area */
  2497. {
  2498. struct video_window win;
  2499. int err = 0;
  2500. if (copy_from_user(&win, arg, sizeof(win)))
  2501. return -EFAULT;
  2502. DBG(6, "VIDIOCSWIN called: clipcount=%d, flags=%u, "
  2503. "x=%u, y=%u, %ux%u", win.clipcount, win.flags,
  2504. win.x, win.y, win.width, win.height)
  2505. if (win.clipcount != 0 || win.flags != 0)
  2506. return -EINVAL;
  2507. if ((err = w9968cf_adjust_window_size(cam, &win.width,
  2508. &win.height))) {
  2509. DBG(4, "Resolution not supported (%ux%u). "
  2510. "VIDIOCSWIN failed", win.width, win.height)
  2511. return err;
  2512. }
  2513. if (win.x != cam->window.x ||
  2514. win.y != cam->window.y ||
  2515. win.width != cam->window.width ||
  2516. win.height != cam->window.height) {
  2517. if(*cam->requested_frame
  2518. || cam->frame_current->queued) {
  2519. err = wait_event_interruptible
  2520. ( cam->wait_queue,
  2521. cam->disconnected ||
  2522. (!*cam->requested_frame &&
  2523. !cam->frame_current->queued) );
  2524. if (err)
  2525. return err;
  2526. if (cam->disconnected)
  2527. return -ENODEV;
  2528. }
  2529. if (w9968cf_stop_transfer(cam))
  2530. goto ioctl_fail;
  2531. /* This _must_ be called before set_window() */
  2532. if (w9968cf_set_picture(cam, cam->picture))
  2533. goto ioctl_fail;
  2534. if (w9968cf_set_window(cam, win))
  2535. goto ioctl_fail;
  2536. if (w9968cf_start_transfer(cam))
  2537. goto ioctl_fail;
  2538. }
  2539. DBG(5, "VIDIOCSWIN successfully called. ")
  2540. return 0;
  2541. }
  2542. case VIDIOCGWIN: /* get current window properties */
  2543. {
  2544. if (copy_to_user(arg,&cam->window,sizeof(struct video_window)))
  2545. return -EFAULT;
  2546. DBG(5, "VIDIOCGWIN successfully called")
  2547. return 0;
  2548. }
  2549. case VIDIOCGMBUF: /* request for memory (mapped) buffer */
  2550. {
  2551. struct video_mbuf mbuf;
  2552. u8 i;
  2553. mbuf.size = cam->nbuffers * cam->frame[0].size;
  2554. mbuf.frames = cam->nbuffers;
  2555. for (i = 0; i < cam->nbuffers; i++)
  2556. mbuf.offsets[i] = (unsigned long)cam->frame[i].buffer -
  2557. (unsigned long)cam->frame[0].buffer;
  2558. if (copy_to_user(arg, &mbuf, sizeof(mbuf)))
  2559. return -EFAULT;
  2560. DBG(5, "VIDIOCGMBUF successfully called")
  2561. return 0;
  2562. }
  2563. case VIDIOCMCAPTURE: /* start the capture to a frame */
  2564. {
  2565. struct video_mmap mmap;
  2566. struct w9968cf_frame_t* fr;
  2567. u32 w, h;
  2568. int err = 0;
  2569. if (copy_from_user(&mmap, arg, sizeof(mmap)))
  2570. return -EFAULT;
  2571. DBG(6, "VIDIOCMCAPTURE called: frame #%u, format=%s, %dx%d",
  2572. mmap.frame, symbolic(v4l1_plist, mmap.format),
  2573. mmap.width, mmap.height)
  2574. if (mmap.frame >= cam->nbuffers) {
  2575. DBG(4, "Invalid frame number (%u). "
  2576. "VIDIOCMCAPTURE failed", mmap.frame)
  2577. return -EINVAL;
  2578. }
  2579. if (mmap.format!=cam->picture.palette &&
  2580. (cam->force_palette || !w9968cf_vpp)) {
  2581. DBG(4, "Palette %s rejected: only %s is allowed",
  2582. symbolic(v4l1_plist, mmap.format),
  2583. symbolic(v4l1_plist, cam->picture.palette))
  2584. return -EINVAL;
  2585. }
  2586. if (!w9968cf_valid_palette(mmap.format)) {
  2587. DBG(4, "Palette %s not supported. "
  2588. "VIDIOCMCAPTURE failed",
  2589. symbolic(v4l1_plist, mmap.format))
  2590. return -EINVAL;
  2591. }
  2592. if (!cam->force_palette) {
  2593. if (cam->decompression == 0) {
  2594. if (w9968cf_need_decompression(mmap.format)) {
  2595. DBG(4, "Decompression disabled: palette %s is not "
  2596. "allowed. VIDIOCSPICT failed",
  2597. symbolic(v4l1_plist, mmap.format))
  2598. return -EINVAL;
  2599. }
  2600. } else if (cam->decompression == 1) {
  2601. if (!w9968cf_need_decompression(mmap.format)) {
  2602. DBG(4, "Decompression forced: palette %s is not "
  2603. "allowed. VIDIOCSPICT failed",
  2604. symbolic(v4l1_plist, mmap.format))
  2605. return -EINVAL;
  2606. }
  2607. }
  2608. }
  2609. w = mmap.width; h = mmap.height;
  2610. err = w9968cf_adjust_window_size(cam, &w, &h);
  2611. mmap.width = w; mmap.height = h;
  2612. if (err) {
  2613. DBG(4, "Resolution not supported (%dx%d). "
  2614. "VIDIOCMCAPTURE failed",
  2615. mmap.width, mmap.height)
  2616. return err;
  2617. }
  2618. fr = &cam->frame[mmap.frame];
  2619. if (mmap.width != cam->window.width ||
  2620. mmap.height != cam->window.height ||
  2621. mmap.format != cam->picture.palette) {
  2622. struct video_window win;
  2623. struct video_picture pict;
  2624. if(*cam->requested_frame
  2625. || cam->frame_current->queued) {
  2626. DBG(6, "VIDIOCMCAPTURE. Change settings for "
  2627. "frame #%u: %dx%d, format %s. Wait...",
  2628. mmap.frame, mmap.width, mmap.height,
  2629. symbolic(v4l1_plist, mmap.format))
  2630. err = wait_event_interruptible
  2631. ( cam->wait_queue,
  2632. cam->disconnected ||
  2633. (!*cam->requested_frame &&
  2634. !cam->frame_current->queued) );
  2635. if (err)
  2636. return err;
  2637. if (cam->disconnected)
  2638. return -ENODEV;
  2639. }
  2640. memcpy(&win, &cam->window, sizeof(win));
  2641. memcpy(&pict, &cam->picture, sizeof(pict));
  2642. win.width = mmap.width;
  2643. win.height = mmap.height;
  2644. pict.palette = mmap.format;
  2645. if (w9968cf_stop_transfer(cam))
  2646. goto ioctl_fail;
  2647. /* This before set_window */
  2648. if (w9968cf_set_picture(cam, pict))
  2649. goto ioctl_fail;
  2650. if (w9968cf_set_window(cam, win))
  2651. goto ioctl_fail;
  2652. if (w9968cf_start_transfer(cam))
  2653. goto ioctl_fail;
  2654. } else if (fr->queued) {
  2655. DBG(6, "Wait until frame #%u is free", mmap.frame)
  2656. err = wait_event_interruptible(cam->wait_queue,
  2657. cam->disconnected ||
  2658. (!fr->queued));
  2659. if (err)
  2660. return err;
  2661. if (cam->disconnected)
  2662. return -ENODEV;
  2663. }
  2664. w9968cf_push_frame(cam, mmap.frame);
  2665. DBG(5, "VIDIOCMCAPTURE(%u): successfully called", mmap.frame)
  2666. return 0;
  2667. }
  2668. case VIDIOCSYNC: /* wait until the capture of a frame is finished */
  2669. {
  2670. unsigned int f_num;
  2671. struct w9968cf_frame_t* fr;
  2672. int err = 0;
  2673. if (copy_from_user(&f_num, arg, sizeof(f_num)))
  2674. return -EFAULT;
  2675. if (f_num >= cam->nbuffers) {
  2676. DBG(4, "Invalid frame number (%u). "
  2677. "VIDIOCMCAPTURE failed", f_num)
  2678. return -EINVAL;
  2679. }
  2680. DBG(6, "VIDIOCSYNC called for frame #%u", f_num)
  2681. fr = &cam->frame[f_num];
  2682. switch (fr->status) {
  2683. case F_UNUSED:
  2684. if (!fr->queued) {
  2685. DBG(4, "VIDIOSYNC: Frame #%u not requested!",
  2686. f_num)
  2687. return -EFAULT;
  2688. }
  2689. case F_ERROR:
  2690. case F_GRABBING:
  2691. err = wait_event_interruptible(cam->wait_queue,
  2692. (fr->status == F_READY)
  2693. || cam->disconnected);
  2694. if (err)
  2695. return err;
  2696. if (cam->disconnected)
  2697. return -ENODEV;
  2698. break;
  2699. case F_READY:
  2700. break;
  2701. }
  2702. if (w9968cf_vpp)
  2703. w9968cf_postprocess_frame(cam, fr);
  2704. fr->status = F_UNUSED;
  2705. DBG(5, "VIDIOCSYNC(%u) successfully called", f_num)
  2706. return 0;
  2707. }
  2708. case VIDIOCGUNIT:/* report the unit numbers of the associated devices*/
  2709. {
  2710. struct video_unit unit = {
  2711. .video = cam->v4ldev->minor,
  2712. .vbi = VIDEO_NO_UNIT,
  2713. .radio = VIDEO_NO_UNIT,
  2714. .audio = VIDEO_NO_UNIT,
  2715. .teletext = VIDEO_NO_UNIT,
  2716. };
  2717. if (copy_to_user(arg, &unit, sizeof(unit)))
  2718. return -EFAULT;
  2719. DBG(5, "VIDIOCGUNIT successfully called")
  2720. return 0;
  2721. }
  2722. case VIDIOCKEY:
  2723. return 0;
  2724. case VIDIOCGFBUF:
  2725. {
  2726. if (clear_user(arg, sizeof(struct video_buffer)))
  2727. return -EFAULT;
  2728. DBG(5, "VIDIOCGFBUF successfully called")
  2729. return 0;
  2730. }
  2731. case VIDIOCGTUNER:
  2732. {
  2733. struct video_tuner tuner;
  2734. if (copy_from_user(&tuner, arg, sizeof(tuner)))
  2735. return -EFAULT;
  2736. if (tuner.tuner != 0)
  2737. return -EINVAL;
  2738. strcpy(tuner.name, "no_tuner");
  2739. tuner.rangelow = 0;
  2740. tuner.rangehigh = 0;
  2741. tuner.flags = VIDEO_TUNER_NORM;
  2742. tuner.mode = VIDEO_MODE_AUTO;
  2743. tuner.signal = 0xffff;
  2744. if (copy_to_user(arg, &tuner, sizeof(tuner)))
  2745. return -EFAULT;
  2746. DBG(5, "VIDIOCGTUNER successfully called")
  2747. return 0;
  2748. }
  2749. case VIDIOCSTUNER:
  2750. {
  2751. struct video_tuner tuner;
  2752. if (copy_from_user(&tuner, arg, sizeof(tuner)))
  2753. return -EFAULT;
  2754. if (tuner.tuner != 0)
  2755. return -EINVAL;
  2756. if (tuner.mode != VIDEO_MODE_AUTO)
  2757. return -EINVAL;
  2758. DBG(5, "VIDIOCSTUNER successfully called")
  2759. return 0;
  2760. }
  2761. case VIDIOCSFBUF:
  2762. case VIDIOCCAPTURE:
  2763. case VIDIOCGFREQ:
  2764. case VIDIOCSFREQ:
  2765. case VIDIOCGAUDIO:
  2766. case VIDIOCSAUDIO:
  2767. case VIDIOCSPLAYMODE:
  2768. case VIDIOCSWRITEMODE:
  2769. case VIDIOCGPLAYINFO:
  2770. case VIDIOCSMICROCODE:
  2771. case VIDIOCGVBIFMT:
  2772. case VIDIOCSVBIFMT:
  2773. DBG(4, "Unsupported V4L1 IOCtl: VIDIOC%s "
  2774. "(type 0x%01X, "
  2775. "n. 0x%01X, "
  2776. "dir. 0x%01X, "
  2777. "size 0x%02X)",
  2778. V4L1_IOCTL(cmd),
  2779. _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
  2780. return -EINVAL;
  2781. default:
  2782. DBG(4, "Invalid V4L1 IOCtl: VIDIOC%s "
  2783. "type 0x%01X, "
  2784. "n. 0x%01X, "
  2785. "dir. 0x%01X, "
  2786. "size 0x%02X",
  2787. V4L1_IOCTL(cmd),
  2788. _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
  2789. return -ENOIOCTLCMD;
  2790. } /* end of switch */
  2791. ioctl_fail:
  2792. cam->misconfigured = 1;
  2793. DBG(1, "VIDIOC%s failed because of hardware problems. "
  2794. "To use the camera, close and open it again.", V4L1_IOCTL(cmd))
  2795. return -EFAULT;
  2796. }
  2797. static const struct v4l2_file_operations w9968cf_fops = {
  2798. .owner = THIS_MODULE,
  2799. .open = w9968cf_open,
  2800. .release = w9968cf_release,
  2801. .read = w9968cf_read,
  2802. .ioctl = w9968cf_ioctl,
  2803. .mmap = w9968cf_mmap,
  2804. };
  2805. /****************************************************************************
  2806. * USB probe and V4L registration, disconnect and id_table[] definition *
  2807. ****************************************************************************/
  2808. static int
  2809. w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
  2810. {
  2811. struct usb_device *udev = interface_to_usbdev(intf);
  2812. struct w9968cf_device* cam;
  2813. int err = 0;
  2814. enum w9968cf_model_id mod_id;
  2815. struct list_head* ptr;
  2816. u8 sc = 0; /* number of simultaneous cameras */
  2817. static unsigned short dev_nr; /* 0 - we are handling device number n */
  2818. static unsigned short addrs[] = {
  2819. OV7xx0_SID,
  2820. OV6xx0_SID,
  2821. I2C_CLIENT_END
  2822. };
  2823. if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[0].idVendor &&
  2824. le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[0].idProduct)
  2825. mod_id = W9968CF_MOD_CLVBWGP; /* see camlist[] table */
  2826. else if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[1].idVendor &&
  2827. le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[1].idProduct)
  2828. mod_id = W9968CF_MOD_GENERIC; /* see camlist[] table */
  2829. else
  2830. return -ENODEV;
  2831. cam = (struct w9968cf_device*)
  2832. kzalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
  2833. if (!cam)
  2834. return -ENOMEM;
  2835. err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
  2836. if (err)
  2837. goto fail0;
  2838. mutex_init(&cam->dev_mutex);
  2839. mutex_lock(&cam->dev_mutex);
  2840. cam->usbdev = udev;
  2841. DBG(2, "%s detected", symbolic(camlist, mod_id))
  2842. if (simcams > W9968CF_MAX_DEVICES)
  2843. simcams = W9968CF_SIMCAMS;
  2844. /* How many cameras are connected ? */
  2845. mutex_lock(&w9968cf_devlist_mutex);
  2846. list_for_each(ptr, &w9968cf_dev_list)
  2847. sc++;
  2848. mutex_unlock(&w9968cf_devlist_mutex);
  2849. if (sc >= simcams) {
  2850. DBG(2, "Device rejected: too many connected cameras "
  2851. "(max. %u)", simcams)
  2852. err = -EPERM;
  2853. goto fail;
  2854. }
  2855. /* Allocate 2 bytes of memory for camera control USB transfers */
  2856. if (!(cam->control_buffer = kzalloc(2, GFP_KERNEL))) {
  2857. DBG(1,"Couldn't allocate memory for camera control transfers")
  2858. err = -ENOMEM;
  2859. goto fail;
  2860. }
  2861. /* Allocate 8 bytes of memory for USB data transfers to the FSB */
  2862. if (!(cam->data_buffer = kzalloc(8, GFP_KERNEL))) {
  2863. DBG(1, "Couldn't allocate memory for data "
  2864. "transfers to the FSB")
  2865. err = -ENOMEM;
  2866. goto fail;
  2867. }
  2868. /* Register the V4L device */
  2869. cam->v4ldev = video_device_alloc();
  2870. if (!cam->v4ldev) {
  2871. DBG(1, "Could not allocate memory for a V4L structure")
  2872. err = -ENOMEM;
  2873. goto fail;
  2874. }
  2875. strcpy(cam->v4ldev->name, symbolic(camlist, mod_id));
  2876. cam->v4ldev->fops = &w9968cf_fops;
  2877. cam->v4ldev->release = video_device_release;
  2878. video_set_drvdata(cam->v4ldev, cam);
  2879. cam->v4ldev->v4l2_dev = &cam->v4l2_dev;
  2880. err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
  2881. video_nr[dev_nr]);
  2882. if (err) {
  2883. DBG(1, "V4L device registration failed")
  2884. if (err == -ENFILE && video_nr[dev_nr] == -1)
  2885. DBG(2, "Couldn't find a free /dev/videoX node")
  2886. video_nr[dev_nr] = -1;
  2887. dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
  2888. goto fail;
  2889. }
  2890. DBG(2, "V4L device registered as %s",
  2891. video_device_node_name(cam->v4ldev))
  2892. /* Set some basic constants */
  2893. w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
  2894. /* Add a new entry into the list of V4L registered devices */
  2895. mutex_lock(&w9968cf_devlist_mutex);
  2896. list_add(&cam->v4llist, &w9968cf_dev_list);
  2897. mutex_unlock(&w9968cf_devlist_mutex);
  2898. dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
  2899. w9968cf_turn_on_led(cam);
  2900. w9968cf_i2c_init(cam);
  2901. cam->sensor_sd = v4l2_i2c_new_subdev(&cam->v4l2_dev,
  2902. &cam->i2c_adapter,
  2903. "ovcamchip", "ovcamchip", 0, addrs);
  2904. usb_set_intfdata(intf, cam);
  2905. mutex_unlock(&cam->dev_mutex);
  2906. err = w9968cf_sensor_init(cam);
  2907. return 0;
  2908. fail: /* Free unused memory */
  2909. kfree(cam->control_buffer);
  2910. kfree(cam->data_buffer);
  2911. if (cam->v4ldev)
  2912. video_device_release(cam->v4ldev);
  2913. mutex_unlock(&cam->dev_mutex);
  2914. v4l2_device_unregister(&cam->v4l2_dev);
  2915. fail0:
  2916. kfree(cam);
  2917. return err;
  2918. }
  2919. static void w9968cf_usb_disconnect(struct usb_interface* intf)
  2920. {
  2921. struct w9968cf_device* cam =
  2922. (struct w9968cf_device*)usb_get_intfdata(intf);
  2923. if (cam) {
  2924. down_write(&w9968cf_disconnect);
  2925. /* Prevent concurrent accesses to data */
  2926. mutex_lock(&cam->dev_mutex);
  2927. cam->disconnected = 1;
  2928. DBG(2, "Disconnecting %s...", symbolic(camlist, cam->id));
  2929. v4l2_device_disconnect(&cam->v4l2_dev);
  2930. wake_up_interruptible_all(&cam->open);
  2931. if (cam->users) {
  2932. DBG(2, "The device is open (%s)! "
  2933. "Process name: %s. Deregistration and memory "
  2934. "deallocation are deferred on close.",
  2935. video_device_node_name(cam->v4ldev), cam->command)
  2936. cam->misconfigured = 1;
  2937. w9968cf_stop_transfer(cam);
  2938. wake_up_interruptible(&cam->wait_queue);
  2939. } else
  2940. w9968cf_release_resources(cam);
  2941. mutex_unlock(&cam->dev_mutex);
  2942. up_write(&w9968cf_disconnect);
  2943. if (!cam->users) {
  2944. kfree(cam);
  2945. }
  2946. }
  2947. }
  2948. static struct usb_driver w9968cf_usb_driver = {
  2949. .name = "w9968cf",
  2950. .id_table = winbond_id_table,
  2951. .probe = w9968cf_usb_probe,
  2952. .disconnect = w9968cf_usb_disconnect,
  2953. };
  2954. /****************************************************************************
  2955. * Module init, exit and intermodule communication *
  2956. ****************************************************************************/
  2957. static int __init w9968cf_module_init(void)
  2958. {
  2959. int err;
  2960. KDBG(2, W9968CF_MODULE_NAME" "W9968CF_MODULE_VERSION)
  2961. KDBG(3, W9968CF_MODULE_AUTHOR)
  2962. if ((err = usb_register(&w9968cf_usb_driver)))
  2963. return err;
  2964. return 0;
  2965. }
  2966. static void __exit w9968cf_module_exit(void)
  2967. {
  2968. /* w9968cf_usb_disconnect() will be called */
  2969. usb_deregister(&w9968cf_usb_driver);
  2970. KDBG(2, W9968CF_MODULE_NAME" deregistered")
  2971. }
  2972. module_init(w9968cf_module_init);
  2973. module_exit(w9968cf_module_exit);