cx23885-video.c 39 KB

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