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