zoran_driver.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683
  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/interrupt.h>
  55. #include <linux/i2c.h>
  56. #include <linux/i2c-algo-bit.h>
  57. #include <linux/spinlock.h>
  58. #define MAP_NR(x) virt_to_page(x)
  59. #define ZORAN_VID_TYPE ( \
  60. VID_TYPE_CAPTURE | \
  61. VID_TYPE_OVERLAY | \
  62. VID_TYPE_CLIPPING | \
  63. VID_TYPE_FRAMERAM | \
  64. VID_TYPE_SCALES | \
  65. VID_TYPE_MJPEG_DECODER | \
  66. VID_TYPE_MJPEG_ENCODER \
  67. )
  68. #include <linux/videodev.h>
  69. #include <media/v4l2-common.h>
  70. #include <media/v4l2-ioctl.h>
  71. #include "videocodec.h"
  72. #include <asm/byteorder.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. /* we declare some card type definitions here, they mean
  83. * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
  84. #define ZORAN_V4L2_VID_FLAGS ( \
  85. V4L2_CAP_STREAMING |\
  86. V4L2_CAP_VIDEO_CAPTURE |\
  87. V4L2_CAP_VIDEO_OUTPUT |\
  88. V4L2_CAP_VIDEO_OVERLAY \
  89. )
  90. #if defined(CONFIG_VIDEO_V4L1_COMPAT)
  91. #define ZFMT(pal, fcc, cs) \
  92. .palette = (pal), .fourcc = (fcc), .colorspace = (cs)
  93. #else
  94. #define ZFMT(pal, fcc, cs) \
  95. .fourcc = (fcc), .colorspace = (cs)
  96. #endif
  97. const struct zoran_format zoran_formats[] = {
  98. {
  99. .name = "15-bit RGB LE",
  100. ZFMT(VIDEO_PALETTE_RGB555,
  101. V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
  102. .depth = 15,
  103. .flags = ZORAN_FORMAT_CAPTURE |
  104. ZORAN_FORMAT_OVERLAY,
  105. .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
  106. ZR36057_VFESPFR_LittleEndian,
  107. }, {
  108. .name = "15-bit RGB BE",
  109. ZFMT(-1,
  110. V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
  111. .depth = 15,
  112. .flags = ZORAN_FORMAT_CAPTURE |
  113. ZORAN_FORMAT_OVERLAY,
  114. .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
  115. }, {
  116. .name = "16-bit RGB LE",
  117. ZFMT(VIDEO_PALETTE_RGB565,
  118. V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
  119. .depth = 16,
  120. .flags = ZORAN_FORMAT_CAPTURE |
  121. ZORAN_FORMAT_OVERLAY,
  122. .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
  123. ZR36057_VFESPFR_LittleEndian,
  124. }, {
  125. .name = "16-bit RGB BE",
  126. ZFMT(-1,
  127. V4L2_PIX_FMT_RGB565X, V4L2_COLORSPACE_SRGB),
  128. .depth = 16,
  129. .flags = ZORAN_FORMAT_CAPTURE |
  130. ZORAN_FORMAT_OVERLAY,
  131. .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
  132. }, {
  133. .name = "24-bit RGB",
  134. ZFMT(VIDEO_PALETTE_RGB24,
  135. V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
  136. .depth = 24,
  137. .flags = ZORAN_FORMAT_CAPTURE |
  138. ZORAN_FORMAT_OVERLAY,
  139. .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
  140. }, {
  141. .name = "32-bit RGB LE",
  142. ZFMT(VIDEO_PALETTE_RGB32,
  143. V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
  144. .depth = 32,
  145. .flags = ZORAN_FORMAT_CAPTURE |
  146. ZORAN_FORMAT_OVERLAY,
  147. .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
  148. }, {
  149. .name = "32-bit RGB BE",
  150. ZFMT(-1,
  151. V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
  152. .depth = 32,
  153. .flags = ZORAN_FORMAT_CAPTURE |
  154. ZORAN_FORMAT_OVERLAY,
  155. .vfespfr = ZR36057_VFESPFR_RGB888,
  156. }, {
  157. .name = "4:2:2, packed, YUYV",
  158. ZFMT(VIDEO_PALETTE_YUV422,
  159. V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
  160. .depth = 16,
  161. .flags = ZORAN_FORMAT_CAPTURE |
  162. ZORAN_FORMAT_OVERLAY,
  163. .vfespfr = ZR36057_VFESPFR_YUV422,
  164. }, {
  165. .name = "4:2:2, packed, UYVY",
  166. ZFMT(VIDEO_PALETTE_UYVY,
  167. V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
  168. .depth = 16,
  169. .flags = ZORAN_FORMAT_CAPTURE |
  170. ZORAN_FORMAT_OVERLAY,
  171. .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
  172. }, {
  173. .name = "Hardware-encoded Motion-JPEG",
  174. ZFMT(-1,
  175. V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
  176. .depth = 0,
  177. .flags = ZORAN_FORMAT_CAPTURE |
  178. ZORAN_FORMAT_PLAYBACK |
  179. ZORAN_FORMAT_COMPRESSED,
  180. }
  181. };
  182. #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
  183. static int lock_norm; /* 0 = default 1 = Don't change TV standard (norm) */
  184. module_param(lock_norm, int, 0644);
  185. MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
  186. /* small helper function for calculating buffersizes for v4l2
  187. * we calculate the nearest higher power-of-two, which
  188. * will be the recommended buffersize */
  189. static __u32
  190. zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
  191. {
  192. __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
  193. __u32 num = (1024 * 512) / (div);
  194. __u32 result = 2;
  195. num--;
  196. while (num) {
  197. num >>= 1;
  198. result <<= 1;
  199. }
  200. if (result > jpg_bufsize)
  201. return jpg_bufsize;
  202. if (result < 8192)
  203. return 8192;
  204. return result;
  205. }
  206. /* forward references */
  207. static void v4l_fbuffer_free(struct file *file);
  208. static void jpg_fbuffer_free(struct file *file);
  209. /*
  210. * Allocate the V4L grab buffers
  211. *
  212. * These have to be pysically contiguous.
  213. */
  214. static int
  215. v4l_fbuffer_alloc (struct file *file)
  216. {
  217. struct zoran_fh *fh = file->private_data;
  218. struct zoran *zr = fh->zr;
  219. int i, off;
  220. unsigned char *mem;
  221. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  222. if (fh->v4l_buffers.buffer[i].fbuffer)
  223. dprintk(2,
  224. KERN_WARNING
  225. "%s: v4l_fbuffer_alloc() - buffer %d already allocated!?\n",
  226. ZR_DEVNAME(zr), i);
  227. //udelay(20);
  228. mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
  229. if (!mem) {
  230. dprintk(1,
  231. KERN_ERR
  232. "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
  233. ZR_DEVNAME(zr), i);
  234. v4l_fbuffer_free(file);
  235. return -ENOBUFS;
  236. }
  237. fh->v4l_buffers.buffer[i].fbuffer = mem;
  238. fh->v4l_buffers.buffer[i].fbuffer_phys =
  239. virt_to_phys(mem);
  240. fh->v4l_buffers.buffer[i].fbuffer_bus =
  241. virt_to_bus(mem);
  242. for (off = 0; off < fh->v4l_buffers.buffer_size;
  243. off += PAGE_SIZE)
  244. SetPageReserved(MAP_NR(mem + off));
  245. dprintk(4,
  246. KERN_INFO
  247. "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
  248. ZR_DEVNAME(zr), i, (unsigned long) mem,
  249. virt_to_bus(mem));
  250. }
  251. fh->v4l_buffers.allocated = 1;
  252. return 0;
  253. }
  254. /* free the V4L grab buffers */
  255. static void
  256. v4l_fbuffer_free (struct file *file)
  257. {
  258. struct zoran_fh *fh = file->private_data;
  259. struct zoran *zr = fh->zr;
  260. int i, off;
  261. unsigned char *mem;
  262. dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
  263. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  264. if (!fh->v4l_buffers.buffer[i].fbuffer)
  265. continue;
  266. mem = fh->v4l_buffers.buffer[i].fbuffer;
  267. for (off = 0; off < fh->v4l_buffers.buffer_size;
  268. off += PAGE_SIZE)
  269. ClearPageReserved(MAP_NR(mem + off));
  270. kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
  271. fh->v4l_buffers.buffer[i].fbuffer = NULL;
  272. }
  273. fh->v4l_buffers.allocated = 0;
  274. }
  275. /*
  276. * Allocate the MJPEG grab buffers.
  277. *
  278. * If a Natoma chipset is present and this is a revision 1 zr36057,
  279. * each MJPEG buffer needs to be physically contiguous.
  280. * (RJ: This statement is from Dave Perks' original driver,
  281. * I could never check it because I have a zr36067)
  282. *
  283. * RJ: The contents grab buffers needs never be accessed in the driver.
  284. * Therefore there is no need to allocate them with vmalloc in order
  285. * to get a contiguous virtual memory space.
  286. * I don't understand why many other drivers first allocate them with
  287. * vmalloc (which uses internally also get_zeroed_page, but delivers you
  288. * virtual addresses) and then again have to make a lot of efforts
  289. * to get the physical address.
  290. *
  291. * Ben Capper:
  292. * On big-endian architectures (such as ppc) some extra steps
  293. * are needed. When reading and writing to the stat_com array
  294. * and fragment buffers, the device expects to see little-
  295. * endian values. The use of cpu_to_le32() and le32_to_cpu()
  296. * in this function (and one or two others in zoran_device.c)
  297. * ensure that these values are always stored in little-endian
  298. * form, regardless of architecture. The zr36057 does Very Bad
  299. * Things on big endian architectures if the stat_com array
  300. * and fragment buffers are not little-endian.
  301. */
  302. static int
  303. jpg_fbuffer_alloc (struct file *file)
  304. {
  305. struct zoran_fh *fh = file->private_data;
  306. struct zoran *zr = fh->zr;
  307. int i, j, off;
  308. unsigned long mem;
  309. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  310. if (fh->jpg_buffers.buffer[i].frag_tab)
  311. dprintk(2,
  312. KERN_WARNING
  313. "%s: jpg_fbuffer_alloc() - buffer %d already allocated!?\n",
  314. ZR_DEVNAME(zr), i);
  315. /* Allocate fragment table for this buffer */
  316. mem = get_zeroed_page(GFP_KERNEL);
  317. if (mem == 0) {
  318. dprintk(1,
  319. KERN_ERR
  320. "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
  321. ZR_DEVNAME(zr), i);
  322. jpg_fbuffer_free(file);
  323. return -ENOBUFS;
  324. }
  325. fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
  326. fh->jpg_buffers.buffer[i].frag_tab_bus =
  327. virt_to_bus((void *) mem);
  328. //if (alloc_contig) {
  329. if (fh->jpg_buffers.need_contiguous) {
  330. mem =
  331. (unsigned long) kmalloc(fh->jpg_buffers.
  332. buffer_size,
  333. GFP_KERNEL);
  334. if (mem == 0) {
  335. dprintk(1,
  336. KERN_ERR
  337. "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
  338. ZR_DEVNAME(zr), i);
  339. jpg_fbuffer_free(file);
  340. return -ENOBUFS;
  341. }
  342. fh->jpg_buffers.buffer[i].frag_tab[0] =
  343. cpu_to_le32(virt_to_bus((void *) mem));
  344. fh->jpg_buffers.buffer[i].frag_tab[1] =
  345. cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
  346. for (off = 0; off < fh->jpg_buffers.buffer_size;
  347. off += PAGE_SIZE)
  348. SetPageReserved(MAP_NR(mem + off));
  349. } else {
  350. /* jpg_bufsize is already page aligned */
  351. for (j = 0;
  352. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  353. j++) {
  354. mem = get_zeroed_page(GFP_KERNEL);
  355. if (mem == 0) {
  356. dprintk(1,
  357. KERN_ERR
  358. "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
  359. ZR_DEVNAME(zr), i);
  360. jpg_fbuffer_free(file);
  361. return -ENOBUFS;
  362. }
  363. fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
  364. cpu_to_le32(virt_to_bus((void *) mem));
  365. fh->jpg_buffers.buffer[i].frag_tab[2 * j +
  366. 1] =
  367. cpu_to_le32((PAGE_SIZE / 4) << 1);
  368. SetPageReserved(MAP_NR(mem));
  369. }
  370. fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
  371. }
  372. }
  373. dprintk(4,
  374. KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
  375. ZR_DEVNAME(zr),
  376. (fh->jpg_buffers.num_buffers *
  377. fh->jpg_buffers.buffer_size) >> 10);
  378. fh->jpg_buffers.allocated = 1;
  379. return 0;
  380. }
  381. /* free the MJPEG grab buffers */
  382. static void
  383. jpg_fbuffer_free (struct file *file)
  384. {
  385. struct zoran_fh *fh = file->private_data;
  386. struct zoran *zr = fh->zr;
  387. int i, j, off;
  388. unsigned char *mem;
  389. dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
  390. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  391. if (!fh->jpg_buffers.buffer[i].frag_tab)
  392. continue;
  393. //if (alloc_contig) {
  394. if (fh->jpg_buffers.need_contiguous) {
  395. if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
  396. mem = (unsigned char *) bus_to_virt(le32_to_cpu(
  397. fh->jpg_buffers.buffer[i].frag_tab[0]));
  398. for (off = 0;
  399. off < fh->jpg_buffers.buffer_size;
  400. off += PAGE_SIZE)
  401. ClearPageReserved(MAP_NR
  402. (mem + off));
  403. kfree(mem);
  404. fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
  405. fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
  406. }
  407. } else {
  408. for (j = 0;
  409. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  410. j++) {
  411. if (!fh->jpg_buffers.buffer[i].
  412. frag_tab[2 * j])
  413. break;
  414. ClearPageReserved(MAP_NR
  415. (bus_to_virt
  416. (le32_to_cpu
  417. (fh->jpg_buffers.
  418. buffer[i].frag_tab[2 *
  419. j]))));
  420. free_page((unsigned long)
  421. bus_to_virt
  422. (le32_to_cpu
  423. (fh->jpg_buffers.
  424. buffer[i].
  425. frag_tab[2 * j])));
  426. fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
  427. 0;
  428. fh->jpg_buffers.buffer[i].frag_tab[2 * j +
  429. 1] = 0;
  430. }
  431. }
  432. free_page((unsigned long) fh->jpg_buffers.buffer[i].
  433. frag_tab);
  434. fh->jpg_buffers.buffer[i].frag_tab = NULL;
  435. }
  436. fh->jpg_buffers.allocated = 0;
  437. }
  438. /*
  439. * V4L Buffer grabbing
  440. */
  441. static int
  442. zoran_v4l_set_format (struct file *file,
  443. int width,
  444. int height,
  445. const struct zoran_format *format)
  446. {
  447. struct zoran_fh *fh = file->private_data;
  448. struct zoran *zr = fh->zr;
  449. int bpp;
  450. /* Check size and format of the grab wanted */
  451. if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
  452. height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
  453. dprintk(1,
  454. KERN_ERR
  455. "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
  456. ZR_DEVNAME(zr), width, height);
  457. return -EINVAL;
  458. }
  459. bpp = (format->depth + 7) / 8;
  460. /* Check against available buffer size */
  461. if (height * width * bpp > fh->v4l_buffers.buffer_size) {
  462. dprintk(1,
  463. KERN_ERR
  464. "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
  465. ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
  466. return -EINVAL;
  467. }
  468. /* The video front end needs 4-byte alinged line sizes */
  469. if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
  470. dprintk(1,
  471. KERN_ERR
  472. "%s: v4l_set_format() - wrong frame alignment\n",
  473. ZR_DEVNAME(zr));
  474. return -EINVAL;
  475. }
  476. fh->v4l_settings.width = width;
  477. fh->v4l_settings.height = height;
  478. fh->v4l_settings.format = format;
  479. fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
  480. return 0;
  481. }
  482. static int
  483. zoran_v4l_queue_frame (struct file *file,
  484. int num)
  485. {
  486. struct zoran_fh *fh = file->private_data;
  487. struct zoran *zr = fh->zr;
  488. unsigned long flags;
  489. int res = 0;
  490. if (!fh->v4l_buffers.allocated) {
  491. dprintk(1,
  492. KERN_ERR
  493. "%s: v4l_queue_frame() - buffers not yet allocated\n",
  494. ZR_DEVNAME(zr));
  495. res = -ENOMEM;
  496. }
  497. /* No grabbing outside the buffer range! */
  498. if (num >= fh->v4l_buffers.num_buffers || num < 0) {
  499. dprintk(1,
  500. KERN_ERR
  501. "%s: v4l_queue_frame() - buffer %d is out of range\n",
  502. ZR_DEVNAME(zr), num);
  503. res = -EINVAL;
  504. }
  505. spin_lock_irqsave(&zr->spinlock, flags);
  506. if (fh->v4l_buffers.active == ZORAN_FREE) {
  507. if (zr->v4l_buffers.active == ZORAN_FREE) {
  508. zr->v4l_buffers = fh->v4l_buffers;
  509. fh->v4l_buffers.active = ZORAN_ACTIVE;
  510. } else {
  511. dprintk(1,
  512. KERN_ERR
  513. "%s: v4l_queue_frame() - another session is already capturing\n",
  514. ZR_DEVNAME(zr));
  515. res = -EBUSY;
  516. }
  517. }
  518. /* make sure a grab isn't going on currently with this buffer */
  519. if (!res) {
  520. switch (zr->v4l_buffers.buffer[num].state) {
  521. default:
  522. case BUZ_STATE_PEND:
  523. if (zr->v4l_buffers.active == ZORAN_FREE) {
  524. fh->v4l_buffers.active = ZORAN_FREE;
  525. zr->v4l_buffers.allocated = 0;
  526. }
  527. res = -EBUSY; /* what are you doing? */
  528. break;
  529. case BUZ_STATE_DONE:
  530. dprintk(2,
  531. KERN_WARNING
  532. "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
  533. ZR_DEVNAME(zr), num);
  534. case BUZ_STATE_USER:
  535. /* since there is at least one unused buffer there's room for at least
  536. * one more pend[] entry */
  537. zr->v4l_pend[zr->v4l_pend_head++ &
  538. V4L_MASK_FRAME] = num;
  539. zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
  540. zr->v4l_buffers.buffer[num].bs.length =
  541. fh->v4l_settings.bytesperline *
  542. zr->v4l_settings.height;
  543. fh->v4l_buffers.buffer[num] =
  544. zr->v4l_buffers.buffer[num];
  545. break;
  546. }
  547. }
  548. spin_unlock_irqrestore(&zr->spinlock, flags);
  549. if (!res && zr->v4l_buffers.active == ZORAN_FREE)
  550. zr->v4l_buffers.active = fh->v4l_buffers.active;
  551. return res;
  552. }
  553. /*
  554. * Sync on a V4L buffer
  555. */
  556. static int
  557. v4l_sync (struct file *file,
  558. int frame)
  559. {
  560. struct zoran_fh *fh = file->private_data;
  561. struct zoran *zr = fh->zr;
  562. unsigned long flags;
  563. if (fh->v4l_buffers.active == ZORAN_FREE) {
  564. dprintk(1,
  565. KERN_ERR
  566. "%s: v4l_sync() - no grab active for this session\n",
  567. ZR_DEVNAME(zr));
  568. return -EINVAL;
  569. }
  570. /* check passed-in frame number */
  571. if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
  572. dprintk(1,
  573. KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
  574. ZR_DEVNAME(zr), frame);
  575. return -EINVAL;
  576. }
  577. /* Check if is buffer was queued at all */
  578. if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
  579. dprintk(1,
  580. KERN_ERR
  581. "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
  582. ZR_DEVNAME(zr));
  583. return -EPROTO;
  584. }
  585. /* wait on this buffer to get ready */
  586. if (!wait_event_interruptible_timeout(zr->v4l_capq,
  587. (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
  588. 10*HZ))
  589. return -ETIME;
  590. if (signal_pending(current))
  591. return -ERESTARTSYS;
  592. /* buffer should now be in BUZ_STATE_DONE */
  593. if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
  594. dprintk(2,
  595. KERN_ERR "%s: v4l_sync() - internal state error\n",
  596. ZR_DEVNAME(zr));
  597. zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
  598. fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
  599. spin_lock_irqsave(&zr->spinlock, flags);
  600. /* Check if streaming capture has finished */
  601. if (zr->v4l_pend_tail == zr->v4l_pend_head) {
  602. zr36057_set_memgrab(zr, 0);
  603. if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
  604. fh->v4l_buffers.active = zr->v4l_buffers.active =
  605. ZORAN_FREE;
  606. zr->v4l_buffers.allocated = 0;
  607. }
  608. }
  609. spin_unlock_irqrestore(&zr->spinlock, flags);
  610. return 0;
  611. }
  612. /*
  613. * Queue a MJPEG buffer for capture/playback
  614. */
  615. static int
  616. zoran_jpg_queue_frame (struct file *file,
  617. int num,
  618. enum zoran_codec_mode mode)
  619. {
  620. struct zoran_fh *fh = file->private_data;
  621. struct zoran *zr = fh->zr;
  622. unsigned long flags;
  623. int res = 0;
  624. /* Check if buffers are allocated */
  625. if (!fh->jpg_buffers.allocated) {
  626. dprintk(1,
  627. KERN_ERR
  628. "%s: jpg_queue_frame() - buffers not yet allocated\n",
  629. ZR_DEVNAME(zr));
  630. return -ENOMEM;
  631. }
  632. /* No grabbing outside the buffer range! */
  633. if (num >= fh->jpg_buffers.num_buffers || num < 0) {
  634. dprintk(1,
  635. KERN_ERR
  636. "%s: jpg_queue_frame() - buffer %d out of range\n",
  637. ZR_DEVNAME(zr), num);
  638. return -EINVAL;
  639. }
  640. /* what is the codec mode right now? */
  641. if (zr->codec_mode == BUZ_MODE_IDLE) {
  642. zr->jpg_settings = fh->jpg_settings;
  643. } else if (zr->codec_mode != mode) {
  644. /* wrong codec mode active - invalid */
  645. dprintk(1,
  646. KERN_ERR
  647. "%s: jpg_queue_frame() - codec in wrong mode\n",
  648. ZR_DEVNAME(zr));
  649. return -EINVAL;
  650. }
  651. if (fh->jpg_buffers.active == ZORAN_FREE) {
  652. if (zr->jpg_buffers.active == ZORAN_FREE) {
  653. zr->jpg_buffers = fh->jpg_buffers;
  654. fh->jpg_buffers.active = ZORAN_ACTIVE;
  655. } else {
  656. dprintk(1,
  657. KERN_ERR
  658. "%s: jpg_queue_frame() - another session is already capturing\n",
  659. ZR_DEVNAME(zr));
  660. res = -EBUSY;
  661. }
  662. }
  663. if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
  664. /* Ok load up the jpeg codec */
  665. zr36057_enable_jpg(zr, mode);
  666. }
  667. spin_lock_irqsave(&zr->spinlock, flags);
  668. if (!res) {
  669. switch (zr->jpg_buffers.buffer[num].state) {
  670. case BUZ_STATE_DONE:
  671. dprintk(2,
  672. KERN_WARNING
  673. "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
  674. ZR_DEVNAME(zr));
  675. case BUZ_STATE_USER:
  676. /* since there is at least one unused buffer there's room for at
  677. *least one more pend[] entry */
  678. zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
  679. num;
  680. zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
  681. fh->jpg_buffers.buffer[num] =
  682. zr->jpg_buffers.buffer[num];
  683. zoran_feed_stat_com(zr);
  684. break;
  685. default:
  686. case BUZ_STATE_DMA:
  687. case BUZ_STATE_PEND:
  688. if (zr->jpg_buffers.active == ZORAN_FREE) {
  689. fh->jpg_buffers.active = ZORAN_FREE;
  690. zr->jpg_buffers.allocated = 0;
  691. }
  692. res = -EBUSY; /* what are you doing? */
  693. break;
  694. }
  695. }
  696. spin_unlock_irqrestore(&zr->spinlock, flags);
  697. if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
  698. zr->jpg_buffers.active = fh->jpg_buffers.active;
  699. }
  700. return res;
  701. }
  702. static int
  703. jpg_qbuf (struct file *file,
  704. int frame,
  705. enum zoran_codec_mode mode)
  706. {
  707. struct zoran_fh *fh = file->private_data;
  708. struct zoran *zr = fh->zr;
  709. int res = 0;
  710. /* Does the user want to stop streaming? */
  711. if (frame < 0) {
  712. if (zr->codec_mode == mode) {
  713. if (fh->jpg_buffers.active == ZORAN_FREE) {
  714. dprintk(1,
  715. KERN_ERR
  716. "%s: jpg_qbuf(-1) - session not active\n",
  717. ZR_DEVNAME(zr));
  718. return -EINVAL;
  719. }
  720. fh->jpg_buffers.active = zr->jpg_buffers.active =
  721. ZORAN_FREE;
  722. zr->jpg_buffers.allocated = 0;
  723. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  724. return 0;
  725. } else {
  726. dprintk(1,
  727. KERN_ERR
  728. "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
  729. ZR_DEVNAME(zr));
  730. return -EINVAL;
  731. }
  732. }
  733. if ((res = zoran_jpg_queue_frame(file, frame, mode)))
  734. return res;
  735. /* Start the jpeg codec when the first frame is queued */
  736. if (!res && zr->jpg_que_head == 1)
  737. jpeg_start(zr);
  738. return res;
  739. }
  740. /*
  741. * Sync on a MJPEG buffer
  742. */
  743. static int
  744. jpg_sync (struct file *file,
  745. struct zoran_sync *bs)
  746. {
  747. struct zoran_fh *fh = file->private_data;
  748. struct zoran *zr = fh->zr;
  749. unsigned long flags;
  750. int frame;
  751. if (fh->jpg_buffers.active == ZORAN_FREE) {
  752. dprintk(1,
  753. KERN_ERR
  754. "%s: jpg_sync() - capture is not currently active\n",
  755. ZR_DEVNAME(zr));
  756. return -EINVAL;
  757. }
  758. if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
  759. zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
  760. dprintk(1,
  761. KERN_ERR
  762. "%s: jpg_sync() - codec not in streaming mode\n",
  763. ZR_DEVNAME(zr));
  764. return -EINVAL;
  765. }
  766. if (!wait_event_interruptible_timeout(zr->jpg_capq,
  767. (zr->jpg_que_tail != zr->jpg_dma_tail ||
  768. zr->jpg_dma_tail == zr->jpg_dma_head),
  769. 10*HZ)) {
  770. int isr;
  771. btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
  772. udelay(1);
  773. zr->codec->control(zr->codec, CODEC_G_STATUS,
  774. sizeof(isr), &isr);
  775. dprintk(1,
  776. KERN_ERR
  777. "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
  778. ZR_DEVNAME(zr), isr);
  779. return -ETIME;
  780. }
  781. if (signal_pending(current))
  782. return -ERESTARTSYS;
  783. spin_lock_irqsave(&zr->spinlock, flags);
  784. if (zr->jpg_dma_tail != zr->jpg_dma_head)
  785. frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
  786. else
  787. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  788. /* buffer should now be in BUZ_STATE_DONE */
  789. if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
  790. dprintk(2,
  791. KERN_ERR "%s: jpg_sync() - internal state error\n",
  792. ZR_DEVNAME(zr));
  793. *bs = zr->jpg_buffers.buffer[frame].bs;
  794. bs->frame = frame;
  795. zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
  796. fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
  797. spin_unlock_irqrestore(&zr->spinlock, flags);
  798. return 0;
  799. }
  800. static void
  801. zoran_open_init_session (struct file *file)
  802. {
  803. int i;
  804. struct zoran_fh *fh = file->private_data;
  805. struct zoran *zr = fh->zr;
  806. /* Per default, map the V4L Buffers */
  807. fh->map_mode = ZORAN_MAP_MODE_RAW;
  808. /* take over the card's current settings */
  809. fh->overlay_settings = zr->overlay_settings;
  810. fh->overlay_settings.is_set = 0;
  811. fh->overlay_settings.format = zr->overlay_settings.format;
  812. fh->overlay_active = ZORAN_FREE;
  813. /* v4l settings */
  814. fh->v4l_settings = zr->v4l_settings;
  815. /* v4l_buffers */
  816. memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
  817. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  818. fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  819. fh->v4l_buffers.buffer[i].bs.frame = i;
  820. }
  821. fh->v4l_buffers.allocated = 0;
  822. fh->v4l_buffers.active = ZORAN_FREE;
  823. fh->v4l_buffers.buffer_size = v4l_bufsize;
  824. fh->v4l_buffers.num_buffers = v4l_nbufs;
  825. /* jpg settings */
  826. fh->jpg_settings = zr->jpg_settings;
  827. /* jpg_buffers */
  828. memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
  829. for (i = 0; i < BUZ_MAX_FRAME; i++) {
  830. fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  831. fh->jpg_buffers.buffer[i].bs.frame = i;
  832. }
  833. fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
  834. fh->jpg_buffers.allocated = 0;
  835. fh->jpg_buffers.active = ZORAN_FREE;
  836. fh->jpg_buffers.buffer_size = jpg_bufsize;
  837. fh->jpg_buffers.num_buffers = jpg_nbufs;
  838. }
  839. static void
  840. zoran_close_end_session (struct file *file)
  841. {
  842. struct zoran_fh *fh = file->private_data;
  843. struct zoran *zr = fh->zr;
  844. /* overlay */
  845. if (fh->overlay_active != ZORAN_FREE) {
  846. fh->overlay_active = zr->overlay_active = ZORAN_FREE;
  847. zr->v4l_overlay_active = 0;
  848. if (!zr->v4l_memgrab_active)
  849. zr36057_overlay(zr, 0);
  850. zr->overlay_mask = NULL;
  851. }
  852. /* v4l capture */
  853. if (fh->v4l_buffers.active != ZORAN_FREE) {
  854. unsigned long flags;
  855. spin_lock_irqsave(&zr->spinlock, flags);
  856. zr36057_set_memgrab(zr, 0);
  857. zr->v4l_buffers.allocated = 0;
  858. zr->v4l_buffers.active = fh->v4l_buffers.active =
  859. ZORAN_FREE;
  860. spin_unlock_irqrestore(&zr->spinlock, flags);
  861. }
  862. /* v4l buffers */
  863. if (fh->v4l_buffers.allocated)
  864. v4l_fbuffer_free(file);
  865. /* jpg capture */
  866. if (fh->jpg_buffers.active != ZORAN_FREE) {
  867. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  868. zr->jpg_buffers.allocated = 0;
  869. zr->jpg_buffers.active = fh->jpg_buffers.active =
  870. ZORAN_FREE;
  871. }
  872. /* jpg buffers */
  873. if (fh->jpg_buffers.allocated)
  874. jpg_fbuffer_free(file);
  875. }
  876. /*
  877. * Open a zoran card. Right now the flags stuff is just playing
  878. */
  879. static int zoran_open(struct file *file)
  880. {
  881. struct zoran *zr = video_drvdata(file);
  882. struct zoran_fh *fh;
  883. int res, first_open = 0;
  884. dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
  885. ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
  886. lock_kernel();
  887. /* see fs/device.c - the kernel already locks during open(),
  888. * so locking ourselves only causes deadlocks */
  889. /*mutex_lock(&zr->resource_lock);*/
  890. if (zr->user >= 2048) {
  891. dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
  892. ZR_DEVNAME(zr), zr->user);
  893. res = -EBUSY;
  894. goto fail_unlock;
  895. }
  896. if (!zr->decoder) {
  897. dprintk(1,
  898. KERN_ERR "%s: no TV decoder loaded for device!\n",
  899. ZR_DEVNAME(zr));
  900. res = -EIO;
  901. goto fail_unlock;
  902. }
  903. if (!try_module_get(zr->decoder->driver->driver.owner)) {
  904. dprintk(1,
  905. KERN_ERR
  906. "%s: failed to grab ownership of video decoder\n",
  907. ZR_DEVNAME(zr));
  908. res = -EIO;
  909. goto fail_unlock;
  910. }
  911. if (zr->encoder &&
  912. !try_module_get(zr->encoder->driver->driver.owner)) {
  913. dprintk(1,
  914. KERN_ERR
  915. "%s: failed to grab ownership of video encoder\n",
  916. ZR_DEVNAME(zr));
  917. res = -EIO;
  918. goto fail_decoder;
  919. }
  920. /* now, create the open()-specific file_ops struct */
  921. fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
  922. if (!fh) {
  923. dprintk(1,
  924. KERN_ERR
  925. "%s: zoran_open() - allocation of zoran_fh failed\n",
  926. ZR_DEVNAME(zr));
  927. res = -ENOMEM;
  928. goto fail_encoder;
  929. }
  930. /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
  931. * on norm-change! */
  932. fh->overlay_mask =
  933. kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
  934. if (!fh->overlay_mask) {
  935. dprintk(1,
  936. KERN_ERR
  937. "%s: zoran_open() - allocation of overlay_mask failed\n",
  938. ZR_DEVNAME(zr));
  939. res = -ENOMEM;
  940. goto fail_fh;
  941. }
  942. if (zr->user++ == 0)
  943. first_open = 1;
  944. /*mutex_unlock(&zr->resource_lock);*/
  945. /* default setup - TODO: look at flags */
  946. if (first_open) { /* First device open */
  947. zr36057_restart(zr);
  948. zoran_open_init_params(zr);
  949. zoran_init_hardware(zr);
  950. btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
  951. }
  952. /* set file_ops stuff */
  953. file->private_data = fh;
  954. fh->zr = zr;
  955. zoran_open_init_session(file);
  956. unlock_kernel();
  957. return 0;
  958. fail_fh:
  959. kfree(fh);
  960. fail_encoder:
  961. if (zr->encoder)
  962. module_put(zr->encoder->driver->driver.owner);
  963. fail_decoder:
  964. module_put(zr->decoder->driver->driver.owner);
  965. fail_unlock:
  966. unlock_kernel();
  967. dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
  968. ZR_DEVNAME(zr), res, zr->user);
  969. return res;
  970. }
  971. static int
  972. zoran_close(struct file *file)
  973. {
  974. struct zoran_fh *fh = file->private_data;
  975. struct zoran *zr = fh->zr;
  976. dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
  977. ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
  978. /* kernel locks (fs/device.c), so don't do that ourselves
  979. * (prevents deadlocks) */
  980. /*mutex_lock(&zr->resource_lock);*/
  981. zoran_close_end_session(file);
  982. if (zr->user-- == 1) { /* Last process */
  983. /* Clean up JPEG process */
  984. wake_up_interruptible(&zr->jpg_capq);
  985. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  986. zr->jpg_buffers.allocated = 0;
  987. zr->jpg_buffers.active = ZORAN_FREE;
  988. /* disable interrupts */
  989. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  990. if (zr36067_debug > 1)
  991. print_interrupts(zr);
  992. /* Overlay off */
  993. zr->v4l_overlay_active = 0;
  994. zr36057_overlay(zr, 0);
  995. zr->overlay_mask = NULL;
  996. /* capture off */
  997. wake_up_interruptible(&zr->v4l_capq);
  998. zr36057_set_memgrab(zr, 0);
  999. zr->v4l_buffers.allocated = 0;
  1000. zr->v4l_buffers.active = ZORAN_FREE;
  1001. zoran_set_pci_master(zr, 0);
  1002. if (!pass_through) { /* Switch to color bar */
  1003. struct v4l2_routing route = { 2, 0 };
  1004. decoder_command(zr, VIDIOC_STREAMOFF, 0);
  1005. encoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  1006. }
  1007. }
  1008. file->private_data = NULL;
  1009. kfree(fh->overlay_mask);
  1010. kfree(fh);
  1011. /* release locks on the i2c modules */
  1012. module_put(zr->decoder->driver->driver.owner);
  1013. if (zr->encoder)
  1014. module_put(zr->encoder->driver->driver.owner);
  1015. /*mutex_unlock(&zr->resource_lock);*/
  1016. dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
  1017. return 0;
  1018. }
  1019. static ssize_t
  1020. zoran_read (struct file *file,
  1021. char __user *data,
  1022. size_t count,
  1023. loff_t *ppos)
  1024. {
  1025. /* we simply don't support read() (yet)... */
  1026. return -EINVAL;
  1027. }
  1028. static ssize_t
  1029. zoran_write (struct file *file,
  1030. const char __user *data,
  1031. size_t count,
  1032. loff_t *ppos)
  1033. {
  1034. /* ...and the same goes for write() */
  1035. return -EINVAL;
  1036. }
  1037. static int
  1038. setup_fbuffer (struct file *file,
  1039. void *base,
  1040. const struct zoran_format *fmt,
  1041. int width,
  1042. int height,
  1043. int bytesperline)
  1044. {
  1045. struct zoran_fh *fh = file->private_data;
  1046. struct zoran *zr = fh->zr;
  1047. /* (Ronald) v4l/v4l2 guidelines */
  1048. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  1049. return -EPERM;
  1050. /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
  1051. ALi Magik (that needs very low latency while the card needs a
  1052. higher value always) */
  1053. if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
  1054. return -ENXIO;
  1055. /* we need a bytesperline value, even if not given */
  1056. if (!bytesperline)
  1057. bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
  1058. #if 0
  1059. if (zr->overlay_active) {
  1060. /* dzjee... stupid users... don't even bother to turn off
  1061. * overlay before changing the memory location...
  1062. * normally, we would return errors here. However, one of
  1063. * the tools that does this is... xawtv! and since xawtv
  1064. * is used by +/- 99% of the users, we'd rather be user-
  1065. * friendly and silently do as if nothing went wrong */
  1066. dprintk(3,
  1067. KERN_ERR
  1068. "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
  1069. ZR_DEVNAME(zr));
  1070. zr36057_overlay(zr, 0);
  1071. }
  1072. #endif
  1073. if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
  1074. dprintk(1,
  1075. KERN_ERR
  1076. "%s: setup_fbuffer() - no valid overlay format given\n",
  1077. ZR_DEVNAME(zr));
  1078. return -EINVAL;
  1079. }
  1080. if (height <= 0 || width <= 0 || bytesperline <= 0) {
  1081. dprintk(1,
  1082. KERN_ERR
  1083. "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
  1084. ZR_DEVNAME(zr), width, height, bytesperline);
  1085. return -EINVAL;
  1086. }
  1087. if (bytesperline & 3) {
  1088. dprintk(1,
  1089. KERN_ERR
  1090. "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
  1091. ZR_DEVNAME(zr), bytesperline);
  1092. return -EINVAL;
  1093. }
  1094. zr->buffer.base = (void *) ((unsigned long) base & ~3);
  1095. zr->buffer.height = height;
  1096. zr->buffer.width = width;
  1097. zr->buffer.depth = fmt->depth;
  1098. zr->overlay_settings.format = fmt;
  1099. zr->buffer.bytesperline = bytesperline;
  1100. /* The user should set new window parameters */
  1101. zr->overlay_settings.is_set = 0;
  1102. return 0;
  1103. }
  1104. static int
  1105. setup_window (struct file *file,
  1106. int x,
  1107. int y,
  1108. int width,
  1109. int height,
  1110. struct video_clip __user *clips,
  1111. int clipcount,
  1112. void __user *bitmap)
  1113. {
  1114. struct zoran_fh *fh = file->private_data;
  1115. struct zoran *zr = fh->zr;
  1116. struct video_clip *vcp = NULL;
  1117. int on, end;
  1118. if (!zr->buffer.base) {
  1119. dprintk(1,
  1120. KERN_ERR
  1121. "%s: setup_window() - frame buffer has to be set first\n",
  1122. ZR_DEVNAME(zr));
  1123. return -EINVAL;
  1124. }
  1125. if (!fh->overlay_settings.format) {
  1126. dprintk(1,
  1127. KERN_ERR
  1128. "%s: setup_window() - no overlay format set\n",
  1129. ZR_DEVNAME(zr));
  1130. return -EINVAL;
  1131. }
  1132. /*
  1133. * The video front end needs 4-byte alinged line sizes, we correct that
  1134. * silently here if necessary
  1135. */
  1136. if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
  1137. end = (x + width) & ~1; /* round down */
  1138. x = (x + 1) & ~1; /* round up */
  1139. width = end - x;
  1140. }
  1141. if (zr->buffer.depth == 24) {
  1142. end = (x + width) & ~3; /* round down */
  1143. x = (x + 3) & ~3; /* round up */
  1144. width = end - x;
  1145. }
  1146. if (width > BUZ_MAX_WIDTH)
  1147. width = BUZ_MAX_WIDTH;
  1148. if (height > BUZ_MAX_HEIGHT)
  1149. height = BUZ_MAX_HEIGHT;
  1150. /* Check for vaild parameters */
  1151. if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
  1152. width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
  1153. dprintk(1,
  1154. KERN_ERR
  1155. "%s: setup_window() - width = %d or height = %d invalid\n",
  1156. ZR_DEVNAME(zr), width, height);
  1157. return -EINVAL;
  1158. }
  1159. fh->overlay_settings.x = x;
  1160. fh->overlay_settings.y = y;
  1161. fh->overlay_settings.width = width;
  1162. fh->overlay_settings.height = height;
  1163. fh->overlay_settings.clipcount = clipcount;
  1164. /*
  1165. * If an overlay is running, we have to switch it off
  1166. * and switch it on again in order to get the new settings in effect.
  1167. *
  1168. * We also want to avoid that the overlay mask is written
  1169. * when an overlay is running.
  1170. */
  1171. on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
  1172. zr->overlay_active != ZORAN_FREE &&
  1173. fh->overlay_active != ZORAN_FREE;
  1174. if (on)
  1175. zr36057_overlay(zr, 0);
  1176. /*
  1177. * Write the overlay mask if clips are wanted.
  1178. * We prefer a bitmap.
  1179. */
  1180. if (bitmap) {
  1181. /* fake value - it just means we want clips */
  1182. fh->overlay_settings.clipcount = 1;
  1183. if (copy_from_user(fh->overlay_mask, bitmap,
  1184. (width * height + 7) / 8)) {
  1185. return -EFAULT;
  1186. }
  1187. } else if (clipcount > 0) {
  1188. /* write our own bitmap from the clips */
  1189. vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
  1190. if (vcp == NULL) {
  1191. dprintk(1,
  1192. KERN_ERR
  1193. "%s: setup_window() - Alloc of clip mask failed\n",
  1194. ZR_DEVNAME(zr));
  1195. return -ENOMEM;
  1196. }
  1197. if (copy_from_user
  1198. (vcp, clips, sizeof(struct video_clip) * clipcount)) {
  1199. vfree(vcp);
  1200. return -EFAULT;
  1201. }
  1202. write_overlay_mask(file, vcp, clipcount);
  1203. vfree(vcp);
  1204. }
  1205. fh->overlay_settings.is_set = 1;
  1206. if (fh->overlay_active != ZORAN_FREE &&
  1207. zr->overlay_active != ZORAN_FREE)
  1208. zr->overlay_settings = fh->overlay_settings;
  1209. if (on)
  1210. zr36057_overlay(zr, 1);
  1211. /* Make sure the changes come into effect */
  1212. return wait_grab_pending(zr);
  1213. }
  1214. static int
  1215. setup_overlay (struct file *file,
  1216. int on)
  1217. {
  1218. struct zoran_fh *fh = file->private_data;
  1219. struct zoran *zr = fh->zr;
  1220. /* If there is nothing to do, return immediatly */
  1221. if ((on && fh->overlay_active != ZORAN_FREE) ||
  1222. (!on && fh->overlay_active == ZORAN_FREE))
  1223. return 0;
  1224. /* check whether we're touching someone else's overlay */
  1225. if (on && zr->overlay_active != ZORAN_FREE &&
  1226. fh->overlay_active == ZORAN_FREE) {
  1227. dprintk(1,
  1228. KERN_ERR
  1229. "%s: setup_overlay() - overlay is already active for another session\n",
  1230. ZR_DEVNAME(zr));
  1231. return -EBUSY;
  1232. }
  1233. if (!on && zr->overlay_active != ZORAN_FREE &&
  1234. fh->overlay_active == ZORAN_FREE) {
  1235. dprintk(1,
  1236. KERN_ERR
  1237. "%s: setup_overlay() - you cannot cancel someone else's session\n",
  1238. ZR_DEVNAME(zr));
  1239. return -EPERM;
  1240. }
  1241. if (on == 0) {
  1242. zr->overlay_active = fh->overlay_active = ZORAN_FREE;
  1243. zr->v4l_overlay_active = 0;
  1244. /* When a grab is running, the video simply
  1245. * won't be switched on any more */
  1246. if (!zr->v4l_memgrab_active)
  1247. zr36057_overlay(zr, 0);
  1248. zr->overlay_mask = NULL;
  1249. } else {
  1250. if (!zr->buffer.base || !fh->overlay_settings.is_set) {
  1251. dprintk(1,
  1252. KERN_ERR
  1253. "%s: setup_overlay() - buffer or window not set\n",
  1254. ZR_DEVNAME(zr));
  1255. return -EINVAL;
  1256. }
  1257. if (!fh->overlay_settings.format) {
  1258. dprintk(1,
  1259. KERN_ERR
  1260. "%s: setup_overlay() - no overlay format set\n",
  1261. ZR_DEVNAME(zr));
  1262. return -EINVAL;
  1263. }
  1264. zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
  1265. zr->v4l_overlay_active = 1;
  1266. zr->overlay_mask = fh->overlay_mask;
  1267. zr->overlay_settings = fh->overlay_settings;
  1268. if (!zr->v4l_memgrab_active)
  1269. zr36057_overlay(zr, 1);
  1270. /* When a grab is running, the video will be
  1271. * switched on when grab is finished */
  1272. }
  1273. /* Make sure the changes come into effect */
  1274. return wait_grab_pending(zr);
  1275. }
  1276. /* get the status of a buffer in the clients buffer queue */
  1277. static int
  1278. zoran_v4l2_buffer_status (struct file *file,
  1279. struct v4l2_buffer *buf,
  1280. int num)
  1281. {
  1282. struct zoran_fh *fh = file->private_data;
  1283. struct zoran *zr = fh->zr;
  1284. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1285. switch (fh->map_mode) {
  1286. case ZORAN_MAP_MODE_RAW:
  1287. /* check range */
  1288. if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
  1289. !fh->v4l_buffers.allocated) {
  1290. dprintk(1,
  1291. KERN_ERR
  1292. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1293. ZR_DEVNAME(zr));
  1294. return -EINVAL;
  1295. }
  1296. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1297. buf->length = fh->v4l_buffers.buffer_size;
  1298. /* get buffer */
  1299. buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
  1300. if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1301. fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
  1302. buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
  1303. buf->flags |= V4L2_BUF_FLAG_DONE;
  1304. buf->timestamp =
  1305. fh->v4l_buffers.buffer[num].bs.timestamp;
  1306. } else {
  1307. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1308. }
  1309. if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
  1310. buf->field = V4L2_FIELD_TOP;
  1311. else
  1312. buf->field = V4L2_FIELD_INTERLACED;
  1313. break;
  1314. case ZORAN_MAP_MODE_JPG_REC:
  1315. case ZORAN_MAP_MODE_JPG_PLAY:
  1316. /* check range */
  1317. if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
  1318. !fh->jpg_buffers.allocated) {
  1319. dprintk(1,
  1320. KERN_ERR
  1321. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1322. ZR_DEVNAME(zr));
  1323. return -EINVAL;
  1324. }
  1325. buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  1326. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  1327. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1328. buf->length = fh->jpg_buffers.buffer_size;
  1329. /* these variables are only written after frame has been captured */
  1330. if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1331. fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
  1332. buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
  1333. buf->timestamp =
  1334. fh->jpg_buffers.buffer[num].bs.timestamp;
  1335. buf->bytesused =
  1336. fh->jpg_buffers.buffer[num].bs.length;
  1337. buf->flags |= V4L2_BUF_FLAG_DONE;
  1338. } else {
  1339. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1340. }
  1341. /* which fields are these? */
  1342. if (fh->jpg_settings.TmpDcm != 1)
  1343. buf->field =
  1344. fh->jpg_settings.
  1345. odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1346. else
  1347. buf->field =
  1348. fh->jpg_settings.
  1349. odd_even ? V4L2_FIELD_SEQ_TB :
  1350. V4L2_FIELD_SEQ_BT;
  1351. break;
  1352. default:
  1353. dprintk(5,
  1354. KERN_ERR
  1355. "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
  1356. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  1357. return -EINVAL;
  1358. }
  1359. buf->memory = V4L2_MEMORY_MMAP;
  1360. buf->index = num;
  1361. buf->m.offset = buf->length * num;
  1362. return 0;
  1363. }
  1364. static int
  1365. zoran_set_norm (struct zoran *zr,
  1366. v4l2_std_id norm)
  1367. {
  1368. int on;
  1369. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1370. zr->jpg_buffers.active != ZORAN_FREE) {
  1371. dprintk(1,
  1372. KERN_WARNING
  1373. "%s: set_norm() called while in playback/capture mode\n",
  1374. ZR_DEVNAME(zr));
  1375. return -EBUSY;
  1376. }
  1377. if (lock_norm && norm != zr->norm) {
  1378. if (lock_norm > 1) {
  1379. dprintk(1,
  1380. KERN_WARNING
  1381. "%s: set_norm() - TV standard is locked, can not switch norm\n",
  1382. ZR_DEVNAME(zr));
  1383. return -EPERM;
  1384. } else {
  1385. dprintk(1,
  1386. KERN_WARNING
  1387. "%s: set_norm() - TV standard is locked, norm was not changed\n",
  1388. ZR_DEVNAME(zr));
  1389. norm = zr->norm;
  1390. }
  1391. }
  1392. if (!(norm & zr->card.norms)) {
  1393. dprintk(1,
  1394. KERN_ERR "%s: set_norm() - unsupported norm %llx\n",
  1395. ZR_DEVNAME(zr), norm);
  1396. return -EINVAL;
  1397. }
  1398. if (norm == V4L2_STD_ALL) {
  1399. int status = 0;
  1400. v4l2_std_id std = 0;
  1401. decoder_command(zr, VIDIOC_QUERYSTD, &std);
  1402. decoder_command(zr, VIDIOC_S_STD, &std);
  1403. /* let changes come into effect */
  1404. ssleep(2);
  1405. decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &status);
  1406. if (status & V4L2_IN_ST_NO_SIGNAL) {
  1407. dprintk(1,
  1408. KERN_ERR
  1409. "%s: set_norm() - no norm detected\n",
  1410. ZR_DEVNAME(zr));
  1411. /* reset norm */
  1412. decoder_command(zr, VIDIOC_S_STD, &zr->norm);
  1413. return -EIO;
  1414. }
  1415. norm = std;
  1416. }
  1417. if (norm & V4L2_STD_SECAM)
  1418. zr->timing = zr->card.tvn[2];
  1419. else if (norm & V4L2_STD_NTSC)
  1420. zr->timing = zr->card.tvn[1];
  1421. else
  1422. zr->timing = zr->card.tvn[0];
  1423. /* We switch overlay off and on since a change in the
  1424. * norm needs different VFE settings */
  1425. on = zr->overlay_active && !zr->v4l_memgrab_active;
  1426. if (on)
  1427. zr36057_overlay(zr, 0);
  1428. decoder_command(zr, VIDIOC_S_STD, &norm);
  1429. encoder_command(zr, VIDIOC_INT_S_STD_OUTPUT, &norm);
  1430. if (on)
  1431. zr36057_overlay(zr, 1);
  1432. /* Make sure the changes come into effect */
  1433. zr->norm = norm;
  1434. return 0;
  1435. }
  1436. static int
  1437. zoran_set_input (struct zoran *zr,
  1438. int input)
  1439. {
  1440. struct v4l2_routing route = { 0, 0 };
  1441. if (input == zr->input) {
  1442. return 0;
  1443. }
  1444. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1445. zr->jpg_buffers.active != ZORAN_FREE) {
  1446. dprintk(1,
  1447. KERN_WARNING
  1448. "%s: set_input() called while in playback/capture mode\n",
  1449. ZR_DEVNAME(zr));
  1450. return -EBUSY;
  1451. }
  1452. if (input < 0 || input >= zr->card.inputs) {
  1453. dprintk(1,
  1454. KERN_ERR
  1455. "%s: set_input() - unnsupported input %d\n",
  1456. ZR_DEVNAME(zr), input);
  1457. return -EINVAL;
  1458. }
  1459. route.input = zr->card.input[input].muxsel;
  1460. zr->input = input;
  1461. decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  1462. return 0;
  1463. }
  1464. /*
  1465. * ioctl routine
  1466. */
  1467. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1468. static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
  1469. {
  1470. struct zoran_fh *fh = __fh;
  1471. struct zoran *zr = fh->zr;
  1472. struct zoran_jpg_settings settings;
  1473. switch (cmd) {
  1474. case BUZIOC_G_PARAMS:
  1475. {
  1476. struct zoran_params *bparams = arg;
  1477. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
  1478. memset(bparams, 0, sizeof(struct zoran_params));
  1479. bparams->major_version = MAJOR_VERSION;
  1480. bparams->minor_version = MINOR_VERSION;
  1481. mutex_lock(&zr->resource_lock);
  1482. if (zr->norm & V4L2_STD_NTSC)
  1483. bparams->norm = VIDEO_MODE_NTSC;
  1484. else if (zr->norm & V4L2_STD_PAL)
  1485. bparams->norm = VIDEO_MODE_PAL;
  1486. else
  1487. bparams->norm = VIDEO_MODE_SECAM;
  1488. bparams->input = zr->input;
  1489. bparams->decimation = fh->jpg_settings.decimation;
  1490. bparams->HorDcm = fh->jpg_settings.HorDcm;
  1491. bparams->VerDcm = fh->jpg_settings.VerDcm;
  1492. bparams->TmpDcm = fh->jpg_settings.TmpDcm;
  1493. bparams->field_per_buff = fh->jpg_settings.field_per_buff;
  1494. bparams->img_x = fh->jpg_settings.img_x;
  1495. bparams->img_y = fh->jpg_settings.img_y;
  1496. bparams->img_width = fh->jpg_settings.img_width;
  1497. bparams->img_height = fh->jpg_settings.img_height;
  1498. bparams->odd_even = fh->jpg_settings.odd_even;
  1499. bparams->quality = fh->jpg_settings.jpg_comp.quality;
  1500. bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
  1501. bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  1502. memcpy(bparams->APP_data,
  1503. fh->jpg_settings.jpg_comp.APP_data,
  1504. sizeof(bparams->APP_data));
  1505. bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
  1506. memcpy(bparams->COM_data,
  1507. fh->jpg_settings.jpg_comp.COM_data,
  1508. sizeof(bparams->COM_data));
  1509. bparams->jpeg_markers =
  1510. fh->jpg_settings.jpg_comp.jpeg_markers;
  1511. mutex_unlock(&zr->resource_lock);
  1512. bparams->VFIFO_FB = 0;
  1513. return 0;
  1514. }
  1515. case BUZIOC_S_PARAMS:
  1516. {
  1517. struct zoran_params *bparams = arg;
  1518. int res = 0;
  1519. dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
  1520. settings.decimation = bparams->decimation;
  1521. settings.HorDcm = bparams->HorDcm;
  1522. settings.VerDcm = bparams->VerDcm;
  1523. settings.TmpDcm = bparams->TmpDcm;
  1524. settings.field_per_buff = bparams->field_per_buff;
  1525. settings.img_x = bparams->img_x;
  1526. settings.img_y = bparams->img_y;
  1527. settings.img_width = bparams->img_width;
  1528. settings.img_height = bparams->img_height;
  1529. settings.odd_even = bparams->odd_even;
  1530. settings.jpg_comp.quality = bparams->quality;
  1531. settings.jpg_comp.APPn = bparams->APPn;
  1532. settings.jpg_comp.APP_len = bparams->APP_len;
  1533. memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
  1534. sizeof(bparams->APP_data));
  1535. settings.jpg_comp.COM_len = bparams->COM_len;
  1536. memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
  1537. sizeof(bparams->COM_data));
  1538. settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
  1539. mutex_lock(&zr->resource_lock);
  1540. if (zr->codec_mode != BUZ_MODE_IDLE) {
  1541. dprintk(1,
  1542. KERN_ERR
  1543. "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
  1544. ZR_DEVNAME(zr));
  1545. res = -EINVAL;
  1546. goto sparams_unlock_and_return;
  1547. }
  1548. /* Check the params first before overwriting our
  1549. * nternal values */
  1550. if (zoran_check_jpg_settings(zr, &settings, 0)) {
  1551. res = -EINVAL;
  1552. goto sparams_unlock_and_return;
  1553. }
  1554. fh->jpg_settings = settings;
  1555. sparams_unlock_and_return:
  1556. mutex_unlock(&zr->resource_lock);
  1557. return res;
  1558. }
  1559. case BUZIOC_REQBUFS:
  1560. {
  1561. struct zoran_requestbuffers *breq = arg;
  1562. int res = 0;
  1563. dprintk(3,
  1564. KERN_DEBUG
  1565. "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
  1566. ZR_DEVNAME(zr), breq->count, breq->size);
  1567. /* Enforce reasonable lower and upper limits */
  1568. if (breq->count < 4)
  1569. breq->count = 4; /* Could be choosen smaller */
  1570. if (breq->count > jpg_nbufs)
  1571. breq->count = jpg_nbufs;
  1572. breq->size = PAGE_ALIGN(breq->size);
  1573. if (breq->size < 8192)
  1574. breq->size = 8192; /* Arbitrary */
  1575. /* breq->size is limited by 1 page for the stat_com
  1576. * tables to a Maximum of 2 MB */
  1577. if (breq->size > jpg_bufsize)
  1578. breq->size = jpg_bufsize;
  1579. mutex_lock(&zr->resource_lock);
  1580. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  1581. dprintk(1,
  1582. KERN_ERR
  1583. "%s: BUZIOC_REQBUFS - buffers already allocated\n",
  1584. ZR_DEVNAME(zr));
  1585. res = -EBUSY;
  1586. goto jpgreqbuf_unlock_and_return;
  1587. }
  1588. fh->jpg_buffers.num_buffers = breq->count;
  1589. fh->jpg_buffers.buffer_size = breq->size;
  1590. if (jpg_fbuffer_alloc(file)) {
  1591. res = -ENOMEM;
  1592. goto jpgreqbuf_unlock_and_return;
  1593. }
  1594. /* The next mmap will map the MJPEG buffers - could
  1595. * also be *_PLAY, but it doesn't matter here */
  1596. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  1597. jpgreqbuf_unlock_and_return:
  1598. mutex_unlock(&zr->resource_lock);
  1599. return res;
  1600. }
  1601. case BUZIOC_QBUF_CAPT:
  1602. {
  1603. int *frame = arg, res;
  1604. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
  1605. ZR_DEVNAME(zr), *frame);
  1606. mutex_lock(&zr->resource_lock);
  1607. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
  1608. mutex_unlock(&zr->resource_lock);
  1609. return res;
  1610. }
  1611. case BUZIOC_QBUF_PLAY:
  1612. {
  1613. int *frame = arg, res;
  1614. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
  1615. ZR_DEVNAME(zr), *frame);
  1616. mutex_lock(&zr->resource_lock);
  1617. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
  1618. mutex_unlock(&zr->resource_lock);
  1619. return res;
  1620. }
  1621. case BUZIOC_SYNC:
  1622. {
  1623. struct zoran_sync *bsync = arg;
  1624. int res;
  1625. dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
  1626. mutex_lock(&zr->resource_lock);
  1627. res = jpg_sync(file, bsync);
  1628. mutex_unlock(&zr->resource_lock);
  1629. return res;
  1630. }
  1631. case BUZIOC_G_STATUS:
  1632. {
  1633. struct zoran_status *bstat = arg;
  1634. struct v4l2_routing route = { 0, 0 };
  1635. int status = 0, res = 0;
  1636. v4l2_std_id norm;
  1637. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
  1638. if (zr->codec_mode != BUZ_MODE_IDLE) {
  1639. dprintk(1,
  1640. KERN_ERR
  1641. "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
  1642. ZR_DEVNAME(zr));
  1643. return -EINVAL;
  1644. }
  1645. route.input = zr->card.input[bstat->input].muxsel;
  1646. mutex_lock(&zr->resource_lock);
  1647. if (zr->codec_mode != BUZ_MODE_IDLE) {
  1648. dprintk(1,
  1649. KERN_ERR
  1650. "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
  1651. ZR_DEVNAME(zr));
  1652. res = -EINVAL;
  1653. goto gstat_unlock_and_return;
  1654. }
  1655. decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  1656. /* sleep 1 second */
  1657. ssleep(1);
  1658. /* Get status of video decoder */
  1659. decoder_command(zr, VIDIOC_QUERYSTD, &norm);
  1660. decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &status);
  1661. /* restore previous input and norm */
  1662. route.input = zr->card.input[zr->input].muxsel;
  1663. decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  1664. gstat_unlock_and_return:
  1665. mutex_unlock(&zr->resource_lock);
  1666. if (!res) {
  1667. bstat->signal =
  1668. (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
  1669. if (norm & V4L2_STD_NTSC)
  1670. bstat->norm = VIDEO_MODE_NTSC;
  1671. else if (norm & V4L2_STD_SECAM)
  1672. bstat->norm = VIDEO_MODE_SECAM;
  1673. else
  1674. bstat->norm = VIDEO_MODE_PAL;
  1675. bstat->color =
  1676. (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
  1677. }
  1678. return res;
  1679. }
  1680. default:
  1681. return -EINVAL;
  1682. }
  1683. }
  1684. static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
  1685. {
  1686. struct zoran_fh *fh = __fh;
  1687. struct zoran *zr = fh->zr;
  1688. int i, res = 0;
  1689. vmbuf->size =
  1690. fh->v4l_buffers.num_buffers *
  1691. fh->v4l_buffers.buffer_size;
  1692. vmbuf->frames = fh->v4l_buffers.num_buffers;
  1693. for (i = 0; i < vmbuf->frames; i++) {
  1694. vmbuf->offsets[i] =
  1695. i * fh->v4l_buffers.buffer_size;
  1696. }
  1697. mutex_lock(&zr->resource_lock);
  1698. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  1699. dprintk(1,
  1700. KERN_ERR
  1701. "%s: VIDIOCGMBUF - buffers already allocated\n",
  1702. ZR_DEVNAME(zr));
  1703. res = -EINVAL;
  1704. goto v4l1reqbuf_unlock_and_return;
  1705. }
  1706. if (v4l_fbuffer_alloc(file)) {
  1707. res = -ENOMEM;
  1708. goto v4l1reqbuf_unlock_and_return;
  1709. }
  1710. /* The next mmap will map the V4L buffers */
  1711. fh->map_mode = ZORAN_MAP_MODE_RAW;
  1712. v4l1reqbuf_unlock_and_return:
  1713. mutex_unlock(&zr->resource_lock);
  1714. return res;
  1715. }
  1716. #endif
  1717. static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
  1718. {
  1719. struct zoran_fh *fh = __fh;
  1720. struct zoran *zr = fh->zr;
  1721. memset(cap, 0, sizeof(*cap));
  1722. strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
  1723. strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
  1724. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  1725. pci_name(zr->pci_dev));
  1726. cap->version =
  1727. KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
  1728. RELEASE_VERSION);
  1729. cap->capabilities = ZORAN_V4L2_VID_FLAGS;
  1730. return 0;
  1731. }
  1732. static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
  1733. {
  1734. int num = -1, i;
  1735. for (i = 0; i < NUM_FORMATS; i++) {
  1736. if (zoran_formats[i].flags & flag)
  1737. num++;
  1738. if (num == fmt->index)
  1739. break;
  1740. }
  1741. if (fmt->index < 0 /* late, but not too late */ || i == NUM_FORMATS)
  1742. return -EINVAL;
  1743. strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
  1744. fmt->pixelformat = zoran_formats[i].fourcc;
  1745. if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
  1746. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  1747. return 0;
  1748. }
  1749. static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
  1750. struct v4l2_fmtdesc *f)
  1751. {
  1752. struct zoran_fh *fh = __fh;
  1753. struct zoran *zr = fh->zr;
  1754. return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
  1755. }
  1756. static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
  1757. struct v4l2_fmtdesc *f)
  1758. {
  1759. struct zoran_fh *fh = __fh;
  1760. struct zoran *zr = fh->zr;
  1761. return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
  1762. }
  1763. static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
  1764. struct v4l2_fmtdesc *f)
  1765. {
  1766. struct zoran_fh *fh = __fh;
  1767. struct zoran *zr = fh->zr;
  1768. return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
  1769. }
  1770. static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
  1771. struct v4l2_format *fmt)
  1772. {
  1773. struct zoran_fh *fh = __fh;
  1774. struct zoran *zr = fh->zr;
  1775. mutex_lock(&zr->resource_lock);
  1776. fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
  1777. fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
  1778. (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
  1779. fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  1780. fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
  1781. if (fh->jpg_settings.TmpDcm == 1)
  1782. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1783. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
  1784. else
  1785. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1786. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
  1787. fmt->fmt.pix.bytesperline = 0;
  1788. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1789. mutex_unlock(&zr->resource_lock);
  1790. return 0;
  1791. }
  1792. static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
  1793. struct v4l2_format *fmt)
  1794. {
  1795. struct zoran_fh *fh = __fh;
  1796. struct zoran *zr = fh->zr;
  1797. if (fh->map_mode != ZORAN_MAP_MODE_RAW)
  1798. return zoran_g_fmt_vid_out(file, fh, fmt);
  1799. mutex_lock(&zr->resource_lock);
  1800. fmt->fmt.pix.width = fh->v4l_settings.width;
  1801. fmt->fmt.pix.height = fh->v4l_settings.height;
  1802. fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
  1803. fh->v4l_settings.height;
  1804. fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
  1805. fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
  1806. fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
  1807. if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
  1808. fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
  1809. else
  1810. fmt->fmt.pix.field = V4L2_FIELD_TOP;
  1811. mutex_unlock(&zr->resource_lock);
  1812. return 0;
  1813. }
  1814. static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
  1815. struct v4l2_format *fmt)
  1816. {
  1817. struct zoran_fh *fh = __fh;
  1818. struct zoran *zr = fh->zr;
  1819. mutex_lock(&zr->resource_lock);
  1820. fmt->fmt.win.w.left = fh->overlay_settings.x;
  1821. fmt->fmt.win.w.top = fh->overlay_settings.y;
  1822. fmt->fmt.win.w.width = fh->overlay_settings.width;
  1823. fmt->fmt.win.w.height = fh->overlay_settings.height;
  1824. if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
  1825. fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
  1826. else
  1827. fmt->fmt.win.field = V4L2_FIELD_TOP;
  1828. mutex_unlock(&zr->resource_lock);
  1829. return 0;
  1830. }
  1831. static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
  1832. struct v4l2_format *fmt)
  1833. {
  1834. struct zoran_fh *fh = __fh;
  1835. struct zoran *zr = fh->zr;
  1836. mutex_lock(&zr->resource_lock);
  1837. if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
  1838. fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
  1839. if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
  1840. fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
  1841. if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
  1842. fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
  1843. if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
  1844. fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
  1845. mutex_unlock(&zr->resource_lock);
  1846. return 0;
  1847. }
  1848. static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
  1849. struct v4l2_format *fmt)
  1850. {
  1851. struct zoran_fh *fh = __fh;
  1852. struct zoran *zr = fh->zr;
  1853. struct zoran_jpg_settings settings;
  1854. int res = 0;
  1855. if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1856. return -EINVAL;
  1857. fmt->fmt.pix.bytesperline = 0;
  1858. mutex_lock(&zr->resource_lock);
  1859. settings = fh->jpg_settings;
  1860. /* we actually need to set 'real' parameters now */
  1861. if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
  1862. settings.TmpDcm = 1;
  1863. else
  1864. settings.TmpDcm = 2;
  1865. settings.decimation = 0;
  1866. if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
  1867. settings.VerDcm = 2;
  1868. else
  1869. settings.VerDcm = 1;
  1870. if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
  1871. settings.HorDcm = 4;
  1872. else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
  1873. settings.HorDcm = 2;
  1874. else
  1875. settings.HorDcm = 1;
  1876. if (settings.TmpDcm == 1)
  1877. settings.field_per_buff = 2;
  1878. else
  1879. settings.field_per_buff = 1;
  1880. /* check */
  1881. res = zoran_check_jpg_settings(zr, &settings, 1);
  1882. if (res)
  1883. goto tryfmt_unlock_and_return;
  1884. /* tell the user what we actually did */
  1885. fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
  1886. fmt->fmt.pix.height = settings.img_height * 2 /
  1887. (settings.TmpDcm * settings.VerDcm);
  1888. if (settings.TmpDcm == 1)
  1889. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1890. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
  1891. else
  1892. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1893. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
  1894. fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
  1895. tryfmt_unlock_and_return:
  1896. mutex_unlock(&zr->resource_lock);
  1897. return res;
  1898. }
  1899. static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
  1900. struct v4l2_format *fmt)
  1901. {
  1902. struct zoran_fh *fh = __fh;
  1903. struct zoran *zr = fh->zr;
  1904. int bpp;
  1905. int i;
  1906. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
  1907. return zoran_try_fmt_vid_out(file, fh, fmt);
  1908. mutex_lock(&zr->resource_lock);
  1909. for (i = 0; i < NUM_FORMATS; i++)
  1910. if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
  1911. break;
  1912. if (i == NUM_FORMATS) {
  1913. mutex_unlock(&zr->resource_lock);
  1914. return -EINVAL;
  1915. }
  1916. bpp = (zoran_formats[i].depth + 7) / 8;
  1917. fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
  1918. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  1919. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  1920. if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
  1921. fmt->fmt.pix.width = BUZ_MIN_WIDTH;
  1922. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  1923. fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
  1924. if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
  1925. fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
  1926. mutex_unlock(&zr->resource_lock);
  1927. return 0;
  1928. }
  1929. static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
  1930. struct v4l2_format *fmt)
  1931. {
  1932. struct zoran_fh *fh = __fh;
  1933. struct zoran *zr = fh->zr;
  1934. int res;
  1935. dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
  1936. fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  1937. fmt->fmt.win.w.width,
  1938. fmt->fmt.win.w.height,
  1939. fmt->fmt.win.clipcount,
  1940. fmt->fmt.win.bitmap);
  1941. mutex_lock(&zr->resource_lock);
  1942. res = setup_window(file, fmt->fmt.win.w.left,
  1943. fmt->fmt.win.w.top,
  1944. fmt->fmt.win.w.width,
  1945. fmt->fmt.win.w.height,
  1946. (struct video_clip __user *)
  1947. fmt->fmt.win.clips,
  1948. fmt->fmt.win.clipcount,
  1949. fmt->fmt.win.bitmap);
  1950. mutex_unlock(&zr->resource_lock);
  1951. return res;
  1952. }
  1953. static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
  1954. struct v4l2_format *fmt)
  1955. {
  1956. struct zoran_fh *fh = __fh;
  1957. struct zoran *zr = fh->zr;
  1958. __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
  1959. struct zoran_jpg_settings settings;
  1960. int res = 0;
  1961. dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
  1962. fmt->fmt.pix.width, fmt->fmt.pix.height,
  1963. fmt->fmt.pix.pixelformat,
  1964. (char *) &printformat);
  1965. if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1966. return -EINVAL;
  1967. mutex_lock(&zr->resource_lock);
  1968. settings = fh->jpg_settings;
  1969. if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
  1970. dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  1971. ZR_DEVNAME(zr));
  1972. res = -EBUSY;
  1973. goto sfmtjpg_unlock_and_return;
  1974. }
  1975. /* we actually need to set 'real' parameters now */
  1976. if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
  1977. settings.TmpDcm = 1;
  1978. else
  1979. settings.TmpDcm = 2;
  1980. settings.decimation = 0;
  1981. if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
  1982. settings.VerDcm = 2;
  1983. else
  1984. settings.VerDcm = 1;
  1985. if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
  1986. settings.HorDcm = 4;
  1987. else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
  1988. settings.HorDcm = 2;
  1989. else
  1990. settings.HorDcm = 1;
  1991. if (settings.TmpDcm == 1)
  1992. settings.field_per_buff = 2;
  1993. else
  1994. settings.field_per_buff = 1;
  1995. if (settings.HorDcm > 1) {
  1996. settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
  1997. settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
  1998. } else {
  1999. settings.img_x = 0;
  2000. settings.img_width = BUZ_MAX_WIDTH;
  2001. }
  2002. /* check */
  2003. res = zoran_check_jpg_settings(zr, &settings, 0);
  2004. if (res)
  2005. goto sfmtjpg_unlock_and_return;
  2006. /* it's ok, so set them */
  2007. fh->jpg_settings = settings;
  2008. /* tell the user what we actually did */
  2009. fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
  2010. fmt->fmt.pix.height = settings.img_height * 2 /
  2011. (settings.TmpDcm * settings.VerDcm);
  2012. if (settings.TmpDcm == 1)
  2013. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  2014. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
  2015. else
  2016. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  2017. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
  2018. fh->jpg_buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2019. fmt->fmt.pix.bytesperline = 0;
  2020. fmt->fmt.pix.sizeimage = fh->jpg_buffers.buffer_size;
  2021. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  2022. /* we hereby abuse this variable to show that
  2023. * we're gonna do mjpeg capture */
  2024. fh->map_mode = (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
  2025. ZORAN_MAP_MODE_JPG_REC : ZORAN_MAP_MODE_JPG_PLAY;
  2026. sfmtjpg_unlock_and_return:
  2027. mutex_unlock(&zr->resource_lock);
  2028. return res;
  2029. }
  2030. static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
  2031. struct v4l2_format *fmt)
  2032. {
  2033. struct zoran_fh *fh = __fh;
  2034. struct zoran *zr = fh->zr;
  2035. int i;
  2036. int res = 0;
  2037. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
  2038. return zoran_s_fmt_vid_out(file, fh, fmt);
  2039. for (i = 0; i < NUM_FORMATS; i++)
  2040. if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
  2041. break;
  2042. if (i == NUM_FORMATS) {
  2043. dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
  2044. ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
  2045. return -EINVAL;
  2046. }
  2047. mutex_lock(&zr->resource_lock);
  2048. if (fh->jpg_buffers.allocated ||
  2049. (fh->v4l_buffers.allocated && fh->v4l_buffers.active != ZORAN_FREE)) {
  2050. dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2051. ZR_DEVNAME(zr));
  2052. res = -EBUSY;
  2053. goto sfmtv4l_unlock_and_return;
  2054. }
  2055. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  2056. fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
  2057. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  2058. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  2059. res = zoran_v4l_set_format(file, fmt->fmt.pix.width,
  2060. fmt->fmt.pix.height, &zoran_formats[i]);
  2061. if (res)
  2062. goto sfmtv4l_unlock_and_return;
  2063. /* tell the user the
  2064. * results/missing stuff */
  2065. fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
  2066. fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
  2067. fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
  2068. if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
  2069. fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
  2070. else
  2071. fmt->fmt.pix.field = V4L2_FIELD_TOP;
  2072. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2073. sfmtv4l_unlock_and_return:
  2074. mutex_unlock(&zr->resource_lock);
  2075. return res;
  2076. }
  2077. static int zoran_g_fbuf(struct file *file, void *__fh,
  2078. struct v4l2_framebuffer *fb)
  2079. {
  2080. struct zoran_fh *fh = __fh;
  2081. struct zoran *zr = fh->zr;
  2082. memset(fb, 0, sizeof(*fb));
  2083. mutex_lock(&zr->resource_lock);
  2084. fb->base = zr->buffer.base;
  2085. fb->fmt.width = zr->buffer.width;
  2086. fb->fmt.height = zr->buffer.height;
  2087. if (zr->overlay_settings.format)
  2088. fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
  2089. fb->fmt.bytesperline = zr->buffer.bytesperline;
  2090. mutex_unlock(&zr->resource_lock);
  2091. fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  2092. fb->fmt.field = V4L2_FIELD_INTERLACED;
  2093. fb->flags = V4L2_FBUF_FLAG_OVERLAY;
  2094. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2095. return 0;
  2096. }
  2097. static int zoran_s_fbuf(struct file *file, void *__fh,
  2098. struct v4l2_framebuffer *fb)
  2099. {
  2100. struct zoran_fh *fh = __fh;
  2101. struct zoran *zr = fh->zr;
  2102. int i, res = 0;
  2103. __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
  2104. for (i = 0; i < NUM_FORMATS; i++)
  2105. if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
  2106. break;
  2107. if (i == NUM_FORMATS) {
  2108. dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
  2109. ZR_DEVNAME(zr), fb->fmt.pixelformat,
  2110. (char *)&printformat);
  2111. return -EINVAL;
  2112. }
  2113. mutex_lock(&zr->resource_lock);
  2114. res = setup_fbuffer(file, fb->base, &zoran_formats[i],
  2115. fb->fmt.width, fb->fmt.height,
  2116. fb->fmt.bytesperline);
  2117. mutex_unlock(&zr->resource_lock);
  2118. return res;
  2119. }
  2120. static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
  2121. {
  2122. struct zoran_fh *fh = __fh;
  2123. struct zoran *zr = fh->zr;
  2124. int res;
  2125. mutex_lock(&zr->resource_lock);
  2126. res = setup_overlay(file, on);
  2127. mutex_unlock(&zr->resource_lock);
  2128. return res;
  2129. }
  2130. static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
  2131. static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
  2132. {
  2133. struct zoran_fh *fh = __fh;
  2134. struct zoran *zr = fh->zr;
  2135. int res = 0;
  2136. if (req->memory != V4L2_MEMORY_MMAP) {
  2137. dprintk(2,
  2138. KERN_ERR
  2139. "%s: only MEMORY_MMAP capture is supported, not %d\n",
  2140. ZR_DEVNAME(zr), req->memory);
  2141. return -EINVAL;
  2142. }
  2143. if (req->count == 0)
  2144. return zoran_streamoff(file, fh, req->type);
  2145. mutex_lock(&zr->resource_lock);
  2146. if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
  2147. dprintk(2,
  2148. KERN_ERR
  2149. "%s: VIDIOC_REQBUFS - buffers already allocated\n",
  2150. ZR_DEVNAME(zr));
  2151. res = -EBUSY;
  2152. goto v4l2reqbuf_unlock_and_return;
  2153. }
  2154. if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
  2155. req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2156. /* control user input */
  2157. if (req->count < 2)
  2158. req->count = 2;
  2159. if (req->count > v4l_nbufs)
  2160. req->count = v4l_nbufs;
  2161. fh->v4l_buffers.num_buffers = req->count;
  2162. if (v4l_fbuffer_alloc(file)) {
  2163. res = -ENOMEM;
  2164. goto v4l2reqbuf_unlock_and_return;
  2165. }
  2166. /* The next mmap will map the V4L buffers */
  2167. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2168. } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
  2169. fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2170. /* we need to calculate size ourselves now */
  2171. if (req->count < 4)
  2172. req->count = 4;
  2173. if (req->count > jpg_nbufs)
  2174. req->count = jpg_nbufs;
  2175. fh->jpg_buffers.num_buffers = req->count;
  2176. fh->jpg_buffers.buffer_size =
  2177. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2178. if (jpg_fbuffer_alloc(file)) {
  2179. res = -ENOMEM;
  2180. goto v4l2reqbuf_unlock_and_return;
  2181. }
  2182. /* The next mmap will map the MJPEG buffers */
  2183. if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2184. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2185. else
  2186. fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
  2187. } else {
  2188. dprintk(1,
  2189. KERN_ERR
  2190. "%s: VIDIOC_REQBUFS - unknown type %d\n",
  2191. ZR_DEVNAME(zr), req->type);
  2192. res = -EINVAL;
  2193. goto v4l2reqbuf_unlock_and_return;
  2194. }
  2195. v4l2reqbuf_unlock_and_return:
  2196. mutex_unlock(&zr->resource_lock);
  2197. return res;
  2198. }
  2199. static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  2200. {
  2201. struct zoran_fh *fh = __fh;
  2202. struct zoran *zr = fh->zr;
  2203. __u32 type = buf->type;
  2204. int index = buf->index, res;
  2205. memset(buf, 0, sizeof(*buf));
  2206. buf->type = type;
  2207. buf->index = index;
  2208. mutex_lock(&zr->resource_lock);
  2209. res = zoran_v4l2_buffer_status(file, buf, buf->index);
  2210. mutex_unlock(&zr->resource_lock);
  2211. return res;
  2212. }
  2213. static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  2214. {
  2215. struct zoran_fh *fh = __fh;
  2216. struct zoran *zr = fh->zr;
  2217. int res = 0, codec_mode, buf_type;
  2218. mutex_lock(&zr->resource_lock);
  2219. switch (fh->map_mode) {
  2220. case ZORAN_MAP_MODE_RAW:
  2221. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2222. dprintk(1, KERN_ERR
  2223. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2224. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2225. res = -EINVAL;
  2226. goto qbuf_unlock_and_return;
  2227. }
  2228. res = zoran_v4l_queue_frame(file, buf->index);
  2229. if (res)
  2230. goto qbuf_unlock_and_return;
  2231. if (!zr->v4l_memgrab_active &&
  2232. fh->v4l_buffers.active == ZORAN_LOCKED)
  2233. zr36057_set_memgrab(zr, 1);
  2234. break;
  2235. case ZORAN_MAP_MODE_JPG_REC:
  2236. case ZORAN_MAP_MODE_JPG_PLAY:
  2237. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2238. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2239. codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
  2240. } else {
  2241. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2242. codec_mode = BUZ_MODE_MOTION_COMPRESS;
  2243. }
  2244. if (buf->type != buf_type) {
  2245. dprintk(1, KERN_ERR
  2246. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2247. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2248. res = -EINVAL;
  2249. goto qbuf_unlock_and_return;
  2250. }
  2251. res = zoran_jpg_queue_frame(file, buf->index,
  2252. codec_mode);
  2253. if (res != 0)
  2254. goto qbuf_unlock_and_return;
  2255. if (zr->codec_mode == BUZ_MODE_IDLE &&
  2256. fh->jpg_buffers.active == ZORAN_LOCKED) {
  2257. zr36057_enable_jpg(zr, codec_mode);
  2258. }
  2259. break;
  2260. default:
  2261. dprintk(1, KERN_ERR
  2262. "%s: VIDIOC_QBUF - unsupported type %d\n",
  2263. ZR_DEVNAME(zr), buf->type);
  2264. res = -EINVAL;
  2265. break;
  2266. }
  2267. qbuf_unlock_and_return:
  2268. mutex_unlock(&zr->resource_lock);
  2269. return res;
  2270. }
  2271. static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  2272. {
  2273. struct zoran_fh *fh = __fh;
  2274. struct zoran *zr = fh->zr;
  2275. int res = 0, buf_type, num = -1; /* compiler borks here (?) */
  2276. mutex_lock(&zr->resource_lock);
  2277. switch (fh->map_mode) {
  2278. case ZORAN_MAP_MODE_RAW:
  2279. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2280. dprintk(1, KERN_ERR
  2281. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2282. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2283. res = -EINVAL;
  2284. goto dqbuf_unlock_and_return;
  2285. }
  2286. num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2287. if (file->f_flags & O_NONBLOCK &&
  2288. zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
  2289. res = -EAGAIN;
  2290. goto dqbuf_unlock_and_return;
  2291. }
  2292. res = v4l_sync(file, num);
  2293. if (res)
  2294. goto dqbuf_unlock_and_return;
  2295. zr->v4l_sync_tail++;
  2296. res = zoran_v4l2_buffer_status(file, buf, num);
  2297. break;
  2298. case ZORAN_MAP_MODE_JPG_REC:
  2299. case ZORAN_MAP_MODE_JPG_PLAY:
  2300. {
  2301. struct zoran_sync bs;
  2302. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
  2303. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2304. else
  2305. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2306. if (buf->type != buf_type) {
  2307. dprintk(1, KERN_ERR
  2308. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2309. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2310. res = -EINVAL;
  2311. goto dqbuf_unlock_and_return;
  2312. }
  2313. num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  2314. if (file->f_flags & O_NONBLOCK &&
  2315. zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
  2316. res = -EAGAIN;
  2317. goto dqbuf_unlock_and_return;
  2318. }
  2319. res = jpg_sync(file, &bs);
  2320. if (res)
  2321. goto dqbuf_unlock_and_return;
  2322. res = zoran_v4l2_buffer_status(file, buf, bs.frame);
  2323. break;
  2324. }
  2325. default:
  2326. dprintk(1, KERN_ERR
  2327. "%s: VIDIOC_DQBUF - unsupported type %d\n",
  2328. ZR_DEVNAME(zr), buf->type);
  2329. res = -EINVAL;
  2330. break;
  2331. }
  2332. dqbuf_unlock_and_return:
  2333. mutex_unlock(&zr->resource_lock);
  2334. return res;
  2335. }
  2336. static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
  2337. {
  2338. struct zoran_fh *fh = __fh;
  2339. struct zoran *zr = fh->zr;
  2340. int res = 0;
  2341. mutex_lock(&zr->resource_lock);
  2342. switch (fh->map_mode) {
  2343. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2344. if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
  2345. fh->v4l_buffers.active != ZORAN_ACTIVE) {
  2346. res = -EBUSY;
  2347. goto strmon_unlock_and_return;
  2348. }
  2349. zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
  2350. zr->v4l_settings = fh->v4l_settings;
  2351. zr->v4l_sync_tail = zr->v4l_pend_tail;
  2352. if (!zr->v4l_memgrab_active &&
  2353. zr->v4l_pend_head != zr->v4l_pend_tail) {
  2354. zr36057_set_memgrab(zr, 1);
  2355. }
  2356. break;
  2357. case ZORAN_MAP_MODE_JPG_REC:
  2358. case ZORAN_MAP_MODE_JPG_PLAY:
  2359. /* what is the codec mode right now? */
  2360. if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
  2361. fh->jpg_buffers.active != ZORAN_ACTIVE) {
  2362. res = -EBUSY;
  2363. goto strmon_unlock_and_return;
  2364. }
  2365. zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
  2366. if (zr->jpg_que_head != zr->jpg_que_tail) {
  2367. /* Start the jpeg codec when the first frame is queued */
  2368. jpeg_start(zr);
  2369. }
  2370. break;
  2371. default:
  2372. dprintk(1,
  2373. KERN_ERR
  2374. "%s: VIDIOC_STREAMON - invalid map mode %d\n",
  2375. ZR_DEVNAME(zr), fh->map_mode);
  2376. res = -EINVAL;
  2377. break;
  2378. }
  2379. strmon_unlock_and_return:
  2380. mutex_unlock(&zr->resource_lock);
  2381. return res;
  2382. }
  2383. static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
  2384. {
  2385. struct zoran_fh *fh = __fh;
  2386. struct zoran *zr = fh->zr;
  2387. int i, res = 0;
  2388. mutex_lock(&zr->resource_lock);
  2389. switch (fh->map_mode) {
  2390. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2391. if (fh->v4l_buffers.active == ZORAN_FREE &&
  2392. zr->v4l_buffers.active != ZORAN_FREE) {
  2393. res = -EPERM; /* stay off other's settings! */
  2394. goto strmoff_unlock_and_return;
  2395. }
  2396. if (zr->v4l_buffers.active == ZORAN_FREE)
  2397. goto strmoff_unlock_and_return;
  2398. /* unload capture */
  2399. if (zr->v4l_memgrab_active) {
  2400. unsigned long flags;
  2401. spin_lock_irqsave(&zr->spinlock, flags);
  2402. zr36057_set_memgrab(zr, 0);
  2403. spin_unlock_irqrestore(&zr->spinlock, flags);
  2404. }
  2405. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  2406. zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
  2407. fh->v4l_buffers = zr->v4l_buffers;
  2408. zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
  2409. zr->v4l_grab_seq = 0;
  2410. zr->v4l_pend_head = zr->v4l_pend_tail = 0;
  2411. zr->v4l_sync_tail = 0;
  2412. break;
  2413. case ZORAN_MAP_MODE_JPG_REC:
  2414. case ZORAN_MAP_MODE_JPG_PLAY:
  2415. if (fh->jpg_buffers.active == ZORAN_FREE &&
  2416. zr->jpg_buffers.active != ZORAN_FREE) {
  2417. res = -EPERM; /* stay off other's settings! */
  2418. goto strmoff_unlock_and_return;
  2419. }
  2420. if (zr->jpg_buffers.active == ZORAN_FREE)
  2421. goto strmoff_unlock_and_return;
  2422. res = jpg_qbuf(file, -1,
  2423. (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  2424. BUZ_MODE_MOTION_COMPRESS :
  2425. BUZ_MODE_MOTION_DECOMPRESS);
  2426. if (res)
  2427. goto strmoff_unlock_and_return;
  2428. break;
  2429. default:
  2430. dprintk(1, KERN_ERR
  2431. "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
  2432. ZR_DEVNAME(zr), fh->map_mode);
  2433. res = -EINVAL;
  2434. break;
  2435. }
  2436. strmoff_unlock_and_return:
  2437. mutex_unlock(&zr->resource_lock);
  2438. return res;
  2439. }
  2440. static int zoran_queryctrl(struct file *file, void *__fh,
  2441. struct v4l2_queryctrl *ctrl)
  2442. {
  2443. struct zoran_fh *fh = __fh;
  2444. struct zoran *zr = fh->zr;
  2445. /* we only support hue/saturation/contrast/brightness */
  2446. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2447. ctrl->id > V4L2_CID_HUE)
  2448. return -EINVAL;
  2449. decoder_command(zr, VIDIOC_QUERYCTRL, ctrl);
  2450. return 0;
  2451. }
  2452. static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
  2453. {
  2454. struct zoran_fh *fh = __fh;
  2455. struct zoran *zr = fh->zr;
  2456. /* we only support hue/saturation/contrast/brightness */
  2457. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2458. ctrl->id > V4L2_CID_HUE)
  2459. return -EINVAL;
  2460. mutex_lock(&zr->resource_lock);
  2461. decoder_command(zr, VIDIOC_G_CTRL, ctrl);
  2462. mutex_unlock(&zr->resource_lock);
  2463. return 0;
  2464. }
  2465. static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
  2466. {
  2467. struct zoran_fh *fh = __fh;
  2468. struct zoran *zr = fh->zr;
  2469. /* we only support hue/saturation/contrast/brightness */
  2470. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2471. ctrl->id > V4L2_CID_HUE)
  2472. return -EINVAL;
  2473. mutex_lock(&zr->resource_lock);
  2474. decoder_command(zr, VIDIOC_S_CTRL, ctrl);
  2475. mutex_unlock(&zr->resource_lock);
  2476. return 0;
  2477. }
  2478. static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
  2479. {
  2480. struct zoran_fh *fh = __fh;
  2481. struct zoran *zr = fh->zr;
  2482. mutex_lock(&zr->resource_lock);
  2483. *std = zr->norm;
  2484. mutex_unlock(&zr->resource_lock);
  2485. return 0;
  2486. }
  2487. static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
  2488. {
  2489. struct zoran_fh *fh = __fh;
  2490. struct zoran *zr = fh->zr;
  2491. int res = 0;
  2492. mutex_lock(&zr->resource_lock);
  2493. res = zoran_set_norm(zr, *std);
  2494. if (res)
  2495. goto sstd_unlock_and_return;
  2496. res = wait_grab_pending(zr);
  2497. sstd_unlock_and_return:
  2498. mutex_unlock(&zr->resource_lock);
  2499. return res;
  2500. }
  2501. static int zoran_enum_input(struct file *file, void *__fh,
  2502. struct v4l2_input *inp)
  2503. {
  2504. struct zoran_fh *fh = __fh;
  2505. struct zoran *zr = fh->zr;
  2506. if (inp->index < 0 || inp->index >= zr->card.inputs)
  2507. return -EINVAL;
  2508. else {
  2509. int id = inp->index;
  2510. memset(inp, 0, sizeof(*inp));
  2511. inp->index = id;
  2512. }
  2513. strncpy(inp->name, zr->card.input[inp->index].name,
  2514. sizeof(inp->name) - 1);
  2515. inp->type = V4L2_INPUT_TYPE_CAMERA;
  2516. inp->std = V4L2_STD_ALL;
  2517. /* Get status of video decoder */
  2518. mutex_lock(&zr->resource_lock);
  2519. decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &inp->status);
  2520. mutex_unlock(&zr->resource_lock);
  2521. return 0;
  2522. }
  2523. static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
  2524. {
  2525. struct zoran_fh *fh = __fh;
  2526. struct zoran *zr = fh->zr;
  2527. mutex_lock(&zr->resource_lock);
  2528. *input = zr->input;
  2529. mutex_unlock(&zr->resource_lock);
  2530. return 0;
  2531. }
  2532. static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
  2533. {
  2534. struct zoran_fh *fh = __fh;
  2535. struct zoran *zr = fh->zr;
  2536. int res;
  2537. mutex_lock(&zr->resource_lock);
  2538. res = zoran_set_input(zr, input);
  2539. if (res)
  2540. goto sinput_unlock_and_return;
  2541. /* Make sure the changes come into effect */
  2542. res = wait_grab_pending(zr);
  2543. sinput_unlock_and_return:
  2544. mutex_unlock(&zr->resource_lock);
  2545. return res;
  2546. }
  2547. static int zoran_enum_output(struct file *file, void *__fh,
  2548. struct v4l2_output *outp)
  2549. {
  2550. if (outp->index != 0)
  2551. return -EINVAL;
  2552. memset(outp, 0, sizeof(*outp));
  2553. outp->index = 0;
  2554. outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
  2555. strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
  2556. return 0;
  2557. }
  2558. static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
  2559. {
  2560. *output = 0;
  2561. return 0;
  2562. }
  2563. static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
  2564. {
  2565. if (output != 0)
  2566. return -EINVAL;
  2567. return 0;
  2568. }
  2569. /* cropping (sub-frame capture) */
  2570. static int zoran_cropcap(struct file *file, void *__fh,
  2571. struct v4l2_cropcap *cropcap)
  2572. {
  2573. struct zoran_fh *fh = __fh;
  2574. struct zoran *zr = fh->zr;
  2575. int type = cropcap->type, res = 0;
  2576. memset(cropcap, 0, sizeof(*cropcap));
  2577. cropcap->type = type;
  2578. mutex_lock(&zr->resource_lock);
  2579. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2580. (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2581. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  2582. dprintk(1, KERN_ERR
  2583. "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
  2584. ZR_DEVNAME(zr));
  2585. res = -EINVAL;
  2586. goto cropcap_unlock_and_return;
  2587. }
  2588. cropcap->bounds.top = cropcap->bounds.left = 0;
  2589. cropcap->bounds.width = BUZ_MAX_WIDTH;
  2590. cropcap->bounds.height = BUZ_MAX_HEIGHT;
  2591. cropcap->defrect.top = cropcap->defrect.left = 0;
  2592. cropcap->defrect.width = BUZ_MIN_WIDTH;
  2593. cropcap->defrect.height = BUZ_MIN_HEIGHT;
  2594. cropcap_unlock_and_return:
  2595. mutex_unlock(&zr->resource_lock);
  2596. return res;
  2597. }
  2598. static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
  2599. {
  2600. struct zoran_fh *fh = __fh;
  2601. struct zoran *zr = fh->zr;
  2602. int type = crop->type, res = 0;
  2603. memset(crop, 0, sizeof(*crop));
  2604. crop->type = type;
  2605. mutex_lock(&zr->resource_lock);
  2606. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2607. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2608. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  2609. dprintk(1,
  2610. KERN_ERR
  2611. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  2612. ZR_DEVNAME(zr));
  2613. res = -EINVAL;
  2614. goto gcrop_unlock_and_return;
  2615. }
  2616. crop->c.top = fh->jpg_settings.img_y;
  2617. crop->c.left = fh->jpg_settings.img_x;
  2618. crop->c.width = fh->jpg_settings.img_width;
  2619. crop->c.height = fh->jpg_settings.img_height;
  2620. gcrop_unlock_and_return:
  2621. mutex_unlock(&zr->resource_lock);
  2622. return res;
  2623. }
  2624. static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
  2625. {
  2626. struct zoran_fh *fh = __fh;
  2627. struct zoran *zr = fh->zr;
  2628. int res = 0;
  2629. struct zoran_jpg_settings settings;
  2630. settings = fh->jpg_settings;
  2631. mutex_lock(&zr->resource_lock);
  2632. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  2633. dprintk(1, KERN_ERR
  2634. "%s: VIDIOC_S_CROP - cannot change settings while active\n",
  2635. ZR_DEVNAME(zr));
  2636. res = -EBUSY;
  2637. goto scrop_unlock_and_return;
  2638. }
  2639. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2640. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2641. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  2642. dprintk(1, KERN_ERR
  2643. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  2644. ZR_DEVNAME(zr));
  2645. res = -EINVAL;
  2646. goto scrop_unlock_and_return;
  2647. }
  2648. /* move into a form that we understand */
  2649. settings.img_x = crop->c.left;
  2650. settings.img_y = crop->c.top;
  2651. settings.img_width = crop->c.width;
  2652. settings.img_height = crop->c.height;
  2653. /* check validity */
  2654. res = zoran_check_jpg_settings(zr, &settings, 0);
  2655. if (res)
  2656. goto scrop_unlock_and_return;
  2657. /* accept */
  2658. fh->jpg_settings = settings;
  2659. scrop_unlock_and_return:
  2660. mutex_unlock(&zr->resource_lock);
  2661. return res;
  2662. }
  2663. static int zoran_g_jpegcomp(struct file *file, void *__fh,
  2664. struct v4l2_jpegcompression *params)
  2665. {
  2666. struct zoran_fh *fh = __fh;
  2667. struct zoran *zr = fh->zr;
  2668. memset(params, 0, sizeof(*params));
  2669. mutex_lock(&zr->resource_lock);
  2670. params->quality = fh->jpg_settings.jpg_comp.quality;
  2671. params->APPn = fh->jpg_settings.jpg_comp.APPn;
  2672. memcpy(params->APP_data,
  2673. fh->jpg_settings.jpg_comp.APP_data,
  2674. fh->jpg_settings.jpg_comp.APP_len);
  2675. params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  2676. memcpy(params->COM_data,
  2677. fh->jpg_settings.jpg_comp.COM_data,
  2678. fh->jpg_settings.jpg_comp.COM_len);
  2679. params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
  2680. params->jpeg_markers =
  2681. fh->jpg_settings.jpg_comp.jpeg_markers;
  2682. mutex_unlock(&zr->resource_lock);
  2683. return 0;
  2684. }
  2685. static int zoran_s_jpegcomp(struct file *file, void *__fh,
  2686. struct v4l2_jpegcompression *params)
  2687. {
  2688. struct zoran_fh *fh = __fh;
  2689. struct zoran *zr = fh->zr;
  2690. int res = 0;
  2691. struct zoran_jpg_settings settings;
  2692. settings = fh->jpg_settings;
  2693. settings.jpg_comp = *params;
  2694. mutex_lock(&zr->resource_lock);
  2695. if (fh->v4l_buffers.active != ZORAN_FREE ||
  2696. fh->jpg_buffers.active != ZORAN_FREE) {
  2697. dprintk(1, KERN_WARNING
  2698. "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
  2699. ZR_DEVNAME(zr));
  2700. res = -EBUSY;
  2701. goto sjpegc_unlock_and_return;
  2702. }
  2703. res = zoran_check_jpg_settings(zr, &settings, 0);
  2704. if (res)
  2705. goto sjpegc_unlock_and_return;
  2706. if (!fh->jpg_buffers.allocated)
  2707. fh->jpg_buffers.buffer_size =
  2708. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2709. fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
  2710. sjpegc_unlock_and_return:
  2711. mutex_unlock(&zr->resource_lock);
  2712. return 0;
  2713. }
  2714. static unsigned int
  2715. zoran_poll (struct file *file,
  2716. poll_table *wait)
  2717. {
  2718. struct zoran_fh *fh = file->private_data;
  2719. struct zoran *zr = fh->zr;
  2720. int res = 0, frame;
  2721. unsigned long flags;
  2722. /* we should check whether buffers are ready to be synced on
  2723. * (w/o waits - O_NONBLOCK) here
  2724. * if ready for read (sync), return POLLIN|POLLRDNORM,
  2725. * if ready for write (sync), return POLLOUT|POLLWRNORM,
  2726. * if error, return POLLERR,
  2727. * if no buffers queued or so, return POLLNVAL
  2728. */
  2729. mutex_lock(&zr->resource_lock);
  2730. switch (fh->map_mode) {
  2731. case ZORAN_MAP_MODE_RAW:
  2732. poll_wait(file, &zr->v4l_capq, wait);
  2733. frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2734. spin_lock_irqsave(&zr->spinlock, flags);
  2735. dprintk(3,
  2736. KERN_DEBUG
  2737. "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
  2738. ZR_DEVNAME(zr), __func__,
  2739. "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
  2740. "UPMD"[zr->v4l_buffers.buffer[frame].state],
  2741. zr->v4l_pend_tail, zr->v4l_pend_head);
  2742. /* Process is the one capturing? */
  2743. if (fh->v4l_buffers.active != ZORAN_FREE &&
  2744. /* Buffer ready to DQBUF? */
  2745. zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
  2746. res = POLLIN | POLLRDNORM;
  2747. spin_unlock_irqrestore(&zr->spinlock, flags);
  2748. break;
  2749. case ZORAN_MAP_MODE_JPG_REC:
  2750. case ZORAN_MAP_MODE_JPG_PLAY:
  2751. poll_wait(file, &zr->jpg_capq, wait);
  2752. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  2753. spin_lock_irqsave(&zr->spinlock, flags);
  2754. dprintk(3,
  2755. KERN_DEBUG
  2756. "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
  2757. ZR_DEVNAME(zr), __func__,
  2758. "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
  2759. "UPMD"[zr->jpg_buffers.buffer[frame].state],
  2760. zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
  2761. if (fh->jpg_buffers.active != ZORAN_FREE &&
  2762. zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
  2763. if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
  2764. res = POLLIN | POLLRDNORM;
  2765. else
  2766. res = POLLOUT | POLLWRNORM;
  2767. }
  2768. spin_unlock_irqrestore(&zr->spinlock, flags);
  2769. break;
  2770. default:
  2771. dprintk(1,
  2772. KERN_ERR
  2773. "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
  2774. ZR_DEVNAME(zr), fh->map_mode);
  2775. res = POLLNVAL;
  2776. }
  2777. mutex_unlock(&zr->resource_lock);
  2778. return res;
  2779. }
  2780. /*
  2781. * This maps the buffers to user space.
  2782. *
  2783. * Depending on the state of fh->map_mode
  2784. * the V4L or the MJPEG buffers are mapped
  2785. * per buffer or all together
  2786. *
  2787. * Note that we need to connect to some
  2788. * unmap signal event to unmap the de-allocate
  2789. * the buffer accordingly (zoran_vm_close())
  2790. */
  2791. static void
  2792. zoran_vm_open (struct vm_area_struct *vma)
  2793. {
  2794. struct zoran_mapping *map = vma->vm_private_data;
  2795. map->count++;
  2796. }
  2797. static void
  2798. zoran_vm_close (struct vm_area_struct *vma)
  2799. {
  2800. struct zoran_mapping *map = vma->vm_private_data;
  2801. struct file *file = map->file;
  2802. struct zoran_fh *fh = file->private_data;
  2803. struct zoran *zr = fh->zr;
  2804. int i;
  2805. map->count--;
  2806. if (map->count == 0) {
  2807. switch (fh->map_mode) {
  2808. case ZORAN_MAP_MODE_JPG_REC:
  2809. case ZORAN_MAP_MODE_JPG_PLAY:
  2810. dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
  2811. ZR_DEVNAME(zr));
  2812. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  2813. if (fh->jpg_buffers.buffer[i].map == map) {
  2814. fh->jpg_buffers.buffer[i].map =
  2815. NULL;
  2816. }
  2817. }
  2818. kfree(map);
  2819. for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
  2820. if (fh->jpg_buffers.buffer[i].map)
  2821. break;
  2822. if (i == fh->jpg_buffers.num_buffers) {
  2823. mutex_lock(&zr->resource_lock);
  2824. if (fh->jpg_buffers.active != ZORAN_FREE) {
  2825. jpg_qbuf(file, -1, zr->codec_mode);
  2826. zr->jpg_buffers.allocated = 0;
  2827. zr->jpg_buffers.active =
  2828. fh->jpg_buffers.active =
  2829. ZORAN_FREE;
  2830. }
  2831. jpg_fbuffer_free(file);
  2832. mutex_unlock(&zr->resource_lock);
  2833. }
  2834. break;
  2835. case ZORAN_MAP_MODE_RAW:
  2836. dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
  2837. ZR_DEVNAME(zr));
  2838. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  2839. if (fh->v4l_buffers.buffer[i].map == map) {
  2840. /* unqueue/unmap */
  2841. fh->v4l_buffers.buffer[i].map =
  2842. NULL;
  2843. }
  2844. }
  2845. kfree(map);
  2846. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  2847. if (fh->v4l_buffers.buffer[i].map)
  2848. break;
  2849. if (i == fh->v4l_buffers.num_buffers) {
  2850. mutex_lock(&zr->resource_lock);
  2851. if (fh->v4l_buffers.active != ZORAN_FREE) {
  2852. unsigned long flags;
  2853. spin_lock_irqsave(&zr->spinlock, flags);
  2854. zr36057_set_memgrab(zr, 0);
  2855. zr->v4l_buffers.allocated = 0;
  2856. zr->v4l_buffers.active =
  2857. fh->v4l_buffers.active =
  2858. ZORAN_FREE;
  2859. spin_unlock_irqrestore(&zr->spinlock, flags);
  2860. }
  2861. v4l_fbuffer_free(file);
  2862. mutex_unlock(&zr->resource_lock);
  2863. }
  2864. break;
  2865. default:
  2866. printk(KERN_ERR
  2867. "%s: munmap() - internal error - unknown map mode %d\n",
  2868. ZR_DEVNAME(zr), fh->map_mode);
  2869. break;
  2870. }
  2871. }
  2872. }
  2873. static struct vm_operations_struct zoran_vm_ops = {
  2874. .open = zoran_vm_open,
  2875. .close = zoran_vm_close,
  2876. };
  2877. static int
  2878. zoran_mmap (struct file *file,
  2879. struct vm_area_struct *vma)
  2880. {
  2881. struct zoran_fh *fh = file->private_data;
  2882. struct zoran *zr = fh->zr;
  2883. unsigned long size = (vma->vm_end - vma->vm_start);
  2884. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  2885. int i, j;
  2886. unsigned long page, start = vma->vm_start, todo, pos, fraglen;
  2887. int first, last;
  2888. struct zoran_mapping *map;
  2889. int res = 0;
  2890. dprintk(3,
  2891. KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
  2892. ZR_DEVNAME(zr),
  2893. fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
  2894. vma->vm_start, vma->vm_end, size);
  2895. if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
  2896. !(vma->vm_flags & VM_WRITE)) {
  2897. dprintk(1,
  2898. KERN_ERR
  2899. "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
  2900. ZR_DEVNAME(zr));
  2901. return -EINVAL;
  2902. }
  2903. switch (fh->map_mode) {
  2904. case ZORAN_MAP_MODE_JPG_REC:
  2905. case ZORAN_MAP_MODE_JPG_PLAY:
  2906. /* lock */
  2907. mutex_lock(&zr->resource_lock);
  2908. /* Map the MJPEG buffers */
  2909. if (!fh->jpg_buffers.allocated) {
  2910. dprintk(1,
  2911. KERN_ERR
  2912. "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
  2913. ZR_DEVNAME(zr));
  2914. res = -ENOMEM;
  2915. goto jpg_mmap_unlock_and_return;
  2916. }
  2917. first = offset / fh->jpg_buffers.buffer_size;
  2918. last = first - 1 + size / fh->jpg_buffers.buffer_size;
  2919. if (offset % fh->jpg_buffers.buffer_size != 0 ||
  2920. size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
  2921. last < 0 || first >= fh->jpg_buffers.num_buffers ||
  2922. last >= fh->jpg_buffers.num_buffers) {
  2923. dprintk(1,
  2924. KERN_ERR
  2925. "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  2926. ZR_DEVNAME(zr), offset, size,
  2927. fh->jpg_buffers.buffer_size,
  2928. fh->jpg_buffers.num_buffers);
  2929. res = -EINVAL;
  2930. goto jpg_mmap_unlock_and_return;
  2931. }
  2932. for (i = first; i <= last; i++) {
  2933. if (fh->jpg_buffers.buffer[i].map) {
  2934. dprintk(1,
  2935. KERN_ERR
  2936. "%s: mmap(MJPEG) - buffer %d already mapped\n",
  2937. ZR_DEVNAME(zr), i);
  2938. res = -EBUSY;
  2939. goto jpg_mmap_unlock_and_return;
  2940. }
  2941. }
  2942. /* map these buffers (v4l_buffers[i]) */
  2943. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  2944. if (!map) {
  2945. res = -ENOMEM;
  2946. goto jpg_mmap_unlock_and_return;
  2947. }
  2948. map->file = file;
  2949. map->count = 1;
  2950. vma->vm_ops = &zoran_vm_ops;
  2951. vma->vm_flags |= VM_DONTEXPAND;
  2952. vma->vm_private_data = map;
  2953. for (i = first; i <= last; i++) {
  2954. for (j = 0;
  2955. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  2956. j++) {
  2957. fraglen =
  2958. (le32_to_cpu(fh->jpg_buffers.buffer[i].
  2959. frag_tab[2 * j + 1]) & ~1) << 1;
  2960. todo = size;
  2961. if (todo > fraglen)
  2962. todo = fraglen;
  2963. pos =
  2964. le32_to_cpu(fh->jpg_buffers.
  2965. buffer[i].frag_tab[2 * j]);
  2966. /* should just be pos on i386 */
  2967. page = virt_to_phys(bus_to_virt(pos))
  2968. >> PAGE_SHIFT;
  2969. if (remap_pfn_range(vma, start, page,
  2970. todo, PAGE_SHARED)) {
  2971. dprintk(1,
  2972. KERN_ERR
  2973. "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
  2974. ZR_DEVNAME(zr));
  2975. res = -EAGAIN;
  2976. goto jpg_mmap_unlock_and_return;
  2977. }
  2978. size -= todo;
  2979. start += todo;
  2980. if (size == 0)
  2981. break;
  2982. if (le32_to_cpu(fh->jpg_buffers.buffer[i].
  2983. frag_tab[2 * j + 1]) & 1)
  2984. break; /* was last fragment */
  2985. }
  2986. fh->jpg_buffers.buffer[i].map = map;
  2987. if (size == 0)
  2988. break;
  2989. }
  2990. jpg_mmap_unlock_and_return:
  2991. mutex_unlock(&zr->resource_lock);
  2992. break;
  2993. case ZORAN_MAP_MODE_RAW:
  2994. mutex_lock(&zr->resource_lock);
  2995. /* Map the V4L buffers */
  2996. if (!fh->v4l_buffers.allocated) {
  2997. dprintk(1,
  2998. KERN_ERR
  2999. "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
  3000. ZR_DEVNAME(zr));
  3001. res = -ENOMEM;
  3002. goto v4l_mmap_unlock_and_return;
  3003. }
  3004. first = offset / fh->v4l_buffers.buffer_size;
  3005. last = first - 1 + size / fh->v4l_buffers.buffer_size;
  3006. if (offset % fh->v4l_buffers.buffer_size != 0 ||
  3007. size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
  3008. last < 0 || first >= fh->v4l_buffers.num_buffers ||
  3009. last >= fh->v4l_buffers.buffer_size) {
  3010. dprintk(1,
  3011. KERN_ERR
  3012. "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3013. ZR_DEVNAME(zr), offset, size,
  3014. fh->v4l_buffers.buffer_size,
  3015. fh->v4l_buffers.num_buffers);
  3016. res = -EINVAL;
  3017. goto v4l_mmap_unlock_and_return;
  3018. }
  3019. for (i = first; i <= last; i++) {
  3020. if (fh->v4l_buffers.buffer[i].map) {
  3021. dprintk(1,
  3022. KERN_ERR
  3023. "%s: mmap(V4L) - buffer %d already mapped\n",
  3024. ZR_DEVNAME(zr), i);
  3025. res = -EBUSY;
  3026. goto v4l_mmap_unlock_and_return;
  3027. }
  3028. }
  3029. /* map these buffers (v4l_buffers[i]) */
  3030. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3031. if (!map) {
  3032. res = -ENOMEM;
  3033. goto v4l_mmap_unlock_and_return;
  3034. }
  3035. map->file = file;
  3036. map->count = 1;
  3037. vma->vm_ops = &zoran_vm_ops;
  3038. vma->vm_flags |= VM_DONTEXPAND;
  3039. vma->vm_private_data = map;
  3040. for (i = first; i <= last; i++) {
  3041. todo = size;
  3042. if (todo > fh->v4l_buffers.buffer_size)
  3043. todo = fh->v4l_buffers.buffer_size;
  3044. page = fh->v4l_buffers.buffer[i].fbuffer_phys;
  3045. if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
  3046. todo, PAGE_SHARED)) {
  3047. dprintk(1,
  3048. KERN_ERR
  3049. "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
  3050. ZR_DEVNAME(zr));
  3051. res = -EAGAIN;
  3052. goto v4l_mmap_unlock_and_return;
  3053. }
  3054. size -= todo;
  3055. start += todo;
  3056. fh->v4l_buffers.buffer[i].map = map;
  3057. if (size == 0)
  3058. break;
  3059. }
  3060. v4l_mmap_unlock_and_return:
  3061. mutex_unlock(&zr->resource_lock);
  3062. break;
  3063. default:
  3064. dprintk(1,
  3065. KERN_ERR
  3066. "%s: zoran_mmap() - internal error - unknown map mode %d\n",
  3067. ZR_DEVNAME(zr), fh->map_mode);
  3068. break;
  3069. }
  3070. return 0;
  3071. }
  3072. static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
  3073. .vidioc_querycap = zoran_querycap,
  3074. .vidioc_cropcap = zoran_cropcap,
  3075. .vidioc_s_crop = zoran_s_crop,
  3076. .vidioc_g_crop = zoran_g_crop,
  3077. .vidioc_enum_input = zoran_enum_input,
  3078. .vidioc_g_input = zoran_g_input,
  3079. .vidioc_s_input = zoran_s_input,
  3080. .vidioc_enum_output = zoran_enum_output,
  3081. .vidioc_g_output = zoran_g_output,
  3082. .vidioc_s_output = zoran_s_output,
  3083. .vidioc_g_fbuf = zoran_g_fbuf,
  3084. .vidioc_s_fbuf = zoran_s_fbuf,
  3085. .vidioc_g_std = zoran_g_std,
  3086. .vidioc_s_std = zoran_s_std,
  3087. .vidioc_g_jpegcomp = zoran_g_jpegcomp,
  3088. .vidioc_s_jpegcomp = zoran_s_jpegcomp,
  3089. .vidioc_overlay = zoran_overlay,
  3090. .vidioc_reqbufs = zoran_reqbufs,
  3091. .vidioc_querybuf = zoran_querybuf,
  3092. .vidioc_qbuf = zoran_qbuf,
  3093. .vidioc_dqbuf = zoran_dqbuf,
  3094. .vidioc_streamon = zoran_streamon,
  3095. .vidioc_streamoff = zoran_streamoff,
  3096. .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
  3097. .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
  3098. .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
  3099. .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
  3100. .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
  3101. .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
  3102. .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
  3103. .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
  3104. .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
  3105. .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
  3106. .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
  3107. .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
  3108. .vidioc_queryctrl = zoran_queryctrl,
  3109. .vidioc_s_ctrl = zoran_s_ctrl,
  3110. .vidioc_g_ctrl = zoran_g_ctrl,
  3111. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  3112. .vidioc_default = zoran_default,
  3113. .vidiocgmbuf = zoran_vidiocgmbuf,
  3114. #endif
  3115. };
  3116. static const struct v4l2_file_operations zoran_fops = {
  3117. .owner = THIS_MODULE,
  3118. .open = zoran_open,
  3119. .release = zoran_close,
  3120. .ioctl = video_ioctl2,
  3121. .read = zoran_read,
  3122. .write = zoran_write,
  3123. .mmap = zoran_mmap,
  3124. .poll = zoran_poll,
  3125. };
  3126. struct video_device zoran_template __devinitdata = {
  3127. .name = ZORAN_NAME,
  3128. .fops = &zoran_fops,
  3129. .ioctl_ops = &zoran_ioctl_ops,
  3130. .release = &zoran_vdev_release,
  3131. .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
  3132. .minor = -1
  3133. };