cx23885-video.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  903. static int vidiocgmbuf(struct file *file, void *priv,
  904. struct video_mbuf *mbuf)
  905. {
  906. struct cx23885_fh *fh = priv;
  907. struct videobuf_queue *q;
  908. struct v4l2_requestbuffers req;
  909. unsigned int i;
  910. int err;
  911. q = get_queue(fh);
  912. memset(&req, 0, sizeof(req));
  913. req.type = q->type;
  914. req.count = 8;
  915. req.memory = V4L2_MEMORY_MMAP;
  916. err = videobuf_reqbufs(q, &req);
  917. if (err < 0)
  918. return err;
  919. mbuf->frames = req.count;
  920. mbuf->size = 0;
  921. for (i = 0; i < mbuf->frames; i++) {
  922. mbuf->offsets[i] = q->bufs[i]->boff;
  923. mbuf->size += q->bufs[i]->bsize;
  924. }
  925. return 0;
  926. }
  927. #endif
  928. static int vidioc_reqbufs(struct file *file, void *priv,
  929. struct v4l2_requestbuffers *p)
  930. {
  931. struct cx23885_fh *fh = priv;
  932. return videobuf_reqbufs(get_queue(fh), p);
  933. }
  934. static int vidioc_querybuf(struct file *file, void *priv,
  935. struct v4l2_buffer *p)
  936. {
  937. struct cx23885_fh *fh = priv;
  938. return videobuf_querybuf(get_queue(fh), p);
  939. }
  940. static int vidioc_qbuf(struct file *file, void *priv,
  941. struct v4l2_buffer *p)
  942. {
  943. struct cx23885_fh *fh = priv;
  944. return videobuf_qbuf(get_queue(fh), p);
  945. }
  946. static int vidioc_dqbuf(struct file *file, void *priv,
  947. struct v4l2_buffer *p)
  948. {
  949. struct cx23885_fh *fh = priv;
  950. return videobuf_dqbuf(get_queue(fh), p,
  951. file->f_flags & O_NONBLOCK);
  952. }
  953. static int vidioc_streamon(struct file *file, void *priv,
  954. enum v4l2_buf_type i)
  955. {
  956. struct cx23885_fh *fh = priv;
  957. struct cx23885_dev *dev = fh->dev;
  958. dprintk(1, "%s()\n", __func__);
  959. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  960. return -EINVAL;
  961. if (unlikely(i != fh->type))
  962. return -EINVAL;
  963. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  964. return -EBUSY;
  965. return videobuf_streamon(get_queue(fh));
  966. }
  967. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  968. {
  969. struct cx23885_fh *fh = priv;
  970. struct cx23885_dev *dev = fh->dev;
  971. int err, res;
  972. dprintk(1, "%s()\n", __func__);
  973. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  974. return -EINVAL;
  975. if (i != fh->type)
  976. return -EINVAL;
  977. res = get_resource(fh);
  978. err = videobuf_streamoff(get_queue(fh));
  979. if (err < 0)
  980. return err;
  981. res_free(dev, fh, res);
  982. return 0;
  983. }
  984. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  985. {
  986. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  987. dprintk(1, "%s()\n", __func__);
  988. mutex_lock(&dev->lock);
  989. cx23885_set_tvnorm(dev, *tvnorms);
  990. mutex_unlock(&dev->lock);
  991. return 0;
  992. }
  993. static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  994. {
  995. static const char *iname[] = {
  996. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  997. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  998. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  999. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1000. [CX23885_VMUX_SVIDEO] = "S-Video",
  1001. [CX23885_VMUX_COMPONENT] = "Component",
  1002. [CX23885_VMUX_TELEVISION] = "Television",
  1003. [CX23885_VMUX_CABLE] = "Cable TV",
  1004. [CX23885_VMUX_DVB] = "DVB",
  1005. [CX23885_VMUX_DEBUG] = "for debug only",
  1006. };
  1007. unsigned int n;
  1008. dprintk(1, "%s()\n", __func__);
  1009. n = i->index;
  1010. if (n >= 4)
  1011. return -EINVAL;
  1012. if (0 == INPUT(n)->type)
  1013. return -EINVAL;
  1014. memset(i, 0, sizeof(*i));
  1015. i->index = n;
  1016. i->type = V4L2_INPUT_TYPE_CAMERA;
  1017. strcpy(i->name, iname[INPUT(n)->type]);
  1018. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1019. (CX23885_VMUX_CABLE == INPUT(n)->type)) {
  1020. i->type = V4L2_INPUT_TYPE_TUNER;
  1021. i->std = CX23885_NORMS;
  1022. }
  1023. return 0;
  1024. }
  1025. static int vidioc_enum_input(struct file *file, void *priv,
  1026. struct v4l2_input *i)
  1027. {
  1028. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1029. dprintk(1, "%s()\n", __func__);
  1030. return cx23885_enum_input(dev, i);
  1031. }
  1032. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1033. {
  1034. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1035. *i = dev->input;
  1036. dprintk(1, "%s() returns %d\n", __func__, *i);
  1037. return 0;
  1038. }
  1039. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1040. {
  1041. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1042. dprintk(1, "%s(%d)\n", __func__, i);
  1043. if (i >= 4) {
  1044. dprintk(1, "%s() -EINVAL\n", __func__);
  1045. return -EINVAL;
  1046. }
  1047. mutex_lock(&dev->lock);
  1048. cx23885_video_mux(dev, i);
  1049. mutex_unlock(&dev->lock);
  1050. return 0;
  1051. }
  1052. static int vidioc_log_status(struct file *file, void *priv)
  1053. {
  1054. struct cx23885_fh *fh = priv;
  1055. struct cx23885_dev *dev = fh->dev;
  1056. printk(KERN_INFO
  1057. "%s/0: ============ START LOG STATUS ============\n",
  1058. dev->name);
  1059. call_all(dev, core, log_status);
  1060. printk(KERN_INFO
  1061. "%s/0: ============= END LOG STATUS =============\n",
  1062. dev->name);
  1063. return 0;
  1064. }
  1065. static int vidioc_queryctrl(struct file *file, void *priv,
  1066. struct v4l2_queryctrl *qctrl)
  1067. {
  1068. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1069. if (unlikely(qctrl->id == 0))
  1070. return -EINVAL;
  1071. return cx23885_ctrl_query(qctrl);
  1072. }
  1073. static int vidioc_g_ctrl(struct file *file, void *priv,
  1074. struct v4l2_control *ctl)
  1075. {
  1076. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1077. return cx23885_get_control(dev, ctl);
  1078. }
  1079. static int vidioc_s_ctrl(struct file *file, void *priv,
  1080. struct v4l2_control *ctl)
  1081. {
  1082. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1083. return cx23885_set_control(dev, ctl);
  1084. }
  1085. static int vidioc_g_tuner(struct file *file, void *priv,
  1086. struct v4l2_tuner *t)
  1087. {
  1088. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1089. if (unlikely(UNSET == dev->tuner_type))
  1090. return -EINVAL;
  1091. if (0 != t->index)
  1092. return -EINVAL;
  1093. strcpy(t->name, "Television");
  1094. t->type = V4L2_TUNER_ANALOG_TV;
  1095. t->capability = V4L2_TUNER_CAP_NORM;
  1096. t->rangehigh = 0xffffffffUL;
  1097. t->signal = 0xffff ; /* LOCKED */
  1098. return 0;
  1099. }
  1100. static int vidioc_s_tuner(struct file *file, void *priv,
  1101. struct v4l2_tuner *t)
  1102. {
  1103. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1104. if (UNSET == dev->tuner_type)
  1105. return -EINVAL;
  1106. if (0 != t->index)
  1107. return -EINVAL;
  1108. return 0;
  1109. }
  1110. static int vidioc_g_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(UNSET == dev->tuner_type))
  1116. return -EINVAL;
  1117. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1118. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1119. f->frequency = dev->freq;
  1120. call_all(dev, tuner, g_frequency, f);
  1121. return 0;
  1122. }
  1123. static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1124. {
  1125. if (unlikely(UNSET == dev->tuner_type))
  1126. return -EINVAL;
  1127. if (unlikely(f->tuner != 0))
  1128. return -EINVAL;
  1129. mutex_lock(&dev->lock);
  1130. dev->freq = f->frequency;
  1131. call_all(dev, tuner, s_frequency, f);
  1132. /* When changing channels it is required to reset TVAUDIO */
  1133. msleep(10);
  1134. mutex_unlock(&dev->lock);
  1135. return 0;
  1136. }
  1137. static int vidioc_s_frequency(struct file *file, void *priv,
  1138. struct v4l2_frequency *f)
  1139. {
  1140. struct cx23885_fh *fh = priv;
  1141. struct cx23885_dev *dev = fh->dev;
  1142. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1143. return -EINVAL;
  1144. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1145. return -EINVAL;
  1146. return
  1147. cx23885_set_freq(dev, f);
  1148. }
  1149. /* ----------------------------------------------------------- */
  1150. static void cx23885_vid_timeout(unsigned long data)
  1151. {
  1152. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1153. struct cx23885_dmaqueue *q = &dev->vidq;
  1154. struct cx23885_buffer *buf;
  1155. unsigned long flags;
  1156. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1157. cx_clear(VID_A_DMA_CTL, 0x11);
  1158. spin_lock_irqsave(&dev->slock, flags);
  1159. while (!list_empty(&q->active)) {
  1160. buf = list_entry(q->active.next,
  1161. struct cx23885_buffer, vb.queue);
  1162. list_del(&buf->vb.queue);
  1163. buf->vb.state = VIDEOBUF_ERROR;
  1164. wake_up(&buf->vb.done);
  1165. printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
  1166. dev->name, buf, buf->vb.i,
  1167. (unsigned long)buf->risc.dma);
  1168. }
  1169. cx23885_restart_video_queue(dev, q);
  1170. spin_unlock_irqrestore(&dev->slock, flags);
  1171. }
  1172. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1173. {
  1174. u32 mask, count;
  1175. int handled = 0;
  1176. mask = cx_read(VID_A_INT_MSK);
  1177. if (0 == (status & mask))
  1178. return handled;
  1179. cx_write(VID_A_INT_STAT, status);
  1180. dprintk(2, "%s() status = 0x%08x\n", __func__, status);
  1181. /* risc op code error */
  1182. if (status & (1 << 16)) {
  1183. printk(KERN_WARNING "%s/0: video risc op code error\n",
  1184. dev->name);
  1185. cx_clear(VID_A_DMA_CTL, 0x11);
  1186. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1187. }
  1188. /* risc1 y */
  1189. if (status & 0x01) {
  1190. spin_lock(&dev->slock);
  1191. count = cx_read(VID_A_GPCNT);
  1192. cx23885_video_wakeup(dev, &dev->vidq, count);
  1193. spin_unlock(&dev->slock);
  1194. handled++;
  1195. }
  1196. /* risc2 y */
  1197. if (status & 0x10) {
  1198. dprintk(2, "stopper video\n");
  1199. spin_lock(&dev->slock);
  1200. cx23885_restart_video_queue(dev, &dev->vidq);
  1201. spin_unlock(&dev->slock);
  1202. handled++;
  1203. }
  1204. return handled;
  1205. }
  1206. /* ----------------------------------------------------------- */
  1207. /* exported stuff */
  1208. static const struct v4l2_file_operations video_fops = {
  1209. .owner = THIS_MODULE,
  1210. .open = video_open,
  1211. .release = video_release,
  1212. .read = video_read,
  1213. .poll = video_poll,
  1214. .mmap = video_mmap,
  1215. .ioctl = video_ioctl2,
  1216. };
  1217. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1218. .vidioc_querycap = vidioc_querycap,
  1219. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1220. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1221. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1222. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1223. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  1224. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  1225. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  1226. .vidioc_reqbufs = vidioc_reqbufs,
  1227. .vidioc_querybuf = vidioc_querybuf,
  1228. .vidioc_qbuf = vidioc_qbuf,
  1229. .vidioc_dqbuf = vidioc_dqbuf,
  1230. .vidioc_s_std = vidioc_s_std,
  1231. .vidioc_enum_input = vidioc_enum_input,
  1232. .vidioc_g_input = vidioc_g_input,
  1233. .vidioc_s_input = vidioc_s_input,
  1234. .vidioc_log_status = vidioc_log_status,
  1235. .vidioc_queryctrl = vidioc_queryctrl,
  1236. .vidioc_g_ctrl = vidioc_g_ctrl,
  1237. .vidioc_s_ctrl = vidioc_s_ctrl,
  1238. .vidioc_streamon = vidioc_streamon,
  1239. .vidioc_streamoff = vidioc_streamoff,
  1240. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1241. .vidiocgmbuf = vidiocgmbuf,
  1242. #endif
  1243. .vidioc_g_tuner = vidioc_g_tuner,
  1244. .vidioc_s_tuner = vidioc_s_tuner,
  1245. .vidioc_g_frequency = vidioc_g_frequency,
  1246. .vidioc_s_frequency = vidioc_s_frequency,
  1247. .vidioc_g_chip_ident = cx23885_g_chip_ident,
  1248. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1249. .vidioc_g_register = cx23885_g_register,
  1250. .vidioc_s_register = cx23885_s_register,
  1251. #endif
  1252. };
  1253. static struct video_device cx23885_vbi_template;
  1254. static struct video_device cx23885_video_template = {
  1255. .name = "cx23885-video",
  1256. .fops = &video_fops,
  1257. .ioctl_ops = &video_ioctl_ops,
  1258. .tvnorms = CX23885_NORMS,
  1259. .current_norm = V4L2_STD_NTSC_M,
  1260. };
  1261. static const struct v4l2_file_operations radio_fops = {
  1262. .owner = THIS_MODULE,
  1263. .open = video_open,
  1264. .release = video_release,
  1265. .ioctl = video_ioctl2,
  1266. };
  1267. void cx23885_video_unregister(struct cx23885_dev *dev)
  1268. {
  1269. dprintk(1, "%s()\n", __func__);
  1270. cx23885_irq_remove(dev, 0x01);
  1271. if (dev->video_dev) {
  1272. if (video_is_registered(dev->video_dev))
  1273. video_unregister_device(dev->video_dev);
  1274. else
  1275. video_device_release(dev->video_dev);
  1276. dev->video_dev = NULL;
  1277. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1278. }
  1279. }
  1280. int cx23885_video_register(struct cx23885_dev *dev)
  1281. {
  1282. int err;
  1283. dprintk(1, "%s()\n", __func__);
  1284. spin_lock_init(&dev->slock);
  1285. /* Initialize VBI template */
  1286. memcpy(&cx23885_vbi_template, &cx23885_video_template,
  1287. sizeof(cx23885_vbi_template));
  1288. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1289. dev->tvnorm = cx23885_video_template.current_norm;
  1290. /* init video dma queues */
  1291. INIT_LIST_HEAD(&dev->vidq.active);
  1292. INIT_LIST_HEAD(&dev->vidq.queued);
  1293. dev->vidq.timeout.function = cx23885_vid_timeout;
  1294. dev->vidq.timeout.data = (unsigned long)dev;
  1295. init_timer(&dev->vidq.timeout);
  1296. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1297. VID_A_DMA_CTL, 0x11, 0x00);
  1298. /* Don't enable VBI yet */
  1299. cx23885_irq_add_enable(dev, 0x01);
  1300. if (TUNER_ABSENT != dev->tuner_type) {
  1301. struct v4l2_subdev *sd = NULL;
  1302. if (dev->tuner_addr)
  1303. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1304. &dev->i2c_bus[1].i2c_adap,
  1305. "tuner", dev->tuner_addr, NULL);
  1306. else
  1307. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1308. &dev->i2c_bus[1].i2c_adap,
  1309. "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
  1310. if (sd) {
  1311. struct tuner_setup tun_setup;
  1312. memset(&tun_setup, 0, sizeof(tun_setup));
  1313. tun_setup.mode_mask = T_ANALOG_TV;
  1314. tun_setup.type = dev->tuner_type;
  1315. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  1316. tun_setup.tuner_callback = cx23885_tuner_callback;
  1317. v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
  1318. if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
  1319. struct xc2028_ctrl ctrl = {
  1320. .fname = XC2028_DEFAULT_FIRMWARE,
  1321. .max_len = 64
  1322. };
  1323. struct v4l2_priv_tun_config cfg = {
  1324. .tuner = dev->tuner_type,
  1325. .priv = &ctrl
  1326. };
  1327. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1328. }
  1329. }
  1330. }
  1331. /* register v4l devices */
  1332. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1333. &cx23885_video_template, "video");
  1334. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1335. video_nr[dev->nr]);
  1336. if (err < 0) {
  1337. printk(KERN_INFO "%s: can't register video device\n",
  1338. dev->name);
  1339. goto fail_unreg;
  1340. }
  1341. printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
  1342. dev->name, video_device_node_name(dev->video_dev));
  1343. /* initial device configuration */
  1344. mutex_lock(&dev->lock);
  1345. cx23885_set_tvnorm(dev, dev->tvnorm);
  1346. init_controls(dev);
  1347. cx23885_video_mux(dev, 0);
  1348. mutex_unlock(&dev->lock);
  1349. return 0;
  1350. fail_unreg:
  1351. cx23885_video_unregister(dev);
  1352. return err;
  1353. }