zoran_driver.c 114 KB

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