cx88-video.c 55 KB

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