cx23885-video.c 40 KB

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