cx23885-video.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. /*
  2. * Driver for the Conexant CX23885 PCIe bridge
  3. *
  4. * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/kmod.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/delay.h>
  30. #include <linux/kthread.h>
  31. #include <asm/div64.h>
  32. #include "cx23885.h"
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include "cx23885-ioctl.h"
  36. #include "tuner-xc2028.h"
  37. #include <media/cx25840.h>
  38. MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
  39. MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  40. MODULE_LICENSE("GPL");
  41. /* ------------------------------------------------------------------ */
  42. static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  43. static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  44. static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  45. module_param_array(video_nr, int, NULL, 0444);
  46. module_param_array(vbi_nr, int, NULL, 0444);
  47. module_param_array(radio_nr, int, NULL, 0444);
  48. MODULE_PARM_DESC(video_nr, "video device numbers");
  49. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  50. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  51. static unsigned int video_debug;
  52. module_param(video_debug, int, 0644);
  53. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  54. static unsigned int irq_debug;
  55. module_param(irq_debug, int, 0644);
  56. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  57. static unsigned int vid_limit = 16;
  58. module_param(vid_limit, int, 0644);
  59. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  60. #define dprintk(level, fmt, arg...)\
  61. do { if (video_debug >= level)\
  62. printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
  63. } while (0)
  64. /* ------------------------------------------------------------------- */
  65. /* static data */
  66. #define FORMAT_FLAGS_PACKED 0x01
  67. #if 0
  68. static struct cx23885_fmt formats[] = {
  69. {
  70. .name = "8 bpp, gray",
  71. .fourcc = V4L2_PIX_FMT_GREY,
  72. .depth = 8,
  73. .flags = FORMAT_FLAGS_PACKED,
  74. }, {
  75. .name = "15 bpp RGB, le",
  76. .fourcc = V4L2_PIX_FMT_RGB555,
  77. .depth = 16,
  78. .flags = FORMAT_FLAGS_PACKED,
  79. }, {
  80. .name = "15 bpp RGB, be",
  81. .fourcc = V4L2_PIX_FMT_RGB555X,
  82. .depth = 16,
  83. .flags = FORMAT_FLAGS_PACKED,
  84. }, {
  85. .name = "16 bpp RGB, le",
  86. .fourcc = V4L2_PIX_FMT_RGB565,
  87. .depth = 16,
  88. .flags = FORMAT_FLAGS_PACKED,
  89. }, {
  90. .name = "16 bpp RGB, be",
  91. .fourcc = V4L2_PIX_FMT_RGB565X,
  92. .depth = 16,
  93. .flags = FORMAT_FLAGS_PACKED,
  94. }, {
  95. .name = "24 bpp RGB, le",
  96. .fourcc = V4L2_PIX_FMT_BGR24,
  97. .depth = 24,
  98. .flags = FORMAT_FLAGS_PACKED,
  99. }, {
  100. .name = "32 bpp RGB, le",
  101. .fourcc = V4L2_PIX_FMT_BGR32,
  102. .depth = 32,
  103. .flags = FORMAT_FLAGS_PACKED,
  104. }, {
  105. .name = "32 bpp RGB, be",
  106. .fourcc = V4L2_PIX_FMT_RGB32,
  107. .depth = 32,
  108. .flags = FORMAT_FLAGS_PACKED,
  109. }, {
  110. .name = "4:2:2, packed, YUYV",
  111. .fourcc = V4L2_PIX_FMT_YUYV,
  112. .depth = 16,
  113. .flags = FORMAT_FLAGS_PACKED,
  114. }, {
  115. .name = "4:2:2, packed, UYVY",
  116. .fourcc = V4L2_PIX_FMT_UYVY,
  117. .depth = 16,
  118. .flags = FORMAT_FLAGS_PACKED,
  119. },
  120. };
  121. #else
  122. static struct cx23885_fmt formats[] = {
  123. {
  124. #if 0
  125. .name = "4:2:2, packed, UYVY",
  126. .fourcc = V4L2_PIX_FMT_UYVY,
  127. .depth = 16,
  128. .flags = FORMAT_FLAGS_PACKED,
  129. }, {
  130. #endif
  131. .name = "4:2:2, packed, YUYV",
  132. .fourcc = V4L2_PIX_FMT_YUYV,
  133. .depth = 16,
  134. .flags = FORMAT_FLAGS_PACKED,
  135. }
  136. };
  137. #endif
  138. static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
  139. {
  140. unsigned int i;
  141. for (i = 0; i < ARRAY_SIZE(formats); i++)
  142. if (formats[i].fourcc == fourcc)
  143. return formats+i;
  144. printk(KERN_ERR "%s(%c%c%c%c) NOT FOUND\n", __func__,
  145. (fourcc & 0xff),
  146. ((fourcc >> 8) & 0xff),
  147. ((fourcc >> 16) & 0xff),
  148. ((fourcc >> 24) & 0xff)
  149. );
  150. return NULL;
  151. }
  152. /* ------------------------------------------------------------------- */
  153. static const struct v4l2_queryctrl no_ctl = {
  154. .name = "42",
  155. .flags = V4L2_CTRL_FLAG_DISABLED,
  156. };
  157. static struct cx23885_ctrl cx23885_ctls[] = {
  158. /* --- video --- */
  159. {
  160. .v = {
  161. .id = V4L2_CID_BRIGHTNESS,
  162. .name = "Brightness",
  163. .minimum = 0x00,
  164. .maximum = 0xff,
  165. .step = 1,
  166. .default_value = 0x7f,
  167. .type = V4L2_CTRL_TYPE_INTEGER,
  168. },
  169. .off = 128,
  170. .reg = LUMA_CTRL,
  171. .mask = 0x00ff,
  172. .shift = 0,
  173. }, {
  174. .v = {
  175. .id = V4L2_CID_CONTRAST,
  176. .name = "Contrast",
  177. .minimum = 0,
  178. .maximum = 0x7f,
  179. .step = 1,
  180. .default_value = 0x3f,
  181. .type = V4L2_CTRL_TYPE_INTEGER,
  182. },
  183. .off = 0,
  184. .reg = LUMA_CTRL,
  185. .mask = 0xff00,
  186. .shift = 8,
  187. }, {
  188. .v = {
  189. .id = V4L2_CID_HUE,
  190. .name = "Hue",
  191. .minimum = -127,
  192. .maximum = 128,
  193. .step = 1,
  194. .default_value = 0x0,
  195. .type = V4L2_CTRL_TYPE_INTEGER,
  196. },
  197. .off = 128,
  198. .reg = CHROMA_CTRL,
  199. .mask = 0xff0000,
  200. .shift = 16,
  201. }, {
  202. /* strictly, this only describes only U saturation.
  203. * V saturation is handled specially through code.
  204. */
  205. .v = {
  206. .id = V4L2_CID_SATURATION,
  207. .name = "Saturation",
  208. .minimum = 0,
  209. .maximum = 0x7f,
  210. .step = 1,
  211. .default_value = 0x3f,
  212. .type = V4L2_CTRL_TYPE_INTEGER,
  213. },
  214. .off = 0,
  215. .reg = CHROMA_CTRL,
  216. .mask = 0x00ff,
  217. .shift = 0,
  218. }, {
  219. /* --- audio --- */
  220. .v = {
  221. .id = V4L2_CID_AUDIO_MUTE,
  222. .name = "Mute",
  223. .minimum = 0,
  224. .maximum = 1,
  225. .default_value = 1,
  226. .type = V4L2_CTRL_TYPE_BOOLEAN,
  227. },
  228. .reg = PATH1_CTL1,
  229. .mask = (0x1f << 24),
  230. .shift = 24,
  231. }, {
  232. .v = {
  233. .id = V4L2_CID_AUDIO_VOLUME,
  234. .name = "Volume",
  235. .minimum = 0,
  236. .maximum = 65535,
  237. .step = 65535 / 100,
  238. .default_value = 65535,
  239. .type = V4L2_CTRL_TYPE_INTEGER,
  240. },
  241. .reg = PATH1_VOL_CTL,
  242. .mask = 0xff,
  243. .shift = 0,
  244. }
  245. };
  246. static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
  247. /* Must be sorted from low to high control ID! */
  248. static const u32 cx23885_user_ctrls[] = {
  249. V4L2_CID_USER_CLASS,
  250. V4L2_CID_BRIGHTNESS,
  251. V4L2_CID_CONTRAST,
  252. V4L2_CID_SATURATION,
  253. V4L2_CID_HUE,
  254. V4L2_CID_AUDIO_VOLUME,
  255. V4L2_CID_AUDIO_MUTE,
  256. 0
  257. };
  258. static const u32 *ctrl_classes[] = {
  259. cx23885_user_ctrls,
  260. NULL
  261. };
  262. void cx23885_video_wakeup(struct cx23885_dev *dev,
  263. struct cx23885_dmaqueue *q, u32 count)
  264. {
  265. struct cx23885_buffer *buf;
  266. int bc;
  267. for (bc = 0;; bc++) {
  268. if (list_empty(&q->active))
  269. break;
  270. buf = list_entry(q->active.next,
  271. struct cx23885_buffer, vb.queue);
  272. /* count comes from the hw and is is 16bit wide --
  273. * this trick handles wrap-arounds correctly for
  274. * up to 32767 buffers in flight... */
  275. if ((s16) (count - buf->count) < 0)
  276. break;
  277. v4l2_get_timestamp(&buf->vb.ts);
  278. dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
  279. count, buf->count);
  280. buf->vb.state = VIDEOBUF_DONE;
  281. list_del(&buf->vb.queue);
  282. wake_up(&buf->vb.done);
  283. }
  284. if (list_empty(&q->active))
  285. del_timer(&q->timeout);
  286. else
  287. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  288. if (bc != 1)
  289. printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
  290. __func__, bc);
  291. }
  292. int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
  293. {
  294. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  295. __func__,
  296. (unsigned int)norm,
  297. v4l2_norm_to_name(norm));
  298. dev->tvnorm = norm;
  299. call_all(dev, core, s_std, norm);
  300. return 0;
  301. }
  302. static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
  303. struct pci_dev *pci,
  304. struct video_device *template,
  305. char *type)
  306. {
  307. struct video_device *vfd;
  308. dprintk(1, "%s()\n", __func__);
  309. vfd = video_device_alloc();
  310. if (NULL == vfd)
  311. return NULL;
  312. *vfd = *template;
  313. vfd->v4l2_dev = &dev->v4l2_dev;
  314. vfd->release = video_device_release;
  315. snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
  316. cx23885_boards[dev->board].name, type);
  317. video_set_drvdata(vfd, dev);
  318. return vfd;
  319. }
  320. static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
  321. {
  322. int i;
  323. if (qctrl->id < V4L2_CID_BASE ||
  324. qctrl->id >= V4L2_CID_LASTP1)
  325. return -EINVAL;
  326. for (i = 0; i < CX23885_CTLS; i++)
  327. if (cx23885_ctls[i].v.id == qctrl->id)
  328. break;
  329. if (i == CX23885_CTLS) {
  330. *qctrl = no_ctl;
  331. return 0;
  332. }
  333. *qctrl = cx23885_ctls[i].v;
  334. return 0;
  335. }
  336. /* ------------------------------------------------------------------- */
  337. /* resource management */
  338. static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
  339. unsigned int bit)
  340. {
  341. dprintk(1, "%s()\n", __func__);
  342. if (fh->resources & bit)
  343. /* have it already allocated */
  344. return 1;
  345. /* is it free? */
  346. mutex_lock(&dev->lock);
  347. if (dev->resources & bit) {
  348. /* no, someone else uses it */
  349. mutex_unlock(&dev->lock);
  350. return 0;
  351. }
  352. /* it's free, grab it */
  353. fh->resources |= bit;
  354. dev->resources |= bit;
  355. dprintk(1, "res: get %d\n", bit);
  356. mutex_unlock(&dev->lock);
  357. return 1;
  358. }
  359. static int res_check(struct cx23885_fh *fh, unsigned int bit)
  360. {
  361. return fh->resources & bit;
  362. }
  363. static int res_locked(struct cx23885_dev *dev, unsigned int bit)
  364. {
  365. return dev->resources & bit;
  366. }
  367. static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
  368. unsigned int bits)
  369. {
  370. BUG_ON((fh->resources & bits) != bits);
  371. dprintk(1, "%s()\n", __func__);
  372. mutex_lock(&dev->lock);
  373. fh->resources &= ~bits;
  374. dev->resources &= ~bits;
  375. dprintk(1, "res: put %d\n", bits);
  376. mutex_unlock(&dev->lock);
  377. }
  378. static int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
  379. {
  380. /* 8 bit registers, 8 bit values */
  381. u8 buf[] = { reg, data };
  382. struct i2c_msg msg = { .addr = 0x98 >> 1,
  383. .flags = 0, .buf = buf, .len = 2 };
  384. return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
  385. }
  386. static u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
  387. {
  388. /* 8 bit registers, 8 bit values */
  389. int ret;
  390. u8 b0[] = { reg };
  391. u8 b1[] = { 0 };
  392. struct i2c_msg msg[] = {
  393. { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
  394. { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
  395. };
  396. ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
  397. if (ret != 2)
  398. printk(KERN_ERR "%s() error\n", __func__);
  399. return b1[0];
  400. }
  401. static void cx23885_flatiron_dump(struct cx23885_dev *dev)
  402. {
  403. int i;
  404. dprintk(1, "Flatiron dump\n");
  405. for (i = 0; i < 0x24; i++) {
  406. dprintk(1, "FI[%02x] = %02x\n", i,
  407. cx23885_flatiron_read(dev, i));
  408. }
  409. }
  410. static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
  411. {
  412. u8 val;
  413. dprintk(1, "%s(input = %d)\n", __func__, input);
  414. if (input == 1)
  415. val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
  416. else if (input == 2)
  417. val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
  418. else
  419. return -EINVAL;
  420. val |= 0x20; /* Enable clock to delta-sigma and dec filter */
  421. cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
  422. /* Wake up */
  423. cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
  424. if (video_debug)
  425. cx23885_flatiron_dump(dev);
  426. return 0;
  427. }
  428. static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
  429. {
  430. dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  431. __func__,
  432. input, INPUT(input)->vmux,
  433. INPUT(input)->gpio0, INPUT(input)->gpio1,
  434. INPUT(input)->gpio2, INPUT(input)->gpio3);
  435. dev->input = input;
  436. if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
  437. dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
  438. dev->board == CX23885_BOARD_MYGICA_X8507) {
  439. /* Select Analog TV */
  440. if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
  441. cx23885_gpio_clear(dev, GPIO_0);
  442. }
  443. /* Tell the internal A/V decoder */
  444. v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
  445. INPUT(input)->vmux, 0, 0);
  446. if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
  447. (dev->board == CX23885_BOARD_MPX885) ||
  448. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
  449. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
  450. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
  451. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
  452. (dev->board == CX23885_BOARD_MYGICA_X8507)) {
  453. /* Configure audio routing */
  454. v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
  455. INPUT(input)->amux, 0, 0);
  456. if (INPUT(input)->amux == CX25840_AUDIO7)
  457. cx23885_flatiron_mux(dev, 1);
  458. else if (INPUT(input)->amux == CX25840_AUDIO6)
  459. cx23885_flatiron_mux(dev, 2);
  460. }
  461. return 0;
  462. }
  463. static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
  464. {
  465. dprintk(1, "%s(input=%d)\n", __func__, input);
  466. /* The baseband video core of the cx23885 has two audio inputs.
  467. * LR1 and LR2. In almost every single case so far only HVR1xxx
  468. * cards we've only ever supported LR1. Time to support LR2,
  469. * which is available via the optional white breakout header on
  470. * the board.
  471. * We'll use a could of existing enums in the card struct to allow
  472. * devs to specify which baseband input they need, or just default
  473. * to what we've always used.
  474. */
  475. if (INPUT(input)->amux == CX25840_AUDIO7)
  476. cx23885_flatiron_mux(dev, 1);
  477. else if (INPUT(input)->amux == CX25840_AUDIO6)
  478. cx23885_flatiron_mux(dev, 2);
  479. else {
  480. /* Not specifically defined, assume the default. */
  481. cx23885_flatiron_mux(dev, 1);
  482. }
  483. return 0;
  484. }
  485. /* ------------------------------------------------------------------ */
  486. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  487. struct cx23885_dmaqueue *q,
  488. struct cx23885_buffer *buf)
  489. {
  490. dprintk(1, "%s()\n", __func__);
  491. /* Stop the dma/fifo before we tamper with it's risc programs */
  492. cx_clear(VID_A_DMA_CTL, 0x11);
  493. /* setup fifo + format */
  494. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  495. buf->bpl, buf->risc.dma);
  496. /* reset counter */
  497. cx_write(VID_A_GPCNT_CTL, 3);
  498. q->count = 1;
  499. /* enable irq */
  500. cx23885_irq_add_enable(dev, 0x01);
  501. cx_set(VID_A_INT_MSK, 0x000011);
  502. /* start dma */
  503. cx_set(DEV_CNTRL2, (1<<5));
  504. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  505. return 0;
  506. }
  507. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  508. struct cx23885_dmaqueue *q)
  509. {
  510. struct cx23885_buffer *buf, *prev;
  511. struct list_head *item;
  512. dprintk(1, "%s()\n", __func__);
  513. if (!list_empty(&q->active)) {
  514. buf = list_entry(q->active.next, struct cx23885_buffer,
  515. vb.queue);
  516. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  517. buf, buf->vb.i);
  518. cx23885_start_video_dma(dev, q, buf);
  519. list_for_each(item, &q->active) {
  520. buf = list_entry(item, struct cx23885_buffer,
  521. vb.queue);
  522. buf->count = q->count++;
  523. }
  524. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  525. return 0;
  526. }
  527. prev = NULL;
  528. for (;;) {
  529. if (list_empty(&q->queued))
  530. return 0;
  531. buf = list_entry(q->queued.next, struct cx23885_buffer,
  532. vb.queue);
  533. if (NULL == prev) {
  534. list_move_tail(&buf->vb.queue, &q->active);
  535. cx23885_start_video_dma(dev, q, buf);
  536. buf->vb.state = VIDEOBUF_ACTIVE;
  537. buf->count = q->count++;
  538. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  539. dprintk(2, "[%p/%d] restart_queue - first active\n",
  540. buf, buf->vb.i);
  541. } else if (prev->vb.width == buf->vb.width &&
  542. prev->vb.height == buf->vb.height &&
  543. prev->fmt == buf->fmt) {
  544. list_move_tail(&buf->vb.queue, &q->active);
  545. buf->vb.state = VIDEOBUF_ACTIVE;
  546. buf->count = q->count++;
  547. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  548. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  549. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  550. buf, buf->vb.i);
  551. } else {
  552. return 0;
  553. }
  554. prev = buf;
  555. }
  556. }
  557. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  558. unsigned int *size)
  559. {
  560. struct cx23885_fh *fh = q->priv_data;
  561. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  562. if (0 == *count)
  563. *count = 32;
  564. if (*size * *count > vid_limit * 1024 * 1024)
  565. *count = (vid_limit * 1024 * 1024) / *size;
  566. return 0;
  567. }
  568. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  569. enum v4l2_field field)
  570. {
  571. struct cx23885_fh *fh = q->priv_data;
  572. struct cx23885_dev *dev = fh->dev;
  573. struct cx23885_buffer *buf =
  574. container_of(vb, struct cx23885_buffer, vb);
  575. int rc, init_buffer = 0;
  576. u32 line0_offset, line1_offset;
  577. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  578. int field_tff;
  579. BUG_ON(NULL == fh->fmt);
  580. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  581. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  582. return -EINVAL;
  583. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  584. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  585. return -EINVAL;
  586. if (buf->fmt != fh->fmt ||
  587. buf->vb.width != fh->width ||
  588. buf->vb.height != fh->height ||
  589. buf->vb.field != field) {
  590. buf->fmt = fh->fmt;
  591. buf->vb.width = fh->width;
  592. buf->vb.height = fh->height;
  593. buf->vb.field = field;
  594. init_buffer = 1;
  595. }
  596. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  597. init_buffer = 1;
  598. rc = videobuf_iolock(q, &buf->vb, NULL);
  599. if (0 != rc)
  600. goto fail;
  601. }
  602. if (init_buffer) {
  603. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  604. switch (buf->vb.field) {
  605. case V4L2_FIELD_TOP:
  606. cx23885_risc_buffer(dev->pci, &buf->risc,
  607. dma->sglist, 0, UNSET,
  608. buf->bpl, 0, buf->vb.height);
  609. break;
  610. case V4L2_FIELD_BOTTOM:
  611. cx23885_risc_buffer(dev->pci, &buf->risc,
  612. dma->sglist, UNSET, 0,
  613. buf->bpl, 0, buf->vb.height);
  614. break;
  615. case V4L2_FIELD_INTERLACED:
  616. if (dev->tvnorm & V4L2_STD_NTSC)
  617. /* NTSC or */
  618. field_tff = 1;
  619. else
  620. field_tff = 0;
  621. if (cx23885_boards[dev->board].force_bff)
  622. /* PAL / SECAM OR 888 in NTSC MODE */
  623. field_tff = 0;
  624. if (field_tff) {
  625. /* cx25840 transmits NTSC bottom field first */
  626. dprintk(1, "%s() Creating TFF/NTSC risc\n",
  627. __func__);
  628. line0_offset = buf->bpl;
  629. line1_offset = 0;
  630. } else {
  631. /* All other formats are top field first */
  632. dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
  633. __func__);
  634. line0_offset = 0;
  635. line1_offset = buf->bpl;
  636. }
  637. cx23885_risc_buffer(dev->pci, &buf->risc,
  638. dma->sglist, line0_offset,
  639. line1_offset,
  640. buf->bpl, buf->bpl,
  641. buf->vb.height >> 1);
  642. break;
  643. case V4L2_FIELD_SEQ_TB:
  644. cx23885_risc_buffer(dev->pci, &buf->risc,
  645. dma->sglist,
  646. 0, buf->bpl * (buf->vb.height >> 1),
  647. buf->bpl, 0,
  648. buf->vb.height >> 1);
  649. break;
  650. case V4L2_FIELD_SEQ_BT:
  651. cx23885_risc_buffer(dev->pci, &buf->risc,
  652. dma->sglist,
  653. buf->bpl * (buf->vb.height >> 1), 0,
  654. buf->bpl, 0,
  655. buf->vb.height >> 1);
  656. break;
  657. default:
  658. BUG();
  659. }
  660. }
  661. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  662. buf, buf->vb.i,
  663. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  664. (unsigned long)buf->risc.dma);
  665. buf->vb.state = VIDEOBUF_PREPARED;
  666. return 0;
  667. fail:
  668. cx23885_free_buffer(q, buf);
  669. return rc;
  670. }
  671. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  672. {
  673. struct cx23885_buffer *buf = container_of(vb,
  674. struct cx23885_buffer, vb);
  675. struct cx23885_buffer *prev;
  676. struct cx23885_fh *fh = vq->priv_data;
  677. struct cx23885_dev *dev = fh->dev;
  678. struct cx23885_dmaqueue *q = &dev->vidq;
  679. /* add jump to stopper */
  680. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  681. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  682. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  683. if (!list_empty(&q->queued)) {
  684. list_add_tail(&buf->vb.queue, &q->queued);
  685. buf->vb.state = VIDEOBUF_QUEUED;
  686. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  687. buf, buf->vb.i);
  688. } else if (list_empty(&q->active)) {
  689. list_add_tail(&buf->vb.queue, &q->active);
  690. cx23885_start_video_dma(dev, q, buf);
  691. buf->vb.state = VIDEOBUF_ACTIVE;
  692. buf->count = q->count++;
  693. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  694. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  695. buf, buf->vb.i);
  696. } else {
  697. prev = list_entry(q->active.prev, struct cx23885_buffer,
  698. vb.queue);
  699. if (prev->vb.width == buf->vb.width &&
  700. prev->vb.height == buf->vb.height &&
  701. prev->fmt == buf->fmt) {
  702. list_add_tail(&buf->vb.queue, &q->active);
  703. buf->vb.state = VIDEOBUF_ACTIVE;
  704. buf->count = q->count++;
  705. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  706. /* 64 bit bits 63-32 */
  707. prev->risc.jmp[2] = cpu_to_le32(0);
  708. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  709. buf, buf->vb.i);
  710. } else {
  711. list_add_tail(&buf->vb.queue, &q->queued);
  712. buf->vb.state = VIDEOBUF_QUEUED;
  713. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  714. buf, buf->vb.i);
  715. }
  716. }
  717. }
  718. static void buffer_release(struct videobuf_queue *q,
  719. struct videobuf_buffer *vb)
  720. {
  721. struct cx23885_buffer *buf = container_of(vb,
  722. struct cx23885_buffer, vb);
  723. cx23885_free_buffer(q, buf);
  724. }
  725. static struct videobuf_queue_ops cx23885_video_qops = {
  726. .buf_setup = buffer_setup,
  727. .buf_prepare = buffer_prepare,
  728. .buf_queue = buffer_queue,
  729. .buf_release = buffer_release,
  730. };
  731. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  732. {
  733. switch (fh->type) {
  734. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  735. return &fh->vidq;
  736. case V4L2_BUF_TYPE_VBI_CAPTURE:
  737. return &fh->vbiq;
  738. default:
  739. BUG();
  740. return NULL;
  741. }
  742. }
  743. static int get_resource(struct cx23885_fh *fh)
  744. {
  745. switch (fh->type) {
  746. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  747. return RESOURCE_VIDEO;
  748. case V4L2_BUF_TYPE_VBI_CAPTURE:
  749. return RESOURCE_VBI;
  750. default:
  751. BUG();
  752. return 0;
  753. }
  754. }
  755. static int video_open(struct file *file)
  756. {
  757. struct video_device *vdev = video_devdata(file);
  758. struct cx23885_dev *dev = video_drvdata(file);
  759. struct cx23885_fh *fh;
  760. enum v4l2_buf_type type = 0;
  761. int radio = 0;
  762. switch (vdev->vfl_type) {
  763. case VFL_TYPE_GRABBER:
  764. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  765. break;
  766. case VFL_TYPE_VBI:
  767. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  768. break;
  769. case VFL_TYPE_RADIO:
  770. radio = 1;
  771. break;
  772. }
  773. dprintk(1, "open dev=%s radio=%d type=%s\n",
  774. video_device_node_name(vdev), radio, v4l2_type_names[type]);
  775. /* allocate + initialize per filehandle data */
  776. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  777. if (NULL == fh)
  778. return -ENOMEM;
  779. file->private_data = fh;
  780. fh->dev = dev;
  781. fh->radio = radio;
  782. fh->type = type;
  783. fh->width = 320;
  784. fh->height = 240;
  785. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
  786. videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
  787. &dev->pci->dev, &dev->slock,
  788. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  789. V4L2_FIELD_INTERLACED,
  790. sizeof(struct cx23885_buffer),
  791. fh, NULL);
  792. videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
  793. &dev->pci->dev, &dev->slock,
  794. V4L2_BUF_TYPE_VBI_CAPTURE,
  795. V4L2_FIELD_SEQ_TB,
  796. sizeof(struct cx23885_buffer),
  797. fh, NULL);
  798. dprintk(1, "post videobuf_queue_init()\n");
  799. return 0;
  800. }
  801. static ssize_t video_read(struct file *file, char __user *data,
  802. size_t count, loff_t *ppos)
  803. {
  804. struct cx23885_fh *fh = file->private_data;
  805. switch (fh->type) {
  806. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  807. if (res_locked(fh->dev, RESOURCE_VIDEO))
  808. return -EBUSY;
  809. return videobuf_read_one(&fh->vidq, data, count, ppos,
  810. file->f_flags & O_NONBLOCK);
  811. case V4L2_BUF_TYPE_VBI_CAPTURE:
  812. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  813. return -EBUSY;
  814. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  815. file->f_flags & O_NONBLOCK);
  816. default:
  817. BUG();
  818. return 0;
  819. }
  820. }
  821. static unsigned int video_poll(struct file *file,
  822. struct poll_table_struct *wait)
  823. {
  824. struct cx23885_fh *fh = file->private_data;
  825. struct cx23885_buffer *buf;
  826. unsigned int rc = POLLERR;
  827. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  828. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  829. return POLLERR;
  830. return videobuf_poll_stream(file, &fh->vbiq, wait);
  831. }
  832. mutex_lock(&fh->vidq.vb_lock);
  833. if (res_check(fh, RESOURCE_VIDEO)) {
  834. /* streaming capture */
  835. if (list_empty(&fh->vidq.stream))
  836. goto done;
  837. buf = list_entry(fh->vidq.stream.next,
  838. struct cx23885_buffer, vb.stream);
  839. } else {
  840. /* read() capture */
  841. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  842. if (NULL == buf)
  843. goto done;
  844. }
  845. poll_wait(file, &buf->vb.done, wait);
  846. if (buf->vb.state == VIDEOBUF_DONE ||
  847. buf->vb.state == VIDEOBUF_ERROR)
  848. rc = POLLIN|POLLRDNORM;
  849. else
  850. rc = 0;
  851. done:
  852. mutex_unlock(&fh->vidq.vb_lock);
  853. return rc;
  854. }
  855. static int video_release(struct file *file)
  856. {
  857. struct cx23885_fh *fh = file->private_data;
  858. struct cx23885_dev *dev = fh->dev;
  859. /* turn off overlay */
  860. if (res_check(fh, RESOURCE_OVERLAY)) {
  861. /* FIXME */
  862. res_free(dev, fh, RESOURCE_OVERLAY);
  863. }
  864. /* stop video capture */
  865. if (res_check(fh, RESOURCE_VIDEO)) {
  866. videobuf_queue_cancel(&fh->vidq);
  867. res_free(dev, fh, RESOURCE_VIDEO);
  868. }
  869. if (fh->vidq.read_buf) {
  870. buffer_release(&fh->vidq, fh->vidq.read_buf);
  871. kfree(fh->vidq.read_buf);
  872. }
  873. /* stop vbi capture */
  874. if (res_check(fh, RESOURCE_VBI)) {
  875. if (fh->vbiq.streaming)
  876. videobuf_streamoff(&fh->vbiq);
  877. if (fh->vbiq.reading)
  878. videobuf_read_stop(&fh->vbiq);
  879. res_free(dev, fh, RESOURCE_VBI);
  880. }
  881. videobuf_mmap_free(&fh->vidq);
  882. videobuf_mmap_free(&fh->vbiq);
  883. file->private_data = NULL;
  884. kfree(fh);
  885. /* We are not putting the tuner to sleep here on exit, because
  886. * we want to use the mpeg encoder in another session to capture
  887. * tuner video. Closing this will result in no video to the encoder.
  888. */
  889. return 0;
  890. }
  891. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  892. {
  893. struct cx23885_fh *fh = file->private_data;
  894. return videobuf_mmap_mapper(get_queue(fh), vma);
  895. }
  896. /* ------------------------------------------------------------------ */
  897. /* VIDEO CTRL IOCTLS */
  898. int cx23885_get_control(struct cx23885_dev *dev,
  899. struct v4l2_control *ctl)
  900. {
  901. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
  902. call_all(dev, core, g_ctrl, ctl);
  903. return 0;
  904. }
  905. int cx23885_set_control(struct cx23885_dev *dev,
  906. struct v4l2_control *ctl)
  907. {
  908. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
  909. call_all(dev, core, s_ctrl, ctl);
  910. return 0;
  911. }
  912. static void init_controls(struct cx23885_dev *dev)
  913. {
  914. struct v4l2_control ctrl;
  915. int i;
  916. for (i = 0; i < CX23885_CTLS; i++) {
  917. ctrl.id = cx23885_ctls[i].v.id;
  918. ctrl.value = cx23885_ctls[i].v.default_value;
  919. cx23885_set_control(dev, &ctrl);
  920. }
  921. }
  922. /* ------------------------------------------------------------------ */
  923. /* VIDEO IOCTLS */
  924. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  925. struct v4l2_format *f)
  926. {
  927. struct cx23885_fh *fh = priv;
  928. f->fmt.pix.width = fh->width;
  929. f->fmt.pix.height = fh->height;
  930. f->fmt.pix.field = fh->vidq.field;
  931. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  932. f->fmt.pix.bytesperline =
  933. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  934. f->fmt.pix.sizeimage =
  935. f->fmt.pix.height * f->fmt.pix.bytesperline;
  936. return 0;
  937. }
  938. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  939. struct v4l2_format *f)
  940. {
  941. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  942. struct cx23885_fmt *fmt;
  943. enum v4l2_field field;
  944. unsigned int maxw, maxh;
  945. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  946. if (NULL == fmt)
  947. return -EINVAL;
  948. field = f->fmt.pix.field;
  949. maxw = norm_maxw(dev->tvnorm);
  950. maxh = norm_maxh(dev->tvnorm);
  951. if (V4L2_FIELD_ANY == field) {
  952. field = (f->fmt.pix.height > maxh/2)
  953. ? V4L2_FIELD_INTERLACED
  954. : V4L2_FIELD_BOTTOM;
  955. }
  956. switch (field) {
  957. case V4L2_FIELD_TOP:
  958. case V4L2_FIELD_BOTTOM:
  959. maxh = maxh / 2;
  960. break;
  961. case V4L2_FIELD_INTERLACED:
  962. break;
  963. default:
  964. return -EINVAL;
  965. }
  966. f->fmt.pix.field = field;
  967. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  968. &f->fmt.pix.height, 32, maxh, 0, 0);
  969. f->fmt.pix.bytesperline =
  970. (f->fmt.pix.width * fmt->depth) >> 3;
  971. f->fmt.pix.sizeimage =
  972. f->fmt.pix.height * f->fmt.pix.bytesperline;
  973. return 0;
  974. }
  975. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  976. struct v4l2_format *f)
  977. {
  978. struct cx23885_fh *fh = priv;
  979. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  980. struct v4l2_mbus_framefmt mbus_fmt;
  981. int err;
  982. dprintk(2, "%s()\n", __func__);
  983. err = vidioc_try_fmt_vid_cap(file, priv, f);
  984. if (0 != err)
  985. return err;
  986. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  987. fh->width = f->fmt.pix.width;
  988. fh->height = f->fmt.pix.height;
  989. fh->vidq.field = f->fmt.pix.field;
  990. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  991. fh->width, fh->height, fh->vidq.field);
  992. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  993. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  994. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  995. return 0;
  996. }
  997. static int vidioc_querycap(struct file *file, void *priv,
  998. struct v4l2_capability *cap)
  999. {
  1000. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1001. strcpy(cap->driver, "cx23885");
  1002. strlcpy(cap->card, cx23885_boards[dev->board].name,
  1003. sizeof(cap->card));
  1004. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  1005. cap->capabilities =
  1006. V4L2_CAP_VIDEO_CAPTURE |
  1007. V4L2_CAP_READWRITE |
  1008. V4L2_CAP_STREAMING |
  1009. V4L2_CAP_VBI_CAPTURE;
  1010. if (UNSET != dev->tuner_type)
  1011. cap->capabilities |= V4L2_CAP_TUNER;
  1012. return 0;
  1013. }
  1014. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1015. struct v4l2_fmtdesc *f)
  1016. {
  1017. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  1018. return -EINVAL;
  1019. strlcpy(f->description, formats[f->index].name,
  1020. sizeof(f->description));
  1021. f->pixelformat = formats[f->index].fourcc;
  1022. return 0;
  1023. }
  1024. static int vidioc_reqbufs(struct file *file, void *priv,
  1025. struct v4l2_requestbuffers *p)
  1026. {
  1027. struct cx23885_fh *fh = priv;
  1028. return videobuf_reqbufs(get_queue(fh), p);
  1029. }
  1030. static int vidioc_querybuf(struct file *file, void *priv,
  1031. struct v4l2_buffer *p)
  1032. {
  1033. struct cx23885_fh *fh = priv;
  1034. return videobuf_querybuf(get_queue(fh), p);
  1035. }
  1036. static int vidioc_qbuf(struct file *file, void *priv,
  1037. struct v4l2_buffer *p)
  1038. {
  1039. struct cx23885_fh *fh = priv;
  1040. return videobuf_qbuf(get_queue(fh), p);
  1041. }
  1042. static int vidioc_dqbuf(struct file *file, void *priv,
  1043. struct v4l2_buffer *p)
  1044. {
  1045. struct cx23885_fh *fh = priv;
  1046. return videobuf_dqbuf(get_queue(fh), p,
  1047. file->f_flags & O_NONBLOCK);
  1048. }
  1049. static int vidioc_streamon(struct file *file, void *priv,
  1050. enum v4l2_buf_type i)
  1051. {
  1052. struct cx23885_fh *fh = priv;
  1053. struct cx23885_dev *dev = fh->dev;
  1054. dprintk(1, "%s()\n", __func__);
  1055. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1056. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1057. return -EINVAL;
  1058. if (unlikely(i != fh->type))
  1059. return -EINVAL;
  1060. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  1061. return -EBUSY;
  1062. /* Don't start VBI streaming unless vida streaming
  1063. * has already started.
  1064. */
  1065. if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
  1066. ((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
  1067. return -EINVAL;
  1068. return videobuf_streamon(get_queue(fh));
  1069. }
  1070. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1071. {
  1072. struct cx23885_fh *fh = priv;
  1073. struct cx23885_dev *dev = fh->dev;
  1074. int err, res;
  1075. dprintk(1, "%s()\n", __func__);
  1076. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1077. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1078. return -EINVAL;
  1079. if (i != fh->type)
  1080. return -EINVAL;
  1081. res = get_resource(fh);
  1082. err = videobuf_streamoff(get_queue(fh));
  1083. if (err < 0)
  1084. return err;
  1085. res_free(dev, fh, res);
  1086. return 0;
  1087. }
  1088. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1089. {
  1090. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1091. dprintk(1, "%s()\n", __func__);
  1092. call_all(dev, core, g_std, id);
  1093. return 0;
  1094. }
  1095. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  1096. {
  1097. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1098. dprintk(1, "%s()\n", __func__);
  1099. mutex_lock(&dev->lock);
  1100. cx23885_set_tvnorm(dev, *tvnorms);
  1101. mutex_unlock(&dev->lock);
  1102. return 0;
  1103. }
  1104. int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  1105. {
  1106. static const char *iname[] = {
  1107. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  1108. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  1109. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  1110. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1111. [CX23885_VMUX_SVIDEO] = "S-Video",
  1112. [CX23885_VMUX_COMPONENT] = "Component",
  1113. [CX23885_VMUX_TELEVISION] = "Television",
  1114. [CX23885_VMUX_CABLE] = "Cable TV",
  1115. [CX23885_VMUX_DVB] = "DVB",
  1116. [CX23885_VMUX_DEBUG] = "for debug only",
  1117. };
  1118. unsigned int n;
  1119. dprintk(1, "%s()\n", __func__);
  1120. n = i->index;
  1121. if (n >= MAX_CX23885_INPUT)
  1122. return -EINVAL;
  1123. if (0 == INPUT(n)->type)
  1124. return -EINVAL;
  1125. i->index = n;
  1126. i->type = V4L2_INPUT_TYPE_CAMERA;
  1127. strcpy(i->name, iname[INPUT(n)->type]);
  1128. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1129. (CX23885_VMUX_CABLE == INPUT(n)->type)) {
  1130. i->type = V4L2_INPUT_TYPE_TUNER;
  1131. i->std = CX23885_NORMS;
  1132. }
  1133. /* Two selectable audio inputs for non-tv inputs */
  1134. if (INPUT(n)->type != CX23885_VMUX_TELEVISION)
  1135. i->audioset = 0x3;
  1136. if (dev->input == n) {
  1137. /* enum'd input matches our configured input.
  1138. * Ask the video decoder to process the call
  1139. * and give it an oppertunity to update the
  1140. * status field.
  1141. */
  1142. call_all(dev, video, g_input_status, &i->status);
  1143. }
  1144. return 0;
  1145. }
  1146. static int vidioc_enum_input(struct file *file, void *priv,
  1147. struct v4l2_input *i)
  1148. {
  1149. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1150. dprintk(1, "%s()\n", __func__);
  1151. return cx23885_enum_input(dev, i);
  1152. }
  1153. int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
  1154. {
  1155. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1156. *i = dev->input;
  1157. dprintk(1, "%s() returns %d\n", __func__, *i);
  1158. return 0;
  1159. }
  1160. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1161. {
  1162. return cx23885_get_input(file, priv, i);
  1163. }
  1164. int cx23885_set_input(struct file *file, void *priv, unsigned int i)
  1165. {
  1166. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1167. dprintk(1, "%s(%d)\n", __func__, i);
  1168. if (i >= MAX_CX23885_INPUT) {
  1169. dprintk(1, "%s() -EINVAL\n", __func__);
  1170. return -EINVAL;
  1171. }
  1172. if (INPUT(i)->type == 0)
  1173. return -EINVAL;
  1174. mutex_lock(&dev->lock);
  1175. cx23885_video_mux(dev, i);
  1176. /* By default establish the default audio input for the card also */
  1177. /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
  1178. cx23885_audio_mux(dev, i);
  1179. mutex_unlock(&dev->lock);
  1180. return 0;
  1181. }
  1182. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1183. {
  1184. return cx23885_set_input(file, priv, i);
  1185. }
  1186. static int vidioc_log_status(struct file *file, void *priv)
  1187. {
  1188. struct cx23885_fh *fh = priv;
  1189. struct cx23885_dev *dev = fh->dev;
  1190. printk(KERN_INFO
  1191. "%s/0: ============ START LOG STATUS ============\n",
  1192. dev->name);
  1193. call_all(dev, core, log_status);
  1194. printk(KERN_INFO
  1195. "%s/0: ============= END LOG STATUS =============\n",
  1196. dev->name);
  1197. return 0;
  1198. }
  1199. static int cx23885_query_audinput(struct file *file, void *priv,
  1200. struct v4l2_audio *i)
  1201. {
  1202. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1203. static const char *iname[] = {
  1204. [0] = "Baseband L/R 1",
  1205. [1] = "Baseband L/R 2",
  1206. };
  1207. unsigned int n;
  1208. dprintk(1, "%s()\n", __func__);
  1209. n = i->index;
  1210. if (n >= 2)
  1211. return -EINVAL;
  1212. memset(i, 0, sizeof(*i));
  1213. i->index = n;
  1214. strcpy(i->name, iname[n]);
  1215. i->capability = V4L2_AUDCAP_STEREO;
  1216. i->mode = V4L2_AUDMODE_AVL;
  1217. return 0;
  1218. }
  1219. static int vidioc_enum_audinput(struct file *file, void *priv,
  1220. struct v4l2_audio *i)
  1221. {
  1222. return cx23885_query_audinput(file, priv, i);
  1223. }
  1224. static int vidioc_g_audinput(struct file *file, void *priv,
  1225. struct v4l2_audio *i)
  1226. {
  1227. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1228. i->index = dev->audinput;
  1229. dprintk(1, "%s(input=%d)\n", __func__, i->index);
  1230. return cx23885_query_audinput(file, priv, i);
  1231. }
  1232. static int vidioc_s_audinput(struct file *file, void *priv,
  1233. const struct v4l2_audio *i)
  1234. {
  1235. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1236. if (i->index >= 2)
  1237. return -EINVAL;
  1238. dprintk(1, "%s(%d)\n", __func__, i->index);
  1239. dev->audinput = i->index;
  1240. /* Skip the audio defaults from the cards struct, caller wants
  1241. * directly touch the audio mux hardware. */
  1242. cx23885_flatiron_mux(dev, dev->audinput + 1);
  1243. return 0;
  1244. }
  1245. static int vidioc_queryctrl(struct file *file, void *priv,
  1246. struct v4l2_queryctrl *qctrl)
  1247. {
  1248. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1249. if (unlikely(qctrl->id == 0))
  1250. return -EINVAL;
  1251. return cx23885_ctrl_query(qctrl);
  1252. }
  1253. static int vidioc_g_ctrl(struct file *file, void *priv,
  1254. struct v4l2_control *ctl)
  1255. {
  1256. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1257. return cx23885_get_control(dev, ctl);
  1258. }
  1259. static int vidioc_s_ctrl(struct file *file, void *priv,
  1260. struct v4l2_control *ctl)
  1261. {
  1262. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1263. return cx23885_set_control(dev, ctl);
  1264. }
  1265. static int vidioc_g_tuner(struct file *file, void *priv,
  1266. struct v4l2_tuner *t)
  1267. {
  1268. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1269. if (unlikely(UNSET == dev->tuner_type))
  1270. return -EINVAL;
  1271. if (0 != t->index)
  1272. return -EINVAL;
  1273. strcpy(t->name, "Television");
  1274. call_all(dev, tuner, g_tuner, t);
  1275. return 0;
  1276. }
  1277. static int vidioc_s_tuner(struct file *file, void *priv,
  1278. struct v4l2_tuner *t)
  1279. {
  1280. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1281. if (UNSET == dev->tuner_type)
  1282. return -EINVAL;
  1283. if (0 != t->index)
  1284. return -EINVAL;
  1285. /* Update the A/V core */
  1286. call_all(dev, tuner, s_tuner, t);
  1287. return 0;
  1288. }
  1289. static int vidioc_g_frequency(struct file *file, void *priv,
  1290. struct v4l2_frequency *f)
  1291. {
  1292. struct cx23885_fh *fh = priv;
  1293. struct cx23885_dev *dev = fh->dev;
  1294. if (unlikely(UNSET == dev->tuner_type))
  1295. return -EINVAL;
  1296. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1297. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1298. f->frequency = dev->freq;
  1299. call_all(dev, tuner, g_frequency, f);
  1300. return 0;
  1301. }
  1302. static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1303. {
  1304. struct v4l2_control ctrl;
  1305. if (unlikely(UNSET == dev->tuner_type))
  1306. return -EINVAL;
  1307. if (unlikely(f->tuner != 0))
  1308. return -EINVAL;
  1309. mutex_lock(&dev->lock);
  1310. dev->freq = f->frequency;
  1311. /* I need to mute audio here */
  1312. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1313. ctrl.value = 1;
  1314. cx23885_set_control(dev, &ctrl);
  1315. call_all(dev, tuner, s_frequency, f);
  1316. /* When changing channels it is required to reset TVAUDIO */
  1317. msleep(100);
  1318. /* I need to unmute audio here */
  1319. ctrl.value = 0;
  1320. cx23885_set_control(dev, &ctrl);
  1321. mutex_unlock(&dev->lock);
  1322. return 0;
  1323. }
  1324. static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
  1325. struct v4l2_frequency *f)
  1326. {
  1327. struct v4l2_control ctrl;
  1328. struct videobuf_dvb_frontend *vfe;
  1329. struct dvb_frontend *fe;
  1330. struct analog_parameters params = {
  1331. .mode = V4L2_TUNER_ANALOG_TV,
  1332. .audmode = V4L2_TUNER_MODE_STEREO,
  1333. .std = dev->tvnorm,
  1334. .frequency = f->frequency
  1335. };
  1336. mutex_lock(&dev->lock);
  1337. dev->freq = f->frequency;
  1338. /* I need to mute audio here */
  1339. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1340. ctrl.value = 1;
  1341. cx23885_set_control(dev, &ctrl);
  1342. /* If HVR1850 */
  1343. dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
  1344. params.frequency, f->tuner, params.std);
  1345. vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1);
  1346. if (!vfe) {
  1347. mutex_unlock(&dev->lock);
  1348. return -EINVAL;
  1349. }
  1350. fe = vfe->dvb.frontend;
  1351. if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
  1352. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
  1353. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
  1354. fe = &dev->ts1.analog_fe;
  1355. if (fe && fe->ops.tuner_ops.set_analog_params) {
  1356. call_all(dev, core, s_std, dev->tvnorm);
  1357. fe->ops.tuner_ops.set_analog_params(fe, &params);
  1358. }
  1359. else
  1360. printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
  1361. /* When changing channels it is required to reset TVAUDIO */
  1362. msleep(100);
  1363. /* I need to unmute audio here */
  1364. ctrl.value = 0;
  1365. cx23885_set_control(dev, &ctrl);
  1366. mutex_unlock(&dev->lock);
  1367. return 0;
  1368. }
  1369. int cx23885_set_frequency(struct file *file, void *priv,
  1370. struct v4l2_frequency *f)
  1371. {
  1372. struct cx23885_fh *fh = priv;
  1373. struct cx23885_dev *dev = fh->dev;
  1374. int ret;
  1375. switch (dev->board) {
  1376. case CX23885_BOARD_HAUPPAUGE_HVR1255:
  1377. case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
  1378. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  1379. ret = cx23885_set_freq_via_ops(dev, f);
  1380. break;
  1381. default:
  1382. ret = cx23885_set_freq(dev, f);
  1383. }
  1384. return ret;
  1385. }
  1386. static int vidioc_s_frequency(struct file *file, void *priv,
  1387. struct v4l2_frequency *f)
  1388. {
  1389. return cx23885_set_frequency(file, priv, f);
  1390. }
  1391. /* ----------------------------------------------------------- */
  1392. static void cx23885_vid_timeout(unsigned long data)
  1393. {
  1394. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1395. struct cx23885_dmaqueue *q = &dev->vidq;
  1396. struct cx23885_buffer *buf;
  1397. unsigned long flags;
  1398. spin_lock_irqsave(&dev->slock, flags);
  1399. while (!list_empty(&q->active)) {
  1400. buf = list_entry(q->active.next,
  1401. struct cx23885_buffer, vb.queue);
  1402. list_del(&buf->vb.queue);
  1403. buf->vb.state = VIDEOBUF_ERROR;
  1404. wake_up(&buf->vb.done);
  1405. printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
  1406. dev->name, buf, buf->vb.i,
  1407. (unsigned long)buf->risc.dma);
  1408. }
  1409. cx23885_restart_video_queue(dev, q);
  1410. spin_unlock_irqrestore(&dev->slock, flags);
  1411. }
  1412. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1413. {
  1414. u32 mask, count;
  1415. int handled = 0;
  1416. mask = cx_read(VID_A_INT_MSK);
  1417. if (0 == (status & mask))
  1418. return handled;
  1419. cx_write(VID_A_INT_STAT, status);
  1420. /* risc op code error, fifo overflow or line sync detection error */
  1421. if ((status & VID_BC_MSK_OPC_ERR) ||
  1422. (status & VID_BC_MSK_SYNC) ||
  1423. (status & VID_BC_MSK_OF)) {
  1424. if (status & VID_BC_MSK_OPC_ERR) {
  1425. dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
  1426. VID_BC_MSK_OPC_ERR);
  1427. printk(KERN_WARNING "%s: video risc op code error\n",
  1428. dev->name);
  1429. cx23885_sram_channel_dump(dev,
  1430. &dev->sram_channels[SRAM_CH01]);
  1431. }
  1432. if (status & VID_BC_MSK_SYNC)
  1433. dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
  1434. "video lines miss-match\n",
  1435. VID_BC_MSK_SYNC);
  1436. if (status & VID_BC_MSK_OF)
  1437. dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
  1438. VID_BC_MSK_OF);
  1439. }
  1440. /* Video */
  1441. if (status & VID_BC_MSK_RISCI1) {
  1442. spin_lock(&dev->slock);
  1443. count = cx_read(VID_A_GPCNT);
  1444. cx23885_video_wakeup(dev, &dev->vidq, count);
  1445. spin_unlock(&dev->slock);
  1446. handled++;
  1447. }
  1448. if (status & VID_BC_MSK_RISCI2) {
  1449. dprintk(2, "stopper video\n");
  1450. spin_lock(&dev->slock);
  1451. cx23885_restart_video_queue(dev, &dev->vidq);
  1452. spin_unlock(&dev->slock);
  1453. handled++;
  1454. }
  1455. /* Allow the VBI framework to process it's payload */
  1456. handled += cx23885_vbi_irq(dev, status);
  1457. return handled;
  1458. }
  1459. /* ----------------------------------------------------------- */
  1460. /* exported stuff */
  1461. static const struct v4l2_file_operations video_fops = {
  1462. .owner = THIS_MODULE,
  1463. .open = video_open,
  1464. .release = video_release,
  1465. .read = video_read,
  1466. .poll = video_poll,
  1467. .mmap = video_mmap,
  1468. .ioctl = video_ioctl2,
  1469. };
  1470. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1471. .vidioc_querycap = vidioc_querycap,
  1472. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1473. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1474. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1475. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1476. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  1477. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  1478. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  1479. .vidioc_reqbufs = vidioc_reqbufs,
  1480. .vidioc_querybuf = vidioc_querybuf,
  1481. .vidioc_qbuf = vidioc_qbuf,
  1482. .vidioc_dqbuf = vidioc_dqbuf,
  1483. .vidioc_s_std = vidioc_s_std,
  1484. .vidioc_g_std = vidioc_g_std,
  1485. .vidioc_querystd = vidioc_g_std,
  1486. .vidioc_enum_input = vidioc_enum_input,
  1487. .vidioc_g_input = vidioc_g_input,
  1488. .vidioc_s_input = vidioc_s_input,
  1489. .vidioc_log_status = vidioc_log_status,
  1490. .vidioc_queryctrl = vidioc_queryctrl,
  1491. .vidioc_g_ctrl = vidioc_g_ctrl,
  1492. .vidioc_s_ctrl = vidioc_s_ctrl,
  1493. .vidioc_streamon = vidioc_streamon,
  1494. .vidioc_streamoff = vidioc_streamoff,
  1495. .vidioc_g_tuner = vidioc_g_tuner,
  1496. .vidioc_s_tuner = vidioc_s_tuner,
  1497. .vidioc_g_frequency = vidioc_g_frequency,
  1498. .vidioc_s_frequency = vidioc_s_frequency,
  1499. .vidioc_g_chip_ident = cx23885_g_chip_ident,
  1500. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1501. .vidioc_g_register = cx23885_g_register,
  1502. .vidioc_s_register = cx23885_s_register,
  1503. #endif
  1504. .vidioc_enumaudio = vidioc_enum_audinput,
  1505. .vidioc_g_audio = vidioc_g_audinput,
  1506. .vidioc_s_audio = vidioc_s_audinput,
  1507. };
  1508. static struct video_device cx23885_vbi_template;
  1509. static struct video_device cx23885_video_template = {
  1510. .name = "cx23885-video",
  1511. .fops = &video_fops,
  1512. .ioctl_ops = &video_ioctl_ops,
  1513. .tvnorms = CX23885_NORMS,
  1514. .current_norm = V4L2_STD_NTSC_M,
  1515. };
  1516. static const struct v4l2_file_operations radio_fops = {
  1517. .owner = THIS_MODULE,
  1518. .open = video_open,
  1519. .release = video_release,
  1520. .ioctl = video_ioctl2,
  1521. };
  1522. void cx23885_video_unregister(struct cx23885_dev *dev)
  1523. {
  1524. dprintk(1, "%s()\n", __func__);
  1525. cx23885_irq_remove(dev, 0x01);
  1526. if (dev->vbi_dev) {
  1527. if (video_is_registered(dev->vbi_dev))
  1528. video_unregister_device(dev->vbi_dev);
  1529. else
  1530. video_device_release(dev->vbi_dev);
  1531. dev->vbi_dev = NULL;
  1532. btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
  1533. }
  1534. if (dev->video_dev) {
  1535. if (video_is_registered(dev->video_dev))
  1536. video_unregister_device(dev->video_dev);
  1537. else
  1538. video_device_release(dev->video_dev);
  1539. dev->video_dev = NULL;
  1540. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1541. }
  1542. if (dev->audio_dev)
  1543. cx23885_audio_unregister(dev);
  1544. }
  1545. int cx23885_video_register(struct cx23885_dev *dev)
  1546. {
  1547. int err;
  1548. dprintk(1, "%s()\n", __func__);
  1549. spin_lock_init(&dev->slock);
  1550. /* Initialize VBI template */
  1551. memcpy(&cx23885_vbi_template, &cx23885_video_template,
  1552. sizeof(cx23885_vbi_template));
  1553. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1554. dev->tvnorm = cx23885_video_template.current_norm;
  1555. /* init video dma queues */
  1556. INIT_LIST_HEAD(&dev->vidq.active);
  1557. INIT_LIST_HEAD(&dev->vidq.queued);
  1558. dev->vidq.timeout.function = cx23885_vid_timeout;
  1559. dev->vidq.timeout.data = (unsigned long)dev;
  1560. init_timer(&dev->vidq.timeout);
  1561. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1562. VID_A_DMA_CTL, 0x11, 0x00);
  1563. /* init vbi dma queues */
  1564. INIT_LIST_HEAD(&dev->vbiq.active);
  1565. INIT_LIST_HEAD(&dev->vbiq.queued);
  1566. dev->vbiq.timeout.function = cx23885_vbi_timeout;
  1567. dev->vbiq.timeout.data = (unsigned long)dev;
  1568. init_timer(&dev->vbiq.timeout);
  1569. cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
  1570. VID_A_DMA_CTL, 0x22, 0x00);
  1571. cx23885_irq_add_enable(dev, 0x01);
  1572. if ((TUNER_ABSENT != dev->tuner_type) &&
  1573. ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
  1574. struct v4l2_subdev *sd = NULL;
  1575. if (dev->tuner_addr)
  1576. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1577. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  1578. "tuner", dev->tuner_addr, NULL);
  1579. else
  1580. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1581. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  1582. "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
  1583. if (sd) {
  1584. struct tuner_setup tun_setup;
  1585. memset(&tun_setup, 0, sizeof(tun_setup));
  1586. tun_setup.mode_mask = T_ANALOG_TV;
  1587. tun_setup.type = dev->tuner_type;
  1588. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  1589. tun_setup.tuner_callback = cx23885_tuner_callback;
  1590. v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
  1591. if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
  1592. struct xc2028_ctrl ctrl = {
  1593. .fname = XC2028_DEFAULT_FIRMWARE,
  1594. .max_len = 64
  1595. };
  1596. struct v4l2_priv_tun_config cfg = {
  1597. .tuner = dev->tuner_type,
  1598. .priv = &ctrl
  1599. };
  1600. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1601. }
  1602. }
  1603. }
  1604. /* register Video device */
  1605. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1606. &cx23885_video_template, "video");
  1607. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1608. video_nr[dev->nr]);
  1609. if (err < 0) {
  1610. printk(KERN_INFO "%s: can't register video device\n",
  1611. dev->name);
  1612. goto fail_unreg;
  1613. }
  1614. printk(KERN_INFO "%s: registered device %s [v4l2]\n",
  1615. dev->name, video_device_node_name(dev->video_dev));
  1616. /* register VBI device */
  1617. dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
  1618. &cx23885_vbi_template, "vbi");
  1619. err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1620. vbi_nr[dev->nr]);
  1621. if (err < 0) {
  1622. printk(KERN_INFO "%s: can't register vbi device\n",
  1623. dev->name);
  1624. goto fail_unreg;
  1625. }
  1626. printk(KERN_INFO "%s: registered device %s\n",
  1627. dev->name, video_device_node_name(dev->vbi_dev));
  1628. /* Register ALSA audio device */
  1629. dev->audio_dev = cx23885_audio_register(dev);
  1630. /* initial device configuration */
  1631. mutex_lock(&dev->lock);
  1632. cx23885_set_tvnorm(dev, dev->tvnorm);
  1633. init_controls(dev);
  1634. cx23885_video_mux(dev, 0);
  1635. cx23885_audio_mux(dev, 0);
  1636. mutex_unlock(&dev->lock);
  1637. return 0;
  1638. fail_unreg:
  1639. cx23885_video_unregister(dev);
  1640. return err;
  1641. }