cx23885-video.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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/smp_lock.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/kthread.h>
  32. #include <asm/div64.h>
  33. #include "cx23885.h"
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include "cx23885-ioctl.h"
  37. #include "tuner-xc2028.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/0: " fmt, dev->name, ## arg);\
  63. } while (0)
  64. /* ------------------------------------------------------------------- */
  65. /* static data */
  66. #define FORMAT_FLAGS_PACKED 0x01
  67. static struct cx23885_fmt formats[] = {
  68. {
  69. .name = "8 bpp, gray",
  70. .fourcc = V4L2_PIX_FMT_GREY,
  71. .depth = 8,
  72. .flags = FORMAT_FLAGS_PACKED,
  73. }, {
  74. .name = "15 bpp RGB, le",
  75. .fourcc = V4L2_PIX_FMT_RGB555,
  76. .depth = 16,
  77. .flags = FORMAT_FLAGS_PACKED,
  78. }, {
  79. .name = "15 bpp RGB, be",
  80. .fourcc = V4L2_PIX_FMT_RGB555X,
  81. .depth = 16,
  82. .flags = FORMAT_FLAGS_PACKED,
  83. }, {
  84. .name = "16 bpp RGB, le",
  85. .fourcc = V4L2_PIX_FMT_RGB565,
  86. .depth = 16,
  87. .flags = FORMAT_FLAGS_PACKED,
  88. }, {
  89. .name = "16 bpp RGB, be",
  90. .fourcc = V4L2_PIX_FMT_RGB565X,
  91. .depth = 16,
  92. .flags = FORMAT_FLAGS_PACKED,
  93. }, {
  94. .name = "24 bpp RGB, le",
  95. .fourcc = V4L2_PIX_FMT_BGR24,
  96. .depth = 24,
  97. .flags = FORMAT_FLAGS_PACKED,
  98. }, {
  99. .name = "32 bpp RGB, le",
  100. .fourcc = V4L2_PIX_FMT_BGR32,
  101. .depth = 32,
  102. .flags = FORMAT_FLAGS_PACKED,
  103. }, {
  104. .name = "32 bpp RGB, be",
  105. .fourcc = V4L2_PIX_FMT_RGB32,
  106. .depth = 32,
  107. .flags = FORMAT_FLAGS_PACKED,
  108. }, {
  109. .name = "4:2:2, packed, YUYV",
  110. .fourcc = V4L2_PIX_FMT_YUYV,
  111. .depth = 16,
  112. .flags = FORMAT_FLAGS_PACKED,
  113. }, {
  114. .name = "4:2:2, packed, UYVY",
  115. .fourcc = V4L2_PIX_FMT_UYVY,
  116. .depth = 16,
  117. .flags = FORMAT_FLAGS_PACKED,
  118. },
  119. };
  120. static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
  121. {
  122. unsigned int i;
  123. for (i = 0; i < ARRAY_SIZE(formats); i++)
  124. if (formats[i].fourcc == fourcc)
  125. return formats+i;
  126. printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
  127. return NULL;
  128. }
  129. /* ------------------------------------------------------------------- */
  130. static const struct v4l2_queryctrl no_ctl = {
  131. .name = "42",
  132. .flags = V4L2_CTRL_FLAG_DISABLED,
  133. };
  134. static struct cx23885_ctrl cx23885_ctls[] = {
  135. /* --- video --- */
  136. {
  137. .v = {
  138. .id = V4L2_CID_BRIGHTNESS,
  139. .name = "Brightness",
  140. .minimum = 0x00,
  141. .maximum = 0xff,
  142. .step = 1,
  143. .default_value = 0x7f,
  144. .type = V4L2_CTRL_TYPE_INTEGER,
  145. },
  146. .off = 128,
  147. .reg = LUMA_CTRL,
  148. .mask = 0x00ff,
  149. .shift = 0,
  150. }, {
  151. .v = {
  152. .id = V4L2_CID_CONTRAST,
  153. .name = "Contrast",
  154. .minimum = 0,
  155. .maximum = 0xff,
  156. .step = 1,
  157. .default_value = 0x3f,
  158. .type = V4L2_CTRL_TYPE_INTEGER,
  159. },
  160. .off = 0,
  161. .reg = LUMA_CTRL,
  162. .mask = 0xff00,
  163. .shift = 8,
  164. }, {
  165. .v = {
  166. .id = V4L2_CID_HUE,
  167. .name = "Hue",
  168. .minimum = 0,
  169. .maximum = 0xff,
  170. .step = 1,
  171. .default_value = 0x7f,
  172. .type = V4L2_CTRL_TYPE_INTEGER,
  173. },
  174. .off = 128,
  175. .reg = CHROMA_CTRL,
  176. .mask = 0xff0000,
  177. .shift = 16,
  178. }, {
  179. /* strictly, this only describes only U saturation.
  180. * V saturation is handled specially through code.
  181. */
  182. .v = {
  183. .id = V4L2_CID_SATURATION,
  184. .name = "Saturation",
  185. .minimum = 0,
  186. .maximum = 0xff,
  187. .step = 1,
  188. .default_value = 0x7f,
  189. .type = V4L2_CTRL_TYPE_INTEGER,
  190. },
  191. .off = 0,
  192. .reg = CHROMA_CTRL,
  193. .mask = 0x00ff,
  194. .shift = 0,
  195. }, {
  196. /* --- audio --- */
  197. .v = {
  198. .id = V4L2_CID_AUDIO_MUTE,
  199. .name = "Mute",
  200. .minimum = 0,
  201. .maximum = 1,
  202. .default_value = 1,
  203. .type = V4L2_CTRL_TYPE_BOOLEAN,
  204. },
  205. .reg = PATH1_CTL1,
  206. .mask = (0x1f << 24),
  207. .shift = 24,
  208. }, {
  209. .v = {
  210. .id = V4L2_CID_AUDIO_VOLUME,
  211. .name = "Volume",
  212. .minimum = 0,
  213. .maximum = 0x3f,
  214. .step = 1,
  215. .default_value = 0x3f,
  216. .type = V4L2_CTRL_TYPE_INTEGER,
  217. },
  218. .reg = PATH1_VOL_CTL,
  219. .mask = 0xff,
  220. .shift = 0,
  221. }
  222. };
  223. static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
  224. /* Must be sorted from low to high control ID! */
  225. static const u32 cx23885_user_ctrls[] = {
  226. V4L2_CID_USER_CLASS,
  227. V4L2_CID_BRIGHTNESS,
  228. V4L2_CID_CONTRAST,
  229. V4L2_CID_SATURATION,
  230. V4L2_CID_HUE,
  231. V4L2_CID_AUDIO_VOLUME,
  232. V4L2_CID_AUDIO_MUTE,
  233. 0
  234. };
  235. static const u32 *ctrl_classes[] = {
  236. cx23885_user_ctrls,
  237. NULL
  238. };
  239. static void cx23885_video_wakeup(struct cx23885_dev *dev,
  240. struct cx23885_dmaqueue *q, u32 count)
  241. {
  242. struct cx23885_buffer *buf;
  243. int bc;
  244. for (bc = 0;; bc++) {
  245. if (list_empty(&q->active))
  246. break;
  247. buf = list_entry(q->active.next,
  248. struct cx23885_buffer, vb.queue);
  249. /* count comes from the hw and is is 16bit wide --
  250. * this trick handles wrap-arounds correctly for
  251. * up to 32767 buffers in flight... */
  252. if ((s16) (count - buf->count) < 0)
  253. break;
  254. do_gettimeofday(&buf->vb.ts);
  255. dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
  256. count, buf->count);
  257. buf->vb.state = VIDEOBUF_DONE;
  258. list_del(&buf->vb.queue);
  259. wake_up(&buf->vb.done);
  260. }
  261. if (list_empty(&q->active))
  262. del_timer(&q->timeout);
  263. else
  264. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  265. if (bc != 1)
  266. printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
  267. __func__, bc);
  268. }
  269. static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
  270. {
  271. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  272. __func__,
  273. (unsigned int)norm,
  274. v4l2_norm_to_name(norm));
  275. dev->tvnorm = norm;
  276. call_all(dev, core, s_std, norm);
  277. return 0;
  278. }
  279. static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
  280. struct pci_dev *pci,
  281. struct video_device *template,
  282. char *type)
  283. {
  284. struct video_device *vfd;
  285. dprintk(1, "%s()\n", __func__);
  286. vfd = video_device_alloc();
  287. if (NULL == vfd)
  288. return NULL;
  289. *vfd = *template;
  290. vfd->v4l2_dev = &dev->v4l2_dev;
  291. vfd->release = video_device_release;
  292. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
  293. dev->name, type, cx23885_boards[dev->board].name);
  294. video_set_drvdata(vfd, dev);
  295. return vfd;
  296. }
  297. static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
  298. {
  299. int i;
  300. if (qctrl->id < V4L2_CID_BASE ||
  301. qctrl->id >= V4L2_CID_LASTP1)
  302. return -EINVAL;
  303. for (i = 0; i < CX23885_CTLS; i++)
  304. if (cx23885_ctls[i].v.id == qctrl->id)
  305. break;
  306. if (i == CX23885_CTLS) {
  307. *qctrl = no_ctl;
  308. return 0;
  309. }
  310. *qctrl = cx23885_ctls[i].v;
  311. return 0;
  312. }
  313. /* ------------------------------------------------------------------- */
  314. /* resource management */
  315. static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
  316. unsigned int bit)
  317. {
  318. dprintk(1, "%s()\n", __func__);
  319. if (fh->resources & bit)
  320. /* have it already allocated */
  321. return 1;
  322. /* is it free? */
  323. mutex_lock(&dev->lock);
  324. if (dev->resources & bit) {
  325. /* no, someone else uses it */
  326. mutex_unlock(&dev->lock);
  327. return 0;
  328. }
  329. /* it's free, grab it */
  330. fh->resources |= bit;
  331. dev->resources |= bit;
  332. dprintk(1, "res: get %d\n", bit);
  333. mutex_unlock(&dev->lock);
  334. return 1;
  335. }
  336. static int res_check(struct cx23885_fh *fh, unsigned int bit)
  337. {
  338. return fh->resources & bit;
  339. }
  340. static int res_locked(struct cx23885_dev *dev, unsigned int bit)
  341. {
  342. return dev->resources & bit;
  343. }
  344. static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
  345. unsigned int bits)
  346. {
  347. BUG_ON((fh->resources & bits) != bits);
  348. dprintk(1, "%s()\n", __func__);
  349. mutex_lock(&dev->lock);
  350. fh->resources &= ~bits;
  351. dev->resources &= ~bits;
  352. dprintk(1, "res: put %d\n", bits);
  353. mutex_unlock(&dev->lock);
  354. }
  355. static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
  356. {
  357. dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  358. __func__,
  359. input, INPUT(input)->vmux,
  360. INPUT(input)->gpio0, INPUT(input)->gpio1,
  361. INPUT(input)->gpio2, INPUT(input)->gpio3);
  362. dev->input = input;
  363. if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
  364. dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2) {
  365. /* Select Analog TV */
  366. if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
  367. cx23885_gpio_clear(dev, GPIO_0);
  368. }
  369. /* Tell the internal A/V decoder */
  370. v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
  371. INPUT(input)->vmux, 0, 0);
  372. return 0;
  373. }
  374. /* ------------------------------------------------------------------ */
  375. static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
  376. unsigned int height, enum v4l2_field field)
  377. {
  378. dprintk(1, "%s()\n", __func__);
  379. return 0;
  380. }
  381. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  382. struct cx23885_dmaqueue *q,
  383. struct cx23885_buffer *buf)
  384. {
  385. dprintk(1, "%s()\n", __func__);
  386. /* setup fifo + format */
  387. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  388. buf->bpl, buf->risc.dma);
  389. cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
  390. /* reset counter */
  391. cx_write(VID_A_GPCNT_CTL, 3);
  392. q->count = 1;
  393. /* enable irq */
  394. cx23885_irq_add_enable(dev, 0x01);
  395. cx_set(VID_A_INT_MSK, 0x000011);
  396. /* start dma */
  397. cx_set(DEV_CNTRL2, (1<<5));
  398. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  399. return 0;
  400. }
  401. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  402. struct cx23885_dmaqueue *q)
  403. {
  404. struct cx23885_buffer *buf, *prev;
  405. struct list_head *item;
  406. dprintk(1, "%s()\n", __func__);
  407. if (!list_empty(&q->active)) {
  408. buf = list_entry(q->active.next, struct cx23885_buffer,
  409. vb.queue);
  410. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  411. buf, buf->vb.i);
  412. cx23885_start_video_dma(dev, q, buf);
  413. list_for_each(item, &q->active) {
  414. buf = list_entry(item, struct cx23885_buffer,
  415. vb.queue);
  416. buf->count = q->count++;
  417. }
  418. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  419. return 0;
  420. }
  421. prev = NULL;
  422. for (;;) {
  423. if (list_empty(&q->queued))
  424. return 0;
  425. buf = list_entry(q->queued.next, struct cx23885_buffer,
  426. vb.queue);
  427. if (NULL == prev) {
  428. list_move_tail(&buf->vb.queue, &q->active);
  429. cx23885_start_video_dma(dev, q, buf);
  430. buf->vb.state = VIDEOBUF_ACTIVE;
  431. buf->count = q->count++;
  432. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  433. dprintk(2, "[%p/%d] restart_queue - first active\n",
  434. buf, buf->vb.i);
  435. } else if (prev->vb.width == buf->vb.width &&
  436. prev->vb.height == buf->vb.height &&
  437. prev->fmt == buf->fmt) {
  438. list_move_tail(&buf->vb.queue, &q->active);
  439. buf->vb.state = VIDEOBUF_ACTIVE;
  440. buf->count = q->count++;
  441. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  442. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  443. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  444. buf, buf->vb.i);
  445. } else {
  446. return 0;
  447. }
  448. prev = buf;
  449. }
  450. }
  451. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  452. unsigned int *size)
  453. {
  454. struct cx23885_fh *fh = q->priv_data;
  455. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  456. if (0 == *count)
  457. *count = 32;
  458. if (*size * *count > vid_limit * 1024 * 1024)
  459. *count = (vid_limit * 1024 * 1024) / *size;
  460. return 0;
  461. }
  462. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  463. enum v4l2_field field)
  464. {
  465. struct cx23885_fh *fh = q->priv_data;
  466. struct cx23885_dev *dev = fh->dev;
  467. struct cx23885_buffer *buf =
  468. container_of(vb, struct cx23885_buffer, vb);
  469. int rc, init_buffer = 0;
  470. u32 line0_offset, line1_offset;
  471. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  472. BUG_ON(NULL == fh->fmt);
  473. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  474. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  475. return -EINVAL;
  476. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  477. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  478. return -EINVAL;
  479. if (buf->fmt != fh->fmt ||
  480. buf->vb.width != fh->width ||
  481. buf->vb.height != fh->height ||
  482. buf->vb.field != field) {
  483. buf->fmt = fh->fmt;
  484. buf->vb.width = fh->width;
  485. buf->vb.height = fh->height;
  486. buf->vb.field = field;
  487. init_buffer = 1;
  488. }
  489. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  490. init_buffer = 1;
  491. rc = videobuf_iolock(q, &buf->vb, NULL);
  492. if (0 != rc)
  493. goto fail;
  494. }
  495. if (init_buffer) {
  496. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  497. switch (buf->vb.field) {
  498. case V4L2_FIELD_TOP:
  499. cx23885_risc_buffer(dev->pci, &buf->risc,
  500. dma->sglist, 0, UNSET,
  501. buf->bpl, 0, buf->vb.height);
  502. break;
  503. case V4L2_FIELD_BOTTOM:
  504. cx23885_risc_buffer(dev->pci, &buf->risc,
  505. dma->sglist, UNSET, 0,
  506. buf->bpl, 0, buf->vb.height);
  507. break;
  508. case V4L2_FIELD_INTERLACED:
  509. if (dev->tvnorm & V4L2_STD_NTSC) {
  510. /* cx25840 transmits NTSC bottom field first */
  511. dprintk(1, "%s() Creating NTSC risc\n",
  512. __func__);
  513. line0_offset = buf->bpl;
  514. line1_offset = 0;
  515. } else {
  516. /* All other formats are top field first */
  517. dprintk(1, "%s() Creating PAL/SECAM risc\n",
  518. __func__);
  519. line0_offset = 0;
  520. line1_offset = buf->bpl;
  521. }
  522. cx23885_risc_buffer(dev->pci, &buf->risc,
  523. dma->sglist, line0_offset,
  524. line1_offset,
  525. buf->bpl, buf->bpl,
  526. buf->vb.height >> 1);
  527. break;
  528. case V4L2_FIELD_SEQ_TB:
  529. cx23885_risc_buffer(dev->pci, &buf->risc,
  530. dma->sglist,
  531. 0, buf->bpl * (buf->vb.height >> 1),
  532. buf->bpl, 0,
  533. buf->vb.height >> 1);
  534. break;
  535. case V4L2_FIELD_SEQ_BT:
  536. cx23885_risc_buffer(dev->pci, &buf->risc,
  537. dma->sglist,
  538. buf->bpl * (buf->vb.height >> 1), 0,
  539. buf->bpl, 0,
  540. buf->vb.height >> 1);
  541. break;
  542. default:
  543. BUG();
  544. }
  545. }
  546. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  547. buf, buf->vb.i,
  548. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  549. (unsigned long)buf->risc.dma);
  550. buf->vb.state = VIDEOBUF_PREPARED;
  551. return 0;
  552. fail:
  553. cx23885_free_buffer(q, buf);
  554. return rc;
  555. }
  556. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  557. {
  558. struct cx23885_buffer *buf = container_of(vb,
  559. struct cx23885_buffer, vb);
  560. struct cx23885_buffer *prev;
  561. struct cx23885_fh *fh = vq->priv_data;
  562. struct cx23885_dev *dev = fh->dev;
  563. struct cx23885_dmaqueue *q = &dev->vidq;
  564. /* add jump to stopper */
  565. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  566. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  567. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  568. if (!list_empty(&q->queued)) {
  569. list_add_tail(&buf->vb.queue, &q->queued);
  570. buf->vb.state = VIDEOBUF_QUEUED;
  571. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  572. buf, buf->vb.i);
  573. } else if (list_empty(&q->active)) {
  574. list_add_tail(&buf->vb.queue, &q->active);
  575. cx23885_start_video_dma(dev, q, buf);
  576. buf->vb.state = VIDEOBUF_ACTIVE;
  577. buf->count = q->count++;
  578. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  579. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  580. buf, buf->vb.i);
  581. } else {
  582. prev = list_entry(q->active.prev, struct cx23885_buffer,
  583. vb.queue);
  584. if (prev->vb.width == buf->vb.width &&
  585. prev->vb.height == buf->vb.height &&
  586. prev->fmt == buf->fmt) {
  587. list_add_tail(&buf->vb.queue, &q->active);
  588. buf->vb.state = VIDEOBUF_ACTIVE;
  589. buf->count = q->count++;
  590. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  591. /* 64 bit bits 63-32 */
  592. prev->risc.jmp[2] = cpu_to_le32(0);
  593. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  594. buf, buf->vb.i);
  595. } else {
  596. list_add_tail(&buf->vb.queue, &q->queued);
  597. buf->vb.state = VIDEOBUF_QUEUED;
  598. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  599. buf, buf->vb.i);
  600. }
  601. }
  602. }
  603. static void buffer_release(struct videobuf_queue *q,
  604. struct videobuf_buffer *vb)
  605. {
  606. struct cx23885_buffer *buf = container_of(vb,
  607. struct cx23885_buffer, vb);
  608. cx23885_free_buffer(q, buf);
  609. }
  610. static struct videobuf_queue_ops cx23885_video_qops = {
  611. .buf_setup = buffer_setup,
  612. .buf_prepare = buffer_prepare,
  613. .buf_queue = buffer_queue,
  614. .buf_release = buffer_release,
  615. };
  616. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  617. {
  618. switch (fh->type) {
  619. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  620. return &fh->vidq;
  621. case V4L2_BUF_TYPE_VBI_CAPTURE:
  622. return &fh->vbiq;
  623. default:
  624. BUG();
  625. return NULL;
  626. }
  627. }
  628. static int get_resource(struct cx23885_fh *fh)
  629. {
  630. switch (fh->type) {
  631. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  632. return RESOURCE_VIDEO;
  633. case V4L2_BUF_TYPE_VBI_CAPTURE:
  634. return RESOURCE_VBI;
  635. default:
  636. BUG();
  637. return 0;
  638. }
  639. }
  640. static int video_open(struct file *file)
  641. {
  642. struct video_device *vdev = video_devdata(file);
  643. struct cx23885_dev *dev = video_drvdata(file);
  644. struct cx23885_fh *fh;
  645. enum v4l2_buf_type type = 0;
  646. int radio = 0;
  647. switch (vdev->vfl_type) {
  648. case VFL_TYPE_GRABBER:
  649. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  650. break;
  651. case VFL_TYPE_VBI:
  652. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  653. break;
  654. case VFL_TYPE_RADIO:
  655. radio = 1;
  656. break;
  657. }
  658. dprintk(1, "open dev=%s radio=%d type=%s\n",
  659. video_device_node_name(vdev), radio, v4l2_type_names[type]);
  660. /* allocate + initialize per filehandle data */
  661. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  662. if (NULL == fh)
  663. return -ENOMEM;
  664. lock_kernel();
  665. file->private_data = fh;
  666. fh->dev = dev;
  667. fh->radio = radio;
  668. fh->type = type;
  669. fh->width = 320;
  670. fh->height = 240;
  671. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  672. videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
  673. &dev->pci->dev, &dev->slock,
  674. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  675. V4L2_FIELD_INTERLACED,
  676. sizeof(struct cx23885_buffer),
  677. fh);
  678. dprintk(1, "post videobuf_queue_init()\n");
  679. unlock_kernel();
  680. return 0;
  681. }
  682. static ssize_t video_read(struct file *file, char __user *data,
  683. size_t count, loff_t *ppos)
  684. {
  685. struct cx23885_fh *fh = file->private_data;
  686. switch (fh->type) {
  687. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  688. if (res_locked(fh->dev, RESOURCE_VIDEO))
  689. return -EBUSY;
  690. return videobuf_read_one(&fh->vidq, data, count, ppos,
  691. file->f_flags & O_NONBLOCK);
  692. case V4L2_BUF_TYPE_VBI_CAPTURE:
  693. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  694. return -EBUSY;
  695. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  696. file->f_flags & O_NONBLOCK);
  697. default:
  698. BUG();
  699. return 0;
  700. }
  701. }
  702. static unsigned int video_poll(struct file *file,
  703. struct poll_table_struct *wait)
  704. {
  705. struct cx23885_fh *fh = file->private_data;
  706. struct cx23885_buffer *buf;
  707. unsigned int rc = POLLERR;
  708. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  709. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  710. return POLLERR;
  711. return videobuf_poll_stream(file, &fh->vbiq, wait);
  712. }
  713. mutex_lock(&fh->vidq.vb_lock);
  714. if (res_check(fh, RESOURCE_VIDEO)) {
  715. /* streaming capture */
  716. if (list_empty(&fh->vidq.stream))
  717. goto done;
  718. buf = list_entry(fh->vidq.stream.next,
  719. struct cx23885_buffer, vb.stream);
  720. } else {
  721. /* read() capture */
  722. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  723. if (NULL == buf)
  724. goto done;
  725. }
  726. poll_wait(file, &buf->vb.done, wait);
  727. if (buf->vb.state == VIDEOBUF_DONE ||
  728. buf->vb.state == VIDEOBUF_ERROR)
  729. rc = POLLIN|POLLRDNORM;
  730. else
  731. rc = 0;
  732. done:
  733. mutex_unlock(&fh->vidq.vb_lock);
  734. return rc;
  735. }
  736. static int video_release(struct file *file)
  737. {
  738. struct cx23885_fh *fh = file->private_data;
  739. struct cx23885_dev *dev = fh->dev;
  740. /* turn off overlay */
  741. if (res_check(fh, RESOURCE_OVERLAY)) {
  742. /* FIXME */
  743. res_free(dev, fh, RESOURCE_OVERLAY);
  744. }
  745. /* stop video capture */
  746. if (res_check(fh, RESOURCE_VIDEO)) {
  747. videobuf_queue_cancel(&fh->vidq);
  748. res_free(dev, fh, RESOURCE_VIDEO);
  749. }
  750. if (fh->vidq.read_buf) {
  751. buffer_release(&fh->vidq, fh->vidq.read_buf);
  752. kfree(fh->vidq.read_buf);
  753. }
  754. /* stop vbi capture */
  755. if (res_check(fh, RESOURCE_VBI)) {
  756. if (fh->vbiq.streaming)
  757. videobuf_streamoff(&fh->vbiq);
  758. if (fh->vbiq.reading)
  759. videobuf_read_stop(&fh->vbiq);
  760. res_free(dev, fh, RESOURCE_VBI);
  761. }
  762. videobuf_mmap_free(&fh->vidq);
  763. file->private_data = NULL;
  764. kfree(fh);
  765. /* We are not putting the tuner to sleep here on exit, because
  766. * we want to use the mpeg encoder in another session to capture
  767. * tuner video. Closing this will result in no video to the encoder.
  768. */
  769. return 0;
  770. }
  771. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  772. {
  773. struct cx23885_fh *fh = file->private_data;
  774. return videobuf_mmap_mapper(get_queue(fh), vma);
  775. }
  776. /* ------------------------------------------------------------------ */
  777. /* VIDEO CTRL IOCTLS */
  778. static int cx23885_get_control(struct cx23885_dev *dev,
  779. struct v4l2_control *ctl)
  780. {
  781. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
  782. call_all(dev, core, g_ctrl, ctl);
  783. return 0;
  784. }
  785. static int cx23885_set_control(struct cx23885_dev *dev,
  786. struct v4l2_control *ctl)
  787. {
  788. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
  789. " (disabled - no action)\n", __func__);
  790. return 0;
  791. }
  792. static void init_controls(struct cx23885_dev *dev)
  793. {
  794. struct v4l2_control ctrl;
  795. int i;
  796. for (i = 0; i < CX23885_CTLS; i++) {
  797. ctrl.id = cx23885_ctls[i].v.id;
  798. ctrl.value = cx23885_ctls[i].v.default_value;
  799. cx23885_set_control(dev, &ctrl);
  800. }
  801. }
  802. /* ------------------------------------------------------------------ */
  803. /* VIDEO IOCTLS */
  804. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  805. struct v4l2_format *f)
  806. {
  807. struct cx23885_fh *fh = priv;
  808. f->fmt.pix.width = fh->width;
  809. f->fmt.pix.height = fh->height;
  810. f->fmt.pix.field = fh->vidq.field;
  811. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  812. f->fmt.pix.bytesperline =
  813. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  814. f->fmt.pix.sizeimage =
  815. f->fmt.pix.height * f->fmt.pix.bytesperline;
  816. return 0;
  817. }
  818. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  819. struct v4l2_format *f)
  820. {
  821. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  822. struct cx23885_fmt *fmt;
  823. enum v4l2_field field;
  824. unsigned int maxw, maxh;
  825. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  826. if (NULL == fmt)
  827. return -EINVAL;
  828. field = f->fmt.pix.field;
  829. maxw = norm_maxw(dev->tvnorm);
  830. maxh = norm_maxh(dev->tvnorm);
  831. if (V4L2_FIELD_ANY == field) {
  832. field = (f->fmt.pix.height > maxh/2)
  833. ? V4L2_FIELD_INTERLACED
  834. : V4L2_FIELD_BOTTOM;
  835. }
  836. switch (field) {
  837. case V4L2_FIELD_TOP:
  838. case V4L2_FIELD_BOTTOM:
  839. maxh = maxh / 2;
  840. break;
  841. case V4L2_FIELD_INTERLACED:
  842. break;
  843. default:
  844. return -EINVAL;
  845. }
  846. f->fmt.pix.field = field;
  847. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  848. &f->fmt.pix.height, 32, maxh, 0, 0);
  849. f->fmt.pix.bytesperline =
  850. (f->fmt.pix.width * fmt->depth) >> 3;
  851. f->fmt.pix.sizeimage =
  852. f->fmt.pix.height * f->fmt.pix.bytesperline;
  853. return 0;
  854. }
  855. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  856. struct v4l2_format *f)
  857. {
  858. struct cx23885_fh *fh = priv;
  859. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  860. struct v4l2_mbus_framefmt mbus_fmt;
  861. int err;
  862. dprintk(2, "%s()\n", __func__);
  863. err = vidioc_try_fmt_vid_cap(file, priv, f);
  864. if (0 != err)
  865. return err;
  866. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  867. fh->width = f->fmt.pix.width;
  868. fh->height = f->fmt.pix.height;
  869. fh->vidq.field = f->fmt.pix.field;
  870. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  871. fh->width, fh->height, fh->vidq.field);
  872. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  873. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  874. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  875. return 0;
  876. }
  877. static int vidioc_querycap(struct file *file, void *priv,
  878. struct v4l2_capability *cap)
  879. {
  880. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  881. strcpy(cap->driver, "cx23885");
  882. strlcpy(cap->card, cx23885_boards[dev->board].name,
  883. sizeof(cap->card));
  884. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  885. cap->version = CX23885_VERSION_CODE;
  886. cap->capabilities =
  887. V4L2_CAP_VIDEO_CAPTURE |
  888. V4L2_CAP_READWRITE |
  889. V4L2_CAP_STREAMING |
  890. V4L2_CAP_VBI_CAPTURE;
  891. if (UNSET != dev->tuner_type)
  892. cap->capabilities |= V4L2_CAP_TUNER;
  893. return 0;
  894. }
  895. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  896. struct v4l2_fmtdesc *f)
  897. {
  898. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  899. return -EINVAL;
  900. strlcpy(f->description, formats[f->index].name,
  901. sizeof(f->description));
  902. f->pixelformat = formats[f->index].fourcc;
  903. return 0;
  904. }
  905. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  906. static int vidiocgmbuf(struct file *file, void *priv,
  907. struct video_mbuf *mbuf)
  908. {
  909. struct cx23885_fh *fh = priv;
  910. struct videobuf_queue *q;
  911. struct v4l2_requestbuffers req;
  912. unsigned int i;
  913. int err;
  914. q = get_queue(fh);
  915. memset(&req, 0, sizeof(req));
  916. req.type = q->type;
  917. req.count = 8;
  918. req.memory = V4L2_MEMORY_MMAP;
  919. err = videobuf_reqbufs(q, &req);
  920. if (err < 0)
  921. return err;
  922. mbuf->frames = req.count;
  923. mbuf->size = 0;
  924. for (i = 0; i < mbuf->frames; i++) {
  925. mbuf->offsets[i] = q->bufs[i]->boff;
  926. mbuf->size += q->bufs[i]->bsize;
  927. }
  928. return 0;
  929. }
  930. #endif
  931. static int vidioc_reqbufs(struct file *file, void *priv,
  932. struct v4l2_requestbuffers *p)
  933. {
  934. struct cx23885_fh *fh = priv;
  935. return videobuf_reqbufs(get_queue(fh), p);
  936. }
  937. static int vidioc_querybuf(struct file *file, void *priv,
  938. struct v4l2_buffer *p)
  939. {
  940. struct cx23885_fh *fh = priv;
  941. return videobuf_querybuf(get_queue(fh), p);
  942. }
  943. static int vidioc_qbuf(struct file *file, void *priv,
  944. struct v4l2_buffer *p)
  945. {
  946. struct cx23885_fh *fh = priv;
  947. return videobuf_qbuf(get_queue(fh), p);
  948. }
  949. static int vidioc_dqbuf(struct file *file, void *priv,
  950. struct v4l2_buffer *p)
  951. {
  952. struct cx23885_fh *fh = priv;
  953. return videobuf_dqbuf(get_queue(fh), p,
  954. file->f_flags & O_NONBLOCK);
  955. }
  956. static int vidioc_streamon(struct file *file, void *priv,
  957. enum v4l2_buf_type i)
  958. {
  959. struct cx23885_fh *fh = priv;
  960. struct cx23885_dev *dev = fh->dev;
  961. dprintk(1, "%s()\n", __func__);
  962. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  963. return -EINVAL;
  964. if (unlikely(i != fh->type))
  965. return -EINVAL;
  966. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  967. return -EBUSY;
  968. return videobuf_streamon(get_queue(fh));
  969. }
  970. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  971. {
  972. struct cx23885_fh *fh = priv;
  973. struct cx23885_dev *dev = fh->dev;
  974. int err, res;
  975. dprintk(1, "%s()\n", __func__);
  976. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  977. return -EINVAL;
  978. if (i != fh->type)
  979. return -EINVAL;
  980. res = get_resource(fh);
  981. err = videobuf_streamoff(get_queue(fh));
  982. if (err < 0)
  983. return err;
  984. res_free(dev, fh, res);
  985. return 0;
  986. }
  987. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  988. {
  989. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  990. dprintk(1, "%s()\n", __func__);
  991. mutex_lock(&dev->lock);
  992. cx23885_set_tvnorm(dev, *tvnorms);
  993. mutex_unlock(&dev->lock);
  994. return 0;
  995. }
  996. static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  997. {
  998. static const char *iname[] = {
  999. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  1000. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  1001. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  1002. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1003. [CX23885_VMUX_SVIDEO] = "S-Video",
  1004. [CX23885_VMUX_COMPONENT] = "Component",
  1005. [CX23885_VMUX_TELEVISION] = "Television",
  1006. [CX23885_VMUX_CABLE] = "Cable TV",
  1007. [CX23885_VMUX_DVB] = "DVB",
  1008. [CX23885_VMUX_DEBUG] = "for debug only",
  1009. };
  1010. unsigned int n;
  1011. dprintk(1, "%s()\n", __func__);
  1012. n = i->index;
  1013. if (n >= 4)
  1014. return -EINVAL;
  1015. if (0 == INPUT(n)->type)
  1016. return -EINVAL;
  1017. memset(i, 0, sizeof(*i));
  1018. i->index = n;
  1019. i->type = V4L2_INPUT_TYPE_CAMERA;
  1020. strcpy(i->name, iname[INPUT(n)->type]);
  1021. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1022. (CX23885_VMUX_CABLE == INPUT(n)->type))
  1023. i->type = V4L2_INPUT_TYPE_TUNER;
  1024. i->std = CX23885_NORMS;
  1025. return 0;
  1026. }
  1027. static int vidioc_enum_input(struct file *file, void *priv,
  1028. struct v4l2_input *i)
  1029. {
  1030. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1031. dprintk(1, "%s()\n", __func__);
  1032. return cx23885_enum_input(dev, i);
  1033. }
  1034. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1035. {
  1036. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1037. *i = dev->input;
  1038. dprintk(1, "%s() returns %d\n", __func__, *i);
  1039. return 0;
  1040. }
  1041. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1042. {
  1043. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1044. dprintk(1, "%s(%d)\n", __func__, i);
  1045. if (i >= 4) {
  1046. dprintk(1, "%s() -EINVAL\n", __func__);
  1047. return -EINVAL;
  1048. }
  1049. mutex_lock(&dev->lock);
  1050. cx23885_video_mux(dev, i);
  1051. mutex_unlock(&dev->lock);
  1052. return 0;
  1053. }
  1054. static int vidioc_log_status(struct file *file, void *priv)
  1055. {
  1056. struct cx23885_fh *fh = priv;
  1057. struct cx23885_dev *dev = fh->dev;
  1058. printk(KERN_INFO
  1059. "%s/0: ============ START LOG STATUS ============\n",
  1060. dev->name);
  1061. call_all(dev, core, log_status);
  1062. printk(KERN_INFO
  1063. "%s/0: ============= END LOG STATUS =============\n",
  1064. dev->name);
  1065. return 0;
  1066. }
  1067. static int vidioc_queryctrl(struct file *file, void *priv,
  1068. struct v4l2_queryctrl *qctrl)
  1069. {
  1070. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1071. if (unlikely(qctrl->id == 0))
  1072. return -EINVAL;
  1073. return cx23885_ctrl_query(qctrl);
  1074. }
  1075. static int vidioc_g_ctrl(struct file *file, void *priv,
  1076. struct v4l2_control *ctl)
  1077. {
  1078. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1079. return cx23885_get_control(dev, ctl);
  1080. }
  1081. static int vidioc_s_ctrl(struct file *file, void *priv,
  1082. struct v4l2_control *ctl)
  1083. {
  1084. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1085. return cx23885_set_control(dev, ctl);
  1086. }
  1087. static int vidioc_g_tuner(struct file *file, void *priv,
  1088. struct v4l2_tuner *t)
  1089. {
  1090. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1091. if (unlikely(UNSET == dev->tuner_type))
  1092. return -EINVAL;
  1093. if (0 != t->index)
  1094. return -EINVAL;
  1095. strcpy(t->name, "Television");
  1096. t->type = V4L2_TUNER_ANALOG_TV;
  1097. t->capability = V4L2_TUNER_CAP_NORM;
  1098. t->rangehigh = 0xffffffffUL;
  1099. t->signal = 0xffff ; /* LOCKED */
  1100. return 0;
  1101. }
  1102. static int vidioc_s_tuner(struct file *file, void *priv,
  1103. struct v4l2_tuner *t)
  1104. {
  1105. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1106. if (UNSET == dev->tuner_type)
  1107. return -EINVAL;
  1108. if (0 != t->index)
  1109. return -EINVAL;
  1110. return 0;
  1111. }
  1112. static int vidioc_g_frequency(struct file *file, void *priv,
  1113. struct v4l2_frequency *f)
  1114. {
  1115. struct cx23885_fh *fh = priv;
  1116. struct cx23885_dev *dev = fh->dev;
  1117. if (unlikely(UNSET == dev->tuner_type))
  1118. return -EINVAL;
  1119. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1120. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1121. f->frequency = dev->freq;
  1122. call_all(dev, tuner, g_frequency, f);
  1123. return 0;
  1124. }
  1125. static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1126. {
  1127. if (unlikely(UNSET == dev->tuner_type))
  1128. return -EINVAL;
  1129. if (unlikely(f->tuner != 0))
  1130. return -EINVAL;
  1131. mutex_lock(&dev->lock);
  1132. dev->freq = f->frequency;
  1133. call_all(dev, tuner, s_frequency, f);
  1134. /* When changing channels it is required to reset TVAUDIO */
  1135. msleep(10);
  1136. mutex_unlock(&dev->lock);
  1137. return 0;
  1138. }
  1139. static int vidioc_s_frequency(struct file *file, void *priv,
  1140. struct v4l2_frequency *f)
  1141. {
  1142. struct cx23885_fh *fh = priv;
  1143. struct cx23885_dev *dev = fh->dev;
  1144. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1145. return -EINVAL;
  1146. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1147. return -EINVAL;
  1148. return
  1149. cx23885_set_freq(dev, f);
  1150. }
  1151. /* ----------------------------------------------------------- */
  1152. static void cx23885_vid_timeout(unsigned long data)
  1153. {
  1154. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1155. struct cx23885_dmaqueue *q = &dev->vidq;
  1156. struct cx23885_buffer *buf;
  1157. unsigned long flags;
  1158. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1159. cx_clear(VID_A_DMA_CTL, 0x11);
  1160. spin_lock_irqsave(&dev->slock, flags);
  1161. while (!list_empty(&q->active)) {
  1162. buf = list_entry(q->active.next,
  1163. struct cx23885_buffer, vb.queue);
  1164. list_del(&buf->vb.queue);
  1165. buf->vb.state = VIDEOBUF_ERROR;
  1166. wake_up(&buf->vb.done);
  1167. printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
  1168. dev->name, buf, buf->vb.i,
  1169. (unsigned long)buf->risc.dma);
  1170. }
  1171. cx23885_restart_video_queue(dev, q);
  1172. spin_unlock_irqrestore(&dev->slock, flags);
  1173. }
  1174. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1175. {
  1176. u32 mask, count;
  1177. int handled = 0;
  1178. mask = cx_read(VID_A_INT_MSK);
  1179. if (0 == (status & mask))
  1180. return handled;
  1181. cx_write(VID_A_INT_STAT, status);
  1182. dprintk(2, "%s() status = 0x%08x\n", __func__, status);
  1183. /* risc op code error */
  1184. if (status & (1 << 16)) {
  1185. printk(KERN_WARNING "%s/0: video risc op code error\n",
  1186. dev->name);
  1187. cx_clear(VID_A_DMA_CTL, 0x11);
  1188. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1189. }
  1190. /* risc1 y */
  1191. if (status & 0x01) {
  1192. spin_lock(&dev->slock);
  1193. count = cx_read(VID_A_GPCNT);
  1194. cx23885_video_wakeup(dev, &dev->vidq, count);
  1195. spin_unlock(&dev->slock);
  1196. handled++;
  1197. }
  1198. /* risc2 y */
  1199. if (status & 0x10) {
  1200. dprintk(2, "stopper video\n");
  1201. spin_lock(&dev->slock);
  1202. cx23885_restart_video_queue(dev, &dev->vidq);
  1203. spin_unlock(&dev->slock);
  1204. handled++;
  1205. }
  1206. return handled;
  1207. }
  1208. /* ----------------------------------------------------------- */
  1209. /* exported stuff */
  1210. static const struct v4l2_file_operations video_fops = {
  1211. .owner = THIS_MODULE,
  1212. .open = video_open,
  1213. .release = video_release,
  1214. .read = video_read,
  1215. .poll = video_poll,
  1216. .mmap = video_mmap,
  1217. .ioctl = video_ioctl2,
  1218. };
  1219. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1220. .vidioc_querycap = vidioc_querycap,
  1221. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1222. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1223. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1224. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1225. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  1226. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  1227. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  1228. .vidioc_reqbufs = vidioc_reqbufs,
  1229. .vidioc_querybuf = vidioc_querybuf,
  1230. .vidioc_qbuf = vidioc_qbuf,
  1231. .vidioc_dqbuf = vidioc_dqbuf,
  1232. .vidioc_s_std = vidioc_s_std,
  1233. .vidioc_enum_input = vidioc_enum_input,
  1234. .vidioc_g_input = vidioc_g_input,
  1235. .vidioc_s_input = vidioc_s_input,
  1236. .vidioc_log_status = vidioc_log_status,
  1237. .vidioc_queryctrl = vidioc_queryctrl,
  1238. .vidioc_g_ctrl = vidioc_g_ctrl,
  1239. .vidioc_s_ctrl = vidioc_s_ctrl,
  1240. .vidioc_streamon = vidioc_streamon,
  1241. .vidioc_streamoff = vidioc_streamoff,
  1242. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1243. .vidiocgmbuf = vidiocgmbuf,
  1244. #endif
  1245. .vidioc_g_tuner = vidioc_g_tuner,
  1246. .vidioc_s_tuner = vidioc_s_tuner,
  1247. .vidioc_g_frequency = vidioc_g_frequency,
  1248. .vidioc_s_frequency = vidioc_s_frequency,
  1249. .vidioc_g_chip_ident = cx23885_g_chip_ident,
  1250. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1251. .vidioc_g_register = cx23885_g_register,
  1252. .vidioc_s_register = cx23885_s_register,
  1253. #endif
  1254. };
  1255. static struct video_device cx23885_vbi_template;
  1256. static struct video_device cx23885_video_template = {
  1257. .name = "cx23885-video",
  1258. .fops = &video_fops,
  1259. .ioctl_ops = &video_ioctl_ops,
  1260. .tvnorms = CX23885_NORMS,
  1261. .current_norm = V4L2_STD_NTSC_M,
  1262. };
  1263. static const struct v4l2_file_operations radio_fops = {
  1264. .owner = THIS_MODULE,
  1265. .open = video_open,
  1266. .release = video_release,
  1267. .ioctl = video_ioctl2,
  1268. };
  1269. void cx23885_video_unregister(struct cx23885_dev *dev)
  1270. {
  1271. dprintk(1, "%s()\n", __func__);
  1272. cx23885_irq_remove(dev, 0x01);
  1273. if (dev->video_dev) {
  1274. if (video_is_registered(dev->video_dev))
  1275. video_unregister_device(dev->video_dev);
  1276. else
  1277. video_device_release(dev->video_dev);
  1278. dev->video_dev = NULL;
  1279. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1280. }
  1281. }
  1282. int cx23885_video_register(struct cx23885_dev *dev)
  1283. {
  1284. int err;
  1285. dprintk(1, "%s()\n", __func__);
  1286. spin_lock_init(&dev->slock);
  1287. /* Initialize VBI template */
  1288. memcpy(&cx23885_vbi_template, &cx23885_video_template,
  1289. sizeof(cx23885_vbi_template));
  1290. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1291. dev->tvnorm = cx23885_video_template.current_norm;
  1292. /* init video dma queues */
  1293. INIT_LIST_HEAD(&dev->vidq.active);
  1294. INIT_LIST_HEAD(&dev->vidq.queued);
  1295. dev->vidq.timeout.function = cx23885_vid_timeout;
  1296. dev->vidq.timeout.data = (unsigned long)dev;
  1297. init_timer(&dev->vidq.timeout);
  1298. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1299. VID_A_DMA_CTL, 0x11, 0x00);
  1300. /* Don't enable VBI yet */
  1301. cx23885_irq_add_enable(dev, 0x01);
  1302. if (TUNER_ABSENT != dev->tuner_type) {
  1303. struct v4l2_subdev *sd = NULL;
  1304. if (dev->tuner_addr)
  1305. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1306. &dev->i2c_bus[1].i2c_adap,
  1307. "tuner", "tuner", dev->tuner_addr, NULL);
  1308. else
  1309. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1310. &dev->i2c_bus[1].i2c_adap,
  1311. "tuner", "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
  1312. if (sd) {
  1313. struct tuner_setup tun_setup;
  1314. memset(&tun_setup, 0, sizeof(tun_setup));
  1315. tun_setup.mode_mask = T_ANALOG_TV;
  1316. tun_setup.type = dev->tuner_type;
  1317. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  1318. tun_setup.tuner_callback = cx23885_tuner_callback;
  1319. v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
  1320. if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
  1321. struct xc2028_ctrl ctrl = {
  1322. .fname = XC2028_DEFAULT_FIRMWARE,
  1323. .max_len = 64
  1324. };
  1325. struct v4l2_priv_tun_config cfg = {
  1326. .tuner = dev->tuner_type,
  1327. .priv = &ctrl
  1328. };
  1329. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1330. }
  1331. }
  1332. }
  1333. /* register v4l devices */
  1334. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1335. &cx23885_video_template, "video");
  1336. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1337. video_nr[dev->nr]);
  1338. if (err < 0) {
  1339. printk(KERN_INFO "%s: can't register video device\n",
  1340. dev->name);
  1341. goto fail_unreg;
  1342. }
  1343. printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
  1344. dev->name, video_device_node_name(dev->video_dev));
  1345. /* initial device configuration */
  1346. mutex_lock(&dev->lock);
  1347. cx23885_set_tvnorm(dev, dev->tvnorm);
  1348. init_controls(dev);
  1349. cx23885_video_mux(dev, 0);
  1350. mutex_unlock(&dev->lock);
  1351. return 0;
  1352. fail_unreg:
  1353. cx23885_video_unregister(dev);
  1354. return err;
  1355. }