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