cx88-video.c 52 KB

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