cx88-video.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  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 = STATE_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 = STATE_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 (STATE_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 = STATE_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 = STATE_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 = STATE_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 = STATE_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 = STATE_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 == STATE_DONE ||
  735. buf->vb.state == STATE_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. if (fh->vbiq.streaming)
  760. videobuf_streamoff(&fh->vbiq);
  761. if (fh->vbiq.reading)
  762. videobuf_read_stop(&fh->vbiq);
  763. res_free(dev,fh,RESOURCE_VBI);
  764. }
  765. videobuf_mmap_free(&fh->vidq);
  766. videobuf_mmap_free(&fh->vbiq);
  767. file->private_data = NULL;
  768. kfree(fh);
  769. cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
  770. return 0;
  771. }
  772. static int
  773. video_mmap(struct file *file, struct vm_area_struct * vma)
  774. {
  775. struct cx8800_fh *fh = file->private_data;
  776. return videobuf_mmap_mapper(get_queue(fh), vma);
  777. }
  778. /* ------------------------------------------------------------------ */
  779. /* VIDEO CTRL IOCTLS */
  780. int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
  781. {
  782. struct cx88_ctrl *c = NULL;
  783. u32 value;
  784. int i;
  785. for (i = 0; i < CX8800_CTLS; i++)
  786. if (cx8800_ctls[i].v.id == ctl->id)
  787. c = &cx8800_ctls[i];
  788. if (unlikely(NULL == c))
  789. return -EINVAL;
  790. value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
  791. switch (ctl->id) {
  792. case V4L2_CID_AUDIO_BALANCE:
  793. ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
  794. : (0x7f - (value & 0x7f));
  795. break;
  796. case V4L2_CID_AUDIO_VOLUME:
  797. ctl->value = 0x3f - (value & 0x3f);
  798. break;
  799. default:
  800. ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
  801. break;
  802. }
  803. dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  804. ctl->id, c->v.name, ctl->value, c->reg,
  805. value,c->mask, c->sreg ? " [shadowed]" : "");
  806. return 0;
  807. }
  808. EXPORT_SYMBOL(cx88_get_control);
  809. int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
  810. {
  811. struct cx88_ctrl *c = NULL;
  812. u32 value,mask;
  813. int i;
  814. for (i = 0; i < CX8800_CTLS; i++) {
  815. if (cx8800_ctls[i].v.id == ctl->id) {
  816. c = &cx8800_ctls[i];
  817. }
  818. }
  819. if (unlikely(NULL == c))
  820. return -EINVAL;
  821. if (ctl->value < c->v.minimum)
  822. ctl->value = c->v.minimum;
  823. if (ctl->value > c->v.maximum)
  824. ctl->value = c->v.maximum;
  825. mask=c->mask;
  826. switch (ctl->id) {
  827. case V4L2_CID_AUDIO_BALANCE:
  828. value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
  829. break;
  830. case V4L2_CID_AUDIO_VOLUME:
  831. value = 0x3f - (ctl->value & 0x3f);
  832. break;
  833. case V4L2_CID_SATURATION:
  834. /* special v_sat handling */
  835. value = ((ctl->value - c->off) << c->shift) & c->mask;
  836. if (core->tvnorm & V4L2_STD_SECAM) {
  837. /* For SECAM, both U and V sat should be equal */
  838. value=value<<8|value;
  839. } else {
  840. /* Keeps U Saturation proportional to V Sat */
  841. value=(value*0x5a)/0x7f<<8|value;
  842. }
  843. mask=0xffff;
  844. break;
  845. default:
  846. value = ((ctl->value - c->off) << c->shift) & c->mask;
  847. break;
  848. }
  849. dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  850. ctl->id, c->v.name, ctl->value, c->reg, value,
  851. mask, c->sreg ? " [shadowed]" : "");
  852. if (c->sreg) {
  853. cx_sandor(c->sreg, c->reg, mask, value);
  854. } else {
  855. cx_andor(c->reg, mask, value);
  856. }
  857. return 0;
  858. }
  859. EXPORT_SYMBOL(cx88_set_control);
  860. static void init_controls(struct cx88_core *core)
  861. {
  862. struct v4l2_control ctrl;
  863. int i;
  864. for (i = 0; i < CX8800_CTLS; i++) {
  865. ctrl.id=cx8800_ctls[i].v.id;
  866. ctrl.value=cx8800_ctls[i].v.default_value;
  867. cx88_set_control(core, &ctrl);
  868. }
  869. }
  870. /* ------------------------------------------------------------------ */
  871. /* VIDEO IOCTLS */
  872. static int vidioc_g_fmt_cap (struct file *file, void *priv,
  873. struct v4l2_format *f)
  874. {
  875. struct cx8800_fh *fh = priv;
  876. f->fmt.pix.width = fh->width;
  877. f->fmt.pix.height = fh->height;
  878. f->fmt.pix.field = fh->vidq.field;
  879. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  880. f->fmt.pix.bytesperline =
  881. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  882. f->fmt.pix.sizeimage =
  883. f->fmt.pix.height * f->fmt.pix.bytesperline;
  884. return 0;
  885. }
  886. static int vidioc_try_fmt_cap (struct file *file, void *priv,
  887. struct v4l2_format *f)
  888. {
  889. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  890. struct cx8800_fmt *fmt;
  891. enum v4l2_field field;
  892. unsigned int maxw, maxh;
  893. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  894. if (NULL == fmt)
  895. return -EINVAL;
  896. field = f->fmt.pix.field;
  897. maxw = norm_maxw(core->tvnorm);
  898. maxh = norm_maxh(core->tvnorm);
  899. if (V4L2_FIELD_ANY == field) {
  900. field = (f->fmt.pix.height > maxh/2)
  901. ? V4L2_FIELD_INTERLACED
  902. : V4L2_FIELD_BOTTOM;
  903. }
  904. switch (field) {
  905. case V4L2_FIELD_TOP:
  906. case V4L2_FIELD_BOTTOM:
  907. maxh = maxh / 2;
  908. break;
  909. case V4L2_FIELD_INTERLACED:
  910. break;
  911. default:
  912. return -EINVAL;
  913. }
  914. f->fmt.pix.field = field;
  915. if (f->fmt.pix.height < 32)
  916. f->fmt.pix.height = 32;
  917. if (f->fmt.pix.height > maxh)
  918. f->fmt.pix.height = maxh;
  919. if (f->fmt.pix.width < 48)
  920. f->fmt.pix.width = 48;
  921. if (f->fmt.pix.width > maxw)
  922. f->fmt.pix.width = maxw;
  923. f->fmt.pix.width &= ~0x03;
  924. f->fmt.pix.bytesperline =
  925. (f->fmt.pix.width * fmt->depth) >> 3;
  926. f->fmt.pix.sizeimage =
  927. f->fmt.pix.height * f->fmt.pix.bytesperline;
  928. return 0;
  929. }
  930. static int vidioc_s_fmt_cap (struct file *file, void *priv,
  931. struct v4l2_format *f)
  932. {
  933. struct cx8800_fh *fh = priv;
  934. int err = vidioc_try_fmt_cap (file,priv,f);
  935. if (0 != err)
  936. return err;
  937. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  938. fh->width = f->fmt.pix.width;
  939. fh->height = f->fmt.pix.height;
  940. fh->vidq.field = f->fmt.pix.field;
  941. return 0;
  942. }
  943. static int vidioc_querycap (struct file *file, void *priv,
  944. struct v4l2_capability *cap)
  945. {
  946. struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
  947. struct cx88_core *core = dev->core;
  948. strcpy(cap->driver, "cx8800");
  949. strlcpy(cap->card, core->board.name, sizeof(cap->card));
  950. sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
  951. cap->version = CX88_VERSION_CODE;
  952. cap->capabilities =
  953. V4L2_CAP_VIDEO_CAPTURE |
  954. V4L2_CAP_READWRITE |
  955. V4L2_CAP_STREAMING |
  956. V4L2_CAP_VBI_CAPTURE;
  957. if (UNSET != core->board.tuner_type)
  958. cap->capabilities |= V4L2_CAP_TUNER;
  959. return 0;
  960. }
  961. static int vidioc_enum_fmt_cap (struct file *file, void *priv,
  962. struct v4l2_fmtdesc *f)
  963. {
  964. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  965. return -EINVAL;
  966. strlcpy(f->description,formats[f->index].name,sizeof(f->description));
  967. f->pixelformat = formats[f->index].fourcc;
  968. return 0;
  969. }
  970. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  971. static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
  972. {
  973. struct cx8800_fh *fh = priv;
  974. return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
  975. }
  976. #endif
  977. static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
  978. {
  979. struct cx8800_fh *fh = priv;
  980. return (videobuf_reqbufs(get_queue(fh), p));
  981. }
  982. static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
  983. {
  984. struct cx8800_fh *fh = priv;
  985. return (videobuf_querybuf(get_queue(fh), p));
  986. }
  987. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  988. {
  989. struct cx8800_fh *fh = priv;
  990. return (videobuf_qbuf(get_queue(fh), p));
  991. }
  992. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  993. {
  994. struct cx8800_fh *fh = priv;
  995. return (videobuf_dqbuf(get_queue(fh), p,
  996. file->f_flags & O_NONBLOCK));
  997. }
  998. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  999. {
  1000. struct cx8800_fh *fh = priv;
  1001. struct cx8800_dev *dev = fh->dev;
  1002. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  1003. return -EINVAL;
  1004. if (unlikely(i != fh->type))
  1005. return -EINVAL;
  1006. if (unlikely(!res_get(dev,fh,get_ressource(fh))))
  1007. return -EBUSY;
  1008. return videobuf_streamon(get_queue(fh));
  1009. }
  1010. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1011. {
  1012. struct cx8800_fh *fh = priv;
  1013. struct cx8800_dev *dev = fh->dev;
  1014. int err, res;
  1015. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1016. return -EINVAL;
  1017. if (i != fh->type)
  1018. return -EINVAL;
  1019. res = get_ressource(fh);
  1020. err = videobuf_streamoff(get_queue(fh));
  1021. if (err < 0)
  1022. return err;
  1023. res_free(dev,fh,res);
  1024. return 0;
  1025. }
  1026. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
  1027. {
  1028. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1029. mutex_lock(&core->lock);
  1030. cx88_set_tvnorm(core,*tvnorms);
  1031. mutex_unlock(&core->lock);
  1032. return 0;
  1033. }
  1034. /* only one input in this sample driver */
  1035. int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
  1036. {
  1037. static const char *iname[] = {
  1038. [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
  1039. [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
  1040. [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
  1041. [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
  1042. [ CX88_VMUX_SVIDEO ] = "S-Video",
  1043. [ CX88_VMUX_TELEVISION ] = "Television",
  1044. [ CX88_VMUX_CABLE ] = "Cable TV",
  1045. [ CX88_VMUX_DVB ] = "DVB",
  1046. [ CX88_VMUX_DEBUG ] = "for debug only",
  1047. };
  1048. unsigned int n;
  1049. n = i->index;
  1050. if (n >= 4)
  1051. return -EINVAL;
  1052. if (0 == INPUT(n).type)
  1053. return -EINVAL;
  1054. memset(i,0,sizeof(*i));
  1055. i->index = n;
  1056. i->type = V4L2_INPUT_TYPE_CAMERA;
  1057. strcpy(i->name,iname[INPUT(n).type]);
  1058. if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
  1059. (CX88_VMUX_CABLE == INPUT(n).type))
  1060. i->type = V4L2_INPUT_TYPE_TUNER;
  1061. i->std = CX88_NORMS;
  1062. return 0;
  1063. }
  1064. EXPORT_SYMBOL(cx88_enum_input);
  1065. static int vidioc_enum_input (struct file *file, void *priv,
  1066. struct v4l2_input *i)
  1067. {
  1068. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1069. return cx88_enum_input (core,i);
  1070. }
  1071. static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
  1072. {
  1073. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1074. *i = core->input;
  1075. return 0;
  1076. }
  1077. static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
  1078. {
  1079. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1080. if (i >= 4)
  1081. return -EINVAL;
  1082. mutex_lock(&core->lock);
  1083. cx88_newstation(core);
  1084. cx88_video_mux(core,i);
  1085. mutex_unlock(&core->lock);
  1086. return 0;
  1087. }
  1088. static int vidioc_queryctrl (struct file *file, void *priv,
  1089. struct v4l2_queryctrl *qctrl)
  1090. {
  1091. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1092. if (unlikely(qctrl->id == 0))
  1093. return -EINVAL;
  1094. return cx8800_ctrl_query(qctrl);
  1095. }
  1096. static int vidioc_g_ctrl (struct file *file, void *priv,
  1097. struct v4l2_control *ctl)
  1098. {
  1099. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1100. return
  1101. cx88_get_control(core,ctl);
  1102. }
  1103. static int vidioc_s_ctrl (struct file *file, void *priv,
  1104. struct v4l2_control *ctl)
  1105. {
  1106. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1107. return
  1108. cx88_set_control(core,ctl);
  1109. }
  1110. static int vidioc_g_tuner (struct file *file, void *priv,
  1111. struct v4l2_tuner *t)
  1112. {
  1113. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1114. u32 reg;
  1115. if (unlikely(UNSET == core->board.tuner_type))
  1116. return -EINVAL;
  1117. if (0 != t->index)
  1118. return -EINVAL;
  1119. strcpy(t->name, "Television");
  1120. t->type = V4L2_TUNER_ANALOG_TV;
  1121. t->capability = V4L2_TUNER_CAP_NORM;
  1122. t->rangehigh = 0xffffffffUL;
  1123. cx88_get_stereo(core ,t);
  1124. reg = cx_read(MO_DEVICE_STATUS);
  1125. t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
  1126. return 0;
  1127. }
  1128. static int vidioc_s_tuner (struct file *file, void *priv,
  1129. struct v4l2_tuner *t)
  1130. {
  1131. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1132. if (UNSET == core->board.tuner_type)
  1133. return -EINVAL;
  1134. if (0 != t->index)
  1135. return -EINVAL;
  1136. cx88_set_stereo(core, t->audmode, 1);
  1137. return 0;
  1138. }
  1139. static int vidioc_g_frequency (struct file *file, void *priv,
  1140. struct v4l2_frequency *f)
  1141. {
  1142. struct cx8800_fh *fh = priv;
  1143. struct cx88_core *core = fh->dev->core;
  1144. if (unlikely(UNSET == core->board.tuner_type))
  1145. return -EINVAL;
  1146. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1147. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1148. f->frequency = core->freq;
  1149. cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
  1150. return 0;
  1151. }
  1152. int cx88_set_freq (struct cx88_core *core,
  1153. struct v4l2_frequency *f)
  1154. {
  1155. if (unlikely(UNSET == core->board.tuner_type))
  1156. return -EINVAL;
  1157. if (unlikely(f->tuner != 0))
  1158. return -EINVAL;
  1159. mutex_lock(&core->lock);
  1160. core->freq = f->frequency;
  1161. cx88_newstation(core);
  1162. cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
  1163. /* When changing channels it is required to reset TVAUDIO */
  1164. msleep (10);
  1165. cx88_set_tvaudio(core);
  1166. mutex_unlock(&core->lock);
  1167. return 0;
  1168. }
  1169. EXPORT_SYMBOL(cx88_set_freq);
  1170. static int vidioc_s_frequency (struct file *file, void *priv,
  1171. struct v4l2_frequency *f)
  1172. {
  1173. struct cx8800_fh *fh = priv;
  1174. struct cx88_core *core = fh->dev->core;
  1175. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1176. return -EINVAL;
  1177. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1178. return -EINVAL;
  1179. return
  1180. cx88_set_freq (core,f);
  1181. }
  1182. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1183. static int vidioc_g_register (struct file *file, void *fh,
  1184. struct v4l2_register *reg)
  1185. {
  1186. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1187. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1188. return -EINVAL;
  1189. /* cx2388x has a 24-bit register space */
  1190. reg->val = cx_read(reg->reg&0xffffff);
  1191. return 0;
  1192. }
  1193. static int vidioc_s_register (struct file *file, void *fh,
  1194. struct v4l2_register *reg)
  1195. {
  1196. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1197. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1198. return -EINVAL;
  1199. cx_write(reg->reg&0xffffff, reg->val);
  1200. return 0;
  1201. }
  1202. #endif
  1203. /* ----------------------------------------------------------- */
  1204. /* RADIO ESPECIFIC IOCTLS */
  1205. /* ----------------------------------------------------------- */
  1206. static int radio_querycap (struct file *file, void *priv,
  1207. struct v4l2_capability *cap)
  1208. {
  1209. struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
  1210. struct cx88_core *core = dev->core;
  1211. strcpy(cap->driver, "cx8800");
  1212. strlcpy(cap->card, core->board.name, sizeof(cap->card));
  1213. sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
  1214. cap->version = CX88_VERSION_CODE;
  1215. cap->capabilities = V4L2_CAP_TUNER;
  1216. return 0;
  1217. }
  1218. static int radio_g_tuner (struct file *file, void *priv,
  1219. struct v4l2_tuner *t)
  1220. {
  1221. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1222. if (unlikely(t->index > 0))
  1223. return -EINVAL;
  1224. strcpy(t->name, "Radio");
  1225. t->type = V4L2_TUNER_RADIO;
  1226. cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
  1227. return 0;
  1228. }
  1229. static int radio_enum_input (struct file *file, void *priv,
  1230. struct v4l2_input *i)
  1231. {
  1232. if (i->index != 0)
  1233. return -EINVAL;
  1234. strcpy(i->name,"Radio");
  1235. i->type = V4L2_INPUT_TYPE_TUNER;
  1236. return 0;
  1237. }
  1238. static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
  1239. {
  1240. if (unlikely(a->index))
  1241. return -EINVAL;
  1242. memset(a,0,sizeof(*a));
  1243. strcpy(a->name,"Radio");
  1244. return 0;
  1245. }
  1246. /* FIXME: Should add a standard for radio */
  1247. static int radio_s_tuner (struct file *file, void *priv,
  1248. struct v4l2_tuner *t)
  1249. {
  1250. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1251. if (0 != t->index)
  1252. return -EINVAL;
  1253. cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
  1254. return 0;
  1255. }
  1256. static int radio_s_audio (struct file *file, void *fh,
  1257. struct v4l2_audio *a)
  1258. {
  1259. return 0;
  1260. }
  1261. static int radio_s_input (struct file *file, void *fh, unsigned int i)
  1262. {
  1263. return 0;
  1264. }
  1265. static int radio_queryctrl (struct file *file, void *priv,
  1266. struct v4l2_queryctrl *c)
  1267. {
  1268. int i;
  1269. if (c->id < V4L2_CID_BASE ||
  1270. c->id >= V4L2_CID_LASTP1)
  1271. return -EINVAL;
  1272. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1273. for (i = 0; i < CX8800_CTLS; i++)
  1274. if (cx8800_ctls[i].v.id == c->id)
  1275. break;
  1276. *c = cx8800_ctls[i].v;
  1277. } else
  1278. *c = no_ctl;
  1279. return 0;
  1280. }
  1281. /* ----------------------------------------------------------- */
  1282. static void cx8800_vid_timeout(unsigned long data)
  1283. {
  1284. struct cx8800_dev *dev = (struct cx8800_dev*)data;
  1285. struct cx88_core *core = dev->core;
  1286. struct cx88_dmaqueue *q = &dev->vidq;
  1287. struct cx88_buffer *buf;
  1288. unsigned long flags;
  1289. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1290. cx_clear(MO_VID_DMACNTRL, 0x11);
  1291. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1292. spin_lock_irqsave(&dev->slock,flags);
  1293. while (!list_empty(&q->active)) {
  1294. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  1295. list_del(&buf->vb.queue);
  1296. buf->vb.state = STATE_ERROR;
  1297. wake_up(&buf->vb.done);
  1298. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
  1299. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  1300. }
  1301. restart_video_queue(dev,q);
  1302. spin_unlock_irqrestore(&dev->slock,flags);
  1303. }
  1304. static char *cx88_vid_irqs[32] = {
  1305. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  1306. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  1307. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  1308. "y_sync", "u_sync", "v_sync", "vbi_sync",
  1309. "opc_err", "par_err", "rip_err", "pci_abort",
  1310. };
  1311. static void cx8800_vid_irq(struct cx8800_dev *dev)
  1312. {
  1313. struct cx88_core *core = dev->core;
  1314. u32 status, mask, count;
  1315. status = cx_read(MO_VID_INTSTAT);
  1316. mask = cx_read(MO_VID_INTMSK);
  1317. if (0 == (status & mask))
  1318. return;
  1319. cx_write(MO_VID_INTSTAT, status);
  1320. if (irq_debug || (status & mask & ~0xff))
  1321. cx88_print_irqbits(core->name, "irq vid",
  1322. cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
  1323. status, mask);
  1324. /* risc op code error */
  1325. if (status & (1 << 16)) {
  1326. printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
  1327. cx_clear(MO_VID_DMACNTRL, 0x11);
  1328. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1329. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1330. }
  1331. /* risc1 y */
  1332. if (status & 0x01) {
  1333. spin_lock(&dev->slock);
  1334. count = cx_read(MO_VIDY_GPCNT);
  1335. cx88_wakeup(core, &dev->vidq, count);
  1336. spin_unlock(&dev->slock);
  1337. }
  1338. /* risc1 vbi */
  1339. if (status & 0x08) {
  1340. spin_lock(&dev->slock);
  1341. count = cx_read(MO_VBI_GPCNT);
  1342. cx88_wakeup(core, &dev->vbiq, count);
  1343. spin_unlock(&dev->slock);
  1344. }
  1345. /* risc2 y */
  1346. if (status & 0x10) {
  1347. dprintk(2,"stopper video\n");
  1348. spin_lock(&dev->slock);
  1349. restart_video_queue(dev,&dev->vidq);
  1350. spin_unlock(&dev->slock);
  1351. }
  1352. /* risc2 vbi */
  1353. if (status & 0x80) {
  1354. dprintk(2,"stopper vbi\n");
  1355. spin_lock(&dev->slock);
  1356. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1357. spin_unlock(&dev->slock);
  1358. }
  1359. }
  1360. static irqreturn_t cx8800_irq(int irq, void *dev_id)
  1361. {
  1362. struct cx8800_dev *dev = dev_id;
  1363. struct cx88_core *core = dev->core;
  1364. u32 status;
  1365. int loop, handled = 0;
  1366. for (loop = 0; loop < 10; loop++) {
  1367. status = cx_read(MO_PCI_INTSTAT) &
  1368. (core->pci_irqmask | PCI_INT_VIDINT);
  1369. if (0 == status)
  1370. goto out;
  1371. cx_write(MO_PCI_INTSTAT, status);
  1372. handled = 1;
  1373. if (status & core->pci_irqmask)
  1374. cx88_core_irq(core,status);
  1375. if (status & PCI_INT_VIDINT)
  1376. cx8800_vid_irq(dev);
  1377. };
  1378. if (10 == loop) {
  1379. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  1380. core->name);
  1381. cx_write(MO_PCI_INTMSK,0);
  1382. }
  1383. out:
  1384. return IRQ_RETVAL(handled);
  1385. }
  1386. /* ----------------------------------------------------------- */
  1387. /* exported stuff */
  1388. static const struct file_operations video_fops =
  1389. {
  1390. .owner = THIS_MODULE,
  1391. .open = video_open,
  1392. .release = video_release,
  1393. .read = video_read,
  1394. .poll = video_poll,
  1395. .mmap = video_mmap,
  1396. .ioctl = video_ioctl2,
  1397. .compat_ioctl = v4l_compat_ioctl32,
  1398. .llseek = no_llseek,
  1399. };
  1400. static struct video_device cx8800_vbi_template;
  1401. static struct video_device cx8800_video_template =
  1402. {
  1403. .name = "cx8800-video",
  1404. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
  1405. .fops = &video_fops,
  1406. .minor = -1,
  1407. .vidioc_querycap = vidioc_querycap,
  1408. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  1409. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  1410. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  1411. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  1412. .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
  1413. .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
  1414. .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
  1415. .vidioc_reqbufs = vidioc_reqbufs,
  1416. .vidioc_querybuf = vidioc_querybuf,
  1417. .vidioc_qbuf = vidioc_qbuf,
  1418. .vidioc_dqbuf = vidioc_dqbuf,
  1419. .vidioc_s_std = vidioc_s_std,
  1420. .vidioc_enum_input = vidioc_enum_input,
  1421. .vidioc_g_input = vidioc_g_input,
  1422. .vidioc_s_input = vidioc_s_input,
  1423. .vidioc_queryctrl = vidioc_queryctrl,
  1424. .vidioc_g_ctrl = vidioc_g_ctrl,
  1425. .vidioc_s_ctrl = vidioc_s_ctrl,
  1426. .vidioc_streamon = vidioc_streamon,
  1427. .vidioc_streamoff = vidioc_streamoff,
  1428. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1429. .vidiocgmbuf = vidiocgmbuf,
  1430. #endif
  1431. .vidioc_g_tuner = vidioc_g_tuner,
  1432. .vidioc_s_tuner = vidioc_s_tuner,
  1433. .vidioc_g_frequency = vidioc_g_frequency,
  1434. .vidioc_s_frequency = vidioc_s_frequency,
  1435. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1436. .vidioc_g_register = vidioc_g_register,
  1437. .vidioc_s_register = vidioc_s_register,
  1438. #endif
  1439. .tvnorms = CX88_NORMS,
  1440. .current_norm = V4L2_STD_NTSC_M,
  1441. };
  1442. static const struct file_operations radio_fops =
  1443. {
  1444. .owner = THIS_MODULE,
  1445. .open = video_open,
  1446. .release = video_release,
  1447. .ioctl = video_ioctl2,
  1448. .compat_ioctl = v4l_compat_ioctl32,
  1449. .llseek = no_llseek,
  1450. };
  1451. static struct video_device cx8800_radio_template =
  1452. {
  1453. .name = "cx8800-radio",
  1454. .type = VID_TYPE_TUNER,
  1455. .fops = &radio_fops,
  1456. .minor = -1,
  1457. .vidioc_querycap = radio_querycap,
  1458. .vidioc_g_tuner = radio_g_tuner,
  1459. .vidioc_enum_input = radio_enum_input,
  1460. .vidioc_g_audio = radio_g_audio,
  1461. .vidioc_s_tuner = radio_s_tuner,
  1462. .vidioc_s_audio = radio_s_audio,
  1463. .vidioc_s_input = radio_s_input,
  1464. .vidioc_queryctrl = radio_queryctrl,
  1465. .vidioc_g_ctrl = vidioc_g_ctrl,
  1466. .vidioc_s_ctrl = vidioc_s_ctrl,
  1467. .vidioc_g_frequency = vidioc_g_frequency,
  1468. .vidioc_s_frequency = vidioc_s_frequency,
  1469. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1470. .vidioc_g_register = vidioc_g_register,
  1471. .vidioc_s_register = vidioc_s_register,
  1472. #endif
  1473. };
  1474. /* ----------------------------------------------------------- */
  1475. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1476. {
  1477. if (dev->radio_dev) {
  1478. if (-1 != dev->radio_dev->minor)
  1479. video_unregister_device(dev->radio_dev);
  1480. else
  1481. video_device_release(dev->radio_dev);
  1482. dev->radio_dev = NULL;
  1483. }
  1484. if (dev->vbi_dev) {
  1485. if (-1 != dev->vbi_dev->minor)
  1486. video_unregister_device(dev->vbi_dev);
  1487. else
  1488. video_device_release(dev->vbi_dev);
  1489. dev->vbi_dev = NULL;
  1490. }
  1491. if (dev->video_dev) {
  1492. if (-1 != dev->video_dev->minor)
  1493. video_unregister_device(dev->video_dev);
  1494. else
  1495. video_device_release(dev->video_dev);
  1496. dev->video_dev = NULL;
  1497. }
  1498. }
  1499. static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
  1500. const struct pci_device_id *pci_id)
  1501. {
  1502. struct cx8800_dev *dev;
  1503. struct cx88_core *core;
  1504. int err;
  1505. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1506. if (NULL == dev)
  1507. return -ENOMEM;
  1508. /* pci init */
  1509. dev->pci = pci_dev;
  1510. if (pci_enable_device(pci_dev)) {
  1511. err = -EIO;
  1512. goto fail_free;
  1513. }
  1514. core = cx88_core_get(dev->pci);
  1515. if (NULL == core) {
  1516. err = -EINVAL;
  1517. goto fail_free;
  1518. }
  1519. dev->core = core;
  1520. /* print pci info */
  1521. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  1522. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1523. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  1524. "latency: %d, mmio: 0x%llx\n", core->name,
  1525. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1526. dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
  1527. pci_set_master(pci_dev);
  1528. if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
  1529. printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
  1530. err = -EIO;
  1531. goto fail_core;
  1532. }
  1533. /* Initialize VBI template */
  1534. memcpy( &cx8800_vbi_template, &cx8800_video_template,
  1535. sizeof(cx8800_vbi_template) );
  1536. strcpy(cx8800_vbi_template.name,"cx8800-vbi");
  1537. cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
  1538. /* initialize driver struct */
  1539. spin_lock_init(&dev->slock);
  1540. core->tvnorm = cx8800_video_template.current_norm;
  1541. /* init video dma queues */
  1542. INIT_LIST_HEAD(&dev->vidq.active);
  1543. INIT_LIST_HEAD(&dev->vidq.queued);
  1544. dev->vidq.timeout.function = cx8800_vid_timeout;
  1545. dev->vidq.timeout.data = (unsigned long)dev;
  1546. init_timer(&dev->vidq.timeout);
  1547. cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
  1548. MO_VID_DMACNTRL,0x11,0x00);
  1549. /* init vbi dma queues */
  1550. INIT_LIST_HEAD(&dev->vbiq.active);
  1551. INIT_LIST_HEAD(&dev->vbiq.queued);
  1552. dev->vbiq.timeout.function = cx8800_vbi_timeout;
  1553. dev->vbiq.timeout.data = (unsigned long)dev;
  1554. init_timer(&dev->vbiq.timeout);
  1555. cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
  1556. MO_VID_DMACNTRL,0x88,0x00);
  1557. /* get irq */
  1558. err = request_irq(pci_dev->irq, cx8800_irq,
  1559. IRQF_SHARED | IRQF_DISABLED, core->name, dev);
  1560. if (err < 0) {
  1561. printk(KERN_ERR "%s/0: can't get IRQ %d\n",
  1562. core->name,pci_dev->irq);
  1563. goto fail_core;
  1564. }
  1565. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1566. /* load and configure helper modules */
  1567. if (TUNER_ABSENT != core->board.tuner_type)
  1568. request_module("tuner");
  1569. if (core->board.audio_chip == AUDIO_CHIP_WM8775)
  1570. request_module("wm8775");
  1571. switch (core->boardnr) {
  1572. case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
  1573. request_module("ir-kbd-i2c");
  1574. request_module("rtc-isl1208");
  1575. }
  1576. /* register v4l devices */
  1577. dev->video_dev = cx88_vdev_init(core,dev->pci,
  1578. &cx8800_video_template,"video");
  1579. err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
  1580. video_nr[core->nr]);
  1581. if (err < 0) {
  1582. printk(KERN_ERR "%s/0: can't register video device\n",
  1583. core->name);
  1584. goto fail_unreg;
  1585. }
  1586. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1587. core->name,dev->video_dev->minor & 0x1f);
  1588. dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
  1589. err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
  1590. vbi_nr[core->nr]);
  1591. if (err < 0) {
  1592. printk(KERN_ERR "%s/0: can't register vbi device\n",
  1593. core->name);
  1594. goto fail_unreg;
  1595. }
  1596. printk(KERN_INFO "%s/0: registered device vbi%d\n",
  1597. core->name,dev->vbi_dev->minor & 0x1f);
  1598. if (core->board.radio.type == CX88_RADIO) {
  1599. dev->radio_dev = cx88_vdev_init(core,dev->pci,
  1600. &cx8800_radio_template,"radio");
  1601. err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
  1602. radio_nr[core->nr]);
  1603. if (err < 0) {
  1604. printk(KERN_ERR "%s/0: can't register radio device\n",
  1605. core->name);
  1606. goto fail_unreg;
  1607. }
  1608. printk(KERN_INFO "%s/0: registered device radio%d\n",
  1609. core->name,dev->radio_dev->minor & 0x1f);
  1610. }
  1611. /* everything worked */
  1612. list_add_tail(&dev->devlist,&cx8800_devlist);
  1613. pci_set_drvdata(pci_dev,dev);
  1614. /* initial device configuration */
  1615. mutex_lock(&core->lock);
  1616. cx88_set_tvnorm(core,core->tvnorm);
  1617. init_controls(core);
  1618. cx88_video_mux(core,0);
  1619. mutex_unlock(&core->lock);
  1620. /* start tvaudio thread */
  1621. if (core->board.tuner_type != TUNER_ABSENT) {
  1622. core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
  1623. if (IS_ERR(core->kthread)) {
  1624. err = PTR_ERR(core->kthread);
  1625. printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
  1626. core->name, err);
  1627. }
  1628. }
  1629. return 0;
  1630. fail_unreg:
  1631. cx8800_unregister_video(dev);
  1632. free_irq(pci_dev->irq, dev);
  1633. fail_core:
  1634. cx88_core_put(core,dev->pci);
  1635. fail_free:
  1636. kfree(dev);
  1637. return err;
  1638. }
  1639. static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
  1640. {
  1641. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1642. struct cx88_core *core = dev->core;
  1643. /* stop thread */
  1644. if (core->kthread) {
  1645. kthread_stop(core->kthread);
  1646. core->kthread = NULL;
  1647. }
  1648. cx88_shutdown(core); /* FIXME */
  1649. pci_disable_device(pci_dev);
  1650. /* unregister stuff */
  1651. free_irq(pci_dev->irq, dev);
  1652. cx8800_unregister_video(dev);
  1653. pci_set_drvdata(pci_dev, NULL);
  1654. /* free memory */
  1655. btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
  1656. list_del(&dev->devlist);
  1657. cx88_core_put(core,dev->pci);
  1658. kfree(dev);
  1659. }
  1660. #ifdef CONFIG_PM
  1661. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1662. {
  1663. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1664. struct cx88_core *core = dev->core;
  1665. /* stop video+vbi capture */
  1666. spin_lock(&dev->slock);
  1667. if (!list_empty(&dev->vidq.active)) {
  1668. printk("%s/0: suspend video\n", core->name);
  1669. stop_video_dma(dev);
  1670. del_timer(&dev->vidq.timeout);
  1671. }
  1672. if (!list_empty(&dev->vbiq.active)) {
  1673. printk("%s/0: suspend vbi\n", core->name);
  1674. cx8800_stop_vbi_dma(dev);
  1675. del_timer(&dev->vbiq.timeout);
  1676. }
  1677. spin_unlock(&dev->slock);
  1678. if (core->ir)
  1679. cx88_ir_stop(core, core->ir);
  1680. /* FIXME -- shutdown device */
  1681. cx88_shutdown(core);
  1682. pci_save_state(pci_dev);
  1683. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  1684. pci_disable_device(pci_dev);
  1685. dev->state.disabled = 1;
  1686. }
  1687. return 0;
  1688. }
  1689. static int cx8800_resume(struct pci_dev *pci_dev)
  1690. {
  1691. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1692. struct cx88_core *core = dev->core;
  1693. int err;
  1694. if (dev->state.disabled) {
  1695. err=pci_enable_device(pci_dev);
  1696. if (err) {
  1697. printk(KERN_ERR "%s/0: can't enable device\n",
  1698. core->name);
  1699. return err;
  1700. }
  1701. dev->state.disabled = 0;
  1702. }
  1703. err= pci_set_power_state(pci_dev, PCI_D0);
  1704. if (err) {
  1705. printk(KERN_ERR "%s/0: can't set power state\n", core->name);
  1706. pci_disable_device(pci_dev);
  1707. dev->state.disabled = 1;
  1708. return err;
  1709. }
  1710. pci_restore_state(pci_dev);
  1711. /* FIXME: re-initialize hardware */
  1712. cx88_reset(core);
  1713. if (core->ir)
  1714. cx88_ir_start(core, core->ir);
  1715. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1716. /* restart video+vbi capture */
  1717. spin_lock(&dev->slock);
  1718. if (!list_empty(&dev->vidq.active)) {
  1719. printk("%s/0: resume video\n", core->name);
  1720. restart_video_queue(dev,&dev->vidq);
  1721. }
  1722. if (!list_empty(&dev->vbiq.active)) {
  1723. printk("%s/0: resume vbi\n", core->name);
  1724. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1725. }
  1726. spin_unlock(&dev->slock);
  1727. return 0;
  1728. }
  1729. #endif
  1730. /* ----------------------------------------------------------- */
  1731. static struct pci_device_id cx8800_pci_tbl[] = {
  1732. {
  1733. .vendor = 0x14f1,
  1734. .device = 0x8800,
  1735. .subvendor = PCI_ANY_ID,
  1736. .subdevice = PCI_ANY_ID,
  1737. },{
  1738. /* --- end of list --- */
  1739. }
  1740. };
  1741. MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
  1742. static struct pci_driver cx8800_pci_driver = {
  1743. .name = "cx8800",
  1744. .id_table = cx8800_pci_tbl,
  1745. .probe = cx8800_initdev,
  1746. .remove = __devexit_p(cx8800_finidev),
  1747. #ifdef CONFIG_PM
  1748. .suspend = cx8800_suspend,
  1749. .resume = cx8800_resume,
  1750. #endif
  1751. };
  1752. static int cx8800_init(void)
  1753. {
  1754. printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
  1755. (CX88_VERSION_CODE >> 16) & 0xff,
  1756. (CX88_VERSION_CODE >> 8) & 0xff,
  1757. CX88_VERSION_CODE & 0xff);
  1758. #ifdef SNAPSHOT
  1759. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  1760. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  1761. #endif
  1762. return pci_register_driver(&cx8800_pci_driver);
  1763. }
  1764. static void cx8800_fini(void)
  1765. {
  1766. pci_unregister_driver(&cx8800_pci_driver);
  1767. }
  1768. module_init(cx8800_init);
  1769. module_exit(cx8800_fini);
  1770. /* ----------------------------------------------------------- */
  1771. /*
  1772. * Local variables:
  1773. * c-basic-offset: 8
  1774. * End:
  1775. * 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
  1776. */