zoran_driver.c 79 KB

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