cx88-video.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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. if (!list_empty(&q->active)) {
  419. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  420. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  421. buf, buf->vb.i);
  422. start_video_dma(dev, q, buf);
  423. list_for_each_entry(buf, &q->active, vb.queue)
  424. buf->count = q->count++;
  425. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  426. return 0;
  427. }
  428. prev = NULL;
  429. for (;;) {
  430. if (list_empty(&q->queued))
  431. return 0;
  432. buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
  433. if (NULL == prev) {
  434. list_move_tail(&buf->vb.queue, &q->active);
  435. start_video_dma(dev, q, buf);
  436. buf->vb.state = VIDEOBUF_ACTIVE;
  437. buf->count = q->count++;
  438. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  439. dprintk(2,"[%p/%d] restart_queue - first active\n",
  440. buf,buf->vb.i);
  441. } else if (prev->vb.width == buf->vb.width &&
  442. prev->vb.height == buf->vb.height &&
  443. prev->fmt == buf->fmt) {
  444. list_move_tail(&buf->vb.queue, &q->active);
  445. buf->vb.state = VIDEOBUF_ACTIVE;
  446. buf->count = q->count++;
  447. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  448. dprintk(2,"[%p/%d] restart_queue - move to active\n",
  449. buf,buf->vb.i);
  450. } else {
  451. return 0;
  452. }
  453. prev = buf;
  454. }
  455. }
  456. /* ------------------------------------------------------------------ */
  457. static int
  458. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  459. {
  460. struct cx8800_fh *fh = q->priv_data;
  461. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  462. if (0 == *count)
  463. *count = 32;
  464. while (*size * *count > vid_limit * 1024 * 1024)
  465. (*count)--;
  466. return 0;
  467. }
  468. static int
  469. buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  470. enum v4l2_field field)
  471. {
  472. struct cx8800_fh *fh = q->priv_data;
  473. struct cx8800_dev *dev = fh->dev;
  474. struct cx88_core *core = dev->core;
  475. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  476. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  477. int rc, init_buffer = 0;
  478. BUG_ON(NULL == fh->fmt);
  479. if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
  480. fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
  481. return -EINVAL;
  482. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  483. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  484. return -EINVAL;
  485. if (buf->fmt != fh->fmt ||
  486. buf->vb.width != fh->width ||
  487. buf->vb.height != fh->height ||
  488. buf->vb.field != field) {
  489. buf->fmt = fh->fmt;
  490. buf->vb.width = fh->width;
  491. buf->vb.height = fh->height;
  492. buf->vb.field = field;
  493. init_buffer = 1;
  494. }
  495. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  496. init_buffer = 1;
  497. if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
  498. goto fail;
  499. }
  500. if (init_buffer) {
  501. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  502. switch (buf->vb.field) {
  503. case V4L2_FIELD_TOP:
  504. cx88_risc_buffer(dev->pci, &buf->risc,
  505. dma->sglist, 0, UNSET,
  506. buf->bpl, 0, buf->vb.height);
  507. break;
  508. case V4L2_FIELD_BOTTOM:
  509. cx88_risc_buffer(dev->pci, &buf->risc,
  510. dma->sglist, UNSET, 0,
  511. buf->bpl, 0, buf->vb.height);
  512. break;
  513. case V4L2_FIELD_INTERLACED:
  514. cx88_risc_buffer(dev->pci, &buf->risc,
  515. dma->sglist, 0, buf->bpl,
  516. buf->bpl, buf->bpl,
  517. buf->vb.height >> 1);
  518. break;
  519. case V4L2_FIELD_SEQ_TB:
  520. cx88_risc_buffer(dev->pci, &buf->risc,
  521. dma->sglist,
  522. 0, buf->bpl * (buf->vb.height >> 1),
  523. buf->bpl, 0,
  524. buf->vb.height >> 1);
  525. break;
  526. case V4L2_FIELD_SEQ_BT:
  527. cx88_risc_buffer(dev->pci, &buf->risc,
  528. dma->sglist,
  529. buf->bpl * (buf->vb.height >> 1), 0,
  530. buf->bpl, 0,
  531. buf->vb.height >> 1);
  532. break;
  533. default:
  534. BUG();
  535. }
  536. }
  537. dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  538. buf, buf->vb.i,
  539. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  540. (unsigned long)buf->risc.dma);
  541. buf->vb.state = VIDEOBUF_PREPARED;
  542. return 0;
  543. fail:
  544. cx88_free_buffer(q,buf);
  545. return rc;
  546. }
  547. static void
  548. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  549. {
  550. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  551. struct cx88_buffer *prev;
  552. struct cx8800_fh *fh = vq->priv_data;
  553. struct cx8800_dev *dev = fh->dev;
  554. struct cx88_core *core = dev->core;
  555. struct cx88_dmaqueue *q = &dev->vidq;
  556. /* add jump to stopper */
  557. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  558. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  559. if (!list_empty(&q->queued)) {
  560. list_add_tail(&buf->vb.queue,&q->queued);
  561. buf->vb.state = VIDEOBUF_QUEUED;
  562. dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
  563. buf, buf->vb.i);
  564. } else if (list_empty(&q->active)) {
  565. list_add_tail(&buf->vb.queue,&q->active);
  566. start_video_dma(dev, q, buf);
  567. buf->vb.state = VIDEOBUF_ACTIVE;
  568. buf->count = q->count++;
  569. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  570. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  571. buf, buf->vb.i);
  572. } else {
  573. prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
  574. if (prev->vb.width == buf->vb.width &&
  575. prev->vb.height == buf->vb.height &&
  576. prev->fmt == buf->fmt) {
  577. list_add_tail(&buf->vb.queue,&q->active);
  578. buf->vb.state = VIDEOBUF_ACTIVE;
  579. buf->count = q->count++;
  580. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  581. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  582. buf, buf->vb.i);
  583. } else {
  584. list_add_tail(&buf->vb.queue,&q->queued);
  585. buf->vb.state = VIDEOBUF_QUEUED;
  586. dprintk(2,"[%p/%d] buffer_queue - first queued\n",
  587. buf, buf->vb.i);
  588. }
  589. }
  590. }
  591. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  592. {
  593. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  594. cx88_free_buffer(q,buf);
  595. }
  596. static struct videobuf_queue_ops cx8800_video_qops = {
  597. .buf_setup = buffer_setup,
  598. .buf_prepare = buffer_prepare,
  599. .buf_queue = buffer_queue,
  600. .buf_release = buffer_release,
  601. };
  602. /* ------------------------------------------------------------------ */
  603. /* ------------------------------------------------------------------ */
  604. static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
  605. {
  606. switch (fh->type) {
  607. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  608. return &fh->vidq;
  609. case V4L2_BUF_TYPE_VBI_CAPTURE:
  610. return &fh->vbiq;
  611. default:
  612. BUG();
  613. return NULL;
  614. }
  615. }
  616. static int get_ressource(struct cx8800_fh *fh)
  617. {
  618. switch (fh->type) {
  619. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  620. return RESOURCE_VIDEO;
  621. case V4L2_BUF_TYPE_VBI_CAPTURE:
  622. return RESOURCE_VBI;
  623. default:
  624. BUG();
  625. return 0;
  626. }
  627. }
  628. static int video_open(struct inode *inode, struct file *file)
  629. {
  630. int minor = iminor(inode);
  631. struct cx8800_dev *h,*dev = NULL;
  632. struct cx88_core *core;
  633. struct cx8800_fh *fh;
  634. enum v4l2_buf_type type = 0;
  635. int radio = 0;
  636. list_for_each_entry(h, &cx8800_devlist, devlist) {
  637. if (h->video_dev->minor == minor) {
  638. dev = h;
  639. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  640. }
  641. if (h->vbi_dev->minor == minor) {
  642. dev = h;
  643. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  644. }
  645. if (h->radio_dev &&
  646. h->radio_dev->minor == minor) {
  647. radio = 1;
  648. dev = h;
  649. }
  650. }
  651. if (NULL == dev)
  652. return -ENODEV;
  653. core = dev->core;
  654. dprintk(1,"open minor=%d radio=%d type=%s\n",
  655. minor,radio,v4l2_type_names[type]);
  656. /* allocate + initialize per filehandle data */
  657. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  658. if (NULL == fh)
  659. return -ENOMEM;
  660. file->private_data = fh;
  661. fh->dev = dev;
  662. fh->radio = radio;
  663. fh->type = type;
  664. fh->width = 320;
  665. fh->height = 240;
  666. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  667. videobuf_queue_pci_init(&fh->vidq, &cx8800_video_qops,
  668. dev->pci, &dev->slock,
  669. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  670. V4L2_FIELD_INTERLACED,
  671. sizeof(struct cx88_buffer),
  672. fh);
  673. videobuf_queue_pci_init(&fh->vbiq, &cx8800_vbi_qops,
  674. dev->pci, &dev->slock,
  675. V4L2_BUF_TYPE_VBI_CAPTURE,
  676. V4L2_FIELD_SEQ_TB,
  677. sizeof(struct cx88_buffer),
  678. fh);
  679. if (fh->radio) {
  680. dprintk(1,"video_open: setting radio device\n");
  681. cx_write(MO_GP3_IO, core->board.radio.gpio3);
  682. cx_write(MO_GP0_IO, core->board.radio.gpio0);
  683. cx_write(MO_GP1_IO, core->board.radio.gpio1);
  684. cx_write(MO_GP2_IO, core->board.radio.gpio2);
  685. core->tvaudio = WW_FM;
  686. cx88_set_tvaudio(core);
  687. cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
  688. cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
  689. }
  690. return 0;
  691. }
  692. static ssize_t
  693. video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  694. {
  695. struct cx8800_fh *fh = file->private_data;
  696. switch (fh->type) {
  697. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  698. if (res_locked(fh->dev,RESOURCE_VIDEO))
  699. return -EBUSY;
  700. return videobuf_read_one(&fh->vidq, data, count, ppos,
  701. file->f_flags & O_NONBLOCK);
  702. case V4L2_BUF_TYPE_VBI_CAPTURE:
  703. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  704. return -EBUSY;
  705. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  706. file->f_flags & O_NONBLOCK);
  707. default:
  708. BUG();
  709. return 0;
  710. }
  711. }
  712. static unsigned int
  713. video_poll(struct file *file, struct poll_table_struct *wait)
  714. {
  715. struct cx8800_fh *fh = file->private_data;
  716. struct cx88_buffer *buf;
  717. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  718. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  719. return POLLERR;
  720. return videobuf_poll_stream(file, &fh->vbiq, wait);
  721. }
  722. if (res_check(fh,RESOURCE_VIDEO)) {
  723. /* streaming capture */
  724. if (list_empty(&fh->vidq.stream))
  725. return POLLERR;
  726. buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
  727. } else {
  728. /* read() capture */
  729. buf = (struct cx88_buffer*)fh->vidq.read_buf;
  730. if (NULL == buf)
  731. return POLLERR;
  732. }
  733. poll_wait(file, &buf->vb.done, wait);
  734. if (buf->vb.state == VIDEOBUF_DONE ||
  735. buf->vb.state == VIDEOBUF_ERROR)
  736. return POLLIN|POLLRDNORM;
  737. return 0;
  738. }
  739. static int video_release(struct inode *inode, struct file *file)
  740. {
  741. struct cx8800_fh *fh = file->private_data;
  742. struct cx8800_dev *dev = fh->dev;
  743. /* turn off overlay */
  744. if (res_check(fh, RESOURCE_OVERLAY)) {
  745. /* FIXME */
  746. res_free(dev,fh,RESOURCE_OVERLAY);
  747. }
  748. /* stop video capture */
  749. if (res_check(fh, RESOURCE_VIDEO)) {
  750. videobuf_queue_cancel(&fh->vidq);
  751. res_free(dev,fh,RESOURCE_VIDEO);
  752. }
  753. if (fh->vidq.read_buf) {
  754. buffer_release(&fh->vidq,fh->vidq.read_buf);
  755. kfree(fh->vidq.read_buf);
  756. }
  757. /* stop vbi capture */
  758. if (res_check(fh, RESOURCE_VBI)) {
  759. videobuf_stop(&fh->vbiq);
  760. res_free(dev,fh,RESOURCE_VBI);
  761. }
  762. videobuf_mmap_free(&fh->vidq);
  763. videobuf_mmap_free(&fh->vbiq);
  764. file->private_data = NULL;
  765. kfree(fh);
  766. cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
  767. return 0;
  768. }
  769. static int
  770. video_mmap(struct file *file, struct vm_area_struct * vma)
  771. {
  772. struct cx8800_fh *fh = file->private_data;
  773. return videobuf_mmap_mapper(get_queue(fh), vma);
  774. }
  775. /* ------------------------------------------------------------------ */
  776. /* VIDEO CTRL IOCTLS */
  777. int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
  778. {
  779. struct cx88_ctrl *c = NULL;
  780. u32 value;
  781. int i;
  782. for (i = 0; i < CX8800_CTLS; i++)
  783. if (cx8800_ctls[i].v.id == ctl->id)
  784. c = &cx8800_ctls[i];
  785. if (unlikely(NULL == c))
  786. return -EINVAL;
  787. value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
  788. switch (ctl->id) {
  789. case V4L2_CID_AUDIO_BALANCE:
  790. ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
  791. : (0x7f - (value & 0x7f));
  792. break;
  793. case V4L2_CID_AUDIO_VOLUME:
  794. ctl->value = 0x3f - (value & 0x3f);
  795. break;
  796. default:
  797. ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
  798. break;
  799. }
  800. dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  801. ctl->id, c->v.name, ctl->value, c->reg,
  802. value,c->mask, c->sreg ? " [shadowed]" : "");
  803. return 0;
  804. }
  805. EXPORT_SYMBOL(cx88_get_control);
  806. int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
  807. {
  808. struct cx88_ctrl *c = NULL;
  809. u32 value,mask;
  810. int i;
  811. for (i = 0; i < CX8800_CTLS; i++) {
  812. if (cx8800_ctls[i].v.id == ctl->id) {
  813. c = &cx8800_ctls[i];
  814. }
  815. }
  816. if (unlikely(NULL == c))
  817. return -EINVAL;
  818. if (ctl->value < c->v.minimum)
  819. ctl->value = c->v.minimum;
  820. if (ctl->value > c->v.maximum)
  821. ctl->value = c->v.maximum;
  822. mask=c->mask;
  823. switch (ctl->id) {
  824. case V4L2_CID_AUDIO_BALANCE:
  825. value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
  826. break;
  827. case V4L2_CID_AUDIO_VOLUME:
  828. value = 0x3f - (ctl->value & 0x3f);
  829. break;
  830. case V4L2_CID_SATURATION:
  831. /* special v_sat handling */
  832. value = ((ctl->value - c->off) << c->shift) & c->mask;
  833. if (core->tvnorm & V4L2_STD_SECAM) {
  834. /* For SECAM, both U and V sat should be equal */
  835. value=value<<8|value;
  836. } else {
  837. /* Keeps U Saturation proportional to V Sat */
  838. value=(value*0x5a)/0x7f<<8|value;
  839. }
  840. mask=0xffff;
  841. break;
  842. default:
  843. value = ((ctl->value - c->off) << c->shift) & c->mask;
  844. break;
  845. }
  846. dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  847. ctl->id, c->v.name, ctl->value, c->reg, value,
  848. mask, c->sreg ? " [shadowed]" : "");
  849. if (c->sreg) {
  850. cx_sandor(c->sreg, c->reg, mask, value);
  851. } else {
  852. cx_andor(c->reg, mask, value);
  853. }
  854. return 0;
  855. }
  856. EXPORT_SYMBOL(cx88_set_control);
  857. static void init_controls(struct cx88_core *core)
  858. {
  859. struct v4l2_control ctrl;
  860. int i;
  861. for (i = 0; i < CX8800_CTLS; i++) {
  862. ctrl.id=cx8800_ctls[i].v.id;
  863. ctrl.value=cx8800_ctls[i].v.default_value;
  864. cx88_set_control(core, &ctrl);
  865. }
  866. }
  867. /* ------------------------------------------------------------------ */
  868. /* VIDEO IOCTLS */
  869. static int vidioc_g_fmt_cap (struct file *file, void *priv,
  870. struct v4l2_format *f)
  871. {
  872. struct cx8800_fh *fh = priv;
  873. f->fmt.pix.width = fh->width;
  874. f->fmt.pix.height = fh->height;
  875. f->fmt.pix.field = fh->vidq.field;
  876. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  877. f->fmt.pix.bytesperline =
  878. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  879. f->fmt.pix.sizeimage =
  880. f->fmt.pix.height * f->fmt.pix.bytesperline;
  881. return 0;
  882. }
  883. static int vidioc_try_fmt_cap (struct file *file, void *priv,
  884. struct v4l2_format *f)
  885. {
  886. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  887. struct cx8800_fmt *fmt;
  888. enum v4l2_field field;
  889. unsigned int maxw, maxh;
  890. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  891. if (NULL == fmt)
  892. return -EINVAL;
  893. field = f->fmt.pix.field;
  894. maxw = norm_maxw(core->tvnorm);
  895. maxh = norm_maxh(core->tvnorm);
  896. if (V4L2_FIELD_ANY == field) {
  897. field = (f->fmt.pix.height > maxh/2)
  898. ? V4L2_FIELD_INTERLACED
  899. : V4L2_FIELD_BOTTOM;
  900. }
  901. switch (field) {
  902. case V4L2_FIELD_TOP:
  903. case V4L2_FIELD_BOTTOM:
  904. maxh = maxh / 2;
  905. break;
  906. case V4L2_FIELD_INTERLACED:
  907. break;
  908. default:
  909. return -EINVAL;
  910. }
  911. f->fmt.pix.field = field;
  912. if (f->fmt.pix.height < 32)
  913. f->fmt.pix.height = 32;
  914. if (f->fmt.pix.height > maxh)
  915. f->fmt.pix.height = maxh;
  916. if (f->fmt.pix.width < 48)
  917. f->fmt.pix.width = 48;
  918. if (f->fmt.pix.width > maxw)
  919. f->fmt.pix.width = maxw;
  920. f->fmt.pix.width &= ~0x03;
  921. f->fmt.pix.bytesperline =
  922. (f->fmt.pix.width * fmt->depth) >> 3;
  923. f->fmt.pix.sizeimage =
  924. f->fmt.pix.height * f->fmt.pix.bytesperline;
  925. return 0;
  926. }
  927. static int vidioc_s_fmt_cap (struct file *file, void *priv,
  928. struct v4l2_format *f)
  929. {
  930. struct cx8800_fh *fh = priv;
  931. int err = vidioc_try_fmt_cap (file,priv,f);
  932. if (0 != err)
  933. return err;
  934. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  935. fh->width = f->fmt.pix.width;
  936. fh->height = f->fmt.pix.height;
  937. fh->vidq.field = f->fmt.pix.field;
  938. return 0;
  939. }
  940. static int vidioc_querycap (struct file *file, void *priv,
  941. struct v4l2_capability *cap)
  942. {
  943. struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
  944. struct cx88_core *core = dev->core;
  945. strcpy(cap->driver, "cx8800");
  946. strlcpy(cap->card, core->board.name, sizeof(cap->card));
  947. sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
  948. cap->version = CX88_VERSION_CODE;
  949. cap->capabilities =
  950. V4L2_CAP_VIDEO_CAPTURE |
  951. V4L2_CAP_READWRITE |
  952. V4L2_CAP_STREAMING |
  953. V4L2_CAP_VBI_CAPTURE;
  954. if (UNSET != core->board.tuner_type)
  955. cap->capabilities |= V4L2_CAP_TUNER;
  956. return 0;
  957. }
  958. static int vidioc_enum_fmt_cap (struct file *file, void *priv,
  959. struct v4l2_fmtdesc *f)
  960. {
  961. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  962. return -EINVAL;
  963. strlcpy(f->description,formats[f->index].name,sizeof(f->description));
  964. f->pixelformat = formats[f->index].fourcc;
  965. return 0;
  966. }
  967. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  968. static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
  969. {
  970. struct cx8800_fh *fh = priv;
  971. return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
  972. }
  973. #endif
  974. static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
  975. {
  976. struct cx8800_fh *fh = priv;
  977. return (videobuf_reqbufs(get_queue(fh), p));
  978. }
  979. static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
  980. {
  981. struct cx8800_fh *fh = priv;
  982. return (videobuf_querybuf(get_queue(fh), p));
  983. }
  984. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  985. {
  986. struct cx8800_fh *fh = priv;
  987. return (videobuf_qbuf(get_queue(fh), p));
  988. }
  989. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  990. {
  991. struct cx8800_fh *fh = priv;
  992. return (videobuf_dqbuf(get_queue(fh), p,
  993. file->f_flags & O_NONBLOCK));
  994. }
  995. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  996. {
  997. struct cx8800_fh *fh = priv;
  998. struct cx8800_dev *dev = fh->dev;
  999. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  1000. return -EINVAL;
  1001. if (unlikely(i != fh->type))
  1002. return -EINVAL;
  1003. if (unlikely(!res_get(dev,fh,get_ressource(fh))))
  1004. return -EBUSY;
  1005. return videobuf_streamon(get_queue(fh));
  1006. }
  1007. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1008. {
  1009. struct cx8800_fh *fh = priv;
  1010. struct cx8800_dev *dev = fh->dev;
  1011. int err, res;
  1012. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1013. return -EINVAL;
  1014. if (i != fh->type)
  1015. return -EINVAL;
  1016. res = get_ressource(fh);
  1017. err = videobuf_streamoff(get_queue(fh));
  1018. if (err < 0)
  1019. return err;
  1020. res_free(dev,fh,res);
  1021. return 0;
  1022. }
  1023. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
  1024. {
  1025. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1026. mutex_lock(&core->lock);
  1027. cx88_set_tvnorm(core,*tvnorms);
  1028. mutex_unlock(&core->lock);
  1029. return 0;
  1030. }
  1031. /* only one input in this sample driver */
  1032. int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
  1033. {
  1034. static const char *iname[] = {
  1035. [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
  1036. [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
  1037. [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
  1038. [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
  1039. [ CX88_VMUX_SVIDEO ] = "S-Video",
  1040. [ CX88_VMUX_TELEVISION ] = "Television",
  1041. [ CX88_VMUX_CABLE ] = "Cable TV",
  1042. [ CX88_VMUX_DVB ] = "DVB",
  1043. [ CX88_VMUX_DEBUG ] = "for debug only",
  1044. };
  1045. unsigned int n;
  1046. n = i->index;
  1047. if (n >= 4)
  1048. return -EINVAL;
  1049. if (0 == INPUT(n).type)
  1050. return -EINVAL;
  1051. memset(i,0,sizeof(*i));
  1052. i->index = n;
  1053. i->type = V4L2_INPUT_TYPE_CAMERA;
  1054. strcpy(i->name,iname[INPUT(n).type]);
  1055. if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
  1056. (CX88_VMUX_CABLE == INPUT(n).type))
  1057. i->type = V4L2_INPUT_TYPE_TUNER;
  1058. i->std = CX88_NORMS;
  1059. return 0;
  1060. }
  1061. EXPORT_SYMBOL(cx88_enum_input);
  1062. static int vidioc_enum_input (struct file *file, void *priv,
  1063. struct v4l2_input *i)
  1064. {
  1065. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1066. return cx88_enum_input (core,i);
  1067. }
  1068. static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
  1069. {
  1070. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1071. *i = core->input;
  1072. return 0;
  1073. }
  1074. static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
  1075. {
  1076. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1077. if (i >= 4)
  1078. return -EINVAL;
  1079. mutex_lock(&core->lock);
  1080. cx88_newstation(core);
  1081. cx88_video_mux(core,i);
  1082. mutex_unlock(&core->lock);
  1083. return 0;
  1084. }
  1085. static int vidioc_queryctrl (struct file *file, void *priv,
  1086. struct v4l2_queryctrl *qctrl)
  1087. {
  1088. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1089. if (unlikely(qctrl->id == 0))
  1090. return -EINVAL;
  1091. return cx8800_ctrl_query(qctrl);
  1092. }
  1093. static int vidioc_g_ctrl (struct file *file, void *priv,
  1094. struct v4l2_control *ctl)
  1095. {
  1096. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1097. return
  1098. cx88_get_control(core,ctl);
  1099. }
  1100. static int vidioc_s_ctrl (struct file *file, void *priv,
  1101. struct v4l2_control *ctl)
  1102. {
  1103. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1104. return
  1105. cx88_set_control(core,ctl);
  1106. }
  1107. static int vidioc_g_tuner (struct file *file, void *priv,
  1108. struct v4l2_tuner *t)
  1109. {
  1110. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1111. u32 reg;
  1112. if (unlikely(UNSET == core->board.tuner_type))
  1113. return -EINVAL;
  1114. if (0 != t->index)
  1115. return -EINVAL;
  1116. strcpy(t->name, "Television");
  1117. t->type = V4L2_TUNER_ANALOG_TV;
  1118. t->capability = V4L2_TUNER_CAP_NORM;
  1119. t->rangehigh = 0xffffffffUL;
  1120. cx88_get_stereo(core ,t);
  1121. reg = cx_read(MO_DEVICE_STATUS);
  1122. t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
  1123. return 0;
  1124. }
  1125. static int vidioc_s_tuner (struct file *file, void *priv,
  1126. struct v4l2_tuner *t)
  1127. {
  1128. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1129. if (UNSET == core->board.tuner_type)
  1130. return -EINVAL;
  1131. if (0 != t->index)
  1132. return -EINVAL;
  1133. cx88_set_stereo(core, t->audmode, 1);
  1134. return 0;
  1135. }
  1136. static int vidioc_g_frequency (struct file *file, void *priv,
  1137. struct v4l2_frequency *f)
  1138. {
  1139. struct cx8800_fh *fh = priv;
  1140. struct cx88_core *core = fh->dev->core;
  1141. if (unlikely(UNSET == core->board.tuner_type))
  1142. return -EINVAL;
  1143. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1144. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1145. f->frequency = core->freq;
  1146. cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
  1147. return 0;
  1148. }
  1149. int cx88_set_freq (struct cx88_core *core,
  1150. struct v4l2_frequency *f)
  1151. {
  1152. if (unlikely(UNSET == core->board.tuner_type))
  1153. return -EINVAL;
  1154. if (unlikely(f->tuner != 0))
  1155. return -EINVAL;
  1156. mutex_lock(&core->lock);
  1157. core->freq = f->frequency;
  1158. cx88_newstation(core);
  1159. cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
  1160. /* When changing channels it is required to reset TVAUDIO */
  1161. msleep (10);
  1162. cx88_set_tvaudio(core);
  1163. mutex_unlock(&core->lock);
  1164. return 0;
  1165. }
  1166. EXPORT_SYMBOL(cx88_set_freq);
  1167. static int vidioc_s_frequency (struct file *file, void *priv,
  1168. struct v4l2_frequency *f)
  1169. {
  1170. struct cx8800_fh *fh = priv;
  1171. struct cx88_core *core = fh->dev->core;
  1172. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1173. return -EINVAL;
  1174. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1175. return -EINVAL;
  1176. return
  1177. cx88_set_freq (core,f);
  1178. }
  1179. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1180. static int vidioc_g_register (struct file *file, void *fh,
  1181. struct v4l2_register *reg)
  1182. {
  1183. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1184. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1185. return -EINVAL;
  1186. /* cx2388x has a 24-bit register space */
  1187. reg->val = cx_read(reg->reg&0xffffff);
  1188. return 0;
  1189. }
  1190. static int vidioc_s_register (struct file *file, void *fh,
  1191. struct v4l2_register *reg)
  1192. {
  1193. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1194. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1195. return -EINVAL;
  1196. cx_write(reg->reg&0xffffff, reg->val);
  1197. return 0;
  1198. }
  1199. #endif
  1200. /* ----------------------------------------------------------- */
  1201. /* RADIO ESPECIFIC IOCTLS */
  1202. /* ----------------------------------------------------------- */
  1203. static int radio_querycap (struct file *file, void *priv,
  1204. struct v4l2_capability *cap)
  1205. {
  1206. struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
  1207. struct cx88_core *core = dev->core;
  1208. strcpy(cap->driver, "cx8800");
  1209. strlcpy(cap->card, core->board.name, sizeof(cap->card));
  1210. sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
  1211. cap->version = CX88_VERSION_CODE;
  1212. cap->capabilities = V4L2_CAP_TUNER;
  1213. return 0;
  1214. }
  1215. static int radio_g_tuner (struct file *file, void *priv,
  1216. struct v4l2_tuner *t)
  1217. {
  1218. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1219. if (unlikely(t->index > 0))
  1220. return -EINVAL;
  1221. strcpy(t->name, "Radio");
  1222. t->type = V4L2_TUNER_RADIO;
  1223. cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
  1224. return 0;
  1225. }
  1226. static int radio_enum_input (struct file *file, void *priv,
  1227. struct v4l2_input *i)
  1228. {
  1229. if (i->index != 0)
  1230. return -EINVAL;
  1231. strcpy(i->name,"Radio");
  1232. i->type = V4L2_INPUT_TYPE_TUNER;
  1233. return 0;
  1234. }
  1235. static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
  1236. {
  1237. if (unlikely(a->index))
  1238. return -EINVAL;
  1239. memset(a,0,sizeof(*a));
  1240. strcpy(a->name,"Radio");
  1241. return 0;
  1242. }
  1243. /* FIXME: Should add a standard for radio */
  1244. static int radio_s_tuner (struct file *file, void *priv,
  1245. struct v4l2_tuner *t)
  1246. {
  1247. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1248. if (0 != t->index)
  1249. return -EINVAL;
  1250. cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
  1251. return 0;
  1252. }
  1253. static int radio_s_audio (struct file *file, void *fh,
  1254. struct v4l2_audio *a)
  1255. {
  1256. return 0;
  1257. }
  1258. static int radio_s_input (struct file *file, void *fh, unsigned int i)
  1259. {
  1260. return 0;
  1261. }
  1262. static int radio_queryctrl (struct file *file, void *priv,
  1263. struct v4l2_queryctrl *c)
  1264. {
  1265. int i;
  1266. if (c->id < V4L2_CID_BASE ||
  1267. c->id >= V4L2_CID_LASTP1)
  1268. return -EINVAL;
  1269. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1270. for (i = 0; i < CX8800_CTLS; i++)
  1271. if (cx8800_ctls[i].v.id == c->id)
  1272. break;
  1273. *c = cx8800_ctls[i].v;
  1274. } else
  1275. *c = no_ctl;
  1276. return 0;
  1277. }
  1278. /* ----------------------------------------------------------- */
  1279. static void cx8800_vid_timeout(unsigned long data)
  1280. {
  1281. struct cx8800_dev *dev = (struct cx8800_dev*)data;
  1282. struct cx88_core *core = dev->core;
  1283. struct cx88_dmaqueue *q = &dev->vidq;
  1284. struct cx88_buffer *buf;
  1285. unsigned long flags;
  1286. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1287. cx_clear(MO_VID_DMACNTRL, 0x11);
  1288. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1289. spin_lock_irqsave(&dev->slock,flags);
  1290. while (!list_empty(&q->active)) {
  1291. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  1292. list_del(&buf->vb.queue);
  1293. buf->vb.state = VIDEOBUF_ERROR;
  1294. wake_up(&buf->vb.done);
  1295. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
  1296. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  1297. }
  1298. restart_video_queue(dev,q);
  1299. spin_unlock_irqrestore(&dev->slock,flags);
  1300. }
  1301. static char *cx88_vid_irqs[32] = {
  1302. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  1303. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  1304. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  1305. "y_sync", "u_sync", "v_sync", "vbi_sync",
  1306. "opc_err", "par_err", "rip_err", "pci_abort",
  1307. };
  1308. static void cx8800_vid_irq(struct cx8800_dev *dev)
  1309. {
  1310. struct cx88_core *core = dev->core;
  1311. u32 status, mask, count;
  1312. status = cx_read(MO_VID_INTSTAT);
  1313. mask = cx_read(MO_VID_INTMSK);
  1314. if (0 == (status & mask))
  1315. return;
  1316. cx_write(MO_VID_INTSTAT, status);
  1317. if (irq_debug || (status & mask & ~0xff))
  1318. cx88_print_irqbits(core->name, "irq vid",
  1319. cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
  1320. status, mask);
  1321. /* risc op code error */
  1322. if (status & (1 << 16)) {
  1323. printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
  1324. cx_clear(MO_VID_DMACNTRL, 0x11);
  1325. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1326. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1327. }
  1328. /* risc1 y */
  1329. if (status & 0x01) {
  1330. spin_lock(&dev->slock);
  1331. count = cx_read(MO_VIDY_GPCNT);
  1332. cx88_wakeup(core, &dev->vidq, count);
  1333. spin_unlock(&dev->slock);
  1334. }
  1335. /* risc1 vbi */
  1336. if (status & 0x08) {
  1337. spin_lock(&dev->slock);
  1338. count = cx_read(MO_VBI_GPCNT);
  1339. cx88_wakeup(core, &dev->vbiq, count);
  1340. spin_unlock(&dev->slock);
  1341. }
  1342. /* risc2 y */
  1343. if (status & 0x10) {
  1344. dprintk(2,"stopper video\n");
  1345. spin_lock(&dev->slock);
  1346. restart_video_queue(dev,&dev->vidq);
  1347. spin_unlock(&dev->slock);
  1348. }
  1349. /* risc2 vbi */
  1350. if (status & 0x80) {
  1351. dprintk(2,"stopper vbi\n");
  1352. spin_lock(&dev->slock);
  1353. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1354. spin_unlock(&dev->slock);
  1355. }
  1356. }
  1357. static irqreturn_t cx8800_irq(int irq, void *dev_id)
  1358. {
  1359. struct cx8800_dev *dev = dev_id;
  1360. struct cx88_core *core = dev->core;
  1361. u32 status;
  1362. int loop, handled = 0;
  1363. for (loop = 0; loop < 10; loop++) {
  1364. status = cx_read(MO_PCI_INTSTAT) &
  1365. (core->pci_irqmask | PCI_INT_VIDINT);
  1366. if (0 == status)
  1367. goto out;
  1368. cx_write(MO_PCI_INTSTAT, status);
  1369. handled = 1;
  1370. if (status & core->pci_irqmask)
  1371. cx88_core_irq(core,status);
  1372. if (status & PCI_INT_VIDINT)
  1373. cx8800_vid_irq(dev);
  1374. };
  1375. if (10 == loop) {
  1376. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  1377. core->name);
  1378. cx_write(MO_PCI_INTMSK,0);
  1379. }
  1380. out:
  1381. return IRQ_RETVAL(handled);
  1382. }
  1383. /* ----------------------------------------------------------- */
  1384. /* exported stuff */
  1385. static const struct file_operations video_fops =
  1386. {
  1387. .owner = THIS_MODULE,
  1388. .open = video_open,
  1389. .release = video_release,
  1390. .read = video_read,
  1391. .poll = video_poll,
  1392. .mmap = video_mmap,
  1393. .ioctl = video_ioctl2,
  1394. .compat_ioctl = v4l_compat_ioctl32,
  1395. .llseek = no_llseek,
  1396. };
  1397. static struct video_device cx8800_vbi_template;
  1398. static struct video_device cx8800_video_template =
  1399. {
  1400. .name = "cx8800-video",
  1401. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
  1402. .fops = &video_fops,
  1403. .minor = -1,
  1404. .vidioc_querycap = vidioc_querycap,
  1405. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  1406. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  1407. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  1408. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  1409. .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
  1410. .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
  1411. .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
  1412. .vidioc_reqbufs = vidioc_reqbufs,
  1413. .vidioc_querybuf = vidioc_querybuf,
  1414. .vidioc_qbuf = vidioc_qbuf,
  1415. .vidioc_dqbuf = vidioc_dqbuf,
  1416. .vidioc_s_std = vidioc_s_std,
  1417. .vidioc_enum_input = vidioc_enum_input,
  1418. .vidioc_g_input = vidioc_g_input,
  1419. .vidioc_s_input = vidioc_s_input,
  1420. .vidioc_queryctrl = vidioc_queryctrl,
  1421. .vidioc_g_ctrl = vidioc_g_ctrl,
  1422. .vidioc_s_ctrl = vidioc_s_ctrl,
  1423. .vidioc_streamon = vidioc_streamon,
  1424. .vidioc_streamoff = vidioc_streamoff,
  1425. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1426. .vidiocgmbuf = vidiocgmbuf,
  1427. #endif
  1428. .vidioc_g_tuner = vidioc_g_tuner,
  1429. .vidioc_s_tuner = vidioc_s_tuner,
  1430. .vidioc_g_frequency = vidioc_g_frequency,
  1431. .vidioc_s_frequency = vidioc_s_frequency,
  1432. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1433. .vidioc_g_register = vidioc_g_register,
  1434. .vidioc_s_register = vidioc_s_register,
  1435. #endif
  1436. .tvnorms = CX88_NORMS,
  1437. .current_norm = V4L2_STD_NTSC_M,
  1438. };
  1439. static const struct file_operations radio_fops =
  1440. {
  1441. .owner = THIS_MODULE,
  1442. .open = video_open,
  1443. .release = video_release,
  1444. .ioctl = video_ioctl2,
  1445. .compat_ioctl = v4l_compat_ioctl32,
  1446. .llseek = no_llseek,
  1447. };
  1448. static struct video_device cx8800_radio_template =
  1449. {
  1450. .name = "cx8800-radio",
  1451. .type = VID_TYPE_TUNER,
  1452. .fops = &radio_fops,
  1453. .minor = -1,
  1454. .vidioc_querycap = radio_querycap,
  1455. .vidioc_g_tuner = radio_g_tuner,
  1456. .vidioc_enum_input = radio_enum_input,
  1457. .vidioc_g_audio = radio_g_audio,
  1458. .vidioc_s_tuner = radio_s_tuner,
  1459. .vidioc_s_audio = radio_s_audio,
  1460. .vidioc_s_input = radio_s_input,
  1461. .vidioc_queryctrl = radio_queryctrl,
  1462. .vidioc_g_ctrl = vidioc_g_ctrl,
  1463. .vidioc_s_ctrl = vidioc_s_ctrl,
  1464. .vidioc_g_frequency = vidioc_g_frequency,
  1465. .vidioc_s_frequency = vidioc_s_frequency,
  1466. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1467. .vidioc_g_register = vidioc_g_register,
  1468. .vidioc_s_register = vidioc_s_register,
  1469. #endif
  1470. };
  1471. /* ----------------------------------------------------------- */
  1472. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1473. {
  1474. if (dev->radio_dev) {
  1475. if (-1 != dev->radio_dev->minor)
  1476. video_unregister_device(dev->radio_dev);
  1477. else
  1478. video_device_release(dev->radio_dev);
  1479. dev->radio_dev = NULL;
  1480. }
  1481. if (dev->vbi_dev) {
  1482. if (-1 != dev->vbi_dev->minor)
  1483. video_unregister_device(dev->vbi_dev);
  1484. else
  1485. video_device_release(dev->vbi_dev);
  1486. dev->vbi_dev = NULL;
  1487. }
  1488. if (dev->video_dev) {
  1489. if (-1 != dev->video_dev->minor)
  1490. video_unregister_device(dev->video_dev);
  1491. else
  1492. video_device_release(dev->video_dev);
  1493. dev->video_dev = NULL;
  1494. }
  1495. }
  1496. static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
  1497. const struct pci_device_id *pci_id)
  1498. {
  1499. struct cx8800_dev *dev;
  1500. struct cx88_core *core;
  1501. int err;
  1502. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1503. if (NULL == dev)
  1504. return -ENOMEM;
  1505. /* pci init */
  1506. dev->pci = pci_dev;
  1507. if (pci_enable_device(pci_dev)) {
  1508. err = -EIO;
  1509. goto fail_free;
  1510. }
  1511. core = cx88_core_get(dev->pci);
  1512. if (NULL == core) {
  1513. err = -EINVAL;
  1514. goto fail_free;
  1515. }
  1516. dev->core = core;
  1517. /* print pci info */
  1518. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  1519. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1520. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  1521. "latency: %d, mmio: 0x%llx\n", core->name,
  1522. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1523. dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
  1524. pci_set_master(pci_dev);
  1525. if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
  1526. printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
  1527. err = -EIO;
  1528. goto fail_core;
  1529. }
  1530. /* Initialize VBI template */
  1531. memcpy( &cx8800_vbi_template, &cx8800_video_template,
  1532. sizeof(cx8800_vbi_template) );
  1533. strcpy(cx8800_vbi_template.name,"cx8800-vbi");
  1534. cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
  1535. /* initialize driver struct */
  1536. spin_lock_init(&dev->slock);
  1537. core->tvnorm = cx8800_video_template.current_norm;
  1538. /* init video dma queues */
  1539. INIT_LIST_HEAD(&dev->vidq.active);
  1540. INIT_LIST_HEAD(&dev->vidq.queued);
  1541. dev->vidq.timeout.function = cx8800_vid_timeout;
  1542. dev->vidq.timeout.data = (unsigned long)dev;
  1543. init_timer(&dev->vidq.timeout);
  1544. cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
  1545. MO_VID_DMACNTRL,0x11,0x00);
  1546. /* init vbi dma queues */
  1547. INIT_LIST_HEAD(&dev->vbiq.active);
  1548. INIT_LIST_HEAD(&dev->vbiq.queued);
  1549. dev->vbiq.timeout.function = cx8800_vbi_timeout;
  1550. dev->vbiq.timeout.data = (unsigned long)dev;
  1551. init_timer(&dev->vbiq.timeout);
  1552. cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
  1553. MO_VID_DMACNTRL,0x88,0x00);
  1554. /* get irq */
  1555. err = request_irq(pci_dev->irq, cx8800_irq,
  1556. IRQF_SHARED | IRQF_DISABLED, core->name, dev);
  1557. if (err < 0) {
  1558. printk(KERN_ERR "%s/0: can't get IRQ %d\n",
  1559. core->name,pci_dev->irq);
  1560. goto fail_core;
  1561. }
  1562. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1563. /* load and configure helper modules */
  1564. if (TUNER_ABSENT != core->board.tuner_type)
  1565. request_module("tuner");
  1566. if (core->board.audio_chip == AUDIO_CHIP_WM8775)
  1567. request_module("wm8775");
  1568. switch (core->boardnr) {
  1569. case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
  1570. request_module("ir-kbd-i2c");
  1571. request_module("rtc-isl1208");
  1572. }
  1573. /* register v4l devices */
  1574. dev->video_dev = cx88_vdev_init(core,dev->pci,
  1575. &cx8800_video_template,"video");
  1576. err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
  1577. video_nr[core->nr]);
  1578. if (err < 0) {
  1579. printk(KERN_ERR "%s/0: can't register video device\n",
  1580. core->name);
  1581. goto fail_unreg;
  1582. }
  1583. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1584. core->name,dev->video_dev->minor & 0x1f);
  1585. dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
  1586. err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
  1587. vbi_nr[core->nr]);
  1588. if (err < 0) {
  1589. printk(KERN_ERR "%s/0: can't register vbi device\n",
  1590. core->name);
  1591. goto fail_unreg;
  1592. }
  1593. printk(KERN_INFO "%s/0: registered device vbi%d\n",
  1594. core->name,dev->vbi_dev->minor & 0x1f);
  1595. if (core->board.radio.type == CX88_RADIO) {
  1596. dev->radio_dev = cx88_vdev_init(core,dev->pci,
  1597. &cx8800_radio_template,"radio");
  1598. err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
  1599. radio_nr[core->nr]);
  1600. if (err < 0) {
  1601. printk(KERN_ERR "%s/0: can't register radio device\n",
  1602. core->name);
  1603. goto fail_unreg;
  1604. }
  1605. printk(KERN_INFO "%s/0: registered device radio%d\n",
  1606. core->name,dev->radio_dev->minor & 0x1f);
  1607. }
  1608. /* everything worked */
  1609. list_add_tail(&dev->devlist,&cx8800_devlist);
  1610. pci_set_drvdata(pci_dev,dev);
  1611. /* initial device configuration */
  1612. mutex_lock(&core->lock);
  1613. cx88_set_tvnorm(core,core->tvnorm);
  1614. init_controls(core);
  1615. cx88_video_mux(core,0);
  1616. mutex_unlock(&core->lock);
  1617. /* start tvaudio thread */
  1618. if (core->board.tuner_type != TUNER_ABSENT) {
  1619. core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
  1620. if (IS_ERR(core->kthread)) {
  1621. err = PTR_ERR(core->kthread);
  1622. printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
  1623. core->name, err);
  1624. }
  1625. }
  1626. return 0;
  1627. fail_unreg:
  1628. cx8800_unregister_video(dev);
  1629. free_irq(pci_dev->irq, dev);
  1630. fail_core:
  1631. cx88_core_put(core,dev->pci);
  1632. fail_free:
  1633. kfree(dev);
  1634. return err;
  1635. }
  1636. static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
  1637. {
  1638. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1639. struct cx88_core *core = dev->core;
  1640. /* stop thread */
  1641. if (core->kthread) {
  1642. kthread_stop(core->kthread);
  1643. core->kthread = NULL;
  1644. }
  1645. cx88_shutdown(core); /* FIXME */
  1646. pci_disable_device(pci_dev);
  1647. /* unregister stuff */
  1648. free_irq(pci_dev->irq, dev);
  1649. cx8800_unregister_video(dev);
  1650. pci_set_drvdata(pci_dev, NULL);
  1651. /* free memory */
  1652. btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
  1653. list_del(&dev->devlist);
  1654. cx88_core_put(core,dev->pci);
  1655. kfree(dev);
  1656. }
  1657. #ifdef CONFIG_PM
  1658. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1659. {
  1660. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1661. struct cx88_core *core = dev->core;
  1662. /* stop video+vbi capture */
  1663. spin_lock(&dev->slock);
  1664. if (!list_empty(&dev->vidq.active)) {
  1665. printk("%s/0: suspend video\n", core->name);
  1666. stop_video_dma(dev);
  1667. del_timer(&dev->vidq.timeout);
  1668. }
  1669. if (!list_empty(&dev->vbiq.active)) {
  1670. printk("%s/0: suspend vbi\n", core->name);
  1671. cx8800_stop_vbi_dma(dev);
  1672. del_timer(&dev->vbiq.timeout);
  1673. }
  1674. spin_unlock(&dev->slock);
  1675. if (core->ir)
  1676. cx88_ir_stop(core, core->ir);
  1677. /* FIXME -- shutdown device */
  1678. cx88_shutdown(core);
  1679. pci_save_state(pci_dev);
  1680. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  1681. pci_disable_device(pci_dev);
  1682. dev->state.disabled = 1;
  1683. }
  1684. return 0;
  1685. }
  1686. static int cx8800_resume(struct pci_dev *pci_dev)
  1687. {
  1688. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1689. struct cx88_core *core = dev->core;
  1690. int err;
  1691. if (dev->state.disabled) {
  1692. err=pci_enable_device(pci_dev);
  1693. if (err) {
  1694. printk(KERN_ERR "%s/0: can't enable device\n",
  1695. core->name);
  1696. return err;
  1697. }
  1698. dev->state.disabled = 0;
  1699. }
  1700. err= pci_set_power_state(pci_dev, PCI_D0);
  1701. if (err) {
  1702. printk(KERN_ERR "%s/0: can't set power state\n", core->name);
  1703. pci_disable_device(pci_dev);
  1704. dev->state.disabled = 1;
  1705. return err;
  1706. }
  1707. pci_restore_state(pci_dev);
  1708. /* FIXME: re-initialize hardware */
  1709. cx88_reset(core);
  1710. if (core->ir)
  1711. cx88_ir_start(core, core->ir);
  1712. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1713. /* restart video+vbi capture */
  1714. spin_lock(&dev->slock);
  1715. if (!list_empty(&dev->vidq.active)) {
  1716. printk("%s/0: resume video\n", core->name);
  1717. restart_video_queue(dev,&dev->vidq);
  1718. }
  1719. if (!list_empty(&dev->vbiq.active)) {
  1720. printk("%s/0: resume vbi\n", core->name);
  1721. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1722. }
  1723. spin_unlock(&dev->slock);
  1724. return 0;
  1725. }
  1726. #endif
  1727. /* ----------------------------------------------------------- */
  1728. static struct pci_device_id cx8800_pci_tbl[] = {
  1729. {
  1730. .vendor = 0x14f1,
  1731. .device = 0x8800,
  1732. .subvendor = PCI_ANY_ID,
  1733. .subdevice = PCI_ANY_ID,
  1734. },{
  1735. /* --- end of list --- */
  1736. }
  1737. };
  1738. MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
  1739. static struct pci_driver cx8800_pci_driver = {
  1740. .name = "cx8800",
  1741. .id_table = cx8800_pci_tbl,
  1742. .probe = cx8800_initdev,
  1743. .remove = __devexit_p(cx8800_finidev),
  1744. #ifdef CONFIG_PM
  1745. .suspend = cx8800_suspend,
  1746. .resume = cx8800_resume,
  1747. #endif
  1748. };
  1749. static int cx8800_init(void)
  1750. {
  1751. printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
  1752. (CX88_VERSION_CODE >> 16) & 0xff,
  1753. (CX88_VERSION_CODE >> 8) & 0xff,
  1754. CX88_VERSION_CODE & 0xff);
  1755. #ifdef SNAPSHOT
  1756. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  1757. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  1758. #endif
  1759. return pci_register_driver(&cx8800_pci_driver);
  1760. }
  1761. static void cx8800_fini(void)
  1762. {
  1763. pci_unregister_driver(&cx8800_pci_driver);
  1764. }
  1765. module_init(cx8800_init);
  1766. module_exit(cx8800_fini);
  1767. /* ----------------------------------------------------------- */
  1768. /*
  1769. * Local variables:
  1770. * c-basic-offset: 8
  1771. * End:
  1772. * 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
  1773. */