cx23885-video.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  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 "cx23885-video.h"
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include "cx23885-ioctl.h"
  37. #include "tuner-xc2028.h"
  38. #include <media/cx25840.h>
  39. MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
  40. MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  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: " fmt, dev->name, ## arg);\
  64. } while (0)
  65. /* ------------------------------------------------------------------- */
  66. /* static data */
  67. #define FORMAT_FLAGS_PACKED 0x01
  68. #if 0
  69. static struct cx23885_fmt formats[] = {
  70. {
  71. .name = "8 bpp, gray",
  72. .fourcc = V4L2_PIX_FMT_GREY,
  73. .depth = 8,
  74. .flags = FORMAT_FLAGS_PACKED,
  75. }, {
  76. .name = "15 bpp RGB, le",
  77. .fourcc = V4L2_PIX_FMT_RGB555,
  78. .depth = 16,
  79. .flags = FORMAT_FLAGS_PACKED,
  80. }, {
  81. .name = "15 bpp RGB, be",
  82. .fourcc = V4L2_PIX_FMT_RGB555X,
  83. .depth = 16,
  84. .flags = FORMAT_FLAGS_PACKED,
  85. }, {
  86. .name = "16 bpp RGB, le",
  87. .fourcc = V4L2_PIX_FMT_RGB565,
  88. .depth = 16,
  89. .flags = FORMAT_FLAGS_PACKED,
  90. }, {
  91. .name = "16 bpp RGB, be",
  92. .fourcc = V4L2_PIX_FMT_RGB565X,
  93. .depth = 16,
  94. .flags = FORMAT_FLAGS_PACKED,
  95. }, {
  96. .name = "24 bpp RGB, le",
  97. .fourcc = V4L2_PIX_FMT_BGR24,
  98. .depth = 24,
  99. .flags = FORMAT_FLAGS_PACKED,
  100. }, {
  101. .name = "32 bpp RGB, le",
  102. .fourcc = V4L2_PIX_FMT_BGR32,
  103. .depth = 32,
  104. .flags = FORMAT_FLAGS_PACKED,
  105. }, {
  106. .name = "32 bpp RGB, be",
  107. .fourcc = V4L2_PIX_FMT_RGB32,
  108. .depth = 32,
  109. .flags = FORMAT_FLAGS_PACKED,
  110. }, {
  111. .name = "4:2:2, packed, YUYV",
  112. .fourcc = V4L2_PIX_FMT_YUYV,
  113. .depth = 16,
  114. .flags = FORMAT_FLAGS_PACKED,
  115. }, {
  116. .name = "4:2:2, packed, UYVY",
  117. .fourcc = V4L2_PIX_FMT_UYVY,
  118. .depth = 16,
  119. .flags = FORMAT_FLAGS_PACKED,
  120. },
  121. };
  122. #else
  123. static struct cx23885_fmt formats[] = {
  124. {
  125. #if 0
  126. .name = "4:2:2, packed, UYVY",
  127. .fourcc = V4L2_PIX_FMT_UYVY,
  128. .depth = 16,
  129. .flags = FORMAT_FLAGS_PACKED,
  130. }, {
  131. #endif
  132. .name = "4:2:2, packed, YUYV",
  133. .fourcc = V4L2_PIX_FMT_YUYV,
  134. .depth = 16,
  135. .flags = FORMAT_FLAGS_PACKED,
  136. }
  137. };
  138. #endif
  139. static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
  140. {
  141. unsigned int i;
  142. for (i = 0; i < ARRAY_SIZE(formats); i++)
  143. if (formats[i].fourcc == fourcc)
  144. return formats+i;
  145. printk(KERN_ERR "%s(%c%c%c%c) NOT FOUND\n", __func__,
  146. (fourcc & 0xff),
  147. ((fourcc >> 8) & 0xff),
  148. ((fourcc >> 16) & 0xff),
  149. ((fourcc >> 24) & 0xff)
  150. );
  151. return NULL;
  152. }
  153. /* ------------------------------------------------------------------- */
  154. static const struct v4l2_queryctrl no_ctl = {
  155. .name = "42",
  156. .flags = V4L2_CTRL_FLAG_DISABLED,
  157. };
  158. static struct cx23885_ctrl cx23885_ctls[] = {
  159. /* --- video --- */
  160. {
  161. .v = {
  162. .id = V4L2_CID_BRIGHTNESS,
  163. .name = "Brightness",
  164. .minimum = 0x00,
  165. .maximum = 0xff,
  166. .step = 1,
  167. .default_value = 0x7f,
  168. .type = V4L2_CTRL_TYPE_INTEGER,
  169. },
  170. .off = 128,
  171. .reg = LUMA_CTRL,
  172. .mask = 0x00ff,
  173. .shift = 0,
  174. }, {
  175. .v = {
  176. .id = V4L2_CID_CONTRAST,
  177. .name = "Contrast",
  178. .minimum = 0,
  179. .maximum = 0x7f,
  180. .step = 1,
  181. .default_value = 0x3f,
  182. .type = V4L2_CTRL_TYPE_INTEGER,
  183. },
  184. .off = 0,
  185. .reg = LUMA_CTRL,
  186. .mask = 0xff00,
  187. .shift = 8,
  188. }, {
  189. .v = {
  190. .id = V4L2_CID_HUE,
  191. .name = "Hue",
  192. .minimum = -127,
  193. .maximum = 128,
  194. .step = 1,
  195. .default_value = 0x0,
  196. .type = V4L2_CTRL_TYPE_INTEGER,
  197. },
  198. .off = 128,
  199. .reg = CHROMA_CTRL,
  200. .mask = 0xff0000,
  201. .shift = 16,
  202. }, {
  203. /* strictly, this only describes only U saturation.
  204. * V saturation is handled specially through code.
  205. */
  206. .v = {
  207. .id = V4L2_CID_SATURATION,
  208. .name = "Saturation",
  209. .minimum = 0,
  210. .maximum = 0x7f,
  211. .step = 1,
  212. .default_value = 0x3f,
  213. .type = V4L2_CTRL_TYPE_INTEGER,
  214. },
  215. .off = 0,
  216. .reg = CHROMA_CTRL,
  217. .mask = 0x00ff,
  218. .shift = 0,
  219. }, {
  220. /* --- audio --- */
  221. .v = {
  222. .id = V4L2_CID_AUDIO_MUTE,
  223. .name = "Mute",
  224. .minimum = 0,
  225. .maximum = 1,
  226. .default_value = 1,
  227. .type = V4L2_CTRL_TYPE_BOOLEAN,
  228. },
  229. .reg = PATH1_CTL1,
  230. .mask = (0x1f << 24),
  231. .shift = 24,
  232. }, {
  233. .v = {
  234. .id = V4L2_CID_AUDIO_VOLUME,
  235. .name = "Volume",
  236. .minimum = 0,
  237. .maximum = 65535,
  238. .step = 65535 / 100,
  239. .default_value = 65535,
  240. .type = V4L2_CTRL_TYPE_INTEGER,
  241. },
  242. .reg = PATH1_VOL_CTL,
  243. .mask = 0xff,
  244. .shift = 0,
  245. }
  246. };
  247. static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
  248. /* Must be sorted from low to high control ID! */
  249. static const u32 cx23885_user_ctrls[] = {
  250. V4L2_CID_USER_CLASS,
  251. V4L2_CID_BRIGHTNESS,
  252. V4L2_CID_CONTRAST,
  253. V4L2_CID_SATURATION,
  254. V4L2_CID_HUE,
  255. V4L2_CID_AUDIO_VOLUME,
  256. V4L2_CID_AUDIO_MUTE,
  257. 0
  258. };
  259. static const u32 *ctrl_classes[] = {
  260. cx23885_user_ctrls,
  261. NULL
  262. };
  263. void cx23885_video_wakeup(struct cx23885_dev *dev,
  264. struct cx23885_dmaqueue *q, u32 count)
  265. {
  266. struct cx23885_buffer *buf;
  267. int bc;
  268. for (bc = 0;; bc++) {
  269. if (list_empty(&q->active))
  270. break;
  271. buf = list_entry(q->active.next,
  272. struct cx23885_buffer, vb.queue);
  273. /* count comes from the hw and is is 16bit wide --
  274. * this trick handles wrap-arounds correctly for
  275. * up to 32767 buffers in flight... */
  276. if ((s16) (count - buf->count) < 0)
  277. break;
  278. v4l2_get_timestamp(&buf->vb.ts);
  279. dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
  280. count, buf->count);
  281. buf->vb.state = VIDEOBUF_DONE;
  282. list_del(&buf->vb.queue);
  283. wake_up(&buf->vb.done);
  284. }
  285. if (list_empty(&q->active))
  286. del_timer(&q->timeout);
  287. else
  288. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  289. if (bc != 1)
  290. printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
  291. __func__, bc);
  292. }
  293. int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
  294. {
  295. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  296. __func__,
  297. (unsigned int)norm,
  298. v4l2_norm_to_name(norm));
  299. dev->tvnorm = norm;
  300. call_all(dev, core, s_std, norm);
  301. return 0;
  302. }
  303. static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
  304. struct pci_dev *pci,
  305. struct video_device *template,
  306. char *type)
  307. {
  308. struct video_device *vfd;
  309. dprintk(1, "%s()\n", __func__);
  310. vfd = video_device_alloc();
  311. if (NULL == vfd)
  312. return NULL;
  313. *vfd = *template;
  314. vfd->v4l2_dev = &dev->v4l2_dev;
  315. vfd->release = video_device_release;
  316. snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
  317. cx23885_boards[dev->board].name, type);
  318. video_set_drvdata(vfd, dev);
  319. return vfd;
  320. }
  321. static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
  322. {
  323. int i;
  324. if (qctrl->id < V4L2_CID_BASE ||
  325. qctrl->id >= V4L2_CID_LASTP1)
  326. return -EINVAL;
  327. for (i = 0; i < CX23885_CTLS; i++)
  328. if (cx23885_ctls[i].v.id == qctrl->id)
  329. break;
  330. if (i == CX23885_CTLS) {
  331. *qctrl = no_ctl;
  332. return 0;
  333. }
  334. *qctrl = cx23885_ctls[i].v;
  335. return 0;
  336. }
  337. /* ------------------------------------------------------------------- */
  338. /* resource management */
  339. static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
  340. unsigned int bit)
  341. {
  342. dprintk(1, "%s()\n", __func__);
  343. if (fh->resources & bit)
  344. /* have it already allocated */
  345. return 1;
  346. /* is it free? */
  347. mutex_lock(&dev->lock);
  348. if (dev->resources & bit) {
  349. /* no, someone else uses it */
  350. mutex_unlock(&dev->lock);
  351. return 0;
  352. }
  353. /* it's free, grab it */
  354. fh->resources |= bit;
  355. dev->resources |= bit;
  356. dprintk(1, "res: get %d\n", bit);
  357. mutex_unlock(&dev->lock);
  358. return 1;
  359. }
  360. static int res_check(struct cx23885_fh *fh, unsigned int bit)
  361. {
  362. return fh->resources & bit;
  363. }
  364. static int res_locked(struct cx23885_dev *dev, unsigned int bit)
  365. {
  366. return dev->resources & bit;
  367. }
  368. static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
  369. unsigned int bits)
  370. {
  371. BUG_ON((fh->resources & bits) != bits);
  372. dprintk(1, "%s()\n", __func__);
  373. mutex_lock(&dev->lock);
  374. fh->resources &= ~bits;
  375. dev->resources &= ~bits;
  376. dprintk(1, "res: put %d\n", bits);
  377. mutex_unlock(&dev->lock);
  378. }
  379. int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
  380. {
  381. /* 8 bit registers, 8 bit values */
  382. u8 buf[] = { reg, data };
  383. struct i2c_msg msg = { .addr = 0x98 >> 1,
  384. .flags = 0, .buf = buf, .len = 2 };
  385. return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
  386. }
  387. u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
  388. {
  389. /* 8 bit registers, 8 bit values */
  390. int ret;
  391. u8 b0[] = { reg };
  392. u8 b1[] = { 0 };
  393. struct i2c_msg msg[] = {
  394. { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
  395. { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
  396. };
  397. ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
  398. if (ret != 2)
  399. printk(KERN_ERR "%s() error\n", __func__);
  400. return b1[0];
  401. }
  402. static void cx23885_flatiron_dump(struct cx23885_dev *dev)
  403. {
  404. int i;
  405. dprintk(1, "Flatiron dump\n");
  406. for (i = 0; i < 0x24; i++) {
  407. dprintk(1, "FI[%02x] = %02x\n", i,
  408. cx23885_flatiron_read(dev, i));
  409. }
  410. }
  411. static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
  412. {
  413. u8 val;
  414. dprintk(1, "%s(input = %d)\n", __func__, input);
  415. if (input == 1)
  416. val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
  417. else if (input == 2)
  418. val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
  419. else
  420. return -EINVAL;
  421. val |= 0x20; /* Enable clock to delta-sigma and dec filter */
  422. cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
  423. /* Wake up */
  424. cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
  425. if (video_debug)
  426. cx23885_flatiron_dump(dev);
  427. return 0;
  428. }
  429. static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
  430. {
  431. dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  432. __func__,
  433. input, INPUT(input)->vmux,
  434. INPUT(input)->gpio0, INPUT(input)->gpio1,
  435. INPUT(input)->gpio2, INPUT(input)->gpio3);
  436. dev->input = input;
  437. if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
  438. dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
  439. dev->board == CX23885_BOARD_MYGICA_X8507) {
  440. /* Select Analog TV */
  441. if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
  442. cx23885_gpio_clear(dev, GPIO_0);
  443. }
  444. /* Tell the internal A/V decoder */
  445. v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
  446. INPUT(input)->vmux, 0, 0);
  447. if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
  448. (dev->board == CX23885_BOARD_MPX885) ||
  449. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
  450. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
  451. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
  452. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
  453. (dev->board == CX23885_BOARD_MYGICA_X8507) ||
  454. (dev->board == CX23885_BOARD_AVERMEDIA_HC81R)) {
  455. /* Configure audio routing */
  456. v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
  457. INPUT(input)->amux, 0, 0);
  458. if (INPUT(input)->amux == CX25840_AUDIO7)
  459. cx23885_flatiron_mux(dev, 1);
  460. else if (INPUT(input)->amux == CX25840_AUDIO6)
  461. cx23885_flatiron_mux(dev, 2);
  462. }
  463. return 0;
  464. }
  465. static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
  466. {
  467. dprintk(1, "%s(input=%d)\n", __func__, input);
  468. /* The baseband video core of the cx23885 has two audio inputs.
  469. * LR1 and LR2. In almost every single case so far only HVR1xxx
  470. * cards we've only ever supported LR1. Time to support LR2,
  471. * which is available via the optional white breakout header on
  472. * the board.
  473. * We'll use a could of existing enums in the card struct to allow
  474. * devs to specify which baseband input they need, or just default
  475. * to what we've always used.
  476. */
  477. if (INPUT(input)->amux == CX25840_AUDIO7)
  478. cx23885_flatiron_mux(dev, 1);
  479. else if (INPUT(input)->amux == CX25840_AUDIO6)
  480. cx23885_flatiron_mux(dev, 2);
  481. else {
  482. /* Not specifically defined, assume the default. */
  483. cx23885_flatiron_mux(dev, 1);
  484. }
  485. return 0;
  486. }
  487. /* ------------------------------------------------------------------ */
  488. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  489. struct cx23885_dmaqueue *q,
  490. struct cx23885_buffer *buf)
  491. {
  492. dprintk(1, "%s()\n", __func__);
  493. /* Stop the dma/fifo before we tamper with it's risc programs */
  494. cx_clear(VID_A_DMA_CTL, 0x11);
  495. /* setup fifo + format */
  496. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  497. buf->bpl, buf->risc.dma);
  498. /* reset counter */
  499. cx_write(VID_A_GPCNT_CTL, 3);
  500. q->count = 1;
  501. /* enable irq */
  502. cx23885_irq_add_enable(dev, 0x01);
  503. cx_set(VID_A_INT_MSK, 0x000011);
  504. /* start dma */
  505. cx_set(DEV_CNTRL2, (1<<5));
  506. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  507. return 0;
  508. }
  509. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  510. struct cx23885_dmaqueue *q)
  511. {
  512. struct cx23885_buffer *buf, *prev;
  513. struct list_head *item;
  514. dprintk(1, "%s()\n", __func__);
  515. if (!list_empty(&q->active)) {
  516. buf = list_entry(q->active.next, struct cx23885_buffer,
  517. vb.queue);
  518. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  519. buf, buf->vb.i);
  520. cx23885_start_video_dma(dev, q, buf);
  521. list_for_each(item, &q->active) {
  522. buf = list_entry(item, struct cx23885_buffer,
  523. vb.queue);
  524. buf->count = q->count++;
  525. }
  526. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  527. return 0;
  528. }
  529. prev = NULL;
  530. for (;;) {
  531. if (list_empty(&q->queued))
  532. return 0;
  533. buf = list_entry(q->queued.next, struct cx23885_buffer,
  534. vb.queue);
  535. if (NULL == prev) {
  536. list_move_tail(&buf->vb.queue, &q->active);
  537. cx23885_start_video_dma(dev, q, buf);
  538. buf->vb.state = VIDEOBUF_ACTIVE;
  539. buf->count = q->count++;
  540. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  541. dprintk(2, "[%p/%d] restart_queue - first active\n",
  542. buf, buf->vb.i);
  543. } else if (prev->vb.width == buf->vb.width &&
  544. prev->vb.height == buf->vb.height &&
  545. prev->fmt == buf->fmt) {
  546. list_move_tail(&buf->vb.queue, &q->active);
  547. buf->vb.state = VIDEOBUF_ACTIVE;
  548. buf->count = q->count++;
  549. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  550. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  551. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  552. buf, buf->vb.i);
  553. } else {
  554. return 0;
  555. }
  556. prev = buf;
  557. }
  558. }
  559. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  560. unsigned int *size)
  561. {
  562. struct cx23885_fh *fh = q->priv_data;
  563. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  564. if (0 == *count)
  565. *count = 32;
  566. if (*size * *count > vid_limit * 1024 * 1024)
  567. *count = (vid_limit * 1024 * 1024) / *size;
  568. return 0;
  569. }
  570. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  571. enum v4l2_field field)
  572. {
  573. struct cx23885_fh *fh = q->priv_data;
  574. struct cx23885_dev *dev = fh->dev;
  575. struct cx23885_buffer *buf =
  576. container_of(vb, struct cx23885_buffer, vb);
  577. int rc, init_buffer = 0;
  578. u32 line0_offset, line1_offset;
  579. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  580. int field_tff;
  581. BUG_ON(NULL == fh->fmt);
  582. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  583. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  584. return -EINVAL;
  585. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  586. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  587. return -EINVAL;
  588. if (buf->fmt != fh->fmt ||
  589. buf->vb.width != fh->width ||
  590. buf->vb.height != fh->height ||
  591. buf->vb.field != field) {
  592. buf->fmt = fh->fmt;
  593. buf->vb.width = fh->width;
  594. buf->vb.height = fh->height;
  595. buf->vb.field = field;
  596. init_buffer = 1;
  597. }
  598. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  599. init_buffer = 1;
  600. rc = videobuf_iolock(q, &buf->vb, NULL);
  601. if (0 != rc)
  602. goto fail;
  603. }
  604. if (init_buffer) {
  605. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  606. switch (buf->vb.field) {
  607. case V4L2_FIELD_TOP:
  608. cx23885_risc_buffer(dev->pci, &buf->risc,
  609. dma->sglist, 0, UNSET,
  610. buf->bpl, 0, buf->vb.height);
  611. break;
  612. case V4L2_FIELD_BOTTOM:
  613. cx23885_risc_buffer(dev->pci, &buf->risc,
  614. dma->sglist, UNSET, 0,
  615. buf->bpl, 0, buf->vb.height);
  616. break;
  617. case V4L2_FIELD_INTERLACED:
  618. if (dev->tvnorm & V4L2_STD_NTSC)
  619. /* NTSC or */
  620. field_tff = 1;
  621. else
  622. field_tff = 0;
  623. if (cx23885_boards[dev->board].force_bff)
  624. /* PAL / SECAM OR 888 in NTSC MODE */
  625. field_tff = 0;
  626. if (field_tff) {
  627. /* cx25840 transmits NTSC bottom field first */
  628. dprintk(1, "%s() Creating TFF/NTSC risc\n",
  629. __func__);
  630. line0_offset = buf->bpl;
  631. line1_offset = 0;
  632. } else {
  633. /* All other formats are top field first */
  634. dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
  635. __func__);
  636. line0_offset = 0;
  637. line1_offset = buf->bpl;
  638. }
  639. cx23885_risc_buffer(dev->pci, &buf->risc,
  640. dma->sglist, line0_offset,
  641. line1_offset,
  642. buf->bpl, buf->bpl,
  643. buf->vb.height >> 1);
  644. break;
  645. case V4L2_FIELD_SEQ_TB:
  646. cx23885_risc_buffer(dev->pci, &buf->risc,
  647. dma->sglist,
  648. 0, buf->bpl * (buf->vb.height >> 1),
  649. buf->bpl, 0,
  650. buf->vb.height >> 1);
  651. break;
  652. case V4L2_FIELD_SEQ_BT:
  653. cx23885_risc_buffer(dev->pci, &buf->risc,
  654. dma->sglist,
  655. buf->bpl * (buf->vb.height >> 1), 0,
  656. buf->bpl, 0,
  657. buf->vb.height >> 1);
  658. break;
  659. default:
  660. BUG();
  661. }
  662. }
  663. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  664. buf, buf->vb.i,
  665. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  666. (unsigned long)buf->risc.dma);
  667. buf->vb.state = VIDEOBUF_PREPARED;
  668. return 0;
  669. fail:
  670. cx23885_free_buffer(q, buf);
  671. return rc;
  672. }
  673. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  674. {
  675. struct cx23885_buffer *buf = container_of(vb,
  676. struct cx23885_buffer, vb);
  677. struct cx23885_buffer *prev;
  678. struct cx23885_fh *fh = vq->priv_data;
  679. struct cx23885_dev *dev = fh->dev;
  680. struct cx23885_dmaqueue *q = &dev->vidq;
  681. /* add jump to stopper */
  682. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  683. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  684. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  685. if (!list_empty(&q->queued)) {
  686. list_add_tail(&buf->vb.queue, &q->queued);
  687. buf->vb.state = VIDEOBUF_QUEUED;
  688. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  689. buf, buf->vb.i);
  690. } else if (list_empty(&q->active)) {
  691. list_add_tail(&buf->vb.queue, &q->active);
  692. cx23885_start_video_dma(dev, q, buf);
  693. buf->vb.state = VIDEOBUF_ACTIVE;
  694. buf->count = q->count++;
  695. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  696. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  697. buf, buf->vb.i);
  698. } else {
  699. prev = list_entry(q->active.prev, struct cx23885_buffer,
  700. vb.queue);
  701. if (prev->vb.width == buf->vb.width &&
  702. prev->vb.height == buf->vb.height &&
  703. prev->fmt == buf->fmt) {
  704. list_add_tail(&buf->vb.queue, &q->active);
  705. buf->vb.state = VIDEOBUF_ACTIVE;
  706. buf->count = q->count++;
  707. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  708. /* 64 bit bits 63-32 */
  709. prev->risc.jmp[2] = cpu_to_le32(0);
  710. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  711. buf, buf->vb.i);
  712. } else {
  713. list_add_tail(&buf->vb.queue, &q->queued);
  714. buf->vb.state = VIDEOBUF_QUEUED;
  715. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  716. buf, buf->vb.i);
  717. }
  718. }
  719. }
  720. static void buffer_release(struct videobuf_queue *q,
  721. struct videobuf_buffer *vb)
  722. {
  723. struct cx23885_buffer *buf = container_of(vb,
  724. struct cx23885_buffer, vb);
  725. cx23885_free_buffer(q, buf);
  726. }
  727. static struct videobuf_queue_ops cx23885_video_qops = {
  728. .buf_setup = buffer_setup,
  729. .buf_prepare = buffer_prepare,
  730. .buf_queue = buffer_queue,
  731. .buf_release = buffer_release,
  732. };
  733. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  734. {
  735. switch (fh->type) {
  736. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  737. return &fh->vidq;
  738. case V4L2_BUF_TYPE_VBI_CAPTURE:
  739. return &fh->vbiq;
  740. default:
  741. BUG();
  742. return NULL;
  743. }
  744. }
  745. static int get_resource(struct cx23885_fh *fh)
  746. {
  747. switch (fh->type) {
  748. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  749. return RESOURCE_VIDEO;
  750. case V4L2_BUF_TYPE_VBI_CAPTURE:
  751. return RESOURCE_VBI;
  752. default:
  753. BUG();
  754. return 0;
  755. }
  756. }
  757. static int video_open(struct file *file)
  758. {
  759. struct video_device *vdev = video_devdata(file);
  760. struct cx23885_dev *dev = video_drvdata(file);
  761. struct cx23885_fh *fh;
  762. enum v4l2_buf_type type = 0;
  763. int radio = 0;
  764. switch (vdev->vfl_type) {
  765. case VFL_TYPE_GRABBER:
  766. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  767. break;
  768. case VFL_TYPE_VBI:
  769. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  770. break;
  771. case VFL_TYPE_RADIO:
  772. radio = 1;
  773. break;
  774. }
  775. dprintk(1, "open dev=%s radio=%d type=%s\n",
  776. video_device_node_name(vdev), radio, v4l2_type_names[type]);
  777. /* allocate + initialize per filehandle data */
  778. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  779. if (NULL == fh)
  780. return -ENOMEM;
  781. file->private_data = fh;
  782. fh->dev = dev;
  783. fh->radio = radio;
  784. fh->type = type;
  785. fh->width = 320;
  786. fh->height = 240;
  787. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
  788. videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
  789. &dev->pci->dev, &dev->slock,
  790. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  791. V4L2_FIELD_INTERLACED,
  792. sizeof(struct cx23885_buffer),
  793. fh, NULL);
  794. videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
  795. &dev->pci->dev, &dev->slock,
  796. V4L2_BUF_TYPE_VBI_CAPTURE,
  797. V4L2_FIELD_SEQ_TB,
  798. sizeof(struct cx23885_buffer),
  799. fh, NULL);
  800. dprintk(1, "post videobuf_queue_init()\n");
  801. return 0;
  802. }
  803. static ssize_t video_read(struct file *file, char __user *data,
  804. size_t count, loff_t *ppos)
  805. {
  806. struct cx23885_fh *fh = file->private_data;
  807. switch (fh->type) {
  808. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  809. if (res_locked(fh->dev, RESOURCE_VIDEO))
  810. return -EBUSY;
  811. return videobuf_read_one(&fh->vidq, data, count, ppos,
  812. file->f_flags & O_NONBLOCK);
  813. case V4L2_BUF_TYPE_VBI_CAPTURE:
  814. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  815. return -EBUSY;
  816. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  817. file->f_flags & O_NONBLOCK);
  818. default:
  819. BUG();
  820. return 0;
  821. }
  822. }
  823. static unsigned int video_poll(struct file *file,
  824. struct poll_table_struct *wait)
  825. {
  826. struct cx23885_fh *fh = file->private_data;
  827. struct cx23885_buffer *buf;
  828. unsigned int rc = POLLERR;
  829. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  830. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  831. return POLLERR;
  832. return videobuf_poll_stream(file, &fh->vbiq, wait);
  833. }
  834. mutex_lock(&fh->vidq.vb_lock);
  835. if (res_check(fh, RESOURCE_VIDEO)) {
  836. /* streaming capture */
  837. if (list_empty(&fh->vidq.stream))
  838. goto done;
  839. buf = list_entry(fh->vidq.stream.next,
  840. struct cx23885_buffer, vb.stream);
  841. } else {
  842. /* read() capture */
  843. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  844. if (NULL == buf)
  845. goto done;
  846. }
  847. poll_wait(file, &buf->vb.done, wait);
  848. if (buf->vb.state == VIDEOBUF_DONE ||
  849. buf->vb.state == VIDEOBUF_ERROR)
  850. rc = POLLIN|POLLRDNORM;
  851. else
  852. rc = 0;
  853. done:
  854. mutex_unlock(&fh->vidq.vb_lock);
  855. return rc;
  856. }
  857. static int video_release(struct file *file)
  858. {
  859. struct cx23885_fh *fh = file->private_data;
  860. struct cx23885_dev *dev = fh->dev;
  861. /* turn off overlay */
  862. if (res_check(fh, RESOURCE_OVERLAY)) {
  863. /* FIXME */
  864. res_free(dev, fh, RESOURCE_OVERLAY);
  865. }
  866. /* stop video capture */
  867. if (res_check(fh, RESOURCE_VIDEO)) {
  868. videobuf_queue_cancel(&fh->vidq);
  869. res_free(dev, fh, RESOURCE_VIDEO);
  870. }
  871. if (fh->vidq.read_buf) {
  872. buffer_release(&fh->vidq, fh->vidq.read_buf);
  873. kfree(fh->vidq.read_buf);
  874. }
  875. /* stop vbi capture */
  876. if (res_check(fh, RESOURCE_VBI)) {
  877. if (fh->vbiq.streaming)
  878. videobuf_streamoff(&fh->vbiq);
  879. if (fh->vbiq.reading)
  880. videobuf_read_stop(&fh->vbiq);
  881. res_free(dev, fh, RESOURCE_VBI);
  882. }
  883. videobuf_mmap_free(&fh->vidq);
  884. videobuf_mmap_free(&fh->vbiq);
  885. file->private_data = NULL;
  886. kfree(fh);
  887. /* We are not putting the tuner to sleep here on exit, because
  888. * we want to use the mpeg encoder in another session to capture
  889. * tuner video. Closing this will result in no video to the encoder.
  890. */
  891. return 0;
  892. }
  893. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  894. {
  895. struct cx23885_fh *fh = file->private_data;
  896. return videobuf_mmap_mapper(get_queue(fh), vma);
  897. }
  898. /* ------------------------------------------------------------------ */
  899. /* VIDEO CTRL IOCTLS */
  900. int cx23885_get_control(struct cx23885_dev *dev,
  901. struct v4l2_control *ctl)
  902. {
  903. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
  904. call_all(dev, core, g_ctrl, ctl);
  905. return 0;
  906. }
  907. int cx23885_set_control(struct cx23885_dev *dev,
  908. struct v4l2_control *ctl)
  909. {
  910. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
  911. call_all(dev, core, s_ctrl, ctl);
  912. return 0;
  913. }
  914. static void init_controls(struct cx23885_dev *dev)
  915. {
  916. struct v4l2_control ctrl;
  917. int i;
  918. for (i = 0; i < CX23885_CTLS; i++) {
  919. ctrl.id = cx23885_ctls[i].v.id;
  920. ctrl.value = cx23885_ctls[i].v.default_value;
  921. cx23885_set_control(dev, &ctrl);
  922. }
  923. }
  924. /* ------------------------------------------------------------------ */
  925. /* VIDEO IOCTLS */
  926. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  927. struct v4l2_format *f)
  928. {
  929. struct cx23885_fh *fh = priv;
  930. f->fmt.pix.width = fh->width;
  931. f->fmt.pix.height = fh->height;
  932. f->fmt.pix.field = fh->vidq.field;
  933. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  934. f->fmt.pix.bytesperline =
  935. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  936. f->fmt.pix.sizeimage =
  937. f->fmt.pix.height * f->fmt.pix.bytesperline;
  938. return 0;
  939. }
  940. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  941. struct v4l2_format *f)
  942. {
  943. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  944. struct cx23885_fmt *fmt;
  945. enum v4l2_field field;
  946. unsigned int maxw, maxh;
  947. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  948. if (NULL == fmt)
  949. return -EINVAL;
  950. field = f->fmt.pix.field;
  951. maxw = norm_maxw(dev->tvnorm);
  952. maxh = norm_maxh(dev->tvnorm);
  953. if (V4L2_FIELD_ANY == field) {
  954. field = (f->fmt.pix.height > maxh/2)
  955. ? V4L2_FIELD_INTERLACED
  956. : V4L2_FIELD_BOTTOM;
  957. }
  958. switch (field) {
  959. case V4L2_FIELD_TOP:
  960. case V4L2_FIELD_BOTTOM:
  961. maxh = maxh / 2;
  962. break;
  963. case V4L2_FIELD_INTERLACED:
  964. break;
  965. default:
  966. return -EINVAL;
  967. }
  968. f->fmt.pix.field = field;
  969. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  970. &f->fmt.pix.height, 32, maxh, 0, 0);
  971. f->fmt.pix.bytesperline =
  972. (f->fmt.pix.width * fmt->depth) >> 3;
  973. f->fmt.pix.sizeimage =
  974. f->fmt.pix.height * f->fmt.pix.bytesperline;
  975. return 0;
  976. }
  977. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  978. struct v4l2_format *f)
  979. {
  980. struct cx23885_fh *fh = priv;
  981. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  982. struct v4l2_mbus_framefmt mbus_fmt;
  983. int err;
  984. dprintk(2, "%s()\n", __func__);
  985. err = vidioc_try_fmt_vid_cap(file, priv, f);
  986. if (0 != err)
  987. return err;
  988. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  989. fh->width = f->fmt.pix.width;
  990. fh->height = f->fmt.pix.height;
  991. fh->vidq.field = f->fmt.pix.field;
  992. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  993. fh->width, fh->height, fh->vidq.field);
  994. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  995. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  996. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  997. return 0;
  998. }
  999. static int vidioc_querycap(struct file *file, void *priv,
  1000. struct v4l2_capability *cap)
  1001. {
  1002. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1003. strcpy(cap->driver, "cx23885");
  1004. strlcpy(cap->card, cx23885_boards[dev->board].name,
  1005. sizeof(cap->card));
  1006. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  1007. cap->capabilities =
  1008. V4L2_CAP_VIDEO_CAPTURE |
  1009. V4L2_CAP_READWRITE |
  1010. V4L2_CAP_STREAMING |
  1011. V4L2_CAP_VBI_CAPTURE;
  1012. if (UNSET != dev->tuner_type)
  1013. cap->capabilities |= V4L2_CAP_TUNER;
  1014. return 0;
  1015. }
  1016. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1017. struct v4l2_fmtdesc *f)
  1018. {
  1019. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  1020. return -EINVAL;
  1021. strlcpy(f->description, formats[f->index].name,
  1022. sizeof(f->description));
  1023. f->pixelformat = formats[f->index].fourcc;
  1024. return 0;
  1025. }
  1026. static int vidioc_reqbufs(struct file *file, void *priv,
  1027. struct v4l2_requestbuffers *p)
  1028. {
  1029. struct cx23885_fh *fh = priv;
  1030. return videobuf_reqbufs(get_queue(fh), p);
  1031. }
  1032. static int vidioc_querybuf(struct file *file, void *priv,
  1033. struct v4l2_buffer *p)
  1034. {
  1035. struct cx23885_fh *fh = priv;
  1036. return videobuf_querybuf(get_queue(fh), p);
  1037. }
  1038. static int vidioc_qbuf(struct file *file, void *priv,
  1039. struct v4l2_buffer *p)
  1040. {
  1041. struct cx23885_fh *fh = priv;
  1042. return videobuf_qbuf(get_queue(fh), p);
  1043. }
  1044. static int vidioc_dqbuf(struct file *file, void *priv,
  1045. struct v4l2_buffer *p)
  1046. {
  1047. struct cx23885_fh *fh = priv;
  1048. return videobuf_dqbuf(get_queue(fh), p,
  1049. file->f_flags & O_NONBLOCK);
  1050. }
  1051. static int vidioc_streamon(struct file *file, void *priv,
  1052. enum v4l2_buf_type i)
  1053. {
  1054. struct cx23885_fh *fh = priv;
  1055. struct cx23885_dev *dev = fh->dev;
  1056. dprintk(1, "%s()\n", __func__);
  1057. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1058. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1059. return -EINVAL;
  1060. if (unlikely(i != fh->type))
  1061. return -EINVAL;
  1062. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  1063. return -EBUSY;
  1064. /* Don't start VBI streaming unless vida streaming
  1065. * has already started.
  1066. */
  1067. if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
  1068. ((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
  1069. return -EINVAL;
  1070. return videobuf_streamon(get_queue(fh));
  1071. }
  1072. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1073. {
  1074. struct cx23885_fh *fh = priv;
  1075. struct cx23885_dev *dev = fh->dev;
  1076. int err, res;
  1077. dprintk(1, "%s()\n", __func__);
  1078. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1079. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1080. return -EINVAL;
  1081. if (i != fh->type)
  1082. return -EINVAL;
  1083. res = get_resource(fh);
  1084. err = videobuf_streamoff(get_queue(fh));
  1085. if (err < 0)
  1086. return err;
  1087. res_free(dev, fh, res);
  1088. return 0;
  1089. }
  1090. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1091. {
  1092. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1093. dprintk(1, "%s()\n", __func__);
  1094. *id = dev->tvnorm;
  1095. return 0;
  1096. }
  1097. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  1098. {
  1099. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1100. dprintk(1, "%s()\n", __func__);
  1101. mutex_lock(&dev->lock);
  1102. cx23885_set_tvnorm(dev, tvnorms);
  1103. mutex_unlock(&dev->lock);
  1104. return 0;
  1105. }
  1106. int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  1107. {
  1108. static const char *iname[] = {
  1109. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  1110. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  1111. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  1112. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1113. [CX23885_VMUX_SVIDEO] = "S-Video",
  1114. [CX23885_VMUX_COMPONENT] = "Component",
  1115. [CX23885_VMUX_TELEVISION] = "Television",
  1116. [CX23885_VMUX_CABLE] = "Cable TV",
  1117. [CX23885_VMUX_DVB] = "DVB",
  1118. [CX23885_VMUX_DEBUG] = "for debug only",
  1119. };
  1120. unsigned int n;
  1121. dprintk(1, "%s()\n", __func__);
  1122. n = i->index;
  1123. if (n >= MAX_CX23885_INPUT)
  1124. return -EINVAL;
  1125. if (0 == INPUT(n)->type)
  1126. return -EINVAL;
  1127. i->index = n;
  1128. i->type = V4L2_INPUT_TYPE_CAMERA;
  1129. strcpy(i->name, iname[INPUT(n)->type]);
  1130. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1131. (CX23885_VMUX_CABLE == INPUT(n)->type)) {
  1132. i->type = V4L2_INPUT_TYPE_TUNER;
  1133. i->std = CX23885_NORMS;
  1134. }
  1135. /* Two selectable audio inputs for non-tv inputs */
  1136. if (INPUT(n)->type != CX23885_VMUX_TELEVISION)
  1137. i->audioset = 0x3;
  1138. if (dev->input == n) {
  1139. /* enum'd input matches our configured input.
  1140. * Ask the video decoder to process the call
  1141. * and give it an oppertunity to update the
  1142. * status field.
  1143. */
  1144. call_all(dev, video, g_input_status, &i->status);
  1145. }
  1146. return 0;
  1147. }
  1148. static int vidioc_enum_input(struct file *file, void *priv,
  1149. struct v4l2_input *i)
  1150. {
  1151. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1152. dprintk(1, "%s()\n", __func__);
  1153. return cx23885_enum_input(dev, i);
  1154. }
  1155. int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
  1156. {
  1157. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1158. *i = dev->input;
  1159. dprintk(1, "%s() returns %d\n", __func__, *i);
  1160. return 0;
  1161. }
  1162. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1163. {
  1164. return cx23885_get_input(file, priv, i);
  1165. }
  1166. int cx23885_set_input(struct file *file, void *priv, unsigned int i)
  1167. {
  1168. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1169. dprintk(1, "%s(%d)\n", __func__, i);
  1170. if (i >= MAX_CX23885_INPUT) {
  1171. dprintk(1, "%s() -EINVAL\n", __func__);
  1172. return -EINVAL;
  1173. }
  1174. if (INPUT(i)->type == 0)
  1175. return -EINVAL;
  1176. mutex_lock(&dev->lock);
  1177. cx23885_video_mux(dev, i);
  1178. /* By default establish the default audio input for the card also */
  1179. /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
  1180. cx23885_audio_mux(dev, i);
  1181. mutex_unlock(&dev->lock);
  1182. return 0;
  1183. }
  1184. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1185. {
  1186. return cx23885_set_input(file, priv, i);
  1187. }
  1188. static int vidioc_log_status(struct file *file, void *priv)
  1189. {
  1190. struct cx23885_fh *fh = priv;
  1191. struct cx23885_dev *dev = fh->dev;
  1192. printk(KERN_INFO
  1193. "%s/0: ============ START LOG STATUS ============\n",
  1194. dev->name);
  1195. call_all(dev, core, log_status);
  1196. printk(KERN_INFO
  1197. "%s/0: ============= END LOG STATUS =============\n",
  1198. dev->name);
  1199. return 0;
  1200. }
  1201. static int cx23885_query_audinput(struct file *file, void *priv,
  1202. struct v4l2_audio *i)
  1203. {
  1204. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1205. static const char *iname[] = {
  1206. [0] = "Baseband L/R 1",
  1207. [1] = "Baseband L/R 2",
  1208. };
  1209. unsigned int n;
  1210. dprintk(1, "%s()\n", __func__);
  1211. n = i->index;
  1212. if (n >= 2)
  1213. return -EINVAL;
  1214. memset(i, 0, sizeof(*i));
  1215. i->index = n;
  1216. strcpy(i->name, iname[n]);
  1217. i->capability = V4L2_AUDCAP_STEREO;
  1218. i->mode = V4L2_AUDMODE_AVL;
  1219. return 0;
  1220. }
  1221. static int vidioc_enum_audinput(struct file *file, void *priv,
  1222. struct v4l2_audio *i)
  1223. {
  1224. return cx23885_query_audinput(file, priv, i);
  1225. }
  1226. static int vidioc_g_audinput(struct file *file, void *priv,
  1227. struct v4l2_audio *i)
  1228. {
  1229. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1230. i->index = dev->audinput;
  1231. dprintk(1, "%s(input=%d)\n", __func__, i->index);
  1232. return cx23885_query_audinput(file, priv, i);
  1233. }
  1234. static int vidioc_s_audinput(struct file *file, void *priv,
  1235. const struct v4l2_audio *i)
  1236. {
  1237. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1238. if (i->index >= 2)
  1239. return -EINVAL;
  1240. dprintk(1, "%s(%d)\n", __func__, i->index);
  1241. dev->audinput = i->index;
  1242. /* Skip the audio defaults from the cards struct, caller wants
  1243. * directly touch the audio mux hardware. */
  1244. cx23885_flatiron_mux(dev, dev->audinput + 1);
  1245. return 0;
  1246. }
  1247. static int vidioc_queryctrl(struct file *file, void *priv,
  1248. struct v4l2_queryctrl *qctrl)
  1249. {
  1250. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1251. if (unlikely(qctrl->id == 0))
  1252. return -EINVAL;
  1253. return cx23885_ctrl_query(qctrl);
  1254. }
  1255. static int vidioc_g_ctrl(struct file *file, void *priv,
  1256. struct v4l2_control *ctl)
  1257. {
  1258. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1259. return cx23885_get_control(dev, ctl);
  1260. }
  1261. static int vidioc_s_ctrl(struct file *file, void *priv,
  1262. struct v4l2_control *ctl)
  1263. {
  1264. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1265. return cx23885_set_control(dev, ctl);
  1266. }
  1267. static int vidioc_g_tuner(struct file *file, void *priv,
  1268. struct v4l2_tuner *t)
  1269. {
  1270. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1271. if (unlikely(UNSET == dev->tuner_type))
  1272. return -EINVAL;
  1273. if (0 != t->index)
  1274. return -EINVAL;
  1275. strcpy(t->name, "Television");
  1276. call_all(dev, tuner, g_tuner, t);
  1277. return 0;
  1278. }
  1279. static int vidioc_s_tuner(struct file *file, void *priv,
  1280. const struct v4l2_tuner *t)
  1281. {
  1282. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1283. if (UNSET == dev->tuner_type)
  1284. return -EINVAL;
  1285. if (0 != t->index)
  1286. return -EINVAL;
  1287. /* Update the A/V core */
  1288. call_all(dev, tuner, s_tuner, t);
  1289. return 0;
  1290. }
  1291. static int vidioc_g_frequency(struct file *file, void *priv,
  1292. struct v4l2_frequency *f)
  1293. {
  1294. struct cx23885_fh *fh = priv;
  1295. struct cx23885_dev *dev = fh->dev;
  1296. if (unlikely(UNSET == dev->tuner_type))
  1297. return -EINVAL;
  1298. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1299. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1300. f->frequency = dev->freq;
  1301. call_all(dev, tuner, g_frequency, f);
  1302. return 0;
  1303. }
  1304. static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
  1305. {
  1306. struct v4l2_control ctrl;
  1307. if (unlikely(UNSET == dev->tuner_type))
  1308. return -EINVAL;
  1309. if (unlikely(f->tuner != 0))
  1310. return -EINVAL;
  1311. mutex_lock(&dev->lock);
  1312. dev->freq = f->frequency;
  1313. /* I need to mute audio here */
  1314. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1315. ctrl.value = 1;
  1316. cx23885_set_control(dev, &ctrl);
  1317. call_all(dev, tuner, s_frequency, f);
  1318. /* When changing channels it is required to reset TVAUDIO */
  1319. msleep(100);
  1320. /* I need to unmute audio here */
  1321. ctrl.value = 0;
  1322. cx23885_set_control(dev, &ctrl);
  1323. mutex_unlock(&dev->lock);
  1324. return 0;
  1325. }
  1326. static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
  1327. const struct v4l2_frequency *f)
  1328. {
  1329. struct v4l2_control ctrl;
  1330. struct videobuf_dvb_frontend *vfe;
  1331. struct dvb_frontend *fe;
  1332. struct analog_parameters params = {
  1333. .mode = V4L2_TUNER_ANALOG_TV,
  1334. .audmode = V4L2_TUNER_MODE_STEREO,
  1335. .std = dev->tvnorm,
  1336. .frequency = f->frequency
  1337. };
  1338. mutex_lock(&dev->lock);
  1339. dev->freq = f->frequency;
  1340. /* I need to mute audio here */
  1341. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1342. ctrl.value = 1;
  1343. cx23885_set_control(dev, &ctrl);
  1344. /* If HVR1850 */
  1345. dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
  1346. params.frequency, f->tuner, params.std);
  1347. vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1);
  1348. if (!vfe) {
  1349. mutex_unlock(&dev->lock);
  1350. return -EINVAL;
  1351. }
  1352. fe = vfe->dvb.frontend;
  1353. if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
  1354. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
  1355. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
  1356. fe = &dev->ts1.analog_fe;
  1357. if (fe && fe->ops.tuner_ops.set_analog_params) {
  1358. call_all(dev, core, s_std, dev->tvnorm);
  1359. fe->ops.tuner_ops.set_analog_params(fe, &params);
  1360. }
  1361. else
  1362. printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
  1363. /* When changing channels it is required to reset TVAUDIO */
  1364. msleep(100);
  1365. /* I need to unmute audio here */
  1366. ctrl.value = 0;
  1367. cx23885_set_control(dev, &ctrl);
  1368. mutex_unlock(&dev->lock);
  1369. return 0;
  1370. }
  1371. int cx23885_set_frequency(struct file *file, void *priv,
  1372. const struct v4l2_frequency *f)
  1373. {
  1374. struct cx23885_fh *fh = priv;
  1375. struct cx23885_dev *dev = fh->dev;
  1376. int ret;
  1377. switch (dev->board) {
  1378. case CX23885_BOARD_HAUPPAUGE_HVR1255:
  1379. case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
  1380. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  1381. ret = cx23885_set_freq_via_ops(dev, f);
  1382. break;
  1383. default:
  1384. ret = cx23885_set_freq(dev, f);
  1385. }
  1386. return ret;
  1387. }
  1388. static int vidioc_s_frequency(struct file *file, void *priv,
  1389. const struct v4l2_frequency *f)
  1390. {
  1391. return cx23885_set_frequency(file, priv, f);
  1392. }
  1393. /* ----------------------------------------------------------- */
  1394. static void cx23885_vid_timeout(unsigned long data)
  1395. {
  1396. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1397. struct cx23885_dmaqueue *q = &dev->vidq;
  1398. struct cx23885_buffer *buf;
  1399. unsigned long flags;
  1400. spin_lock_irqsave(&dev->slock, flags);
  1401. while (!list_empty(&q->active)) {
  1402. buf = list_entry(q->active.next,
  1403. struct cx23885_buffer, vb.queue);
  1404. list_del(&buf->vb.queue);
  1405. buf->vb.state = VIDEOBUF_ERROR;
  1406. wake_up(&buf->vb.done);
  1407. printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
  1408. dev->name, buf, buf->vb.i,
  1409. (unsigned long)buf->risc.dma);
  1410. }
  1411. cx23885_restart_video_queue(dev, q);
  1412. spin_unlock_irqrestore(&dev->slock, flags);
  1413. }
  1414. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1415. {
  1416. u32 mask, count;
  1417. int handled = 0;
  1418. mask = cx_read(VID_A_INT_MSK);
  1419. if (0 == (status & mask))
  1420. return handled;
  1421. cx_write(VID_A_INT_STAT, status);
  1422. /* risc op code error, fifo overflow or line sync detection error */
  1423. if ((status & VID_BC_MSK_OPC_ERR) ||
  1424. (status & VID_BC_MSK_SYNC) ||
  1425. (status & VID_BC_MSK_OF)) {
  1426. if (status & VID_BC_MSK_OPC_ERR) {
  1427. dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
  1428. VID_BC_MSK_OPC_ERR);
  1429. printk(KERN_WARNING "%s: video risc op code error\n",
  1430. dev->name);
  1431. cx23885_sram_channel_dump(dev,
  1432. &dev->sram_channels[SRAM_CH01]);
  1433. }
  1434. if (status & VID_BC_MSK_SYNC)
  1435. dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
  1436. "video lines miss-match\n",
  1437. VID_BC_MSK_SYNC);
  1438. if (status & VID_BC_MSK_OF)
  1439. dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
  1440. VID_BC_MSK_OF);
  1441. }
  1442. /* Video */
  1443. if (status & VID_BC_MSK_RISCI1) {
  1444. spin_lock(&dev->slock);
  1445. count = cx_read(VID_A_GPCNT);
  1446. cx23885_video_wakeup(dev, &dev->vidq, count);
  1447. spin_unlock(&dev->slock);
  1448. handled++;
  1449. }
  1450. if (status & VID_BC_MSK_RISCI2) {
  1451. dprintk(2, "stopper video\n");
  1452. spin_lock(&dev->slock);
  1453. cx23885_restart_video_queue(dev, &dev->vidq);
  1454. spin_unlock(&dev->slock);
  1455. handled++;
  1456. }
  1457. /* Allow the VBI framework to process it's payload */
  1458. handled += cx23885_vbi_irq(dev, status);
  1459. return handled;
  1460. }
  1461. /* ----------------------------------------------------------- */
  1462. /* exported stuff */
  1463. static const struct v4l2_file_operations video_fops = {
  1464. .owner = THIS_MODULE,
  1465. .open = video_open,
  1466. .release = video_release,
  1467. .read = video_read,
  1468. .poll = video_poll,
  1469. .mmap = video_mmap,
  1470. .ioctl = video_ioctl2,
  1471. };
  1472. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1473. .vidioc_querycap = vidioc_querycap,
  1474. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1475. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1476. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1477. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1478. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  1479. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  1480. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  1481. .vidioc_reqbufs = vidioc_reqbufs,
  1482. .vidioc_querybuf = vidioc_querybuf,
  1483. .vidioc_qbuf = vidioc_qbuf,
  1484. .vidioc_dqbuf = vidioc_dqbuf,
  1485. .vidioc_s_std = vidioc_s_std,
  1486. .vidioc_g_std = vidioc_g_std,
  1487. .vidioc_enum_input = vidioc_enum_input,
  1488. .vidioc_g_input = vidioc_g_input,
  1489. .vidioc_s_input = vidioc_s_input,
  1490. .vidioc_log_status = vidioc_log_status,
  1491. .vidioc_queryctrl = vidioc_queryctrl,
  1492. .vidioc_g_ctrl = vidioc_g_ctrl,
  1493. .vidioc_s_ctrl = vidioc_s_ctrl,
  1494. .vidioc_streamon = vidioc_streamon,
  1495. .vidioc_streamoff = vidioc_streamoff,
  1496. .vidioc_g_tuner = vidioc_g_tuner,
  1497. .vidioc_s_tuner = vidioc_s_tuner,
  1498. .vidioc_g_frequency = vidioc_g_frequency,
  1499. .vidioc_s_frequency = vidioc_s_frequency,
  1500. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1501. .vidioc_g_chip_info = cx23885_g_chip_info,
  1502. .vidioc_g_register = cx23885_g_register,
  1503. .vidioc_s_register = cx23885_s_register,
  1504. #endif
  1505. .vidioc_enumaudio = vidioc_enum_audinput,
  1506. .vidioc_g_audio = vidioc_g_audinput,
  1507. .vidioc_s_audio = vidioc_s_audinput,
  1508. };
  1509. static struct video_device cx23885_vbi_template;
  1510. static struct video_device cx23885_video_template = {
  1511. .name = "cx23885-video",
  1512. .fops = &video_fops,
  1513. .ioctl_ops = &video_ioctl_ops,
  1514. .tvnorms = CX23885_NORMS,
  1515. };
  1516. static const struct v4l2_file_operations radio_fops = {
  1517. .owner = THIS_MODULE,
  1518. .open = video_open,
  1519. .release = video_release,
  1520. .ioctl = video_ioctl2,
  1521. };
  1522. void cx23885_video_unregister(struct cx23885_dev *dev)
  1523. {
  1524. dprintk(1, "%s()\n", __func__);
  1525. cx23885_irq_remove(dev, 0x01);
  1526. if (dev->vbi_dev) {
  1527. if (video_is_registered(dev->vbi_dev))
  1528. video_unregister_device(dev->vbi_dev);
  1529. else
  1530. video_device_release(dev->vbi_dev);
  1531. dev->vbi_dev = NULL;
  1532. btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
  1533. }
  1534. if (dev->video_dev) {
  1535. if (video_is_registered(dev->video_dev))
  1536. video_unregister_device(dev->video_dev);
  1537. else
  1538. video_device_release(dev->video_dev);
  1539. dev->video_dev = NULL;
  1540. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1541. }
  1542. if (dev->audio_dev)
  1543. cx23885_audio_unregister(dev);
  1544. }
  1545. int cx23885_video_register(struct cx23885_dev *dev)
  1546. {
  1547. int err;
  1548. dprintk(1, "%s()\n", __func__);
  1549. spin_lock_init(&dev->slock);
  1550. /* Initialize VBI template */
  1551. cx23885_vbi_template = cx23885_video_template;
  1552. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1553. dev->tvnorm = V4L2_STD_NTSC_M;
  1554. /* init video dma queues */
  1555. INIT_LIST_HEAD(&dev->vidq.active);
  1556. INIT_LIST_HEAD(&dev->vidq.queued);
  1557. dev->vidq.timeout.function = cx23885_vid_timeout;
  1558. dev->vidq.timeout.data = (unsigned long)dev;
  1559. init_timer(&dev->vidq.timeout);
  1560. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1561. VID_A_DMA_CTL, 0x11, 0x00);
  1562. /* init vbi dma queues */
  1563. INIT_LIST_HEAD(&dev->vbiq.active);
  1564. INIT_LIST_HEAD(&dev->vbiq.queued);
  1565. dev->vbiq.timeout.function = cx23885_vbi_timeout;
  1566. dev->vbiq.timeout.data = (unsigned long)dev;
  1567. init_timer(&dev->vbiq.timeout);
  1568. cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
  1569. VID_A_DMA_CTL, 0x22, 0x00);
  1570. cx23885_irq_add_enable(dev, 0x01);
  1571. if ((TUNER_ABSENT != dev->tuner_type) &&
  1572. ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
  1573. struct v4l2_subdev *sd = NULL;
  1574. if (dev->tuner_addr)
  1575. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1576. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  1577. "tuner", dev->tuner_addr, NULL);
  1578. else
  1579. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1580. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  1581. "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
  1582. if (sd) {
  1583. struct tuner_setup tun_setup;
  1584. memset(&tun_setup, 0, sizeof(tun_setup));
  1585. tun_setup.mode_mask = T_ANALOG_TV;
  1586. tun_setup.type = dev->tuner_type;
  1587. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  1588. tun_setup.tuner_callback = cx23885_tuner_callback;
  1589. v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
  1590. if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
  1591. struct xc2028_ctrl ctrl = {
  1592. .fname = XC2028_DEFAULT_FIRMWARE,
  1593. .max_len = 64
  1594. };
  1595. struct v4l2_priv_tun_config cfg = {
  1596. .tuner = dev->tuner_type,
  1597. .priv = &ctrl
  1598. };
  1599. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1600. }
  1601. if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
  1602. struct xc2028_ctrl ctrl = {
  1603. .fname = "xc3028L-v36.fw",
  1604. .max_len = 64
  1605. };
  1606. struct v4l2_priv_tun_config cfg = {
  1607. .tuner = dev->tuner_type,
  1608. .priv = &ctrl
  1609. };
  1610. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1611. }
  1612. }
  1613. }
  1614. /* register Video device */
  1615. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1616. &cx23885_video_template, "video");
  1617. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1618. video_nr[dev->nr]);
  1619. if (err < 0) {
  1620. printk(KERN_INFO "%s: can't register video device\n",
  1621. dev->name);
  1622. goto fail_unreg;
  1623. }
  1624. printk(KERN_INFO "%s: registered device %s [v4l2]\n",
  1625. dev->name, video_device_node_name(dev->video_dev));
  1626. /* register VBI device */
  1627. dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
  1628. &cx23885_vbi_template, "vbi");
  1629. err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1630. vbi_nr[dev->nr]);
  1631. if (err < 0) {
  1632. printk(KERN_INFO "%s: can't register vbi device\n",
  1633. dev->name);
  1634. goto fail_unreg;
  1635. }
  1636. printk(KERN_INFO "%s: registered device %s\n",
  1637. dev->name, video_device_node_name(dev->vbi_dev));
  1638. /* Register ALSA audio device */
  1639. dev->audio_dev = cx23885_audio_register(dev);
  1640. /* initial device configuration */
  1641. mutex_lock(&dev->lock);
  1642. cx23885_set_tvnorm(dev, dev->tvnorm);
  1643. init_controls(dev);
  1644. cx23885_video_mux(dev, 0);
  1645. cx23885_audio_mux(dev, 0);
  1646. mutex_unlock(&dev->lock);
  1647. return 0;
  1648. fail_unreg:
  1649. cx23885_video_unregister(dev);
  1650. return err;
  1651. }