cx23885-video.c 40 KB

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