zoran_driver.c 114 KB

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