w9968cf.c 106 KB

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