zoran_driver.c 114 KB

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