zoran_driver.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652
  1. /*
  2. * Zoran zr36057/zr36067 PCI controller driver, for the
  3. * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
  4. * Media Labs LML33/LML33R10.
  5. *
  6. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  7. *
  8. * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
  9. *
  10. * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
  11. *
  12. * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
  13. *
  14. * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
  15. *
  16. * Based on
  17. *
  18. * Miro DC10 driver
  19. * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  20. *
  21. * Iomega Buz driver version 1.0
  22. * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
  23. *
  24. * buz.0.0.3
  25. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  26. *
  27. * bttv - Bt848 frame grabber driver
  28. * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
  29. * & Marcus Metzler (mocm@thp.uni-koeln.de)
  30. *
  31. *
  32. * This program is free software; you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. * the Free Software Foundation; either version 2 of the License, or
  35. * (at your option) any later version.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License
  43. * along with this program; if not, write to the Free Software
  44. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  45. */
  46. #include <linux/version.h>
  47. #include <linux/init.h>
  48. #include <linux/module.h>
  49. #include <linux/delay.h>
  50. #include <linux/slab.h>
  51. #include <linux/pci.h>
  52. #include <linux/vmalloc.h>
  53. #include <linux/wait.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/i2c.h>
  56. #include <linux/i2c-algo-bit.h>
  57. #include <linux/spinlock.h>
  58. #define MAP_NR(x) virt_to_page(x)
  59. #define ZORAN_VID_TYPE ( \
  60. VID_TYPE_CAPTURE | \
  61. VID_TYPE_OVERLAY | \
  62. VID_TYPE_CLIPPING | \
  63. VID_TYPE_FRAMERAM | \
  64. VID_TYPE_SCALES | \
  65. VID_TYPE_MJPEG_DECODER | \
  66. VID_TYPE_MJPEG_ENCODER \
  67. )
  68. #include <linux/videodev.h>
  69. #include <media/v4l2-common.h>
  70. #include "videocodec.h"
  71. #include <asm/byteorder.h>
  72. #include <asm/io.h>
  73. #include <asm/uaccess.h>
  74. #include <linux/proc_fs.h>
  75. #include <linux/video_decoder.h>
  76. #include <linux/video_encoder.h>
  77. #include <linux/mutex.h>
  78. #include "zoran.h"
  79. #include "zoran_device.h"
  80. #include "zoran_card.h"
  81. /* we declare some card type definitions here, they mean
  82. * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
  83. #define ZORAN_V4L2_VID_FLAGS ( \
  84. V4L2_CAP_STREAMING |\
  85. V4L2_CAP_VIDEO_CAPTURE |\
  86. V4L2_CAP_VIDEO_OUTPUT |\
  87. V4L2_CAP_VIDEO_OVERLAY \
  88. )
  89. #if defined(CONFIG_VIDEO_V4L1_COMPAT)
  90. #define ZFMT(pal, fcc, cs) \
  91. .palette = (pal), .fourcc = (fcc), .colorspace = (cs)
  92. #else
  93. #define ZFMT(pal, fcc, cs) \
  94. .fourcc = (fcc), .colorspace = (cs)
  95. #endif
  96. const struct zoran_format zoran_formats[] = {
  97. {
  98. .name = "15-bit RGB LE",
  99. ZFMT(VIDEO_PALETTE_RGB555,
  100. V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
  101. .depth = 15,
  102. .flags = ZORAN_FORMAT_CAPTURE |
  103. ZORAN_FORMAT_OVERLAY,
  104. .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
  105. ZR36057_VFESPFR_LittleEndian,
  106. }, {
  107. .name = "15-bit RGB BE",
  108. ZFMT(-1,
  109. V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
  110. .depth = 15,
  111. .flags = ZORAN_FORMAT_CAPTURE |
  112. ZORAN_FORMAT_OVERLAY,
  113. .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
  114. }, {
  115. .name = "16-bit RGB LE",
  116. ZFMT(VIDEO_PALETTE_RGB565,
  117. V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
  118. .depth = 16,
  119. .flags = ZORAN_FORMAT_CAPTURE |
  120. ZORAN_FORMAT_OVERLAY,
  121. .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
  122. ZR36057_VFESPFR_LittleEndian,
  123. }, {
  124. .name = "16-bit RGB BE",
  125. ZFMT(-1,
  126. V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
  127. .depth = 16,
  128. .flags = ZORAN_FORMAT_CAPTURE |
  129. ZORAN_FORMAT_OVERLAY,
  130. .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
  131. }, {
  132. .name = "24-bit RGB",
  133. ZFMT(VIDEO_PALETTE_RGB24,
  134. V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
  135. .depth = 24,
  136. .flags = ZORAN_FORMAT_CAPTURE |
  137. ZORAN_FORMAT_OVERLAY,
  138. .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
  139. }, {
  140. .name = "32-bit RGB LE",
  141. ZFMT(VIDEO_PALETTE_RGB32,
  142. V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
  143. .depth = 32,
  144. .flags = ZORAN_FORMAT_CAPTURE |
  145. ZORAN_FORMAT_OVERLAY,
  146. .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
  147. }, {
  148. .name = "32-bit RGB BE",
  149. ZFMT(-1,
  150. V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
  151. .depth = 32,
  152. .flags = ZORAN_FORMAT_CAPTURE |
  153. ZORAN_FORMAT_OVERLAY,
  154. .vfespfr = ZR36057_VFESPFR_RGB888,
  155. }, {
  156. .name = "4:2:2, packed, YUYV",
  157. ZFMT(VIDEO_PALETTE_YUV422,
  158. V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
  159. .depth = 16,
  160. .flags = ZORAN_FORMAT_CAPTURE |
  161. ZORAN_FORMAT_OVERLAY,
  162. .vfespfr = ZR36057_VFESPFR_YUV422,
  163. }, {
  164. .name = "4:2:2, packed, UYVY",
  165. ZFMT(VIDEO_PALETTE_UYVY,
  166. V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
  167. .depth = 16,
  168. .flags = ZORAN_FORMAT_CAPTURE |
  169. ZORAN_FORMAT_OVERLAY,
  170. .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
  171. }, {
  172. .name = "Hardware-encoded Motion-JPEG",
  173. ZFMT(-1,
  174. V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
  175. .depth = 0,
  176. .flags = ZORAN_FORMAT_CAPTURE |
  177. ZORAN_FORMAT_PLAYBACK |
  178. ZORAN_FORMAT_COMPRESSED,
  179. }
  180. };
  181. #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
  182. // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
  183. extern int v4l_nbufs;
  184. extern int v4l_bufsize;
  185. extern int jpg_nbufs;
  186. extern int jpg_bufsize;
  187. extern int pass_through;
  188. static int lock_norm; /* 0 = default 1 = Don't change TV standard (norm) */
  189. module_param(lock_norm, int, 0644);
  190. MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
  191. /* small helper function for calculating buffersizes for v4l2
  192. * we calculate the nearest higher power-of-two, which
  193. * will be the recommended buffersize */
  194. static __u32
  195. zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
  196. {
  197. __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
  198. __u32 num = (1024 * 512) / (div);
  199. __u32 result = 2;
  200. num--;
  201. while (num) {
  202. num >>= 1;
  203. result <<= 1;
  204. }
  205. if (result > jpg_bufsize)
  206. return jpg_bufsize;
  207. if (result < 8192)
  208. return 8192;
  209. return result;
  210. }
  211. /* forward references */
  212. static void v4l_fbuffer_free(struct file *file);
  213. static void jpg_fbuffer_free(struct file *file);
  214. /*
  215. * Allocate the V4L grab buffers
  216. *
  217. * These have to be pysically contiguous.
  218. * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
  219. * else we try to allocate them with bigphysarea_alloc_pages
  220. * if the bigphysarea patch is present in the kernel,
  221. * else we try to use high memory (if the user has bootet
  222. * Linux with the necessary memory left over).
  223. */
  224. static unsigned long
  225. get_high_mem (unsigned long size)
  226. {
  227. /*
  228. * Check if there is usable memory at the end of Linux memory
  229. * of at least size. Return the physical address of this memory,
  230. * return 0 on failure.
  231. *
  232. * The idea is from Alexandro Rubini's book "Linux device drivers".
  233. * The driver from him which is downloadable from O'Reilly's
  234. * web site misses the "virt_to_phys(high_memory)" part
  235. * (and therefore doesn't work at all - at least with 2.2.x kernels).
  236. *
  237. * It should be unnecessary to mention that THIS IS DANGEROUS,
  238. * if more than one driver at a time has the idea to use this memory!!!!
  239. */
  240. volatile unsigned char __iomem *mem;
  241. unsigned char c;
  242. unsigned long hi_mem_ph;
  243. unsigned long i;
  244. /* Map the high memory to user space */
  245. hi_mem_ph = virt_to_phys(high_memory);
  246. mem = ioremap(hi_mem_ph, size);
  247. if (!mem) {
  248. dprintk(1,
  249. KERN_ERR "%s: get_high_mem() - ioremap failed\n",
  250. ZORAN_NAME);
  251. return 0;
  252. }
  253. for (i = 0; i < size; i++) {
  254. /* Check if it is memory */
  255. c = i & 0xff;
  256. writeb(c, mem + i);
  257. if (readb(mem + i) != c)
  258. break;
  259. c = 255 - c;
  260. writeb(c, mem + i);
  261. if (readb(mem + i) != c)
  262. break;
  263. writeb(0, mem + i); /* zero out memory */
  264. /* give the kernel air to breath */
  265. if ((i & 0x3ffff) == 0x3ffff)
  266. schedule();
  267. }
  268. iounmap(mem);
  269. if (i != size) {
  270. dprintk(1,
  271. KERN_ERR
  272. "%s: get_high_mem() - requested %lu, avail %lu\n",
  273. ZORAN_NAME, size, i);
  274. return 0;
  275. }
  276. return hi_mem_ph;
  277. }
  278. static int
  279. v4l_fbuffer_alloc (struct file *file)
  280. {
  281. struct zoran_fh *fh = file->private_data;
  282. struct zoran *zr = fh->zr;
  283. int i, off;
  284. unsigned char *mem;
  285. unsigned long pmem = 0;
  286. /* we might have old buffers lying around... */
  287. if (fh->v4l_buffers.ready_to_be_freed) {
  288. v4l_fbuffer_free(file);
  289. }
  290. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  291. if (fh->v4l_buffers.buffer[i].fbuffer)
  292. dprintk(2,
  293. KERN_WARNING
  294. "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
  295. ZR_DEVNAME(zr), i);
  296. //udelay(20);
  297. if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
  298. /* Use kmalloc */
  299. mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
  300. if (!mem) {
  301. dprintk(1,
  302. KERN_ERR
  303. "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
  304. ZR_DEVNAME(zr), i);
  305. v4l_fbuffer_free(file);
  306. return -ENOBUFS;
  307. }
  308. fh->v4l_buffers.buffer[i].fbuffer = mem;
  309. fh->v4l_buffers.buffer[i].fbuffer_phys =
  310. virt_to_phys(mem);
  311. fh->v4l_buffers.buffer[i].fbuffer_bus =
  312. virt_to_bus(mem);
  313. for (off = 0; off < fh->v4l_buffers.buffer_size;
  314. off += PAGE_SIZE)
  315. SetPageReserved(MAP_NR(mem + off));
  316. dprintk(4,
  317. KERN_INFO
  318. "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
  319. ZR_DEVNAME(zr), i, (unsigned long) mem,
  320. virt_to_bus(mem));
  321. } else {
  322. /* Use high memory which has been left at boot time */
  323. /* Ok., Ok. this is an evil hack - we make
  324. * the assumption that physical addresses are
  325. * the same as bus addresses (true at least
  326. * for Intel processors). The whole method of
  327. * obtaining and using this memory is not very
  328. * nice - but I hope it saves some poor users
  329. * from kernel hacking, which might have even
  330. * more evil results */
  331. if (i == 0) {
  332. int size =
  333. fh->v4l_buffers.num_buffers *
  334. fh->v4l_buffers.buffer_size;
  335. pmem = get_high_mem(size);
  336. if (pmem == 0) {
  337. dprintk(1,
  338. KERN_ERR
  339. "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
  340. ZR_DEVNAME(zr), size >> 10);
  341. return -ENOBUFS;
  342. }
  343. fh->v4l_buffers.buffer[0].fbuffer = NULL;
  344. fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
  345. fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
  346. dprintk(4,
  347. KERN_INFO
  348. "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
  349. ZR_DEVNAME(zr), size >> 10);
  350. } else {
  351. fh->v4l_buffers.buffer[i].fbuffer = NULL;
  352. fh->v4l_buffers.buffer[i].fbuffer_phys =
  353. pmem + i * fh->v4l_buffers.buffer_size;
  354. fh->v4l_buffers.buffer[i].fbuffer_bus =
  355. pmem + i * fh->v4l_buffers.buffer_size;
  356. }
  357. }
  358. }
  359. fh->v4l_buffers.allocated = 1;
  360. return 0;
  361. }
  362. /* free the V4L grab buffers */
  363. static void
  364. v4l_fbuffer_free (struct file *file)
  365. {
  366. struct zoran_fh *fh = file->private_data;
  367. struct zoran *zr = fh->zr;
  368. int i, off;
  369. unsigned char *mem;
  370. dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
  371. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  372. if (!fh->v4l_buffers.buffer[i].fbuffer)
  373. continue;
  374. if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
  375. mem = fh->v4l_buffers.buffer[i].fbuffer;
  376. for (off = 0; off < fh->v4l_buffers.buffer_size;
  377. off += PAGE_SIZE)
  378. ClearPageReserved(MAP_NR(mem + off));
  379. kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
  380. }
  381. fh->v4l_buffers.buffer[i].fbuffer = NULL;
  382. }
  383. fh->v4l_buffers.allocated = 0;
  384. fh->v4l_buffers.ready_to_be_freed = 0;
  385. }
  386. /*
  387. * Allocate the MJPEG grab buffers.
  388. *
  389. * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
  390. * kmalloc is used to request a physically contiguous area,
  391. * else we allocate the memory in framgents with get_zeroed_page.
  392. *
  393. * If a Natoma chipset is present and this is a revision 1 zr36057,
  394. * each MJPEG buffer needs to be physically contiguous.
  395. * (RJ: This statement is from Dave Perks' original driver,
  396. * I could never check it because I have a zr36067)
  397. * The driver cares about this because it reduces the buffer
  398. * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
  399. *
  400. * RJ: The contents grab buffers needs never be accessed in the driver.
  401. * Therefore there is no need to allocate them with vmalloc in order
  402. * to get a contiguous virtual memory space.
  403. * I don't understand why many other drivers first allocate them with
  404. * vmalloc (which uses internally also get_zeroed_page, but delivers you
  405. * virtual addresses) and then again have to make a lot of efforts
  406. * to get the physical address.
  407. *
  408. * Ben Capper:
  409. * On big-endian architectures (such as ppc) some extra steps
  410. * are needed. When reading and writing to the stat_com array
  411. * and fragment buffers, the device expects to see little-
  412. * endian values. The use of cpu_to_le32() and le32_to_cpu()
  413. * in this function (and one or two others in zoran_device.c)
  414. * ensure that these values are always stored in little-endian
  415. * form, regardless of architecture. The zr36057 does Very Bad
  416. * Things on big endian architectures if the stat_com array
  417. * and fragment buffers are not little-endian.
  418. */
  419. static int
  420. jpg_fbuffer_alloc (struct file *file)
  421. {
  422. struct zoran_fh *fh = file->private_data;
  423. struct zoran *zr = fh->zr;
  424. int i, j, off;
  425. unsigned long mem;
  426. /* we might have old buffers lying around */
  427. if (fh->jpg_buffers.ready_to_be_freed) {
  428. jpg_fbuffer_free(file);
  429. }
  430. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  431. if (fh->jpg_buffers.buffer[i].frag_tab)
  432. dprintk(2,
  433. KERN_WARNING
  434. "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
  435. ZR_DEVNAME(zr), i);
  436. /* Allocate fragment table for this buffer */
  437. mem = get_zeroed_page(GFP_KERNEL);
  438. if (mem == 0) {
  439. dprintk(1,
  440. KERN_ERR
  441. "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
  442. ZR_DEVNAME(zr), i);
  443. jpg_fbuffer_free(file);
  444. return -ENOBUFS;
  445. }
  446. fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
  447. fh->jpg_buffers.buffer[i].frag_tab_bus =
  448. virt_to_bus((void *) mem);
  449. //if (alloc_contig) {
  450. if (fh->jpg_buffers.need_contiguous) {
  451. mem =
  452. (unsigned long) kmalloc(fh->jpg_buffers.
  453. buffer_size,
  454. GFP_KERNEL);
  455. if (mem == 0) {
  456. dprintk(1,
  457. KERN_ERR
  458. "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
  459. ZR_DEVNAME(zr), i);
  460. jpg_fbuffer_free(file);
  461. return -ENOBUFS;
  462. }
  463. fh->jpg_buffers.buffer[i].frag_tab[0] =
  464. cpu_to_le32(virt_to_bus((void *) mem));
  465. fh->jpg_buffers.buffer[i].frag_tab[1] =
  466. cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
  467. for (off = 0; off < fh->jpg_buffers.buffer_size;
  468. off += PAGE_SIZE)
  469. SetPageReserved(MAP_NR(mem + off));
  470. } else {
  471. /* jpg_bufsize is allreay page aligned */
  472. for (j = 0;
  473. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  474. j++) {
  475. mem = get_zeroed_page(GFP_KERNEL);
  476. if (mem == 0) {
  477. dprintk(1,
  478. KERN_ERR
  479. "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
  480. ZR_DEVNAME(zr), i);
  481. jpg_fbuffer_free(file);
  482. return -ENOBUFS;
  483. }
  484. fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
  485. cpu_to_le32(virt_to_bus((void *) mem));
  486. fh->jpg_buffers.buffer[i].frag_tab[2 * j +
  487. 1] =
  488. cpu_to_le32((PAGE_SIZE / 4) << 1);
  489. SetPageReserved(MAP_NR(mem));
  490. }
  491. fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
  492. }
  493. }
  494. dprintk(4,
  495. KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
  496. ZR_DEVNAME(zr),
  497. (fh->jpg_buffers.num_buffers *
  498. fh->jpg_buffers.buffer_size) >> 10);
  499. fh->jpg_buffers.allocated = 1;
  500. return 0;
  501. }
  502. /* free the MJPEG grab buffers */
  503. static void
  504. jpg_fbuffer_free (struct file *file)
  505. {
  506. struct zoran_fh *fh = file->private_data;
  507. struct zoran *zr = fh->zr;
  508. int i, j, off;
  509. unsigned char *mem;
  510. dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
  511. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  512. if (!fh->jpg_buffers.buffer[i].frag_tab)
  513. continue;
  514. //if (alloc_contig) {
  515. if (fh->jpg_buffers.need_contiguous) {
  516. if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
  517. mem = (unsigned char *) bus_to_virt(le32_to_cpu(
  518. fh->jpg_buffers.buffer[i].frag_tab[0]));
  519. for (off = 0;
  520. off < fh->jpg_buffers.buffer_size;
  521. off += PAGE_SIZE)
  522. ClearPageReserved(MAP_NR
  523. (mem + off));
  524. kfree(mem);
  525. fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
  526. fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
  527. }
  528. } else {
  529. for (j = 0;
  530. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  531. j++) {
  532. if (!fh->jpg_buffers.buffer[i].
  533. frag_tab[2 * j])
  534. break;
  535. ClearPageReserved(MAP_NR
  536. (bus_to_virt
  537. (le32_to_cpu
  538. (fh->jpg_buffers.
  539. buffer[i].frag_tab[2 *
  540. j]))));
  541. free_page((unsigned long)
  542. bus_to_virt
  543. (le32_to_cpu
  544. (fh->jpg_buffers.
  545. buffer[i].
  546. frag_tab[2 * j])));
  547. fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
  548. 0;
  549. fh->jpg_buffers.buffer[i].frag_tab[2 * j +
  550. 1] = 0;
  551. }
  552. }
  553. free_page((unsigned long) fh->jpg_buffers.buffer[i].
  554. frag_tab);
  555. fh->jpg_buffers.buffer[i].frag_tab = NULL;
  556. }
  557. fh->jpg_buffers.allocated = 0;
  558. fh->jpg_buffers.ready_to_be_freed = 0;
  559. }
  560. /*
  561. * V4L Buffer grabbing
  562. */
  563. static int
  564. zoran_v4l_set_format (struct file *file,
  565. int width,
  566. int height,
  567. const struct zoran_format *format)
  568. {
  569. struct zoran_fh *fh = file->private_data;
  570. struct zoran *zr = fh->zr;
  571. int bpp;
  572. /* Check size and format of the grab wanted */
  573. if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
  574. height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
  575. dprintk(1,
  576. KERN_ERR
  577. "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
  578. ZR_DEVNAME(zr), width, height);
  579. return -EINVAL;
  580. }
  581. bpp = (format->depth + 7) / 8;
  582. /* Check against available buffer size */
  583. if (height * width * bpp > fh->v4l_buffers.buffer_size) {
  584. dprintk(1,
  585. KERN_ERR
  586. "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
  587. ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
  588. return -EINVAL;
  589. }
  590. /* The video front end needs 4-byte alinged line sizes */
  591. if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
  592. dprintk(1,
  593. KERN_ERR
  594. "%s: v4l_set_format() - wrong frame alingment\n",
  595. ZR_DEVNAME(zr));
  596. return -EINVAL;
  597. }
  598. fh->v4l_settings.width = width;
  599. fh->v4l_settings.height = height;
  600. fh->v4l_settings.format = format;
  601. fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
  602. return 0;
  603. }
  604. static int
  605. zoran_v4l_queue_frame (struct file *file,
  606. int num)
  607. {
  608. struct zoran_fh *fh = file->private_data;
  609. struct zoran *zr = fh->zr;
  610. unsigned long flags;
  611. int res = 0;
  612. if (!fh->v4l_buffers.allocated) {
  613. dprintk(1,
  614. KERN_ERR
  615. "%s: v4l_queue_frame() - buffers not yet allocated\n",
  616. ZR_DEVNAME(zr));
  617. res = -ENOMEM;
  618. }
  619. /* No grabbing outside the buffer range! */
  620. if (num >= fh->v4l_buffers.num_buffers || num < 0) {
  621. dprintk(1,
  622. KERN_ERR
  623. "%s: v4l_queue_frame() - buffer %d is out of range\n",
  624. ZR_DEVNAME(zr), num);
  625. res = -EINVAL;
  626. }
  627. spin_lock_irqsave(&zr->spinlock, flags);
  628. if (fh->v4l_buffers.active == ZORAN_FREE) {
  629. if (zr->v4l_buffers.active == ZORAN_FREE) {
  630. zr->v4l_buffers = fh->v4l_buffers;
  631. fh->v4l_buffers.active = ZORAN_ACTIVE;
  632. } else {
  633. dprintk(1,
  634. KERN_ERR
  635. "%s: v4l_queue_frame() - another session is already capturing\n",
  636. ZR_DEVNAME(zr));
  637. res = -EBUSY;
  638. }
  639. }
  640. /* make sure a grab isn't going on currently with this buffer */
  641. if (!res) {
  642. switch (zr->v4l_buffers.buffer[num].state) {
  643. default:
  644. case BUZ_STATE_PEND:
  645. if (zr->v4l_buffers.active == ZORAN_FREE) {
  646. fh->v4l_buffers.active = ZORAN_FREE;
  647. zr->v4l_buffers.allocated = 0;
  648. }
  649. res = -EBUSY; /* what are you doing? */
  650. break;
  651. case BUZ_STATE_DONE:
  652. dprintk(2,
  653. KERN_WARNING
  654. "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
  655. ZR_DEVNAME(zr), num);
  656. case BUZ_STATE_USER:
  657. /* since there is at least one unused buffer there's room for at least
  658. * one more pend[] entry */
  659. zr->v4l_pend[zr->v4l_pend_head++ &
  660. V4L_MASK_FRAME] = num;
  661. zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
  662. zr->v4l_buffers.buffer[num].bs.length =
  663. fh->v4l_settings.bytesperline *
  664. zr->v4l_settings.height;
  665. fh->v4l_buffers.buffer[num] =
  666. zr->v4l_buffers.buffer[num];
  667. break;
  668. }
  669. }
  670. spin_unlock_irqrestore(&zr->spinlock, flags);
  671. if (!res && zr->v4l_buffers.active == ZORAN_FREE)
  672. zr->v4l_buffers.active = fh->v4l_buffers.active;
  673. return res;
  674. }
  675. static int
  676. v4l_grab (struct file *file,
  677. struct video_mmap *mp)
  678. {
  679. struct zoran_fh *fh = file->private_data;
  680. struct zoran *zr = fh->zr;
  681. int res = 0, i;
  682. for (i = 0; i < NUM_FORMATS; i++) {
  683. if (zoran_formats[i].palette == mp->format &&
  684. zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
  685. !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
  686. break;
  687. }
  688. if (i == NUM_FORMATS || zoran_formats[i].depth == 0) {
  689. dprintk(1,
  690. KERN_ERR
  691. "%s: v4l_grab() - wrong bytes-per-pixel format\n",
  692. ZR_DEVNAME(zr));
  693. return -EINVAL;
  694. }
  695. /*
  696. * To minimize the time spent in the IRQ routine, we avoid setting up
  697. * the video front end there.
  698. * If this grab has different parameters from a running streaming capture
  699. * we stop the streaming capture and start it over again.
  700. */
  701. if (zr->v4l_memgrab_active &&
  702. (zr->v4l_settings.width != mp->width ||
  703. zr->v4l_settings.height != mp->height ||
  704. zr->v4l_settings.format->palette != mp->format)) {
  705. res = wait_grab_pending(zr);
  706. if (res)
  707. return res;
  708. }
  709. if ((res = zoran_v4l_set_format(file,
  710. mp->width,
  711. mp->height,
  712. &zoran_formats[i])))
  713. return res;
  714. zr->v4l_settings = fh->v4l_settings;
  715. /* queue the frame in the pending queue */
  716. if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
  717. fh->v4l_buffers.active = ZORAN_FREE;
  718. return res;
  719. }
  720. /* put the 36057 into frame grabbing mode */
  721. if (!res && !zr->v4l_memgrab_active)
  722. zr36057_set_memgrab(zr, 1);
  723. //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
  724. return res;
  725. }
  726. /*
  727. * Sync on a V4L buffer
  728. */
  729. static int
  730. v4l_sync (struct file *file,
  731. int frame)
  732. {
  733. struct zoran_fh *fh = file->private_data;
  734. struct zoran *zr = fh->zr;
  735. unsigned long flags;
  736. if (fh->v4l_buffers.active == ZORAN_FREE) {
  737. dprintk(1,
  738. KERN_ERR
  739. "%s: v4l_sync() - no grab active for this session\n",
  740. ZR_DEVNAME(zr));
  741. return -EINVAL;
  742. }
  743. /* check passed-in frame number */
  744. if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
  745. dprintk(1,
  746. KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
  747. ZR_DEVNAME(zr), frame);
  748. return -EINVAL;
  749. }
  750. /* Check if is buffer was queued at all */
  751. if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
  752. dprintk(1,
  753. KERN_ERR
  754. "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
  755. ZR_DEVNAME(zr));
  756. return -EPROTO;
  757. }
  758. /* wait on this buffer to get ready */
  759. if (!wait_event_interruptible_timeout(zr->v4l_capq,
  760. (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
  761. 10*HZ))
  762. return -ETIME;
  763. if (signal_pending(current))
  764. return -ERESTARTSYS;
  765. /* buffer should now be in BUZ_STATE_DONE */
  766. if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
  767. dprintk(2,
  768. KERN_ERR "%s: v4l_sync() - internal state error\n",
  769. ZR_DEVNAME(zr));
  770. zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
  771. fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
  772. spin_lock_irqsave(&zr->spinlock, flags);
  773. /* Check if streaming capture has finished */
  774. if (zr->v4l_pend_tail == zr->v4l_pend_head) {
  775. zr36057_set_memgrab(zr, 0);
  776. if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
  777. fh->v4l_buffers.active = zr->v4l_buffers.active =
  778. ZORAN_FREE;
  779. zr->v4l_buffers.allocated = 0;
  780. }
  781. }
  782. spin_unlock_irqrestore(&zr->spinlock, flags);
  783. return 0;
  784. }
  785. /*
  786. * Queue a MJPEG buffer for capture/playback
  787. */
  788. static int
  789. zoran_jpg_queue_frame (struct file *file,
  790. int num,
  791. enum zoran_codec_mode mode)
  792. {
  793. struct zoran_fh *fh = file->private_data;
  794. struct zoran *zr = fh->zr;
  795. unsigned long flags;
  796. int res = 0;
  797. /* Check if buffers are allocated */
  798. if (!fh->jpg_buffers.allocated) {
  799. dprintk(1,
  800. KERN_ERR
  801. "%s: jpg_queue_frame() - buffers not yet allocated\n",
  802. ZR_DEVNAME(zr));
  803. return -ENOMEM;
  804. }
  805. /* No grabbing outside the buffer range! */
  806. if (num >= fh->jpg_buffers.num_buffers || num < 0) {
  807. dprintk(1,
  808. KERN_ERR
  809. "%s: jpg_queue_frame() - buffer %d out of range\n",
  810. ZR_DEVNAME(zr), num);
  811. return -EINVAL;
  812. }
  813. /* what is the codec mode right now? */
  814. if (zr->codec_mode == BUZ_MODE_IDLE) {
  815. zr->jpg_settings = fh->jpg_settings;
  816. } else if (zr->codec_mode != mode) {
  817. /* wrong codec mode active - invalid */
  818. dprintk(1,
  819. KERN_ERR
  820. "%s: jpg_queue_frame() - codec in wrong mode\n",
  821. ZR_DEVNAME(zr));
  822. return -EINVAL;
  823. }
  824. if (fh->jpg_buffers.active == ZORAN_FREE) {
  825. if (zr->jpg_buffers.active == ZORAN_FREE) {
  826. zr->jpg_buffers = fh->jpg_buffers;
  827. fh->jpg_buffers.active = ZORAN_ACTIVE;
  828. } else {
  829. dprintk(1,
  830. KERN_ERR
  831. "%s: jpg_queue_frame() - another session is already capturing\n",
  832. ZR_DEVNAME(zr));
  833. res = -EBUSY;
  834. }
  835. }
  836. if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
  837. /* Ok load up the jpeg codec */
  838. zr36057_enable_jpg(zr, mode);
  839. }
  840. spin_lock_irqsave(&zr->spinlock, flags);
  841. if (!res) {
  842. switch (zr->jpg_buffers.buffer[num].state) {
  843. case BUZ_STATE_DONE:
  844. dprintk(2,
  845. KERN_WARNING
  846. "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
  847. ZR_DEVNAME(zr));
  848. case BUZ_STATE_USER:
  849. /* since there is at least one unused buffer there's room for at
  850. *least one more pend[] entry */
  851. zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
  852. num;
  853. zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
  854. fh->jpg_buffers.buffer[num] =
  855. zr->jpg_buffers.buffer[num];
  856. zoran_feed_stat_com(zr);
  857. break;
  858. default:
  859. case BUZ_STATE_DMA:
  860. case BUZ_STATE_PEND:
  861. if (zr->jpg_buffers.active == ZORAN_FREE) {
  862. fh->jpg_buffers.active = ZORAN_FREE;
  863. zr->jpg_buffers.allocated = 0;
  864. }
  865. res = -EBUSY; /* what are you doing? */
  866. break;
  867. }
  868. }
  869. spin_unlock_irqrestore(&zr->spinlock, flags);
  870. if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
  871. zr->jpg_buffers.active = fh->jpg_buffers.active;
  872. }
  873. return res;
  874. }
  875. static int
  876. jpg_qbuf (struct file *file,
  877. int frame,
  878. enum zoran_codec_mode mode)
  879. {
  880. struct zoran_fh *fh = file->private_data;
  881. struct zoran *zr = fh->zr;
  882. int res = 0;
  883. /* Does the user want to stop streaming? */
  884. if (frame < 0) {
  885. if (zr->codec_mode == mode) {
  886. if (fh->jpg_buffers.active == ZORAN_FREE) {
  887. dprintk(1,
  888. KERN_ERR
  889. "%s: jpg_qbuf(-1) - session not active\n",
  890. ZR_DEVNAME(zr));
  891. return -EINVAL;
  892. }
  893. fh->jpg_buffers.active = zr->jpg_buffers.active =
  894. ZORAN_FREE;
  895. zr->jpg_buffers.allocated = 0;
  896. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  897. return 0;
  898. } else {
  899. dprintk(1,
  900. KERN_ERR
  901. "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
  902. ZR_DEVNAME(zr));
  903. return -EINVAL;
  904. }
  905. }
  906. if ((res = zoran_jpg_queue_frame(file, frame, mode)))
  907. return res;
  908. /* Start the jpeg codec when the first frame is queued */
  909. if (!res && zr->jpg_que_head == 1)
  910. jpeg_start(zr);
  911. return res;
  912. }
  913. /*
  914. * Sync on a MJPEG buffer
  915. */
  916. static int
  917. jpg_sync (struct file *file,
  918. struct zoran_sync *bs)
  919. {
  920. struct zoran_fh *fh = file->private_data;
  921. struct zoran *zr = fh->zr;
  922. unsigned long flags;
  923. int frame;
  924. if (fh->jpg_buffers.active == ZORAN_FREE) {
  925. dprintk(1,
  926. KERN_ERR
  927. "%s: jpg_sync() - capture is not currently active\n",
  928. ZR_DEVNAME(zr));
  929. return -EINVAL;
  930. }
  931. if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
  932. zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
  933. dprintk(1,
  934. KERN_ERR
  935. "%s: jpg_sync() - codec not in streaming mode\n",
  936. ZR_DEVNAME(zr));
  937. return -EINVAL;
  938. }
  939. if (!wait_event_interruptible_timeout(zr->jpg_capq,
  940. (zr->jpg_que_tail != zr->jpg_dma_tail ||
  941. zr->jpg_dma_tail == zr->jpg_dma_head),
  942. 10*HZ)) {
  943. int isr;
  944. btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
  945. udelay(1);
  946. zr->codec->control(zr->codec, CODEC_G_STATUS,
  947. sizeof(isr), &isr);
  948. dprintk(1,
  949. KERN_ERR
  950. "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
  951. ZR_DEVNAME(zr), isr);
  952. return -ETIME;
  953. }
  954. if (signal_pending(current))
  955. return -ERESTARTSYS;
  956. spin_lock_irqsave(&zr->spinlock, flags);
  957. if (zr->jpg_dma_tail != zr->jpg_dma_head)
  958. frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
  959. else
  960. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  961. /* buffer should now be in BUZ_STATE_DONE */
  962. if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
  963. dprintk(2,
  964. KERN_ERR "%s: jpg_sync() - internal state error\n",
  965. ZR_DEVNAME(zr));
  966. *bs = zr->jpg_buffers.buffer[frame].bs;
  967. bs->frame = frame;
  968. zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
  969. fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
  970. spin_unlock_irqrestore(&zr->spinlock, flags);
  971. return 0;
  972. }
  973. static void
  974. zoran_open_init_session (struct file *file)
  975. {
  976. int i;
  977. struct zoran_fh *fh = file->private_data;
  978. struct zoran *zr = fh->zr;
  979. /* Per default, map the V4L Buffers */
  980. fh->map_mode = ZORAN_MAP_MODE_RAW;
  981. /* take over the card's current settings */
  982. fh->overlay_settings = zr->overlay_settings;
  983. fh->overlay_settings.is_set = 0;
  984. fh->overlay_settings.format = zr->overlay_settings.format;
  985. fh->overlay_active = ZORAN_FREE;
  986. /* v4l settings */
  987. fh->v4l_settings = zr->v4l_settings;
  988. /* v4l_buffers */
  989. memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
  990. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  991. fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  992. fh->v4l_buffers.buffer[i].bs.frame = i;
  993. }
  994. fh->v4l_buffers.allocated = 0;
  995. fh->v4l_buffers.ready_to_be_freed = 0;
  996. fh->v4l_buffers.active = ZORAN_FREE;
  997. fh->v4l_buffers.buffer_size = v4l_bufsize;
  998. fh->v4l_buffers.num_buffers = v4l_nbufs;
  999. /* jpg settings */
  1000. fh->jpg_settings = zr->jpg_settings;
  1001. /* jpg_buffers */
  1002. memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
  1003. for (i = 0; i < BUZ_MAX_FRAME; i++) {
  1004. fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  1005. fh->jpg_buffers.buffer[i].bs.frame = i;
  1006. }
  1007. fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
  1008. fh->jpg_buffers.allocated = 0;
  1009. fh->jpg_buffers.ready_to_be_freed = 0;
  1010. fh->jpg_buffers.active = ZORAN_FREE;
  1011. fh->jpg_buffers.buffer_size = jpg_bufsize;
  1012. fh->jpg_buffers.num_buffers = jpg_nbufs;
  1013. }
  1014. static void
  1015. zoran_close_end_session (struct file *file)
  1016. {
  1017. struct zoran_fh *fh = file->private_data;
  1018. struct zoran *zr = fh->zr;
  1019. /* overlay */
  1020. if (fh->overlay_active != ZORAN_FREE) {
  1021. fh->overlay_active = zr->overlay_active = ZORAN_FREE;
  1022. zr->v4l_overlay_active = 0;
  1023. if (!zr->v4l_memgrab_active)
  1024. zr36057_overlay(zr, 0);
  1025. zr->overlay_mask = NULL;
  1026. }
  1027. /* v4l capture */
  1028. if (fh->v4l_buffers.active != ZORAN_FREE) {
  1029. unsigned long flags;
  1030. spin_lock_irqsave(&zr->spinlock, flags);
  1031. zr36057_set_memgrab(zr, 0);
  1032. zr->v4l_buffers.allocated = 0;
  1033. zr->v4l_buffers.active = fh->v4l_buffers.active =
  1034. ZORAN_FREE;
  1035. spin_unlock_irqrestore(&zr->spinlock, flags);
  1036. }
  1037. /* v4l buffers */
  1038. if (fh->v4l_buffers.allocated ||
  1039. fh->v4l_buffers.ready_to_be_freed) {
  1040. v4l_fbuffer_free(file);
  1041. }
  1042. /* jpg capture */
  1043. if (fh->jpg_buffers.active != ZORAN_FREE) {
  1044. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1045. zr->jpg_buffers.allocated = 0;
  1046. zr->jpg_buffers.active = fh->jpg_buffers.active =
  1047. ZORAN_FREE;
  1048. }
  1049. /* jpg buffers */
  1050. if (fh->jpg_buffers.allocated ||
  1051. fh->jpg_buffers.ready_to_be_freed) {
  1052. jpg_fbuffer_free(file);
  1053. }
  1054. }
  1055. /*
  1056. * Open a zoran card. Right now the flags stuff is just playing
  1057. */
  1058. static int
  1059. zoran_open (struct inode *inode,
  1060. struct file *file)
  1061. {
  1062. unsigned int minor = iminor(inode);
  1063. struct zoran *zr = NULL;
  1064. struct zoran_fh *fh;
  1065. int i, res, first_open = 0, have_module_locks = 0;
  1066. /* find the device */
  1067. for (i = 0; i < zoran_num; i++) {
  1068. if (zoran[i].video_dev->minor == minor) {
  1069. zr = &zoran[i];
  1070. break;
  1071. }
  1072. }
  1073. if (!zr) {
  1074. dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
  1075. res = -ENODEV;
  1076. goto open_unlock_and_return;
  1077. }
  1078. /* see fs/device.c - the kernel already locks during open(),
  1079. * so locking ourselves only causes deadlocks */
  1080. /*mutex_lock(&zr->resource_lock);*/
  1081. if (!zr->decoder) {
  1082. dprintk(1,
  1083. KERN_ERR "%s: no TV decoder loaded for device!\n",
  1084. ZR_DEVNAME(zr));
  1085. res = -EIO;
  1086. goto open_unlock_and_return;
  1087. }
  1088. /* try to grab a module lock */
  1089. if (!try_module_get(THIS_MODULE)) {
  1090. dprintk(1,
  1091. KERN_ERR
  1092. "%s: failed to acquire my own lock! PANIC!\n",
  1093. ZR_DEVNAME(zr));
  1094. res = -ENODEV;
  1095. goto open_unlock_and_return;
  1096. }
  1097. if (!try_module_get(zr->decoder->driver->driver.owner)) {
  1098. dprintk(1,
  1099. KERN_ERR
  1100. "%s: failed to grab ownership of i2c decoder\n",
  1101. ZR_DEVNAME(zr));
  1102. res = -EIO;
  1103. module_put(THIS_MODULE);
  1104. goto open_unlock_and_return;
  1105. }
  1106. if (zr->encoder &&
  1107. !try_module_get(zr->encoder->driver->driver.owner)) {
  1108. dprintk(1,
  1109. KERN_ERR
  1110. "%s: failed to grab ownership of i2c encoder\n",
  1111. ZR_DEVNAME(zr));
  1112. res = -EIO;
  1113. module_put(zr->decoder->driver->driver.owner);
  1114. module_put(THIS_MODULE);
  1115. goto open_unlock_and_return;
  1116. }
  1117. have_module_locks = 1;
  1118. if (zr->user >= 2048) {
  1119. dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
  1120. ZR_DEVNAME(zr), zr->user);
  1121. res = -EBUSY;
  1122. goto open_unlock_and_return;
  1123. }
  1124. dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
  1125. ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user);
  1126. /* now, create the open()-specific file_ops struct */
  1127. fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
  1128. if (!fh) {
  1129. dprintk(1,
  1130. KERN_ERR
  1131. "%s: zoran_open() - allocation of zoran_fh failed\n",
  1132. ZR_DEVNAME(zr));
  1133. res = -ENOMEM;
  1134. goto open_unlock_and_return;
  1135. }
  1136. /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
  1137. * on norm-change! */
  1138. fh->overlay_mask =
  1139. kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
  1140. if (!fh->overlay_mask) {
  1141. dprintk(1,
  1142. KERN_ERR
  1143. "%s: zoran_open() - allocation of overlay_mask failed\n",
  1144. ZR_DEVNAME(zr));
  1145. kfree(fh);
  1146. res = -ENOMEM;
  1147. goto open_unlock_and_return;
  1148. }
  1149. if (zr->user++ == 0)
  1150. first_open = 1;
  1151. /*mutex_unlock(&zr->resource_lock);*/
  1152. /* default setup - TODO: look at flags */
  1153. if (first_open) { /* First device open */
  1154. zr36057_restart(zr);
  1155. zoran_open_init_params(zr);
  1156. zoran_init_hardware(zr);
  1157. btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1158. }
  1159. /* set file_ops stuff */
  1160. file->private_data = fh;
  1161. fh->zr = zr;
  1162. zoran_open_init_session(file);
  1163. return 0;
  1164. open_unlock_and_return:
  1165. /* if we grabbed locks, release them accordingly */
  1166. if (have_module_locks) {
  1167. module_put(zr->decoder->driver->driver.owner);
  1168. if (zr->encoder) {
  1169. module_put(zr->encoder->driver->driver.owner);
  1170. }
  1171. module_put(THIS_MODULE);
  1172. }
  1173. /* if there's no device found, we didn't obtain the lock either */
  1174. if (zr) {
  1175. /*mutex_unlock(&zr->resource_lock);*/
  1176. }
  1177. return res;
  1178. }
  1179. static int
  1180. zoran_close (struct inode *inode,
  1181. struct file *file)
  1182. {
  1183. struct zoran_fh *fh = file->private_data;
  1184. struct zoran *zr = fh->zr;
  1185. dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
  1186. ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user);
  1187. /* kernel locks (fs/device.c), so don't do that ourselves
  1188. * (prevents deadlocks) */
  1189. /*mutex_lock(&zr->resource_lock);*/
  1190. zoran_close_end_session(file);
  1191. if (zr->user-- == 1) { /* Last process */
  1192. /* Clean up JPEG process */
  1193. wake_up_interruptible(&zr->jpg_capq);
  1194. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1195. zr->jpg_buffers.allocated = 0;
  1196. zr->jpg_buffers.active = ZORAN_FREE;
  1197. /* disable interrupts */
  1198. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1199. if (zr36067_debug > 1)
  1200. print_interrupts(zr);
  1201. /* Overlay off */
  1202. zr->v4l_overlay_active = 0;
  1203. zr36057_overlay(zr, 0);
  1204. zr->overlay_mask = NULL;
  1205. /* capture off */
  1206. wake_up_interruptible(&zr->v4l_capq);
  1207. zr36057_set_memgrab(zr, 0);
  1208. zr->v4l_buffers.allocated = 0;
  1209. zr->v4l_buffers.active = ZORAN_FREE;
  1210. zoran_set_pci_master(zr, 0);
  1211. if (!pass_through) { /* Switch to color bar */
  1212. int zero = 0, two = 2;
  1213. decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
  1214. encoder_command(zr, ENCODER_SET_INPUT, &two);
  1215. }
  1216. }
  1217. file->private_data = NULL;
  1218. kfree(fh->overlay_mask);
  1219. kfree(fh);
  1220. /* release locks on the i2c modules */
  1221. module_put(zr->decoder->driver->driver.owner);
  1222. if (zr->encoder) {
  1223. module_put(zr->encoder->driver->driver.owner);
  1224. }
  1225. module_put(THIS_MODULE);
  1226. /*mutex_unlock(&zr->resource_lock);*/
  1227. dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
  1228. return 0;
  1229. }
  1230. static ssize_t
  1231. zoran_read (struct file *file,
  1232. char __user *data,
  1233. size_t count,
  1234. loff_t *ppos)
  1235. {
  1236. /* we simply don't support read() (yet)... */
  1237. return -EINVAL;
  1238. }
  1239. static ssize_t
  1240. zoran_write (struct file *file,
  1241. const char __user *data,
  1242. size_t count,
  1243. loff_t *ppos)
  1244. {
  1245. /* ...and the same goes for write() */
  1246. return -EINVAL;
  1247. }
  1248. static int
  1249. setup_fbuffer (struct file *file,
  1250. void *base,
  1251. const struct zoran_format *fmt,
  1252. int width,
  1253. int height,
  1254. int bytesperline)
  1255. {
  1256. struct zoran_fh *fh = file->private_data;
  1257. struct zoran *zr = fh->zr;
  1258. /* (Ronald) v4l/v4l2 guidelines */
  1259. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  1260. return -EPERM;
  1261. /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
  1262. ALi Magik (that needs very low latency while the card needs a
  1263. higher value always) */
  1264. if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
  1265. return -ENXIO;
  1266. /* we need a bytesperline value, even if not given */
  1267. if (!bytesperline)
  1268. bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
  1269. #if 0
  1270. if (zr->overlay_active) {
  1271. /* dzjee... stupid users... don't even bother to turn off
  1272. * overlay before changing the memory location...
  1273. * normally, we would return errors here. However, one of
  1274. * the tools that does this is... xawtv! and since xawtv
  1275. * is used by +/- 99% of the users, we'd rather be user-
  1276. * friendly and silently do as if nothing went wrong */
  1277. dprintk(3,
  1278. KERN_ERR
  1279. "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
  1280. ZR_DEVNAME(zr));
  1281. zr36057_overlay(zr, 0);
  1282. }
  1283. #endif
  1284. if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
  1285. dprintk(1,
  1286. KERN_ERR
  1287. "%s: setup_fbuffer() - no valid overlay format given\n",
  1288. ZR_DEVNAME(zr));
  1289. return -EINVAL;
  1290. }
  1291. if (height <= 0 || width <= 0 || bytesperline <= 0) {
  1292. dprintk(1,
  1293. KERN_ERR
  1294. "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
  1295. ZR_DEVNAME(zr), width, height, bytesperline);
  1296. return -EINVAL;
  1297. }
  1298. if (bytesperline & 3) {
  1299. dprintk(1,
  1300. KERN_ERR
  1301. "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
  1302. ZR_DEVNAME(zr), bytesperline);
  1303. return -EINVAL;
  1304. }
  1305. zr->buffer.base = (void *) ((unsigned long) base & ~3);
  1306. zr->buffer.height = height;
  1307. zr->buffer.width = width;
  1308. zr->buffer.depth = fmt->depth;
  1309. zr->overlay_settings.format = fmt;
  1310. zr->buffer.bytesperline = bytesperline;
  1311. /* The user should set new window parameters */
  1312. zr->overlay_settings.is_set = 0;
  1313. return 0;
  1314. }
  1315. static int
  1316. setup_window (struct file *file,
  1317. int x,
  1318. int y,
  1319. int width,
  1320. int height,
  1321. struct video_clip __user *clips,
  1322. int clipcount,
  1323. void __user *bitmap)
  1324. {
  1325. struct zoran_fh *fh = file->private_data;
  1326. struct zoran *zr = fh->zr;
  1327. struct video_clip *vcp = NULL;
  1328. int on, end;
  1329. if (!zr->buffer.base) {
  1330. dprintk(1,
  1331. KERN_ERR
  1332. "%s: setup_window() - frame buffer has to be set first\n",
  1333. ZR_DEVNAME(zr));
  1334. return -EINVAL;
  1335. }
  1336. if (!fh->overlay_settings.format) {
  1337. dprintk(1,
  1338. KERN_ERR
  1339. "%s: setup_window() - no overlay format set\n",
  1340. ZR_DEVNAME(zr));
  1341. return -EINVAL;
  1342. }
  1343. /*
  1344. * The video front end needs 4-byte alinged line sizes, we correct that
  1345. * silently here if necessary
  1346. */
  1347. if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
  1348. end = (x + width) & ~1; /* round down */
  1349. x = (x + 1) & ~1; /* round up */
  1350. width = end - x;
  1351. }
  1352. if (zr->buffer.depth == 24) {
  1353. end = (x + width) & ~3; /* round down */
  1354. x = (x + 3) & ~3; /* round up */
  1355. width = end - x;
  1356. }
  1357. if (width > BUZ_MAX_WIDTH)
  1358. width = BUZ_MAX_WIDTH;
  1359. if (height > BUZ_MAX_HEIGHT)
  1360. height = BUZ_MAX_HEIGHT;
  1361. /* Check for vaild parameters */
  1362. if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
  1363. width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
  1364. dprintk(1,
  1365. KERN_ERR
  1366. "%s: setup_window() - width = %d or height = %d invalid\n",
  1367. ZR_DEVNAME(zr), width, height);
  1368. return -EINVAL;
  1369. }
  1370. fh->overlay_settings.x = x;
  1371. fh->overlay_settings.y = y;
  1372. fh->overlay_settings.width = width;
  1373. fh->overlay_settings.height = height;
  1374. fh->overlay_settings.clipcount = clipcount;
  1375. /*
  1376. * If an overlay is running, we have to switch it off
  1377. * and switch it on again in order to get the new settings in effect.
  1378. *
  1379. * We also want to avoid that the overlay mask is written
  1380. * when an overlay is running.
  1381. */
  1382. on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
  1383. zr->overlay_active != ZORAN_FREE &&
  1384. fh->overlay_active != ZORAN_FREE;
  1385. if (on)
  1386. zr36057_overlay(zr, 0);
  1387. /*
  1388. * Write the overlay mask if clips are wanted.
  1389. * We prefer a bitmap.
  1390. */
  1391. if (bitmap) {
  1392. /* fake value - it just means we want clips */
  1393. fh->overlay_settings.clipcount = 1;
  1394. if (copy_from_user(fh->overlay_mask, bitmap,
  1395. (width * height + 7) / 8)) {
  1396. return -EFAULT;
  1397. }
  1398. } else if (clipcount > 0) {
  1399. /* write our own bitmap from the clips */
  1400. vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
  1401. if (vcp == NULL) {
  1402. dprintk(1,
  1403. KERN_ERR
  1404. "%s: setup_window() - Alloc of clip mask failed\n",
  1405. ZR_DEVNAME(zr));
  1406. return -ENOMEM;
  1407. }
  1408. if (copy_from_user
  1409. (vcp, clips, sizeof(struct video_clip) * clipcount)) {
  1410. vfree(vcp);
  1411. return -EFAULT;
  1412. }
  1413. write_overlay_mask(file, vcp, clipcount);
  1414. vfree(vcp);
  1415. }
  1416. fh->overlay_settings.is_set = 1;
  1417. if (fh->overlay_active != ZORAN_FREE &&
  1418. zr->overlay_active != ZORAN_FREE)
  1419. zr->overlay_settings = fh->overlay_settings;
  1420. if (on)
  1421. zr36057_overlay(zr, 1);
  1422. /* Make sure the changes come into effect */
  1423. return wait_grab_pending(zr);
  1424. }
  1425. static int
  1426. setup_overlay (struct file *file,
  1427. int on)
  1428. {
  1429. struct zoran_fh *fh = file->private_data;
  1430. struct zoran *zr = fh->zr;
  1431. /* If there is nothing to do, return immediatly */
  1432. if ((on && fh->overlay_active != ZORAN_FREE) ||
  1433. (!on && fh->overlay_active == ZORAN_FREE))
  1434. return 0;
  1435. /* check whether we're touching someone else's overlay */
  1436. if (on && zr->overlay_active != ZORAN_FREE &&
  1437. fh->overlay_active == ZORAN_FREE) {
  1438. dprintk(1,
  1439. KERN_ERR
  1440. "%s: setup_overlay() - overlay is already active for another session\n",
  1441. ZR_DEVNAME(zr));
  1442. return -EBUSY;
  1443. }
  1444. if (!on && zr->overlay_active != ZORAN_FREE &&
  1445. fh->overlay_active == ZORAN_FREE) {
  1446. dprintk(1,
  1447. KERN_ERR
  1448. "%s: setup_overlay() - you cannot cancel someone else's session\n",
  1449. ZR_DEVNAME(zr));
  1450. return -EPERM;
  1451. }
  1452. if (on == 0) {
  1453. zr->overlay_active = fh->overlay_active = ZORAN_FREE;
  1454. zr->v4l_overlay_active = 0;
  1455. /* When a grab is running, the video simply
  1456. * won't be switched on any more */
  1457. if (!zr->v4l_memgrab_active)
  1458. zr36057_overlay(zr, 0);
  1459. zr->overlay_mask = NULL;
  1460. } else {
  1461. if (!zr->buffer.base || !fh->overlay_settings.is_set) {
  1462. dprintk(1,
  1463. KERN_ERR
  1464. "%s: setup_overlay() - buffer or window not set\n",
  1465. ZR_DEVNAME(zr));
  1466. return -EINVAL;
  1467. }
  1468. if (!fh->overlay_settings.format) {
  1469. dprintk(1,
  1470. KERN_ERR
  1471. "%s: setup_overlay() - no overlay format set\n",
  1472. ZR_DEVNAME(zr));
  1473. return -EINVAL;
  1474. }
  1475. zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
  1476. zr->v4l_overlay_active = 1;
  1477. zr->overlay_mask = fh->overlay_mask;
  1478. zr->overlay_settings = fh->overlay_settings;
  1479. if (!zr->v4l_memgrab_active)
  1480. zr36057_overlay(zr, 1);
  1481. /* When a grab is running, the video will be
  1482. * switched on when grab is finished */
  1483. }
  1484. /* Make sure the changes come into effect */
  1485. return wait_grab_pending(zr);
  1486. }
  1487. /* get the status of a buffer in the clients buffer queue */
  1488. static int
  1489. zoran_v4l2_buffer_status (struct file *file,
  1490. struct v4l2_buffer *buf,
  1491. int num)
  1492. {
  1493. struct zoran_fh *fh = file->private_data;
  1494. struct zoran *zr = fh->zr;
  1495. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1496. switch (fh->map_mode) {
  1497. case ZORAN_MAP_MODE_RAW:
  1498. /* check range */
  1499. if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
  1500. !fh->v4l_buffers.allocated) {
  1501. dprintk(1,
  1502. KERN_ERR
  1503. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1504. ZR_DEVNAME(zr));
  1505. return -EINVAL;
  1506. }
  1507. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1508. buf->length = fh->v4l_buffers.buffer_size;
  1509. /* get buffer */
  1510. buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
  1511. if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1512. fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
  1513. buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
  1514. buf->flags |= V4L2_BUF_FLAG_DONE;
  1515. buf->timestamp =
  1516. fh->v4l_buffers.buffer[num].bs.timestamp;
  1517. } else {
  1518. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1519. }
  1520. if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
  1521. buf->field = V4L2_FIELD_TOP;
  1522. else
  1523. buf->field = V4L2_FIELD_INTERLACED;
  1524. break;
  1525. case ZORAN_MAP_MODE_JPG_REC:
  1526. case ZORAN_MAP_MODE_JPG_PLAY:
  1527. /* check range */
  1528. if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
  1529. !fh->jpg_buffers.allocated) {
  1530. dprintk(1,
  1531. KERN_ERR
  1532. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1533. ZR_DEVNAME(zr));
  1534. return -EINVAL;
  1535. }
  1536. buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  1537. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  1538. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1539. buf->length = fh->jpg_buffers.buffer_size;
  1540. /* these variables are only written after frame has been captured */
  1541. if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1542. fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
  1543. buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
  1544. buf->timestamp =
  1545. fh->jpg_buffers.buffer[num].bs.timestamp;
  1546. buf->bytesused =
  1547. fh->jpg_buffers.buffer[num].bs.length;
  1548. buf->flags |= V4L2_BUF_FLAG_DONE;
  1549. } else {
  1550. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1551. }
  1552. /* which fields are these? */
  1553. if (fh->jpg_settings.TmpDcm != 1)
  1554. buf->field =
  1555. fh->jpg_settings.
  1556. odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1557. else
  1558. buf->field =
  1559. fh->jpg_settings.
  1560. odd_even ? V4L2_FIELD_SEQ_TB :
  1561. V4L2_FIELD_SEQ_BT;
  1562. break;
  1563. default:
  1564. dprintk(5,
  1565. KERN_ERR
  1566. "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
  1567. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  1568. return -EINVAL;
  1569. }
  1570. buf->memory = V4L2_MEMORY_MMAP;
  1571. buf->index = num;
  1572. buf->m.offset = buf->length * num;
  1573. return 0;
  1574. }
  1575. static int
  1576. zoran_set_norm (struct zoran *zr,
  1577. int norm) /* VIDEO_MODE_* */
  1578. {
  1579. int norm_encoder, on;
  1580. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1581. zr->jpg_buffers.active != ZORAN_FREE) {
  1582. dprintk(1,
  1583. KERN_WARNING
  1584. "%s: set_norm() called while in playback/capture mode\n",
  1585. ZR_DEVNAME(zr));
  1586. return -EBUSY;
  1587. }
  1588. if (lock_norm && norm != zr->norm) {
  1589. if (lock_norm > 1) {
  1590. dprintk(1,
  1591. KERN_WARNING
  1592. "%s: set_norm() - TV standard is locked, can not switch norm\n",
  1593. ZR_DEVNAME(zr));
  1594. return -EPERM;
  1595. } else {
  1596. dprintk(1,
  1597. KERN_WARNING
  1598. "%s: set_norm() - TV standard is locked, norm was not changed\n",
  1599. ZR_DEVNAME(zr));
  1600. norm = zr->norm;
  1601. }
  1602. }
  1603. if (norm != VIDEO_MODE_AUTO &&
  1604. (norm < 0 || norm >= zr->card.norms ||
  1605. !zr->card.tvn[norm])) {
  1606. dprintk(1,
  1607. KERN_ERR "%s: set_norm() - unsupported norm %d\n",
  1608. ZR_DEVNAME(zr), norm);
  1609. return -EINVAL;
  1610. }
  1611. if (norm == VIDEO_MODE_AUTO) {
  1612. int status;
  1613. /* if we have autodetect, ... */
  1614. struct video_decoder_capability caps;
  1615. decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
  1616. if (!(caps.flags & VIDEO_DECODER_AUTO)) {
  1617. dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
  1618. ZR_DEVNAME(zr));
  1619. return -EINVAL;
  1620. }
  1621. decoder_command(zr, DECODER_SET_NORM, &norm);
  1622. /* let changes come into effect */
  1623. ssleep(2);
  1624. decoder_command(zr, DECODER_GET_STATUS, &status);
  1625. if (!(status & DECODER_STATUS_GOOD)) {
  1626. dprintk(1,
  1627. KERN_ERR
  1628. "%s: set_norm() - no norm detected\n",
  1629. ZR_DEVNAME(zr));
  1630. /* reset norm */
  1631. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  1632. return -EIO;
  1633. }
  1634. if (status & DECODER_STATUS_NTSC)
  1635. norm = VIDEO_MODE_NTSC;
  1636. else if (status & DECODER_STATUS_SECAM)
  1637. norm = VIDEO_MODE_SECAM;
  1638. else
  1639. norm = VIDEO_MODE_PAL;
  1640. }
  1641. zr->timing = zr->card.tvn[norm];
  1642. norm_encoder = norm;
  1643. /* We switch overlay off and on since a change in the
  1644. * norm needs different VFE settings */
  1645. on = zr->overlay_active && !zr->v4l_memgrab_active;
  1646. if (on)
  1647. zr36057_overlay(zr, 0);
  1648. decoder_command(zr, DECODER_SET_NORM, &norm);
  1649. encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
  1650. if (on)
  1651. zr36057_overlay(zr, 1);
  1652. /* Make sure the changes come into effect */
  1653. zr->norm = norm;
  1654. return 0;
  1655. }
  1656. static int
  1657. zoran_set_input (struct zoran *zr,
  1658. int input)
  1659. {
  1660. int realinput;
  1661. if (input == zr->input) {
  1662. return 0;
  1663. }
  1664. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1665. zr->jpg_buffers.active != ZORAN_FREE) {
  1666. dprintk(1,
  1667. KERN_WARNING
  1668. "%s: set_input() called while in playback/capture mode\n",
  1669. ZR_DEVNAME(zr));
  1670. return -EBUSY;
  1671. }
  1672. if (input < 0 || input >= zr->card.inputs) {
  1673. dprintk(1,
  1674. KERN_ERR
  1675. "%s: set_input() - unnsupported input %d\n",
  1676. ZR_DEVNAME(zr), input);
  1677. return -EINVAL;
  1678. }
  1679. realinput = zr->card.input[input].muxsel;
  1680. zr->input = input;
  1681. decoder_command(zr, DECODER_SET_INPUT, &realinput);
  1682. return 0;
  1683. }
  1684. /*
  1685. * ioctl routine
  1686. */
  1687. static int
  1688. zoran_do_ioctl (struct inode *inode,
  1689. struct file *file,
  1690. unsigned int cmd,
  1691. void *arg)
  1692. {
  1693. struct zoran_fh *fh = file->private_data;
  1694. struct zoran *zr = fh->zr;
  1695. /* CAREFUL: used in multiple places here */
  1696. struct zoran_jpg_settings settings;
  1697. /* we might have older buffers lying around... We don't want
  1698. * to wait, but we do want to try cleaning them up ASAP. So
  1699. * we try to obtain the lock and free them. If that fails, we
  1700. * don't do anything and wait for the next turn. In the end,
  1701. * zoran_close() or a new allocation will still free them...
  1702. * This is just a 'the sooner the better' extra 'feature'
  1703. *
  1704. * We don't free the buffers right on munmap() because that
  1705. * causes oopses (kfree() inside munmap() oopses for no
  1706. * apparent reason - it's also not reproduceable in any way,
  1707. * but moving the free code outside the munmap() handler fixes
  1708. * all this... If someone knows why, please explain me (Ronald)
  1709. */
  1710. if (mutex_trylock(&zr->resource_lock)) {
  1711. /* we obtained it! Let's try to free some things */
  1712. if (fh->jpg_buffers.ready_to_be_freed)
  1713. jpg_fbuffer_free(file);
  1714. if (fh->v4l_buffers.ready_to_be_freed)
  1715. v4l_fbuffer_free(file);
  1716. mutex_unlock(&zr->resource_lock);
  1717. }
  1718. switch (cmd) {
  1719. case VIDIOCGCAP:
  1720. {
  1721. struct video_capability *vcap = arg;
  1722. dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
  1723. memset(vcap, 0, sizeof(struct video_capability));
  1724. strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
  1725. vcap->type = ZORAN_VID_TYPE;
  1726. vcap->channels = zr->card.inputs;
  1727. vcap->audios = 0;
  1728. mutex_lock(&zr->resource_lock);
  1729. vcap->maxwidth = BUZ_MAX_WIDTH;
  1730. vcap->maxheight = BUZ_MAX_HEIGHT;
  1731. vcap->minwidth = BUZ_MIN_WIDTH;
  1732. vcap->minheight = BUZ_MIN_HEIGHT;
  1733. mutex_unlock(&zr->resource_lock);
  1734. return 0;
  1735. }
  1736. break;
  1737. case VIDIOCGCHAN:
  1738. {
  1739. struct video_channel *vchan = arg;
  1740. int channel = vchan->channel;
  1741. dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
  1742. ZR_DEVNAME(zr), vchan->channel);
  1743. memset(vchan, 0, sizeof(struct video_channel));
  1744. if (channel > zr->card.inputs || channel < 0) {
  1745. dprintk(1,
  1746. KERN_ERR
  1747. "%s: VIDIOCGCHAN on not existing channel %d\n",
  1748. ZR_DEVNAME(zr), channel);
  1749. return -EINVAL;
  1750. }
  1751. strcpy(vchan->name, zr->card.input[channel].name);
  1752. vchan->tuners = 0;
  1753. vchan->flags = 0;
  1754. vchan->type = VIDEO_TYPE_CAMERA;
  1755. mutex_lock(&zr->resource_lock);
  1756. vchan->norm = zr->norm;
  1757. mutex_unlock(&zr->resource_lock);
  1758. vchan->channel = channel;
  1759. return 0;
  1760. }
  1761. break;
  1762. /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
  1763. *
  1764. * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
  1765. * * ^^^^^^^
  1766. * * The famos BTTV driver has it implemented with a struct video_channel argument
  1767. * * and we follow it for compatibility reasons
  1768. * *
  1769. * * BTW: this is the only way the user can set the norm!
  1770. */
  1771. case VIDIOCSCHAN:
  1772. {
  1773. struct video_channel *vchan = arg;
  1774. int res;
  1775. dprintk(3,
  1776. KERN_DEBUG
  1777. "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
  1778. ZR_DEVNAME(zr), vchan->channel, vchan->norm);
  1779. mutex_lock(&zr->resource_lock);
  1780. if ((res = zoran_set_input(zr, vchan->channel)))
  1781. goto schan_unlock_and_return;
  1782. if ((res = zoran_set_norm(zr, vchan->norm)))
  1783. goto schan_unlock_and_return;
  1784. /* Make sure the changes come into effect */
  1785. res = wait_grab_pending(zr);
  1786. schan_unlock_and_return:
  1787. mutex_unlock(&zr->resource_lock);
  1788. return res;
  1789. }
  1790. break;
  1791. case VIDIOCGPICT:
  1792. {
  1793. struct video_picture *vpict = arg;
  1794. dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
  1795. memset(vpict, 0, sizeof(struct video_picture));
  1796. mutex_lock(&zr->resource_lock);
  1797. vpict->hue = zr->hue;
  1798. vpict->brightness = zr->brightness;
  1799. vpict->contrast = zr->contrast;
  1800. vpict->colour = zr->saturation;
  1801. if (fh->overlay_settings.format) {
  1802. vpict->depth = fh->overlay_settings.format->depth;
  1803. vpict->palette = fh->overlay_settings.format->palette;
  1804. } else {
  1805. vpict->depth = 0;
  1806. }
  1807. mutex_unlock(&zr->resource_lock);
  1808. return 0;
  1809. }
  1810. break;
  1811. case VIDIOCSPICT:
  1812. {
  1813. struct video_picture *vpict = arg;
  1814. int i;
  1815. dprintk(3,
  1816. KERN_DEBUG
  1817. "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
  1818. ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
  1819. vpict->colour, vpict->contrast, vpict->depth,
  1820. vpict->palette);
  1821. for (i = 0; i < NUM_FORMATS; i++) {
  1822. const struct zoran_format *fmt = &zoran_formats[i];
  1823. if (fmt->palette != -1 &&
  1824. fmt->flags & ZORAN_FORMAT_OVERLAY &&
  1825. fmt->palette == vpict->palette &&
  1826. fmt->depth == vpict->depth)
  1827. break;
  1828. }
  1829. if (i == NUM_FORMATS) {
  1830. dprintk(1,
  1831. KERN_ERR
  1832. "%s: VIDIOCSPICT - Invalid palette %d\n",
  1833. ZR_DEVNAME(zr), vpict->palette);
  1834. return -EINVAL;
  1835. }
  1836. mutex_lock(&zr->resource_lock);
  1837. decoder_command(zr, DECODER_SET_PICTURE, vpict);
  1838. zr->hue = vpict->hue;
  1839. zr->contrast = vpict->contrast;
  1840. zr->saturation = vpict->colour;
  1841. zr->brightness = vpict->brightness;
  1842. fh->overlay_settings.format = &zoran_formats[i];
  1843. mutex_unlock(&zr->resource_lock);
  1844. return 0;
  1845. }
  1846. break;
  1847. case VIDIOCCAPTURE:
  1848. {
  1849. int *on = arg, res;
  1850. dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
  1851. ZR_DEVNAME(zr), *on);
  1852. mutex_lock(&zr->resource_lock);
  1853. res = setup_overlay(file, *on);
  1854. mutex_unlock(&zr->resource_lock);
  1855. return res;
  1856. }
  1857. break;
  1858. case VIDIOCGWIN:
  1859. {
  1860. struct video_window *vwin = arg;
  1861. dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
  1862. memset(vwin, 0, sizeof(struct video_window));
  1863. mutex_lock(&zr->resource_lock);
  1864. vwin->x = fh->overlay_settings.x;
  1865. vwin->y = fh->overlay_settings.y;
  1866. vwin->width = fh->overlay_settings.width;
  1867. vwin->height = fh->overlay_settings.height;
  1868. mutex_unlock(&zr->resource_lock);
  1869. vwin->clipcount = 0;
  1870. return 0;
  1871. }
  1872. break;
  1873. case VIDIOCSWIN:
  1874. {
  1875. struct video_window *vwin = arg;
  1876. int res;
  1877. dprintk(3,
  1878. KERN_DEBUG
  1879. "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
  1880. ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
  1881. vwin->height, vwin->clipcount);
  1882. mutex_lock(&zr->resource_lock);
  1883. res =
  1884. setup_window(file, vwin->x, vwin->y, vwin->width,
  1885. vwin->height, vwin->clips,
  1886. vwin->clipcount, NULL);
  1887. mutex_unlock(&zr->resource_lock);
  1888. return res;
  1889. }
  1890. break;
  1891. case VIDIOCGFBUF:
  1892. {
  1893. struct video_buffer *vbuf = arg;
  1894. dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
  1895. mutex_lock(&zr->resource_lock);
  1896. *vbuf = zr->buffer;
  1897. mutex_unlock(&zr->resource_lock);
  1898. return 0;
  1899. }
  1900. break;
  1901. case VIDIOCSFBUF:
  1902. {
  1903. struct video_buffer *vbuf = arg;
  1904. int i, res = 0;
  1905. dprintk(3,
  1906. KERN_DEBUG
  1907. "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
  1908. ZR_DEVNAME(zr), vbuf->base, vbuf->width,
  1909. vbuf->height, vbuf->depth, vbuf->bytesperline);
  1910. for (i = 0; i < NUM_FORMATS; i++)
  1911. if (zoran_formats[i].depth == vbuf->depth)
  1912. break;
  1913. if (i == NUM_FORMATS) {
  1914. dprintk(1,
  1915. KERN_ERR
  1916. "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
  1917. ZR_DEVNAME(zr), vbuf->depth);
  1918. return -EINVAL;
  1919. }
  1920. mutex_lock(&zr->resource_lock);
  1921. res =
  1922. setup_fbuffer(file, vbuf->base, &zoran_formats[i],
  1923. vbuf->width, vbuf->height,
  1924. vbuf->bytesperline);
  1925. mutex_unlock(&zr->resource_lock);
  1926. return res;
  1927. }
  1928. break;
  1929. case VIDIOCSYNC:
  1930. {
  1931. int *frame = arg, res;
  1932. dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
  1933. ZR_DEVNAME(zr), *frame);
  1934. mutex_lock(&zr->resource_lock);
  1935. res = v4l_sync(file, *frame);
  1936. mutex_unlock(&zr->resource_lock);
  1937. if (!res)
  1938. zr->v4l_sync_tail++;
  1939. return res;
  1940. }
  1941. break;
  1942. case VIDIOCMCAPTURE:
  1943. {
  1944. struct video_mmap *vmap = arg;
  1945. int res;
  1946. dprintk(3,
  1947. KERN_DEBUG
  1948. "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
  1949. ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
  1950. vmap->format);
  1951. mutex_lock(&zr->resource_lock);
  1952. res = v4l_grab(file, vmap);
  1953. mutex_unlock(&zr->resource_lock);
  1954. return res;
  1955. }
  1956. break;
  1957. case VIDIOCGMBUF:
  1958. {
  1959. struct video_mbuf *vmbuf = arg;
  1960. int i, res = 0;
  1961. dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
  1962. vmbuf->size =
  1963. fh->v4l_buffers.num_buffers *
  1964. fh->v4l_buffers.buffer_size;
  1965. vmbuf->frames = fh->v4l_buffers.num_buffers;
  1966. for (i = 0; i < vmbuf->frames; i++) {
  1967. vmbuf->offsets[i] =
  1968. i * fh->v4l_buffers.buffer_size;
  1969. }
  1970. mutex_lock(&zr->resource_lock);
  1971. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  1972. dprintk(1,
  1973. KERN_ERR
  1974. "%s: VIDIOCGMBUF - buffers already allocated\n",
  1975. ZR_DEVNAME(zr));
  1976. res = -EINVAL;
  1977. goto v4l1reqbuf_unlock_and_return;
  1978. }
  1979. if (v4l_fbuffer_alloc(file)) {
  1980. res = -ENOMEM;
  1981. goto v4l1reqbuf_unlock_and_return;
  1982. }
  1983. /* The next mmap will map the V4L buffers */
  1984. fh->map_mode = ZORAN_MAP_MODE_RAW;
  1985. v4l1reqbuf_unlock_and_return:
  1986. mutex_unlock(&zr->resource_lock);
  1987. return res;
  1988. }
  1989. break;
  1990. case VIDIOCGUNIT:
  1991. {
  1992. struct video_unit *vunit = arg;
  1993. dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
  1994. vunit->video = zr->video_dev->minor;
  1995. vunit->vbi = VIDEO_NO_UNIT;
  1996. vunit->radio = VIDEO_NO_UNIT;
  1997. vunit->audio = VIDEO_NO_UNIT;
  1998. vunit->teletext = VIDEO_NO_UNIT;
  1999. return 0;
  2000. }
  2001. break;
  2002. /*
  2003. * RJ: In principal we could support subcaptures for V4L grabbing.
  2004. * Not even the famous BTTV driver has them, however.
  2005. * If there should be a strong demand, one could consider
  2006. * to implement them.
  2007. */
  2008. case VIDIOCGCAPTURE:
  2009. {
  2010. dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
  2011. ZR_DEVNAME(zr));
  2012. return -EINVAL;
  2013. }
  2014. break;
  2015. case VIDIOCSCAPTURE:
  2016. {
  2017. dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
  2018. ZR_DEVNAME(zr));
  2019. return -EINVAL;
  2020. }
  2021. break;
  2022. case BUZIOC_G_PARAMS:
  2023. {
  2024. struct zoran_params *bparams = arg;
  2025. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
  2026. memset(bparams, 0, sizeof(struct zoran_params));
  2027. bparams->major_version = MAJOR_VERSION;
  2028. bparams->minor_version = MINOR_VERSION;
  2029. mutex_lock(&zr->resource_lock);
  2030. bparams->norm = zr->norm;
  2031. bparams->input = zr->input;
  2032. bparams->decimation = fh->jpg_settings.decimation;
  2033. bparams->HorDcm = fh->jpg_settings.HorDcm;
  2034. bparams->VerDcm = fh->jpg_settings.VerDcm;
  2035. bparams->TmpDcm = fh->jpg_settings.TmpDcm;
  2036. bparams->field_per_buff = fh->jpg_settings.field_per_buff;
  2037. bparams->img_x = fh->jpg_settings.img_x;
  2038. bparams->img_y = fh->jpg_settings.img_y;
  2039. bparams->img_width = fh->jpg_settings.img_width;
  2040. bparams->img_height = fh->jpg_settings.img_height;
  2041. bparams->odd_even = fh->jpg_settings.odd_even;
  2042. bparams->quality = fh->jpg_settings.jpg_comp.quality;
  2043. bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
  2044. bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  2045. memcpy(bparams->APP_data,
  2046. fh->jpg_settings.jpg_comp.APP_data,
  2047. sizeof(bparams->APP_data));
  2048. bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
  2049. memcpy(bparams->COM_data,
  2050. fh->jpg_settings.jpg_comp.COM_data,
  2051. sizeof(bparams->COM_data));
  2052. bparams->jpeg_markers =
  2053. fh->jpg_settings.jpg_comp.jpeg_markers;
  2054. mutex_unlock(&zr->resource_lock);
  2055. bparams->VFIFO_FB = 0;
  2056. return 0;
  2057. }
  2058. break;
  2059. case BUZIOC_S_PARAMS:
  2060. {
  2061. struct zoran_params *bparams = arg;
  2062. int res = 0;
  2063. dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
  2064. settings.decimation = bparams->decimation;
  2065. settings.HorDcm = bparams->HorDcm;
  2066. settings.VerDcm = bparams->VerDcm;
  2067. settings.TmpDcm = bparams->TmpDcm;
  2068. settings.field_per_buff = bparams->field_per_buff;
  2069. settings.img_x = bparams->img_x;
  2070. settings.img_y = bparams->img_y;
  2071. settings.img_width = bparams->img_width;
  2072. settings.img_height = bparams->img_height;
  2073. settings.odd_even = bparams->odd_even;
  2074. settings.jpg_comp.quality = bparams->quality;
  2075. settings.jpg_comp.APPn = bparams->APPn;
  2076. settings.jpg_comp.APP_len = bparams->APP_len;
  2077. memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
  2078. sizeof(bparams->APP_data));
  2079. settings.jpg_comp.COM_len = bparams->COM_len;
  2080. memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
  2081. sizeof(bparams->COM_data));
  2082. settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
  2083. mutex_lock(&zr->resource_lock);
  2084. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2085. dprintk(1,
  2086. KERN_ERR
  2087. "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
  2088. ZR_DEVNAME(zr));
  2089. res = -EINVAL;
  2090. goto sparams_unlock_and_return;
  2091. }
  2092. /* Check the params first before overwriting our
  2093. * nternal values */
  2094. if (zoran_check_jpg_settings(zr, &settings)) {
  2095. res = -EINVAL;
  2096. goto sparams_unlock_and_return;
  2097. }
  2098. fh->jpg_settings = settings;
  2099. sparams_unlock_and_return:
  2100. mutex_unlock(&zr->resource_lock);
  2101. return res;
  2102. }
  2103. break;
  2104. case BUZIOC_REQBUFS:
  2105. {
  2106. struct zoran_requestbuffers *breq = arg;
  2107. int res = 0;
  2108. dprintk(3,
  2109. KERN_DEBUG
  2110. "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
  2111. ZR_DEVNAME(zr), breq->count, breq->size);
  2112. /* Enforce reasonable lower and upper limits */
  2113. if (breq->count < 4)
  2114. breq->count = 4; /* Could be choosen smaller */
  2115. if (breq->count > jpg_nbufs)
  2116. breq->count = jpg_nbufs;
  2117. breq->size = PAGE_ALIGN(breq->size);
  2118. if (breq->size < 8192)
  2119. breq->size = 8192; /* Arbitrary */
  2120. /* breq->size is limited by 1 page for the stat_com
  2121. * tables to a Maximum of 2 MB */
  2122. if (breq->size > jpg_bufsize)
  2123. breq->size = jpg_bufsize;
  2124. if (fh->jpg_buffers.need_contiguous &&
  2125. breq->size > MAX_KMALLOC_MEM)
  2126. breq->size = MAX_KMALLOC_MEM;
  2127. mutex_lock(&zr->resource_lock);
  2128. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  2129. dprintk(1,
  2130. KERN_ERR
  2131. "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
  2132. ZR_DEVNAME(zr));
  2133. res = -EBUSY;
  2134. goto jpgreqbuf_unlock_and_return;
  2135. }
  2136. fh->jpg_buffers.num_buffers = breq->count;
  2137. fh->jpg_buffers.buffer_size = breq->size;
  2138. if (jpg_fbuffer_alloc(file)) {
  2139. res = -ENOMEM;
  2140. goto jpgreqbuf_unlock_and_return;
  2141. }
  2142. /* The next mmap will map the MJPEG buffers - could
  2143. * also be *_PLAY, but it doesn't matter here */
  2144. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2145. jpgreqbuf_unlock_and_return:
  2146. mutex_unlock(&zr->resource_lock);
  2147. return res;
  2148. }
  2149. break;
  2150. case BUZIOC_QBUF_CAPT:
  2151. {
  2152. int *frame = arg, res;
  2153. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
  2154. ZR_DEVNAME(zr), *frame);
  2155. mutex_lock(&zr->resource_lock);
  2156. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
  2157. mutex_unlock(&zr->resource_lock);
  2158. return res;
  2159. }
  2160. break;
  2161. case BUZIOC_QBUF_PLAY:
  2162. {
  2163. int *frame = arg, res;
  2164. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
  2165. ZR_DEVNAME(zr), *frame);
  2166. mutex_lock(&zr->resource_lock);
  2167. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
  2168. mutex_unlock(&zr->resource_lock);
  2169. return res;
  2170. }
  2171. break;
  2172. case BUZIOC_SYNC:
  2173. {
  2174. struct zoran_sync *bsync = arg;
  2175. int res;
  2176. dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
  2177. mutex_lock(&zr->resource_lock);
  2178. res = jpg_sync(file, bsync);
  2179. mutex_unlock(&zr->resource_lock);
  2180. return res;
  2181. }
  2182. break;
  2183. case BUZIOC_G_STATUS:
  2184. {
  2185. struct zoran_status *bstat = arg;
  2186. int norm, input, status, res = 0;
  2187. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
  2188. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2189. dprintk(1,
  2190. KERN_ERR
  2191. "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
  2192. ZR_DEVNAME(zr));
  2193. return -EINVAL;
  2194. }
  2195. input = zr->card.input[bstat->input].muxsel;
  2196. norm = VIDEO_MODE_AUTO;
  2197. mutex_lock(&zr->resource_lock);
  2198. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2199. dprintk(1,
  2200. KERN_ERR
  2201. "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
  2202. ZR_DEVNAME(zr));
  2203. res = -EINVAL;
  2204. goto gstat_unlock_and_return;
  2205. }
  2206. decoder_command(zr, DECODER_SET_INPUT, &input);
  2207. decoder_command(zr, DECODER_SET_NORM, &norm);
  2208. /* sleep 1 second */
  2209. ssleep(1);
  2210. /* Get status of video decoder */
  2211. decoder_command(zr, DECODER_GET_STATUS, &status);
  2212. /* restore previous input and norm */
  2213. input = zr->card.input[zr->input].muxsel;
  2214. decoder_command(zr, DECODER_SET_INPUT, &input);
  2215. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  2216. gstat_unlock_and_return:
  2217. mutex_unlock(&zr->resource_lock);
  2218. if (!res) {
  2219. bstat->signal =
  2220. (status & DECODER_STATUS_GOOD) ? 1 : 0;
  2221. if (status & DECODER_STATUS_NTSC)
  2222. bstat->norm = VIDEO_MODE_NTSC;
  2223. else if (status & DECODER_STATUS_SECAM)
  2224. bstat->norm = VIDEO_MODE_SECAM;
  2225. else
  2226. bstat->norm = VIDEO_MODE_PAL;
  2227. bstat->color =
  2228. (status & DECODER_STATUS_COLOR) ? 1 : 0;
  2229. }
  2230. return res;
  2231. }
  2232. break;
  2233. /* The new video4linux2 capture interface - much nicer than video4linux1, since
  2234. * it allows for integrating the JPEG capturing calls inside standard v4l2
  2235. */
  2236. case VIDIOC_QUERYCAP:
  2237. {
  2238. struct v4l2_capability *cap = arg;
  2239. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
  2240. memset(cap, 0, sizeof(*cap));
  2241. strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
  2242. strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
  2243. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  2244. pci_name(zr->pci_dev));
  2245. cap->version =
  2246. KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
  2247. RELEASE_VERSION);
  2248. cap->capabilities = ZORAN_V4L2_VID_FLAGS;
  2249. return 0;
  2250. }
  2251. break;
  2252. case VIDIOC_ENUM_FMT:
  2253. {
  2254. struct v4l2_fmtdesc *fmt = arg;
  2255. int index = fmt->index, num = -1, i, flag = 0, type =
  2256. fmt->type;
  2257. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
  2258. ZR_DEVNAME(zr), fmt->index);
  2259. switch (fmt->type) {
  2260. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2261. flag = ZORAN_FORMAT_CAPTURE;
  2262. break;
  2263. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2264. flag = ZORAN_FORMAT_PLAYBACK;
  2265. break;
  2266. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2267. flag = ZORAN_FORMAT_OVERLAY;
  2268. break;
  2269. default:
  2270. dprintk(1,
  2271. KERN_ERR
  2272. "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
  2273. ZR_DEVNAME(zr), fmt->type);
  2274. return -EINVAL;
  2275. }
  2276. for (i = 0; i < NUM_FORMATS; i++) {
  2277. if (zoran_formats[i].flags & flag)
  2278. num++;
  2279. if (num == fmt->index)
  2280. break;
  2281. }
  2282. if (fmt->index < 0 /* late, but not too late */ ||
  2283. i == NUM_FORMATS)
  2284. return -EINVAL;
  2285. memset(fmt, 0, sizeof(*fmt));
  2286. fmt->index = index;
  2287. fmt->type = type;
  2288. strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
  2289. fmt->pixelformat = zoran_formats[i].fourcc;
  2290. if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
  2291. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  2292. return 0;
  2293. }
  2294. break;
  2295. case VIDIOC_G_FMT:
  2296. {
  2297. struct v4l2_format *fmt = arg;
  2298. int type = fmt->type;
  2299. dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
  2300. memset(fmt, 0, sizeof(*fmt));
  2301. fmt->type = type;
  2302. switch (fmt->type) {
  2303. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2304. mutex_lock(&zr->resource_lock);
  2305. fmt->fmt.win.w.left = fh->overlay_settings.x;
  2306. fmt->fmt.win.w.top = fh->overlay_settings.y;
  2307. fmt->fmt.win.w.width = fh->overlay_settings.width;
  2308. fmt->fmt.win.w.height =
  2309. fh->overlay_settings.height;
  2310. if (fh->overlay_settings.width * 2 >
  2311. BUZ_MAX_HEIGHT)
  2312. fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
  2313. else
  2314. fmt->fmt.win.field = V4L2_FIELD_TOP;
  2315. mutex_unlock(&zr->resource_lock);
  2316. break;
  2317. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2318. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2319. mutex_lock(&zr->resource_lock);
  2320. if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2321. fh->map_mode == ZORAN_MAP_MODE_RAW) {
  2322. fmt->fmt.pix.width =
  2323. fh->v4l_settings.width;
  2324. fmt->fmt.pix.height =
  2325. fh->v4l_settings.height;
  2326. fmt->fmt.pix.sizeimage =
  2327. fh->v4l_settings.bytesperline *
  2328. fh->v4l_settings.height;
  2329. fmt->fmt.pix.pixelformat =
  2330. fh->v4l_settings.format->fourcc;
  2331. fmt->fmt.pix.colorspace =
  2332. fh->v4l_settings.format->colorspace;
  2333. fmt->fmt.pix.bytesperline = 0;
  2334. if (BUZ_MAX_HEIGHT <
  2335. (fh->v4l_settings.height * 2))
  2336. fmt->fmt.pix.field =
  2337. V4L2_FIELD_INTERLACED;
  2338. else
  2339. fmt->fmt.pix.field =
  2340. V4L2_FIELD_TOP;
  2341. } else {
  2342. fmt->fmt.pix.width =
  2343. fh->jpg_settings.img_width /
  2344. fh->jpg_settings.HorDcm;
  2345. fmt->fmt.pix.height =
  2346. fh->jpg_settings.img_height /
  2347. (fh->jpg_settings.VerDcm *
  2348. fh->jpg_settings.TmpDcm);
  2349. fmt->fmt.pix.sizeimage =
  2350. zoran_v4l2_calc_bufsize(&fh->
  2351. jpg_settings);
  2352. fmt->fmt.pix.pixelformat =
  2353. V4L2_PIX_FMT_MJPEG;
  2354. if (fh->jpg_settings.TmpDcm == 1)
  2355. fmt->fmt.pix.field =
  2356. (fh->jpg_settings.
  2357. odd_even ? V4L2_FIELD_SEQ_BT :
  2358. V4L2_FIELD_SEQ_BT);
  2359. else
  2360. fmt->fmt.pix.field =
  2361. (fh->jpg_settings.
  2362. odd_even ? V4L2_FIELD_TOP :
  2363. V4L2_FIELD_BOTTOM);
  2364. fmt->fmt.pix.bytesperline = 0;
  2365. fmt->fmt.pix.colorspace =
  2366. V4L2_COLORSPACE_SMPTE170M;
  2367. }
  2368. mutex_unlock(&zr->resource_lock);
  2369. break;
  2370. default:
  2371. dprintk(1,
  2372. KERN_ERR
  2373. "%s: VIDIOC_G_FMT - unsupported type %d\n",
  2374. ZR_DEVNAME(zr), fmt->type);
  2375. return -EINVAL;
  2376. }
  2377. return 0;
  2378. }
  2379. break;
  2380. case VIDIOC_S_FMT:
  2381. {
  2382. struct v4l2_format *fmt = arg;
  2383. int i, res = 0;
  2384. __u32 printformat;
  2385. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
  2386. ZR_DEVNAME(zr), fmt->type);
  2387. switch (fmt->type) {
  2388. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2389. dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
  2390. fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  2391. fmt->fmt.win.w.width,
  2392. fmt->fmt.win.w.height,
  2393. fmt->fmt.win.clipcount,
  2394. fmt->fmt.win.bitmap);
  2395. mutex_lock(&zr->resource_lock);
  2396. res =
  2397. setup_window(file, fmt->fmt.win.w.left,
  2398. fmt->fmt.win.w.top,
  2399. fmt->fmt.win.w.width,
  2400. fmt->fmt.win.w.height,
  2401. (struct video_clip __user *)
  2402. fmt->fmt.win.clips,
  2403. fmt->fmt.win.clipcount,
  2404. fmt->fmt.win.bitmap);
  2405. mutex_unlock(&zr->resource_lock);
  2406. return res;
  2407. break;
  2408. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2409. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2410. printformat =
  2411. __cpu_to_le32(fmt->fmt.pix.pixelformat);
  2412. dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
  2413. fmt->fmt.pix.width, fmt->fmt.pix.height,
  2414. fmt->fmt.pix.pixelformat,
  2415. (char *) &printformat);
  2416. if (fmt->fmt.pix.bytesperline > 0) {
  2417. dprintk(5,
  2418. KERN_ERR "%s: bpl not supported\n",
  2419. ZR_DEVNAME(zr));
  2420. return -EINVAL;
  2421. }
  2422. /* we can be requested to do JPEG/raw playback/capture */
  2423. if (!
  2424. (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2425. (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2426. fmt->fmt.pix.pixelformat ==
  2427. V4L2_PIX_FMT_MJPEG))) {
  2428. dprintk(1,
  2429. KERN_ERR
  2430. "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
  2431. ZR_DEVNAME(zr), fmt->type,
  2432. fmt->fmt.pix.pixelformat,
  2433. (char *) &printformat);
  2434. return -EINVAL;
  2435. }
  2436. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  2437. mutex_lock(&zr->resource_lock);
  2438. settings = fh->jpg_settings;
  2439. if (fh->v4l_buffers.allocated ||
  2440. fh->jpg_buffers.allocated) {
  2441. dprintk(1,
  2442. KERN_ERR
  2443. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2444. ZR_DEVNAME(zr));
  2445. res = -EBUSY;
  2446. goto sfmtjpg_unlock_and_return;
  2447. }
  2448. /* we actually need to set 'real' parameters now */
  2449. if ((fmt->fmt.pix.height * 2) >
  2450. BUZ_MAX_HEIGHT)
  2451. settings.TmpDcm = 1;
  2452. else
  2453. settings.TmpDcm = 2;
  2454. settings.decimation = 0;
  2455. if (fmt->fmt.pix.height <=
  2456. fh->jpg_settings.img_height / 2)
  2457. settings.VerDcm = 2;
  2458. else
  2459. settings.VerDcm = 1;
  2460. if (fmt->fmt.pix.width <=
  2461. fh->jpg_settings.img_width / 4)
  2462. settings.HorDcm = 4;
  2463. else if (fmt->fmt.pix.width <=
  2464. fh->jpg_settings.img_width / 2)
  2465. settings.HorDcm = 2;
  2466. else
  2467. settings.HorDcm = 1;
  2468. if (settings.TmpDcm == 1)
  2469. settings.field_per_buff = 2;
  2470. else
  2471. settings.field_per_buff = 1;
  2472. /* check */
  2473. if ((res =
  2474. zoran_check_jpg_settings(zr,
  2475. &settings)))
  2476. goto sfmtjpg_unlock_and_return;
  2477. /* it's ok, so set them */
  2478. fh->jpg_settings = settings;
  2479. /* tell the user what we actually did */
  2480. fmt->fmt.pix.width =
  2481. settings.img_width / settings.HorDcm;
  2482. fmt->fmt.pix.height =
  2483. settings.img_height * 2 /
  2484. (settings.TmpDcm * settings.VerDcm);
  2485. if (settings.TmpDcm == 1)
  2486. fmt->fmt.pix.field =
  2487. (fh->jpg_settings.
  2488. odd_even ? V4L2_FIELD_SEQ_TB :
  2489. V4L2_FIELD_SEQ_BT);
  2490. else
  2491. fmt->fmt.pix.field =
  2492. (fh->jpg_settings.
  2493. odd_even ? V4L2_FIELD_TOP :
  2494. V4L2_FIELD_BOTTOM);
  2495. fh->jpg_buffers.buffer_size =
  2496. zoran_v4l2_calc_bufsize(&fh->
  2497. jpg_settings);
  2498. fmt->fmt.pix.sizeimage =
  2499. fh->jpg_buffers.buffer_size;
  2500. /* we hereby abuse this variable to show that
  2501. * we're gonna do mjpeg capture */
  2502. fh->map_mode =
  2503. (fmt->type ==
  2504. V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
  2505. ZORAN_MAP_MODE_JPG_REC :
  2506. ZORAN_MAP_MODE_JPG_PLAY;
  2507. sfmtjpg_unlock_and_return:
  2508. mutex_unlock(&zr->resource_lock);
  2509. } else {
  2510. for (i = 0; i < NUM_FORMATS; i++)
  2511. if (fmt->fmt.pix.pixelformat ==
  2512. zoran_formats[i].fourcc)
  2513. break;
  2514. if (i == NUM_FORMATS) {
  2515. dprintk(1,
  2516. KERN_ERR
  2517. "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
  2518. ZR_DEVNAME(zr),
  2519. fmt->fmt.pix.pixelformat,
  2520. (char *) &printformat);
  2521. return -EINVAL;
  2522. }
  2523. mutex_lock(&zr->resource_lock);
  2524. if (fh->jpg_buffers.allocated ||
  2525. (fh->v4l_buffers.allocated &&
  2526. fh->v4l_buffers.active !=
  2527. ZORAN_FREE)) {
  2528. dprintk(1,
  2529. KERN_ERR
  2530. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2531. ZR_DEVNAME(zr));
  2532. res = -EBUSY;
  2533. goto sfmtv4l_unlock_and_return;
  2534. }
  2535. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  2536. fmt->fmt.pix.height =
  2537. BUZ_MAX_HEIGHT;
  2538. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  2539. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  2540. if ((res =
  2541. zoran_v4l_set_format(file,
  2542. fmt->fmt.pix.
  2543. width,
  2544. fmt->fmt.pix.
  2545. height,
  2546. &zoran_formats
  2547. [i])))
  2548. goto sfmtv4l_unlock_and_return;
  2549. /* tell the user the
  2550. * results/missing stuff */
  2551. fmt->fmt.pix.sizeimage =
  2552. fh->v4l_settings.height *
  2553. fh->v4l_settings.bytesperline;
  2554. if (BUZ_MAX_HEIGHT <
  2555. (fh->v4l_settings.height * 2))
  2556. fmt->fmt.pix.field =
  2557. V4L2_FIELD_INTERLACED;
  2558. else
  2559. fmt->fmt.pix.field =
  2560. V4L2_FIELD_TOP;
  2561. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2562. sfmtv4l_unlock_and_return:
  2563. mutex_unlock(&zr->resource_lock);
  2564. }
  2565. break;
  2566. default:
  2567. dprintk(3, "unsupported\n");
  2568. dprintk(1,
  2569. KERN_ERR
  2570. "%s: VIDIOC_S_FMT - unsupported type %d\n",
  2571. ZR_DEVNAME(zr), fmt->type);
  2572. return -EINVAL;
  2573. }
  2574. return res;
  2575. }
  2576. break;
  2577. case VIDIOC_G_FBUF:
  2578. {
  2579. struct v4l2_framebuffer *fb = arg;
  2580. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
  2581. memset(fb, 0, sizeof(*fb));
  2582. mutex_lock(&zr->resource_lock);
  2583. fb->base = zr->buffer.base;
  2584. fb->fmt.width = zr->buffer.width;
  2585. fb->fmt.height = zr->buffer.height;
  2586. if (zr->overlay_settings.format) {
  2587. fb->fmt.pixelformat =
  2588. fh->overlay_settings.format->fourcc;
  2589. }
  2590. fb->fmt.bytesperline = zr->buffer.bytesperline;
  2591. mutex_unlock(&zr->resource_lock);
  2592. fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  2593. fb->fmt.field = V4L2_FIELD_INTERLACED;
  2594. fb->flags = V4L2_FBUF_FLAG_OVERLAY;
  2595. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2596. return 0;
  2597. }
  2598. break;
  2599. case VIDIOC_S_FBUF:
  2600. {
  2601. int i, res = 0;
  2602. struct v4l2_framebuffer *fb = arg;
  2603. __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
  2604. dprintk(3,
  2605. KERN_DEBUG
  2606. "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
  2607. ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
  2608. fb->fmt.bytesperline, fb->fmt.pixelformat,
  2609. (char *) &printformat);
  2610. for (i = 0; i < NUM_FORMATS; i++)
  2611. if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
  2612. break;
  2613. if (i == NUM_FORMATS) {
  2614. dprintk(1,
  2615. KERN_ERR
  2616. "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
  2617. ZR_DEVNAME(zr), fb->fmt.pixelformat,
  2618. (char *) &printformat);
  2619. return -EINVAL;
  2620. }
  2621. mutex_lock(&zr->resource_lock);
  2622. res =
  2623. setup_fbuffer(file, fb->base, &zoran_formats[i],
  2624. fb->fmt.width, fb->fmt.height,
  2625. fb->fmt.bytesperline);
  2626. mutex_unlock(&zr->resource_lock);
  2627. return res;
  2628. }
  2629. break;
  2630. case VIDIOC_OVERLAY:
  2631. {
  2632. int *on = arg, res;
  2633. dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
  2634. ZR_DEVNAME(zr), *on);
  2635. mutex_lock(&zr->resource_lock);
  2636. res = setup_overlay(file, *on);
  2637. mutex_unlock(&zr->resource_lock);
  2638. return res;
  2639. }
  2640. break;
  2641. case VIDIOC_REQBUFS:
  2642. {
  2643. struct v4l2_requestbuffers *req = arg;
  2644. int res = 0;
  2645. dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
  2646. ZR_DEVNAME(zr), req->type);
  2647. if (req->memory != V4L2_MEMORY_MMAP) {
  2648. dprintk(1,
  2649. KERN_ERR
  2650. "%s: only MEMORY_MMAP capture is supported, not %d\n",
  2651. ZR_DEVNAME(zr), req->memory);
  2652. return -EINVAL;
  2653. }
  2654. mutex_lock(&zr->resource_lock);
  2655. if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
  2656. dprintk(1,
  2657. KERN_ERR
  2658. "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
  2659. ZR_DEVNAME(zr));
  2660. res = -EBUSY;
  2661. goto v4l2reqbuf_unlock_and_return;
  2662. }
  2663. if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
  2664. req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2665. /* control user input */
  2666. if (req->count < 2)
  2667. req->count = 2;
  2668. if (req->count > v4l_nbufs)
  2669. req->count = v4l_nbufs;
  2670. fh->v4l_buffers.num_buffers = req->count;
  2671. if (v4l_fbuffer_alloc(file)) {
  2672. res = -ENOMEM;
  2673. goto v4l2reqbuf_unlock_and_return;
  2674. }
  2675. /* The next mmap will map the V4L buffers */
  2676. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2677. } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
  2678. fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2679. /* we need to calculate size ourselves now */
  2680. if (req->count < 4)
  2681. req->count = 4;
  2682. if (req->count > jpg_nbufs)
  2683. req->count = jpg_nbufs;
  2684. fh->jpg_buffers.num_buffers = req->count;
  2685. fh->jpg_buffers.buffer_size =
  2686. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2687. if (jpg_fbuffer_alloc(file)) {
  2688. res = -ENOMEM;
  2689. goto v4l2reqbuf_unlock_and_return;
  2690. }
  2691. /* The next mmap will map the MJPEG buffers */
  2692. if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2693. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2694. else
  2695. fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
  2696. } else {
  2697. dprintk(1,
  2698. KERN_ERR
  2699. "%s: VIDIOC_REQBUFS - unknown type %d\n",
  2700. ZR_DEVNAME(zr), req->type);
  2701. res = -EINVAL;
  2702. goto v4l2reqbuf_unlock_and_return;
  2703. }
  2704. v4l2reqbuf_unlock_and_return:
  2705. mutex_unlock(&zr->resource_lock);
  2706. return 0;
  2707. }
  2708. break;
  2709. case VIDIOC_QUERYBUF:
  2710. {
  2711. struct v4l2_buffer *buf = arg;
  2712. __u32 type = buf->type;
  2713. int index = buf->index, res;
  2714. dprintk(3,
  2715. KERN_DEBUG
  2716. "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
  2717. ZR_DEVNAME(zr), buf->index, buf->type);
  2718. memset(buf, 0, sizeof(*buf));
  2719. buf->type = type;
  2720. buf->index = index;
  2721. mutex_lock(&zr->resource_lock);
  2722. res = zoran_v4l2_buffer_status(file, buf, buf->index);
  2723. mutex_unlock(&zr->resource_lock);
  2724. return res;
  2725. }
  2726. break;
  2727. case VIDIOC_QBUF:
  2728. {
  2729. struct v4l2_buffer *buf = arg;
  2730. int res = 0, codec_mode, buf_type;
  2731. dprintk(3,
  2732. KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
  2733. ZR_DEVNAME(zr), buf->type, buf->index);
  2734. mutex_lock(&zr->resource_lock);
  2735. switch (fh->map_mode) {
  2736. case ZORAN_MAP_MODE_RAW:
  2737. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2738. dprintk(1,
  2739. KERN_ERR
  2740. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2741. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2742. res = -EINVAL;
  2743. goto qbuf_unlock_and_return;
  2744. }
  2745. res = zoran_v4l_queue_frame(file, buf->index);
  2746. if (res)
  2747. goto qbuf_unlock_and_return;
  2748. if (!zr->v4l_memgrab_active &&
  2749. fh->v4l_buffers.active == ZORAN_LOCKED)
  2750. zr36057_set_memgrab(zr, 1);
  2751. break;
  2752. case ZORAN_MAP_MODE_JPG_REC:
  2753. case ZORAN_MAP_MODE_JPG_PLAY:
  2754. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2755. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2756. codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
  2757. } else {
  2758. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2759. codec_mode = BUZ_MODE_MOTION_COMPRESS;
  2760. }
  2761. if (buf->type != buf_type) {
  2762. dprintk(1,
  2763. KERN_ERR
  2764. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2765. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2766. res = -EINVAL;
  2767. goto qbuf_unlock_and_return;
  2768. }
  2769. res =
  2770. zoran_jpg_queue_frame(file, buf->index,
  2771. codec_mode);
  2772. if (res != 0)
  2773. goto qbuf_unlock_and_return;
  2774. if (zr->codec_mode == BUZ_MODE_IDLE &&
  2775. fh->jpg_buffers.active == ZORAN_LOCKED) {
  2776. zr36057_enable_jpg(zr, codec_mode);
  2777. }
  2778. break;
  2779. default:
  2780. dprintk(1,
  2781. KERN_ERR
  2782. "%s: VIDIOC_QBUF - unsupported type %d\n",
  2783. ZR_DEVNAME(zr), buf->type);
  2784. res = -EINVAL;
  2785. goto qbuf_unlock_and_return;
  2786. }
  2787. qbuf_unlock_and_return:
  2788. mutex_unlock(&zr->resource_lock);
  2789. return res;
  2790. }
  2791. break;
  2792. case VIDIOC_DQBUF:
  2793. {
  2794. struct v4l2_buffer *buf = arg;
  2795. int res = 0, buf_type, num = -1; /* compiler borks here (?) */
  2796. dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
  2797. ZR_DEVNAME(zr), buf->type);
  2798. mutex_lock(&zr->resource_lock);
  2799. switch (fh->map_mode) {
  2800. case ZORAN_MAP_MODE_RAW:
  2801. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2802. dprintk(1,
  2803. KERN_ERR
  2804. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2805. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2806. res = -EINVAL;
  2807. goto dqbuf_unlock_and_return;
  2808. }
  2809. num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2810. if (file->f_flags & O_NONBLOCK &&
  2811. zr->v4l_buffers.buffer[num].state !=
  2812. BUZ_STATE_DONE) {
  2813. res = -EAGAIN;
  2814. goto dqbuf_unlock_and_return;
  2815. }
  2816. res = v4l_sync(file, num);
  2817. if (res)
  2818. goto dqbuf_unlock_and_return;
  2819. else
  2820. zr->v4l_sync_tail++;
  2821. res = zoran_v4l2_buffer_status(file, buf, num);
  2822. break;
  2823. case ZORAN_MAP_MODE_JPG_REC:
  2824. case ZORAN_MAP_MODE_JPG_PLAY:
  2825. {
  2826. struct zoran_sync bs;
  2827. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
  2828. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2829. else
  2830. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2831. if (buf->type != buf_type) {
  2832. dprintk(1,
  2833. KERN_ERR
  2834. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2835. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2836. res = -EINVAL;
  2837. goto dqbuf_unlock_and_return;
  2838. }
  2839. num =
  2840. zr->jpg_pend[zr->
  2841. jpg_que_tail & BUZ_MASK_FRAME];
  2842. if (file->f_flags & O_NONBLOCK &&
  2843. zr->jpg_buffers.buffer[num].state !=
  2844. BUZ_STATE_DONE) {
  2845. res = -EAGAIN;
  2846. goto dqbuf_unlock_and_return;
  2847. }
  2848. res = jpg_sync(file, &bs);
  2849. if (res)
  2850. goto dqbuf_unlock_and_return;
  2851. res =
  2852. zoran_v4l2_buffer_status(file, buf, bs.frame);
  2853. break;
  2854. }
  2855. default:
  2856. dprintk(1,
  2857. KERN_ERR
  2858. "%s: VIDIOC_DQBUF - unsupported type %d\n",
  2859. ZR_DEVNAME(zr), buf->type);
  2860. res = -EINVAL;
  2861. goto dqbuf_unlock_and_return;
  2862. }
  2863. dqbuf_unlock_and_return:
  2864. mutex_unlock(&zr->resource_lock);
  2865. return res;
  2866. }
  2867. break;
  2868. case VIDIOC_STREAMON:
  2869. {
  2870. int res = 0;
  2871. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
  2872. mutex_lock(&zr->resource_lock);
  2873. switch (fh->map_mode) {
  2874. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2875. if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
  2876. fh->v4l_buffers.active != ZORAN_ACTIVE) {
  2877. res = -EBUSY;
  2878. goto strmon_unlock_and_return;
  2879. }
  2880. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2881. ZORAN_LOCKED;
  2882. zr->v4l_settings = fh->v4l_settings;
  2883. zr->v4l_sync_tail = zr->v4l_pend_tail;
  2884. if (!zr->v4l_memgrab_active &&
  2885. zr->v4l_pend_head != zr->v4l_pend_tail) {
  2886. zr36057_set_memgrab(zr, 1);
  2887. }
  2888. break;
  2889. case ZORAN_MAP_MODE_JPG_REC:
  2890. case ZORAN_MAP_MODE_JPG_PLAY:
  2891. /* what is the codec mode right now? */
  2892. if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
  2893. fh->jpg_buffers.active != ZORAN_ACTIVE) {
  2894. res = -EBUSY;
  2895. goto strmon_unlock_and_return;
  2896. }
  2897. zr->jpg_buffers.active = fh->jpg_buffers.active =
  2898. ZORAN_LOCKED;
  2899. if (zr->jpg_que_head != zr->jpg_que_tail) {
  2900. /* Start the jpeg codec when the first frame is queued */
  2901. jpeg_start(zr);
  2902. }
  2903. break;
  2904. default:
  2905. dprintk(1,
  2906. KERN_ERR
  2907. "%s: VIDIOC_STREAMON - invalid map mode %d\n",
  2908. ZR_DEVNAME(zr), fh->map_mode);
  2909. res = -EINVAL;
  2910. goto strmon_unlock_and_return;
  2911. }
  2912. strmon_unlock_and_return:
  2913. mutex_unlock(&zr->resource_lock);
  2914. return res;
  2915. }
  2916. break;
  2917. case VIDIOC_STREAMOFF:
  2918. {
  2919. int i, res = 0;
  2920. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
  2921. mutex_lock(&zr->resource_lock);
  2922. switch (fh->map_mode) {
  2923. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2924. if (fh->v4l_buffers.active == ZORAN_FREE &&
  2925. zr->v4l_buffers.active != ZORAN_FREE) {
  2926. res = -EPERM; /* stay off other's settings! */
  2927. goto strmoff_unlock_and_return;
  2928. }
  2929. if (zr->v4l_buffers.active == ZORAN_FREE)
  2930. goto strmoff_unlock_and_return;
  2931. /* unload capture */
  2932. if (zr->v4l_memgrab_active) {
  2933. unsigned long flags;
  2934. spin_lock_irqsave(&zr->spinlock, flags);
  2935. zr36057_set_memgrab(zr, 0);
  2936. spin_unlock_irqrestore(&zr->spinlock, flags);
  2937. }
  2938. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  2939. zr->v4l_buffers.buffer[i].state =
  2940. BUZ_STATE_USER;
  2941. fh->v4l_buffers = zr->v4l_buffers;
  2942. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2943. ZORAN_FREE;
  2944. zr->v4l_grab_seq = 0;
  2945. zr->v4l_pend_head = zr->v4l_pend_tail = 0;
  2946. zr->v4l_sync_tail = 0;
  2947. break;
  2948. case ZORAN_MAP_MODE_JPG_REC:
  2949. case ZORAN_MAP_MODE_JPG_PLAY:
  2950. if (fh->jpg_buffers.active == ZORAN_FREE &&
  2951. zr->jpg_buffers.active != ZORAN_FREE) {
  2952. res = -EPERM; /* stay off other's settings! */
  2953. goto strmoff_unlock_and_return;
  2954. }
  2955. if (zr->jpg_buffers.active == ZORAN_FREE)
  2956. goto strmoff_unlock_and_return;
  2957. res =
  2958. jpg_qbuf(file, -1,
  2959. (fh->map_mode ==
  2960. ZORAN_MAP_MODE_JPG_REC) ?
  2961. BUZ_MODE_MOTION_COMPRESS :
  2962. BUZ_MODE_MOTION_DECOMPRESS);
  2963. if (res)
  2964. goto strmoff_unlock_and_return;
  2965. break;
  2966. default:
  2967. dprintk(1,
  2968. KERN_ERR
  2969. "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
  2970. ZR_DEVNAME(zr), fh->map_mode);
  2971. res = -EINVAL;
  2972. goto strmoff_unlock_and_return;
  2973. }
  2974. strmoff_unlock_and_return:
  2975. mutex_unlock(&zr->resource_lock);
  2976. return res;
  2977. }
  2978. break;
  2979. case VIDIOC_QUERYCTRL:
  2980. {
  2981. struct v4l2_queryctrl *ctrl = arg;
  2982. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
  2983. ZR_DEVNAME(zr), ctrl->id);
  2984. /* we only support hue/saturation/contrast/brightness */
  2985. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2986. ctrl->id > V4L2_CID_HUE)
  2987. return -EINVAL;
  2988. else {
  2989. int id = ctrl->id;
  2990. memset(ctrl, 0, sizeof(*ctrl));
  2991. ctrl->id = id;
  2992. }
  2993. switch (ctrl->id) {
  2994. case V4L2_CID_BRIGHTNESS:
  2995. strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
  2996. break;
  2997. case V4L2_CID_CONTRAST:
  2998. strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
  2999. break;
  3000. case V4L2_CID_SATURATION:
  3001. strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
  3002. break;
  3003. case V4L2_CID_HUE:
  3004. strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
  3005. break;
  3006. }
  3007. ctrl->minimum = 0;
  3008. ctrl->maximum = 65535;
  3009. ctrl->step = 1;
  3010. ctrl->default_value = 32768;
  3011. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  3012. return 0;
  3013. }
  3014. break;
  3015. case VIDIOC_G_CTRL:
  3016. {
  3017. struct v4l2_control *ctrl = arg;
  3018. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
  3019. ZR_DEVNAME(zr), ctrl->id);
  3020. /* we only support hue/saturation/contrast/brightness */
  3021. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3022. ctrl->id > V4L2_CID_HUE)
  3023. return -EINVAL;
  3024. mutex_lock(&zr->resource_lock);
  3025. switch (ctrl->id) {
  3026. case V4L2_CID_BRIGHTNESS:
  3027. ctrl->value = zr->brightness;
  3028. break;
  3029. case V4L2_CID_CONTRAST:
  3030. ctrl->value = zr->contrast;
  3031. break;
  3032. case V4L2_CID_SATURATION:
  3033. ctrl->value = zr->saturation;
  3034. break;
  3035. case V4L2_CID_HUE:
  3036. ctrl->value = zr->hue;
  3037. break;
  3038. }
  3039. mutex_unlock(&zr->resource_lock);
  3040. return 0;
  3041. }
  3042. break;
  3043. case VIDIOC_S_CTRL:
  3044. {
  3045. struct v4l2_control *ctrl = arg;
  3046. struct video_picture pict;
  3047. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
  3048. ZR_DEVNAME(zr), ctrl->id);
  3049. /* we only support hue/saturation/contrast/brightness */
  3050. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3051. ctrl->id > V4L2_CID_HUE)
  3052. return -EINVAL;
  3053. if (ctrl->value < 0 || ctrl->value > 65535) {
  3054. dprintk(1,
  3055. KERN_ERR
  3056. "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
  3057. ZR_DEVNAME(zr), ctrl->value, ctrl->id);
  3058. return -EINVAL;
  3059. }
  3060. mutex_lock(&zr->resource_lock);
  3061. switch (ctrl->id) {
  3062. case V4L2_CID_BRIGHTNESS:
  3063. zr->brightness = ctrl->value;
  3064. break;
  3065. case V4L2_CID_CONTRAST:
  3066. zr->contrast = ctrl->value;
  3067. break;
  3068. case V4L2_CID_SATURATION:
  3069. zr->saturation = ctrl->value;
  3070. break;
  3071. case V4L2_CID_HUE:
  3072. zr->hue = ctrl->value;
  3073. break;
  3074. }
  3075. pict.brightness = zr->brightness;
  3076. pict.contrast = zr->contrast;
  3077. pict.colour = zr->saturation;
  3078. pict.hue = zr->hue;
  3079. decoder_command(zr, DECODER_SET_PICTURE, &pict);
  3080. mutex_unlock(&zr->resource_lock);
  3081. return 0;
  3082. }
  3083. break;
  3084. case VIDIOC_ENUMSTD:
  3085. {
  3086. struct v4l2_standard *std = arg;
  3087. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
  3088. ZR_DEVNAME(zr), std->index);
  3089. if (std->index < 0 || std->index >= (zr->card.norms + 1))
  3090. return -EINVAL;
  3091. else {
  3092. int id = std->index;
  3093. memset(std, 0, sizeof(*std));
  3094. std->index = id;
  3095. }
  3096. if (std->index == zr->card.norms) {
  3097. /* if we have autodetect, ... */
  3098. struct video_decoder_capability caps;
  3099. decoder_command(zr, DECODER_GET_CAPABILITIES,
  3100. &caps);
  3101. if (caps.flags & VIDEO_DECODER_AUTO) {
  3102. std->id = V4L2_STD_ALL;
  3103. strncpy(std->name, "Autodetect", sizeof(std->name)-1);
  3104. return 0;
  3105. } else
  3106. return -EINVAL;
  3107. }
  3108. switch (std->index) {
  3109. case 0:
  3110. std->id = V4L2_STD_PAL;
  3111. strncpy(std->name, "PAL", sizeof(std->name)-1);
  3112. std->frameperiod.numerator = 1;
  3113. std->frameperiod.denominator = 25;
  3114. std->framelines = zr->card.tvn[0]->Ht;
  3115. break;
  3116. case 1:
  3117. std->id = V4L2_STD_NTSC;
  3118. strncpy(std->name, "NTSC", sizeof(std->name)-1);
  3119. std->frameperiod.numerator = 1001;
  3120. std->frameperiod.denominator = 30000;
  3121. std->framelines = zr->card.tvn[1]->Ht;
  3122. break;
  3123. case 2:
  3124. std->id = V4L2_STD_SECAM;
  3125. strncpy(std->name, "SECAM", sizeof(std->name)-1);
  3126. std->frameperiod.numerator = 1;
  3127. std->frameperiod.denominator = 25;
  3128. std->framelines = zr->card.tvn[2]->Ht;
  3129. break;
  3130. }
  3131. return 0;
  3132. }
  3133. break;
  3134. case VIDIOC_G_STD:
  3135. {
  3136. v4l2_std_id *std = arg;
  3137. int norm;
  3138. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
  3139. mutex_lock(&zr->resource_lock);
  3140. norm = zr->norm;
  3141. mutex_unlock(&zr->resource_lock);
  3142. switch (norm) {
  3143. case VIDEO_MODE_PAL:
  3144. *std = V4L2_STD_PAL;
  3145. break;
  3146. case VIDEO_MODE_NTSC:
  3147. *std = V4L2_STD_NTSC;
  3148. break;
  3149. case VIDEO_MODE_SECAM:
  3150. *std = V4L2_STD_SECAM;
  3151. break;
  3152. }
  3153. return 0;
  3154. }
  3155. break;
  3156. case VIDIOC_S_STD:
  3157. {
  3158. int norm = -1, res = 0;
  3159. v4l2_std_id *std = arg;
  3160. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
  3161. ZR_DEVNAME(zr), (unsigned long long)*std);
  3162. if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
  3163. norm = VIDEO_MODE_PAL;
  3164. else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
  3165. norm = VIDEO_MODE_NTSC;
  3166. else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
  3167. norm = VIDEO_MODE_SECAM;
  3168. else if (*std == V4L2_STD_ALL)
  3169. norm = VIDEO_MODE_AUTO;
  3170. else {
  3171. dprintk(1,
  3172. KERN_ERR
  3173. "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
  3174. ZR_DEVNAME(zr), (unsigned long long)*std);
  3175. return -EINVAL;
  3176. }
  3177. mutex_lock(&zr->resource_lock);
  3178. if ((res = zoran_set_norm(zr, norm)))
  3179. goto sstd_unlock_and_return;
  3180. res = wait_grab_pending(zr);
  3181. sstd_unlock_and_return:
  3182. mutex_unlock(&zr->resource_lock);
  3183. return res;
  3184. }
  3185. break;
  3186. case VIDIOC_ENUMINPUT:
  3187. {
  3188. struct v4l2_input *inp = arg;
  3189. int status;
  3190. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
  3191. ZR_DEVNAME(zr), inp->index);
  3192. if (inp->index < 0 || inp->index >= zr->card.inputs)
  3193. return -EINVAL;
  3194. else {
  3195. int id = inp->index;
  3196. memset(inp, 0, sizeof(*inp));
  3197. inp->index = id;
  3198. }
  3199. strncpy(inp->name, zr->card.input[inp->index].name,
  3200. sizeof(inp->name) - 1);
  3201. inp->type = V4L2_INPUT_TYPE_CAMERA;
  3202. inp->std = V4L2_STD_ALL;
  3203. /* Get status of video decoder */
  3204. mutex_lock(&zr->resource_lock);
  3205. decoder_command(zr, DECODER_GET_STATUS, &status);
  3206. mutex_unlock(&zr->resource_lock);
  3207. if (!(status & DECODER_STATUS_GOOD)) {
  3208. inp->status |= V4L2_IN_ST_NO_POWER;
  3209. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  3210. }
  3211. if (!(status & DECODER_STATUS_COLOR))
  3212. inp->status |= V4L2_IN_ST_NO_COLOR;
  3213. return 0;
  3214. }
  3215. break;
  3216. case VIDIOC_G_INPUT:
  3217. {
  3218. int *input = arg;
  3219. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
  3220. mutex_lock(&zr->resource_lock);
  3221. *input = zr->input;
  3222. mutex_unlock(&zr->resource_lock);
  3223. return 0;
  3224. }
  3225. break;
  3226. case VIDIOC_S_INPUT:
  3227. {
  3228. int *input = arg, res = 0;
  3229. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
  3230. ZR_DEVNAME(zr), *input);
  3231. mutex_lock(&zr->resource_lock);
  3232. if ((res = zoran_set_input(zr, *input)))
  3233. goto sinput_unlock_and_return;
  3234. /* Make sure the changes come into effect */
  3235. res = wait_grab_pending(zr);
  3236. sinput_unlock_and_return:
  3237. mutex_unlock(&zr->resource_lock);
  3238. return res;
  3239. }
  3240. break;
  3241. case VIDIOC_ENUMOUTPUT:
  3242. {
  3243. struct v4l2_output *outp = arg;
  3244. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
  3245. ZR_DEVNAME(zr), outp->index);
  3246. if (outp->index != 0)
  3247. return -EINVAL;
  3248. memset(outp, 0, sizeof(*outp));
  3249. outp->index = 0;
  3250. outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
  3251. strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
  3252. return 0;
  3253. }
  3254. break;
  3255. case VIDIOC_G_OUTPUT:
  3256. {
  3257. int *output = arg;
  3258. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
  3259. *output = 0;
  3260. return 0;
  3261. }
  3262. break;
  3263. case VIDIOC_S_OUTPUT:
  3264. {
  3265. int *output = arg;
  3266. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
  3267. ZR_DEVNAME(zr), *output);
  3268. if (*output != 0)
  3269. return -EINVAL;
  3270. return 0;
  3271. }
  3272. break;
  3273. /* cropping (sub-frame capture) */
  3274. case VIDIOC_CROPCAP:
  3275. {
  3276. struct v4l2_cropcap *cropcap = arg;
  3277. int type = cropcap->type, res = 0;
  3278. dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
  3279. ZR_DEVNAME(zr), cropcap->type);
  3280. memset(cropcap, 0, sizeof(*cropcap));
  3281. cropcap->type = type;
  3282. mutex_lock(&zr->resource_lock);
  3283. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3284. (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3285. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3286. dprintk(1,
  3287. KERN_ERR
  3288. "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
  3289. ZR_DEVNAME(zr));
  3290. res = -EINVAL;
  3291. goto cropcap_unlock_and_return;
  3292. }
  3293. cropcap->bounds.top = cropcap->bounds.left = 0;
  3294. cropcap->bounds.width = BUZ_MAX_WIDTH;
  3295. cropcap->bounds.height = BUZ_MAX_HEIGHT;
  3296. cropcap->defrect.top = cropcap->defrect.left = 0;
  3297. cropcap->defrect.width = BUZ_MIN_WIDTH;
  3298. cropcap->defrect.height = BUZ_MIN_HEIGHT;
  3299. cropcap_unlock_and_return:
  3300. mutex_unlock(&zr->resource_lock);
  3301. return res;
  3302. }
  3303. break;
  3304. case VIDIOC_G_CROP:
  3305. {
  3306. struct v4l2_crop *crop = arg;
  3307. int type = crop->type, res = 0;
  3308. dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
  3309. ZR_DEVNAME(zr), crop->type);
  3310. memset(crop, 0, sizeof(*crop));
  3311. crop->type = type;
  3312. mutex_lock(&zr->resource_lock);
  3313. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3314. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3315. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3316. dprintk(1,
  3317. KERN_ERR
  3318. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3319. ZR_DEVNAME(zr));
  3320. res = -EINVAL;
  3321. goto gcrop_unlock_and_return;
  3322. }
  3323. crop->c.top = fh->jpg_settings.img_y;
  3324. crop->c.left = fh->jpg_settings.img_x;
  3325. crop->c.width = fh->jpg_settings.img_width;
  3326. crop->c.height = fh->jpg_settings.img_height;
  3327. gcrop_unlock_and_return:
  3328. mutex_unlock(&zr->resource_lock);
  3329. return res;
  3330. }
  3331. break;
  3332. case VIDIOC_S_CROP:
  3333. {
  3334. struct v4l2_crop *crop = arg;
  3335. int res = 0;
  3336. settings = fh->jpg_settings;
  3337. dprintk(3,
  3338. KERN_ERR
  3339. "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
  3340. ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
  3341. crop->c.width, crop->c.height);
  3342. mutex_lock(&zr->resource_lock);
  3343. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  3344. dprintk(1,
  3345. KERN_ERR
  3346. "%s: VIDIOC_S_CROP - cannot change settings while active\n",
  3347. ZR_DEVNAME(zr));
  3348. res = -EBUSY;
  3349. goto scrop_unlock_and_return;
  3350. }
  3351. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3352. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3353. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3354. dprintk(1,
  3355. KERN_ERR
  3356. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3357. ZR_DEVNAME(zr));
  3358. res = -EINVAL;
  3359. goto scrop_unlock_and_return;
  3360. }
  3361. /* move into a form that we understand */
  3362. settings.img_x = crop->c.left;
  3363. settings.img_y = crop->c.top;
  3364. settings.img_width = crop->c.width;
  3365. settings.img_height = crop->c.height;
  3366. /* check validity */
  3367. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3368. goto scrop_unlock_and_return;
  3369. /* accept */
  3370. fh->jpg_settings = settings;
  3371. scrop_unlock_and_return:
  3372. mutex_unlock(&zr->resource_lock);
  3373. return res;
  3374. }
  3375. break;
  3376. case VIDIOC_G_JPEGCOMP:
  3377. {
  3378. struct v4l2_jpegcompression *params = arg;
  3379. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
  3380. ZR_DEVNAME(zr));
  3381. memset(params, 0, sizeof(*params));
  3382. mutex_lock(&zr->resource_lock);
  3383. params->quality = fh->jpg_settings.jpg_comp.quality;
  3384. params->APPn = fh->jpg_settings.jpg_comp.APPn;
  3385. memcpy(params->APP_data,
  3386. fh->jpg_settings.jpg_comp.APP_data,
  3387. fh->jpg_settings.jpg_comp.APP_len);
  3388. params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  3389. memcpy(params->COM_data,
  3390. fh->jpg_settings.jpg_comp.COM_data,
  3391. fh->jpg_settings.jpg_comp.COM_len);
  3392. params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
  3393. params->jpeg_markers =
  3394. fh->jpg_settings.jpg_comp.jpeg_markers;
  3395. mutex_unlock(&zr->resource_lock);
  3396. return 0;
  3397. }
  3398. break;
  3399. case VIDIOC_S_JPEGCOMP:
  3400. {
  3401. struct v4l2_jpegcompression *params = arg;
  3402. int res = 0;
  3403. settings = fh->jpg_settings;
  3404. dprintk(3,
  3405. KERN_DEBUG
  3406. "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
  3407. ZR_DEVNAME(zr), params->quality, params->APPn,
  3408. params->APP_len, params->COM_len);
  3409. settings.jpg_comp = *params;
  3410. mutex_lock(&zr->resource_lock);
  3411. if (fh->v4l_buffers.active != ZORAN_FREE ||
  3412. fh->jpg_buffers.active != ZORAN_FREE) {
  3413. dprintk(1,
  3414. KERN_WARNING
  3415. "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
  3416. ZR_DEVNAME(zr));
  3417. res = -EBUSY;
  3418. goto sjpegc_unlock_and_return;
  3419. }
  3420. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3421. goto sjpegc_unlock_and_return;
  3422. if (!fh->jpg_buffers.allocated)
  3423. fh->jpg_buffers.buffer_size =
  3424. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  3425. fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
  3426. sjpegc_unlock_and_return:
  3427. mutex_unlock(&zr->resource_lock);
  3428. return 0;
  3429. }
  3430. break;
  3431. case VIDIOC_QUERYSTD: /* why is this useful? */
  3432. {
  3433. v4l2_std_id *std = arg;
  3434. dprintk(3,
  3435. KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
  3436. ZR_DEVNAME(zr), (unsigned long long)*std);
  3437. if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
  3438. *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
  3439. zr->card.norms == 3)) {
  3440. return 0;
  3441. }
  3442. return -EINVAL;
  3443. }
  3444. break;
  3445. case VIDIOC_TRY_FMT:
  3446. {
  3447. struct v4l2_format *fmt = arg;
  3448. int res = 0;
  3449. dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
  3450. ZR_DEVNAME(zr), fmt->type);
  3451. switch (fmt->type) {
  3452. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3453. mutex_lock(&zr->resource_lock);
  3454. if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
  3455. fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
  3456. if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
  3457. fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
  3458. if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
  3459. fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
  3460. if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
  3461. fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
  3462. mutex_unlock(&zr->resource_lock);
  3463. break;
  3464. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  3465. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  3466. if (fmt->fmt.pix.bytesperline > 0)
  3467. return -EINVAL;
  3468. mutex_lock(&zr->resource_lock);
  3469. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  3470. settings = fh->jpg_settings;
  3471. /* we actually need to set 'real' parameters now */
  3472. if ((fmt->fmt.pix.height * 2) >
  3473. BUZ_MAX_HEIGHT)
  3474. settings.TmpDcm = 1;
  3475. else
  3476. settings.TmpDcm = 2;
  3477. settings.decimation = 0;
  3478. if (fmt->fmt.pix.height <=
  3479. fh->jpg_settings.img_height / 2)
  3480. settings.VerDcm = 2;
  3481. else
  3482. settings.VerDcm = 1;
  3483. if (fmt->fmt.pix.width <=
  3484. fh->jpg_settings.img_width / 4)
  3485. settings.HorDcm = 4;
  3486. else if (fmt->fmt.pix.width <=
  3487. fh->jpg_settings.img_width / 2)
  3488. settings.HorDcm = 2;
  3489. else
  3490. settings.HorDcm = 1;
  3491. if (settings.TmpDcm == 1)
  3492. settings.field_per_buff = 2;
  3493. else
  3494. settings.field_per_buff = 1;
  3495. /* check */
  3496. if ((res =
  3497. zoran_check_jpg_settings(zr,
  3498. &settings)))
  3499. goto tryfmt_unlock_and_return;
  3500. /* tell the user what we actually did */
  3501. fmt->fmt.pix.width =
  3502. settings.img_width / settings.HorDcm;
  3503. fmt->fmt.pix.height =
  3504. settings.img_height * 2 /
  3505. (settings.TmpDcm * settings.VerDcm);
  3506. if (settings.TmpDcm == 1)
  3507. fmt->fmt.pix.field =
  3508. (fh->jpg_settings.
  3509. odd_even ? V4L2_FIELD_SEQ_TB :
  3510. V4L2_FIELD_SEQ_BT);
  3511. else
  3512. fmt->fmt.pix.field =
  3513. (fh->jpg_settings.
  3514. odd_even ? V4L2_FIELD_TOP :
  3515. V4L2_FIELD_BOTTOM);
  3516. fmt->fmt.pix.sizeimage =
  3517. zoran_v4l2_calc_bufsize(&settings);
  3518. } else if (fmt->type ==
  3519. V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  3520. int i;
  3521. for (i = 0; i < NUM_FORMATS; i++)
  3522. if (zoran_formats[i].fourcc ==
  3523. fmt->fmt.pix.pixelformat)
  3524. break;
  3525. if (i == NUM_FORMATS) {
  3526. res = -EINVAL;
  3527. goto tryfmt_unlock_and_return;
  3528. }
  3529. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  3530. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  3531. if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
  3532. fmt->fmt.pix.width = BUZ_MIN_WIDTH;
  3533. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  3534. fmt->fmt.pix.height =
  3535. BUZ_MAX_HEIGHT;
  3536. if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
  3537. fmt->fmt.pix.height =
  3538. BUZ_MIN_HEIGHT;
  3539. } else {
  3540. res = -EINVAL;
  3541. goto tryfmt_unlock_and_return;
  3542. }
  3543. tryfmt_unlock_and_return:
  3544. mutex_unlock(&zr->resource_lock);
  3545. return res;
  3546. break;
  3547. default:
  3548. return -EINVAL;
  3549. }
  3550. return 0;
  3551. }
  3552. break;
  3553. default:
  3554. dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
  3555. ZR_DEVNAME(zr), cmd);
  3556. return -ENOIOCTLCMD;
  3557. break;
  3558. }
  3559. return 0;
  3560. }
  3561. static int
  3562. zoran_ioctl (struct inode *inode,
  3563. struct file *file,
  3564. unsigned int cmd,
  3565. unsigned long arg)
  3566. {
  3567. return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
  3568. }
  3569. static unsigned int
  3570. zoran_poll (struct file *file,
  3571. poll_table *wait)
  3572. {
  3573. struct zoran_fh *fh = file->private_data;
  3574. struct zoran *zr = fh->zr;
  3575. int res = 0, frame;
  3576. unsigned long flags;
  3577. /* we should check whether buffers are ready to be synced on
  3578. * (w/o waits - O_NONBLOCK) here
  3579. * if ready for read (sync), return POLLIN|POLLRDNORM,
  3580. * if ready for write (sync), return POLLOUT|POLLWRNORM,
  3581. * if error, return POLLERR,
  3582. * if no buffers queued or so, return POLLNVAL
  3583. */
  3584. mutex_lock(&zr->resource_lock);
  3585. switch (fh->map_mode) {
  3586. case ZORAN_MAP_MODE_RAW:
  3587. poll_wait(file, &zr->v4l_capq, wait);
  3588. frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  3589. spin_lock_irqsave(&zr->spinlock, flags);
  3590. dprintk(3,
  3591. KERN_DEBUG
  3592. "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
  3593. ZR_DEVNAME(zr), __func__,
  3594. "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
  3595. "UPMD"[zr->v4l_buffers.buffer[frame].state],
  3596. zr->v4l_pend_tail, zr->v4l_pend_head);
  3597. /* Process is the one capturing? */
  3598. if (fh->v4l_buffers.active != ZORAN_FREE &&
  3599. /* Buffer ready to DQBUF? */
  3600. zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
  3601. res = POLLIN | POLLRDNORM;
  3602. spin_unlock_irqrestore(&zr->spinlock, flags);
  3603. break;
  3604. case ZORAN_MAP_MODE_JPG_REC:
  3605. case ZORAN_MAP_MODE_JPG_PLAY:
  3606. poll_wait(file, &zr->jpg_capq, wait);
  3607. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  3608. spin_lock_irqsave(&zr->spinlock, flags);
  3609. dprintk(3,
  3610. KERN_DEBUG
  3611. "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
  3612. ZR_DEVNAME(zr), __func__,
  3613. "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
  3614. "UPMD"[zr->jpg_buffers.buffer[frame].state],
  3615. zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
  3616. if (fh->jpg_buffers.active != ZORAN_FREE &&
  3617. zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
  3618. if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
  3619. res = POLLIN | POLLRDNORM;
  3620. else
  3621. res = POLLOUT | POLLWRNORM;
  3622. }
  3623. spin_unlock_irqrestore(&zr->spinlock, flags);
  3624. break;
  3625. default:
  3626. dprintk(1,
  3627. KERN_ERR
  3628. "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
  3629. ZR_DEVNAME(zr), fh->map_mode);
  3630. res = POLLNVAL;
  3631. }
  3632. mutex_unlock(&zr->resource_lock);
  3633. return res;
  3634. }
  3635. /*
  3636. * This maps the buffers to user space.
  3637. *
  3638. * Depending on the state of fh->map_mode
  3639. * the V4L or the MJPEG buffers are mapped
  3640. * per buffer or all together
  3641. *
  3642. * Note that we need to connect to some
  3643. * unmap signal event to unmap the de-allocate
  3644. * the buffer accordingly (zoran_vm_close())
  3645. */
  3646. static void
  3647. zoran_vm_open (struct vm_area_struct *vma)
  3648. {
  3649. struct zoran_mapping *map = vma->vm_private_data;
  3650. map->count++;
  3651. }
  3652. static void
  3653. zoran_vm_close (struct vm_area_struct *vma)
  3654. {
  3655. struct zoran_mapping *map = vma->vm_private_data;
  3656. struct file *file = map->file;
  3657. struct zoran_fh *fh = file->private_data;
  3658. struct zoran *zr = fh->zr;
  3659. int i;
  3660. map->count--;
  3661. if (map->count == 0) {
  3662. switch (fh->map_mode) {
  3663. case ZORAN_MAP_MODE_JPG_REC:
  3664. case ZORAN_MAP_MODE_JPG_PLAY:
  3665. dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
  3666. ZR_DEVNAME(zr));
  3667. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  3668. if (fh->jpg_buffers.buffer[i].map == map) {
  3669. fh->jpg_buffers.buffer[i].map =
  3670. NULL;
  3671. }
  3672. }
  3673. kfree(map);
  3674. for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
  3675. if (fh->jpg_buffers.buffer[i].map)
  3676. break;
  3677. if (i == fh->jpg_buffers.num_buffers) {
  3678. mutex_lock(&zr->resource_lock);
  3679. if (fh->jpg_buffers.active != ZORAN_FREE) {
  3680. jpg_qbuf(file, -1, zr->codec_mode);
  3681. zr->jpg_buffers.allocated = 0;
  3682. zr->jpg_buffers.active =
  3683. fh->jpg_buffers.active =
  3684. ZORAN_FREE;
  3685. }
  3686. //jpg_fbuffer_free(file);
  3687. fh->jpg_buffers.allocated = 0;
  3688. fh->jpg_buffers.ready_to_be_freed = 1;
  3689. mutex_unlock(&zr->resource_lock);
  3690. }
  3691. break;
  3692. case ZORAN_MAP_MODE_RAW:
  3693. dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
  3694. ZR_DEVNAME(zr));
  3695. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  3696. if (fh->v4l_buffers.buffer[i].map == map) {
  3697. /* unqueue/unmap */
  3698. fh->v4l_buffers.buffer[i].map =
  3699. NULL;
  3700. }
  3701. }
  3702. kfree(map);
  3703. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  3704. if (fh->v4l_buffers.buffer[i].map)
  3705. break;
  3706. if (i == fh->v4l_buffers.num_buffers) {
  3707. mutex_lock(&zr->resource_lock);
  3708. if (fh->v4l_buffers.active != ZORAN_FREE) {
  3709. unsigned long flags;
  3710. spin_lock_irqsave(&zr->spinlock, flags);
  3711. zr36057_set_memgrab(zr, 0);
  3712. zr->v4l_buffers.allocated = 0;
  3713. zr->v4l_buffers.active =
  3714. fh->v4l_buffers.active =
  3715. ZORAN_FREE;
  3716. spin_unlock_irqrestore(&zr->spinlock, flags);
  3717. }
  3718. //v4l_fbuffer_free(file);
  3719. fh->v4l_buffers.allocated = 0;
  3720. fh->v4l_buffers.ready_to_be_freed = 1;
  3721. mutex_unlock(&zr->resource_lock);
  3722. }
  3723. break;
  3724. default:
  3725. printk(KERN_ERR
  3726. "%s: munmap() - internal error - unknown map mode %d\n",
  3727. ZR_DEVNAME(zr), fh->map_mode);
  3728. break;
  3729. }
  3730. }
  3731. }
  3732. static struct vm_operations_struct zoran_vm_ops = {
  3733. .open = zoran_vm_open,
  3734. .close = zoran_vm_close,
  3735. };
  3736. static int
  3737. zoran_mmap (struct file *file,
  3738. struct vm_area_struct *vma)
  3739. {
  3740. struct zoran_fh *fh = file->private_data;
  3741. struct zoran *zr = fh->zr;
  3742. unsigned long size = (vma->vm_end - vma->vm_start);
  3743. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  3744. int i, j;
  3745. unsigned long page, start = vma->vm_start, todo, pos, fraglen;
  3746. int first, last;
  3747. struct zoran_mapping *map;
  3748. int res = 0;
  3749. dprintk(3,
  3750. KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
  3751. ZR_DEVNAME(zr),
  3752. fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
  3753. vma->vm_start, vma->vm_end, size);
  3754. if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
  3755. !(vma->vm_flags & VM_WRITE)) {
  3756. dprintk(1,
  3757. KERN_ERR
  3758. "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
  3759. ZR_DEVNAME(zr));
  3760. return -EINVAL;
  3761. }
  3762. switch (fh->map_mode) {
  3763. case ZORAN_MAP_MODE_JPG_REC:
  3764. case ZORAN_MAP_MODE_JPG_PLAY:
  3765. /* lock */
  3766. mutex_lock(&zr->resource_lock);
  3767. /* Map the MJPEG buffers */
  3768. if (!fh->jpg_buffers.allocated) {
  3769. dprintk(1,
  3770. KERN_ERR
  3771. "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
  3772. ZR_DEVNAME(zr));
  3773. res = -ENOMEM;
  3774. goto jpg_mmap_unlock_and_return;
  3775. }
  3776. first = offset / fh->jpg_buffers.buffer_size;
  3777. last = first - 1 + size / fh->jpg_buffers.buffer_size;
  3778. if (offset % fh->jpg_buffers.buffer_size != 0 ||
  3779. size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
  3780. last < 0 || first >= fh->jpg_buffers.num_buffers ||
  3781. last >= fh->jpg_buffers.num_buffers) {
  3782. dprintk(1,
  3783. KERN_ERR
  3784. "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3785. ZR_DEVNAME(zr), offset, size,
  3786. fh->jpg_buffers.buffer_size,
  3787. fh->jpg_buffers.num_buffers);
  3788. res = -EINVAL;
  3789. goto jpg_mmap_unlock_and_return;
  3790. }
  3791. for (i = first; i <= last; i++) {
  3792. if (fh->jpg_buffers.buffer[i].map) {
  3793. dprintk(1,
  3794. KERN_ERR
  3795. "%s: mmap(MJPEG) - buffer %d already mapped\n",
  3796. ZR_DEVNAME(zr), i);
  3797. res = -EBUSY;
  3798. goto jpg_mmap_unlock_and_return;
  3799. }
  3800. }
  3801. /* map these buffers (v4l_buffers[i]) */
  3802. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3803. if (!map) {
  3804. res = -ENOMEM;
  3805. goto jpg_mmap_unlock_and_return;
  3806. }
  3807. map->file = file;
  3808. map->count = 1;
  3809. vma->vm_ops = &zoran_vm_ops;
  3810. vma->vm_flags |= VM_DONTEXPAND;
  3811. vma->vm_private_data = map;
  3812. for (i = first; i <= last; i++) {
  3813. for (j = 0;
  3814. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  3815. j++) {
  3816. fraglen =
  3817. (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3818. frag_tab[2 * j + 1]) & ~1) << 1;
  3819. todo = size;
  3820. if (todo > fraglen)
  3821. todo = fraglen;
  3822. pos =
  3823. le32_to_cpu(fh->jpg_buffers.
  3824. buffer[i].frag_tab[2 * j]);
  3825. /* should just be pos on i386 */
  3826. page = virt_to_phys(bus_to_virt(pos))
  3827. >> PAGE_SHIFT;
  3828. if (remap_pfn_range(vma, start, page,
  3829. todo, PAGE_SHARED)) {
  3830. dprintk(1,
  3831. KERN_ERR
  3832. "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
  3833. ZR_DEVNAME(zr));
  3834. res = -EAGAIN;
  3835. goto jpg_mmap_unlock_and_return;
  3836. }
  3837. size -= todo;
  3838. start += todo;
  3839. if (size == 0)
  3840. break;
  3841. if (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3842. frag_tab[2 * j + 1]) & 1)
  3843. break; /* was last fragment */
  3844. }
  3845. fh->jpg_buffers.buffer[i].map = map;
  3846. if (size == 0)
  3847. break;
  3848. }
  3849. jpg_mmap_unlock_and_return:
  3850. mutex_unlock(&zr->resource_lock);
  3851. break;
  3852. case ZORAN_MAP_MODE_RAW:
  3853. mutex_lock(&zr->resource_lock);
  3854. /* Map the V4L buffers */
  3855. if (!fh->v4l_buffers.allocated) {
  3856. dprintk(1,
  3857. KERN_ERR
  3858. "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
  3859. ZR_DEVNAME(zr));
  3860. res = -ENOMEM;
  3861. goto v4l_mmap_unlock_and_return;
  3862. }
  3863. first = offset / fh->v4l_buffers.buffer_size;
  3864. last = first - 1 + size / fh->v4l_buffers.buffer_size;
  3865. if (offset % fh->v4l_buffers.buffer_size != 0 ||
  3866. size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
  3867. last < 0 || first >= fh->v4l_buffers.num_buffers ||
  3868. last >= fh->v4l_buffers.buffer_size) {
  3869. dprintk(1,
  3870. KERN_ERR
  3871. "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3872. ZR_DEVNAME(zr), offset, size,
  3873. fh->v4l_buffers.buffer_size,
  3874. fh->v4l_buffers.num_buffers);
  3875. res = -EINVAL;
  3876. goto v4l_mmap_unlock_and_return;
  3877. }
  3878. for (i = first; i <= last; i++) {
  3879. if (fh->v4l_buffers.buffer[i].map) {
  3880. dprintk(1,
  3881. KERN_ERR
  3882. "%s: mmap(V4L) - buffer %d already mapped\n",
  3883. ZR_DEVNAME(zr), i);
  3884. res = -EBUSY;
  3885. goto v4l_mmap_unlock_and_return;
  3886. }
  3887. }
  3888. /* map these buffers (v4l_buffers[i]) */
  3889. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3890. if (!map) {
  3891. res = -ENOMEM;
  3892. goto v4l_mmap_unlock_and_return;
  3893. }
  3894. map->file = file;
  3895. map->count = 1;
  3896. vma->vm_ops = &zoran_vm_ops;
  3897. vma->vm_flags |= VM_DONTEXPAND;
  3898. vma->vm_private_data = map;
  3899. for (i = first; i <= last; i++) {
  3900. todo = size;
  3901. if (todo > fh->v4l_buffers.buffer_size)
  3902. todo = fh->v4l_buffers.buffer_size;
  3903. page = fh->v4l_buffers.buffer[i].fbuffer_phys;
  3904. if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
  3905. todo, PAGE_SHARED)) {
  3906. dprintk(1,
  3907. KERN_ERR
  3908. "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
  3909. ZR_DEVNAME(zr));
  3910. res = -EAGAIN;
  3911. goto v4l_mmap_unlock_and_return;
  3912. }
  3913. size -= todo;
  3914. start += todo;
  3915. fh->v4l_buffers.buffer[i].map = map;
  3916. if (size == 0)
  3917. break;
  3918. }
  3919. v4l_mmap_unlock_and_return:
  3920. mutex_unlock(&zr->resource_lock);
  3921. break;
  3922. default:
  3923. dprintk(1,
  3924. KERN_ERR
  3925. "%s: zoran_mmap() - internal error - unknown map mode %d\n",
  3926. ZR_DEVNAME(zr), fh->map_mode);
  3927. break;
  3928. }
  3929. return 0;
  3930. }
  3931. static const struct file_operations zoran_fops = {
  3932. .owner = THIS_MODULE,
  3933. .open = zoran_open,
  3934. .release = zoran_close,
  3935. .ioctl = zoran_ioctl,
  3936. #ifdef CONFIG_COMPAT
  3937. .compat_ioctl = v4l_compat_ioctl32,
  3938. #endif
  3939. .llseek = no_llseek,
  3940. .read = zoran_read,
  3941. .write = zoran_write,
  3942. .mmap = zoran_mmap,
  3943. .poll = zoran_poll,
  3944. };
  3945. struct video_device zoran_template __devinitdata = {
  3946. .name = ZORAN_NAME,
  3947. .type = ZORAN_VID_TYPE,
  3948. .type2 = ZORAN_V4L2_VID_FLAGS,
  3949. .fops = &zoran_fops,
  3950. .release = &zoran_vdev_release,
  3951. .minor = -1
  3952. };