zr36120.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. /*
  2. zr36120.c - Zoran 36120/36125 based framegrabbers
  3. Copyright (C) 1998-1999 Pauline Middelink <middelin@polyware.nl>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/delay.h>
  18. #include <linux/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/kernel.h>
  22. #include <linux/major.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/mm.h>
  26. #include <linux/pci.h>
  27. #include <linux/signal.h>
  28. #include <linux/wait.h>
  29. #include <asm/io.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/page.h>
  32. #include <linux/sched.h>
  33. #include <linux/video_decoder.h>
  34. #include <asm/uaccess.h>
  35. #include "tuner.h"
  36. #include "zr36120.h"
  37. #include "zr36120_mem.h"
  38. /* mark an required function argument unused - lintism */
  39. #define UNUSED(x) (void)(x)
  40. /* sensible default */
  41. #ifndef CARDTYPE
  42. #define CARDTYPE 0
  43. #endif
  44. /* Anybody who uses more than four? */
  45. #define ZORAN_MAX 4
  46. static unsigned int triton1=0; /* triton1 chipset? */
  47. static unsigned int cardtype[ZORAN_MAX]={ [ 0 ... ZORAN_MAX-1 ] = CARDTYPE };
  48. static int video_nr = -1;
  49. static int vbi_nr = -1;
  50. static struct pci_device_id zr36120_pci_tbl[] = {
  51. { PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120,
  52. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  53. { 0 }
  54. };
  55. MODULE_DEVICE_TABLE(pci, zr36120_pci_tbl);
  56. MODULE_AUTHOR("Pauline Middelink <middelin@polyware.nl>");
  57. MODULE_DESCRIPTION("Zoran ZR36120 based framegrabber");
  58. MODULE_LICENSE("GPL");
  59. MODULE_PARM(triton1,"i");
  60. MODULE_PARM(cardtype,"1-" __MODULE_STRING(ZORAN_MAX) "i");
  61. MODULE_PARM(video_nr,"i");
  62. MODULE_PARM(vbi_nr,"i");
  63. static int zoran_cards;
  64. static struct zoran zorans[ZORAN_MAX];
  65. /*
  66. * the meaning of each element can be found in zr36120.h
  67. * Determining the value of gpdir/gpval can be tricky. The
  68. * best way is to run the card under the original software
  69. * and read the values from the general purpose registers
  70. * 0x28 and 0x2C. How you do that is left as an exercise
  71. * to the impatient reader :)
  72. */
  73. #define T 1 /* to separate the bools from the ints */
  74. #define F 0
  75. static struct tvcard tvcards[] = {
  76. /* reported working by <middelin@polyware.nl> */
  77. /*0*/ { "Trust Victor II",
  78. 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
  79. /* reported working by <Michael.Paxton@aihw.gov.au> */
  80. /*1*/ { "Aitech WaveWatcher TV-PCI",
  81. 3, 0, T, F, T, T, 0x7F, 0x80, { 1, TUNER(3), SVHS(6) }, { 0 } },
  82. /* reported working by ? */
  83. /*2*/ { "Genius Video Wonder PCI Video Capture Card",
  84. 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
  85. /* reported working by <Pascal.Gabriel@wanadoo.fr> */
  86. /*3*/ { "Guillemot Maxi-TV PCI",
  87. 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
  88. /* reported working by "Craig Whitmore <lennon@igrin.co.nz> */
  89. /*4*/ { "Quadrant Buster",
  90. 3, 3, T, F, T, T, 0x7F, 0x80, { SVHS(1), TUNER(2), 3 }, { 1, 2, 3 } },
  91. /* a debug entry which has all inputs mapped */
  92. /*5*/ { "ZR36120 based framegrabber (all inputs enabled)",
  93. 6, 0, T, T, T, T, 0x7F, 0x80, { 1, 2, 3, 4, 5, 6 }, { 0 } }
  94. };
  95. #undef T
  96. #undef F
  97. #define NRTVCARDS (sizeof(tvcards)/sizeof(tvcards[0]))
  98. #ifdef __sparc__
  99. #define ENDIANESS 0
  100. #else
  101. #define ENDIANESS ZORAN_VFEC_LE
  102. #endif
  103. static struct { const char name[8]; uint mode; uint bpp; } palette2fmt[] = {
  104. /* n/a */ { "n/a", 0, 0 },
  105. /* GREY */ { "GRAY", 0, 0 },
  106. /* HI240 */ { "HI240", 0, 0 },
  107. /* RGB565 */ { "RGB565", ZORAN_VFEC_RGB_RGB565|ENDIANESS, 2 },
  108. /* RGB24 */ { "RGB24", ZORAN_VFEC_RGB_RGB888|ENDIANESS|ZORAN_VFEC_PACK24, 3 },
  109. /* RGB32 */ { "RGB32", ZORAN_VFEC_RGB_RGB888|ENDIANESS, 4 },
  110. /* RGB555 */ { "RGB555", ZORAN_VFEC_RGB_RGB555|ENDIANESS, 2 },
  111. /* YUV422 */ { "YUV422", ZORAN_VFEC_RGB_YUV422|ENDIANESS, 2 },
  112. /* YUYV */ { "YUYV", 0, 0 },
  113. /* UYVY */ { "UYVY", 0, 0 },
  114. /* YUV420 */ { "YUV420", 0, 0 },
  115. /* YUV411 */ { "YUV411", 0, 0 },
  116. /* RAW */ { "RAW", 0, 0 },
  117. /* YUV422P */ { "YUV422P", 0, 0 },
  118. /* YUV411P */ { "YUV411P", 0, 0 }};
  119. #define NRPALETTES (sizeof(palette2fmt)/sizeof(palette2fmt[0]))
  120. #undef ENDIANESS
  121. /* ----------------------------------------------------------------------- */
  122. /* ZORAN chipset detector */
  123. /* shamelessly stolen from bttv.c */
  124. /* Reason for beeing here: we need to detect if we are running on a */
  125. /* Triton based chipset, and if so, enable a certain bit */
  126. /* ----------------------------------------------------------------------- */
  127. static
  128. void __init handle_chipset(void)
  129. {
  130. /* Just in case some nut set this to something dangerous */
  131. if (triton1)
  132. triton1 = ZORAN_VDC_TRICOM;
  133. if (pci_pci_problems & PCIPCI_TRITON) {
  134. printk(KERN_INFO "zoran: Host bridge 82437FX Triton PIIX\n");
  135. triton1 = ZORAN_VDC_TRICOM;
  136. }
  137. }
  138. /* ----------------------------------------------------------------------- */
  139. /* ZORAN functions */
  140. /* ----------------------------------------------------------------------- */
  141. static void zoran_set_geo(struct zoran* ztv, struct vidinfo* i);
  142. #if 0 /* unused */
  143. static
  144. void zoran_dump(struct zoran *ztv)
  145. {
  146. char str[256];
  147. char *p=str; /* shut up, gcc! */
  148. int i;
  149. for (i=0; i<0x60; i+=4) {
  150. if ((i % 16) == 0) {
  151. if (i) printk("%s\n",str);
  152. p = str;
  153. p+= sprintf(str, KERN_DEBUG " %04x: ",i);
  154. }
  155. p += sprintf(p, "%08x ",zrread(i));
  156. }
  157. }
  158. #endif /* unused */
  159. static
  160. void reap_states(struct zoran* ztv)
  161. {
  162. /* count frames */
  163. ztv->fieldnr++;
  164. /*
  165. * Are we busy at all?
  166. * This depends on if there is a workqueue AND the
  167. * videotransfer is enabled on the chip...
  168. */
  169. if (ztv->workqueue && (zrread(ZORAN_VDC) & ZORAN_VDC_VIDEN))
  170. {
  171. struct vidinfo* newitem;
  172. /* did we get a complete frame? */
  173. if (zrread(ZORAN_VSTR) & ZORAN_VSTR_GRAB)
  174. return;
  175. DEBUG(printk(CARD_DEBUG "completed %s at %p\n",CARD,ztv->workqueue->kindof==FBUFFER_GRAB?"grab":"read",ztv->workqueue));
  176. /* we are done with this buffer, tell everyone */
  177. ztv->workqueue->status = FBUFFER_DONE;
  178. ztv->workqueue->fieldnr = ztv->fieldnr;
  179. /* not good, here for BTTV_FIELDNR reasons */
  180. ztv->lastfieldnr = ztv->fieldnr;
  181. switch (ztv->workqueue->kindof) {
  182. case FBUFFER_GRAB:
  183. wake_up_interruptible(&ztv->grabq);
  184. break;
  185. case FBUFFER_VBI:
  186. wake_up_interruptible(&ztv->vbiq);
  187. break;
  188. default:
  189. printk(CARD_INFO "somebody killed the workqueue (kindof=%d)!\n",CARD,ztv->workqueue->kindof);
  190. }
  191. /* item completed, skip to next item in queue */
  192. write_lock(&ztv->lock);
  193. newitem = ztv->workqueue->next;
  194. ztv->workqueue->next = 0; /* mark completed */
  195. ztv->workqueue = newitem;
  196. write_unlock(&ztv->lock);
  197. }
  198. /*
  199. * ok, so it seems we have nothing in progress right now.
  200. * Lets see if we can find some work.
  201. */
  202. if (ztv->workqueue)
  203. {
  204. struct vidinfo* newitem;
  205. again:
  206. DEBUG(printk(CARD_DEBUG "starting %s at %p\n",CARD,ztv->workqueue->kindof==FBUFFER_GRAB?"grab":"read",ztv->workqueue));
  207. /* loadup the frame settings */
  208. read_lock(&ztv->lock);
  209. zoran_set_geo(ztv,ztv->workqueue);
  210. read_unlock(&ztv->lock);
  211. switch (ztv->workqueue->kindof) {
  212. case FBUFFER_GRAB:
  213. case FBUFFER_VBI:
  214. zrand(~ZORAN_OCR_OVLEN, ZORAN_OCR);
  215. zror(ZORAN_VSTR_SNAPSHOT,ZORAN_VSTR);
  216. zror(ZORAN_VDC_VIDEN,ZORAN_VDC);
  217. /* start single-shot grab */
  218. zror(ZORAN_VSTR_GRAB, ZORAN_VSTR);
  219. break;
  220. default:
  221. printk(CARD_INFO "what is this doing on the queue? (kindof=%d)\n",CARD,ztv->workqueue->kindof);
  222. write_lock(&ztv->lock);
  223. newitem = ztv->workqueue->next;
  224. ztv->workqueue->next = 0;
  225. ztv->workqueue = newitem;
  226. write_unlock(&ztv->lock);
  227. if (newitem)
  228. goto again; /* yeah, sure.. */
  229. }
  230. /* bye for now */
  231. return;
  232. }
  233. DEBUG(printk(CARD_DEBUG "nothing in queue\n",CARD));
  234. /*
  235. * What? Even the workqueue is empty? Am i really here
  236. * for nothing? Did i come all that way to... do nothing?
  237. */
  238. /* do we need to overlay? */
  239. if (test_bit(STATE_OVERLAY, &ztv->state))
  240. {
  241. /* are we already overlaying? */
  242. if (!(zrread(ZORAN_OCR) & ZORAN_OCR_OVLEN) ||
  243. !(zrread(ZORAN_VDC) & ZORAN_VDC_VIDEN))
  244. {
  245. DEBUG(printk(CARD_DEBUG "starting overlay\n",CARD));
  246. read_lock(&ztv->lock);
  247. zoran_set_geo(ztv,&ztv->overinfo);
  248. read_unlock(&ztv->lock);
  249. zror(ZORAN_OCR_OVLEN, ZORAN_OCR);
  250. zrand(~ZORAN_VSTR_SNAPSHOT,ZORAN_VSTR);
  251. zror(ZORAN_VDC_VIDEN,ZORAN_VDC);
  252. }
  253. /*
  254. * leave overlaying on, but turn interrupts off.
  255. */
  256. zrand(~ZORAN_ICR_EN,ZORAN_ICR);
  257. return;
  258. }
  259. /* do we have any VBI idle time processing? */
  260. if (test_bit(STATE_VBI, &ztv->state))
  261. {
  262. struct vidinfo* item;
  263. struct vidinfo* lastitem;
  264. /* protect the workqueue */
  265. write_lock(&ztv->lock);
  266. lastitem = ztv->workqueue;
  267. if (lastitem)
  268. while (lastitem->next) lastitem = lastitem->next;
  269. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  270. if (item->next == 0 && item->status == FBUFFER_FREE)
  271. {
  272. DEBUG(printk(CARD_DEBUG "%p added to queue\n",CARD,item));
  273. item->status = FBUFFER_BUSY;
  274. if (!lastitem)
  275. ztv->workqueue = item;
  276. else
  277. lastitem->next = item;
  278. lastitem = item;
  279. }
  280. write_unlock(&ztv->lock);
  281. if (ztv->workqueue)
  282. goto again; /* hey, _i_ graduated :) */
  283. }
  284. /*
  285. * Then we must be realy IDLE
  286. */
  287. DEBUG(printk(CARD_DEBUG "turning off\n",CARD));
  288. /* nothing further to do, disable DMA and further IRQs */
  289. zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
  290. zrand(~ZORAN_ICR_EN,ZORAN_ICR);
  291. }
  292. static
  293. void zoran_irq(int irq, void *dev_id, struct pt_regs * regs)
  294. {
  295. u32 stat,estat;
  296. int count = 0;
  297. struct zoran *ztv = dev_id;
  298. UNUSED(irq); UNUSED(regs);
  299. for (;;) {
  300. /* get/clear interrupt status bits */
  301. stat=zrread(ZORAN_ISR);
  302. estat=stat & zrread(ZORAN_ICR);
  303. if (!estat)
  304. return;
  305. zrwrite(estat,ZORAN_ISR);
  306. IDEBUG(printk(CARD_DEBUG "estat %08x\n",CARD,estat));
  307. IDEBUG(printk(CARD_DEBUG " stat %08x\n",CARD,stat));
  308. if (estat & ZORAN_ISR_CODE)
  309. {
  310. IDEBUG(printk(CARD_DEBUG "CodReplIRQ\n",CARD));
  311. }
  312. if (estat & ZORAN_ISR_GIRQ0)
  313. {
  314. IDEBUG(printk(CARD_DEBUG "GIRQ0\n",CARD));
  315. if (!ztv->card->usegirq1)
  316. reap_states(ztv);
  317. }
  318. if (estat & ZORAN_ISR_GIRQ1)
  319. {
  320. IDEBUG(printk(CARD_DEBUG "GIRQ1\n",CARD));
  321. if (ztv->card->usegirq1)
  322. reap_states(ztv);
  323. }
  324. count++;
  325. if (count > 10)
  326. printk(CARD_ERR "irq loop %d (%x)\n",CARD,count,estat);
  327. if (count > 20)
  328. {
  329. zrwrite(0, ZORAN_ICR);
  330. printk(CARD_ERR "IRQ lockup, cleared int mask\n",CARD);
  331. }
  332. }
  333. }
  334. static
  335. int zoran_muxsel(struct zoran* ztv, int channel, int norm)
  336. {
  337. int rv;
  338. /* set the new video norm */
  339. rv = i2c_control_device(&(ztv->i2c), I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &norm);
  340. if (rv)
  341. return rv;
  342. ztv->norm = norm;
  343. /* map the given channel to the cards decoder's channel */
  344. channel = ztv->card->video_mux[channel] & CHANNEL_MASK;
  345. /* set the new channel */
  346. rv = i2c_control_device(&(ztv->i2c), I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &channel);
  347. return rv;
  348. }
  349. /* Tell the interrupt handler what to to. */
  350. static
  351. void zoran_cap(struct zoran* ztv, int on)
  352. {
  353. DEBUG(printk(CARD_DEBUG "zoran_cap(%d) state=%x\n",CARD,on,ztv->state));
  354. if (on) {
  355. ztv->running = 1;
  356. /*
  357. * turn interrupts (back) on. The DMA will be enabled
  358. * inside the irq handler when it detects a restart.
  359. */
  360. zror(ZORAN_ICR_EN,ZORAN_ICR);
  361. }
  362. else {
  363. /*
  364. * turn both interrupts and DMA off
  365. */
  366. zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
  367. zrand(~ZORAN_ICR_EN,ZORAN_ICR);
  368. ztv->running = 0;
  369. }
  370. }
  371. static ulong dmask[] = {
  372. 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8,
  373. 0xFFFFFFF0, 0xFFFFFFE0, 0xFFFFFFC0, 0xFFFFFF80,
  374. 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
  375. 0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000,
  376. 0xFFFF0000, 0xFFFE0000, 0xFFFC0000, 0xFFF80000,
  377. 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
  378. 0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000,
  379. 0xF0000000, 0xE0000000, 0xC0000000, 0x80000000
  380. };
  381. static
  382. void zoran_built_overlay(struct zoran* ztv, int count, struct video_clip *vcp)
  383. {
  384. ulong* mtop;
  385. int ystep = (ztv->vidXshift + ztv->vidWidth+31)/32; /* next DWORD */
  386. int i;
  387. DEBUG(printk(KERN_DEBUG " overlay at %p, ystep=%d, clips=%d\n",ztv->overinfo.overlay,ystep,count));
  388. for (i=0; i<count; i++) {
  389. struct video_clip *vp = vcp+i;
  390. UNUSED(vp);
  391. DEBUG(printk(KERN_DEBUG " %d: clip(%d,%d,%d,%d)\n", i,vp->x,vp->y,vp->width,vp->height));
  392. }
  393. /*
  394. * activate the visible portion of the screen
  395. * Note we take some shortcuts here, because we
  396. * know the width can never be < 32. (I.e. a DWORD)
  397. * We also assume the overlay starts somewhere in
  398. * the FIRST dword.
  399. */
  400. {
  401. int start = ztv->vidXshift;
  402. ulong firstd = dmask[start];
  403. ulong lastd = ~dmask[(start + ztv->overinfo.w) & 31];
  404. mtop = ztv->overinfo.overlay;
  405. for (i=0; i<ztv->overinfo.h; i++) {
  406. int w = ztv->vidWidth;
  407. ulong* line = mtop;
  408. if (start & 31) {
  409. *line++ = firstd;
  410. w -= 32-(start&31);
  411. }
  412. memset(line, ~0, w/8);
  413. if (w & 31)
  414. line[w/32] = lastd;
  415. mtop += ystep;
  416. }
  417. }
  418. /* process clipping regions */
  419. for (i=0; i<count; i++) {
  420. int h;
  421. if (vcp->x < 0 || (uint)vcp->x > ztv->overinfo.w ||
  422. vcp->y < 0 || vcp->y > ztv->overinfo.h ||
  423. vcp->width < 0 || (uint)(vcp->x+vcp->width) > ztv->overinfo.w ||
  424. vcp->height < 0 || (vcp->y+vcp->height) > ztv->overinfo.h)
  425. {
  426. DEBUG(printk(CARD_DEBUG "invalid clipzone (%d,%d,%d,%d) not in (0,0,%d,%d), adapting\n",CARD,vcp->x,vcp->y,vcp->width,vcp->height,ztv->overinfo.w,ztv->overinfo.h));
  427. if (vcp->x < 0) vcp->x = 0;
  428. if ((uint)vcp->x > ztv->overinfo.w) vcp->x = ztv->overinfo.w;
  429. if (vcp->y < 0) vcp->y = 0;
  430. if (vcp->y > ztv->overinfo.h) vcp->y = ztv->overinfo.h;
  431. if (vcp->width < 0) vcp->width = 0;
  432. if ((uint)(vcp->x+vcp->width) > ztv->overinfo.w) vcp->width = ztv->overinfo.w - vcp->x;
  433. if (vcp->height < 0) vcp->height = 0;
  434. if (vcp->y+vcp->height > ztv->overinfo.h) vcp->height = ztv->overinfo.h - vcp->y;
  435. // continue;
  436. }
  437. mtop = &ztv->overinfo.overlay[vcp->y*ystep];
  438. for (h=0; h<=vcp->height; h++) {
  439. int w;
  440. int x = ztv->vidXshift + vcp->x;
  441. for (w=0; w<=vcp->width; w++) {
  442. clear_bit(x&31, &mtop[x/32]);
  443. x++;
  444. }
  445. mtop += ystep;
  446. }
  447. ++vcp;
  448. }
  449. mtop = ztv->overinfo.overlay;
  450. zrwrite(virt_to_bus(mtop), ZORAN_MTOP);
  451. zrwrite(virt_to_bus(mtop+ystep), ZORAN_MBOT);
  452. zraor((ztv->vidInterlace*ystep)<<0,~ZORAN_OCR_MASKSTRIDE,ZORAN_OCR);
  453. }
  454. struct tvnorm
  455. {
  456. u16 Wt, Wa, Ht, Ha, HStart, VStart;
  457. };
  458. static struct tvnorm tvnorms[] = {
  459. /* PAL-BDGHI */
  460. /* { 864, 720, 625, 576, 131, 21 },*/
  461. /*00*/ { 864, 768, 625, 576, 81, 17 },
  462. /* NTSC */
  463. /*01*/ { 858, 720, 525, 480, 121, 10 },
  464. /* SECAM */
  465. /*02*/ { 864, 720, 625, 576, 131, 21 },
  466. /* BW50 */
  467. /*03*/ { 864, 720, 625, 576, 131, 21 },
  468. /* BW60 */
  469. /*04*/ { 858, 720, 525, 480, 121, 10 }
  470. };
  471. #define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
  472. /*
  473. * Program the chip for a setup as described in the vidinfo struct.
  474. *
  475. * Side-effects: calculates vidXshift, vidInterlace,
  476. * vidHeight, vidWidth which are used in a later stage
  477. * to calculate the overlay mask
  478. *
  479. * This is an internal function, as such it does not check the
  480. * validity of the struct members... Spectaculair crashes will
  481. * follow /very/ quick when you're wrong and the chip right :)
  482. */
  483. static
  484. void zoran_set_geo(struct zoran* ztv, struct vidinfo* i)
  485. {
  486. ulong top, bot;
  487. int stride;
  488. int winWidth, winHeight;
  489. int maxWidth, maxHeight, maxXOffset, maxYOffset;
  490. long vfec;
  491. DEBUG(printk(CARD_DEBUG "set_geo(rect=(%d,%d,%d,%d), norm=%d, format=%d, bpp=%d, bpl=%d, busadr=%lx, overlay=%p)\n",CARD,i->x,i->y,i->w,i->h,ztv->norm,i->format,i->bpp,i->bpl,i->busadr,i->overlay));
  492. /*
  493. * make sure the DMA transfers are inhibited during our
  494. * reprogramming of the chip
  495. */
  496. zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
  497. maxWidth = tvnorms[ztv->norm].Wa;
  498. maxHeight = tvnorms[ztv->norm].Ha/2;
  499. maxXOffset = tvnorms[ztv->norm].HStart;
  500. maxYOffset = tvnorms[ztv->norm].VStart;
  501. /* setup vfec register (keep ExtFl,TopField and VCLKPol settings) */
  502. vfec = (zrread(ZORAN_VFEC) & (ZORAN_VFEC_EXTFL|ZORAN_VFEC_TOPFIELD|ZORAN_VFEC_VCLKPOL)) |
  503. (palette2fmt[i->format].mode & (ZORAN_VFEC_RGB|ZORAN_VFEC_ERRDIF|ZORAN_VFEC_LE|ZORAN_VFEC_PACK24));
  504. /*
  505. * Set top, bottom ptrs. Since these must be DWORD aligned,
  506. * possible adjust the x and the width of the window.
  507. * so the endposition stay the same. The vidXshift will make
  508. * sure we are not writing pixels before the requested x.
  509. */
  510. ztv->vidXshift = 0;
  511. winWidth = i->w;
  512. if (winWidth < 0)
  513. winWidth = -winWidth;
  514. top = i->busadr + i->x*i->bpp + i->y*i->bpl;
  515. if (top & 3) {
  516. ztv->vidXshift = (top & 3) / i->bpp;
  517. winWidth += ztv->vidXshift;
  518. DEBUG(printk(KERN_DEBUG " window-x shifted %d pixels left\n",ztv->vidXshift));
  519. top &= ~3;
  520. }
  521. /*
  522. * bottom points to next frame but in interleaved mode we want
  523. * to 'mix' the 2 frames to one capture, so 'bot' points to one
  524. * (physical) line below the top line.
  525. */
  526. bot = top + i->bpl;
  527. zrwrite(top,ZORAN_VTOP);
  528. zrwrite(bot,ZORAN_VBOT);
  529. /*
  530. * Make sure the winWidth is DWORD aligned too,
  531. * thereby automaticly making sure the stride to the
  532. * next line is DWORD aligned too (as required by spec).
  533. */
  534. if ((winWidth*i->bpp) & 3) {
  535. DEBUG(printk(KERN_DEBUG " window-width enlarged by %d pixels\n",(winWidth*i->bpp) & 3));
  536. winWidth += (winWidth*i->bpp) & 3;
  537. }
  538. /* determine the DispMode and stride */
  539. if (i->h >= 0 && i->h <= maxHeight) {
  540. /* single frame grab suffices for this height. */
  541. vfec |= ZORAN_VFEC_DISPMOD;
  542. ztv->vidInterlace = 0;
  543. stride = i->bpl - (winWidth*i->bpp);
  544. winHeight = i->h;
  545. }
  546. else {
  547. /* interleaving needed for this height */
  548. ztv->vidInterlace = 1;
  549. stride = i->bpl*2 - (winWidth*i->bpp);
  550. winHeight = i->h/2;
  551. }
  552. if (winHeight < 0) /* can happen for VBI! */
  553. winHeight = -winHeight;
  554. /* safety net, sometimes bpl is too short??? */
  555. if (stride<0) {
  556. DEBUG(printk(CARD_DEBUG "WARNING stride = %d\n",CARD,stride));
  557. stride = 0;
  558. }
  559. zraor((winHeight<<12)|(winWidth<<0),~(ZORAN_VDC_VIDWINHT|ZORAN_VDC_VIDWINWID), ZORAN_VDC);
  560. zraor(stride<<16,~ZORAN_VSTR_DISPSTRIDE,ZORAN_VSTR);
  561. /* remember vidWidth, vidHeight for overlay calculations */
  562. ztv->vidWidth = winWidth;
  563. ztv->vidHeight = winHeight;
  564. DEBUG(printk(KERN_DEBUG " top=%08lx, bottom=%08lx\n",top,bot));
  565. DEBUG(printk(KERN_DEBUG " winWidth=%d, winHeight=%d\n",winWidth,winHeight));
  566. DEBUG(printk(KERN_DEBUG " maxWidth=%d, maxHeight=%d\n",maxWidth,maxHeight));
  567. DEBUG(printk(KERN_DEBUG " stride=%d\n",stride));
  568. /*
  569. * determine horizontal scales and crops
  570. */
  571. if (i->w < 0) {
  572. int Hstart = 1;
  573. int Hend = Hstart + winWidth;
  574. DEBUG(printk(KERN_DEBUG " Y: scale=0, start=%d, end=%d\n", Hstart, Hend));
  575. zraor((Hstart<<10)|(Hend<<0),~(ZORAN_VFEH_HSTART|ZORAN_VFEH_HEND),ZORAN_VFEH);
  576. }
  577. else {
  578. int Wa = maxWidth;
  579. int X = (winWidth*64+Wa-1)/Wa;
  580. int We = winWidth*64/X;
  581. int HorDcm = 64-X;
  582. int hcrop1 = 2*(Wa-We)/4;
  583. /*
  584. * BUGFIX: Juha Nurmela <junki@qn-lpr2-165.quicknet.inet.fi>
  585. * found the solution to the color phase shift.
  586. * See ChangeLog for the full explanation)
  587. */
  588. int Hstart = (maxXOffset + hcrop1) | 1;
  589. int Hend = Hstart + We - 1;
  590. DEBUG(printk(KERN_DEBUG " X: scale=%d, start=%d, end=%d\n", HorDcm, Hstart, Hend));
  591. zraor((Hstart<<10)|(Hend<<0),~(ZORAN_VFEH_HSTART|ZORAN_VFEH_HEND),ZORAN_VFEH);
  592. vfec |= HorDcm<<14;
  593. if (HorDcm<16)
  594. vfec |= ZORAN_VFEC_HFILTER_1; /* no filter */
  595. else if (HorDcm<32)
  596. vfec |= ZORAN_VFEC_HFILTER_3; /* 3 tap filter */
  597. else if (HorDcm<48)
  598. vfec |= ZORAN_VFEC_HFILTER_4; /* 4 tap filter */
  599. else vfec |= ZORAN_VFEC_HFILTER_5; /* 5 tap filter */
  600. }
  601. /*
  602. * Determine vertical scales and crops
  603. *
  604. * when height is negative, we want to read starting at line 0
  605. * One day someone might need access to these lines...
  606. */
  607. if (i->h < 0) {
  608. int Vstart = 0;
  609. int Vend = Vstart + winHeight;
  610. DEBUG(printk(KERN_DEBUG " Y: scale=0, start=%d, end=%d\n", Vstart, Vend));
  611. zraor((Vstart<<10)|(Vend<<0),~(ZORAN_VFEV_VSTART|ZORAN_VFEV_VEND),ZORAN_VFEV);
  612. }
  613. else {
  614. int Ha = maxHeight;
  615. int Y = (winHeight*64+Ha-1)/Ha;
  616. int He = winHeight*64/Y;
  617. int VerDcm = 64-Y;
  618. int vcrop1 = 2*(Ha-He)/4;
  619. int Vstart = maxYOffset + vcrop1;
  620. int Vend = Vstart + He - 1;
  621. DEBUG(printk(KERN_DEBUG " Y: scale=%d, start=%d, end=%d\n", VerDcm, Vstart, Vend));
  622. zraor((Vstart<<10)|(Vend<<0),~(ZORAN_VFEV_VSTART|ZORAN_VFEV_VEND),ZORAN_VFEV);
  623. vfec |= VerDcm<<8;
  624. }
  625. DEBUG(printk(KERN_DEBUG " F: format=%d(=%s)\n",i->format,palette2fmt[i->format].name));
  626. /* setup the requested format */
  627. zrwrite(vfec, ZORAN_VFEC);
  628. }
  629. static
  630. void zoran_common_open(struct zoran* ztv, int flags)
  631. {
  632. UNUSED(flags);
  633. /* already opened? */
  634. if (ztv->users++ != 0)
  635. return;
  636. /* unmute audio */
  637. /* /what/ audio? */
  638. ztv->state = 0;
  639. /* setup the encoder to the initial values */
  640. ztv->picture.colour=254<<7;
  641. ztv->picture.brightness=128<<8;
  642. ztv->picture.hue=128<<8;
  643. ztv->picture.contrast=216<<7;
  644. i2c_control_device(&ztv->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &ztv->picture);
  645. /* default to the composite input since my camera is there */
  646. zoran_muxsel(ztv, 0, VIDEO_MODE_PAL);
  647. }
  648. static
  649. void zoran_common_close(struct zoran* ztv)
  650. {
  651. if (--ztv->users != 0)
  652. return;
  653. /* mute audio */
  654. /* /what/ audio? */
  655. /* stop the chip */
  656. zoran_cap(ztv, 0);
  657. }
  658. /*
  659. * Open a zoran card. Right now the flags are just a hack
  660. */
  661. static int zoran_open(struct video_device *dev, int flags)
  662. {
  663. struct zoran *ztv = (struct zoran*)dev;
  664. struct vidinfo* item;
  665. char* pos;
  666. DEBUG(printk(CARD_DEBUG "open(dev,%d)\n",CARD,flags));
  667. /*********************************************
  668. * We really should be doing lazy allocing...
  669. *********************************************/
  670. /* allocate a frame buffer */
  671. if (!ztv->fbuffer)
  672. ztv->fbuffer = bmalloc(ZORAN_MAX_FBUFSIZE);
  673. if (!ztv->fbuffer) {
  674. /* could not get a buffer, bail out */
  675. return -ENOBUFS;
  676. }
  677. /* at this time we _always_ have a framebuffer */
  678. memset(ztv->fbuffer,0,ZORAN_MAX_FBUFSIZE);
  679. if (!ztv->overinfo.overlay)
  680. ztv->overinfo.overlay = kmalloc(1024*1024/8, GFP_KERNEL);
  681. if (!ztv->overinfo.overlay) {
  682. /* could not get an overlay buffer, bail out */
  683. bfree(ztv->fbuffer, ZORAN_MAX_FBUFSIZE);
  684. return -ENOBUFS;
  685. }
  686. /* at this time we _always_ have a overlay */
  687. /* clear buffer status, and give them a DMAable address */
  688. pos = ztv->fbuffer;
  689. for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
  690. {
  691. item->status = FBUFFER_FREE;
  692. item->memadr = pos;
  693. item->busadr = virt_to_bus(pos);
  694. pos += ZORAN_MAX_FBUFFER;
  695. }
  696. /* do the common part of all open's */
  697. zoran_common_open(ztv, flags);
  698. return 0;
  699. }
  700. static
  701. void zoran_close(struct video_device* dev)
  702. {
  703. struct zoran *ztv = (struct zoran*)dev;
  704. DEBUG(printk(CARD_DEBUG "close(dev)\n",CARD));
  705. /* driver specific closure */
  706. clear_bit(STATE_OVERLAY, &ztv->state);
  707. zoran_common_close(ztv);
  708. /*
  709. * This is sucky but right now I can't find a good way to
  710. * be sure its safe to free the buffer. We wait 5-6 fields
  711. * which is more than sufficient to be sure.
  712. */
  713. msleep(100); /* Wait 1/10th of a second */
  714. /* free the allocated framebuffer */
  715. if (ztv->fbuffer)
  716. bfree( ztv->fbuffer, ZORAN_MAX_FBUFSIZE );
  717. ztv->fbuffer = 0;
  718. if (ztv->overinfo.overlay)
  719. kfree( ztv->overinfo.overlay );
  720. ztv->overinfo.overlay = 0;
  721. }
  722. /*
  723. * This read function could be used reentrant in a SMP situation.
  724. *
  725. * This is made possible by the spinlock which is kept till we
  726. * found and marked a buffer for our own use. The lock must
  727. * be released as soon as possible to prevent lock contention.
  728. */
  729. static
  730. long zoran_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
  731. {
  732. struct zoran *ztv = (struct zoran*)dev;
  733. unsigned long max;
  734. struct vidinfo* unused = 0;
  735. struct vidinfo* done = 0;
  736. DEBUG(printk(CARD_DEBUG "zoran_read(%p,%ld,%d)\n",CARD,buf,count,nonblock));
  737. /* find ourself a free or completed buffer */
  738. for (;;) {
  739. struct vidinfo* item;
  740. write_lock_irq(&ztv->lock);
  741. for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
  742. {
  743. if (!unused && item->status == FBUFFER_FREE)
  744. unused = item;
  745. if (!done && item->status == FBUFFER_DONE)
  746. done = item;
  747. }
  748. if (done || unused)
  749. break;
  750. /* no more free buffers, wait for them. */
  751. write_unlock_irq(&ztv->lock);
  752. if (nonblock)
  753. return -EWOULDBLOCK;
  754. interruptible_sleep_on(&ztv->grabq);
  755. if (signal_pending(current))
  756. return -EINTR;
  757. }
  758. /* Do we have 'ready' data? */
  759. if (!done) {
  760. /* no? than this will take a while... */
  761. if (nonblock) {
  762. write_unlock_irq(&ztv->lock);
  763. return -EWOULDBLOCK;
  764. }
  765. /* mark the unused buffer as wanted */
  766. unused->status = FBUFFER_BUSY;
  767. unused->w = 320;
  768. unused->h = 240;
  769. unused->format = VIDEO_PALETTE_RGB24;
  770. unused->bpp = palette2fmt[unused->format].bpp;
  771. unused->bpl = unused->w * unused->bpp;
  772. unused->next = 0;
  773. { /* add to tail of queue */
  774. struct vidinfo* oldframe = ztv->workqueue;
  775. if (!oldframe) ztv->workqueue = unused;
  776. else {
  777. while (oldframe->next) oldframe = oldframe->next;
  778. oldframe->next = unused;
  779. }
  780. }
  781. write_unlock_irq(&ztv->lock);
  782. /* tell the state machine we want it filled /NOW/ */
  783. zoran_cap(ztv, 1);
  784. /* wait till this buffer gets grabbed */
  785. wait_event_interruptible(ztv->grabq,
  786. (unused->status != FBUFFER_BUSY));
  787. /* see if a signal did it */
  788. if (signal_pending(current))
  789. return -EINTR;
  790. done = unused;
  791. }
  792. else
  793. write_unlock_irq(&ztv->lock);
  794. /* Yes! we got data! */
  795. max = done->bpl * done->h;
  796. if (count > max)
  797. count = max;
  798. if (copy_to_user((void*)buf, done->memadr, count))
  799. count = -EFAULT;
  800. /* keep the engine running */
  801. done->status = FBUFFER_FREE;
  802. // zoran_cap(ztv,1);
  803. /* tell listeners this buffer became free */
  804. wake_up_interruptible(&ztv->grabq);
  805. /* goodbye */
  806. DEBUG(printk(CARD_DEBUG "zoran_read() returns %lu\n",CARD,count));
  807. return count;
  808. }
  809. static
  810. long zoran_write(struct video_device* dev, const char* buf, unsigned long count, int nonblock)
  811. {
  812. struct zoran *ztv = (struct zoran *)dev;
  813. UNUSED(ztv); UNUSED(dev); UNUSED(buf); UNUSED(count); UNUSED(nonblock);
  814. DEBUG(printk(CARD_DEBUG "zoran_write\n",CARD));
  815. return -EINVAL;
  816. }
  817. static
  818. unsigned int zoran_poll(struct video_device *dev, struct file *file, poll_table *wait)
  819. {
  820. struct zoran *ztv = (struct zoran *)dev;
  821. struct vidinfo* item;
  822. unsigned int mask = 0;
  823. poll_wait(file, &ztv->grabq, wait);
  824. for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
  825. if (item->status == FBUFFER_DONE)
  826. {
  827. mask |= (POLLIN | POLLRDNORM);
  828. break;
  829. }
  830. DEBUG(printk(CARD_DEBUG "zoran_poll()=%x\n",CARD,mask));
  831. return mask;
  832. }
  833. /* append a new clipregion to the vector of video_clips */
  834. static
  835. void new_clip(struct video_window* vw, struct video_clip* vcp, int x, int y, int w, int h)
  836. {
  837. vcp[vw->clipcount].x = x;
  838. vcp[vw->clipcount].y = y;
  839. vcp[vw->clipcount].width = w;
  840. vcp[vw->clipcount].height = h;
  841. vw->clipcount++;
  842. }
  843. static
  844. int zoran_ioctl(struct video_device* dev, unsigned int cmd, void *arg)
  845. {
  846. struct zoran* ztv = (struct zoran*)dev;
  847. switch (cmd) {
  848. case VIDIOCGCAP:
  849. {
  850. struct video_capability c;
  851. DEBUG(printk(CARD_DEBUG "VIDIOCGCAP\n",CARD));
  852. strcpy(c.name,ztv->video_dev.name);
  853. c.type = VID_TYPE_CAPTURE|
  854. VID_TYPE_OVERLAY|
  855. VID_TYPE_CLIPPING|
  856. VID_TYPE_FRAMERAM|
  857. VID_TYPE_SCALES;
  858. if (ztv->have_tuner)
  859. c.type |= VID_TYPE_TUNER;
  860. if (ztv->have_decoder) {
  861. c.channels = ztv->card->video_inputs;
  862. c.audios = ztv->card->audio_inputs;
  863. } else
  864. /* no decoder -> no channels */
  865. c.channels = c.audios = 0;
  866. c.maxwidth = 768;
  867. c.maxheight = 576;
  868. c.minwidth = 32;
  869. c.minheight = 32;
  870. if (copy_to_user(arg,&c,sizeof(c)))
  871. return -EFAULT;
  872. break;
  873. }
  874. case VIDIOCGCHAN:
  875. {
  876. struct video_channel v;
  877. int mux;
  878. if (copy_from_user(&v, arg,sizeof(v)))
  879. return -EFAULT;
  880. DEBUG(printk(CARD_DEBUG "VIDIOCGCHAN(%d)\n",CARD,v.channel));
  881. v.flags=VIDEO_VC_AUDIO
  882. #ifdef VIDEO_VC_NORM
  883. |VIDEO_VC_NORM
  884. #endif
  885. ;
  886. v.tuners=0;
  887. v.type=VIDEO_TYPE_CAMERA;
  888. #ifdef I_EXPECT_POSSIBLE_NORMS_IN_THE_API
  889. v.norm=VIDEO_MODE_PAL|
  890. VIDEO_MODE_NTSC|
  891. VIDEO_MODE_SECAM;
  892. #else
  893. v.norm=VIDEO_MODE_PAL;
  894. #endif
  895. /* too many inputs? no decoder -> no channels */
  896. if (!ztv->have_decoder || v.channel < 0 || v.channel >= ztv->card->video_inputs)
  897. return -EINVAL;
  898. /* now determine the name of the channel */
  899. mux = ztv->card->video_mux[v.channel];
  900. if (mux & IS_TUNER) {
  901. /* lets assume only one tuner, yes? */
  902. strcpy(v.name,"Television");
  903. v.type = VIDEO_TYPE_TV;
  904. if (ztv->have_tuner) {
  905. v.flags |= VIDEO_VC_TUNER;
  906. v.tuners = 1;
  907. }
  908. }
  909. else if (mux & IS_SVHS)
  910. sprintf(v.name,"S-Video-%d",v.channel);
  911. else
  912. sprintf(v.name,"CVBS-%d",v.channel);
  913. if (copy_to_user(arg,&v,sizeof(v)))
  914. return -EFAULT;
  915. break;
  916. }
  917. case VIDIOCSCHAN:
  918. { /* set video channel */
  919. struct video_channel v;
  920. if (copy_from_user(&v, arg,sizeof(v)))
  921. return -EFAULT;
  922. DEBUG(printk(CARD_DEBUG "VIDIOCSCHAN(%d,%d)\n",CARD,v.channel,v.norm));
  923. /* too many inputs? no decoder -> no channels */
  924. if (!ztv->have_decoder || v.channel >= ztv->card->video_inputs || v.channel < 0)
  925. return -EINVAL;
  926. if (v.norm != VIDEO_MODE_PAL &&
  927. v.norm != VIDEO_MODE_NTSC &&
  928. v.norm != VIDEO_MODE_SECAM &&
  929. v.norm != VIDEO_MODE_AUTO)
  930. return -EOPNOTSUPP;
  931. /* make it happen, nr1! */
  932. return zoran_muxsel(ztv,v.channel,v.norm);
  933. }
  934. case VIDIOCGTUNER:
  935. {
  936. struct video_tuner v;
  937. if (copy_from_user(&v, arg,sizeof(v)))
  938. return -EFAULT;
  939. DEBUG(printk(CARD_DEBUG "VIDIOCGTUNER(%d)\n",CARD,v.tuner));
  940. /* Only no or one tuner for now */
  941. if (!ztv->have_tuner || v.tuner)
  942. return -EINVAL;
  943. strcpy(v.name,"Television");
  944. v.rangelow = 0;
  945. v.rangehigh = ~0;
  946. v.flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
  947. v.mode = ztv->norm;
  948. v.signal = 0xFFFF; /* unknown */
  949. if (copy_to_user(arg,&v,sizeof(v)))
  950. return -EFAULT;
  951. break;
  952. }
  953. case VIDIOCSTUNER:
  954. {
  955. struct video_tuner v;
  956. if (copy_from_user(&v, arg, sizeof(v)))
  957. return -EFAULT;
  958. DEBUG(printk(CARD_DEBUG "VIDIOCSTUNER(%d,%d)\n",CARD,v.tuner,v.mode));
  959. /* Only no or one tuner for now */
  960. if (!ztv->have_tuner || v.tuner)
  961. return -EINVAL;
  962. /* and it only has certain valid modes */
  963. if( v.mode != VIDEO_MODE_PAL &&
  964. v.mode != VIDEO_MODE_NTSC &&
  965. v.mode != VIDEO_MODE_SECAM)
  966. return -EOPNOTSUPP;
  967. /* engage! */
  968. return zoran_muxsel(ztv,v.tuner,v.mode);
  969. }
  970. case VIDIOCGPICT:
  971. {
  972. struct video_picture p = ztv->picture;
  973. DEBUG(printk(CARD_DEBUG "VIDIOCGPICT\n",CARD));
  974. p.depth = ztv->depth;
  975. switch (p.depth) {
  976. case 8: p.palette=VIDEO_PALETTE_YUV422;
  977. break;
  978. case 15: p.palette=VIDEO_PALETTE_RGB555;
  979. break;
  980. case 16: p.palette=VIDEO_PALETTE_RGB565;
  981. break;
  982. case 24: p.palette=VIDEO_PALETTE_RGB24;
  983. break;
  984. case 32: p.palette=VIDEO_PALETTE_RGB32;
  985. break;
  986. }
  987. if (copy_to_user(arg, &p, sizeof(p)))
  988. return -EFAULT;
  989. break;
  990. }
  991. case VIDIOCSPICT:
  992. {
  993. struct video_picture p;
  994. if (copy_from_user(&p, arg,sizeof(p)))
  995. return -EFAULT;
  996. DEBUG(printk(CARD_DEBUG "VIDIOCSPICT(%d,%d,%d,%d,%d,%d,%d)\n",CARD,p.brightness,p.hue,p.colour,p.contrast,p.whiteness,p.depth,p.palette));
  997. /* depth must match with framebuffer */
  998. if (p.depth != ztv->depth)
  999. return -EINVAL;
  1000. /* check if palette matches this bpp */
  1001. if (p.palette>NRPALETTES ||
  1002. palette2fmt[p.palette].bpp != ztv->overinfo.bpp)
  1003. return -EINVAL;
  1004. write_lock_irq(&ztv->lock);
  1005. ztv->overinfo.format = p.palette;
  1006. ztv->picture = p;
  1007. write_unlock_irq(&ztv->lock);
  1008. /* tell the decoder */
  1009. i2c_control_device(&ztv->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &p);
  1010. break;
  1011. }
  1012. case VIDIOCGWIN:
  1013. {
  1014. struct video_window vw;
  1015. DEBUG(printk(CARD_DEBUG "VIDIOCGWIN\n",CARD));
  1016. read_lock(&ztv->lock);
  1017. vw.x = ztv->overinfo.x;
  1018. vw.y = ztv->overinfo.y;
  1019. vw.width = ztv->overinfo.w;
  1020. vw.height = ztv->overinfo.h;
  1021. vw.chromakey= 0;
  1022. vw.flags = 0;
  1023. if (ztv->vidInterlace)
  1024. vw.flags|=VIDEO_WINDOW_INTERLACE;
  1025. read_unlock(&ztv->lock);
  1026. if (copy_to_user(arg,&vw,sizeof(vw)))
  1027. return -EFAULT;
  1028. break;
  1029. }
  1030. case VIDIOCSWIN:
  1031. {
  1032. struct video_window vw;
  1033. struct video_clip *vcp;
  1034. int on;
  1035. if (copy_from_user(&vw,arg,sizeof(vw)))
  1036. return -EFAULT;
  1037. DEBUG(printk(CARD_DEBUG "VIDIOCSWIN(%d,%d,%d,%d,%x,%d)\n",CARD,vw.x,vw.y,vw.width,vw.height,vw.flags,vw.clipcount));
  1038. if (vw.flags)
  1039. return -EINVAL;
  1040. if (vw.clipcount <0 || vw.clipcount>256)
  1041. return -EDOM; /* Too many! */
  1042. /*
  1043. * Do any clips.
  1044. */
  1045. vcp = vmalloc(sizeof(struct video_clip)*(vw.clipcount+4));
  1046. if (vcp==NULL)
  1047. return -ENOMEM;
  1048. if (vw.clipcount && copy_from_user(vcp,vw.clips,sizeof(struct video_clip)*vw.clipcount)) {
  1049. vfree(vcp);
  1050. return -EFAULT;
  1051. }
  1052. on = ztv->running;
  1053. if (on)
  1054. zoran_cap(ztv, 0);
  1055. /*
  1056. * strange, it seems xawtv sometimes calls us with 0
  1057. * width and/or height. Ignore these values
  1058. */
  1059. if (vw.x == 0)
  1060. vw.x = ztv->overinfo.x;
  1061. if (vw.y == 0)
  1062. vw.y = ztv->overinfo.y;
  1063. /* by now we are committed to the new data... */
  1064. write_lock_irq(&ztv->lock);
  1065. ztv->overinfo.x = vw.x;
  1066. ztv->overinfo.y = vw.y;
  1067. ztv->overinfo.w = vw.width;
  1068. ztv->overinfo.h = vw.height;
  1069. write_unlock_irq(&ztv->lock);
  1070. /*
  1071. * Impose display clips
  1072. */
  1073. if (vw.x+vw.width > ztv->swidth)
  1074. new_clip(&vw, vcp, ztv->swidth-vw.x, 0, vw.width-1, vw.height-1);
  1075. if (vw.y+vw.height > ztv->sheight)
  1076. new_clip(&vw, vcp, 0, ztv->sheight-vw.y, vw.width-1, vw.height-1);
  1077. /* built the requested clipping zones */
  1078. zoran_set_geo(ztv, &ztv->overinfo);
  1079. zoran_built_overlay(ztv, vw.clipcount, vcp);
  1080. vfree(vcp);
  1081. /* if we were on, restart the video engine */
  1082. if (on)
  1083. zoran_cap(ztv, 1);
  1084. break;
  1085. }
  1086. case VIDIOCCAPTURE:
  1087. {
  1088. int v;
  1089. if (get_user(v, (int *)arg))
  1090. return -EFAULT;
  1091. DEBUG(printk(CARD_DEBUG "VIDIOCCAPTURE(%d)\n",CARD,v));
  1092. if (v==0) {
  1093. clear_bit(STATE_OVERLAY, &ztv->state);
  1094. zoran_cap(ztv, 1);
  1095. }
  1096. else {
  1097. /* is VIDIOCSFBUF, VIDIOCSWIN done? */
  1098. if (ztv->overinfo.busadr==0 || ztv->overinfo.w==0 || ztv->overinfo.h==0)
  1099. return -EINVAL;
  1100. set_bit(STATE_OVERLAY, &ztv->state);
  1101. zoran_cap(ztv, 1);
  1102. }
  1103. break;
  1104. }
  1105. case VIDIOCGFBUF:
  1106. {
  1107. struct video_buffer v;
  1108. DEBUG(printk(CARD_DEBUG "VIDIOCGFBUF\n",CARD));
  1109. read_lock(&ztv->lock);
  1110. v.base = (void *)ztv->overinfo.busadr;
  1111. v.height = ztv->sheight;
  1112. v.width = ztv->swidth;
  1113. v.depth = ztv->depth;
  1114. v.bytesperline = ztv->overinfo.bpl;
  1115. read_unlock(&ztv->lock);
  1116. if(copy_to_user(arg, &v,sizeof(v)))
  1117. return -EFAULT;
  1118. break;
  1119. }
  1120. case VIDIOCSFBUF:
  1121. {
  1122. struct video_buffer v;
  1123. if(!capable(CAP_SYS_ADMIN))
  1124. return -EPERM;
  1125. if (copy_from_user(&v, arg,sizeof(v)))
  1126. return -EFAULT;
  1127. DEBUG(printk(CARD_DEBUG "VIDIOCSFBUF(%p,%d,%d,%d,%d)\n",CARD,v.base, v.width,v.height,v.depth,v.bytesperline));
  1128. if (v.depth!=15 && v.depth!=16 && v.depth!=24 && v.depth!=32)
  1129. return -EINVAL;
  1130. if (v.bytesperline<1)
  1131. return -EINVAL;
  1132. if (ztv->running)
  1133. return -EBUSY;
  1134. write_lock_irq(&ztv->lock);
  1135. ztv->overinfo.busadr = (ulong)v.base;
  1136. ztv->sheight = v.height;
  1137. ztv->swidth = v.width;
  1138. ztv->depth = v.depth; /* bits per pixel */
  1139. ztv->overinfo.bpp = ((v.depth+1)&0x38)/8;/* bytes per pixel */
  1140. ztv->overinfo.bpl = v.bytesperline; /* bytes per line */
  1141. write_unlock_irq(&ztv->lock);
  1142. break;
  1143. }
  1144. case VIDIOCKEY:
  1145. {
  1146. /* Will be handled higher up .. */
  1147. break;
  1148. }
  1149. case VIDIOCSYNC:
  1150. {
  1151. int i;
  1152. if (get_user(i, (int *) arg))
  1153. return -EFAULT;
  1154. DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d)\n",CARD,i));
  1155. if (i<0 || i>ZORAN_MAX_FBUFFERS)
  1156. return -EINVAL;
  1157. switch (ztv->grabinfo[i].status) {
  1158. case FBUFFER_FREE:
  1159. return -EINVAL;
  1160. case FBUFFER_BUSY:
  1161. /* wait till this buffer gets grabbed */
  1162. wait_event_interruptible(ztv->grabq,
  1163. (ztv->grabinfo[i].status != FBUFFER_BUSY));
  1164. /* see if a signal did it */
  1165. if (signal_pending(current))
  1166. return -EINTR;
  1167. /* don't fall through; a DONE buffer is not UNUSED */
  1168. break;
  1169. case FBUFFER_DONE:
  1170. ztv->grabinfo[i].status = FBUFFER_FREE;
  1171. /* tell ppl we have a spare buffer */
  1172. wake_up_interruptible(&ztv->grabq);
  1173. break;
  1174. }
  1175. DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d) returns\n",CARD,i));
  1176. break;
  1177. }
  1178. case VIDIOCMCAPTURE:
  1179. {
  1180. struct video_mmap vm;
  1181. struct vidinfo* frame;
  1182. if (copy_from_user(&vm,arg,sizeof(vm)))
  1183. return -EFAULT;
  1184. DEBUG(printk(CARD_DEBUG "VIDIOCMCAPTURE(%d,(%d,%d),%d)\n",CARD,vm.frame,vm.width,vm.height,vm.format));
  1185. if (vm.frame<0 || vm.frame>ZORAN_MAX_FBUFFERS ||
  1186. vm.width<32 || vm.width>768 ||
  1187. vm.height<32 || vm.height>576 ||
  1188. vm.format>NRPALETTES ||
  1189. palette2fmt[vm.format].mode == 0)
  1190. return -EINVAL;
  1191. /* we are allowed to take over UNUSED and DONE buffers */
  1192. frame = &ztv->grabinfo[vm.frame];
  1193. if (frame->status == FBUFFER_BUSY)
  1194. return -EBUSY;
  1195. /* setup the other parameters if they are given */
  1196. write_lock_irq(&ztv->lock);
  1197. frame->w = vm.width;
  1198. frame->h = vm.height;
  1199. frame->format = vm.format;
  1200. frame->bpp = palette2fmt[frame->format].bpp;
  1201. frame->bpl = frame->w*frame->bpp;
  1202. frame->status = FBUFFER_BUSY;
  1203. frame->next = 0;
  1204. { /* add to tail of queue */
  1205. struct vidinfo* oldframe = ztv->workqueue;
  1206. if (!oldframe) ztv->workqueue = frame;
  1207. else {
  1208. while (oldframe->next) oldframe = oldframe->next;
  1209. oldframe->next = frame;
  1210. }
  1211. }
  1212. write_unlock_irq(&ztv->lock);
  1213. zoran_cap(ztv, 1);
  1214. break;
  1215. }
  1216. case VIDIOCGMBUF:
  1217. {
  1218. struct video_mbuf mb;
  1219. int i;
  1220. DEBUG(printk(CARD_DEBUG "VIDIOCGMBUF\n",CARD));
  1221. mb.size = ZORAN_MAX_FBUFSIZE;
  1222. mb.frames = ZORAN_MAX_FBUFFERS;
  1223. for (i=0; i<ZORAN_MAX_FBUFFERS; i++)
  1224. mb.offsets[i] = i*ZORAN_MAX_FBUFFER;
  1225. if(copy_to_user(arg, &mb,sizeof(mb)))
  1226. return -EFAULT;
  1227. break;
  1228. }
  1229. case VIDIOCGUNIT:
  1230. {
  1231. struct video_unit vu;
  1232. DEBUG(printk(CARD_DEBUG "VIDIOCGUNIT\n",CARD));
  1233. vu.video = ztv->video_dev.minor;
  1234. vu.vbi = ztv->vbi_dev.minor;
  1235. vu.radio = VIDEO_NO_UNIT;
  1236. vu.audio = VIDEO_NO_UNIT;
  1237. vu.teletext = VIDEO_NO_UNIT;
  1238. if(copy_to_user(arg, &vu,sizeof(vu)))
  1239. return -EFAULT;
  1240. break;
  1241. }
  1242. case VIDIOCGFREQ:
  1243. {
  1244. unsigned long v = ztv->tuner_freq;
  1245. if (copy_to_user(arg,&v,sizeof(v)))
  1246. return -EFAULT;
  1247. DEBUG(printk(CARD_DEBUG "VIDIOCGFREQ\n",CARD));
  1248. break;
  1249. }
  1250. case VIDIOCSFREQ:
  1251. {
  1252. unsigned long v;
  1253. if (copy_from_user(&v, arg, sizeof(v)))
  1254. return -EFAULT;
  1255. DEBUG(printk(CARD_DEBUG "VIDIOCSFREQ\n",CARD));
  1256. if (ztv->have_tuner) {
  1257. int fixme = v;
  1258. if (i2c_control_device(&(ztv->i2c), I2C_DRIVERID_TUNER, TUNER_SET_TVFREQ, &fixme) < 0)
  1259. return -EAGAIN;
  1260. }
  1261. ztv->tuner_freq = v;
  1262. break;
  1263. }
  1264. /* Why isn't this in the API?
  1265. * And why doesn't it take a buffer number?
  1266. case BTTV_FIELDNR:
  1267. {
  1268. unsigned long v = ztv->lastfieldnr;
  1269. if (copy_to_user(arg,&v,sizeof(v)))
  1270. return -EFAULT;
  1271. DEBUG(printk(CARD_DEBUG "BTTV_FIELDNR\n",CARD));
  1272. break;
  1273. }
  1274. */
  1275. default:
  1276. return -ENOIOCTLCMD;
  1277. }
  1278. return 0;
  1279. }
  1280. static
  1281. int zoran_mmap(struct vm_area_struct *vma, struct video_device* dev, const char* adr, unsigned long size)
  1282. {
  1283. struct zoran* ztv = (struct zoran*)dev;
  1284. unsigned long start = (unsigned long)adr;
  1285. unsigned long pos;
  1286. DEBUG(printk(CARD_DEBUG "zoran_mmap(0x%p,%ld)\n",CARD,adr,size));
  1287. /* sanity checks */
  1288. if (size > ZORAN_MAX_FBUFSIZE || !ztv->fbuffer)
  1289. return -EINVAL;
  1290. /* start mapping the whole shabang to user memory */
  1291. pos = (unsigned long)ztv->fbuffer;
  1292. while (size>0) {
  1293. unsigned long pfn = virt_to_phys((void*)pos) >> PAGE_SHIFT;
  1294. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
  1295. return -EAGAIN;
  1296. start += PAGE_SIZE;
  1297. pos += PAGE_SIZE;
  1298. size -= PAGE_SIZE;
  1299. }
  1300. return 0;
  1301. }
  1302. static struct video_device zr36120_template=
  1303. {
  1304. .owner = THIS_MODULE,
  1305. .name = "UNSET",
  1306. .type = VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY,
  1307. .hardware = VID_HARDWARE_ZR36120,
  1308. .open = zoran_open,
  1309. .close = zoran_close,
  1310. .read = zoran_read,
  1311. .write = zoran_write,
  1312. .poll = zoran_poll,
  1313. .ioctl = zoran_ioctl,
  1314. .mmap = zoran_mmap,
  1315. .minor = -1,
  1316. };
  1317. static
  1318. int vbi_open(struct video_device *dev, int flags)
  1319. {
  1320. struct zoran *ztv = dev->priv;
  1321. struct vidinfo* item;
  1322. DEBUG(printk(CARD_DEBUG "vbi_open(dev,%d)\n",CARD,flags));
  1323. /*
  1324. * During VBI device open, we continiously grab VBI-like
  1325. * data in the vbi buffer when we have nothing to do.
  1326. * Only when there is an explicit request for VBI data
  1327. * (read call) we /force/ a read.
  1328. */
  1329. /* allocate buffers */
  1330. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1331. {
  1332. item->status = FBUFFER_FREE;
  1333. /* alloc */
  1334. if (!item->memadr) {
  1335. item->memadr = bmalloc(ZORAN_VBI_BUFSIZE);
  1336. if (!item->memadr) {
  1337. /* could not get a buffer, bail out */
  1338. while (item != ztv->readinfo) {
  1339. item--;
  1340. bfree(item->memadr, ZORAN_VBI_BUFSIZE);
  1341. item->memadr = 0;
  1342. item->busadr = 0;
  1343. }
  1344. return -ENOBUFS;
  1345. }
  1346. }
  1347. /* determine the DMAable address */
  1348. item->busadr = virt_to_bus(item->memadr);
  1349. }
  1350. /* do the common part of all open's */
  1351. zoran_common_open(ztv, flags);
  1352. set_bit(STATE_VBI, &ztv->state);
  1353. /* start read-ahead */
  1354. zoran_cap(ztv, 1);
  1355. return 0;
  1356. }
  1357. static
  1358. void vbi_close(struct video_device *dev)
  1359. {
  1360. struct zoran *ztv = dev->priv;
  1361. struct vidinfo* item;
  1362. DEBUG(printk(CARD_DEBUG "vbi_close(dev)\n",CARD));
  1363. /* driver specific closure */
  1364. clear_bit(STATE_VBI, &ztv->state);
  1365. zoran_common_close(ztv);
  1366. /*
  1367. * This is sucky but right now I can't find a good way to
  1368. * be sure its safe to free the buffer. We wait 5-6 fields
  1369. * which is more than sufficient to be sure.
  1370. */
  1371. msleep(100); /* Wait 1/10th of a second */
  1372. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1373. {
  1374. if (item->memadr)
  1375. bfree(item->memadr, ZORAN_VBI_BUFSIZE);
  1376. item->memadr = 0;
  1377. }
  1378. }
  1379. /*
  1380. * This read function could be used reentrant in a SMP situation.
  1381. *
  1382. * This is made possible by the spinlock which is kept till we
  1383. * found and marked a buffer for our own use. The lock must
  1384. * be released as soon as possible to prevent lock contention.
  1385. */
  1386. static
  1387. long vbi_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
  1388. {
  1389. struct zoran *ztv = dev->priv;
  1390. unsigned long max;
  1391. struct vidinfo* unused = 0;
  1392. struct vidinfo* done = 0;
  1393. DEBUG(printk(CARD_DEBUG "vbi_read(0x%p,%ld,%d)\n",CARD,buf,count,nonblock));
  1394. /* find ourself a free or completed buffer */
  1395. for (;;) {
  1396. struct vidinfo* item;
  1397. write_lock_irq(&ztv->lock);
  1398. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++) {
  1399. if (!unused && item->status == FBUFFER_FREE)
  1400. unused = item;
  1401. if (!done && item->status == FBUFFER_DONE)
  1402. done = item;
  1403. }
  1404. if (done || unused)
  1405. break;
  1406. /* no more free buffers, wait for them. */
  1407. write_unlock_irq(&ztv->lock);
  1408. if (nonblock)
  1409. return -EWOULDBLOCK;
  1410. interruptible_sleep_on(&ztv->vbiq);
  1411. if (signal_pending(current))
  1412. return -EINTR;
  1413. }
  1414. /* Do we have 'ready' data? */
  1415. if (!done) {
  1416. /* no? than this will take a while... */
  1417. if (nonblock) {
  1418. write_unlock_irq(&ztv->lock);
  1419. return -EWOULDBLOCK;
  1420. }
  1421. /* mark the unused buffer as wanted */
  1422. unused->status = FBUFFER_BUSY;
  1423. unused->next = 0;
  1424. { /* add to tail of queue */
  1425. struct vidinfo* oldframe = ztv->workqueue;
  1426. if (!oldframe) ztv->workqueue = unused;
  1427. else {
  1428. while (oldframe->next) oldframe = oldframe->next;
  1429. oldframe->next = unused;
  1430. }
  1431. }
  1432. write_unlock_irq(&ztv->lock);
  1433. /* tell the state machine we want it filled /NOW/ */
  1434. zoran_cap(ztv, 1);
  1435. /* wait till this buffer gets grabbed */
  1436. wait_event_interruptible(ztv->vbiq,
  1437. (unused->status != FBUFFER_BUSY));
  1438. /* see if a signal did it */
  1439. if (signal_pending(current))
  1440. return -EINTR;
  1441. done = unused;
  1442. }
  1443. else
  1444. write_unlock_irq(&ztv->lock);
  1445. /* Yes! we got data! */
  1446. max = done->bpl * -done->h;
  1447. if (count > max)
  1448. count = max;
  1449. /* check if the user gave us enough room to write the data */
  1450. if (!access_ok(VERIFY_WRITE, buf, count)) {
  1451. count = -EFAULT;
  1452. goto out;
  1453. }
  1454. /*
  1455. * Now transform/strip the data from YUV to Y-only
  1456. * NB. Assume the Y is in the LSB of the YUV data.
  1457. */
  1458. {
  1459. unsigned char* optr = buf;
  1460. unsigned char* eptr = buf+count;
  1461. /* are we beeing accessed from an old driver? */
  1462. if (count == 2*19*2048) {
  1463. /*
  1464. * Extreme HACK, old VBI programs expect 2048 points
  1465. * of data, and we only got 864 orso. Double each
  1466. * datapoint and clear the rest of the line.
  1467. * This way we have appear to have a
  1468. * sample_frequency of 29.5 Mc.
  1469. */
  1470. int x,y;
  1471. unsigned char* iptr = done->memadr+1;
  1472. for (y=done->h; optr<eptr && y<0; y++)
  1473. {
  1474. /* copy to doubled data to userland */
  1475. for (x=0; optr+1<eptr && x<-done->w; x++)
  1476. {
  1477. unsigned char a = iptr[x*2];
  1478. __put_user(a, optr++);
  1479. __put_user(a, optr++);
  1480. }
  1481. /* and clear the rest of the line */
  1482. for (x*=2; optr<eptr && x<done->bpl; x++)
  1483. __put_user(0, optr++);
  1484. /* next line */
  1485. iptr += done->bpl;
  1486. }
  1487. }
  1488. else {
  1489. /*
  1490. * Other (probably newer) programs asked
  1491. * us what geometry we are using, and are
  1492. * reading the correct size.
  1493. */
  1494. int x,y;
  1495. unsigned char* iptr = done->memadr+1;
  1496. for (y=done->h; optr<eptr && y<0; y++)
  1497. {
  1498. /* copy to doubled data to userland */
  1499. for (x=0; optr<eptr && x<-done->w; x++)
  1500. __put_user(iptr[x*2], optr++);
  1501. /* and clear the rest of the line */
  1502. for (;optr<eptr && x<done->bpl; x++)
  1503. __put_user(0, optr++);
  1504. /* next line */
  1505. iptr += done->bpl;
  1506. }
  1507. }
  1508. /* API compliance:
  1509. * place the framenumber (half fieldnr) in the last long
  1510. */
  1511. __put_user(done->fieldnr/2, ((ulong*)eptr)[-1]);
  1512. }
  1513. /* keep the engine running */
  1514. done->status = FBUFFER_FREE;
  1515. zoran_cap(ztv, 1);
  1516. /* tell listeners this buffer just became free */
  1517. wake_up_interruptible(&ztv->vbiq);
  1518. /* goodbye */
  1519. out:
  1520. DEBUG(printk(CARD_DEBUG "vbi_read() returns %lu\n",CARD,count));
  1521. return count;
  1522. }
  1523. static
  1524. unsigned int vbi_poll(struct video_device *dev, struct file *file, poll_table *wait)
  1525. {
  1526. struct zoran *ztv = dev->priv;
  1527. struct vidinfo* item;
  1528. unsigned int mask = 0;
  1529. poll_wait(file, &ztv->vbiq, wait);
  1530. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1531. if (item->status == FBUFFER_DONE)
  1532. {
  1533. mask |= (POLLIN | POLLRDNORM);
  1534. break;
  1535. }
  1536. DEBUG(printk(CARD_DEBUG "vbi_poll()=%x\n",CARD,mask));
  1537. return mask;
  1538. }
  1539. static
  1540. int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
  1541. {
  1542. struct zoran* ztv = dev->priv;
  1543. switch (cmd) {
  1544. case VIDIOCGVBIFMT:
  1545. {
  1546. struct vbi_format f;
  1547. DEBUG(printk(CARD_DEBUG "VIDIOCGVBIINFO\n",CARD));
  1548. f.sampling_rate = 14750000UL;
  1549. f.samples_per_line = -ztv->readinfo[0].w;
  1550. f.sample_format = VIDEO_PALETTE_RAW;
  1551. f.start[0] = f.start[1] = ztv->readinfo[0].y;
  1552. f.start[1] += 312;
  1553. f.count[0] = f.count[1] = -ztv->readinfo[0].h;
  1554. f.flags = VBI_INTERLACED;
  1555. if (copy_to_user(arg,&f,sizeof(f)))
  1556. return -EFAULT;
  1557. break;
  1558. }
  1559. case VIDIOCSVBIFMT:
  1560. {
  1561. struct vbi_format f;
  1562. int i;
  1563. if (copy_from_user(&f, arg,sizeof(f)))
  1564. return -EFAULT;
  1565. DEBUG(printk(CARD_DEBUG "VIDIOCSVBIINFO(%d,%d,%d,%d,%d,%d,%d,%x)\n",CARD,f.sampling_rate,f.samples_per_line,f.sample_format,f.start[0],f.start[1],f.count[0],f.count[1],f.flags));
  1566. /* lots of parameters are fixed... (PAL) */
  1567. if (f.sampling_rate != 14750000UL ||
  1568. f.samples_per_line > 864 ||
  1569. f.sample_format != VIDEO_PALETTE_RAW ||
  1570. f.start[0] < 0 ||
  1571. f.start[0] != f.start[1]-312 ||
  1572. f.count[0] != f.count[1] ||
  1573. f.start[0]+f.count[0] >= 288 ||
  1574. f.flags != VBI_INTERLACED)
  1575. return -EINVAL;
  1576. write_lock_irq(&ztv->lock);
  1577. ztv->readinfo[0].y = f.start[0];
  1578. ztv->readinfo[0].w = -f.samples_per_line;
  1579. ztv->readinfo[0].h = -f.count[0];
  1580. ztv->readinfo[0].bpl = f.samples_per_line*ztv->readinfo[0].bpp;
  1581. for (i=1; i<ZORAN_VBI_BUFFERS; i++)
  1582. ztv->readinfo[i] = ztv->readinfo[i];
  1583. write_unlock_irq(&ztv->lock);
  1584. break;
  1585. }
  1586. default:
  1587. return -ENOIOCTLCMD;
  1588. }
  1589. return 0;
  1590. }
  1591. static struct video_device vbi_template=
  1592. {
  1593. .owner = THIS_MODULE,
  1594. .name = "UNSET",
  1595. .type = VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
  1596. .hardware = VID_HARDWARE_ZR36120,
  1597. .open = vbi_open,
  1598. .close = vbi_close,
  1599. .read = vbi_read,
  1600. .write = zoran_write,
  1601. .poll = vbi_poll,
  1602. .ioctl = vbi_ioctl,
  1603. .minor = -1,
  1604. };
  1605. /*
  1606. * Scan for a Zoran chip, request the irq and map the io memory
  1607. */
  1608. static
  1609. int __init find_zoran(void)
  1610. {
  1611. int result;
  1612. struct zoran *ztv;
  1613. struct pci_dev *dev = NULL;
  1614. unsigned char revision;
  1615. int zoran_num=0;
  1616. while ((dev = pci_find_device(PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120, dev)))
  1617. {
  1618. /* Ok, a ZR36120/ZR36125 found! */
  1619. ztv = &zorans[zoran_num];
  1620. ztv->dev = dev;
  1621. if (pci_enable_device(dev))
  1622. return -EIO;
  1623. pci_read_config_byte(dev, PCI_CLASS_REVISION, &revision);
  1624. printk(KERN_INFO "zoran: Zoran %x (rev %d) ",
  1625. dev->device, revision);
  1626. printk("bus: %d, devfn: %d, irq: %d, ",
  1627. dev->bus->number, dev->devfn, dev->irq);
  1628. printk("memory: 0x%08lx.\n", ztv->zoran_adr);
  1629. ztv->zoran_mem = ioremap(ztv->zoran_adr, 0x1000);
  1630. DEBUG(printk(KERN_DEBUG "zoran: mapped-memory at 0x%p\n",ztv->zoran_mem));
  1631. result = request_irq(dev->irq, zoran_irq,
  1632. SA_SHIRQ|SA_INTERRUPT,"zoran", ztv);
  1633. if (result==-EINVAL)
  1634. {
  1635. iounmap(ztv->zoran_mem);
  1636. printk(KERN_ERR "zoran: Bad irq number or handler\n");
  1637. return -EINVAL;
  1638. }
  1639. if (result==-EBUSY)
  1640. printk(KERN_ERR "zoran: IRQ %d busy, change your PnP config in BIOS\n",dev->irq);
  1641. if (result < 0) {
  1642. iounmap(ztv->zoran_mem);
  1643. return result;
  1644. }
  1645. /* Enable bus-mastering */
  1646. pci_set_master(dev);
  1647. zoran_num++;
  1648. }
  1649. if(zoran_num)
  1650. printk(KERN_INFO "zoran: %d Zoran card(s) found.\n",zoran_num);
  1651. return zoran_num;
  1652. }
  1653. static
  1654. int __init init_zoran(int card)
  1655. {
  1656. struct zoran *ztv = &zorans[card];
  1657. int i;
  1658. /* if the given cardtype valid? */
  1659. if (cardtype[card]>=NRTVCARDS) {
  1660. printk(KERN_INFO "invalid cardtype(%d) detected\n",cardtype[card]);
  1661. return -1;
  1662. }
  1663. /* reset the zoran */
  1664. zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1665. udelay(10);
  1666. zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1667. udelay(10);
  1668. /* zoran chip specific details */
  1669. ztv->card = tvcards+cardtype[card]; /* point to the selected card */
  1670. ztv->norm = 0; /* PAL */
  1671. ztv->tuner_freq = 0;
  1672. /* videocard details */
  1673. ztv->swidth = 800;
  1674. ztv->sheight = 600;
  1675. ztv->depth = 16;
  1676. /* State details */
  1677. ztv->fbuffer = 0;
  1678. ztv->overinfo.kindof = FBUFFER_OVERLAY;
  1679. ztv->overinfo.status = FBUFFER_FREE;
  1680. ztv->overinfo.x = 0;
  1681. ztv->overinfo.y = 0;
  1682. ztv->overinfo.w = 768; /* 640 */
  1683. ztv->overinfo.h = 576; /* 480 */
  1684. ztv->overinfo.format = VIDEO_PALETTE_RGB565;
  1685. ztv->overinfo.bpp = palette2fmt[ztv->overinfo.format].bpp;
  1686. ztv->overinfo.bpl = ztv->overinfo.bpp*ztv->swidth;
  1687. ztv->overinfo.busadr = 0;
  1688. ztv->overinfo.memadr = 0;
  1689. ztv->overinfo.overlay = 0;
  1690. for (i=0; i<ZORAN_MAX_FBUFFERS; i++) {
  1691. ztv->grabinfo[i] = ztv->overinfo;
  1692. ztv->grabinfo[i].kindof = FBUFFER_GRAB;
  1693. }
  1694. init_waitqueue_head(&ztv->grabq);
  1695. /* VBI details */
  1696. ztv->readinfo[0] = ztv->overinfo;
  1697. ztv->readinfo[0].kindof = FBUFFER_VBI;
  1698. ztv->readinfo[0].w = -864;
  1699. ztv->readinfo[0].h = -38;
  1700. ztv->readinfo[0].format = VIDEO_PALETTE_YUV422;
  1701. ztv->readinfo[0].bpp = palette2fmt[ztv->readinfo[0].format].bpp;
  1702. ztv->readinfo[0].bpl = 1024*ztv->readinfo[0].bpp;
  1703. for (i=1; i<ZORAN_VBI_BUFFERS; i++)
  1704. ztv->readinfo[i] = ztv->readinfo[0];
  1705. init_waitqueue_head(&ztv->vbiq);
  1706. /* maintenance data */
  1707. ztv->have_decoder = 0;
  1708. ztv->have_tuner = 0;
  1709. ztv->tuner_type = 0;
  1710. ztv->running = 0;
  1711. ztv->users = 0;
  1712. rwlock_init(&ztv->lock);
  1713. ztv->workqueue = 0;
  1714. ztv->fieldnr = 0;
  1715. ztv->lastfieldnr = 0;
  1716. if (triton1)
  1717. zrand(~ZORAN_VDC_TRICOM, ZORAN_VDC);
  1718. /* external FL determines TOP frame */
  1719. zror(ZORAN_VFEC_EXTFL, ZORAN_VFEC);
  1720. /* set HSpol */
  1721. if (ztv->card->hsync_pos)
  1722. zrwrite(ZORAN_VFEH_HSPOL, ZORAN_VFEH);
  1723. /* set VSpol */
  1724. if (ztv->card->vsync_pos)
  1725. zrwrite(ZORAN_VFEV_VSPOL, ZORAN_VFEV);
  1726. /* Set the proper General Purpuse register bits */
  1727. /* implicit: no softreset, 0 waitstates */
  1728. zrwrite(ZORAN_PCI_SOFTRESET|(ztv->card->gpdir<<0),ZORAN_PCI);
  1729. /* implicit: 3 duration and recovery PCI clocks on guest 0-3 */
  1730. zrwrite(ztv->card->gpval<<24,ZORAN_GUEST);
  1731. /* clear interrupt status */
  1732. zrwrite(~0, ZORAN_ISR);
  1733. /*
  1734. * i2c template
  1735. */
  1736. ztv->i2c = zoran_i2c_bus_template;
  1737. sprintf(ztv->i2c.name,"zoran-%d",card);
  1738. ztv->i2c.data = ztv;
  1739. /*
  1740. * Now add the template and register the device unit
  1741. */
  1742. ztv->video_dev = zr36120_template;
  1743. strcpy(ztv->video_dev.name, ztv->i2c.name);
  1744. ztv->video_dev.priv = ztv;
  1745. if (video_register_device(&ztv->video_dev, VFL_TYPE_GRABBER, video_nr) < 0)
  1746. return -1;
  1747. ztv->vbi_dev = vbi_template;
  1748. strcpy(ztv->vbi_dev.name, ztv->i2c.name);
  1749. ztv->vbi_dev.priv = ztv;
  1750. if (video_register_device(&ztv->vbi_dev, VFL_TYPE_VBI, vbi_nr) < 0) {
  1751. video_unregister_device(&ztv->video_dev);
  1752. return -1;
  1753. }
  1754. i2c_register_bus(&ztv->i2c);
  1755. /* set interrupt mask - the PIN enable will be set later */
  1756. zrwrite(ZORAN_ICR_GIRQ0|ZORAN_ICR_GIRQ1|ZORAN_ICR_CODE, ZORAN_ICR);
  1757. printk(KERN_INFO "%s: installed %s\n",ztv->i2c.name,ztv->card->name);
  1758. return 0;
  1759. }
  1760. static
  1761. void release_zoran(int max)
  1762. {
  1763. struct zoran *ztv;
  1764. int i;
  1765. for (i=0;i<max; i++)
  1766. {
  1767. ztv = &zorans[i];
  1768. /* turn off all capturing, DMA and IRQs */
  1769. /* reset the zoran */
  1770. zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1771. udelay(10);
  1772. zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1773. udelay(10);
  1774. /* first disable interrupts before unmapping the memory! */
  1775. zrwrite(0, ZORAN_ICR);
  1776. zrwrite(0xffffffffUL,ZORAN_ISR);
  1777. /* free it */
  1778. free_irq(ztv->dev->irq,ztv);
  1779. /* unregister i2c_bus */
  1780. i2c_unregister_bus((&ztv->i2c));
  1781. /* unmap and free memory */
  1782. if (ztv->zoran_mem)
  1783. iounmap(ztv->zoran_mem);
  1784. video_unregister_device(&ztv->video_dev);
  1785. video_unregister_device(&ztv->vbi_dev);
  1786. }
  1787. }
  1788. void __exit zr36120_exit(void)
  1789. {
  1790. release_zoran(zoran_cards);
  1791. }
  1792. int __init zr36120_init(void)
  1793. {
  1794. int card;
  1795. handle_chipset();
  1796. zoran_cards = find_zoran();
  1797. if (zoran_cards<0)
  1798. /* no cards found, no need for a driver */
  1799. return -EIO;
  1800. /* initialize Zorans */
  1801. for (card=0; card<zoran_cards; card++) {
  1802. if (init_zoran(card)<0) {
  1803. /* only release the zorans we have registered */
  1804. release_zoran(card);
  1805. return -EIO;
  1806. }
  1807. }
  1808. return 0;
  1809. }
  1810. module_init(zr36120_init);
  1811. module_exit(zr36120_exit);