cx88-video.c 51 KB

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