zoran_driver.c 115 KB

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