cx23885-video.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  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. (dev->board == CX23885_BOARD_AVERMEDIA_HC81R)) {
  454. /* Configure audio routing */
  455. v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
  456. INPUT(input)->amux, 0, 0);
  457. if (INPUT(input)->amux == CX25840_AUDIO7)
  458. cx23885_flatiron_mux(dev, 1);
  459. else if (INPUT(input)->amux == CX25840_AUDIO6)
  460. cx23885_flatiron_mux(dev, 2);
  461. }
  462. return 0;
  463. }
  464. static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
  465. {
  466. dprintk(1, "%s(input=%d)\n", __func__, input);
  467. /* The baseband video core of the cx23885 has two audio inputs.
  468. * LR1 and LR2. In almost every single case so far only HVR1xxx
  469. * cards we've only ever supported LR1. Time to support LR2,
  470. * which is available via the optional white breakout header on
  471. * the board.
  472. * We'll use a could of existing enums in the card struct to allow
  473. * devs to specify which baseband input they need, or just default
  474. * to what we've always used.
  475. */
  476. if (INPUT(input)->amux == CX25840_AUDIO7)
  477. cx23885_flatiron_mux(dev, 1);
  478. else if (INPUT(input)->amux == CX25840_AUDIO6)
  479. cx23885_flatiron_mux(dev, 2);
  480. else {
  481. /* Not specifically defined, assume the default. */
  482. cx23885_flatiron_mux(dev, 1);
  483. }
  484. return 0;
  485. }
  486. /* ------------------------------------------------------------------ */
  487. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  488. struct cx23885_dmaqueue *q,
  489. struct cx23885_buffer *buf)
  490. {
  491. dprintk(1, "%s()\n", __func__);
  492. /* Stop the dma/fifo before we tamper with it's risc programs */
  493. cx_clear(VID_A_DMA_CTL, 0x11);
  494. /* setup fifo + format */
  495. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  496. buf->bpl, buf->risc.dma);
  497. /* reset counter */
  498. cx_write(VID_A_GPCNT_CTL, 3);
  499. q->count = 1;
  500. /* enable irq */
  501. cx23885_irq_add_enable(dev, 0x01);
  502. cx_set(VID_A_INT_MSK, 0x000011);
  503. /* start dma */
  504. cx_set(DEV_CNTRL2, (1<<5));
  505. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  506. return 0;
  507. }
  508. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  509. struct cx23885_dmaqueue *q)
  510. {
  511. struct cx23885_buffer *buf, *prev;
  512. struct list_head *item;
  513. dprintk(1, "%s()\n", __func__);
  514. if (!list_empty(&q->active)) {
  515. buf = list_entry(q->active.next, struct cx23885_buffer,
  516. vb.queue);
  517. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  518. buf, buf->vb.i);
  519. cx23885_start_video_dma(dev, q, buf);
  520. list_for_each(item, &q->active) {
  521. buf = list_entry(item, struct cx23885_buffer,
  522. vb.queue);
  523. buf->count = q->count++;
  524. }
  525. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  526. return 0;
  527. }
  528. prev = NULL;
  529. for (;;) {
  530. if (list_empty(&q->queued))
  531. return 0;
  532. buf = list_entry(q->queued.next, struct cx23885_buffer,
  533. vb.queue);
  534. if (NULL == prev) {
  535. list_move_tail(&buf->vb.queue, &q->active);
  536. cx23885_start_video_dma(dev, q, buf);
  537. buf->vb.state = VIDEOBUF_ACTIVE;
  538. buf->count = q->count++;
  539. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  540. dprintk(2, "[%p/%d] restart_queue - first active\n",
  541. buf, buf->vb.i);
  542. } else if (prev->vb.width == buf->vb.width &&
  543. prev->vb.height == buf->vb.height &&
  544. prev->fmt == buf->fmt) {
  545. list_move_tail(&buf->vb.queue, &q->active);
  546. buf->vb.state = VIDEOBUF_ACTIVE;
  547. buf->count = q->count++;
  548. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  549. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  550. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  551. buf, buf->vb.i);
  552. } else {
  553. return 0;
  554. }
  555. prev = buf;
  556. }
  557. }
  558. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  559. unsigned int *size)
  560. {
  561. struct cx23885_fh *fh = q->priv_data;
  562. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  563. if (0 == *count)
  564. *count = 32;
  565. if (*size * *count > vid_limit * 1024 * 1024)
  566. *count = (vid_limit * 1024 * 1024) / *size;
  567. return 0;
  568. }
  569. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  570. enum v4l2_field field)
  571. {
  572. struct cx23885_fh *fh = q->priv_data;
  573. struct cx23885_dev *dev = fh->dev;
  574. struct cx23885_buffer *buf =
  575. container_of(vb, struct cx23885_buffer, vb);
  576. int rc, init_buffer = 0;
  577. u32 line0_offset, line1_offset;
  578. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  579. int field_tff;
  580. BUG_ON(NULL == fh->fmt);
  581. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  582. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  583. return -EINVAL;
  584. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  585. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  586. return -EINVAL;
  587. if (buf->fmt != fh->fmt ||
  588. buf->vb.width != fh->width ||
  589. buf->vb.height != fh->height ||
  590. buf->vb.field != field) {
  591. buf->fmt = fh->fmt;
  592. buf->vb.width = fh->width;
  593. buf->vb.height = fh->height;
  594. buf->vb.field = field;
  595. init_buffer = 1;
  596. }
  597. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  598. init_buffer = 1;
  599. rc = videobuf_iolock(q, &buf->vb, NULL);
  600. if (0 != rc)
  601. goto fail;
  602. }
  603. if (init_buffer) {
  604. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  605. switch (buf->vb.field) {
  606. case V4L2_FIELD_TOP:
  607. cx23885_risc_buffer(dev->pci, &buf->risc,
  608. dma->sglist, 0, UNSET,
  609. buf->bpl, 0, buf->vb.height);
  610. break;
  611. case V4L2_FIELD_BOTTOM:
  612. cx23885_risc_buffer(dev->pci, &buf->risc,
  613. dma->sglist, UNSET, 0,
  614. buf->bpl, 0, buf->vb.height);
  615. break;
  616. case V4L2_FIELD_INTERLACED:
  617. if (dev->tvnorm & V4L2_STD_NTSC)
  618. /* NTSC or */
  619. field_tff = 1;
  620. else
  621. field_tff = 0;
  622. if (cx23885_boards[dev->board].force_bff)
  623. /* PAL / SECAM OR 888 in NTSC MODE */
  624. field_tff = 0;
  625. if (field_tff) {
  626. /* cx25840 transmits NTSC bottom field first */
  627. dprintk(1, "%s() Creating TFF/NTSC risc\n",
  628. __func__);
  629. line0_offset = buf->bpl;
  630. line1_offset = 0;
  631. } else {
  632. /* All other formats are top field first */
  633. dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
  634. __func__);
  635. line0_offset = 0;
  636. line1_offset = buf->bpl;
  637. }
  638. cx23885_risc_buffer(dev->pci, &buf->risc,
  639. dma->sglist, line0_offset,
  640. line1_offset,
  641. buf->bpl, buf->bpl,
  642. buf->vb.height >> 1);
  643. break;
  644. case V4L2_FIELD_SEQ_TB:
  645. cx23885_risc_buffer(dev->pci, &buf->risc,
  646. dma->sglist,
  647. 0, buf->bpl * (buf->vb.height >> 1),
  648. buf->bpl, 0,
  649. buf->vb.height >> 1);
  650. break;
  651. case V4L2_FIELD_SEQ_BT:
  652. cx23885_risc_buffer(dev->pci, &buf->risc,
  653. dma->sglist,
  654. buf->bpl * (buf->vb.height >> 1), 0,
  655. buf->bpl, 0,
  656. buf->vb.height >> 1);
  657. break;
  658. default:
  659. BUG();
  660. }
  661. }
  662. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  663. buf, buf->vb.i,
  664. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  665. (unsigned long)buf->risc.dma);
  666. buf->vb.state = VIDEOBUF_PREPARED;
  667. return 0;
  668. fail:
  669. cx23885_free_buffer(q, buf);
  670. return rc;
  671. }
  672. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  673. {
  674. struct cx23885_buffer *buf = container_of(vb,
  675. struct cx23885_buffer, vb);
  676. struct cx23885_buffer *prev;
  677. struct cx23885_fh *fh = vq->priv_data;
  678. struct cx23885_dev *dev = fh->dev;
  679. struct cx23885_dmaqueue *q = &dev->vidq;
  680. /* add jump to stopper */
  681. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  682. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  683. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  684. if (!list_empty(&q->queued)) {
  685. list_add_tail(&buf->vb.queue, &q->queued);
  686. buf->vb.state = VIDEOBUF_QUEUED;
  687. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  688. buf, buf->vb.i);
  689. } else if (list_empty(&q->active)) {
  690. list_add_tail(&buf->vb.queue, &q->active);
  691. cx23885_start_video_dma(dev, q, buf);
  692. buf->vb.state = VIDEOBUF_ACTIVE;
  693. buf->count = q->count++;
  694. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  695. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  696. buf, buf->vb.i);
  697. } else {
  698. prev = list_entry(q->active.prev, struct cx23885_buffer,
  699. vb.queue);
  700. if (prev->vb.width == buf->vb.width &&
  701. prev->vb.height == buf->vb.height &&
  702. prev->fmt == buf->fmt) {
  703. list_add_tail(&buf->vb.queue, &q->active);
  704. buf->vb.state = VIDEOBUF_ACTIVE;
  705. buf->count = q->count++;
  706. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  707. /* 64 bit bits 63-32 */
  708. prev->risc.jmp[2] = cpu_to_le32(0);
  709. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  710. buf, buf->vb.i);
  711. } else {
  712. list_add_tail(&buf->vb.queue, &q->queued);
  713. buf->vb.state = VIDEOBUF_QUEUED;
  714. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  715. buf, buf->vb.i);
  716. }
  717. }
  718. }
  719. static void buffer_release(struct videobuf_queue *q,
  720. struct videobuf_buffer *vb)
  721. {
  722. struct cx23885_buffer *buf = container_of(vb,
  723. struct cx23885_buffer, vb);
  724. cx23885_free_buffer(q, buf);
  725. }
  726. static struct videobuf_queue_ops cx23885_video_qops = {
  727. .buf_setup = buffer_setup,
  728. .buf_prepare = buffer_prepare,
  729. .buf_queue = buffer_queue,
  730. .buf_release = buffer_release,
  731. };
  732. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  733. {
  734. switch (fh->type) {
  735. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  736. return &fh->vidq;
  737. case V4L2_BUF_TYPE_VBI_CAPTURE:
  738. return &fh->vbiq;
  739. default:
  740. BUG();
  741. return NULL;
  742. }
  743. }
  744. static int get_resource(struct cx23885_fh *fh)
  745. {
  746. switch (fh->type) {
  747. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  748. return RESOURCE_VIDEO;
  749. case V4L2_BUF_TYPE_VBI_CAPTURE:
  750. return RESOURCE_VBI;
  751. default:
  752. BUG();
  753. return 0;
  754. }
  755. }
  756. static int video_open(struct file *file)
  757. {
  758. struct video_device *vdev = video_devdata(file);
  759. struct cx23885_dev *dev = video_drvdata(file);
  760. struct cx23885_fh *fh;
  761. enum v4l2_buf_type type = 0;
  762. int radio = 0;
  763. switch (vdev->vfl_type) {
  764. case VFL_TYPE_GRABBER:
  765. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  766. break;
  767. case VFL_TYPE_VBI:
  768. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  769. break;
  770. case VFL_TYPE_RADIO:
  771. radio = 1;
  772. break;
  773. }
  774. dprintk(1, "open dev=%s radio=%d type=%s\n",
  775. video_device_node_name(vdev), radio, v4l2_type_names[type]);
  776. /* allocate + initialize per filehandle data */
  777. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  778. if (NULL == fh)
  779. return -ENOMEM;
  780. file->private_data = fh;
  781. fh->dev = dev;
  782. fh->radio = radio;
  783. fh->type = type;
  784. fh->width = 320;
  785. fh->height = 240;
  786. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
  787. videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
  788. &dev->pci->dev, &dev->slock,
  789. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  790. V4L2_FIELD_INTERLACED,
  791. sizeof(struct cx23885_buffer),
  792. fh, NULL);
  793. videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
  794. &dev->pci->dev, &dev->slock,
  795. V4L2_BUF_TYPE_VBI_CAPTURE,
  796. V4L2_FIELD_SEQ_TB,
  797. sizeof(struct cx23885_buffer),
  798. fh, NULL);
  799. dprintk(1, "post videobuf_queue_init()\n");
  800. return 0;
  801. }
  802. static ssize_t video_read(struct file *file, char __user *data,
  803. size_t count, loff_t *ppos)
  804. {
  805. struct cx23885_fh *fh = file->private_data;
  806. switch (fh->type) {
  807. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  808. if (res_locked(fh->dev, RESOURCE_VIDEO))
  809. return -EBUSY;
  810. return videobuf_read_one(&fh->vidq, data, count, ppos,
  811. file->f_flags & O_NONBLOCK);
  812. case V4L2_BUF_TYPE_VBI_CAPTURE:
  813. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  814. return -EBUSY;
  815. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  816. file->f_flags & O_NONBLOCK);
  817. default:
  818. BUG();
  819. return 0;
  820. }
  821. }
  822. static unsigned int video_poll(struct file *file,
  823. struct poll_table_struct *wait)
  824. {
  825. struct cx23885_fh *fh = file->private_data;
  826. struct cx23885_buffer *buf;
  827. unsigned int rc = POLLERR;
  828. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  829. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  830. return POLLERR;
  831. return videobuf_poll_stream(file, &fh->vbiq, wait);
  832. }
  833. mutex_lock(&fh->vidq.vb_lock);
  834. if (res_check(fh, RESOURCE_VIDEO)) {
  835. /* streaming capture */
  836. if (list_empty(&fh->vidq.stream))
  837. goto done;
  838. buf = list_entry(fh->vidq.stream.next,
  839. struct cx23885_buffer, vb.stream);
  840. } else {
  841. /* read() capture */
  842. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  843. if (NULL == buf)
  844. goto done;
  845. }
  846. poll_wait(file, &buf->vb.done, wait);
  847. if (buf->vb.state == VIDEOBUF_DONE ||
  848. buf->vb.state == VIDEOBUF_ERROR)
  849. rc = POLLIN|POLLRDNORM;
  850. else
  851. rc = 0;
  852. done:
  853. mutex_unlock(&fh->vidq.vb_lock);
  854. return rc;
  855. }
  856. static int video_release(struct file *file)
  857. {
  858. struct cx23885_fh *fh = file->private_data;
  859. struct cx23885_dev *dev = fh->dev;
  860. /* turn off overlay */
  861. if (res_check(fh, RESOURCE_OVERLAY)) {
  862. /* FIXME */
  863. res_free(dev, fh, RESOURCE_OVERLAY);
  864. }
  865. /* stop video capture */
  866. if (res_check(fh, RESOURCE_VIDEO)) {
  867. videobuf_queue_cancel(&fh->vidq);
  868. res_free(dev, fh, RESOURCE_VIDEO);
  869. }
  870. if (fh->vidq.read_buf) {
  871. buffer_release(&fh->vidq, fh->vidq.read_buf);
  872. kfree(fh->vidq.read_buf);
  873. }
  874. /* stop vbi capture */
  875. if (res_check(fh, RESOURCE_VBI)) {
  876. if (fh->vbiq.streaming)
  877. videobuf_streamoff(&fh->vbiq);
  878. if (fh->vbiq.reading)
  879. videobuf_read_stop(&fh->vbiq);
  880. res_free(dev, fh, RESOURCE_VBI);
  881. }
  882. videobuf_mmap_free(&fh->vidq);
  883. videobuf_mmap_free(&fh->vbiq);
  884. file->private_data = NULL;
  885. kfree(fh);
  886. /* We are not putting the tuner to sleep here on exit, because
  887. * we want to use the mpeg encoder in another session to capture
  888. * tuner video. Closing this will result in no video to the encoder.
  889. */
  890. return 0;
  891. }
  892. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  893. {
  894. struct cx23885_fh *fh = file->private_data;
  895. return videobuf_mmap_mapper(get_queue(fh), vma);
  896. }
  897. /* ------------------------------------------------------------------ */
  898. /* VIDEO CTRL IOCTLS */
  899. int cx23885_get_control(struct cx23885_dev *dev,
  900. struct v4l2_control *ctl)
  901. {
  902. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
  903. call_all(dev, core, g_ctrl, ctl);
  904. return 0;
  905. }
  906. int cx23885_set_control(struct cx23885_dev *dev,
  907. struct v4l2_control *ctl)
  908. {
  909. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
  910. call_all(dev, core, s_ctrl, ctl);
  911. return 0;
  912. }
  913. static void init_controls(struct cx23885_dev *dev)
  914. {
  915. struct v4l2_control ctrl;
  916. int i;
  917. for (i = 0; i < CX23885_CTLS; i++) {
  918. ctrl.id = cx23885_ctls[i].v.id;
  919. ctrl.value = cx23885_ctls[i].v.default_value;
  920. cx23885_set_control(dev, &ctrl);
  921. }
  922. }
  923. /* ------------------------------------------------------------------ */
  924. /* VIDEO IOCTLS */
  925. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  926. struct v4l2_format *f)
  927. {
  928. struct cx23885_fh *fh = priv;
  929. f->fmt.pix.width = fh->width;
  930. f->fmt.pix.height = fh->height;
  931. f->fmt.pix.field = fh->vidq.field;
  932. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  933. f->fmt.pix.bytesperline =
  934. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  935. f->fmt.pix.sizeimage =
  936. f->fmt.pix.height * f->fmt.pix.bytesperline;
  937. return 0;
  938. }
  939. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  940. struct v4l2_format *f)
  941. {
  942. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  943. struct cx23885_fmt *fmt;
  944. enum v4l2_field field;
  945. unsigned int maxw, maxh;
  946. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  947. if (NULL == fmt)
  948. return -EINVAL;
  949. field = f->fmt.pix.field;
  950. maxw = norm_maxw(dev->tvnorm);
  951. maxh = norm_maxh(dev->tvnorm);
  952. if (V4L2_FIELD_ANY == field) {
  953. field = (f->fmt.pix.height > maxh/2)
  954. ? V4L2_FIELD_INTERLACED
  955. : V4L2_FIELD_BOTTOM;
  956. }
  957. switch (field) {
  958. case V4L2_FIELD_TOP:
  959. case V4L2_FIELD_BOTTOM:
  960. maxh = maxh / 2;
  961. break;
  962. case V4L2_FIELD_INTERLACED:
  963. break;
  964. default:
  965. return -EINVAL;
  966. }
  967. f->fmt.pix.field = field;
  968. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  969. &f->fmt.pix.height, 32, maxh, 0, 0);
  970. f->fmt.pix.bytesperline =
  971. (f->fmt.pix.width * fmt->depth) >> 3;
  972. f->fmt.pix.sizeimage =
  973. f->fmt.pix.height * f->fmt.pix.bytesperline;
  974. return 0;
  975. }
  976. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  977. struct v4l2_format *f)
  978. {
  979. struct cx23885_fh *fh = priv;
  980. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  981. struct v4l2_mbus_framefmt mbus_fmt;
  982. int err;
  983. dprintk(2, "%s()\n", __func__);
  984. err = vidioc_try_fmt_vid_cap(file, priv, f);
  985. if (0 != err)
  986. return err;
  987. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  988. fh->width = f->fmt.pix.width;
  989. fh->height = f->fmt.pix.height;
  990. fh->vidq.field = f->fmt.pix.field;
  991. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  992. fh->width, fh->height, fh->vidq.field);
  993. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  994. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  995. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  996. return 0;
  997. }
  998. static int vidioc_querycap(struct file *file, void *priv,
  999. struct v4l2_capability *cap)
  1000. {
  1001. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1002. strcpy(cap->driver, "cx23885");
  1003. strlcpy(cap->card, cx23885_boards[dev->board].name,
  1004. sizeof(cap->card));
  1005. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  1006. cap->capabilities =
  1007. V4L2_CAP_VIDEO_CAPTURE |
  1008. V4L2_CAP_READWRITE |
  1009. V4L2_CAP_STREAMING |
  1010. V4L2_CAP_VBI_CAPTURE;
  1011. if (UNSET != dev->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,
  1021. sizeof(f->description));
  1022. f->pixelformat = formats[f->index].fourcc;
  1023. return 0;
  1024. }
  1025. static int vidioc_reqbufs(struct file *file, void *priv,
  1026. struct v4l2_requestbuffers *p)
  1027. {
  1028. struct cx23885_fh *fh = priv;
  1029. return videobuf_reqbufs(get_queue(fh), p);
  1030. }
  1031. static int vidioc_querybuf(struct file *file, void *priv,
  1032. struct v4l2_buffer *p)
  1033. {
  1034. struct cx23885_fh *fh = priv;
  1035. return videobuf_querybuf(get_queue(fh), p);
  1036. }
  1037. static int vidioc_qbuf(struct file *file, void *priv,
  1038. struct v4l2_buffer *p)
  1039. {
  1040. struct cx23885_fh *fh = priv;
  1041. return videobuf_qbuf(get_queue(fh), p);
  1042. }
  1043. static int vidioc_dqbuf(struct file *file, void *priv,
  1044. struct v4l2_buffer *p)
  1045. {
  1046. struct cx23885_fh *fh = priv;
  1047. return videobuf_dqbuf(get_queue(fh), p,
  1048. file->f_flags & O_NONBLOCK);
  1049. }
  1050. static int vidioc_streamon(struct file *file, void *priv,
  1051. enum v4l2_buf_type i)
  1052. {
  1053. struct cx23885_fh *fh = priv;
  1054. struct cx23885_dev *dev = fh->dev;
  1055. dprintk(1, "%s()\n", __func__);
  1056. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1057. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1058. return -EINVAL;
  1059. if (unlikely(i != fh->type))
  1060. return -EINVAL;
  1061. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  1062. return -EBUSY;
  1063. /* Don't start VBI streaming unless vida streaming
  1064. * has already started.
  1065. */
  1066. if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
  1067. ((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
  1068. return -EINVAL;
  1069. return videobuf_streamon(get_queue(fh));
  1070. }
  1071. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1072. {
  1073. struct cx23885_fh *fh = priv;
  1074. struct cx23885_dev *dev = fh->dev;
  1075. int err, res;
  1076. dprintk(1, "%s()\n", __func__);
  1077. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1078. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1079. return -EINVAL;
  1080. if (i != fh->type)
  1081. return -EINVAL;
  1082. res = get_resource(fh);
  1083. err = videobuf_streamoff(get_queue(fh));
  1084. if (err < 0)
  1085. return err;
  1086. res_free(dev, fh, res);
  1087. return 0;
  1088. }
  1089. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1090. {
  1091. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1092. dprintk(1, "%s()\n", __func__);
  1093. call_all(dev, core, g_std, id);
  1094. return 0;
  1095. }
  1096. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  1097. {
  1098. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1099. dprintk(1, "%s()\n", __func__);
  1100. mutex_lock(&dev->lock);
  1101. cx23885_set_tvnorm(dev, *tvnorms);
  1102. mutex_unlock(&dev->lock);
  1103. return 0;
  1104. }
  1105. int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  1106. {
  1107. static const char *iname[] = {
  1108. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  1109. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  1110. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  1111. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1112. [CX23885_VMUX_SVIDEO] = "S-Video",
  1113. [CX23885_VMUX_COMPONENT] = "Component",
  1114. [CX23885_VMUX_TELEVISION] = "Television",
  1115. [CX23885_VMUX_CABLE] = "Cable TV",
  1116. [CX23885_VMUX_DVB] = "DVB",
  1117. [CX23885_VMUX_DEBUG] = "for debug only",
  1118. };
  1119. unsigned int n;
  1120. dprintk(1, "%s()\n", __func__);
  1121. n = i->index;
  1122. if (n >= MAX_CX23885_INPUT)
  1123. return -EINVAL;
  1124. if (0 == INPUT(n)->type)
  1125. return -EINVAL;
  1126. i->index = n;
  1127. i->type = V4L2_INPUT_TYPE_CAMERA;
  1128. strcpy(i->name, iname[INPUT(n)->type]);
  1129. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1130. (CX23885_VMUX_CABLE == INPUT(n)->type)) {
  1131. i->type = V4L2_INPUT_TYPE_TUNER;
  1132. i->std = CX23885_NORMS;
  1133. }
  1134. /* Two selectable audio inputs for non-tv inputs */
  1135. if (INPUT(n)->type != CX23885_VMUX_TELEVISION)
  1136. i->audioset = 0x3;
  1137. if (dev->input == n) {
  1138. /* enum'd input matches our configured input.
  1139. * Ask the video decoder to process the call
  1140. * and give it an oppertunity to update the
  1141. * status field.
  1142. */
  1143. call_all(dev, video, g_input_status, &i->status);
  1144. }
  1145. return 0;
  1146. }
  1147. static int vidioc_enum_input(struct file *file, void *priv,
  1148. struct v4l2_input *i)
  1149. {
  1150. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1151. dprintk(1, "%s()\n", __func__);
  1152. return cx23885_enum_input(dev, i);
  1153. }
  1154. int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
  1155. {
  1156. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1157. *i = dev->input;
  1158. dprintk(1, "%s() returns %d\n", __func__, *i);
  1159. return 0;
  1160. }
  1161. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1162. {
  1163. return cx23885_get_input(file, priv, i);
  1164. }
  1165. int cx23885_set_input(struct file *file, void *priv, unsigned int i)
  1166. {
  1167. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1168. dprintk(1, "%s(%d)\n", __func__, i);
  1169. if (i >= MAX_CX23885_INPUT) {
  1170. dprintk(1, "%s() -EINVAL\n", __func__);
  1171. return -EINVAL;
  1172. }
  1173. if (INPUT(i)->type == 0)
  1174. return -EINVAL;
  1175. mutex_lock(&dev->lock);
  1176. cx23885_video_mux(dev, i);
  1177. /* By default establish the default audio input for the card also */
  1178. /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
  1179. cx23885_audio_mux(dev, i);
  1180. mutex_unlock(&dev->lock);
  1181. return 0;
  1182. }
  1183. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1184. {
  1185. return cx23885_set_input(file, priv, i);
  1186. }
  1187. static int vidioc_log_status(struct file *file, void *priv)
  1188. {
  1189. struct cx23885_fh *fh = priv;
  1190. struct cx23885_dev *dev = fh->dev;
  1191. printk(KERN_INFO
  1192. "%s/0: ============ START LOG STATUS ============\n",
  1193. dev->name);
  1194. call_all(dev, core, log_status);
  1195. printk(KERN_INFO
  1196. "%s/0: ============= END LOG STATUS =============\n",
  1197. dev->name);
  1198. return 0;
  1199. }
  1200. static int cx23885_query_audinput(struct file *file, void *priv,
  1201. struct v4l2_audio *i)
  1202. {
  1203. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1204. static const char *iname[] = {
  1205. [0] = "Baseband L/R 1",
  1206. [1] = "Baseband L/R 2",
  1207. };
  1208. unsigned int n;
  1209. dprintk(1, "%s()\n", __func__);
  1210. n = i->index;
  1211. if (n >= 2)
  1212. return -EINVAL;
  1213. memset(i, 0, sizeof(*i));
  1214. i->index = n;
  1215. strcpy(i->name, iname[n]);
  1216. i->capability = V4L2_AUDCAP_STEREO;
  1217. i->mode = V4L2_AUDMODE_AVL;
  1218. return 0;
  1219. }
  1220. static int vidioc_enum_audinput(struct file *file, void *priv,
  1221. struct v4l2_audio *i)
  1222. {
  1223. return cx23885_query_audinput(file, priv, i);
  1224. }
  1225. static int vidioc_g_audinput(struct file *file, void *priv,
  1226. struct v4l2_audio *i)
  1227. {
  1228. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1229. i->index = dev->audinput;
  1230. dprintk(1, "%s(input=%d)\n", __func__, i->index);
  1231. return cx23885_query_audinput(file, priv, i);
  1232. }
  1233. static int vidioc_s_audinput(struct file *file, void *priv,
  1234. const struct v4l2_audio *i)
  1235. {
  1236. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1237. if (i->index >= 2)
  1238. return -EINVAL;
  1239. dprintk(1, "%s(%d)\n", __func__, i->index);
  1240. dev->audinput = i->index;
  1241. /* Skip the audio defaults from the cards struct, caller wants
  1242. * directly touch the audio mux hardware. */
  1243. cx23885_flatiron_mux(dev, dev->audinput + 1);
  1244. return 0;
  1245. }
  1246. static int vidioc_queryctrl(struct file *file, void *priv,
  1247. struct v4l2_queryctrl *qctrl)
  1248. {
  1249. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1250. if (unlikely(qctrl->id == 0))
  1251. return -EINVAL;
  1252. return cx23885_ctrl_query(qctrl);
  1253. }
  1254. static int vidioc_g_ctrl(struct file *file, void *priv,
  1255. struct v4l2_control *ctl)
  1256. {
  1257. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1258. return cx23885_get_control(dev, ctl);
  1259. }
  1260. static int vidioc_s_ctrl(struct file *file, void *priv,
  1261. struct v4l2_control *ctl)
  1262. {
  1263. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1264. return cx23885_set_control(dev, ctl);
  1265. }
  1266. static int vidioc_g_tuner(struct file *file, void *priv,
  1267. struct v4l2_tuner *t)
  1268. {
  1269. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1270. if (unlikely(UNSET == dev->tuner_type))
  1271. return -EINVAL;
  1272. if (0 != t->index)
  1273. return -EINVAL;
  1274. strcpy(t->name, "Television");
  1275. call_all(dev, tuner, g_tuner, t);
  1276. return 0;
  1277. }
  1278. static int vidioc_s_tuner(struct file *file, void *priv,
  1279. struct v4l2_tuner *t)
  1280. {
  1281. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1282. if (UNSET == dev->tuner_type)
  1283. return -EINVAL;
  1284. if (0 != t->index)
  1285. return -EINVAL;
  1286. /* Update the A/V core */
  1287. call_all(dev, tuner, s_tuner, t);
  1288. return 0;
  1289. }
  1290. static int vidioc_g_frequency(struct file *file, void *priv,
  1291. struct v4l2_frequency *f)
  1292. {
  1293. struct cx23885_fh *fh = priv;
  1294. struct cx23885_dev *dev = fh->dev;
  1295. if (unlikely(UNSET == dev->tuner_type))
  1296. return -EINVAL;
  1297. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1298. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1299. f->frequency = dev->freq;
  1300. call_all(dev, tuner, g_frequency, f);
  1301. return 0;
  1302. }
  1303. static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1304. {
  1305. struct v4l2_control ctrl;
  1306. if (unlikely(UNSET == dev->tuner_type))
  1307. return -EINVAL;
  1308. if (unlikely(f->tuner != 0))
  1309. return -EINVAL;
  1310. mutex_lock(&dev->lock);
  1311. dev->freq = f->frequency;
  1312. /* I need to mute audio here */
  1313. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1314. ctrl.value = 1;
  1315. cx23885_set_control(dev, &ctrl);
  1316. call_all(dev, tuner, s_frequency, f);
  1317. /* When changing channels it is required to reset TVAUDIO */
  1318. msleep(100);
  1319. /* I need to unmute audio here */
  1320. ctrl.value = 0;
  1321. cx23885_set_control(dev, &ctrl);
  1322. mutex_unlock(&dev->lock);
  1323. return 0;
  1324. }
  1325. static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
  1326. struct v4l2_frequency *f)
  1327. {
  1328. struct v4l2_control ctrl;
  1329. struct videobuf_dvb_frontend *vfe;
  1330. struct dvb_frontend *fe;
  1331. struct analog_parameters params = {
  1332. .mode = V4L2_TUNER_ANALOG_TV,
  1333. .audmode = V4L2_TUNER_MODE_STEREO,
  1334. .std = dev->tvnorm,
  1335. .frequency = f->frequency
  1336. };
  1337. mutex_lock(&dev->lock);
  1338. dev->freq = f->frequency;
  1339. /* I need to mute audio here */
  1340. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1341. ctrl.value = 1;
  1342. cx23885_set_control(dev, &ctrl);
  1343. /* If HVR1850 */
  1344. dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
  1345. params.frequency, f->tuner, params.std);
  1346. vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1);
  1347. if (!vfe) {
  1348. mutex_unlock(&dev->lock);
  1349. return -EINVAL;
  1350. }
  1351. fe = vfe->dvb.frontend;
  1352. if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
  1353. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
  1354. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
  1355. fe = &dev->ts1.analog_fe;
  1356. if (fe && fe->ops.tuner_ops.set_analog_params) {
  1357. call_all(dev, core, s_std, dev->tvnorm);
  1358. fe->ops.tuner_ops.set_analog_params(fe, &params);
  1359. }
  1360. else
  1361. printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
  1362. /* When changing channels it is required to reset TVAUDIO */
  1363. msleep(100);
  1364. /* I need to unmute audio here */
  1365. ctrl.value = 0;
  1366. cx23885_set_control(dev, &ctrl);
  1367. mutex_unlock(&dev->lock);
  1368. return 0;
  1369. }
  1370. int cx23885_set_frequency(struct file *file, void *priv,
  1371. struct v4l2_frequency *f)
  1372. {
  1373. struct cx23885_fh *fh = priv;
  1374. struct cx23885_dev *dev = fh->dev;
  1375. int ret;
  1376. switch (dev->board) {
  1377. case CX23885_BOARD_HAUPPAUGE_HVR1255:
  1378. case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
  1379. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  1380. ret = cx23885_set_freq_via_ops(dev, f);
  1381. break;
  1382. default:
  1383. ret = cx23885_set_freq(dev, f);
  1384. }
  1385. return ret;
  1386. }
  1387. static int vidioc_s_frequency(struct file *file, void *priv,
  1388. struct v4l2_frequency *f)
  1389. {
  1390. return cx23885_set_frequency(file, priv, f);
  1391. }
  1392. /* ----------------------------------------------------------- */
  1393. static void cx23885_vid_timeout(unsigned long data)
  1394. {
  1395. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1396. struct cx23885_dmaqueue *q = &dev->vidq;
  1397. struct cx23885_buffer *buf;
  1398. unsigned long flags;
  1399. spin_lock_irqsave(&dev->slock, flags);
  1400. while (!list_empty(&q->active)) {
  1401. buf = list_entry(q->active.next,
  1402. struct cx23885_buffer, vb.queue);
  1403. list_del(&buf->vb.queue);
  1404. buf->vb.state = VIDEOBUF_ERROR;
  1405. wake_up(&buf->vb.done);
  1406. printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
  1407. dev->name, buf, buf->vb.i,
  1408. (unsigned long)buf->risc.dma);
  1409. }
  1410. cx23885_restart_video_queue(dev, q);
  1411. spin_unlock_irqrestore(&dev->slock, flags);
  1412. }
  1413. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1414. {
  1415. u32 mask, count;
  1416. int handled = 0;
  1417. mask = cx_read(VID_A_INT_MSK);
  1418. if (0 == (status & mask))
  1419. return handled;
  1420. cx_write(VID_A_INT_STAT, status);
  1421. /* risc op code error, fifo overflow or line sync detection error */
  1422. if ((status & VID_BC_MSK_OPC_ERR) ||
  1423. (status & VID_BC_MSK_SYNC) ||
  1424. (status & VID_BC_MSK_OF)) {
  1425. if (status & VID_BC_MSK_OPC_ERR) {
  1426. dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
  1427. VID_BC_MSK_OPC_ERR);
  1428. printk(KERN_WARNING "%s: video risc op code error\n",
  1429. dev->name);
  1430. cx23885_sram_channel_dump(dev,
  1431. &dev->sram_channels[SRAM_CH01]);
  1432. }
  1433. if (status & VID_BC_MSK_SYNC)
  1434. dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
  1435. "video lines miss-match\n",
  1436. VID_BC_MSK_SYNC);
  1437. if (status & VID_BC_MSK_OF)
  1438. dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
  1439. VID_BC_MSK_OF);
  1440. }
  1441. /* Video */
  1442. if (status & VID_BC_MSK_RISCI1) {
  1443. spin_lock(&dev->slock);
  1444. count = cx_read(VID_A_GPCNT);
  1445. cx23885_video_wakeup(dev, &dev->vidq, count);
  1446. spin_unlock(&dev->slock);
  1447. handled++;
  1448. }
  1449. if (status & VID_BC_MSK_RISCI2) {
  1450. dprintk(2, "stopper video\n");
  1451. spin_lock(&dev->slock);
  1452. cx23885_restart_video_queue(dev, &dev->vidq);
  1453. spin_unlock(&dev->slock);
  1454. handled++;
  1455. }
  1456. /* Allow the VBI framework to process it's payload */
  1457. handled += cx23885_vbi_irq(dev, status);
  1458. return handled;
  1459. }
  1460. /* ----------------------------------------------------------- */
  1461. /* exported stuff */
  1462. static const struct v4l2_file_operations video_fops = {
  1463. .owner = THIS_MODULE,
  1464. .open = video_open,
  1465. .release = video_release,
  1466. .read = video_read,
  1467. .poll = video_poll,
  1468. .mmap = video_mmap,
  1469. .ioctl = video_ioctl2,
  1470. };
  1471. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1472. .vidioc_querycap = vidioc_querycap,
  1473. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1474. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1475. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1476. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1477. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  1478. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  1479. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  1480. .vidioc_reqbufs = vidioc_reqbufs,
  1481. .vidioc_querybuf = vidioc_querybuf,
  1482. .vidioc_qbuf = vidioc_qbuf,
  1483. .vidioc_dqbuf = vidioc_dqbuf,
  1484. .vidioc_s_std = vidioc_s_std,
  1485. .vidioc_g_std = vidioc_g_std,
  1486. .vidioc_querystd = vidioc_g_std,
  1487. .vidioc_enum_input = vidioc_enum_input,
  1488. .vidioc_g_input = vidioc_g_input,
  1489. .vidioc_s_input = vidioc_s_input,
  1490. .vidioc_log_status = vidioc_log_status,
  1491. .vidioc_queryctrl = vidioc_queryctrl,
  1492. .vidioc_g_ctrl = vidioc_g_ctrl,
  1493. .vidioc_s_ctrl = vidioc_s_ctrl,
  1494. .vidioc_streamon = vidioc_streamon,
  1495. .vidioc_streamoff = vidioc_streamoff,
  1496. .vidioc_g_tuner = vidioc_g_tuner,
  1497. .vidioc_s_tuner = vidioc_s_tuner,
  1498. .vidioc_g_frequency = vidioc_g_frequency,
  1499. .vidioc_s_frequency = vidioc_s_frequency,
  1500. .vidioc_g_chip_ident = cx23885_g_chip_ident,
  1501. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1502. .vidioc_g_register = cx23885_g_register,
  1503. .vidioc_s_register = cx23885_s_register,
  1504. #endif
  1505. .vidioc_enumaudio = vidioc_enum_audinput,
  1506. .vidioc_g_audio = vidioc_g_audinput,
  1507. .vidioc_s_audio = vidioc_s_audinput,
  1508. };
  1509. static struct video_device cx23885_vbi_template;
  1510. static struct video_device cx23885_video_template = {
  1511. .name = "cx23885-video",
  1512. .fops = &video_fops,
  1513. .ioctl_ops = &video_ioctl_ops,
  1514. .tvnorms = CX23885_NORMS,
  1515. .current_norm = V4L2_STD_NTSC_M,
  1516. };
  1517. static const struct v4l2_file_operations radio_fops = {
  1518. .owner = THIS_MODULE,
  1519. .open = video_open,
  1520. .release = video_release,
  1521. .ioctl = video_ioctl2,
  1522. };
  1523. void cx23885_video_unregister(struct cx23885_dev *dev)
  1524. {
  1525. dprintk(1, "%s()\n", __func__);
  1526. cx23885_irq_remove(dev, 0x01);
  1527. if (dev->vbi_dev) {
  1528. if (video_is_registered(dev->vbi_dev))
  1529. video_unregister_device(dev->vbi_dev);
  1530. else
  1531. video_device_release(dev->vbi_dev);
  1532. dev->vbi_dev = NULL;
  1533. btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
  1534. }
  1535. if (dev->video_dev) {
  1536. if (video_is_registered(dev->video_dev))
  1537. video_unregister_device(dev->video_dev);
  1538. else
  1539. video_device_release(dev->video_dev);
  1540. dev->video_dev = NULL;
  1541. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1542. }
  1543. if (dev->audio_dev)
  1544. cx23885_audio_unregister(dev);
  1545. }
  1546. int cx23885_video_register(struct cx23885_dev *dev)
  1547. {
  1548. int err;
  1549. dprintk(1, "%s()\n", __func__);
  1550. spin_lock_init(&dev->slock);
  1551. /* Initialize VBI template */
  1552. cx23885_vbi_template = cx23885_video_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. if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
  1603. struct xc2028_ctrl ctrl = {
  1604. .fname = "xc3028L-v36.fw",
  1605. .max_len = 64
  1606. };
  1607. struct v4l2_priv_tun_config cfg = {
  1608. .tuner = dev->tuner_type,
  1609. .priv = &ctrl
  1610. };
  1611. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1612. }
  1613. }
  1614. }
  1615. /* register Video device */
  1616. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1617. &cx23885_video_template, "video");
  1618. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1619. video_nr[dev->nr]);
  1620. if (err < 0) {
  1621. printk(KERN_INFO "%s: can't register video device\n",
  1622. dev->name);
  1623. goto fail_unreg;
  1624. }
  1625. printk(KERN_INFO "%s: registered device %s [v4l2]\n",
  1626. dev->name, video_device_node_name(dev->video_dev));
  1627. /* register VBI device */
  1628. dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
  1629. &cx23885_vbi_template, "vbi");
  1630. err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1631. vbi_nr[dev->nr]);
  1632. if (err < 0) {
  1633. printk(KERN_INFO "%s: can't register vbi device\n",
  1634. dev->name);
  1635. goto fail_unreg;
  1636. }
  1637. printk(KERN_INFO "%s: registered device %s\n",
  1638. dev->name, video_device_node_name(dev->vbi_dev));
  1639. /* Register ALSA audio device */
  1640. dev->audio_dev = cx23885_audio_register(dev);
  1641. /* initial device configuration */
  1642. mutex_lock(&dev->lock);
  1643. cx23885_set_tvnorm(dev, dev->tvnorm);
  1644. init_controls(dev);
  1645. cx23885_video_mux(dev, 0);
  1646. cx23885_audio_mux(dev, 0);
  1647. mutex_unlock(&dev->lock);
  1648. return 0;
  1649. fail_unreg:
  1650. cx23885_video_unregister(dev);
  1651. return err;
  1652. }