cx23885-video.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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, tuner, 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. struct v4l2_routing route;
  355. memset(&route, 0, sizeof(route));
  356. dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  357. __func__,
  358. input, INPUT(input)->vmux,
  359. INPUT(input)->gpio0, INPUT(input)->gpio1,
  360. INPUT(input)->gpio2, INPUT(input)->gpio3);
  361. dev->input = input;
  362. route.input = INPUT(input)->vmux;
  363. /* Tell the internal A/V decoder */
  364. v4l2_subdev_call(dev->sd_cx25840, video, s_routing, &route);
  365. return 0;
  366. }
  367. /* ------------------------------------------------------------------ */
  368. static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
  369. unsigned int height, enum v4l2_field field)
  370. {
  371. dprintk(1, "%s()\n", __func__);
  372. return 0;
  373. }
  374. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  375. struct cx23885_dmaqueue *q,
  376. struct cx23885_buffer *buf)
  377. {
  378. dprintk(1, "%s()\n", __func__);
  379. /* setup fifo + format */
  380. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  381. buf->bpl, buf->risc.dma);
  382. cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
  383. /* reset counter */
  384. cx_write(VID_A_GPCNT_CTL, 3);
  385. q->count = 1;
  386. /* enable irq */
  387. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
  388. cx_set(VID_A_INT_MSK, 0x000011);
  389. /* start dma */
  390. cx_set(DEV_CNTRL2, (1<<5));
  391. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  392. return 0;
  393. }
  394. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  395. struct cx23885_dmaqueue *q)
  396. {
  397. struct cx23885_buffer *buf, *prev;
  398. struct list_head *item;
  399. dprintk(1, "%s()\n", __func__);
  400. if (!list_empty(&q->active)) {
  401. buf = list_entry(q->active.next, struct cx23885_buffer,
  402. vb.queue);
  403. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  404. buf, buf->vb.i);
  405. cx23885_start_video_dma(dev, q, buf);
  406. list_for_each(item, &q->active) {
  407. buf = list_entry(item, struct cx23885_buffer,
  408. vb.queue);
  409. buf->count = q->count++;
  410. }
  411. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  412. return 0;
  413. }
  414. prev = NULL;
  415. for (;;) {
  416. if (list_empty(&q->queued))
  417. return 0;
  418. buf = list_entry(q->queued.next, struct cx23885_buffer,
  419. vb.queue);
  420. if (NULL == prev) {
  421. list_move_tail(&buf->vb.queue, &q->active);
  422. cx23885_start_video_dma(dev, q, buf);
  423. buf->vb.state = VIDEOBUF_ACTIVE;
  424. buf->count = q->count++;
  425. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  426. dprintk(2, "[%p/%d] restart_queue - first active\n",
  427. buf, buf->vb.i);
  428. } else if (prev->vb.width == buf->vb.width &&
  429. prev->vb.height == buf->vb.height &&
  430. prev->fmt == buf->fmt) {
  431. list_move_tail(&buf->vb.queue, &q->active);
  432. buf->vb.state = VIDEOBUF_ACTIVE;
  433. buf->count = q->count++;
  434. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  435. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  436. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  437. buf, buf->vb.i);
  438. } else {
  439. return 0;
  440. }
  441. prev = buf;
  442. }
  443. }
  444. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  445. unsigned int *size)
  446. {
  447. struct cx23885_fh *fh = q->priv_data;
  448. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  449. if (0 == *count)
  450. *count = 32;
  451. while (*size * *count > vid_limit * 1024 * 1024)
  452. (*count)--;
  453. return 0;
  454. }
  455. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  456. enum v4l2_field field)
  457. {
  458. struct cx23885_fh *fh = q->priv_data;
  459. struct cx23885_dev *dev = fh->dev;
  460. struct cx23885_buffer *buf =
  461. container_of(vb, struct cx23885_buffer, vb);
  462. int rc, init_buffer = 0;
  463. u32 line0_offset, line1_offset;
  464. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  465. BUG_ON(NULL == fh->fmt);
  466. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  467. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  468. return -EINVAL;
  469. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  470. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  471. return -EINVAL;
  472. if (buf->fmt != fh->fmt ||
  473. buf->vb.width != fh->width ||
  474. buf->vb.height != fh->height ||
  475. buf->vb.field != field) {
  476. buf->fmt = fh->fmt;
  477. buf->vb.width = fh->width;
  478. buf->vb.height = fh->height;
  479. buf->vb.field = field;
  480. init_buffer = 1;
  481. }
  482. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  483. init_buffer = 1;
  484. rc = videobuf_iolock(q, &buf->vb, NULL);
  485. if (0 != rc)
  486. goto fail;
  487. }
  488. if (init_buffer) {
  489. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  490. switch (buf->vb.field) {
  491. case V4L2_FIELD_TOP:
  492. cx23885_risc_buffer(dev->pci, &buf->risc,
  493. dma->sglist, 0, UNSET,
  494. buf->bpl, 0, buf->vb.height);
  495. break;
  496. case V4L2_FIELD_BOTTOM:
  497. cx23885_risc_buffer(dev->pci, &buf->risc,
  498. dma->sglist, UNSET, 0,
  499. buf->bpl, 0, buf->vb.height);
  500. break;
  501. case V4L2_FIELD_INTERLACED:
  502. if (dev->tvnorm & V4L2_STD_NTSC) {
  503. /* cx25840 transmits NTSC bottom field first */
  504. dprintk(1, "%s() Creating NTSC risc\n",
  505. __func__);
  506. line0_offset = buf->bpl;
  507. line1_offset = 0;
  508. } else {
  509. /* All other formats are top field first */
  510. dprintk(1, "%s() Creating PAL/SECAM risc\n",
  511. __func__);
  512. line0_offset = 0;
  513. line1_offset = buf->bpl;
  514. }
  515. cx23885_risc_buffer(dev->pci, &buf->risc,
  516. dma->sglist, line0_offset,
  517. line1_offset,
  518. buf->bpl, buf->bpl,
  519. buf->vb.height >> 1);
  520. break;
  521. case V4L2_FIELD_SEQ_TB:
  522. cx23885_risc_buffer(dev->pci, &buf->risc,
  523. dma->sglist,
  524. 0, buf->bpl * (buf->vb.height >> 1),
  525. buf->bpl, 0,
  526. buf->vb.height >> 1);
  527. break;
  528. case V4L2_FIELD_SEQ_BT:
  529. cx23885_risc_buffer(dev->pci, &buf->risc,
  530. dma->sglist,
  531. buf->bpl * (buf->vb.height >> 1), 0,
  532. buf->bpl, 0,
  533. buf->vb.height >> 1);
  534. break;
  535. default:
  536. BUG();
  537. }
  538. }
  539. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  540. buf, buf->vb.i,
  541. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  542. (unsigned long)buf->risc.dma);
  543. buf->vb.state = VIDEOBUF_PREPARED;
  544. return 0;
  545. fail:
  546. cx23885_free_buffer(q, buf);
  547. return rc;
  548. }
  549. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  550. {
  551. struct cx23885_buffer *buf = container_of(vb,
  552. struct cx23885_buffer, vb);
  553. struct cx23885_buffer *prev;
  554. struct cx23885_fh *fh = vq->priv_data;
  555. struct cx23885_dev *dev = fh->dev;
  556. struct cx23885_dmaqueue *q = &dev->vidq;
  557. /* add jump to stopper */
  558. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  559. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  560. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  561. if (!list_empty(&q->queued)) {
  562. list_add_tail(&buf->vb.queue, &q->queued);
  563. buf->vb.state = VIDEOBUF_QUEUED;
  564. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  565. buf, buf->vb.i);
  566. } else if (list_empty(&q->active)) {
  567. list_add_tail(&buf->vb.queue, &q->active);
  568. cx23885_start_video_dma(dev, q, buf);
  569. buf->vb.state = VIDEOBUF_ACTIVE;
  570. buf->count = q->count++;
  571. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  572. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  573. buf, buf->vb.i);
  574. } else {
  575. prev = list_entry(q->active.prev, struct cx23885_buffer,
  576. vb.queue);
  577. if (prev->vb.width == buf->vb.width &&
  578. prev->vb.height == buf->vb.height &&
  579. prev->fmt == buf->fmt) {
  580. list_add_tail(&buf->vb.queue, &q->active);
  581. buf->vb.state = VIDEOBUF_ACTIVE;
  582. buf->count = q->count++;
  583. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  584. /* 64 bit bits 63-32 */
  585. prev->risc.jmp[2] = cpu_to_le32(0);
  586. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  587. buf, buf->vb.i);
  588. } else {
  589. list_add_tail(&buf->vb.queue, &q->queued);
  590. buf->vb.state = VIDEOBUF_QUEUED;
  591. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  592. buf, buf->vb.i);
  593. }
  594. }
  595. }
  596. static void buffer_release(struct videobuf_queue *q,
  597. struct videobuf_buffer *vb)
  598. {
  599. struct cx23885_buffer *buf = container_of(vb,
  600. struct cx23885_buffer, vb);
  601. cx23885_free_buffer(q, buf);
  602. }
  603. static struct videobuf_queue_ops cx23885_video_qops = {
  604. .buf_setup = buffer_setup,
  605. .buf_prepare = buffer_prepare,
  606. .buf_queue = buffer_queue,
  607. .buf_release = buffer_release,
  608. };
  609. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  610. {
  611. switch (fh->type) {
  612. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  613. return &fh->vidq;
  614. case V4L2_BUF_TYPE_VBI_CAPTURE:
  615. return &fh->vbiq;
  616. default:
  617. BUG();
  618. return NULL;
  619. }
  620. }
  621. static int get_resource(struct cx23885_fh *fh)
  622. {
  623. switch (fh->type) {
  624. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  625. return RESOURCE_VIDEO;
  626. case V4L2_BUF_TYPE_VBI_CAPTURE:
  627. return RESOURCE_VBI;
  628. default:
  629. BUG();
  630. return 0;
  631. }
  632. }
  633. static int video_open(struct file *file)
  634. {
  635. int minor = video_devdata(file)->minor;
  636. struct cx23885_dev *h, *dev = NULL;
  637. struct cx23885_fh *fh;
  638. struct list_head *list;
  639. enum v4l2_buf_type type = 0;
  640. int radio = 0;
  641. lock_kernel();
  642. list_for_each(list, &cx23885_devlist) {
  643. h = list_entry(list, struct cx23885_dev, devlist);
  644. if (h->video_dev &&
  645. h->video_dev->minor == minor) {
  646. dev = h;
  647. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  648. }
  649. if (h->vbi_dev &&
  650. h->vbi_dev->minor == minor) {
  651. dev = h;
  652. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  653. }
  654. if (h->radio_dev &&
  655. h->radio_dev->minor == minor) {
  656. radio = 1;
  657. dev = h;
  658. }
  659. }
  660. if (NULL == dev) {
  661. unlock_kernel();
  662. return -ENODEV;
  663. }
  664. dprintk(1, "open minor=%d radio=%d type=%s\n",
  665. minor, radio, v4l2_type_names[type]);
  666. /* allocate + initialize per filehandle data */
  667. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  668. if (NULL == fh) {
  669. unlock_kernel();
  670. return -ENOMEM;
  671. }
  672. file->private_data = fh;
  673. fh->dev = dev;
  674. fh->radio = radio;
  675. fh->type = type;
  676. fh->width = 320;
  677. fh->height = 240;
  678. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  679. videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
  680. &dev->pci->dev, &dev->slock,
  681. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  682. V4L2_FIELD_INTERLACED,
  683. sizeof(struct cx23885_buffer),
  684. fh);
  685. dprintk(1, "post videobuf_queue_init()\n");
  686. unlock_kernel();
  687. return 0;
  688. }
  689. static ssize_t video_read(struct file *file, char __user *data,
  690. size_t count, loff_t *ppos)
  691. {
  692. struct cx23885_fh *fh = file->private_data;
  693. switch (fh->type) {
  694. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  695. if (res_locked(fh->dev, RESOURCE_VIDEO))
  696. return -EBUSY;
  697. return videobuf_read_one(&fh->vidq, data, count, ppos,
  698. file->f_flags & O_NONBLOCK);
  699. case V4L2_BUF_TYPE_VBI_CAPTURE:
  700. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  701. return -EBUSY;
  702. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  703. file->f_flags & O_NONBLOCK);
  704. default:
  705. BUG();
  706. return 0;
  707. }
  708. }
  709. static unsigned int video_poll(struct file *file,
  710. struct poll_table_struct *wait)
  711. {
  712. struct cx23885_fh *fh = file->private_data;
  713. struct cx23885_buffer *buf;
  714. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  715. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  716. return POLLERR;
  717. return videobuf_poll_stream(file, &fh->vbiq, wait);
  718. }
  719. if (res_check(fh, RESOURCE_VIDEO)) {
  720. /* streaming capture */
  721. if (list_empty(&fh->vidq.stream))
  722. return POLLERR;
  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. return POLLERR;
  730. }
  731. poll_wait(file, &buf->vb.done, wait);
  732. if (buf->vb.state == VIDEOBUF_DONE ||
  733. buf->vb.state == VIDEOBUF_ERROR)
  734. return POLLIN|POLLRDNORM;
  735. return 0;
  736. }
  737. static int video_release(struct file *file)
  738. {
  739. struct cx23885_fh *fh = file->private_data;
  740. struct cx23885_dev *dev = fh->dev;
  741. /* turn off overlay */
  742. if (res_check(fh, RESOURCE_OVERLAY)) {
  743. /* FIXME */
  744. res_free(dev, fh, RESOURCE_OVERLAY);
  745. }
  746. /* stop video capture */
  747. if (res_check(fh, RESOURCE_VIDEO)) {
  748. videobuf_queue_cancel(&fh->vidq);
  749. res_free(dev, fh, RESOURCE_VIDEO);
  750. }
  751. if (fh->vidq.read_buf) {
  752. buffer_release(&fh->vidq, fh->vidq.read_buf);
  753. kfree(fh->vidq.read_buf);
  754. }
  755. /* stop vbi capture */
  756. if (res_check(fh, RESOURCE_VBI)) {
  757. if (fh->vbiq.streaming)
  758. videobuf_streamoff(&fh->vbiq);
  759. if (fh->vbiq.reading)
  760. videobuf_read_stop(&fh->vbiq);
  761. res_free(dev, fh, RESOURCE_VBI);
  762. }
  763. videobuf_mmap_free(&fh->vidq);
  764. file->private_data = NULL;
  765. kfree(fh);
  766. /* We are not putting the tuner to sleep here on exit, because
  767. * we want to use the mpeg encoder in another session to capture
  768. * tuner video. Closing this will result in no video to the encoder.
  769. */
  770. return 0;
  771. }
  772. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  773. {
  774. struct cx23885_fh *fh = file->private_data;
  775. return videobuf_mmap_mapper(get_queue(fh), vma);
  776. }
  777. /* ------------------------------------------------------------------ */
  778. /* VIDEO CTRL IOCTLS */
  779. static int cx23885_get_control(struct cx23885_dev *dev,
  780. struct v4l2_control *ctl)
  781. {
  782. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
  783. call_all(dev, core, g_ctrl, ctl);
  784. return 0;
  785. }
  786. static int cx23885_set_control(struct cx23885_dev *dev,
  787. struct v4l2_control *ctl)
  788. {
  789. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
  790. " (disabled - no action)\n", __func__);
  791. return 0;
  792. }
  793. static void init_controls(struct cx23885_dev *dev)
  794. {
  795. struct v4l2_control ctrl;
  796. int i;
  797. for (i = 0; i < CX23885_CTLS; i++) {
  798. ctrl.id = cx23885_ctls[i].v.id;
  799. ctrl.value = cx23885_ctls[i].v.default_value;
  800. cx23885_set_control(dev, &ctrl);
  801. }
  802. }
  803. /* ------------------------------------------------------------------ */
  804. /* VIDEO IOCTLS */
  805. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  806. struct v4l2_format *f)
  807. {
  808. struct cx23885_fh *fh = priv;
  809. f->fmt.pix.width = fh->width;
  810. f->fmt.pix.height = fh->height;
  811. f->fmt.pix.field = fh->vidq.field;
  812. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  813. f->fmt.pix.bytesperline =
  814. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  815. f->fmt.pix.sizeimage =
  816. f->fmt.pix.height * f->fmt.pix.bytesperline;
  817. return 0;
  818. }
  819. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  820. struct v4l2_format *f)
  821. {
  822. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  823. struct cx23885_fmt *fmt;
  824. enum v4l2_field field;
  825. unsigned int maxw, maxh;
  826. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  827. if (NULL == fmt)
  828. return -EINVAL;
  829. field = f->fmt.pix.field;
  830. maxw = norm_maxw(dev->tvnorm);
  831. maxh = norm_maxh(dev->tvnorm);
  832. if (V4L2_FIELD_ANY == field) {
  833. field = (f->fmt.pix.height > maxh/2)
  834. ? V4L2_FIELD_INTERLACED
  835. : V4L2_FIELD_BOTTOM;
  836. }
  837. switch (field) {
  838. case V4L2_FIELD_TOP:
  839. case V4L2_FIELD_BOTTOM:
  840. maxh = maxh / 2;
  841. break;
  842. case V4L2_FIELD_INTERLACED:
  843. break;
  844. default:
  845. return -EINVAL;
  846. }
  847. f->fmt.pix.field = field;
  848. if (f->fmt.pix.height < 32)
  849. f->fmt.pix.height = 32;
  850. if (f->fmt.pix.height > maxh)
  851. f->fmt.pix.height = maxh;
  852. if (f->fmt.pix.width < 48)
  853. f->fmt.pix.width = 48;
  854. if (f->fmt.pix.width > maxw)
  855. f->fmt.pix.width = maxw;
  856. f->fmt.pix.width &= ~0x03;
  857. f->fmt.pix.bytesperline =
  858. (f->fmt.pix.width * fmt->depth) >> 3;
  859. f->fmt.pix.sizeimage =
  860. f->fmt.pix.height * f->fmt.pix.bytesperline;
  861. return 0;
  862. }
  863. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  864. struct v4l2_format *f)
  865. {
  866. struct cx23885_fh *fh = priv;
  867. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  868. int err;
  869. dprintk(2, "%s()\n", __func__);
  870. err = vidioc_try_fmt_vid_cap(file, priv, f);
  871. if (0 != err)
  872. return err;
  873. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  874. fh->width = f->fmt.pix.width;
  875. fh->height = f->fmt.pix.height;
  876. fh->vidq.field = f->fmt.pix.field;
  877. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  878. fh->width, fh->height, fh->vidq.field);
  879. call_all(dev, video, s_fmt, f);
  880. return 0;
  881. }
  882. static int vidioc_querycap(struct file *file, void *priv,
  883. struct v4l2_capability *cap)
  884. {
  885. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  886. strcpy(cap->driver, "cx23885");
  887. strlcpy(cap->card, cx23885_boards[dev->board].name,
  888. sizeof(cap->card));
  889. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  890. cap->version = CX23885_VERSION_CODE;
  891. cap->capabilities =
  892. V4L2_CAP_VIDEO_CAPTURE |
  893. V4L2_CAP_READWRITE |
  894. V4L2_CAP_STREAMING |
  895. V4L2_CAP_VBI_CAPTURE;
  896. if (UNSET != dev->tuner_type)
  897. cap->capabilities |= V4L2_CAP_TUNER;
  898. return 0;
  899. }
  900. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  901. struct v4l2_fmtdesc *f)
  902. {
  903. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  904. return -EINVAL;
  905. strlcpy(f->description, formats[f->index].name,
  906. sizeof(f->description));
  907. f->pixelformat = formats[f->index].fourcc;
  908. return 0;
  909. }
  910. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  911. static int vidiocgmbuf(struct file *file, void *priv,
  912. struct video_mbuf *mbuf)
  913. {
  914. struct cx23885_fh *fh = priv;
  915. struct videobuf_queue *q;
  916. struct v4l2_requestbuffers req;
  917. unsigned int i;
  918. int err;
  919. q = get_queue(fh);
  920. memset(&req, 0, sizeof(req));
  921. req.type = q->type;
  922. req.count = 8;
  923. req.memory = V4L2_MEMORY_MMAP;
  924. err = videobuf_reqbufs(q, &req);
  925. if (err < 0)
  926. return err;
  927. mbuf->frames = req.count;
  928. mbuf->size = 0;
  929. for (i = 0; i < mbuf->frames; i++) {
  930. mbuf->offsets[i] = q->bufs[i]->boff;
  931. mbuf->size += q->bufs[i]->bsize;
  932. }
  933. return 0;
  934. }
  935. #endif
  936. static int vidioc_reqbufs(struct file *file, void *priv,
  937. struct v4l2_requestbuffers *p)
  938. {
  939. struct cx23885_fh *fh = priv;
  940. return videobuf_reqbufs(get_queue(fh), p);
  941. }
  942. static int vidioc_querybuf(struct file *file, void *priv,
  943. struct v4l2_buffer *p)
  944. {
  945. struct cx23885_fh *fh = priv;
  946. return videobuf_querybuf(get_queue(fh), p);
  947. }
  948. static int vidioc_qbuf(struct file *file, void *priv,
  949. struct v4l2_buffer *p)
  950. {
  951. struct cx23885_fh *fh = priv;
  952. return videobuf_qbuf(get_queue(fh), p);
  953. }
  954. static int vidioc_dqbuf(struct file *file, void *priv,
  955. struct v4l2_buffer *p)
  956. {
  957. struct cx23885_fh *fh = priv;
  958. return videobuf_dqbuf(get_queue(fh), p,
  959. file->f_flags & O_NONBLOCK);
  960. }
  961. static int vidioc_streamon(struct file *file, void *priv,
  962. enum v4l2_buf_type i)
  963. {
  964. struct cx23885_fh *fh = priv;
  965. struct cx23885_dev *dev = fh->dev;
  966. dprintk(1, "%s()\n", __func__);
  967. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  968. return -EINVAL;
  969. if (unlikely(i != fh->type))
  970. return -EINVAL;
  971. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  972. return -EBUSY;
  973. return videobuf_streamon(get_queue(fh));
  974. }
  975. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  976. {
  977. struct cx23885_fh *fh = priv;
  978. struct cx23885_dev *dev = fh->dev;
  979. int err, res;
  980. dprintk(1, "%s()\n", __func__);
  981. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  982. return -EINVAL;
  983. if (i != fh->type)
  984. return -EINVAL;
  985. res = get_resource(fh);
  986. err = videobuf_streamoff(get_queue(fh));
  987. if (err < 0)
  988. return err;
  989. res_free(dev, fh, res);
  990. return 0;
  991. }
  992. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  993. {
  994. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  995. dprintk(1, "%s()\n", __func__);
  996. mutex_lock(&dev->lock);
  997. cx23885_set_tvnorm(dev, *tvnorms);
  998. mutex_unlock(&dev->lock);
  999. return 0;
  1000. }
  1001. static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  1002. {
  1003. static const char *iname[] = {
  1004. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  1005. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  1006. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  1007. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1008. [CX23885_VMUX_SVIDEO] = "S-Video",
  1009. [CX23885_VMUX_TELEVISION] = "Television",
  1010. [CX23885_VMUX_CABLE] = "Cable TV",
  1011. [CX23885_VMUX_DVB] = "DVB",
  1012. [CX23885_VMUX_DEBUG] = "for debug only",
  1013. };
  1014. unsigned int n;
  1015. dprintk(1, "%s()\n", __func__);
  1016. n = i->index;
  1017. if (n >= 4)
  1018. return -EINVAL;
  1019. if (0 == INPUT(n)->type)
  1020. return -EINVAL;
  1021. memset(i, 0, sizeof(*i));
  1022. i->index = n;
  1023. i->type = V4L2_INPUT_TYPE_CAMERA;
  1024. strcpy(i->name, iname[INPUT(n)->type]);
  1025. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1026. (CX23885_VMUX_CABLE == INPUT(n)->type))
  1027. i->type = V4L2_INPUT_TYPE_TUNER;
  1028. i->std = CX23885_NORMS;
  1029. return 0;
  1030. }
  1031. static int vidioc_enum_input(struct file *file, void *priv,
  1032. struct v4l2_input *i)
  1033. {
  1034. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1035. dprintk(1, "%s()\n", __func__);
  1036. return cx23885_enum_input(dev, i);
  1037. }
  1038. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1039. {
  1040. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1041. *i = dev->input;
  1042. dprintk(1, "%s() returns %d\n", __func__, *i);
  1043. return 0;
  1044. }
  1045. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1046. {
  1047. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1048. dprintk(1, "%s(%d)\n", __func__, i);
  1049. if (i >= 4) {
  1050. dprintk(1, "%s() -EINVAL\n", __func__);
  1051. return -EINVAL;
  1052. }
  1053. mutex_lock(&dev->lock);
  1054. cx23885_video_mux(dev, i);
  1055. mutex_unlock(&dev->lock);
  1056. return 0;
  1057. }
  1058. static int vidioc_queryctrl(struct file *file, void *priv,
  1059. struct v4l2_queryctrl *qctrl)
  1060. {
  1061. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1062. if (unlikely(qctrl->id == 0))
  1063. return -EINVAL;
  1064. return cx23885_ctrl_query(qctrl);
  1065. }
  1066. static int vidioc_g_ctrl(struct file *file, void *priv,
  1067. struct v4l2_control *ctl)
  1068. {
  1069. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1070. return cx23885_get_control(dev, ctl);
  1071. }
  1072. static int vidioc_s_ctrl(struct file *file, void *priv,
  1073. struct v4l2_control *ctl)
  1074. {
  1075. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1076. return cx23885_set_control(dev, ctl);
  1077. }
  1078. static int vidioc_g_tuner(struct file *file, void *priv,
  1079. struct v4l2_tuner *t)
  1080. {
  1081. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1082. if (unlikely(UNSET == dev->tuner_type))
  1083. return -EINVAL;
  1084. if (0 != t->index)
  1085. return -EINVAL;
  1086. strcpy(t->name, "Television");
  1087. t->type = V4L2_TUNER_ANALOG_TV;
  1088. t->capability = V4L2_TUNER_CAP_NORM;
  1089. t->rangehigh = 0xffffffffUL;
  1090. t->signal = 0xffff ; /* LOCKED */
  1091. return 0;
  1092. }
  1093. static int vidioc_s_tuner(struct file *file, void *priv,
  1094. struct v4l2_tuner *t)
  1095. {
  1096. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1097. if (UNSET == dev->tuner_type)
  1098. return -EINVAL;
  1099. if (0 != t->index)
  1100. return -EINVAL;
  1101. return 0;
  1102. }
  1103. static int vidioc_g_frequency(struct file *file, void *priv,
  1104. struct v4l2_frequency *f)
  1105. {
  1106. struct cx23885_fh *fh = priv;
  1107. struct cx23885_dev *dev = fh->dev;
  1108. if (unlikely(UNSET == dev->tuner_type))
  1109. return -EINVAL;
  1110. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1111. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1112. f->frequency = dev->freq;
  1113. call_all(dev, tuner, g_frequency, f);
  1114. return 0;
  1115. }
  1116. static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1117. {
  1118. if (unlikely(UNSET == dev->tuner_type))
  1119. return -EINVAL;
  1120. if (unlikely(f->tuner != 0))
  1121. return -EINVAL;
  1122. mutex_lock(&dev->lock);
  1123. dev->freq = f->frequency;
  1124. call_all(dev, tuner, s_frequency, f);
  1125. /* When changing channels it is required to reset TVAUDIO */
  1126. msleep(10);
  1127. mutex_unlock(&dev->lock);
  1128. return 0;
  1129. }
  1130. static int vidioc_s_frequency(struct file *file, void *priv,
  1131. struct v4l2_frequency *f)
  1132. {
  1133. struct cx23885_fh *fh = priv;
  1134. struct cx23885_dev *dev = fh->dev;
  1135. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1136. return -EINVAL;
  1137. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1138. return -EINVAL;
  1139. return
  1140. cx23885_set_freq(dev, f);
  1141. }
  1142. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1143. static int vidioc_g_register(struct file *file, void *fh,
  1144. struct v4l2_dbg_register *reg)
  1145. {
  1146. struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
  1147. if (!v4l2_chip_match_host(&reg->match))
  1148. return -EINVAL;
  1149. call_all(dev, core, g_register, reg);
  1150. return 0;
  1151. }
  1152. static int vidioc_s_register(struct file *file, void *fh,
  1153. struct v4l2_dbg_register *reg)
  1154. {
  1155. struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
  1156. if (!v4l2_chip_match_host(&reg->match))
  1157. return -EINVAL;
  1158. call_all(dev, core, s_register, reg);
  1159. return 0;
  1160. }
  1161. #endif
  1162. /* ----------------------------------------------------------- */
  1163. static void cx23885_vid_timeout(unsigned long data)
  1164. {
  1165. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1166. struct cx23885_dmaqueue *q = &dev->vidq;
  1167. struct cx23885_buffer *buf;
  1168. unsigned long flags;
  1169. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1170. cx_clear(VID_A_DMA_CTL, 0x11);
  1171. spin_lock_irqsave(&dev->slock, flags);
  1172. while (!list_empty(&q->active)) {
  1173. buf = list_entry(q->active.next,
  1174. struct cx23885_buffer, vb.queue);
  1175. list_del(&buf->vb.queue);
  1176. buf->vb.state = VIDEOBUF_ERROR;
  1177. wake_up(&buf->vb.done);
  1178. printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
  1179. dev->name, buf, buf->vb.i,
  1180. (unsigned long)buf->risc.dma);
  1181. }
  1182. cx23885_restart_video_queue(dev, q);
  1183. spin_unlock_irqrestore(&dev->slock, flags);
  1184. }
  1185. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1186. {
  1187. u32 mask, count;
  1188. int handled = 0;
  1189. mask = cx_read(VID_A_INT_MSK);
  1190. if (0 == (status & mask))
  1191. return handled;
  1192. cx_write(VID_A_INT_STAT, status);
  1193. dprintk(2, "%s() status = 0x%08x\n", __func__, status);
  1194. /* risc op code error */
  1195. if (status & (1 << 16)) {
  1196. printk(KERN_WARNING "%s/0: video risc op code error\n",
  1197. dev->name);
  1198. cx_clear(VID_A_DMA_CTL, 0x11);
  1199. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1200. }
  1201. /* risc1 y */
  1202. if (status & 0x01) {
  1203. spin_lock(&dev->slock);
  1204. count = cx_read(VID_A_GPCNT);
  1205. cx23885_video_wakeup(dev, &dev->vidq, count);
  1206. spin_unlock(&dev->slock);
  1207. handled++;
  1208. }
  1209. /* risc2 y */
  1210. if (status & 0x10) {
  1211. dprintk(2, "stopper video\n");
  1212. spin_lock(&dev->slock);
  1213. cx23885_restart_video_queue(dev, &dev->vidq);
  1214. spin_unlock(&dev->slock);
  1215. handled++;
  1216. }
  1217. return handled;
  1218. }
  1219. /* ----------------------------------------------------------- */
  1220. /* exported stuff */
  1221. static const struct v4l2_file_operations video_fops = {
  1222. .owner = THIS_MODULE,
  1223. .open = video_open,
  1224. .release = video_release,
  1225. .read = video_read,
  1226. .poll = video_poll,
  1227. .mmap = video_mmap,
  1228. .ioctl = video_ioctl2,
  1229. };
  1230. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1231. .vidioc_querycap = vidioc_querycap,
  1232. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1233. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1234. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1235. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1236. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  1237. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  1238. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  1239. .vidioc_reqbufs = vidioc_reqbufs,
  1240. .vidioc_querybuf = vidioc_querybuf,
  1241. .vidioc_qbuf = vidioc_qbuf,
  1242. .vidioc_dqbuf = vidioc_dqbuf,
  1243. .vidioc_s_std = vidioc_s_std,
  1244. .vidioc_enum_input = vidioc_enum_input,
  1245. .vidioc_g_input = vidioc_g_input,
  1246. .vidioc_s_input = vidioc_s_input,
  1247. .vidioc_queryctrl = vidioc_queryctrl,
  1248. .vidioc_g_ctrl = vidioc_g_ctrl,
  1249. .vidioc_s_ctrl = vidioc_s_ctrl,
  1250. .vidioc_streamon = vidioc_streamon,
  1251. .vidioc_streamoff = vidioc_streamoff,
  1252. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1253. .vidiocgmbuf = vidiocgmbuf,
  1254. #endif
  1255. .vidioc_g_tuner = vidioc_g_tuner,
  1256. .vidioc_s_tuner = vidioc_s_tuner,
  1257. .vidioc_g_frequency = vidioc_g_frequency,
  1258. .vidioc_s_frequency = vidioc_s_frequency,
  1259. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1260. .vidioc_g_register = vidioc_g_register,
  1261. .vidioc_s_register = vidioc_s_register,
  1262. #endif
  1263. };
  1264. static struct video_device cx23885_vbi_template;
  1265. static struct video_device cx23885_video_template = {
  1266. .name = "cx23885-video",
  1267. .fops = &video_fops,
  1268. .minor = -1,
  1269. .ioctl_ops = &video_ioctl_ops,
  1270. .tvnorms = CX23885_NORMS,
  1271. .current_norm = V4L2_STD_NTSC_M,
  1272. };
  1273. static const struct v4l2_file_operations radio_fops = {
  1274. .owner = THIS_MODULE,
  1275. .open = video_open,
  1276. .release = video_release,
  1277. .ioctl = video_ioctl2,
  1278. };
  1279. void cx23885_video_unregister(struct cx23885_dev *dev)
  1280. {
  1281. dprintk(1, "%s()\n", __func__);
  1282. cx_clear(PCI_INT_MSK, 1);
  1283. if (dev->video_dev) {
  1284. if (-1 != dev->video_dev->minor)
  1285. video_unregister_device(dev->video_dev);
  1286. else
  1287. video_device_release(dev->video_dev);
  1288. dev->video_dev = NULL;
  1289. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1290. }
  1291. }
  1292. int cx23885_video_register(struct cx23885_dev *dev)
  1293. {
  1294. int err;
  1295. dprintk(1, "%s()\n", __func__);
  1296. spin_lock_init(&dev->slock);
  1297. /* Initialize VBI template */
  1298. memcpy(&cx23885_vbi_template, &cx23885_video_template,
  1299. sizeof(cx23885_vbi_template));
  1300. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1301. dev->tvnorm = cx23885_video_template.current_norm;
  1302. /* init video dma queues */
  1303. INIT_LIST_HEAD(&dev->vidq.active);
  1304. INIT_LIST_HEAD(&dev->vidq.queued);
  1305. dev->vidq.timeout.function = cx23885_vid_timeout;
  1306. dev->vidq.timeout.data = (unsigned long)dev;
  1307. init_timer(&dev->vidq.timeout);
  1308. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1309. VID_A_DMA_CTL, 0x11, 0x00);
  1310. /* Don't enable VBI yet */
  1311. cx_set(PCI_INT_MSK, 1);
  1312. if (TUNER_ABSENT != dev->tuner_type) {
  1313. struct v4l2_subdev *sd = NULL;
  1314. if (dev->tuner_addr)
  1315. sd = v4l2_i2c_new_subdev(&dev->i2c_bus[1].i2c_adap,
  1316. "tuner", "tuner", dev->tuner_addr);
  1317. else
  1318. sd = v4l2_i2c_new_probed_subdev(&dev->i2c_bus[1].i2c_adap,
  1319. "tuner", "tuner", v4l2_i2c_tuner_addrs(ADDRS_TV));
  1320. if (sd) {
  1321. struct tuner_setup tun_setup;
  1322. tun_setup.mode_mask = T_ANALOG_TV;
  1323. tun_setup.type = dev->tuner_type;
  1324. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  1325. v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
  1326. }
  1327. }
  1328. /* register v4l devices */
  1329. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1330. &cx23885_video_template, "video");
  1331. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1332. video_nr[dev->nr]);
  1333. if (err < 0) {
  1334. printk(KERN_INFO "%s: can't register video device\n",
  1335. dev->name);
  1336. goto fail_unreg;
  1337. }
  1338. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1339. dev->name, dev->video_dev->num);
  1340. /* initial device configuration */
  1341. mutex_lock(&dev->lock);
  1342. cx23885_set_tvnorm(dev, dev->tvnorm);
  1343. init_controls(dev);
  1344. cx23885_video_mux(dev, 0);
  1345. mutex_unlock(&dev->lock);
  1346. return 0;
  1347. fail_unreg:
  1348. cx23885_video_unregister(dev);
  1349. return err;
  1350. }