cx88-video.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  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/kmod.h>
  31. #include <linux/kernel.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/dma-mapping.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 (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 | PCI_INT_VIDINT);
  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, PCI_INT_VIDINT);
  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. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  480. int rc, init_buffer = 0;
  481. BUG_ON(NULL == fh->fmt);
  482. if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
  483. fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
  484. return -EINVAL;
  485. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  486. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  487. return -EINVAL;
  488. if (buf->fmt != fh->fmt ||
  489. buf->vb.width != fh->width ||
  490. buf->vb.height != fh->height ||
  491. buf->vb.field != field) {
  492. buf->fmt = fh->fmt;
  493. buf->vb.width = fh->width;
  494. buf->vb.height = fh->height;
  495. buf->vb.field = field;
  496. init_buffer = 1;
  497. }
  498. if (STATE_NEEDS_INIT == buf->vb.state) {
  499. init_buffer = 1;
  500. if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
  501. goto fail;
  502. }
  503. if (init_buffer) {
  504. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  505. switch (buf->vb.field) {
  506. case V4L2_FIELD_TOP:
  507. cx88_risc_buffer(dev->pci, &buf->risc,
  508. dma->sglist, 0, UNSET,
  509. buf->bpl, 0, buf->vb.height);
  510. break;
  511. case V4L2_FIELD_BOTTOM:
  512. cx88_risc_buffer(dev->pci, &buf->risc,
  513. dma->sglist, UNSET, 0,
  514. buf->bpl, 0, buf->vb.height);
  515. break;
  516. case V4L2_FIELD_INTERLACED:
  517. cx88_risc_buffer(dev->pci, &buf->risc,
  518. dma->sglist, 0, buf->bpl,
  519. buf->bpl, buf->bpl,
  520. buf->vb.height >> 1);
  521. break;
  522. case V4L2_FIELD_SEQ_TB:
  523. cx88_risc_buffer(dev->pci, &buf->risc,
  524. dma->sglist,
  525. 0, buf->bpl * (buf->vb.height >> 1),
  526. buf->bpl, 0,
  527. buf->vb.height >> 1);
  528. break;
  529. case V4L2_FIELD_SEQ_BT:
  530. cx88_risc_buffer(dev->pci, &buf->risc,
  531. dma->sglist,
  532. buf->bpl * (buf->vb.height >> 1), 0,
  533. buf->bpl, 0,
  534. buf->vb.height >> 1);
  535. break;
  536. default:
  537. BUG();
  538. }
  539. }
  540. dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  541. buf, buf->vb.i,
  542. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  543. (unsigned long)buf->risc.dma);
  544. buf->vb.state = STATE_PREPARED;
  545. return 0;
  546. fail:
  547. cx88_free_buffer(q,buf);
  548. return rc;
  549. }
  550. static void
  551. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  552. {
  553. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  554. struct cx88_buffer *prev;
  555. struct cx8800_fh *fh = vq->priv_data;
  556. struct cx8800_dev *dev = fh->dev;
  557. struct cx88_core *core = dev->core;
  558. struct cx88_dmaqueue *q = &dev->vidq;
  559. /* add jump to stopper */
  560. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  561. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  562. if (!list_empty(&q->queued)) {
  563. list_add_tail(&buf->vb.queue,&q->queued);
  564. buf->vb.state = STATE_QUEUED;
  565. dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
  566. buf, buf->vb.i);
  567. } else if (list_empty(&q->active)) {
  568. list_add_tail(&buf->vb.queue,&q->active);
  569. start_video_dma(dev, q, buf);
  570. buf->vb.state = STATE_ACTIVE;
  571. buf->count = q->count++;
  572. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  573. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  574. buf, buf->vb.i);
  575. } else {
  576. prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
  577. if (prev->vb.width == buf->vb.width &&
  578. prev->vb.height == buf->vb.height &&
  579. prev->fmt == buf->fmt) {
  580. list_add_tail(&buf->vb.queue,&q->active);
  581. buf->vb.state = STATE_ACTIVE;
  582. buf->count = q->count++;
  583. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  584. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  585. buf, buf->vb.i);
  586. } else {
  587. list_add_tail(&buf->vb.queue,&q->queued);
  588. buf->vb.state = STATE_QUEUED;
  589. dprintk(2,"[%p/%d] buffer_queue - first queued\n",
  590. buf, buf->vb.i);
  591. }
  592. }
  593. }
  594. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  595. {
  596. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  597. cx88_free_buffer(q,buf);
  598. }
  599. static struct videobuf_queue_ops cx8800_video_qops = {
  600. .buf_setup = buffer_setup,
  601. .buf_prepare = buffer_prepare,
  602. .buf_queue = buffer_queue,
  603. .buf_release = buffer_release,
  604. };
  605. /* ------------------------------------------------------------------ */
  606. /* ------------------------------------------------------------------ */
  607. static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
  608. {
  609. switch (fh->type) {
  610. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  611. return &fh->vidq;
  612. case V4L2_BUF_TYPE_VBI_CAPTURE:
  613. return &fh->vbiq;
  614. default:
  615. BUG();
  616. return NULL;
  617. }
  618. }
  619. static int get_ressource(struct cx8800_fh *fh)
  620. {
  621. switch (fh->type) {
  622. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  623. return RESOURCE_VIDEO;
  624. case V4L2_BUF_TYPE_VBI_CAPTURE:
  625. return RESOURCE_VBI;
  626. default:
  627. BUG();
  628. return 0;
  629. }
  630. }
  631. static int video_open(struct inode *inode, struct file *file)
  632. {
  633. int minor = iminor(inode);
  634. struct cx8800_dev *h,*dev = NULL;
  635. struct cx88_core *core;
  636. struct cx8800_fh *fh;
  637. struct list_head *list;
  638. enum v4l2_buf_type type = 0;
  639. int radio = 0;
  640. list_for_each(list,&cx8800_devlist) {
  641. h = list_entry(list, struct cx8800_dev, devlist);
  642. if (h->video_dev->minor == minor) {
  643. dev = h;
  644. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  645. }
  646. if (h->vbi_dev->minor == minor) {
  647. dev = h;
  648. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  649. }
  650. if (h->radio_dev &&
  651. h->radio_dev->minor == minor) {
  652. radio = 1;
  653. dev = h;
  654. }
  655. }
  656. if (NULL == dev)
  657. return -ENODEV;
  658. core = dev->core;
  659. dprintk(1,"open minor=%d radio=%d type=%s\n",
  660. minor,radio,v4l2_type_names[type]);
  661. /* allocate + initialize per filehandle data */
  662. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  663. if (NULL == fh)
  664. return -ENOMEM;
  665. file->private_data = fh;
  666. fh->dev = dev;
  667. fh->radio = radio;
  668. fh->type = type;
  669. fh->width = 320;
  670. fh->height = 240;
  671. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  672. videobuf_queue_pci_init(&fh->vidq, &cx8800_video_qops,
  673. dev->pci, &dev->slock,
  674. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  675. V4L2_FIELD_INTERLACED,
  676. sizeof(struct cx88_buffer),
  677. fh);
  678. videobuf_queue_pci_init(&fh->vbiq, &cx8800_vbi_qops,
  679. dev->pci, &dev->slock,
  680. V4L2_BUF_TYPE_VBI_CAPTURE,
  681. V4L2_FIELD_SEQ_TB,
  682. sizeof(struct cx88_buffer),
  683. fh);
  684. if (fh->radio) {
  685. dprintk(1,"video_open: setting radio device\n");
  686. cx_write(MO_GP3_IO, core->board.radio.gpio3);
  687. cx_write(MO_GP0_IO, core->board.radio.gpio0);
  688. cx_write(MO_GP1_IO, core->board.radio.gpio1);
  689. cx_write(MO_GP2_IO, core->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, core->board.name, sizeof(cap->card));
  955. sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
  956. cap->version = CX88_VERSION_CODE;
  957. cap->capabilities =
  958. V4L2_CAP_VIDEO_CAPTURE |
  959. V4L2_CAP_READWRITE |
  960. V4L2_CAP_STREAMING |
  961. V4L2_CAP_VBI_CAPTURE;
  962. if (UNSET != core->board.tuner_type)
  963. cap->capabilities |= V4L2_CAP_TUNER;
  964. return 0;
  965. }
  966. static int vidioc_enum_fmt_cap (struct file *file, void *priv,
  967. struct v4l2_fmtdesc *f)
  968. {
  969. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  970. return -EINVAL;
  971. strlcpy(f->description,formats[f->index].name,sizeof(f->description));
  972. f->pixelformat = formats[f->index].fourcc;
  973. return 0;
  974. }
  975. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  976. static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
  977. {
  978. struct cx8800_fh *fh = priv;
  979. return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
  980. }
  981. #endif
  982. static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
  983. {
  984. struct cx8800_fh *fh = priv;
  985. return (videobuf_reqbufs(get_queue(fh), p));
  986. }
  987. static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
  988. {
  989. struct cx8800_fh *fh = priv;
  990. return (videobuf_querybuf(get_queue(fh), p));
  991. }
  992. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  993. {
  994. struct cx8800_fh *fh = priv;
  995. return (videobuf_qbuf(get_queue(fh), p));
  996. }
  997. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  998. {
  999. struct cx8800_fh *fh = priv;
  1000. return (videobuf_dqbuf(get_queue(fh), p,
  1001. file->f_flags & O_NONBLOCK));
  1002. }
  1003. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  1004. {
  1005. struct cx8800_fh *fh = priv;
  1006. struct cx8800_dev *dev = fh->dev;
  1007. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  1008. return -EINVAL;
  1009. if (unlikely(i != fh->type))
  1010. return -EINVAL;
  1011. if (unlikely(!res_get(dev,fh,get_ressource(fh))))
  1012. return -EBUSY;
  1013. return videobuf_streamon(get_queue(fh));
  1014. }
  1015. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1016. {
  1017. struct cx8800_fh *fh = priv;
  1018. struct cx8800_dev *dev = fh->dev;
  1019. int err, res;
  1020. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1021. return -EINVAL;
  1022. if (i != fh->type)
  1023. return -EINVAL;
  1024. res = get_ressource(fh);
  1025. err = videobuf_streamoff(get_queue(fh));
  1026. if (err < 0)
  1027. return err;
  1028. res_free(dev,fh,res);
  1029. return 0;
  1030. }
  1031. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
  1032. {
  1033. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1034. mutex_lock(&core->lock);
  1035. cx88_set_tvnorm(core,*tvnorms);
  1036. mutex_unlock(&core->lock);
  1037. return 0;
  1038. }
  1039. /* only one input in this sample driver */
  1040. int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
  1041. {
  1042. static const char *iname[] = {
  1043. [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
  1044. [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
  1045. [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
  1046. [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
  1047. [ CX88_VMUX_SVIDEO ] = "S-Video",
  1048. [ CX88_VMUX_TELEVISION ] = "Television",
  1049. [ CX88_VMUX_CABLE ] = "Cable TV",
  1050. [ CX88_VMUX_DVB ] = "DVB",
  1051. [ CX88_VMUX_DEBUG ] = "for debug only",
  1052. };
  1053. unsigned int n;
  1054. n = i->index;
  1055. if (n >= 4)
  1056. return -EINVAL;
  1057. if (0 == INPUT(n).type)
  1058. return -EINVAL;
  1059. memset(i,0,sizeof(*i));
  1060. i->index = n;
  1061. i->type = V4L2_INPUT_TYPE_CAMERA;
  1062. strcpy(i->name,iname[INPUT(n).type]);
  1063. if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
  1064. (CX88_VMUX_CABLE == INPUT(n).type))
  1065. i->type = V4L2_INPUT_TYPE_TUNER;
  1066. i->std = CX88_NORMS;
  1067. return 0;
  1068. }
  1069. EXPORT_SYMBOL(cx88_enum_input);
  1070. static int vidioc_enum_input (struct file *file, void *priv,
  1071. struct v4l2_input *i)
  1072. {
  1073. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1074. return cx88_enum_input (core,i);
  1075. }
  1076. static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
  1077. {
  1078. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1079. *i = core->input;
  1080. return 0;
  1081. }
  1082. static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
  1083. {
  1084. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1085. if (i >= 4)
  1086. return -EINVAL;
  1087. mutex_lock(&core->lock);
  1088. cx88_newstation(core);
  1089. cx88_video_mux(core,i);
  1090. mutex_unlock(&core->lock);
  1091. return 0;
  1092. }
  1093. static int vidioc_queryctrl (struct file *file, void *priv,
  1094. struct v4l2_queryctrl *qctrl)
  1095. {
  1096. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1097. if (unlikely(qctrl->id == 0))
  1098. return -EINVAL;
  1099. return cx8800_ctrl_query(qctrl);
  1100. }
  1101. static int vidioc_g_ctrl (struct file *file, void *priv,
  1102. struct v4l2_control *ctl)
  1103. {
  1104. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1105. return
  1106. cx88_get_control(core,ctl);
  1107. }
  1108. static int vidioc_s_ctrl (struct file *file, void *priv,
  1109. struct v4l2_control *ctl)
  1110. {
  1111. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1112. return
  1113. cx88_set_control(core,ctl);
  1114. }
  1115. static int vidioc_g_tuner (struct file *file, void *priv,
  1116. struct v4l2_tuner *t)
  1117. {
  1118. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1119. u32 reg;
  1120. if (unlikely(UNSET == core->board.tuner_type))
  1121. return -EINVAL;
  1122. if (0 != t->index)
  1123. return -EINVAL;
  1124. strcpy(t->name, "Television");
  1125. t->type = V4L2_TUNER_ANALOG_TV;
  1126. t->capability = V4L2_TUNER_CAP_NORM;
  1127. t->rangehigh = 0xffffffffUL;
  1128. cx88_get_stereo(core ,t);
  1129. reg = cx_read(MO_DEVICE_STATUS);
  1130. t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
  1131. return 0;
  1132. }
  1133. static int vidioc_s_tuner (struct file *file, void *priv,
  1134. struct v4l2_tuner *t)
  1135. {
  1136. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1137. if (UNSET == core->board.tuner_type)
  1138. return -EINVAL;
  1139. if (0 != t->index)
  1140. return -EINVAL;
  1141. cx88_set_stereo(core, t->audmode, 1);
  1142. return 0;
  1143. }
  1144. static int vidioc_g_frequency (struct file *file, void *priv,
  1145. struct v4l2_frequency *f)
  1146. {
  1147. struct cx8800_fh *fh = priv;
  1148. struct cx88_core *core = fh->dev->core;
  1149. if (unlikely(UNSET == core->board.tuner_type))
  1150. return -EINVAL;
  1151. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1152. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1153. f->frequency = core->freq;
  1154. cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
  1155. return 0;
  1156. }
  1157. int cx88_set_freq (struct cx88_core *core,
  1158. struct v4l2_frequency *f)
  1159. {
  1160. if (unlikely(UNSET == core->board.tuner_type))
  1161. return -EINVAL;
  1162. if (unlikely(f->tuner != 0))
  1163. return -EINVAL;
  1164. mutex_lock(&core->lock);
  1165. core->freq = f->frequency;
  1166. cx88_newstation(core);
  1167. cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
  1168. /* When changing channels it is required to reset TVAUDIO */
  1169. msleep (10);
  1170. cx88_set_tvaudio(core);
  1171. mutex_unlock(&core->lock);
  1172. return 0;
  1173. }
  1174. EXPORT_SYMBOL(cx88_set_freq);
  1175. static int vidioc_s_frequency (struct file *file, void *priv,
  1176. struct v4l2_frequency *f)
  1177. {
  1178. struct cx8800_fh *fh = priv;
  1179. struct cx88_core *core = fh->dev->core;
  1180. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1181. return -EINVAL;
  1182. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1183. return -EINVAL;
  1184. return
  1185. cx88_set_freq (core,f);
  1186. }
  1187. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1188. static int vidioc_g_register (struct file *file, void *fh,
  1189. struct v4l2_register *reg)
  1190. {
  1191. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1192. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1193. return -EINVAL;
  1194. /* cx2388x has a 24-bit register space */
  1195. reg->val = cx_read(reg->reg&0xffffff);
  1196. return 0;
  1197. }
  1198. static int vidioc_s_register (struct file *file, void *fh,
  1199. struct v4l2_register *reg)
  1200. {
  1201. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1202. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1203. return -EINVAL;
  1204. cx_write(reg->reg&0xffffff, reg->val);
  1205. return 0;
  1206. }
  1207. #endif
  1208. /* ----------------------------------------------------------- */
  1209. /* RADIO ESPECIFIC IOCTLS */
  1210. /* ----------------------------------------------------------- */
  1211. static int radio_querycap (struct file *file, void *priv,
  1212. struct v4l2_capability *cap)
  1213. {
  1214. struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
  1215. struct cx88_core *core = dev->core;
  1216. strcpy(cap->driver, "cx8800");
  1217. strlcpy(cap->card, core->board.name, sizeof(cap->card));
  1218. sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
  1219. cap->version = CX88_VERSION_CODE;
  1220. cap->capabilities = V4L2_CAP_TUNER;
  1221. return 0;
  1222. }
  1223. static int radio_g_tuner (struct file *file, void *priv,
  1224. struct v4l2_tuner *t)
  1225. {
  1226. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1227. if (unlikely(t->index > 0))
  1228. return -EINVAL;
  1229. strcpy(t->name, "Radio");
  1230. t->type = V4L2_TUNER_RADIO;
  1231. cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
  1232. return 0;
  1233. }
  1234. static int radio_enum_input (struct file *file, void *priv,
  1235. struct v4l2_input *i)
  1236. {
  1237. if (i->index != 0)
  1238. return -EINVAL;
  1239. strcpy(i->name,"Radio");
  1240. i->type = V4L2_INPUT_TYPE_TUNER;
  1241. return 0;
  1242. }
  1243. static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
  1244. {
  1245. if (unlikely(a->index))
  1246. return -EINVAL;
  1247. memset(a,0,sizeof(*a));
  1248. strcpy(a->name,"Radio");
  1249. return 0;
  1250. }
  1251. /* FIXME: Should add a standard for radio */
  1252. static int radio_s_tuner (struct file *file, void *priv,
  1253. struct v4l2_tuner *t)
  1254. {
  1255. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1256. if (0 != t->index)
  1257. return -EINVAL;
  1258. cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
  1259. return 0;
  1260. }
  1261. static int radio_s_audio (struct file *file, void *fh,
  1262. struct v4l2_audio *a)
  1263. {
  1264. return 0;
  1265. }
  1266. static int radio_s_input (struct file *file, void *fh, unsigned int i)
  1267. {
  1268. return 0;
  1269. }
  1270. static int radio_queryctrl (struct file *file, void *priv,
  1271. struct v4l2_queryctrl *c)
  1272. {
  1273. int i;
  1274. if (c->id < V4L2_CID_BASE ||
  1275. c->id >= V4L2_CID_LASTP1)
  1276. return -EINVAL;
  1277. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1278. for (i = 0; i < CX8800_CTLS; i++)
  1279. if (cx8800_ctls[i].v.id == c->id)
  1280. break;
  1281. *c = cx8800_ctls[i].v;
  1282. } else
  1283. *c = no_ctl;
  1284. return 0;
  1285. }
  1286. /* ----------------------------------------------------------- */
  1287. static void cx8800_vid_timeout(unsigned long data)
  1288. {
  1289. struct cx8800_dev *dev = (struct cx8800_dev*)data;
  1290. struct cx88_core *core = dev->core;
  1291. struct cx88_dmaqueue *q = &dev->vidq;
  1292. struct cx88_buffer *buf;
  1293. unsigned long flags;
  1294. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1295. cx_clear(MO_VID_DMACNTRL, 0x11);
  1296. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1297. spin_lock_irqsave(&dev->slock,flags);
  1298. while (!list_empty(&q->active)) {
  1299. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  1300. list_del(&buf->vb.queue);
  1301. buf->vb.state = STATE_ERROR;
  1302. wake_up(&buf->vb.done);
  1303. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
  1304. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  1305. }
  1306. restart_video_queue(dev,q);
  1307. spin_unlock_irqrestore(&dev->slock,flags);
  1308. }
  1309. static char *cx88_vid_irqs[32] = {
  1310. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  1311. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  1312. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  1313. "y_sync", "u_sync", "v_sync", "vbi_sync",
  1314. "opc_err", "par_err", "rip_err", "pci_abort",
  1315. };
  1316. static void cx8800_vid_irq(struct cx8800_dev *dev)
  1317. {
  1318. struct cx88_core *core = dev->core;
  1319. u32 status, mask, count;
  1320. status = cx_read(MO_VID_INTSTAT);
  1321. mask = cx_read(MO_VID_INTMSK);
  1322. if (0 == (status & mask))
  1323. return;
  1324. cx_write(MO_VID_INTSTAT, status);
  1325. if (irq_debug || (status & mask & ~0xff))
  1326. cx88_print_irqbits(core->name, "irq vid",
  1327. cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
  1328. status, mask);
  1329. /* risc op code error */
  1330. if (status & (1 << 16)) {
  1331. printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
  1332. cx_clear(MO_VID_DMACNTRL, 0x11);
  1333. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1334. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1335. }
  1336. /* risc1 y */
  1337. if (status & 0x01) {
  1338. spin_lock(&dev->slock);
  1339. count = cx_read(MO_VIDY_GPCNT);
  1340. cx88_wakeup(core, &dev->vidq, count);
  1341. spin_unlock(&dev->slock);
  1342. }
  1343. /* risc1 vbi */
  1344. if (status & 0x08) {
  1345. spin_lock(&dev->slock);
  1346. count = cx_read(MO_VBI_GPCNT);
  1347. cx88_wakeup(core, &dev->vbiq, count);
  1348. spin_unlock(&dev->slock);
  1349. }
  1350. /* risc2 y */
  1351. if (status & 0x10) {
  1352. dprintk(2,"stopper video\n");
  1353. spin_lock(&dev->slock);
  1354. restart_video_queue(dev,&dev->vidq);
  1355. spin_unlock(&dev->slock);
  1356. }
  1357. /* risc2 vbi */
  1358. if (status & 0x80) {
  1359. dprintk(2,"stopper vbi\n");
  1360. spin_lock(&dev->slock);
  1361. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1362. spin_unlock(&dev->slock);
  1363. }
  1364. }
  1365. static irqreturn_t cx8800_irq(int irq, void *dev_id)
  1366. {
  1367. struct cx8800_dev *dev = dev_id;
  1368. struct cx88_core *core = dev->core;
  1369. u32 status;
  1370. int loop, handled = 0;
  1371. for (loop = 0; loop < 10; loop++) {
  1372. status = cx_read(MO_PCI_INTSTAT) &
  1373. (core->pci_irqmask | PCI_INT_VIDINT);
  1374. if (0 == status)
  1375. goto out;
  1376. cx_write(MO_PCI_INTSTAT, status);
  1377. handled = 1;
  1378. if (status & core->pci_irqmask)
  1379. cx88_core_irq(core,status);
  1380. if (status & PCI_INT_VIDINT)
  1381. cx8800_vid_irq(dev);
  1382. };
  1383. if (10 == loop) {
  1384. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  1385. core->name);
  1386. cx_write(MO_PCI_INTMSK,0);
  1387. }
  1388. out:
  1389. return IRQ_RETVAL(handled);
  1390. }
  1391. /* ----------------------------------------------------------- */
  1392. /* exported stuff */
  1393. static const struct file_operations video_fops =
  1394. {
  1395. .owner = THIS_MODULE,
  1396. .open = video_open,
  1397. .release = video_release,
  1398. .read = video_read,
  1399. .poll = video_poll,
  1400. .mmap = video_mmap,
  1401. .ioctl = video_ioctl2,
  1402. .compat_ioctl = v4l_compat_ioctl32,
  1403. .llseek = no_llseek,
  1404. };
  1405. static struct video_device cx8800_vbi_template;
  1406. static struct video_device cx8800_video_template =
  1407. {
  1408. .name = "cx8800-video",
  1409. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
  1410. .fops = &video_fops,
  1411. .minor = -1,
  1412. .vidioc_querycap = vidioc_querycap,
  1413. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  1414. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  1415. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  1416. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  1417. .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
  1418. .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
  1419. .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
  1420. .vidioc_reqbufs = vidioc_reqbufs,
  1421. .vidioc_querybuf = vidioc_querybuf,
  1422. .vidioc_qbuf = vidioc_qbuf,
  1423. .vidioc_dqbuf = vidioc_dqbuf,
  1424. .vidioc_s_std = vidioc_s_std,
  1425. .vidioc_enum_input = vidioc_enum_input,
  1426. .vidioc_g_input = vidioc_g_input,
  1427. .vidioc_s_input = vidioc_s_input,
  1428. .vidioc_queryctrl = vidioc_queryctrl,
  1429. .vidioc_g_ctrl = vidioc_g_ctrl,
  1430. .vidioc_s_ctrl = vidioc_s_ctrl,
  1431. .vidioc_streamon = vidioc_streamon,
  1432. .vidioc_streamoff = vidioc_streamoff,
  1433. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1434. .vidiocgmbuf = vidiocgmbuf,
  1435. #endif
  1436. .vidioc_g_tuner = vidioc_g_tuner,
  1437. .vidioc_s_tuner = vidioc_s_tuner,
  1438. .vidioc_g_frequency = vidioc_g_frequency,
  1439. .vidioc_s_frequency = vidioc_s_frequency,
  1440. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1441. .vidioc_g_register = vidioc_g_register,
  1442. .vidioc_s_register = vidioc_s_register,
  1443. #endif
  1444. .tvnorms = CX88_NORMS,
  1445. .current_norm = V4L2_STD_NTSC_M,
  1446. };
  1447. static const struct file_operations radio_fops =
  1448. {
  1449. .owner = THIS_MODULE,
  1450. .open = video_open,
  1451. .release = video_release,
  1452. .ioctl = video_ioctl2,
  1453. .compat_ioctl = v4l_compat_ioctl32,
  1454. .llseek = no_llseek,
  1455. };
  1456. static struct video_device cx8800_radio_template =
  1457. {
  1458. .name = "cx8800-radio",
  1459. .type = VID_TYPE_TUNER,
  1460. .hardware = 0,
  1461. .fops = &radio_fops,
  1462. .minor = -1,
  1463. .vidioc_querycap = radio_querycap,
  1464. .vidioc_g_tuner = radio_g_tuner,
  1465. .vidioc_enum_input = radio_enum_input,
  1466. .vidioc_g_audio = radio_g_audio,
  1467. .vidioc_s_tuner = radio_s_tuner,
  1468. .vidioc_s_audio = radio_s_audio,
  1469. .vidioc_s_input = radio_s_input,
  1470. .vidioc_queryctrl = radio_queryctrl,
  1471. .vidioc_g_ctrl = vidioc_g_ctrl,
  1472. .vidioc_s_ctrl = vidioc_s_ctrl,
  1473. .vidioc_g_frequency = vidioc_g_frequency,
  1474. .vidioc_s_frequency = vidioc_s_frequency,
  1475. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1476. .vidioc_g_register = vidioc_g_register,
  1477. .vidioc_s_register = vidioc_s_register,
  1478. #endif
  1479. };
  1480. /* ----------------------------------------------------------- */
  1481. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1482. {
  1483. if (dev->radio_dev) {
  1484. if (-1 != dev->radio_dev->minor)
  1485. video_unregister_device(dev->radio_dev);
  1486. else
  1487. video_device_release(dev->radio_dev);
  1488. dev->radio_dev = NULL;
  1489. }
  1490. if (dev->vbi_dev) {
  1491. if (-1 != dev->vbi_dev->minor)
  1492. video_unregister_device(dev->vbi_dev);
  1493. else
  1494. video_device_release(dev->vbi_dev);
  1495. dev->vbi_dev = NULL;
  1496. }
  1497. if (dev->video_dev) {
  1498. if (-1 != dev->video_dev->minor)
  1499. video_unregister_device(dev->video_dev);
  1500. else
  1501. video_device_release(dev->video_dev);
  1502. dev->video_dev = NULL;
  1503. }
  1504. }
  1505. static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
  1506. const struct pci_device_id *pci_id)
  1507. {
  1508. struct cx8800_dev *dev;
  1509. struct cx88_core *core;
  1510. int err;
  1511. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1512. if (NULL == dev)
  1513. return -ENOMEM;
  1514. /* pci init */
  1515. dev->pci = pci_dev;
  1516. if (pci_enable_device(pci_dev)) {
  1517. err = -EIO;
  1518. goto fail_free;
  1519. }
  1520. core = cx88_core_get(dev->pci);
  1521. if (NULL == core) {
  1522. err = -EINVAL;
  1523. goto fail_free;
  1524. }
  1525. dev->core = core;
  1526. /* print pci info */
  1527. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  1528. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1529. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  1530. "latency: %d, mmio: 0x%llx\n", core->name,
  1531. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1532. dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
  1533. pci_set_master(pci_dev);
  1534. if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
  1535. printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
  1536. err = -EIO;
  1537. goto fail_core;
  1538. }
  1539. /* Initialize VBI template */
  1540. memcpy( &cx8800_vbi_template, &cx8800_video_template,
  1541. sizeof(cx8800_vbi_template) );
  1542. strcpy(cx8800_vbi_template.name,"cx8800-vbi");
  1543. cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
  1544. /* initialize driver struct */
  1545. spin_lock_init(&dev->slock);
  1546. core->tvnorm = cx8800_video_template.current_norm;
  1547. /* init video dma queues */
  1548. INIT_LIST_HEAD(&dev->vidq.active);
  1549. INIT_LIST_HEAD(&dev->vidq.queued);
  1550. dev->vidq.timeout.function = cx8800_vid_timeout;
  1551. dev->vidq.timeout.data = (unsigned long)dev;
  1552. init_timer(&dev->vidq.timeout);
  1553. cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
  1554. MO_VID_DMACNTRL,0x11,0x00);
  1555. /* init vbi dma queues */
  1556. INIT_LIST_HEAD(&dev->vbiq.active);
  1557. INIT_LIST_HEAD(&dev->vbiq.queued);
  1558. dev->vbiq.timeout.function = cx8800_vbi_timeout;
  1559. dev->vbiq.timeout.data = (unsigned long)dev;
  1560. init_timer(&dev->vbiq.timeout);
  1561. cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
  1562. MO_VID_DMACNTRL,0x88,0x00);
  1563. /* get irq */
  1564. err = request_irq(pci_dev->irq, cx8800_irq,
  1565. IRQF_SHARED | IRQF_DISABLED, core->name, dev);
  1566. if (err < 0) {
  1567. printk(KERN_ERR "%s/0: can't get IRQ %d\n",
  1568. core->name,pci_dev->irq);
  1569. goto fail_core;
  1570. }
  1571. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1572. /* load and configure helper modules */
  1573. if (TUNER_ABSENT != core->board.tuner_type)
  1574. request_module("tuner");
  1575. if (core->board.audio_chip == AUDIO_CHIP_WM8775)
  1576. request_module("wm8775");
  1577. switch (core->boardnr) {
  1578. case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
  1579. request_module("ir-kbd-i2c");
  1580. request_module("rtc-isl1208");
  1581. }
  1582. /* register v4l devices */
  1583. dev->video_dev = cx88_vdev_init(core,dev->pci,
  1584. &cx8800_video_template,"video");
  1585. err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
  1586. video_nr[core->nr]);
  1587. if (err < 0) {
  1588. printk(KERN_ERR "%s/0: can't register video device\n",
  1589. core->name);
  1590. goto fail_unreg;
  1591. }
  1592. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1593. core->name,dev->video_dev->minor & 0x1f);
  1594. dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
  1595. err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
  1596. vbi_nr[core->nr]);
  1597. if (err < 0) {
  1598. printk(KERN_ERR "%s/0: can't register vbi device\n",
  1599. core->name);
  1600. goto fail_unreg;
  1601. }
  1602. printk(KERN_INFO "%s/0: registered device vbi%d\n",
  1603. core->name,dev->vbi_dev->minor & 0x1f);
  1604. if (core->board.radio.type == CX88_RADIO) {
  1605. dev->radio_dev = cx88_vdev_init(core,dev->pci,
  1606. &cx8800_radio_template,"radio");
  1607. err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
  1608. radio_nr[core->nr]);
  1609. if (err < 0) {
  1610. printk(KERN_ERR "%s/0: can't register radio device\n",
  1611. core->name);
  1612. goto fail_unreg;
  1613. }
  1614. printk(KERN_INFO "%s/0: registered device radio%d\n",
  1615. core->name,dev->radio_dev->minor & 0x1f);
  1616. }
  1617. /* everything worked */
  1618. list_add_tail(&dev->devlist,&cx8800_devlist);
  1619. pci_set_drvdata(pci_dev,dev);
  1620. /* initial device configuration */
  1621. mutex_lock(&core->lock);
  1622. cx88_set_tvnorm(core,core->tvnorm);
  1623. init_controls(core);
  1624. cx88_video_mux(core,0);
  1625. mutex_unlock(&core->lock);
  1626. /* start tvaudio thread */
  1627. if (core->board.tuner_type != TUNER_ABSENT) {
  1628. core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
  1629. if (IS_ERR(core->kthread)) {
  1630. err = PTR_ERR(core->kthread);
  1631. printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
  1632. core->name, err);
  1633. }
  1634. }
  1635. return 0;
  1636. fail_unreg:
  1637. cx8800_unregister_video(dev);
  1638. free_irq(pci_dev->irq, dev);
  1639. fail_core:
  1640. cx88_core_put(core,dev->pci);
  1641. fail_free:
  1642. kfree(dev);
  1643. return err;
  1644. }
  1645. static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
  1646. {
  1647. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1648. struct cx88_core *core = dev->core;
  1649. /* stop thread */
  1650. if (core->kthread) {
  1651. kthread_stop(core->kthread);
  1652. core->kthread = NULL;
  1653. }
  1654. cx88_shutdown(core); /* FIXME */
  1655. pci_disable_device(pci_dev);
  1656. /* unregister stuff */
  1657. free_irq(pci_dev->irq, dev);
  1658. cx8800_unregister_video(dev);
  1659. pci_set_drvdata(pci_dev, NULL);
  1660. /* free memory */
  1661. btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
  1662. list_del(&dev->devlist);
  1663. cx88_core_put(core,dev->pci);
  1664. kfree(dev);
  1665. }
  1666. #ifdef CONFIG_PM
  1667. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1668. {
  1669. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1670. struct cx88_core *core = dev->core;
  1671. /* stop video+vbi capture */
  1672. spin_lock(&dev->slock);
  1673. if (!list_empty(&dev->vidq.active)) {
  1674. printk("%s/0: suspend video\n", core->name);
  1675. stop_video_dma(dev);
  1676. del_timer(&dev->vidq.timeout);
  1677. }
  1678. if (!list_empty(&dev->vbiq.active)) {
  1679. printk("%s/0: suspend vbi\n", core->name);
  1680. cx8800_stop_vbi_dma(dev);
  1681. del_timer(&dev->vbiq.timeout);
  1682. }
  1683. spin_unlock(&dev->slock);
  1684. if (core->ir)
  1685. cx88_ir_stop(core, core->ir);
  1686. /* FIXME -- shutdown device */
  1687. cx88_shutdown(core);
  1688. pci_save_state(pci_dev);
  1689. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  1690. pci_disable_device(pci_dev);
  1691. dev->state.disabled = 1;
  1692. }
  1693. return 0;
  1694. }
  1695. static int cx8800_resume(struct pci_dev *pci_dev)
  1696. {
  1697. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1698. struct cx88_core *core = dev->core;
  1699. int err;
  1700. if (dev->state.disabled) {
  1701. err=pci_enable_device(pci_dev);
  1702. if (err) {
  1703. printk(KERN_ERR "%s/0: can't enable device\n",
  1704. core->name);
  1705. return err;
  1706. }
  1707. dev->state.disabled = 0;
  1708. }
  1709. err= pci_set_power_state(pci_dev, PCI_D0);
  1710. if (err) {
  1711. printk(KERN_ERR "%s/0: can't set power state\n", core->name);
  1712. pci_disable_device(pci_dev);
  1713. dev->state.disabled = 1;
  1714. return err;
  1715. }
  1716. pci_restore_state(pci_dev);
  1717. /* FIXME: re-initialize hardware */
  1718. cx88_reset(core);
  1719. if (core->ir)
  1720. cx88_ir_start(core, core->ir);
  1721. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1722. /* restart video+vbi capture */
  1723. spin_lock(&dev->slock);
  1724. if (!list_empty(&dev->vidq.active)) {
  1725. printk("%s/0: resume video\n", core->name);
  1726. restart_video_queue(dev,&dev->vidq);
  1727. }
  1728. if (!list_empty(&dev->vbiq.active)) {
  1729. printk("%s/0: 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 "cx88/0: 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. */