zoran_driver.c 114 KB

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