cx23885-video.c 39 KB

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