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/smp_lock.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/kthread.h>
  32. #include <asm/div64.h>
  33. #include "cx23885.h"
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
  37. MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  38. MODULE_LICENSE("GPL");
  39. /* ------------------------------------------------------------------ */
  40. static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  41. static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  42. static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  43. module_param_array(video_nr, int, NULL, 0444);
  44. module_param_array(vbi_nr, int, NULL, 0444);
  45. module_param_array(radio_nr, int, NULL, 0444);
  46. MODULE_PARM_DESC(video_nr, "video device numbers");
  47. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  48. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  49. static unsigned int video_debug;
  50. module_param(video_debug, int, 0644);
  51. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  52. static unsigned int irq_debug;
  53. module_param(irq_debug, int, 0644);
  54. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  55. static unsigned int vid_limit = 16;
  56. module_param(vid_limit, int, 0644);
  57. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  58. #define dprintk(level, fmt, arg...)\
  59. do { if (video_debug >= level)\
  60. printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
  61. } while (0)
  62. /* ------------------------------------------------------------------- */
  63. /* static data */
  64. #define FORMAT_FLAGS_PACKED 0x01
  65. static struct cx23885_fmt formats[] = {
  66. {
  67. .name = "8 bpp, gray",
  68. .fourcc = V4L2_PIX_FMT_GREY,
  69. .depth = 8,
  70. .flags = FORMAT_FLAGS_PACKED,
  71. }, {
  72. .name = "15 bpp RGB, le",
  73. .fourcc = V4L2_PIX_FMT_RGB555,
  74. .depth = 16,
  75. .flags = FORMAT_FLAGS_PACKED,
  76. }, {
  77. .name = "15 bpp RGB, be",
  78. .fourcc = V4L2_PIX_FMT_RGB555X,
  79. .depth = 16,
  80. .flags = FORMAT_FLAGS_PACKED,
  81. }, {
  82. .name = "16 bpp RGB, le",
  83. .fourcc = V4L2_PIX_FMT_RGB565,
  84. .depth = 16,
  85. .flags = FORMAT_FLAGS_PACKED,
  86. }, {
  87. .name = "16 bpp RGB, be",
  88. .fourcc = V4L2_PIX_FMT_RGB565X,
  89. .depth = 16,
  90. .flags = FORMAT_FLAGS_PACKED,
  91. }, {
  92. .name = "24 bpp RGB, le",
  93. .fourcc = V4L2_PIX_FMT_BGR24,
  94. .depth = 24,
  95. .flags = FORMAT_FLAGS_PACKED,
  96. }, {
  97. .name = "32 bpp RGB, le",
  98. .fourcc = V4L2_PIX_FMT_BGR32,
  99. .depth = 32,
  100. .flags = FORMAT_FLAGS_PACKED,
  101. }, {
  102. .name = "32 bpp RGB, be",
  103. .fourcc = V4L2_PIX_FMT_RGB32,
  104. .depth = 32,
  105. .flags = FORMAT_FLAGS_PACKED,
  106. }, {
  107. .name = "4:2:2, packed, YUYV",
  108. .fourcc = V4L2_PIX_FMT_YUYV,
  109. .depth = 16,
  110. .flags = FORMAT_FLAGS_PACKED,
  111. }, {
  112. .name = "4:2:2, packed, UYVY",
  113. .fourcc = V4L2_PIX_FMT_UYVY,
  114. .depth = 16,
  115. .flags = FORMAT_FLAGS_PACKED,
  116. },
  117. };
  118. static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
  119. {
  120. unsigned int i;
  121. for (i = 0; i < ARRAY_SIZE(formats); i++)
  122. if (formats[i].fourcc == fourcc)
  123. return formats+i;
  124. printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
  125. return NULL;
  126. }
  127. /* ------------------------------------------------------------------- */
  128. static const struct v4l2_queryctrl no_ctl = {
  129. .name = "42",
  130. .flags = V4L2_CTRL_FLAG_DISABLED,
  131. };
  132. static struct cx23885_ctrl cx23885_ctls[] = {
  133. /* --- video --- */
  134. {
  135. .v = {
  136. .id = V4L2_CID_BRIGHTNESS,
  137. .name = "Brightness",
  138. .minimum = 0x00,
  139. .maximum = 0xff,
  140. .step = 1,
  141. .default_value = 0x7f,
  142. .type = V4L2_CTRL_TYPE_INTEGER,
  143. },
  144. .off = 128,
  145. .reg = LUMA_CTRL,
  146. .mask = 0x00ff,
  147. .shift = 0,
  148. }, {
  149. .v = {
  150. .id = V4L2_CID_CONTRAST,
  151. .name = "Contrast",
  152. .minimum = 0,
  153. .maximum = 0xff,
  154. .step = 1,
  155. .default_value = 0x3f,
  156. .type = V4L2_CTRL_TYPE_INTEGER,
  157. },
  158. .off = 0,
  159. .reg = LUMA_CTRL,
  160. .mask = 0xff00,
  161. .shift = 8,
  162. }, {
  163. .v = {
  164. .id = V4L2_CID_HUE,
  165. .name = "Hue",
  166. .minimum = 0,
  167. .maximum = 0xff,
  168. .step = 1,
  169. .default_value = 0x7f,
  170. .type = V4L2_CTRL_TYPE_INTEGER,
  171. },
  172. .off = 128,
  173. .reg = CHROMA_CTRL,
  174. .mask = 0xff0000,
  175. .shift = 16,
  176. }, {
  177. /* strictly, this only describes only U saturation.
  178. * V saturation is handled specially through code.
  179. */
  180. .v = {
  181. .id = V4L2_CID_SATURATION,
  182. .name = "Saturation",
  183. .minimum = 0,
  184. .maximum = 0xff,
  185. .step = 1,
  186. .default_value = 0x7f,
  187. .type = V4L2_CTRL_TYPE_INTEGER,
  188. },
  189. .off = 0,
  190. .reg = CHROMA_CTRL,
  191. .mask = 0x00ff,
  192. .shift = 0,
  193. }, {
  194. /* --- audio --- */
  195. .v = {
  196. .id = V4L2_CID_AUDIO_MUTE,
  197. .name = "Mute",
  198. .minimum = 0,
  199. .maximum = 1,
  200. .default_value = 1,
  201. .type = V4L2_CTRL_TYPE_BOOLEAN,
  202. },
  203. .reg = PATH1_CTL1,
  204. .mask = (0x1f << 24),
  205. .shift = 24,
  206. }, {
  207. .v = {
  208. .id = V4L2_CID_AUDIO_VOLUME,
  209. .name = "Volume",
  210. .minimum = 0,
  211. .maximum = 0x3f,
  212. .step = 1,
  213. .default_value = 0x3f,
  214. .type = V4L2_CTRL_TYPE_INTEGER,
  215. },
  216. .reg = PATH1_VOL_CTL,
  217. .mask = 0xff,
  218. .shift = 0,
  219. }
  220. };
  221. static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
  222. /* Must be sorted from low to high control ID! */
  223. static const u32 cx23885_user_ctrls[] = {
  224. V4L2_CID_USER_CLASS,
  225. V4L2_CID_BRIGHTNESS,
  226. V4L2_CID_CONTRAST,
  227. V4L2_CID_SATURATION,
  228. V4L2_CID_HUE,
  229. V4L2_CID_AUDIO_VOLUME,
  230. V4L2_CID_AUDIO_MUTE,
  231. 0
  232. };
  233. static const u32 *ctrl_classes[] = {
  234. cx23885_user_ctrls,
  235. NULL
  236. };
  237. static void cx23885_video_wakeup(struct cx23885_dev *dev,
  238. struct cx23885_dmaqueue *q, u32 count)
  239. {
  240. struct cx23885_buffer *buf;
  241. int bc;
  242. for (bc = 0;; bc++) {
  243. if (list_empty(&q->active))
  244. break;
  245. buf = list_entry(q->active.next,
  246. struct cx23885_buffer, vb.queue);
  247. /* count comes from the hw and is is 16bit wide --
  248. * this trick handles wrap-arounds correctly for
  249. * up to 32767 buffers in flight... */
  250. if ((s16) (count - buf->count) < 0)
  251. break;
  252. do_gettimeofday(&buf->vb.ts);
  253. dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
  254. count, buf->count);
  255. buf->vb.state = VIDEOBUF_DONE;
  256. list_del(&buf->vb.queue);
  257. wake_up(&buf->vb.done);
  258. }
  259. if (list_empty(&q->active))
  260. del_timer(&q->timeout);
  261. else
  262. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  263. if (bc != 1)
  264. printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
  265. __func__, bc);
  266. }
  267. static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
  268. {
  269. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  270. __func__,
  271. (unsigned int)norm,
  272. v4l2_norm_to_name(norm));
  273. dev->tvnorm = norm;
  274. call_all(dev, core, s_std, norm);
  275. return 0;
  276. }
  277. static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
  278. struct pci_dev *pci,
  279. struct video_device *template,
  280. char *type)
  281. {
  282. struct video_device *vfd;
  283. dprintk(1, "%s()\n", __func__);
  284. vfd = video_device_alloc();
  285. if (NULL == vfd)
  286. return NULL;
  287. *vfd = *template;
  288. vfd->minor = -1;
  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. return vfd;
  294. }
  295. static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
  296. {
  297. int i;
  298. if (qctrl->id < V4L2_CID_BASE ||
  299. qctrl->id >= V4L2_CID_LASTP1)
  300. return -EINVAL;
  301. for (i = 0; i < CX23885_CTLS; i++)
  302. if (cx23885_ctls[i].v.id == qctrl->id)
  303. break;
  304. if (i == CX23885_CTLS) {
  305. *qctrl = no_ctl;
  306. return 0;
  307. }
  308. *qctrl = cx23885_ctls[i].v;
  309. return 0;
  310. }
  311. /* ------------------------------------------------------------------- */
  312. /* resource management */
  313. static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
  314. unsigned int bit)
  315. {
  316. dprintk(1, "%s()\n", __func__);
  317. if (fh->resources & bit)
  318. /* have it already allocated */
  319. return 1;
  320. /* is it free? */
  321. mutex_lock(&dev->lock);
  322. if (dev->resources & bit) {
  323. /* no, someone else uses it */
  324. mutex_unlock(&dev->lock);
  325. return 0;
  326. }
  327. /* it's free, grab it */
  328. fh->resources |= bit;
  329. dev->resources |= bit;
  330. dprintk(1, "res: get %d\n", bit);
  331. mutex_unlock(&dev->lock);
  332. return 1;
  333. }
  334. static int res_check(struct cx23885_fh *fh, unsigned int bit)
  335. {
  336. return fh->resources & bit;
  337. }
  338. static int res_locked(struct cx23885_dev *dev, unsigned int bit)
  339. {
  340. return dev->resources & bit;
  341. }
  342. static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
  343. unsigned int bits)
  344. {
  345. BUG_ON((fh->resources & bits) != bits);
  346. dprintk(1, "%s()\n", __func__);
  347. mutex_lock(&dev->lock);
  348. fh->resources &= ~bits;
  349. dev->resources &= ~bits;
  350. dprintk(1, "res: put %d\n", bits);
  351. mutex_unlock(&dev->lock);
  352. }
  353. static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
  354. {
  355. dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  356. __func__,
  357. input, INPUT(input)->vmux,
  358. INPUT(input)->gpio0, INPUT(input)->gpio1,
  359. INPUT(input)->gpio2, INPUT(input)->gpio3);
  360. dev->input = input;
  361. /* Tell the internal A/V decoder */
  362. v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
  363. INPUT(input)->vmux, 0, 0);
  364. return 0;
  365. }
  366. /* ------------------------------------------------------------------ */
  367. static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
  368. unsigned int height, enum v4l2_field field)
  369. {
  370. dprintk(1, "%s()\n", __func__);
  371. return 0;
  372. }
  373. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  374. struct cx23885_dmaqueue *q,
  375. struct cx23885_buffer *buf)
  376. {
  377. dprintk(1, "%s()\n", __func__);
  378. /* setup fifo + format */
  379. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  380. buf->bpl, buf->risc.dma);
  381. cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
  382. /* reset counter */
  383. cx_write(VID_A_GPCNT_CTL, 3);
  384. q->count = 1;
  385. /* enable irq */
  386. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
  387. cx_set(VID_A_INT_MSK, 0x000011);
  388. /* start dma */
  389. cx_set(DEV_CNTRL2, (1<<5));
  390. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  391. return 0;
  392. }
  393. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  394. struct cx23885_dmaqueue *q)
  395. {
  396. struct cx23885_buffer *buf, *prev;
  397. struct list_head *item;
  398. dprintk(1, "%s()\n", __func__);
  399. if (!list_empty(&q->active)) {
  400. buf = list_entry(q->active.next, struct cx23885_buffer,
  401. vb.queue);
  402. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  403. buf, buf->vb.i);
  404. cx23885_start_video_dma(dev, q, buf);
  405. list_for_each(item, &q->active) {
  406. buf = list_entry(item, struct cx23885_buffer,
  407. vb.queue);
  408. buf->count = q->count++;
  409. }
  410. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  411. return 0;
  412. }
  413. prev = NULL;
  414. for (;;) {
  415. if (list_empty(&q->queued))
  416. return 0;
  417. buf = list_entry(q->queued.next, struct cx23885_buffer,
  418. vb.queue);
  419. if (NULL == prev) {
  420. list_move_tail(&buf->vb.queue, &q->active);
  421. cx23885_start_video_dma(dev, q, buf);
  422. buf->vb.state = VIDEOBUF_ACTIVE;
  423. buf->count = q->count++;
  424. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  425. dprintk(2, "[%p/%d] restart_queue - first active\n",
  426. buf, buf->vb.i);
  427. } else if (prev->vb.width == buf->vb.width &&
  428. prev->vb.height == buf->vb.height &&
  429. prev->fmt == buf->fmt) {
  430. list_move_tail(&buf->vb.queue, &q->active);
  431. buf->vb.state = VIDEOBUF_ACTIVE;
  432. buf->count = q->count++;
  433. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  434. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  435. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  436. buf, buf->vb.i);
  437. } else {
  438. return 0;
  439. }
  440. prev = buf;
  441. }
  442. }
  443. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  444. unsigned int *size)
  445. {
  446. struct cx23885_fh *fh = q->priv_data;
  447. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  448. if (0 == *count)
  449. *count = 32;
  450. while (*size * *count > vid_limit * 1024 * 1024)
  451. (*count)--;
  452. return 0;
  453. }
  454. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  455. enum v4l2_field field)
  456. {
  457. struct cx23885_fh *fh = q->priv_data;
  458. struct cx23885_dev *dev = fh->dev;
  459. struct cx23885_buffer *buf =
  460. container_of(vb, struct cx23885_buffer, vb);
  461. int rc, init_buffer = 0;
  462. u32 line0_offset, line1_offset;
  463. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  464. BUG_ON(NULL == fh->fmt);
  465. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  466. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  467. return -EINVAL;
  468. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  469. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  470. return -EINVAL;
  471. if (buf->fmt != fh->fmt ||
  472. buf->vb.width != fh->width ||
  473. buf->vb.height != fh->height ||
  474. buf->vb.field != field) {
  475. buf->fmt = fh->fmt;
  476. buf->vb.width = fh->width;
  477. buf->vb.height = fh->height;
  478. buf->vb.field = field;
  479. init_buffer = 1;
  480. }
  481. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  482. init_buffer = 1;
  483. rc = videobuf_iolock(q, &buf->vb, NULL);
  484. if (0 != rc)
  485. goto fail;
  486. }
  487. if (init_buffer) {
  488. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  489. switch (buf->vb.field) {
  490. case V4L2_FIELD_TOP:
  491. cx23885_risc_buffer(dev->pci, &buf->risc,
  492. dma->sglist, 0, UNSET,
  493. buf->bpl, 0, buf->vb.height);
  494. break;
  495. case V4L2_FIELD_BOTTOM:
  496. cx23885_risc_buffer(dev->pci, &buf->risc,
  497. dma->sglist, UNSET, 0,
  498. buf->bpl, 0, buf->vb.height);
  499. break;
  500. case V4L2_FIELD_INTERLACED:
  501. if (dev->tvnorm & V4L2_STD_NTSC) {
  502. /* cx25840 transmits NTSC bottom field first */
  503. dprintk(1, "%s() Creating NTSC risc\n",
  504. __func__);
  505. line0_offset = buf->bpl;
  506. line1_offset = 0;
  507. } else {
  508. /* All other formats are top field first */
  509. dprintk(1, "%s() Creating PAL/SECAM risc\n",
  510. __func__);
  511. line0_offset = 0;
  512. line1_offset = buf->bpl;
  513. }
  514. cx23885_risc_buffer(dev->pci, &buf->risc,
  515. dma->sglist, line0_offset,
  516. line1_offset,
  517. buf->bpl, buf->bpl,
  518. buf->vb.height >> 1);
  519. break;
  520. case V4L2_FIELD_SEQ_TB:
  521. cx23885_risc_buffer(dev->pci, &buf->risc,
  522. dma->sglist,
  523. 0, buf->bpl * (buf->vb.height >> 1),
  524. buf->bpl, 0,
  525. buf->vb.height >> 1);
  526. break;
  527. case V4L2_FIELD_SEQ_BT:
  528. cx23885_risc_buffer(dev->pci, &buf->risc,
  529. dma->sglist,
  530. buf->bpl * (buf->vb.height >> 1), 0,
  531. buf->bpl, 0,
  532. buf->vb.height >> 1);
  533. break;
  534. default:
  535. BUG();
  536. }
  537. }
  538. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  539. buf, buf->vb.i,
  540. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  541. (unsigned long)buf->risc.dma);
  542. buf->vb.state = VIDEOBUF_PREPARED;
  543. return 0;
  544. fail:
  545. cx23885_free_buffer(q, buf);
  546. return rc;
  547. }
  548. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  549. {
  550. struct cx23885_buffer *buf = container_of(vb,
  551. struct cx23885_buffer, vb);
  552. struct cx23885_buffer *prev;
  553. struct cx23885_fh *fh = vq->priv_data;
  554. struct cx23885_dev *dev = fh->dev;
  555. struct cx23885_dmaqueue *q = &dev->vidq;
  556. /* add jump to stopper */
  557. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  558. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  559. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  560. if (!list_empty(&q->queued)) {
  561. list_add_tail(&buf->vb.queue, &q->queued);
  562. buf->vb.state = VIDEOBUF_QUEUED;
  563. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  564. buf, buf->vb.i);
  565. } else if (list_empty(&q->active)) {
  566. list_add_tail(&buf->vb.queue, &q->active);
  567. cx23885_start_video_dma(dev, q, buf);
  568. buf->vb.state = VIDEOBUF_ACTIVE;
  569. buf->count = q->count++;
  570. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  571. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  572. buf, buf->vb.i);
  573. } else {
  574. prev = list_entry(q->active.prev, struct cx23885_buffer,
  575. vb.queue);
  576. if (prev->vb.width == buf->vb.width &&
  577. prev->vb.height == buf->vb.height &&
  578. prev->fmt == buf->fmt) {
  579. list_add_tail(&buf->vb.queue, &q->active);
  580. buf->vb.state = VIDEOBUF_ACTIVE;
  581. buf->count = q->count++;
  582. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  583. /* 64 bit bits 63-32 */
  584. prev->risc.jmp[2] = cpu_to_le32(0);
  585. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  586. buf, buf->vb.i);
  587. } else {
  588. list_add_tail(&buf->vb.queue, &q->queued);
  589. buf->vb.state = VIDEOBUF_QUEUED;
  590. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  591. buf, buf->vb.i);
  592. }
  593. }
  594. }
  595. static void buffer_release(struct videobuf_queue *q,
  596. struct videobuf_buffer *vb)
  597. {
  598. struct cx23885_buffer *buf = container_of(vb,
  599. struct cx23885_buffer, vb);
  600. cx23885_free_buffer(q, buf);
  601. }
  602. static struct videobuf_queue_ops cx23885_video_qops = {
  603. .buf_setup = buffer_setup,
  604. .buf_prepare = buffer_prepare,
  605. .buf_queue = buffer_queue,
  606. .buf_release = buffer_release,
  607. };
  608. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  609. {
  610. switch (fh->type) {
  611. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  612. return &fh->vidq;
  613. case V4L2_BUF_TYPE_VBI_CAPTURE:
  614. return &fh->vbiq;
  615. default:
  616. BUG();
  617. return NULL;
  618. }
  619. }
  620. static int get_resource(struct cx23885_fh *fh)
  621. {
  622. switch (fh->type) {
  623. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  624. return RESOURCE_VIDEO;
  625. case V4L2_BUF_TYPE_VBI_CAPTURE:
  626. return RESOURCE_VBI;
  627. default:
  628. BUG();
  629. return 0;
  630. }
  631. }
  632. static int video_open(struct file *file)
  633. {
  634. int minor = video_devdata(file)->minor;
  635. struct cx23885_dev *h, *dev = NULL;
  636. struct cx23885_fh *fh;
  637. struct list_head *list;
  638. enum v4l2_buf_type type = 0;
  639. int radio = 0;
  640. lock_kernel();
  641. list_for_each(list, &cx23885_devlist) {
  642. h = list_entry(list, struct cx23885_dev, devlist);
  643. if (h->video_dev &&
  644. h->video_dev->minor == minor) {
  645. dev = h;
  646. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  647. }
  648. if (h->vbi_dev &&
  649. h->vbi_dev->minor == minor) {
  650. dev = h;
  651. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  652. }
  653. if (h->radio_dev &&
  654. h->radio_dev->minor == minor) {
  655. radio = 1;
  656. dev = h;
  657. }
  658. }
  659. if (NULL == dev) {
  660. unlock_kernel();
  661. return -ENODEV;
  662. }
  663. dprintk(1, "open minor=%d radio=%d type=%s\n",
  664. minor, radio, v4l2_type_names[type]);
  665. /* allocate + initialize per filehandle data */
  666. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  667. if (NULL == fh) {
  668. unlock_kernel();
  669. return -ENOMEM;
  670. }
  671. file->private_data = fh;
  672. fh->dev = dev;
  673. fh->radio = radio;
  674. fh->type = type;
  675. fh->width = 320;
  676. fh->height = 240;
  677. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  678. videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
  679. &dev->pci->dev, &dev->slock,
  680. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  681. V4L2_FIELD_INTERLACED,
  682. sizeof(struct cx23885_buffer),
  683. fh);
  684. dprintk(1, "post videobuf_queue_init()\n");
  685. unlock_kernel();
  686. return 0;
  687. }
  688. static ssize_t video_read(struct file *file, char __user *data,
  689. size_t count, loff_t *ppos)
  690. {
  691. struct cx23885_fh *fh = file->private_data;
  692. switch (fh->type) {
  693. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  694. if (res_locked(fh->dev, RESOURCE_VIDEO))
  695. return -EBUSY;
  696. return videobuf_read_one(&fh->vidq, data, count, ppos,
  697. file->f_flags & O_NONBLOCK);
  698. case V4L2_BUF_TYPE_VBI_CAPTURE:
  699. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  700. return -EBUSY;
  701. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  702. file->f_flags & O_NONBLOCK);
  703. default:
  704. BUG();
  705. return 0;
  706. }
  707. }
  708. static unsigned int video_poll(struct file *file,
  709. struct poll_table_struct *wait)
  710. {
  711. struct cx23885_fh *fh = file->private_data;
  712. struct cx23885_buffer *buf;
  713. unsigned int rc = POLLERR;
  714. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  715. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  716. return POLLERR;
  717. return videobuf_poll_stream(file, &fh->vbiq, wait);
  718. }
  719. mutex_lock(&fh->vidq.vb_lock);
  720. if (res_check(fh, RESOURCE_VIDEO)) {
  721. /* streaming capture */
  722. if (list_empty(&fh->vidq.stream))
  723. goto done;
  724. buf = list_entry(fh->vidq.stream.next,
  725. struct cx23885_buffer, vb.stream);
  726. } else {
  727. /* read() capture */
  728. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  729. if (NULL == buf)
  730. goto done;
  731. }
  732. poll_wait(file, &buf->vb.done, wait);
  733. if (buf->vb.state == VIDEOBUF_DONE ||
  734. buf->vb.state == VIDEOBUF_ERROR)
  735. rc = POLLIN|POLLRDNORM;
  736. else
  737. rc = 0;
  738. done:
  739. mutex_unlock(&fh->vidq.vb_lock);
  740. return rc;
  741. }
  742. static int video_release(struct file *file)
  743. {
  744. struct cx23885_fh *fh = file->private_data;
  745. struct cx23885_dev *dev = fh->dev;
  746. /* turn off overlay */
  747. if (res_check(fh, RESOURCE_OVERLAY)) {
  748. /* FIXME */
  749. res_free(dev, fh, RESOURCE_OVERLAY);
  750. }
  751. /* stop video capture */
  752. if (res_check(fh, RESOURCE_VIDEO)) {
  753. videobuf_queue_cancel(&fh->vidq);
  754. res_free(dev, fh, RESOURCE_VIDEO);
  755. }
  756. if (fh->vidq.read_buf) {
  757. buffer_release(&fh->vidq, fh->vidq.read_buf);
  758. kfree(fh->vidq.read_buf);
  759. }
  760. /* stop vbi capture */
  761. if (res_check(fh, RESOURCE_VBI)) {
  762. if (fh->vbiq.streaming)
  763. videobuf_streamoff(&fh->vbiq);
  764. if (fh->vbiq.reading)
  765. videobuf_read_stop(&fh->vbiq);
  766. res_free(dev, fh, RESOURCE_VBI);
  767. }
  768. videobuf_mmap_free(&fh->vidq);
  769. file->private_data = NULL;
  770. kfree(fh);
  771. /* We are not putting the tuner to sleep here on exit, because
  772. * we want to use the mpeg encoder in another session to capture
  773. * tuner video. Closing this will result in no video to the encoder.
  774. */
  775. return 0;
  776. }
  777. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  778. {
  779. struct cx23885_fh *fh = file->private_data;
  780. return videobuf_mmap_mapper(get_queue(fh), vma);
  781. }
  782. /* ------------------------------------------------------------------ */
  783. /* VIDEO CTRL IOCTLS */
  784. static int cx23885_get_control(struct cx23885_dev *dev,
  785. struct v4l2_control *ctl)
  786. {
  787. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
  788. call_all(dev, core, g_ctrl, ctl);
  789. return 0;
  790. }
  791. static int cx23885_set_control(struct cx23885_dev *dev,
  792. struct v4l2_control *ctl)
  793. {
  794. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
  795. " (disabled - no action)\n", __func__);
  796. return 0;
  797. }
  798. static void init_controls(struct cx23885_dev *dev)
  799. {
  800. struct v4l2_control ctrl;
  801. int i;
  802. for (i = 0; i < CX23885_CTLS; i++) {
  803. ctrl.id = cx23885_ctls[i].v.id;
  804. ctrl.value = cx23885_ctls[i].v.default_value;
  805. cx23885_set_control(dev, &ctrl);
  806. }
  807. }
  808. /* ------------------------------------------------------------------ */
  809. /* VIDEO IOCTLS */
  810. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  811. struct v4l2_format *f)
  812. {
  813. struct cx23885_fh *fh = priv;
  814. f->fmt.pix.width = fh->width;
  815. f->fmt.pix.height = fh->height;
  816. f->fmt.pix.field = fh->vidq.field;
  817. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  818. f->fmt.pix.bytesperline =
  819. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  820. f->fmt.pix.sizeimage =
  821. f->fmt.pix.height * f->fmt.pix.bytesperline;
  822. return 0;
  823. }
  824. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  825. struct v4l2_format *f)
  826. {
  827. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  828. struct cx23885_fmt *fmt;
  829. enum v4l2_field field;
  830. unsigned int maxw, maxh;
  831. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  832. if (NULL == fmt)
  833. return -EINVAL;
  834. field = f->fmt.pix.field;
  835. maxw = norm_maxw(dev->tvnorm);
  836. maxh = norm_maxh(dev->tvnorm);
  837. if (V4L2_FIELD_ANY == field) {
  838. field = (f->fmt.pix.height > maxh/2)
  839. ? V4L2_FIELD_INTERLACED
  840. : V4L2_FIELD_BOTTOM;
  841. }
  842. switch (field) {
  843. case V4L2_FIELD_TOP:
  844. case V4L2_FIELD_BOTTOM:
  845. maxh = maxh / 2;
  846. break;
  847. case V4L2_FIELD_INTERLACED:
  848. break;
  849. default:
  850. return -EINVAL;
  851. }
  852. f->fmt.pix.field = field;
  853. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  854. &f->fmt.pix.height, 32, maxh, 0, 0);
  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. }