zoran_driver.c 92 KB

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