cx88-video.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. /*
  2. *
  3. * device driver for Conexant 2388x based TV cards
  4. * video4linux video interface
  5. *
  6. * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  7. *
  8. * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
  9. * - Multituner support
  10. * - video_ioctl2 conversion
  11. * - PAL/M fixes
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/init.h>
  28. #include <linux/list.h>
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/kmod.h>
  32. #include <linux/kernel.h>
  33. #include <linux/slab.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/delay.h>
  36. #include <linux/kthread.h>
  37. #include <asm/div64.h>
  38. #include "cx88.h"
  39. #include <media/v4l2-common.h>
  40. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  41. /* Include V4L1 specific functions. Should be removed soon */
  42. #include <linux/videodev.h>
  43. #endif
  44. MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
  45. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  46. MODULE_LICENSE("GPL");
  47. /* ------------------------------------------------------------------ */
  48. static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  49. static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  50. static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  51. module_param_array(video_nr, int, NULL, 0444);
  52. module_param_array(vbi_nr, int, NULL, 0444);
  53. module_param_array(radio_nr, int, NULL, 0444);
  54. MODULE_PARM_DESC(video_nr,"video device numbers");
  55. MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
  56. MODULE_PARM_DESC(radio_nr,"radio device numbers");
  57. static unsigned int video_debug = 0;
  58. module_param(video_debug,int,0644);
  59. MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
  60. static unsigned int irq_debug = 0;
  61. module_param(irq_debug,int,0644);
  62. MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
  63. static unsigned int vid_limit = 16;
  64. module_param(vid_limit,int,0644);
  65. MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
  66. #define dprintk(level,fmt, arg...) if (video_debug >= level) \
  67. printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
  68. /* ------------------------------------------------------------------ */
  69. static LIST_HEAD(cx8800_devlist);
  70. /* ------------------------------------------------------------------- */
  71. /* static data */
  72. static struct cx8800_fmt formats[] = {
  73. {
  74. .name = "8 bpp, gray",
  75. .fourcc = V4L2_PIX_FMT_GREY,
  76. .cxformat = ColorFormatY8,
  77. .depth = 8,
  78. .flags = FORMAT_FLAGS_PACKED,
  79. },{
  80. .name = "15 bpp RGB, le",
  81. .fourcc = V4L2_PIX_FMT_RGB555,
  82. .cxformat = ColorFormatRGB15,
  83. .depth = 16,
  84. .flags = FORMAT_FLAGS_PACKED,
  85. },{
  86. .name = "15 bpp RGB, be",
  87. .fourcc = V4L2_PIX_FMT_RGB555X,
  88. .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
  89. .depth = 16,
  90. .flags = FORMAT_FLAGS_PACKED,
  91. },{
  92. .name = "16 bpp RGB, le",
  93. .fourcc = V4L2_PIX_FMT_RGB565,
  94. .cxformat = ColorFormatRGB16,
  95. .depth = 16,
  96. .flags = FORMAT_FLAGS_PACKED,
  97. },{
  98. .name = "16 bpp RGB, be",
  99. .fourcc = V4L2_PIX_FMT_RGB565X,
  100. .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
  101. .depth = 16,
  102. .flags = FORMAT_FLAGS_PACKED,
  103. },{
  104. .name = "24 bpp RGB, le",
  105. .fourcc = V4L2_PIX_FMT_BGR24,
  106. .cxformat = ColorFormatRGB24,
  107. .depth = 24,
  108. .flags = FORMAT_FLAGS_PACKED,
  109. },{
  110. .name = "32 bpp RGB, le",
  111. .fourcc = V4L2_PIX_FMT_BGR32,
  112. .cxformat = ColorFormatRGB32,
  113. .depth = 32,
  114. .flags = FORMAT_FLAGS_PACKED,
  115. },{
  116. .name = "32 bpp RGB, be",
  117. .fourcc = V4L2_PIX_FMT_RGB32,
  118. .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
  119. .depth = 32,
  120. .flags = FORMAT_FLAGS_PACKED,
  121. },{
  122. .name = "4:2:2, packed, YUYV",
  123. .fourcc = V4L2_PIX_FMT_YUYV,
  124. .cxformat = ColorFormatYUY2,
  125. .depth = 16,
  126. .flags = FORMAT_FLAGS_PACKED,
  127. },{
  128. .name = "4:2:2, packed, UYVY",
  129. .fourcc = V4L2_PIX_FMT_UYVY,
  130. .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
  131. .depth = 16,
  132. .flags = FORMAT_FLAGS_PACKED,
  133. },
  134. };
  135. static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
  136. {
  137. unsigned int i;
  138. for (i = 0; i < ARRAY_SIZE(formats); i++)
  139. if (formats[i].fourcc == fourcc)
  140. return formats+i;
  141. return NULL;
  142. }
  143. /* ------------------------------------------------------------------- */
  144. static const struct v4l2_queryctrl no_ctl = {
  145. .name = "42",
  146. .flags = V4L2_CTRL_FLAG_DISABLED,
  147. };
  148. static struct cx88_ctrl cx8800_ctls[] = {
  149. /* --- video --- */
  150. {
  151. .v = {
  152. .id = V4L2_CID_BRIGHTNESS,
  153. .name = "Brightness",
  154. .minimum = 0x00,
  155. .maximum = 0xff,
  156. .step = 1,
  157. .default_value = 0x7f,
  158. .type = V4L2_CTRL_TYPE_INTEGER,
  159. },
  160. .off = 128,
  161. .reg = MO_CONTR_BRIGHT,
  162. .mask = 0x00ff,
  163. .shift = 0,
  164. },{
  165. .v = {
  166. .id = V4L2_CID_CONTRAST,
  167. .name = "Contrast",
  168. .minimum = 0,
  169. .maximum = 0xff,
  170. .step = 1,
  171. .default_value = 0x3f,
  172. .type = V4L2_CTRL_TYPE_INTEGER,
  173. },
  174. .off = 0,
  175. .reg = MO_CONTR_BRIGHT,
  176. .mask = 0xff00,
  177. .shift = 8,
  178. },{
  179. .v = {
  180. .id = V4L2_CID_HUE,
  181. .name = "Hue",
  182. .minimum = 0,
  183. .maximum = 0xff,
  184. .step = 1,
  185. .default_value = 0x7f,
  186. .type = V4L2_CTRL_TYPE_INTEGER,
  187. },
  188. .off = 128,
  189. .reg = MO_HUE,
  190. .mask = 0x00ff,
  191. .shift = 0,
  192. },{
  193. /* strictly, this only describes only U saturation.
  194. * V saturation is handled specially through code.
  195. */
  196. .v = {
  197. .id = V4L2_CID_SATURATION,
  198. .name = "Saturation",
  199. .minimum = 0,
  200. .maximum = 0xff,
  201. .step = 1,
  202. .default_value = 0x7f,
  203. .type = V4L2_CTRL_TYPE_INTEGER,
  204. },
  205. .off = 0,
  206. .reg = MO_UV_SATURATION,
  207. .mask = 0x00ff,
  208. .shift = 0,
  209. },{
  210. /* --- audio --- */
  211. .v = {
  212. .id = V4L2_CID_AUDIO_MUTE,
  213. .name = "Mute",
  214. .minimum = 0,
  215. .maximum = 1,
  216. .default_value = 1,
  217. .type = V4L2_CTRL_TYPE_BOOLEAN,
  218. },
  219. .reg = AUD_VOL_CTL,
  220. .sreg = SHADOW_AUD_VOL_CTL,
  221. .mask = (1 << 6),
  222. .shift = 6,
  223. },{
  224. .v = {
  225. .id = V4L2_CID_AUDIO_VOLUME,
  226. .name = "Volume",
  227. .minimum = 0,
  228. .maximum = 0x3f,
  229. .step = 1,
  230. .default_value = 0x3f,
  231. .type = V4L2_CTRL_TYPE_INTEGER,
  232. },
  233. .reg = AUD_VOL_CTL,
  234. .sreg = SHADOW_AUD_VOL_CTL,
  235. .mask = 0x3f,
  236. .shift = 0,
  237. },{
  238. .v = {
  239. .id = V4L2_CID_AUDIO_BALANCE,
  240. .name = "Balance",
  241. .minimum = 0,
  242. .maximum = 0x7f,
  243. .step = 1,
  244. .default_value = 0x40,
  245. .type = V4L2_CTRL_TYPE_INTEGER,
  246. },
  247. .reg = AUD_BAL_CTL,
  248. .sreg = SHADOW_AUD_BAL_CTL,
  249. .mask = 0x7f,
  250. .shift = 0,
  251. }
  252. };
  253. static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
  254. const u32 cx88_user_ctrls[] = {
  255. V4L2_CID_USER_CLASS,
  256. V4L2_CID_BRIGHTNESS,
  257. V4L2_CID_CONTRAST,
  258. V4L2_CID_SATURATION,
  259. V4L2_CID_HUE,
  260. V4L2_CID_AUDIO_VOLUME,
  261. V4L2_CID_AUDIO_BALANCE,
  262. V4L2_CID_AUDIO_MUTE,
  263. 0
  264. };
  265. EXPORT_SYMBOL(cx88_user_ctrls);
  266. static const u32 *ctrl_classes[] = {
  267. cx88_user_ctrls,
  268. NULL
  269. };
  270. int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
  271. {
  272. int i;
  273. if (qctrl->id < V4L2_CID_BASE ||
  274. qctrl->id >= V4L2_CID_LASTP1)
  275. return -EINVAL;
  276. for (i = 0; i < CX8800_CTLS; i++)
  277. if (cx8800_ctls[i].v.id == qctrl->id)
  278. break;
  279. if (i == CX8800_CTLS) {
  280. *qctrl = no_ctl;
  281. return 0;
  282. }
  283. *qctrl = cx8800_ctls[i].v;
  284. return 0;
  285. }
  286. EXPORT_SYMBOL(cx8800_ctrl_query);
  287. /* ------------------------------------------------------------------- */
  288. /* resource management */
  289. static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
  290. {
  291. struct cx88_core *core = dev->core;
  292. if (fh->resources & bit)
  293. /* have it already allocated */
  294. return 1;
  295. /* is it free? */
  296. mutex_lock(&core->lock);
  297. if (dev->resources & bit) {
  298. /* no, someone else uses it */
  299. mutex_unlock(&core->lock);
  300. return 0;
  301. }
  302. /* it's free, grab it */
  303. fh->resources |= bit;
  304. dev->resources |= bit;
  305. dprintk(1,"res: get %d\n",bit);
  306. mutex_unlock(&core->lock);
  307. return 1;
  308. }
  309. static
  310. int res_check(struct cx8800_fh *fh, unsigned int bit)
  311. {
  312. return (fh->resources & bit);
  313. }
  314. static
  315. int res_locked(struct cx8800_dev *dev, unsigned int bit)
  316. {
  317. return (dev->resources & bit);
  318. }
  319. static
  320. void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
  321. {
  322. struct cx88_core *core = dev->core;
  323. BUG_ON((fh->resources & bits) != bits);
  324. mutex_lock(&core->lock);
  325. fh->resources &= ~bits;
  326. dev->resources &= ~bits;
  327. dprintk(1,"res: put %d\n",bits);
  328. mutex_unlock(&core->lock);
  329. }
  330. /* ------------------------------------------------------------------ */
  331. int cx88_video_mux(struct cx88_core *core, unsigned int input)
  332. {
  333. /* struct cx88_core *core = dev->core; */
  334. dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
  335. input, INPUT(input)->vmux,
  336. INPUT(input)->gpio0,INPUT(input)->gpio1,
  337. INPUT(input)->gpio2,INPUT(input)->gpio3);
  338. core->input = input;
  339. cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
  340. cx_write(MO_GP3_IO, INPUT(input)->gpio3);
  341. cx_write(MO_GP0_IO, INPUT(input)->gpio0);
  342. cx_write(MO_GP1_IO, INPUT(input)->gpio1);
  343. cx_write(MO_GP2_IO, INPUT(input)->gpio2);
  344. switch (INPUT(input)->type) {
  345. case CX88_VMUX_SVIDEO:
  346. cx_set(MO_AFECFG_IO, 0x00000001);
  347. cx_set(MO_INPUT_FORMAT, 0x00010010);
  348. cx_set(MO_FILTER_EVEN, 0x00002020);
  349. cx_set(MO_FILTER_ODD, 0x00002020);
  350. break;
  351. default:
  352. cx_clear(MO_AFECFG_IO, 0x00000001);
  353. cx_clear(MO_INPUT_FORMAT, 0x00010010);
  354. cx_clear(MO_FILTER_EVEN, 0x00002020);
  355. cx_clear(MO_FILTER_ODD, 0x00002020);
  356. break;
  357. }
  358. if (cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD) {
  359. /* sets sound input from external adc */
  360. if (INPUT(input)->extadc)
  361. cx_set(AUD_CTL, EN_I2SIN_ENABLE);
  362. else
  363. cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
  364. }
  365. return 0;
  366. }
  367. EXPORT_SYMBOL(cx88_video_mux);
  368. /* ------------------------------------------------------------------ */
  369. static int start_video_dma(struct cx8800_dev *dev,
  370. struct cx88_dmaqueue *q,
  371. struct cx88_buffer *buf)
  372. {
  373. struct cx88_core *core = dev->core;
  374. /* setup fifo + format */
  375. cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
  376. buf->bpl, buf->risc.dma);
  377. cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
  378. cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
  379. /* reset counter */
  380. cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
  381. q->count = 1;
  382. /* enable irqs */
  383. cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
  384. /* Enables corresponding bits at PCI_INT_STAT:
  385. bits 0 to 4: video, audio, transport stream, VIP, Host
  386. bit 7: timer
  387. bits 8 and 9: DMA complete for: SRC, DST
  388. bits 10 and 11: BERR signal asserted for RISC: RD, WR
  389. bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
  390. */
  391. cx_set(MO_VID_INTMSK, 0x0f0011);
  392. /* enable capture */
  393. cx_set(VID_CAPTURE_CONTROL,0x06);
  394. /* start dma */
  395. cx_set(MO_DEV_CNTRL2, (1<<5));
  396. cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
  397. return 0;
  398. }
  399. #ifdef CONFIG_PM
  400. static int stop_video_dma(struct cx8800_dev *dev)
  401. {
  402. struct cx88_core *core = dev->core;
  403. /* stop dma */
  404. cx_clear(MO_VID_DMACNTRL, 0x11);
  405. /* disable capture */
  406. cx_clear(VID_CAPTURE_CONTROL,0x06);
  407. /* disable irqs */
  408. cx_clear(MO_PCI_INTMSK, 0x000001);
  409. cx_clear(MO_VID_INTMSK, 0x0f0011);
  410. return 0;
  411. }
  412. #endif
  413. static int restart_video_queue(struct cx8800_dev *dev,
  414. struct cx88_dmaqueue *q)
  415. {
  416. struct cx88_core *core = dev->core;
  417. struct cx88_buffer *buf, *prev;
  418. struct list_head *item;
  419. if (!list_empty(&q->active)) {
  420. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  421. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  422. buf, buf->vb.i);
  423. start_video_dma(dev, q, buf);
  424. list_for_each(item,&q->active) {
  425. buf = list_entry(item, struct cx88_buffer, vb.queue);
  426. buf->count = q->count++;
  427. }
  428. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  429. return 0;
  430. }
  431. prev = NULL;
  432. for (;;) {
  433. if (list_empty(&q->queued))
  434. return 0;
  435. buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
  436. if (NULL == prev) {
  437. list_move_tail(&buf->vb.queue, &q->active);
  438. start_video_dma(dev, q, buf);
  439. buf->vb.state = STATE_ACTIVE;
  440. buf->count = q->count++;
  441. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  442. dprintk(2,"[%p/%d] restart_queue - first active\n",
  443. buf,buf->vb.i);
  444. } else if (prev->vb.width == buf->vb.width &&
  445. prev->vb.height == buf->vb.height &&
  446. prev->fmt == buf->fmt) {
  447. list_move_tail(&buf->vb.queue, &q->active);
  448. buf->vb.state = STATE_ACTIVE;
  449. buf->count = q->count++;
  450. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  451. dprintk(2,"[%p/%d] restart_queue - move to active\n",
  452. buf,buf->vb.i);
  453. } else {
  454. return 0;
  455. }
  456. prev = buf;
  457. }
  458. }
  459. /* ------------------------------------------------------------------ */
  460. static int
  461. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  462. {
  463. struct cx8800_fh *fh = q->priv_data;
  464. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  465. if (0 == *count)
  466. *count = 32;
  467. while (*size * *count > vid_limit * 1024 * 1024)
  468. (*count)--;
  469. return 0;
  470. }
  471. static int
  472. buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  473. enum v4l2_field field)
  474. {
  475. struct cx8800_fh *fh = q->priv_data;
  476. struct cx8800_dev *dev = fh->dev;
  477. struct cx88_core *core = dev->core;
  478. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  479. int rc, init_buffer = 0;
  480. BUG_ON(NULL == fh->fmt);
  481. if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
  482. fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
  483. return -EINVAL;
  484. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  485. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  486. return -EINVAL;
  487. if (buf->fmt != fh->fmt ||
  488. buf->vb.width != fh->width ||
  489. buf->vb.height != fh->height ||
  490. buf->vb.field != field) {
  491. buf->fmt = fh->fmt;
  492. buf->vb.width = fh->width;
  493. buf->vb.height = fh->height;
  494. buf->vb.field = field;
  495. init_buffer = 1;
  496. }
  497. if (STATE_NEEDS_INIT == buf->vb.state) {
  498. init_buffer = 1;
  499. if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
  500. goto fail;
  501. }
  502. if (init_buffer) {
  503. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  504. switch (buf->vb.field) {
  505. case V4L2_FIELD_TOP:
  506. cx88_risc_buffer(dev->pci, &buf->risc,
  507. buf->vb.dma.sglist, 0, UNSET,
  508. buf->bpl, 0, buf->vb.height);
  509. break;
  510. case V4L2_FIELD_BOTTOM:
  511. cx88_risc_buffer(dev->pci, &buf->risc,
  512. buf->vb.dma.sglist, UNSET, 0,
  513. buf->bpl, 0, buf->vb.height);
  514. break;
  515. case V4L2_FIELD_INTERLACED:
  516. cx88_risc_buffer(dev->pci, &buf->risc,
  517. buf->vb.dma.sglist, 0, buf->bpl,
  518. buf->bpl, buf->bpl,
  519. buf->vb.height >> 1);
  520. break;
  521. case V4L2_FIELD_SEQ_TB:
  522. cx88_risc_buffer(dev->pci, &buf->risc,
  523. buf->vb.dma.sglist,
  524. 0, buf->bpl * (buf->vb.height >> 1),
  525. buf->bpl, 0,
  526. buf->vb.height >> 1);
  527. break;
  528. case V4L2_FIELD_SEQ_BT:
  529. cx88_risc_buffer(dev->pci, &buf->risc,
  530. buf->vb.dma.sglist,
  531. buf->bpl * (buf->vb.height >> 1), 0,
  532. buf->bpl, 0,
  533. buf->vb.height >> 1);
  534. break;
  535. default:
  536. BUG();
  537. }
  538. }
  539. dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  540. buf, buf->vb.i,
  541. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  542. (unsigned long)buf->risc.dma);
  543. buf->vb.state = STATE_PREPARED;
  544. return 0;
  545. fail:
  546. cx88_free_buffer(q,buf);
  547. return rc;
  548. }
  549. static void
  550. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  551. {
  552. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  553. struct cx88_buffer *prev;
  554. struct cx8800_fh *fh = vq->priv_data;
  555. struct cx8800_dev *dev = fh->dev;
  556. struct cx88_core *core = dev->core;
  557. struct cx88_dmaqueue *q = &dev->vidq;
  558. /* add jump to stopper */
  559. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  560. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  561. if (!list_empty(&q->queued)) {
  562. list_add_tail(&buf->vb.queue,&q->queued);
  563. buf->vb.state = STATE_QUEUED;
  564. dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
  565. buf, buf->vb.i);
  566. } else if (list_empty(&q->active)) {
  567. list_add_tail(&buf->vb.queue,&q->active);
  568. start_video_dma(dev, q, buf);
  569. buf->vb.state = STATE_ACTIVE;
  570. buf->count = q->count++;
  571. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  572. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  573. buf, buf->vb.i);
  574. } else {
  575. prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
  576. if (prev->vb.width == buf->vb.width &&
  577. prev->vb.height == buf->vb.height &&
  578. prev->fmt == buf->fmt) {
  579. list_add_tail(&buf->vb.queue,&q->active);
  580. buf->vb.state = STATE_ACTIVE;
  581. buf->count = q->count++;
  582. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  583. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  584. buf, buf->vb.i);
  585. } else {
  586. list_add_tail(&buf->vb.queue,&q->queued);
  587. buf->vb.state = STATE_QUEUED;
  588. dprintk(2,"[%p/%d] buffer_queue - first queued\n",
  589. buf, buf->vb.i);
  590. }
  591. }
  592. }
  593. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  594. {
  595. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  596. cx88_free_buffer(q,buf);
  597. }
  598. static struct videobuf_queue_ops cx8800_video_qops = {
  599. .buf_setup = buffer_setup,
  600. .buf_prepare = buffer_prepare,
  601. .buf_queue = buffer_queue,
  602. .buf_release = buffer_release,
  603. };
  604. /* ------------------------------------------------------------------ */
  605. /* ------------------------------------------------------------------ */
  606. static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
  607. {
  608. switch (fh->type) {
  609. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  610. return &fh->vidq;
  611. case V4L2_BUF_TYPE_VBI_CAPTURE:
  612. return &fh->vbiq;
  613. default:
  614. BUG();
  615. return NULL;
  616. }
  617. }
  618. static int get_ressource(struct cx8800_fh *fh)
  619. {
  620. switch (fh->type) {
  621. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  622. return RESOURCE_VIDEO;
  623. case V4L2_BUF_TYPE_VBI_CAPTURE:
  624. return RESOURCE_VBI;
  625. default:
  626. BUG();
  627. return 0;
  628. }
  629. }
  630. static int video_open(struct inode *inode, struct file *file)
  631. {
  632. int minor = iminor(inode);
  633. struct cx8800_dev *h,*dev = NULL;
  634. struct cx88_core *core;
  635. struct cx8800_fh *fh;
  636. struct list_head *list;
  637. enum v4l2_buf_type type = 0;
  638. int radio = 0;
  639. list_for_each(list,&cx8800_devlist) {
  640. h = list_entry(list, struct cx8800_dev, devlist);
  641. if (h->video_dev->minor == minor) {
  642. dev = h;
  643. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  644. }
  645. if (h->vbi_dev->minor == minor) {
  646. dev = h;
  647. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  648. }
  649. if (h->radio_dev &&
  650. h->radio_dev->minor == minor) {
  651. radio = 1;
  652. dev = h;
  653. }
  654. }
  655. if (NULL == dev)
  656. return -ENODEV;
  657. core = dev->core;
  658. dprintk(1,"open minor=%d radio=%d type=%s\n",
  659. minor,radio,v4l2_type_names[type]);
  660. /* allocate + initialize per filehandle data */
  661. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  662. if (NULL == fh)
  663. return -ENOMEM;
  664. file->private_data = fh;
  665. fh->dev = dev;
  666. fh->radio = radio;
  667. fh->type = type;
  668. fh->width = 320;
  669. fh->height = 240;
  670. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  671. videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
  672. dev->pci, &dev->slock,
  673. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  674. V4L2_FIELD_INTERLACED,
  675. sizeof(struct cx88_buffer),
  676. fh);
  677. videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
  678. dev->pci, &dev->slock,
  679. V4L2_BUF_TYPE_VBI_CAPTURE,
  680. V4L2_FIELD_SEQ_TB,
  681. sizeof(struct cx88_buffer),
  682. fh);
  683. if (fh->radio) {
  684. int board = core->board;
  685. dprintk(1,"video_open: setting radio device\n");
  686. cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
  687. cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
  688. cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
  689. cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
  690. core->tvaudio = WW_FM;
  691. cx88_set_tvaudio(core);
  692. cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
  693. cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
  694. }
  695. return 0;
  696. }
  697. static ssize_t
  698. video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  699. {
  700. struct cx8800_fh *fh = file->private_data;
  701. switch (fh->type) {
  702. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  703. if (res_locked(fh->dev,RESOURCE_VIDEO))
  704. return -EBUSY;
  705. return videobuf_read_one(&fh->vidq, data, count, ppos,
  706. file->f_flags & O_NONBLOCK);
  707. case V4L2_BUF_TYPE_VBI_CAPTURE:
  708. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  709. return -EBUSY;
  710. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  711. file->f_flags & O_NONBLOCK);
  712. default:
  713. BUG();
  714. return 0;
  715. }
  716. }
  717. static unsigned int
  718. video_poll(struct file *file, struct poll_table_struct *wait)
  719. {
  720. struct cx8800_fh *fh = file->private_data;
  721. struct cx88_buffer *buf;
  722. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  723. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  724. return POLLERR;
  725. return videobuf_poll_stream(file, &fh->vbiq, wait);
  726. }
  727. if (res_check(fh,RESOURCE_VIDEO)) {
  728. /* streaming capture */
  729. if (list_empty(&fh->vidq.stream))
  730. return POLLERR;
  731. buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
  732. } else {
  733. /* read() capture */
  734. buf = (struct cx88_buffer*)fh->vidq.read_buf;
  735. if (NULL == buf)
  736. return POLLERR;
  737. }
  738. poll_wait(file, &buf->vb.done, wait);
  739. if (buf->vb.state == STATE_DONE ||
  740. buf->vb.state == STATE_ERROR)
  741. return POLLIN|POLLRDNORM;
  742. return 0;
  743. }
  744. static int video_release(struct inode *inode, struct file *file)
  745. {
  746. struct cx8800_fh *fh = file->private_data;
  747. struct cx8800_dev *dev = fh->dev;
  748. /* turn off overlay */
  749. if (res_check(fh, RESOURCE_OVERLAY)) {
  750. /* FIXME */
  751. res_free(dev,fh,RESOURCE_OVERLAY);
  752. }
  753. /* stop video capture */
  754. if (res_check(fh, RESOURCE_VIDEO)) {
  755. videobuf_queue_cancel(&fh->vidq);
  756. res_free(dev,fh,RESOURCE_VIDEO);
  757. }
  758. if (fh->vidq.read_buf) {
  759. buffer_release(&fh->vidq,fh->vidq.read_buf);
  760. kfree(fh->vidq.read_buf);
  761. }
  762. /* stop vbi capture */
  763. if (res_check(fh, RESOURCE_VBI)) {
  764. if (fh->vbiq.streaming)
  765. videobuf_streamoff(&fh->vbiq);
  766. if (fh->vbiq.reading)
  767. videobuf_read_stop(&fh->vbiq);
  768. res_free(dev,fh,RESOURCE_VBI);
  769. }
  770. videobuf_mmap_free(&fh->vidq);
  771. videobuf_mmap_free(&fh->vbiq);
  772. file->private_data = NULL;
  773. kfree(fh);
  774. cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
  775. return 0;
  776. }
  777. static int
  778. video_mmap(struct file *file, struct vm_area_struct * vma)
  779. {
  780. struct cx8800_fh *fh = file->private_data;
  781. return videobuf_mmap_mapper(get_queue(fh), vma);
  782. }
  783. /* ------------------------------------------------------------------ */
  784. /* VIDEO CTRL IOCTLS */
  785. int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
  786. {
  787. struct cx88_ctrl *c = NULL;
  788. u32 value;
  789. int i;
  790. for (i = 0; i < CX8800_CTLS; i++)
  791. if (cx8800_ctls[i].v.id == ctl->id)
  792. c = &cx8800_ctls[i];
  793. if (unlikely(NULL == c))
  794. return -EINVAL;
  795. value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
  796. switch (ctl->id) {
  797. case V4L2_CID_AUDIO_BALANCE:
  798. ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
  799. : (0x7f - (value & 0x7f));
  800. break;
  801. case V4L2_CID_AUDIO_VOLUME:
  802. ctl->value = 0x3f - (value & 0x3f);
  803. break;
  804. default:
  805. ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
  806. break;
  807. }
  808. dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  809. ctl->id, c->v.name, ctl->value, c->reg,
  810. value,c->mask, c->sreg ? " [shadowed]" : "");
  811. return 0;
  812. }
  813. EXPORT_SYMBOL(cx88_get_control);
  814. int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
  815. {
  816. struct cx88_ctrl *c = NULL;
  817. u32 value,mask;
  818. int i;
  819. for (i = 0; i < CX8800_CTLS; i++) {
  820. if (cx8800_ctls[i].v.id == ctl->id) {
  821. c = &cx8800_ctls[i];
  822. }
  823. }
  824. if (unlikely(NULL == c))
  825. return -EINVAL;
  826. if (ctl->value < c->v.minimum)
  827. ctl->value = c->v.minimum;
  828. if (ctl->value > c->v.maximum)
  829. ctl->value = c->v.maximum;
  830. mask=c->mask;
  831. switch (ctl->id) {
  832. case V4L2_CID_AUDIO_BALANCE:
  833. value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
  834. break;
  835. case V4L2_CID_AUDIO_VOLUME:
  836. value = 0x3f - (ctl->value & 0x3f);
  837. break;
  838. case V4L2_CID_SATURATION:
  839. /* special v_sat handling */
  840. value = ((ctl->value - c->off) << c->shift) & c->mask;
  841. if (core->tvnorm & V4L2_STD_SECAM) {
  842. /* For SECAM, both U and V sat should be equal */
  843. value=value<<8|value;
  844. } else {
  845. /* Keeps U Saturation proportional to V Sat */
  846. value=(value*0x5a)/0x7f<<8|value;
  847. }
  848. mask=0xffff;
  849. break;
  850. default:
  851. value = ((ctl->value - c->off) << c->shift) & c->mask;
  852. break;
  853. }
  854. dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  855. ctl->id, c->v.name, ctl->value, c->reg, value,
  856. mask, c->sreg ? " [shadowed]" : "");
  857. if (c->sreg) {
  858. cx_sandor(c->sreg, c->reg, mask, value);
  859. } else {
  860. cx_andor(c->reg, mask, value);
  861. }
  862. return 0;
  863. }
  864. EXPORT_SYMBOL(cx88_set_control);
  865. static void init_controls(struct cx88_core *core)
  866. {
  867. struct v4l2_control ctrl;
  868. int i;
  869. for (i = 0; i < CX8800_CTLS; i++) {
  870. ctrl.id=cx8800_ctls[i].v.id;
  871. ctrl.value=cx8800_ctls[i].v.default_value;
  872. cx88_set_control(core, &ctrl);
  873. }
  874. }
  875. /* ------------------------------------------------------------------ */
  876. /* VIDEO IOCTLS */
  877. static int vidioc_g_fmt_cap (struct file *file, void *priv,
  878. struct v4l2_format *f)
  879. {
  880. struct cx8800_fh *fh = priv;
  881. f->fmt.pix.width = fh->width;
  882. f->fmt.pix.height = fh->height;
  883. f->fmt.pix.field = fh->vidq.field;
  884. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  885. f->fmt.pix.bytesperline =
  886. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  887. f->fmt.pix.sizeimage =
  888. f->fmt.pix.height * f->fmt.pix.bytesperline;
  889. return 0;
  890. }
  891. static int vidioc_try_fmt_cap (struct file *file, void *priv,
  892. struct v4l2_format *f)
  893. {
  894. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  895. struct cx8800_fmt *fmt;
  896. enum v4l2_field field;
  897. unsigned int maxw, maxh;
  898. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  899. if (NULL == fmt)
  900. return -EINVAL;
  901. field = f->fmt.pix.field;
  902. maxw = norm_maxw(core->tvnorm);
  903. maxh = norm_maxh(core->tvnorm);
  904. if (V4L2_FIELD_ANY == field) {
  905. field = (f->fmt.pix.height > maxh/2)
  906. ? V4L2_FIELD_INTERLACED
  907. : V4L2_FIELD_BOTTOM;
  908. }
  909. switch (field) {
  910. case V4L2_FIELD_TOP:
  911. case V4L2_FIELD_BOTTOM:
  912. maxh = maxh / 2;
  913. break;
  914. case V4L2_FIELD_INTERLACED:
  915. break;
  916. default:
  917. return -EINVAL;
  918. }
  919. f->fmt.pix.field = field;
  920. if (f->fmt.pix.height < 32)
  921. f->fmt.pix.height = 32;
  922. if (f->fmt.pix.height > maxh)
  923. f->fmt.pix.height = maxh;
  924. if (f->fmt.pix.width < 48)
  925. f->fmt.pix.width = 48;
  926. if (f->fmt.pix.width > maxw)
  927. f->fmt.pix.width = maxw;
  928. f->fmt.pix.width &= ~0x03;
  929. f->fmt.pix.bytesperline =
  930. (f->fmt.pix.width * fmt->depth) >> 3;
  931. f->fmt.pix.sizeimage =
  932. f->fmt.pix.height * f->fmt.pix.bytesperline;
  933. return 0;
  934. }
  935. static int vidioc_s_fmt_cap (struct file *file, void *priv,
  936. struct v4l2_format *f)
  937. {
  938. struct cx8800_fh *fh = priv;
  939. int err = vidioc_try_fmt_cap (file,priv,f);
  940. if (0 != err)
  941. return err;
  942. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  943. fh->width = f->fmt.pix.width;
  944. fh->height = f->fmt.pix.height;
  945. fh->vidq.field = f->fmt.pix.field;
  946. return 0;
  947. }
  948. static int vidioc_querycap (struct file *file, void *priv,
  949. struct v4l2_capability *cap)
  950. {
  951. struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
  952. struct cx88_core *core = dev->core;
  953. strcpy(cap->driver, "cx8800");
  954. strlcpy(cap->card, cx88_boards[core->board].name,
  955. sizeof(cap->card));
  956. sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
  957. cap->version = CX88_VERSION_CODE;
  958. cap->capabilities =
  959. V4L2_CAP_VIDEO_CAPTURE |
  960. V4L2_CAP_READWRITE |
  961. V4L2_CAP_STREAMING |
  962. V4L2_CAP_VBI_CAPTURE;
  963. if (UNSET != core->tuner_type)
  964. cap->capabilities |= V4L2_CAP_TUNER;
  965. return 0;
  966. }
  967. static int vidioc_enum_fmt_cap (struct file *file, void *priv,
  968. struct v4l2_fmtdesc *f)
  969. {
  970. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  971. return -EINVAL;
  972. strlcpy(f->description,formats[f->index].name,sizeof(f->description));
  973. f->pixelformat = formats[f->index].fourcc;
  974. return 0;
  975. }
  976. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  977. static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
  978. {
  979. struct cx8800_fh *fh = priv;
  980. struct videobuf_queue *q;
  981. struct v4l2_requestbuffers req;
  982. unsigned int i;
  983. int err;
  984. q = get_queue(fh);
  985. memset(&req,0,sizeof(req));
  986. req.type = q->type;
  987. req.count = 8;
  988. req.memory = V4L2_MEMORY_MMAP;
  989. err = videobuf_reqbufs(q,&req);
  990. if (err < 0)
  991. return err;
  992. mbuf->frames = req.count;
  993. mbuf->size = 0;
  994. for (i = 0; i < mbuf->frames; i++) {
  995. mbuf->offsets[i] = q->bufs[i]->boff;
  996. mbuf->size += q->bufs[i]->bsize;
  997. }
  998. return 0;
  999. }
  1000. #endif
  1001. static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
  1002. {
  1003. struct cx8800_fh *fh = priv;
  1004. return (videobuf_reqbufs(get_queue(fh), p));
  1005. }
  1006. static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
  1007. {
  1008. struct cx8800_fh *fh = priv;
  1009. return (videobuf_querybuf(get_queue(fh), p));
  1010. }
  1011. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  1012. {
  1013. struct cx8800_fh *fh = priv;
  1014. return (videobuf_qbuf(get_queue(fh), p));
  1015. }
  1016. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  1017. {
  1018. struct cx8800_fh *fh = priv;
  1019. return (videobuf_dqbuf(get_queue(fh), p,
  1020. file->f_flags & O_NONBLOCK));
  1021. }
  1022. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  1023. {
  1024. struct cx8800_fh *fh = priv;
  1025. struct cx8800_dev *dev = fh->dev;
  1026. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  1027. return -EINVAL;
  1028. if (unlikely(i != fh->type))
  1029. return -EINVAL;
  1030. if (unlikely(!res_get(dev,fh,get_ressource(fh))))
  1031. return -EBUSY;
  1032. return videobuf_streamon(get_queue(fh));
  1033. }
  1034. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1035. {
  1036. struct cx8800_fh *fh = priv;
  1037. struct cx8800_dev *dev = fh->dev;
  1038. int err, res;
  1039. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1040. return -EINVAL;
  1041. if (i != fh->type)
  1042. return -EINVAL;
  1043. res = get_ressource(fh);
  1044. err = videobuf_streamoff(get_queue(fh));
  1045. if (err < 0)
  1046. return err;
  1047. res_free(dev,fh,res);
  1048. return 0;
  1049. }
  1050. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
  1051. {
  1052. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1053. mutex_lock(&core->lock);
  1054. cx88_set_tvnorm(core,*tvnorms);
  1055. mutex_unlock(&core->lock);
  1056. return 0;
  1057. }
  1058. /* only one input in this sample driver */
  1059. int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
  1060. {
  1061. static const char *iname[] = {
  1062. [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
  1063. [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
  1064. [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
  1065. [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
  1066. [ CX88_VMUX_SVIDEO ] = "S-Video",
  1067. [ CX88_VMUX_TELEVISION ] = "Television",
  1068. [ CX88_VMUX_CABLE ] = "Cable TV",
  1069. [ CX88_VMUX_DVB ] = "DVB",
  1070. [ CX88_VMUX_DEBUG ] = "for debug only",
  1071. };
  1072. unsigned int n;
  1073. n = i->index;
  1074. if (n >= 4)
  1075. return -EINVAL;
  1076. if (0 == INPUT(n)->type)
  1077. return -EINVAL;
  1078. memset(i,0,sizeof(*i));
  1079. i->index = n;
  1080. i->type = V4L2_INPUT_TYPE_CAMERA;
  1081. strcpy(i->name,iname[INPUT(n)->type]);
  1082. if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
  1083. (CX88_VMUX_CABLE == INPUT(n)->type))
  1084. i->type = V4L2_INPUT_TYPE_TUNER;
  1085. i->std = CX88_NORMS;
  1086. return 0;
  1087. }
  1088. EXPORT_SYMBOL(cx88_enum_input);
  1089. static int vidioc_enum_input (struct file *file, void *priv,
  1090. struct v4l2_input *i)
  1091. {
  1092. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1093. return cx88_enum_input (core,i);
  1094. }
  1095. static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
  1096. {
  1097. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1098. *i = core->input;
  1099. return 0;
  1100. }
  1101. static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
  1102. {
  1103. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1104. if (i >= 4)
  1105. return -EINVAL;
  1106. mutex_lock(&core->lock);
  1107. cx88_newstation(core);
  1108. cx88_video_mux(core,i);
  1109. mutex_unlock(&core->lock);
  1110. return 0;
  1111. }
  1112. static int vidioc_queryctrl (struct file *file, void *priv,
  1113. struct v4l2_queryctrl *qctrl)
  1114. {
  1115. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1116. if (unlikely(qctrl->id == 0))
  1117. return -EINVAL;
  1118. return cx8800_ctrl_query(qctrl);
  1119. }
  1120. static int vidioc_g_ctrl (struct file *file, void *priv,
  1121. struct v4l2_control *ctl)
  1122. {
  1123. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1124. return
  1125. cx88_get_control(core,ctl);
  1126. }
  1127. static int vidioc_s_ctrl (struct file *file, void *priv,
  1128. struct v4l2_control *ctl)
  1129. {
  1130. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1131. return
  1132. cx88_set_control(core,ctl);
  1133. }
  1134. static int vidioc_g_tuner (struct file *file, void *priv,
  1135. struct v4l2_tuner *t)
  1136. {
  1137. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1138. u32 reg;
  1139. if (unlikely(UNSET == core->tuner_type))
  1140. return -EINVAL;
  1141. if (0 != t->index)
  1142. return -EINVAL;
  1143. strcpy(t->name, "Television");
  1144. t->type = V4L2_TUNER_ANALOG_TV;
  1145. t->capability = V4L2_TUNER_CAP_NORM;
  1146. t->rangehigh = 0xffffffffUL;
  1147. cx88_get_stereo(core ,t);
  1148. reg = cx_read(MO_DEVICE_STATUS);
  1149. t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
  1150. return 0;
  1151. }
  1152. static int vidioc_s_tuner (struct file *file, void *priv,
  1153. struct v4l2_tuner *t)
  1154. {
  1155. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1156. if (UNSET == core->tuner_type)
  1157. return -EINVAL;
  1158. if (0 != t->index)
  1159. return -EINVAL;
  1160. cx88_set_stereo(core, t->audmode, 1);
  1161. return 0;
  1162. }
  1163. static int vidioc_g_frequency (struct file *file, void *priv,
  1164. struct v4l2_frequency *f)
  1165. {
  1166. struct cx8800_fh *fh = priv;
  1167. struct cx88_core *core = fh->dev->core;
  1168. if (unlikely(UNSET == core->tuner_type))
  1169. return -EINVAL;
  1170. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1171. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1172. f->frequency = core->freq;
  1173. cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
  1174. return 0;
  1175. }
  1176. int cx88_set_freq (struct cx88_core *core,
  1177. struct v4l2_frequency *f)
  1178. {
  1179. if (unlikely(UNSET == core->tuner_type))
  1180. return -EINVAL;
  1181. if (unlikely(f->tuner != 0))
  1182. return -EINVAL;
  1183. mutex_lock(&core->lock);
  1184. core->freq = f->frequency;
  1185. cx88_newstation(core);
  1186. cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
  1187. /* When changing channels it is required to reset TVAUDIO */
  1188. msleep (10);
  1189. cx88_set_tvaudio(core);
  1190. mutex_unlock(&core->lock);
  1191. return 0;
  1192. }
  1193. EXPORT_SYMBOL(cx88_set_freq);
  1194. static int vidioc_s_frequency (struct file *file, void *priv,
  1195. struct v4l2_frequency *f)
  1196. {
  1197. struct cx8800_fh *fh = priv;
  1198. struct cx88_core *core = fh->dev->core;
  1199. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1200. return -EINVAL;
  1201. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1202. return -EINVAL;
  1203. return
  1204. cx88_set_freq (core,f);
  1205. }
  1206. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1207. static int vidioc_g_register (struct file *file, void *fh,
  1208. struct v4l2_register *reg)
  1209. {
  1210. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1211. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1212. return -EINVAL;
  1213. /* cx2388x has a 24-bit register space */
  1214. reg->val = cx_read(reg->reg&0xffffff);
  1215. return 0;
  1216. }
  1217. static int vidioc_s_register (struct file *file, void *fh,
  1218. struct v4l2_register *reg)
  1219. {
  1220. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1221. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1222. return -EINVAL;
  1223. cx_write(reg->reg&0xffffff, reg->val);
  1224. return 0;
  1225. }
  1226. #endif
  1227. /* ----------------------------------------------------------- */
  1228. /* RADIO ESPECIFIC IOCTLS */
  1229. /* ----------------------------------------------------------- */
  1230. static int radio_querycap (struct file *file, void *priv,
  1231. struct v4l2_capability *cap)
  1232. {
  1233. struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
  1234. struct cx88_core *core = dev->core;
  1235. strcpy(cap->driver, "cx8800");
  1236. strlcpy(cap->card, cx88_boards[core->board].name,
  1237. sizeof(cap->card));
  1238. sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
  1239. cap->version = CX88_VERSION_CODE;
  1240. cap->capabilities = V4L2_CAP_TUNER;
  1241. return 0;
  1242. }
  1243. static int radio_g_tuner (struct file *file, void *priv,
  1244. struct v4l2_tuner *t)
  1245. {
  1246. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1247. if (unlikely(t->index > 0))
  1248. return -EINVAL;
  1249. strcpy(t->name, "Radio");
  1250. t->type = V4L2_TUNER_RADIO;
  1251. cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
  1252. return 0;
  1253. }
  1254. static int radio_enum_input (struct file *file, void *priv,
  1255. struct v4l2_input *i)
  1256. {
  1257. if (i->index != 0)
  1258. return -EINVAL;
  1259. strcpy(i->name,"Radio");
  1260. i->type = V4L2_INPUT_TYPE_TUNER;
  1261. return 0;
  1262. }
  1263. static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
  1264. {
  1265. if (unlikely(a->index))
  1266. return -EINVAL;
  1267. memset(a,0,sizeof(*a));
  1268. strcpy(a->name,"Radio");
  1269. return 0;
  1270. }
  1271. /* FIXME: Should add a standard for radio */
  1272. static int radio_s_tuner (struct file *file, void *priv,
  1273. struct v4l2_tuner *t)
  1274. {
  1275. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1276. if (0 != t->index)
  1277. return -EINVAL;
  1278. cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
  1279. return 0;
  1280. }
  1281. static int radio_s_audio (struct file *file, void *fh,
  1282. struct v4l2_audio *a)
  1283. {
  1284. return 0;
  1285. }
  1286. static int radio_s_input (struct file *file, void *fh, unsigned int i)
  1287. {
  1288. return 0;
  1289. }
  1290. static int radio_queryctrl (struct file *file, void *priv,
  1291. struct v4l2_queryctrl *c)
  1292. {
  1293. int i;
  1294. if (c->id < V4L2_CID_BASE ||
  1295. c->id >= V4L2_CID_LASTP1)
  1296. return -EINVAL;
  1297. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1298. for (i = 0; i < CX8800_CTLS; i++)
  1299. if (cx8800_ctls[i].v.id == c->id)
  1300. break;
  1301. *c = cx8800_ctls[i].v;
  1302. } else
  1303. *c = no_ctl;
  1304. return 0;
  1305. }
  1306. /* ----------------------------------------------------------- */
  1307. static void cx8800_vid_timeout(unsigned long data)
  1308. {
  1309. struct cx8800_dev *dev = (struct cx8800_dev*)data;
  1310. struct cx88_core *core = dev->core;
  1311. struct cx88_dmaqueue *q = &dev->vidq;
  1312. struct cx88_buffer *buf;
  1313. unsigned long flags;
  1314. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1315. cx_clear(MO_VID_DMACNTRL, 0x11);
  1316. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1317. spin_lock_irqsave(&dev->slock,flags);
  1318. while (!list_empty(&q->active)) {
  1319. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  1320. list_del(&buf->vb.queue);
  1321. buf->vb.state = STATE_ERROR;
  1322. wake_up(&buf->vb.done);
  1323. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
  1324. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  1325. }
  1326. restart_video_queue(dev,q);
  1327. spin_unlock_irqrestore(&dev->slock,flags);
  1328. }
  1329. static char *cx88_vid_irqs[32] = {
  1330. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  1331. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  1332. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  1333. "y_sync", "u_sync", "v_sync", "vbi_sync",
  1334. "opc_err", "par_err", "rip_err", "pci_abort",
  1335. };
  1336. static void cx8800_vid_irq(struct cx8800_dev *dev)
  1337. {
  1338. struct cx88_core *core = dev->core;
  1339. u32 status, mask, count;
  1340. status = cx_read(MO_VID_INTSTAT);
  1341. mask = cx_read(MO_VID_INTMSK);
  1342. if (0 == (status & mask))
  1343. return;
  1344. cx_write(MO_VID_INTSTAT, status);
  1345. if (irq_debug || (status & mask & ~0xff))
  1346. cx88_print_irqbits(core->name, "irq vid",
  1347. cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
  1348. status, mask);
  1349. /* risc op code error */
  1350. if (status & (1 << 16)) {
  1351. printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
  1352. cx_clear(MO_VID_DMACNTRL, 0x11);
  1353. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1354. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1355. }
  1356. /* risc1 y */
  1357. if (status & 0x01) {
  1358. spin_lock(&dev->slock);
  1359. count = cx_read(MO_VIDY_GPCNT);
  1360. cx88_wakeup(core, &dev->vidq, count);
  1361. spin_unlock(&dev->slock);
  1362. }
  1363. /* risc1 vbi */
  1364. if (status & 0x08) {
  1365. spin_lock(&dev->slock);
  1366. count = cx_read(MO_VBI_GPCNT);
  1367. cx88_wakeup(core, &dev->vbiq, count);
  1368. spin_unlock(&dev->slock);
  1369. }
  1370. /* risc2 y */
  1371. if (status & 0x10) {
  1372. dprintk(2,"stopper video\n");
  1373. spin_lock(&dev->slock);
  1374. restart_video_queue(dev,&dev->vidq);
  1375. spin_unlock(&dev->slock);
  1376. }
  1377. /* risc2 vbi */
  1378. if (status & 0x80) {
  1379. dprintk(2,"stopper vbi\n");
  1380. spin_lock(&dev->slock);
  1381. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1382. spin_unlock(&dev->slock);
  1383. }
  1384. }
  1385. static irqreturn_t cx8800_irq(int irq, void *dev_id)
  1386. {
  1387. struct cx8800_dev *dev = dev_id;
  1388. struct cx88_core *core = dev->core;
  1389. u32 status;
  1390. int loop, handled = 0;
  1391. for (loop = 0; loop < 10; loop++) {
  1392. status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
  1393. if (0 == status)
  1394. goto out;
  1395. cx_write(MO_PCI_INTSTAT, status);
  1396. handled = 1;
  1397. if (status & core->pci_irqmask)
  1398. cx88_core_irq(core,status);
  1399. if (status & 0x01)
  1400. cx8800_vid_irq(dev);
  1401. };
  1402. if (10 == loop) {
  1403. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  1404. core->name);
  1405. cx_write(MO_PCI_INTMSK,0);
  1406. }
  1407. out:
  1408. return IRQ_RETVAL(handled);
  1409. }
  1410. /* ----------------------------------------------------------- */
  1411. /* exported stuff */
  1412. static const struct file_operations video_fops =
  1413. {
  1414. .owner = THIS_MODULE,
  1415. .open = video_open,
  1416. .release = video_release,
  1417. .read = video_read,
  1418. .poll = video_poll,
  1419. .mmap = video_mmap,
  1420. .ioctl = video_ioctl2,
  1421. .compat_ioctl = v4l_compat_ioctl32,
  1422. .llseek = no_llseek,
  1423. };
  1424. static struct video_device cx8800_vbi_template;
  1425. static struct video_device cx8800_video_template =
  1426. {
  1427. .name = "cx8800-video",
  1428. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
  1429. .fops = &video_fops,
  1430. .minor = -1,
  1431. .vidioc_querycap = vidioc_querycap,
  1432. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  1433. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  1434. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  1435. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  1436. .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
  1437. .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
  1438. .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
  1439. .vidioc_reqbufs = vidioc_reqbufs,
  1440. .vidioc_querybuf = vidioc_querybuf,
  1441. .vidioc_qbuf = vidioc_qbuf,
  1442. .vidioc_dqbuf = vidioc_dqbuf,
  1443. .vidioc_s_std = vidioc_s_std,
  1444. .vidioc_enum_input = vidioc_enum_input,
  1445. .vidioc_g_input = vidioc_g_input,
  1446. .vidioc_s_input = vidioc_s_input,
  1447. .vidioc_queryctrl = vidioc_queryctrl,
  1448. .vidioc_g_ctrl = vidioc_g_ctrl,
  1449. .vidioc_s_ctrl = vidioc_s_ctrl,
  1450. .vidioc_streamon = vidioc_streamon,
  1451. .vidioc_streamoff = vidioc_streamoff,
  1452. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1453. .vidiocgmbuf = vidiocgmbuf,
  1454. #endif
  1455. .vidioc_g_tuner = vidioc_g_tuner,
  1456. .vidioc_s_tuner = vidioc_s_tuner,
  1457. .vidioc_g_frequency = vidioc_g_frequency,
  1458. .vidioc_s_frequency = vidioc_s_frequency,
  1459. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1460. .vidioc_g_register = vidioc_g_register,
  1461. .vidioc_s_register = vidioc_s_register,
  1462. #endif
  1463. .tvnorms = CX88_NORMS,
  1464. .current_norm = V4L2_STD_NTSC_M,
  1465. };
  1466. static const struct file_operations radio_fops =
  1467. {
  1468. .owner = THIS_MODULE,
  1469. .open = video_open,
  1470. .release = video_release,
  1471. .ioctl = video_ioctl2,
  1472. .compat_ioctl = v4l_compat_ioctl32,
  1473. .llseek = no_llseek,
  1474. };
  1475. static struct video_device cx8800_radio_template =
  1476. {
  1477. .name = "cx8800-radio",
  1478. .type = VID_TYPE_TUNER,
  1479. .hardware = 0,
  1480. .fops = &radio_fops,
  1481. .minor = -1,
  1482. .vidioc_querycap = radio_querycap,
  1483. .vidioc_g_tuner = radio_g_tuner,
  1484. .vidioc_enum_input = radio_enum_input,
  1485. .vidioc_g_audio = radio_g_audio,
  1486. .vidioc_s_tuner = radio_s_tuner,
  1487. .vidioc_s_audio = radio_s_audio,
  1488. .vidioc_s_input = radio_s_input,
  1489. .vidioc_queryctrl = radio_queryctrl,
  1490. .vidioc_g_ctrl = vidioc_g_ctrl,
  1491. .vidioc_s_ctrl = vidioc_s_ctrl,
  1492. .vidioc_g_frequency = vidioc_g_frequency,
  1493. .vidioc_s_frequency = vidioc_s_frequency,
  1494. };
  1495. /* ----------------------------------------------------------- */
  1496. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1497. {
  1498. if (dev->radio_dev) {
  1499. if (-1 != dev->radio_dev->minor)
  1500. video_unregister_device(dev->radio_dev);
  1501. else
  1502. video_device_release(dev->radio_dev);
  1503. dev->radio_dev = NULL;
  1504. }
  1505. if (dev->vbi_dev) {
  1506. if (-1 != dev->vbi_dev->minor)
  1507. video_unregister_device(dev->vbi_dev);
  1508. else
  1509. video_device_release(dev->vbi_dev);
  1510. dev->vbi_dev = NULL;
  1511. }
  1512. if (dev->video_dev) {
  1513. if (-1 != dev->video_dev->minor)
  1514. video_unregister_device(dev->video_dev);
  1515. else
  1516. video_device_release(dev->video_dev);
  1517. dev->video_dev = NULL;
  1518. }
  1519. }
  1520. static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
  1521. const struct pci_device_id *pci_id)
  1522. {
  1523. struct cx8800_dev *dev;
  1524. struct cx88_core *core;
  1525. int err;
  1526. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1527. if (NULL == dev)
  1528. return -ENOMEM;
  1529. /* pci init */
  1530. dev->pci = pci_dev;
  1531. if (pci_enable_device(pci_dev)) {
  1532. err = -EIO;
  1533. goto fail_free;
  1534. }
  1535. core = cx88_core_get(dev->pci);
  1536. if (NULL == core) {
  1537. err = -EINVAL;
  1538. goto fail_free;
  1539. }
  1540. dev->core = core;
  1541. /* print pci info */
  1542. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  1543. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1544. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  1545. "latency: %d, mmio: 0x%llx\n", core->name,
  1546. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1547. dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
  1548. pci_set_master(pci_dev);
  1549. if (!pci_dma_supported(pci_dev,0xffffffff)) {
  1550. printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
  1551. err = -EIO;
  1552. goto fail_core;
  1553. }
  1554. /* Initialize VBI template */
  1555. memcpy( &cx8800_vbi_template, &cx8800_video_template,
  1556. sizeof(cx8800_vbi_template) );
  1557. strcpy(cx8800_vbi_template.name,"cx8800-vbi");
  1558. cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
  1559. /* initialize driver struct */
  1560. spin_lock_init(&dev->slock);
  1561. core->tvnorm = cx8800_video_template.current_norm;
  1562. /* init video dma queues */
  1563. INIT_LIST_HEAD(&dev->vidq.active);
  1564. INIT_LIST_HEAD(&dev->vidq.queued);
  1565. dev->vidq.timeout.function = cx8800_vid_timeout;
  1566. dev->vidq.timeout.data = (unsigned long)dev;
  1567. init_timer(&dev->vidq.timeout);
  1568. cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
  1569. MO_VID_DMACNTRL,0x11,0x00);
  1570. /* init vbi dma queues */
  1571. INIT_LIST_HEAD(&dev->vbiq.active);
  1572. INIT_LIST_HEAD(&dev->vbiq.queued);
  1573. dev->vbiq.timeout.function = cx8800_vbi_timeout;
  1574. dev->vbiq.timeout.data = (unsigned long)dev;
  1575. init_timer(&dev->vbiq.timeout);
  1576. cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
  1577. MO_VID_DMACNTRL,0x88,0x00);
  1578. /* get irq */
  1579. err = request_irq(pci_dev->irq, cx8800_irq,
  1580. IRQF_SHARED | IRQF_DISABLED, core->name, dev);
  1581. if (err < 0) {
  1582. printk(KERN_ERR "%s: can't get IRQ %d\n",
  1583. core->name,pci_dev->irq);
  1584. goto fail_core;
  1585. }
  1586. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1587. /* load and configure helper modules */
  1588. if (TUNER_ABSENT != core->tuner_type)
  1589. request_module("tuner");
  1590. if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
  1591. request_module("wm8775");
  1592. /* register v4l devices */
  1593. dev->video_dev = cx88_vdev_init(core,dev->pci,
  1594. &cx8800_video_template,"video");
  1595. err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
  1596. video_nr[core->nr]);
  1597. if (err < 0) {
  1598. printk(KERN_INFO "%s: can't register video device\n",
  1599. core->name);
  1600. goto fail_unreg;
  1601. }
  1602. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1603. core->name,dev->video_dev->minor & 0x1f);
  1604. dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
  1605. err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
  1606. vbi_nr[core->nr]);
  1607. if (err < 0) {
  1608. printk(KERN_INFO "%s/0: can't register vbi device\n",
  1609. core->name);
  1610. goto fail_unreg;
  1611. }
  1612. printk(KERN_INFO "%s/0: registered device vbi%d\n",
  1613. core->name,dev->vbi_dev->minor & 0x1f);
  1614. if (core->has_radio) {
  1615. dev->radio_dev = cx88_vdev_init(core,dev->pci,
  1616. &cx8800_radio_template,"radio");
  1617. err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
  1618. radio_nr[core->nr]);
  1619. if (err < 0) {
  1620. printk(KERN_INFO "%s/0: can't register radio device\n",
  1621. core->name);
  1622. goto fail_unreg;
  1623. }
  1624. printk(KERN_INFO "%s/0: registered device radio%d\n",
  1625. core->name,dev->radio_dev->minor & 0x1f);
  1626. }
  1627. /* everything worked */
  1628. list_add_tail(&dev->devlist,&cx8800_devlist);
  1629. pci_set_drvdata(pci_dev,dev);
  1630. /* initial device configuration */
  1631. mutex_lock(&core->lock);
  1632. cx88_set_tvnorm(core,core->tvnorm);
  1633. init_controls(core);
  1634. cx88_video_mux(core,0);
  1635. mutex_unlock(&core->lock);
  1636. /* start tvaudio thread */
  1637. if (core->tuner_type != TUNER_ABSENT)
  1638. core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
  1639. return 0;
  1640. fail_unreg:
  1641. cx8800_unregister_video(dev);
  1642. free_irq(pci_dev->irq, dev);
  1643. fail_core:
  1644. cx88_core_put(core,dev->pci);
  1645. fail_free:
  1646. kfree(dev);
  1647. return err;
  1648. }
  1649. static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
  1650. {
  1651. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1652. struct cx88_core *core = dev->core;
  1653. /* stop thread */
  1654. if (core->kthread) {
  1655. kthread_stop(core->kthread);
  1656. core->kthread = NULL;
  1657. }
  1658. cx88_shutdown(core); /* FIXME */
  1659. pci_disable_device(pci_dev);
  1660. /* unregister stuff */
  1661. free_irq(pci_dev->irq, dev);
  1662. cx8800_unregister_video(dev);
  1663. pci_set_drvdata(pci_dev, NULL);
  1664. /* free memory */
  1665. btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
  1666. list_del(&dev->devlist);
  1667. cx88_core_put(core,dev->pci);
  1668. kfree(dev);
  1669. }
  1670. #ifdef CONFIG_PM
  1671. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1672. {
  1673. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1674. struct cx88_core *core = dev->core;
  1675. /* stop video+vbi capture */
  1676. spin_lock(&dev->slock);
  1677. if (!list_empty(&dev->vidq.active)) {
  1678. printk("%s: suspend video\n", core->name);
  1679. stop_video_dma(dev);
  1680. del_timer(&dev->vidq.timeout);
  1681. }
  1682. if (!list_empty(&dev->vbiq.active)) {
  1683. printk("%s: suspend vbi\n", core->name);
  1684. cx8800_stop_vbi_dma(dev);
  1685. del_timer(&dev->vbiq.timeout);
  1686. }
  1687. spin_unlock(&dev->slock);
  1688. /* FIXME -- shutdown device */
  1689. cx88_shutdown(core);
  1690. pci_save_state(pci_dev);
  1691. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  1692. pci_disable_device(pci_dev);
  1693. dev->state.disabled = 1;
  1694. }
  1695. return 0;
  1696. }
  1697. static int cx8800_resume(struct pci_dev *pci_dev)
  1698. {
  1699. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1700. struct cx88_core *core = dev->core;
  1701. int err;
  1702. if (dev->state.disabled) {
  1703. err=pci_enable_device(pci_dev);
  1704. if (err) {
  1705. printk(KERN_ERR "%s: can't enable device\n",
  1706. core->name);
  1707. return err;
  1708. }
  1709. dev->state.disabled = 0;
  1710. }
  1711. err= pci_set_power_state(pci_dev, PCI_D0);
  1712. if (err) {
  1713. printk(KERN_ERR "%s: can't enable device\n",
  1714. core->name);
  1715. pci_disable_device(pci_dev);
  1716. dev->state.disabled = 1;
  1717. return err;
  1718. }
  1719. pci_restore_state(pci_dev);
  1720. /* FIXME: re-initialize hardware */
  1721. cx88_reset(core);
  1722. /* restart video+vbi capture */
  1723. spin_lock(&dev->slock);
  1724. if (!list_empty(&dev->vidq.active)) {
  1725. printk("%s: resume video\n", core->name);
  1726. restart_video_queue(dev,&dev->vidq);
  1727. }
  1728. if (!list_empty(&dev->vbiq.active)) {
  1729. printk("%s: resume vbi\n", core->name);
  1730. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1731. }
  1732. spin_unlock(&dev->slock);
  1733. return 0;
  1734. }
  1735. #endif
  1736. /* ----------------------------------------------------------- */
  1737. static struct pci_device_id cx8800_pci_tbl[] = {
  1738. {
  1739. .vendor = 0x14f1,
  1740. .device = 0x8800,
  1741. .subvendor = PCI_ANY_ID,
  1742. .subdevice = PCI_ANY_ID,
  1743. },{
  1744. /* --- end of list --- */
  1745. }
  1746. };
  1747. MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
  1748. static struct pci_driver cx8800_pci_driver = {
  1749. .name = "cx8800",
  1750. .id_table = cx8800_pci_tbl,
  1751. .probe = cx8800_initdev,
  1752. .remove = __devexit_p(cx8800_finidev),
  1753. #ifdef CONFIG_PM
  1754. .suspend = cx8800_suspend,
  1755. .resume = cx8800_resume,
  1756. #endif
  1757. };
  1758. static int cx8800_init(void)
  1759. {
  1760. printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
  1761. (CX88_VERSION_CODE >> 16) & 0xff,
  1762. (CX88_VERSION_CODE >> 8) & 0xff,
  1763. CX88_VERSION_CODE & 0xff);
  1764. #ifdef SNAPSHOT
  1765. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  1766. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  1767. #endif
  1768. return pci_register_driver(&cx8800_pci_driver);
  1769. }
  1770. static void cx8800_fini(void)
  1771. {
  1772. pci_unregister_driver(&cx8800_pci_driver);
  1773. }
  1774. module_init(cx8800_init);
  1775. module_exit(cx8800_fini);
  1776. /* ----------------------------------------------------------- */
  1777. /*
  1778. * Local variables:
  1779. * c-basic-offset: 8
  1780. * End:
  1781. * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
  1782. */