zoran_driver.c 88 KB

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