zoran_driver.c 113 KB

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