cx23885-video.c 39 KB

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