zoran_driver.c 92 KB

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