zr36120.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  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_param(triton1, uint, 0);
  60. module_param_array(cardtype, uint, NULL, 0);
  61. module_param(video_nr, int, 0);
  62. module_param(vbi_nr, int, 0);
  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)
  294. {
  295. u32 stat,estat;
  296. int count = 0;
  297. struct zoran *ztv = dev_id;
  298. UNUSED(irq);
  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. bfree(ztv->fbuffer, ZORAN_MAX_FBUFSIZE);
  716. ztv->fbuffer = 0;
  717. kfree(ztv->overinfo.overlay);
  718. ztv->overinfo.overlay = 0;
  719. }
  720. /*
  721. * This read function could be used reentrant in a SMP situation.
  722. *
  723. * This is made possible by the spinlock which is kept till we
  724. * found and marked a buffer for our own use. The lock must
  725. * be released as soon as possible to prevent lock contention.
  726. */
  727. static
  728. long zoran_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
  729. {
  730. struct zoran *ztv = (struct zoran*)dev;
  731. unsigned long max;
  732. struct vidinfo* unused = 0;
  733. struct vidinfo* done = 0;
  734. DEBUG(printk(CARD_DEBUG "zoran_read(%p,%ld,%d)\n",CARD,buf,count,nonblock));
  735. /* find ourself a free or completed buffer */
  736. for (;;) {
  737. struct vidinfo* item;
  738. write_lock_irq(&ztv->lock);
  739. for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
  740. {
  741. if (!unused && item->status == FBUFFER_FREE)
  742. unused = item;
  743. if (!done && item->status == FBUFFER_DONE)
  744. done = item;
  745. }
  746. if (done || unused)
  747. break;
  748. /* no more free buffers, wait for them. */
  749. write_unlock_irq(&ztv->lock);
  750. if (nonblock)
  751. return -EWOULDBLOCK;
  752. interruptible_sleep_on(&ztv->grabq);
  753. if (signal_pending(current))
  754. return -EINTR;
  755. }
  756. /* Do we have 'ready' data? */
  757. if (!done) {
  758. /* no? than this will take a while... */
  759. if (nonblock) {
  760. write_unlock_irq(&ztv->lock);
  761. return -EWOULDBLOCK;
  762. }
  763. /* mark the unused buffer as wanted */
  764. unused->status = FBUFFER_BUSY;
  765. unused->w = 320;
  766. unused->h = 240;
  767. unused->format = VIDEO_PALETTE_RGB24;
  768. unused->bpp = palette2fmt[unused->format].bpp;
  769. unused->bpl = unused->w * unused->bpp;
  770. unused->next = 0;
  771. { /* add to tail of queue */
  772. struct vidinfo* oldframe = ztv->workqueue;
  773. if (!oldframe) ztv->workqueue = unused;
  774. else {
  775. while (oldframe->next) oldframe = oldframe->next;
  776. oldframe->next = unused;
  777. }
  778. }
  779. write_unlock_irq(&ztv->lock);
  780. /* tell the state machine we want it filled /NOW/ */
  781. zoran_cap(ztv, 1);
  782. /* wait till this buffer gets grabbed */
  783. wait_event_interruptible(ztv->grabq,
  784. (unused->status != FBUFFER_BUSY));
  785. /* see if a signal did it */
  786. if (signal_pending(current))
  787. return -EINTR;
  788. done = unused;
  789. }
  790. else
  791. write_unlock_irq(&ztv->lock);
  792. /* Yes! we got data! */
  793. max = done->bpl * done->h;
  794. if (count > max)
  795. count = max;
  796. if (copy_to_user((void*)buf, done->memadr, count))
  797. count = -EFAULT;
  798. /* keep the engine running */
  799. done->status = FBUFFER_FREE;
  800. // zoran_cap(ztv,1);
  801. /* tell listeners this buffer became free */
  802. wake_up_interruptible(&ztv->grabq);
  803. /* goodbye */
  804. DEBUG(printk(CARD_DEBUG "zoran_read() returns %lu\n",CARD,count));
  805. return count;
  806. }
  807. static
  808. long zoran_write(struct video_device* dev, const char* buf, unsigned long count, int nonblock)
  809. {
  810. struct zoran *ztv = (struct zoran *)dev;
  811. UNUSED(ztv); UNUSED(dev); UNUSED(buf); UNUSED(count); UNUSED(nonblock);
  812. DEBUG(printk(CARD_DEBUG "zoran_write\n",CARD));
  813. return -EINVAL;
  814. }
  815. static
  816. unsigned int zoran_poll(struct video_device *dev, struct file *file, poll_table *wait)
  817. {
  818. struct zoran *ztv = (struct zoran *)dev;
  819. struct vidinfo* item;
  820. unsigned int mask = 0;
  821. poll_wait(file, &ztv->grabq, wait);
  822. for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
  823. if (item->status == FBUFFER_DONE)
  824. {
  825. mask |= (POLLIN | POLLRDNORM);
  826. break;
  827. }
  828. DEBUG(printk(CARD_DEBUG "zoran_poll()=%x\n",CARD,mask));
  829. return mask;
  830. }
  831. /* append a new clipregion to the vector of video_clips */
  832. static
  833. void new_clip(struct video_window* vw, struct video_clip* vcp, int x, int y, int w, int h)
  834. {
  835. vcp[vw->clipcount].x = x;
  836. vcp[vw->clipcount].y = y;
  837. vcp[vw->clipcount].width = w;
  838. vcp[vw->clipcount].height = h;
  839. vw->clipcount++;
  840. }
  841. static
  842. int zoran_ioctl(struct video_device* dev, unsigned int cmd, void *arg)
  843. {
  844. struct zoran* ztv = (struct zoran*)dev;
  845. switch (cmd) {
  846. case VIDIOCGCAP:
  847. {
  848. struct video_capability c;
  849. DEBUG(printk(CARD_DEBUG "VIDIOCGCAP\n",CARD));
  850. strcpy(c.name,ztv->video_dev.name);
  851. c.type = VID_TYPE_CAPTURE|
  852. VID_TYPE_OVERLAY|
  853. VID_TYPE_CLIPPING|
  854. VID_TYPE_FRAMERAM|
  855. VID_TYPE_SCALES;
  856. if (ztv->have_tuner)
  857. c.type |= VID_TYPE_TUNER;
  858. if (pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL))
  859. c.type &= ~VID_TYPE_OVERLAY;
  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 (pcipci_problems & (PCIPCI_FAIL|PCIAGP_FAIL))
  1126. return -ENXIO;
  1127. if (copy_from_user(&v, arg,sizeof(v)))
  1128. return -EFAULT;
  1129. DEBUG(printk(CARD_DEBUG "VIDIOCSFBUF(%p,%d,%d,%d,%d)\n",CARD,v.base, v.width,v.height,v.depth,v.bytesperline));
  1130. if (v.depth!=15 && v.depth!=16 && v.depth!=24 && v.depth!=32)
  1131. return -EINVAL;
  1132. if (v.bytesperline<1)
  1133. return -EINVAL;
  1134. if (ztv->running)
  1135. return -EBUSY;
  1136. write_lock_irq(&ztv->lock);
  1137. ztv->overinfo.busadr = (ulong)v.base;
  1138. ztv->sheight = v.height;
  1139. ztv->swidth = v.width;
  1140. ztv->depth = v.depth; /* bits per pixel */
  1141. ztv->overinfo.bpp = ((v.depth+1)&0x38)/8;/* bytes per pixel */
  1142. ztv->overinfo.bpl = v.bytesperline; /* bytes per line */
  1143. write_unlock_irq(&ztv->lock);
  1144. break;
  1145. }
  1146. case VIDIOCKEY:
  1147. {
  1148. /* Will be handled higher up .. */
  1149. break;
  1150. }
  1151. case VIDIOCSYNC:
  1152. {
  1153. int i;
  1154. if (get_user(i, (int *) arg))
  1155. return -EFAULT;
  1156. DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d)\n",CARD,i));
  1157. if (i<0 || i>ZORAN_MAX_FBUFFERS)
  1158. return -EINVAL;
  1159. switch (ztv->grabinfo[i].status) {
  1160. case FBUFFER_FREE:
  1161. return -EINVAL;
  1162. case FBUFFER_BUSY:
  1163. /* wait till this buffer gets grabbed */
  1164. wait_event_interruptible(ztv->grabq,
  1165. (ztv->grabinfo[i].status != FBUFFER_BUSY));
  1166. /* see if a signal did it */
  1167. if (signal_pending(current))
  1168. return -EINTR;
  1169. /* don't fall through; a DONE buffer is not UNUSED */
  1170. break;
  1171. case FBUFFER_DONE:
  1172. ztv->grabinfo[i].status = FBUFFER_FREE;
  1173. /* tell ppl we have a spare buffer */
  1174. wake_up_interruptible(&ztv->grabq);
  1175. break;
  1176. }
  1177. DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d) returns\n",CARD,i));
  1178. break;
  1179. }
  1180. case VIDIOCMCAPTURE:
  1181. {
  1182. struct video_mmap vm;
  1183. struct vidinfo* frame;
  1184. if (copy_from_user(&vm,arg,sizeof(vm)))
  1185. return -EFAULT;
  1186. DEBUG(printk(CARD_DEBUG "VIDIOCMCAPTURE(%d,(%d,%d),%d)\n",CARD,vm.frame,vm.width,vm.height,vm.format));
  1187. if (vm.frame<0 || vm.frame>ZORAN_MAX_FBUFFERS ||
  1188. vm.width<32 || vm.width>768 ||
  1189. vm.height<32 || vm.height>576 ||
  1190. vm.format>NRPALETTES ||
  1191. palette2fmt[vm.format].mode == 0)
  1192. return -EINVAL;
  1193. /* we are allowed to take over UNUSED and DONE buffers */
  1194. frame = &ztv->grabinfo[vm.frame];
  1195. if (frame->status == FBUFFER_BUSY)
  1196. return -EBUSY;
  1197. /* setup the other parameters if they are given */
  1198. write_lock_irq(&ztv->lock);
  1199. frame->w = vm.width;
  1200. frame->h = vm.height;
  1201. frame->format = vm.format;
  1202. frame->bpp = palette2fmt[frame->format].bpp;
  1203. frame->bpl = frame->w*frame->bpp;
  1204. frame->status = FBUFFER_BUSY;
  1205. frame->next = 0;
  1206. { /* add to tail of queue */
  1207. struct vidinfo* oldframe = ztv->workqueue;
  1208. if (!oldframe) ztv->workqueue = frame;
  1209. else {
  1210. while (oldframe->next) oldframe = oldframe->next;
  1211. oldframe->next = frame;
  1212. }
  1213. }
  1214. write_unlock_irq(&ztv->lock);
  1215. zoran_cap(ztv, 1);
  1216. break;
  1217. }
  1218. case VIDIOCGMBUF:
  1219. {
  1220. struct video_mbuf mb;
  1221. int i;
  1222. DEBUG(printk(CARD_DEBUG "VIDIOCGMBUF\n",CARD));
  1223. mb.size = ZORAN_MAX_FBUFSIZE;
  1224. mb.frames = ZORAN_MAX_FBUFFERS;
  1225. for (i=0; i<ZORAN_MAX_FBUFFERS; i++)
  1226. mb.offsets[i] = i*ZORAN_MAX_FBUFFER;
  1227. if(copy_to_user(arg, &mb,sizeof(mb)))
  1228. return -EFAULT;
  1229. break;
  1230. }
  1231. case VIDIOCGUNIT:
  1232. {
  1233. struct video_unit vu;
  1234. DEBUG(printk(CARD_DEBUG "VIDIOCGUNIT\n",CARD));
  1235. vu.video = ztv->video_dev.minor;
  1236. vu.vbi = ztv->vbi_dev.minor;
  1237. vu.radio = VIDEO_NO_UNIT;
  1238. vu.audio = VIDEO_NO_UNIT;
  1239. vu.teletext = VIDEO_NO_UNIT;
  1240. if(copy_to_user(arg, &vu,sizeof(vu)))
  1241. return -EFAULT;
  1242. break;
  1243. }
  1244. case VIDIOCGFREQ:
  1245. {
  1246. unsigned long v = ztv->tuner_freq;
  1247. if (copy_to_user(arg,&v,sizeof(v)))
  1248. return -EFAULT;
  1249. DEBUG(printk(CARD_DEBUG "VIDIOCGFREQ\n",CARD));
  1250. break;
  1251. }
  1252. case VIDIOCSFREQ:
  1253. {
  1254. unsigned long v;
  1255. if (copy_from_user(&v, arg, sizeof(v)))
  1256. return -EFAULT;
  1257. DEBUG(printk(CARD_DEBUG "VIDIOCSFREQ\n",CARD));
  1258. if (ztv->have_tuner) {
  1259. int fixme = v;
  1260. if (i2c_control_device(&(ztv->i2c), I2C_DRIVERID_TUNER, TUNER_SET_TVFREQ, &fixme) < 0)
  1261. return -EAGAIN;
  1262. }
  1263. ztv->tuner_freq = v;
  1264. break;
  1265. }
  1266. /* Why isn't this in the API?
  1267. * And why doesn't it take a buffer number?
  1268. case BTTV_FIELDNR:
  1269. {
  1270. unsigned long v = ztv->lastfieldnr;
  1271. if (copy_to_user(arg,&v,sizeof(v)))
  1272. return -EFAULT;
  1273. DEBUG(printk(CARD_DEBUG "BTTV_FIELDNR\n",CARD));
  1274. break;
  1275. }
  1276. */
  1277. default:
  1278. return -ENOIOCTLCMD;
  1279. }
  1280. return 0;
  1281. }
  1282. static
  1283. int zoran_mmap(struct vm_area_struct *vma, struct video_device* dev, const char* adr, unsigned long size)
  1284. {
  1285. struct zoran* ztv = (struct zoran*)dev;
  1286. unsigned long start = (unsigned long)adr;
  1287. unsigned long pos;
  1288. DEBUG(printk(CARD_DEBUG "zoran_mmap(0x%p,%ld)\n",CARD,adr,size));
  1289. /* sanity checks */
  1290. if (size > ZORAN_MAX_FBUFSIZE || !ztv->fbuffer)
  1291. return -EINVAL;
  1292. /* start mapping the whole shabang to user memory */
  1293. pos = (unsigned long)ztv->fbuffer;
  1294. while (size>0) {
  1295. unsigned long pfn = virt_to_phys((void*)pos) >> PAGE_SHIFT;
  1296. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
  1297. return -EAGAIN;
  1298. start += PAGE_SIZE;
  1299. pos += PAGE_SIZE;
  1300. size -= PAGE_SIZE;
  1301. }
  1302. return 0;
  1303. }
  1304. static struct video_device zr36120_template=
  1305. {
  1306. .owner = THIS_MODULE,
  1307. .name = "UNSET",
  1308. .type = VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY,
  1309. .hardware = VID_HARDWARE_ZR36120,
  1310. .open = zoran_open,
  1311. .close = zoran_close,
  1312. .read = zoran_read,
  1313. .write = zoran_write,
  1314. .poll = zoran_poll,
  1315. .ioctl = zoran_ioctl,
  1316. .compat_ioctl = v4l_compat_ioctl32,
  1317. .mmap = zoran_mmap,
  1318. .minor = -1,
  1319. };
  1320. static
  1321. int vbi_open(struct video_device *dev, int flags)
  1322. {
  1323. struct zoran *ztv = dev->priv;
  1324. struct vidinfo* item;
  1325. DEBUG(printk(CARD_DEBUG "vbi_open(dev,%d)\n",CARD,flags));
  1326. /*
  1327. * During VBI device open, we continiously grab VBI-like
  1328. * data in the vbi buffer when we have nothing to do.
  1329. * Only when there is an explicit request for VBI data
  1330. * (read call) we /force/ a read.
  1331. */
  1332. /* allocate buffers */
  1333. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1334. {
  1335. item->status = FBUFFER_FREE;
  1336. /* alloc */
  1337. if (!item->memadr) {
  1338. item->memadr = bmalloc(ZORAN_VBI_BUFSIZE);
  1339. if (!item->memadr) {
  1340. /* could not get a buffer, bail out */
  1341. while (item != ztv->readinfo) {
  1342. item--;
  1343. bfree(item->memadr, ZORAN_VBI_BUFSIZE);
  1344. item->memadr = 0;
  1345. item->busadr = 0;
  1346. }
  1347. return -ENOBUFS;
  1348. }
  1349. }
  1350. /* determine the DMAable address */
  1351. item->busadr = virt_to_bus(item->memadr);
  1352. }
  1353. /* do the common part of all open's */
  1354. zoran_common_open(ztv, flags);
  1355. set_bit(STATE_VBI, &ztv->state);
  1356. /* start read-ahead */
  1357. zoran_cap(ztv, 1);
  1358. return 0;
  1359. }
  1360. static
  1361. void vbi_close(struct video_device *dev)
  1362. {
  1363. struct zoran *ztv = dev->priv;
  1364. struct vidinfo* item;
  1365. DEBUG(printk(CARD_DEBUG "vbi_close(dev)\n",CARD));
  1366. /* driver specific closure */
  1367. clear_bit(STATE_VBI, &ztv->state);
  1368. zoran_common_close(ztv);
  1369. /*
  1370. * This is sucky but right now I can't find a good way to
  1371. * be sure its safe to free the buffer. We wait 5-6 fields
  1372. * which is more than sufficient to be sure.
  1373. */
  1374. msleep(100); /* Wait 1/10th of a second */
  1375. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1376. {
  1377. if (item->memadr)
  1378. bfree(item->memadr, ZORAN_VBI_BUFSIZE);
  1379. item->memadr = 0;
  1380. }
  1381. }
  1382. /*
  1383. * This read function could be used reentrant in a SMP situation.
  1384. *
  1385. * This is made possible by the spinlock which is kept till we
  1386. * found and marked a buffer for our own use. The lock must
  1387. * be released as soon as possible to prevent lock contention.
  1388. */
  1389. static
  1390. long vbi_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
  1391. {
  1392. struct zoran *ztv = dev->priv;
  1393. unsigned long max;
  1394. struct vidinfo* unused = 0;
  1395. struct vidinfo* done = 0;
  1396. DEBUG(printk(CARD_DEBUG "vbi_read(0x%p,%ld,%d)\n",CARD,buf,count,nonblock));
  1397. /* find ourself a free or completed buffer */
  1398. for (;;) {
  1399. struct vidinfo* item;
  1400. write_lock_irq(&ztv->lock);
  1401. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++) {
  1402. if (!unused && item->status == FBUFFER_FREE)
  1403. unused = item;
  1404. if (!done && item->status == FBUFFER_DONE)
  1405. done = item;
  1406. }
  1407. if (done || unused)
  1408. break;
  1409. /* no more free buffers, wait for them. */
  1410. write_unlock_irq(&ztv->lock);
  1411. if (nonblock)
  1412. return -EWOULDBLOCK;
  1413. interruptible_sleep_on(&ztv->vbiq);
  1414. if (signal_pending(current))
  1415. return -EINTR;
  1416. }
  1417. /* Do we have 'ready' data? */
  1418. if (!done) {
  1419. /* no? than this will take a while... */
  1420. if (nonblock) {
  1421. write_unlock_irq(&ztv->lock);
  1422. return -EWOULDBLOCK;
  1423. }
  1424. /* mark the unused buffer as wanted */
  1425. unused->status = FBUFFER_BUSY;
  1426. unused->next = 0;
  1427. { /* add to tail of queue */
  1428. struct vidinfo* oldframe = ztv->workqueue;
  1429. if (!oldframe) ztv->workqueue = unused;
  1430. else {
  1431. while (oldframe->next) oldframe = oldframe->next;
  1432. oldframe->next = unused;
  1433. }
  1434. }
  1435. write_unlock_irq(&ztv->lock);
  1436. /* tell the state machine we want it filled /NOW/ */
  1437. zoran_cap(ztv, 1);
  1438. /* wait till this buffer gets grabbed */
  1439. wait_event_interruptible(ztv->vbiq,
  1440. (unused->status != FBUFFER_BUSY));
  1441. /* see if a signal did it */
  1442. if (signal_pending(current))
  1443. return -EINTR;
  1444. done = unused;
  1445. }
  1446. else
  1447. write_unlock_irq(&ztv->lock);
  1448. /* Yes! we got data! */
  1449. max = done->bpl * -done->h;
  1450. if (count > max)
  1451. count = max;
  1452. /* check if the user gave us enough room to write the data */
  1453. if (!access_ok(VERIFY_WRITE, buf, count)) {
  1454. count = -EFAULT;
  1455. goto out;
  1456. }
  1457. /*
  1458. * Now transform/strip the data from YUV to Y-only
  1459. * NB. Assume the Y is in the LSB of the YUV data.
  1460. */
  1461. {
  1462. unsigned char* optr = buf;
  1463. unsigned char* eptr = buf+count;
  1464. /* are we beeing accessed from an old driver? */
  1465. if (count == 2*19*2048) {
  1466. /*
  1467. * Extreme HACK, old VBI programs expect 2048 points
  1468. * of data, and we only got 864 orso. Double each
  1469. * datapoint and clear the rest of the line.
  1470. * This way we have appear to have a
  1471. * sample_frequency of 29.5 Mc.
  1472. */
  1473. int x,y;
  1474. unsigned char* iptr = done->memadr+1;
  1475. for (y=done->h; optr<eptr && y<0; y++)
  1476. {
  1477. /* copy to doubled data to userland */
  1478. for (x=0; optr+1<eptr && x<-done->w; x++)
  1479. {
  1480. unsigned char a = iptr[x*2];
  1481. __put_user(a, optr++);
  1482. __put_user(a, optr++);
  1483. }
  1484. /* and clear the rest of the line */
  1485. for (x*=2; optr<eptr && x<done->bpl; x++)
  1486. __put_user(0, optr++);
  1487. /* next line */
  1488. iptr += done->bpl;
  1489. }
  1490. }
  1491. else {
  1492. /*
  1493. * Other (probably newer) programs asked
  1494. * us what geometry we are using, and are
  1495. * reading the correct size.
  1496. */
  1497. int x,y;
  1498. unsigned char* iptr = done->memadr+1;
  1499. for (y=done->h; optr<eptr && y<0; y++)
  1500. {
  1501. /* copy to doubled data to userland */
  1502. for (x=0; optr<eptr && x<-done->w; x++)
  1503. __put_user(iptr[x*2], optr++);
  1504. /* and clear the rest of the line */
  1505. for (;optr<eptr && x<done->bpl; x++)
  1506. __put_user(0, optr++);
  1507. /* next line */
  1508. iptr += done->bpl;
  1509. }
  1510. }
  1511. /* API compliance:
  1512. * place the framenumber (half fieldnr) in the last long
  1513. */
  1514. __put_user(done->fieldnr/2, ((ulong*)eptr)[-1]);
  1515. }
  1516. /* keep the engine running */
  1517. done->status = FBUFFER_FREE;
  1518. zoran_cap(ztv, 1);
  1519. /* tell listeners this buffer just became free */
  1520. wake_up_interruptible(&ztv->vbiq);
  1521. /* goodbye */
  1522. out:
  1523. DEBUG(printk(CARD_DEBUG "vbi_read() returns %lu\n",CARD,count));
  1524. return count;
  1525. }
  1526. static
  1527. unsigned int vbi_poll(struct video_device *dev, struct file *file, poll_table *wait)
  1528. {
  1529. struct zoran *ztv = dev->priv;
  1530. struct vidinfo* item;
  1531. unsigned int mask = 0;
  1532. poll_wait(file, &ztv->vbiq, wait);
  1533. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1534. if (item->status == FBUFFER_DONE)
  1535. {
  1536. mask |= (POLLIN | POLLRDNORM);
  1537. break;
  1538. }
  1539. DEBUG(printk(CARD_DEBUG "vbi_poll()=%x\n",CARD,mask));
  1540. return mask;
  1541. }
  1542. static
  1543. int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
  1544. {
  1545. struct zoran* ztv = dev->priv;
  1546. switch (cmd) {
  1547. case VIDIOCGVBIFMT:
  1548. {
  1549. struct vbi_format f;
  1550. DEBUG(printk(CARD_DEBUG "VIDIOCGVBIINFO\n",CARD));
  1551. f.sampling_rate = 14750000UL;
  1552. f.samples_per_line = -ztv->readinfo[0].w;
  1553. f.sample_format = VIDEO_PALETTE_RAW;
  1554. f.start[0] = f.start[1] = ztv->readinfo[0].y;
  1555. f.start[1] += 312;
  1556. f.count[0] = f.count[1] = -ztv->readinfo[0].h;
  1557. f.flags = VBI_INTERLACED;
  1558. if (copy_to_user(arg,&f,sizeof(f)))
  1559. return -EFAULT;
  1560. break;
  1561. }
  1562. case VIDIOCSVBIFMT:
  1563. {
  1564. struct vbi_format f;
  1565. int i;
  1566. if (copy_from_user(&f, arg,sizeof(f)))
  1567. return -EFAULT;
  1568. 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));
  1569. /* lots of parameters are fixed... (PAL) */
  1570. if (f.sampling_rate != 14750000UL ||
  1571. f.samples_per_line > 864 ||
  1572. f.sample_format != VIDEO_PALETTE_RAW ||
  1573. f.start[0] < 0 ||
  1574. f.start[0] != f.start[1]-312 ||
  1575. f.count[0] != f.count[1] ||
  1576. f.start[0]+f.count[0] >= 288 ||
  1577. f.flags != VBI_INTERLACED)
  1578. return -EINVAL;
  1579. write_lock_irq(&ztv->lock);
  1580. ztv->readinfo[0].y = f.start[0];
  1581. ztv->readinfo[0].w = -f.samples_per_line;
  1582. ztv->readinfo[0].h = -f.count[0];
  1583. ztv->readinfo[0].bpl = f.samples_per_line*ztv->readinfo[0].bpp;
  1584. for (i=1; i<ZORAN_VBI_BUFFERS; i++)
  1585. ztv->readinfo[i] = ztv->readinfo[i];
  1586. write_unlock_irq(&ztv->lock);
  1587. break;
  1588. }
  1589. default:
  1590. return -ENOIOCTLCMD;
  1591. }
  1592. return 0;
  1593. }
  1594. static struct video_device vbi_template=
  1595. {
  1596. .owner = THIS_MODULE,
  1597. .name = "UNSET",
  1598. .type = VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
  1599. .hardware = VID_HARDWARE_ZR36120,
  1600. .open = vbi_open,
  1601. .close = vbi_close,
  1602. .read = vbi_read,
  1603. .write = zoran_write,
  1604. .poll = vbi_poll,
  1605. .ioctl = vbi_ioctl,
  1606. .minor = -1,
  1607. };
  1608. /*
  1609. * Scan for a Zoran chip, request the irq and map the io memory
  1610. */
  1611. static
  1612. int __init find_zoran(void)
  1613. {
  1614. int result;
  1615. struct zoran *ztv;
  1616. struct pci_dev *dev = NULL;
  1617. unsigned char revision;
  1618. int zoran_num = 0;
  1619. while ((dev = pci_get_device(PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120, dev)))
  1620. {
  1621. /* Ok, a ZR36120/ZR36125 found! */
  1622. ztv = &zorans[zoran_num];
  1623. ztv->dev = dev;
  1624. if (pci_enable_device(dev))
  1625. continue;
  1626. pci_read_config_byte(dev, PCI_CLASS_REVISION, &revision);
  1627. printk(KERN_INFO "zoran: Zoran %x (rev %d) ",
  1628. dev->device, revision);
  1629. printk("bus: %d, devfn: %d, irq: %d, ",
  1630. dev->bus->number, dev->devfn, dev->irq);
  1631. printk("memory: 0x%08lx.\n", ztv->zoran_adr);
  1632. ztv->zoran_mem = ioremap(ztv->zoran_adr, 0x1000);
  1633. DEBUG(printk(KERN_DEBUG "zoran: mapped-memory at 0x%p\n",ztv->zoran_mem));
  1634. result = request_irq(dev->irq, zoran_irq,
  1635. IRQF_SHARED|IRQF_DISABLED,"zoran", ztv);
  1636. if (result==-EINVAL)
  1637. {
  1638. iounmap(ztv->zoran_mem);
  1639. printk(KERN_ERR "zoran: Bad irq number or handler\n");
  1640. continue;
  1641. }
  1642. if (result==-EBUSY)
  1643. printk(KERN_ERR "zoran: IRQ %d busy, change your PnP config in BIOS\n",dev->irq);
  1644. if (result < 0) {
  1645. iounmap(ztv->zoran_mem);
  1646. continue;
  1647. }
  1648. /* Enable bus-mastering */
  1649. pci_set_master(dev);
  1650. /* Keep a reference */
  1651. pci_dev_get(dev);
  1652. zoran_num++;
  1653. }
  1654. if(zoran_num)
  1655. printk(KERN_INFO "zoran: %d Zoran card(s) found.\n",zoran_num);
  1656. return zoran_num;
  1657. }
  1658. static
  1659. int __init init_zoran(int card)
  1660. {
  1661. struct zoran *ztv = &zorans[card];
  1662. int i;
  1663. /* if the given cardtype valid? */
  1664. if (cardtype[card]>=NRTVCARDS) {
  1665. printk(KERN_INFO "invalid cardtype(%d) detected\n",cardtype[card]);
  1666. return -1;
  1667. }
  1668. /* reset the zoran */
  1669. zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1670. udelay(10);
  1671. zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1672. udelay(10);
  1673. /* zoran chip specific details */
  1674. ztv->card = tvcards+cardtype[card]; /* point to the selected card */
  1675. ztv->norm = 0; /* PAL */
  1676. ztv->tuner_freq = 0;
  1677. /* videocard details */
  1678. ztv->swidth = 800;
  1679. ztv->sheight = 600;
  1680. ztv->depth = 16;
  1681. /* State details */
  1682. ztv->fbuffer = 0;
  1683. ztv->overinfo.kindof = FBUFFER_OVERLAY;
  1684. ztv->overinfo.status = FBUFFER_FREE;
  1685. ztv->overinfo.x = 0;
  1686. ztv->overinfo.y = 0;
  1687. ztv->overinfo.w = 768; /* 640 */
  1688. ztv->overinfo.h = 576; /* 480 */
  1689. ztv->overinfo.format = VIDEO_PALETTE_RGB565;
  1690. ztv->overinfo.bpp = palette2fmt[ztv->overinfo.format].bpp;
  1691. ztv->overinfo.bpl = ztv->overinfo.bpp*ztv->swidth;
  1692. ztv->overinfo.busadr = 0;
  1693. ztv->overinfo.memadr = 0;
  1694. ztv->overinfo.overlay = 0;
  1695. for (i=0; i<ZORAN_MAX_FBUFFERS; i++) {
  1696. ztv->grabinfo[i] = ztv->overinfo;
  1697. ztv->grabinfo[i].kindof = FBUFFER_GRAB;
  1698. }
  1699. init_waitqueue_head(&ztv->grabq);
  1700. /* VBI details */
  1701. ztv->readinfo[0] = ztv->overinfo;
  1702. ztv->readinfo[0].kindof = FBUFFER_VBI;
  1703. ztv->readinfo[0].w = -864;
  1704. ztv->readinfo[0].h = -38;
  1705. ztv->readinfo[0].format = VIDEO_PALETTE_YUV422;
  1706. ztv->readinfo[0].bpp = palette2fmt[ztv->readinfo[0].format].bpp;
  1707. ztv->readinfo[0].bpl = 1024*ztv->readinfo[0].bpp;
  1708. for (i=1; i<ZORAN_VBI_BUFFERS; i++)
  1709. ztv->readinfo[i] = ztv->readinfo[0];
  1710. init_waitqueue_head(&ztv->vbiq);
  1711. /* maintenance data */
  1712. ztv->have_decoder = 0;
  1713. ztv->have_tuner = 0;
  1714. ztv->tuner_type = 0;
  1715. ztv->running = 0;
  1716. ztv->users = 0;
  1717. rwlock_init(&ztv->lock);
  1718. ztv->workqueue = 0;
  1719. ztv->fieldnr = 0;
  1720. ztv->lastfieldnr = 0;
  1721. if (triton1)
  1722. zrand(~ZORAN_VDC_TRICOM, ZORAN_VDC);
  1723. /* external FL determines TOP frame */
  1724. zror(ZORAN_VFEC_EXTFL, ZORAN_VFEC);
  1725. /* set HSpol */
  1726. if (ztv->card->hsync_pos)
  1727. zrwrite(ZORAN_VFEH_HSPOL, ZORAN_VFEH);
  1728. /* set VSpol */
  1729. if (ztv->card->vsync_pos)
  1730. zrwrite(ZORAN_VFEV_VSPOL, ZORAN_VFEV);
  1731. /* Set the proper General Purpuse register bits */
  1732. /* implicit: no softreset, 0 waitstates */
  1733. zrwrite(ZORAN_PCI_SOFTRESET|(ztv->card->gpdir<<0),ZORAN_PCI);
  1734. /* implicit: 3 duration and recovery PCI clocks on guest 0-3 */
  1735. zrwrite(ztv->card->gpval<<24,ZORAN_GUEST);
  1736. /* clear interrupt status */
  1737. zrwrite(~0, ZORAN_ISR);
  1738. /*
  1739. * i2c template
  1740. */
  1741. ztv->i2c = zoran_i2c_bus_template;
  1742. sprintf(ztv->i2c.name,"zoran-%d",card);
  1743. ztv->i2c.data = ztv;
  1744. /*
  1745. * Now add the template and register the device unit
  1746. */
  1747. ztv->video_dev = zr36120_template;
  1748. strcpy(ztv->video_dev.name, ztv->i2c.name);
  1749. ztv->video_dev.priv = ztv;
  1750. if (video_register_device(&ztv->video_dev, VFL_TYPE_GRABBER, video_nr) < 0)
  1751. return -1;
  1752. ztv->vbi_dev = vbi_template;
  1753. strcpy(ztv->vbi_dev.name, ztv->i2c.name);
  1754. ztv->vbi_dev.priv = ztv;
  1755. if (video_register_device(&ztv->vbi_dev, VFL_TYPE_VBI, vbi_nr) < 0) {
  1756. video_unregister_device(&ztv->video_dev);
  1757. return -1;
  1758. }
  1759. i2c_register_bus(&ztv->i2c);
  1760. /* set interrupt mask - the PIN enable will be set later */
  1761. zrwrite(ZORAN_ICR_GIRQ0|ZORAN_ICR_GIRQ1|ZORAN_ICR_CODE, ZORAN_ICR);
  1762. printk(KERN_INFO "%s: installed %s\n",ztv->i2c.name,ztv->card->name);
  1763. return 0;
  1764. }
  1765. static
  1766. void release_zoran(int max)
  1767. {
  1768. struct zoran *ztv;
  1769. int i;
  1770. for (i=0;i<max; i++)
  1771. {
  1772. ztv = &zorans[i];
  1773. /* turn off all capturing, DMA and IRQs */
  1774. /* reset the zoran */
  1775. zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1776. udelay(10);
  1777. zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1778. udelay(10);
  1779. /* first disable interrupts before unmapping the memory! */
  1780. zrwrite(0, ZORAN_ICR);
  1781. zrwrite(0xffffffffUL,ZORAN_ISR);
  1782. /* free it */
  1783. free_irq(ztv->dev->irq,ztv);
  1784. /* unregister i2c_bus */
  1785. i2c_unregister_bus((&ztv->i2c));
  1786. /* unmap and free memory */
  1787. if (ztv->zoran_mem)
  1788. iounmap(ztv->zoran_mem);
  1789. /* Drop PCI device */
  1790. pci_dev_put(ztv->dev);
  1791. video_unregister_device(&ztv->video_dev);
  1792. video_unregister_device(&ztv->vbi_dev);
  1793. }
  1794. }
  1795. void __exit zr36120_exit(void)
  1796. {
  1797. release_zoran(zoran_cards);
  1798. }
  1799. int __init zr36120_init(void)
  1800. {
  1801. int card;
  1802. handle_chipset();
  1803. zoran_cards = find_zoran();
  1804. if (zoran_cards <= 0)
  1805. return -EIO;
  1806. /* initialize Zorans */
  1807. for (card=0; card<zoran_cards; card++) {
  1808. if (init_zoran(card) < 0) {
  1809. /* only release the zorans we have registered */
  1810. release_zoran(card);
  1811. return -EIO;
  1812. }
  1813. }
  1814. return 0;
  1815. }
  1816. module_init(zr36120_init);
  1817. module_exit(zr36120_exit);