cxusb-analog.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // DVB USB compliant linux driver for Conexant USB reference design -
  4. // (analog part).
  5. //
  6. // Copyright (C) 2011, 2017, 2018
  7. // Maciej S. Szmigiero ([email protected])
  8. //
  9. // In case there are new analog / DVB-T hybrid devices released in the market
  10. // using the same general design as Medion MD95700: a CX25840 video decoder
  11. // outputting a BT.656 stream to a USB bridge chip which then forwards it to
  12. // the host in isochronous USB packets this code should be made generic, with
  13. // board specific bits implemented via separate card structures.
  14. //
  15. // This is, however, unlikely as the Medion model was released
  16. // years ago (in 2005).
  17. //
  18. // TODO:
  19. // * audio support,
  20. // * finish radio support (requires audio of course),
  21. // * VBI support,
  22. // * controls support
  23. #include <linux/bitops.h>
  24. #include <linux/device.h>
  25. #include <linux/slab.h>
  26. #include <linux/string.h>
  27. #include <linux/ktime.h>
  28. #include <linux/vmalloc.h>
  29. #include <media/drv-intf/cx25840.h>
  30. #include <media/tuner.h>
  31. #include <media/v4l2-fh.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-subdev.h>
  34. #include <media/videobuf2-vmalloc.h>
  35. #include "cxusb.h"
  36. static int cxusb_medion_v_queue_setup(struct vb2_queue *q,
  37. unsigned int *num_buffers,
  38. unsigned int *num_planes,
  39. unsigned int sizes[],
  40. struct device *alloc_devs[])
  41. {
  42. struct dvb_usb_device *dvbdev = vb2_get_drv_priv(q);
  43. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  44. unsigned int size = cxdev->width * cxdev->height * 2;
  45. if (*num_planes > 0) {
  46. if (*num_planes != 1)
  47. return -EINVAL;
  48. if (sizes[0] < size)
  49. return -EINVAL;
  50. } else {
  51. *num_planes = 1;
  52. sizes[0] = size;
  53. }
  54. return 0;
  55. }
  56. static int cxusb_medion_v_buf_init(struct vb2_buffer *vb)
  57. {
  58. struct dvb_usb_device *dvbdev = vb2_get_drv_priv(vb->vb2_queue);
  59. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  60. cxusb_vprintk(dvbdev, OPS, "buffer init\n");
  61. if (vb2_plane_size(vb, 0) < cxdev->width * cxdev->height * 2)
  62. return -ENOMEM;
  63. cxusb_vprintk(dvbdev, OPS, "buffer OK\n");
  64. return 0;
  65. }
  66. static void cxusb_auxbuf_init(struct dvb_usb_device *dvbdev,
  67. struct cxusb_medion_auxbuf *auxbuf,
  68. u8 *buf, unsigned int len)
  69. {
  70. cxusb_vprintk(dvbdev, AUXB, "initializing auxbuf of len %u\n", len);
  71. auxbuf->buf = buf;
  72. auxbuf->len = len;
  73. auxbuf->paylen = 0;
  74. }
  75. static void cxusb_auxbuf_head_trim(struct dvb_usb_device *dvbdev,
  76. struct cxusb_medion_auxbuf *auxbuf,
  77. unsigned int pos)
  78. {
  79. if (pos == 0)
  80. return;
  81. if (WARN_ON(pos > auxbuf->paylen))
  82. return;
  83. cxusb_vprintk(dvbdev, AUXB,
  84. "trimming auxbuf len by %u to %u\n",
  85. pos, auxbuf->paylen - pos);
  86. memmove(auxbuf->buf, auxbuf->buf + pos, auxbuf->paylen - pos);
  87. auxbuf->paylen -= pos;
  88. }
  89. static unsigned int cxusb_auxbuf_paylen(struct cxusb_medion_auxbuf *auxbuf)
  90. {
  91. return auxbuf->paylen;
  92. }
  93. static bool cxusb_auxbuf_make_space(struct dvb_usb_device *dvbdev,
  94. struct cxusb_medion_auxbuf *auxbuf,
  95. unsigned int howmuch)
  96. {
  97. unsigned int freespace;
  98. if (WARN_ON(howmuch >= auxbuf->len))
  99. howmuch = auxbuf->len - 1;
  100. freespace = auxbuf->len - cxusb_auxbuf_paylen(auxbuf);
  101. cxusb_vprintk(dvbdev, AUXB, "freespace is %u\n", freespace);
  102. if (freespace >= howmuch)
  103. return true;
  104. howmuch -= freespace;
  105. cxusb_vprintk(dvbdev, AUXB, "will overwrite %u bytes of buffer\n",
  106. howmuch);
  107. cxusb_auxbuf_head_trim(dvbdev, auxbuf, howmuch);
  108. return false;
  109. }
  110. /* returns false if some data was overwritten */
  111. static bool cxusb_auxbuf_append_urb(struct dvb_usb_device *dvbdev,
  112. struct cxusb_medion_auxbuf *auxbuf,
  113. struct urb *urb)
  114. {
  115. unsigned long len;
  116. int i;
  117. bool ret;
  118. for (i = 0, len = 0; i < urb->number_of_packets; i++)
  119. len += urb->iso_frame_desc[i].actual_length;
  120. ret = cxusb_auxbuf_make_space(dvbdev, auxbuf, len);
  121. for (i = 0; i < urb->number_of_packets; i++) {
  122. unsigned int to_copy;
  123. to_copy = urb->iso_frame_desc[i].actual_length;
  124. memcpy(auxbuf->buf + auxbuf->paylen, urb->transfer_buffer +
  125. urb->iso_frame_desc[i].offset, to_copy);
  126. auxbuf->paylen += to_copy;
  127. }
  128. return ret;
  129. }
  130. static bool cxusb_auxbuf_copy(struct cxusb_medion_auxbuf *auxbuf,
  131. unsigned int pos, unsigned char *dest,
  132. unsigned int len)
  133. {
  134. if (pos + len > auxbuf->paylen)
  135. return false;
  136. memcpy(dest, auxbuf->buf + pos, len);
  137. return true;
  138. }
  139. static bool cxusb_medion_cf_refc_fld_chg(struct dvb_usb_device *dvbdev,
  140. struct cxusb_bt656_params *bt656,
  141. bool firstfield,
  142. unsigned int maxlines,
  143. unsigned int maxlinesamples,
  144. unsigned char buf[4])
  145. {
  146. bool firstfield_code = (buf[3] & CXUSB_BT656_FIELD_MASK) ==
  147. CXUSB_BT656_FIELD_1;
  148. unsigned int remlines;
  149. if (bt656->line == 0 || firstfield == firstfield_code)
  150. return false;
  151. if (bt656->fmode == LINE_SAMPLES) {
  152. unsigned int remsamples = maxlinesamples -
  153. bt656->linesamples;
  154. cxusb_vprintk(dvbdev, BT656,
  155. "field %c after line %u field change\n",
  156. firstfield ? '1' : '2', bt656->line);
  157. if (bt656->buf && remsamples > 0) {
  158. memset(bt656->buf, 0, remsamples);
  159. bt656->buf += remsamples;
  160. cxusb_vprintk(dvbdev, BT656,
  161. "field %c line %u %u samples still remaining (of %u)\n",
  162. firstfield ? '1' : '2',
  163. bt656->line, remsamples,
  164. maxlinesamples);
  165. }
  166. bt656->line++;
  167. }
  168. remlines = maxlines - bt656->line;
  169. if (bt656->buf && remlines > 0) {
  170. memset(bt656->buf, 0, remlines * maxlinesamples);
  171. bt656->buf += remlines * maxlinesamples;
  172. cxusb_vprintk(dvbdev, BT656,
  173. "field %c %u lines still remaining (of %u)\n",
  174. firstfield ? '1' : '2', remlines,
  175. maxlines);
  176. }
  177. return true;
  178. }
  179. static void cxusb_medion_cf_refc_start_sch(struct dvb_usb_device *dvbdev,
  180. struct cxusb_bt656_params *bt656,
  181. bool firstfield,
  182. unsigned char buf[4])
  183. {
  184. bool firstfield_code = (buf[3] & CXUSB_BT656_FIELD_MASK) ==
  185. CXUSB_BT656_FIELD_1;
  186. bool sav_code = (buf[3] & CXUSB_BT656_SEAV_MASK) ==
  187. CXUSB_BT656_SEAV_SAV;
  188. bool vbi_code = (buf[3] & CXUSB_BT656_VBI_MASK) ==
  189. CXUSB_BT656_VBI_ON;
  190. if (!sav_code || firstfield != firstfield_code)
  191. return;
  192. if (!vbi_code) {
  193. cxusb_vprintk(dvbdev, BT656, "line start @ pos %u\n",
  194. bt656->pos);
  195. bt656->linesamples = 0;
  196. bt656->fmode = LINE_SAMPLES;
  197. } else {
  198. cxusb_vprintk(dvbdev, BT656, "VBI start @ pos %u\n",
  199. bt656->pos);
  200. bt656->fmode = VBI_SAMPLES;
  201. }
  202. }
  203. static void cxusb_medion_cf_refc_line_smpl(struct dvb_usb_device *dvbdev,
  204. struct cxusb_bt656_params *bt656,
  205. bool firstfield,
  206. unsigned int maxlinesamples,
  207. unsigned char buf[4])
  208. {
  209. bool sav_code = (buf[3] & CXUSB_BT656_SEAV_MASK) ==
  210. CXUSB_BT656_SEAV_SAV;
  211. unsigned int remsamples;
  212. if (sav_code)
  213. cxusb_vprintk(dvbdev, BT656,
  214. "SAV in line samples @ line %u, pos %u\n",
  215. bt656->line, bt656->pos);
  216. remsamples = maxlinesamples - bt656->linesamples;
  217. if (bt656->buf && remsamples > 0) {
  218. memset(bt656->buf, 0, remsamples);
  219. bt656->buf += remsamples;
  220. cxusb_vprintk(dvbdev, BT656,
  221. "field %c line %u %u samples still remaining (of %u)\n",
  222. firstfield ? '1' : '2', bt656->line, remsamples,
  223. maxlinesamples);
  224. }
  225. bt656->fmode = START_SEARCH;
  226. bt656->line++;
  227. }
  228. static void cxusb_medion_cf_refc_vbi_smpl(struct dvb_usb_device *dvbdev,
  229. struct cxusb_bt656_params *bt656,
  230. unsigned char buf[4])
  231. {
  232. bool sav_code = (buf[3] & CXUSB_BT656_SEAV_MASK) ==
  233. CXUSB_BT656_SEAV_SAV;
  234. if (sav_code)
  235. cxusb_vprintk(dvbdev, BT656, "SAV in VBI samples @ pos %u\n",
  236. bt656->pos);
  237. bt656->fmode = START_SEARCH;
  238. }
  239. /* returns whether the whole 4-byte code should be skipped in the buffer */
  240. static bool cxusb_medion_cf_ref_code(struct dvb_usb_device *dvbdev,
  241. struct cxusb_bt656_params *bt656,
  242. bool firstfield,
  243. unsigned int maxlines,
  244. unsigned int maxlinesamples,
  245. unsigned char buf[4])
  246. {
  247. if (bt656->fmode == START_SEARCH) {
  248. cxusb_medion_cf_refc_start_sch(dvbdev, bt656, firstfield, buf);
  249. } else if (bt656->fmode == LINE_SAMPLES) {
  250. cxusb_medion_cf_refc_line_smpl(dvbdev, bt656, firstfield,
  251. maxlinesamples, buf);
  252. return false;
  253. } else if (bt656->fmode == VBI_SAMPLES) {
  254. cxusb_medion_cf_refc_vbi_smpl(dvbdev, bt656, buf);
  255. return false;
  256. }
  257. return true;
  258. }
  259. static bool cxusb_medion_cs_start_sch(struct dvb_usb_device *dvbdev,
  260. struct cxusb_medion_auxbuf *auxbuf,
  261. struct cxusb_bt656_params *bt656,
  262. unsigned int maxlinesamples)
  263. {
  264. unsigned char buf[64];
  265. unsigned int idx;
  266. unsigned int tocheck = clamp_t(size_t, maxlinesamples / 4, 3,
  267. sizeof(buf));
  268. if (!cxusb_auxbuf_copy(auxbuf, bt656->pos + 1, buf, tocheck))
  269. return false;
  270. for (idx = 0; idx <= tocheck - 3; idx++)
  271. if (memcmp(buf + idx, CXUSB_BT656_PREAMBLE, 3) == 0) {
  272. bt656->pos += (1 + idx);
  273. return true;
  274. }
  275. cxusb_vprintk(dvbdev, BT656, "line %u early start, pos %u\n",
  276. bt656->line, bt656->pos);
  277. bt656->linesamples = 0;
  278. bt656->fmode = LINE_SAMPLES;
  279. return true;
  280. }
  281. static void cxusb_medion_cs_line_smpl(struct cxusb_bt656_params *bt656,
  282. unsigned int maxlinesamples,
  283. unsigned char val)
  284. {
  285. if (bt656->buf)
  286. *(bt656->buf++) = val;
  287. bt656->linesamples++;
  288. bt656->pos++;
  289. if (bt656->linesamples >= maxlinesamples) {
  290. bt656->fmode = START_SEARCH;
  291. bt656->line++;
  292. }
  293. }
  294. static bool cxusb_medion_copy_samples(struct dvb_usb_device *dvbdev,
  295. struct cxusb_medion_auxbuf *auxbuf,
  296. struct cxusb_bt656_params *bt656,
  297. unsigned int maxlinesamples,
  298. unsigned char val)
  299. {
  300. if (bt656->fmode == START_SEARCH && bt656->line > 0)
  301. return cxusb_medion_cs_start_sch(dvbdev, auxbuf, bt656,
  302. maxlinesamples);
  303. else if (bt656->fmode == LINE_SAMPLES)
  304. cxusb_medion_cs_line_smpl(bt656, maxlinesamples, val);
  305. else /* TODO: copy VBI samples */
  306. bt656->pos++;
  307. return true;
  308. }
  309. static bool cxusb_medion_copy_field(struct dvb_usb_device *dvbdev,
  310. struct cxusb_medion_auxbuf *auxbuf,
  311. struct cxusb_bt656_params *bt656,
  312. bool firstfield,
  313. unsigned int maxlines,
  314. unsigned int maxlinesmpls)
  315. {
  316. while (bt656->line < maxlines) {
  317. unsigned char val;
  318. if (!cxusb_auxbuf_copy(auxbuf, bt656->pos, &val, 1))
  319. break;
  320. if (val == CXUSB_BT656_PREAMBLE[0]) {
  321. unsigned char buf[4];
  322. buf[0] = val;
  323. if (!cxusb_auxbuf_copy(auxbuf, bt656->pos + 1,
  324. buf + 1, 3))
  325. break;
  326. if (buf[1] == CXUSB_BT656_PREAMBLE[1] &&
  327. buf[2] == CXUSB_BT656_PREAMBLE[2]) {
  328. /*
  329. * is this a field change?
  330. * if so, terminate copying the current field
  331. */
  332. if (cxusb_medion_cf_refc_fld_chg(dvbdev,
  333. bt656,
  334. firstfield,
  335. maxlines,
  336. maxlinesmpls,
  337. buf))
  338. return true;
  339. if (cxusb_medion_cf_ref_code(dvbdev, bt656,
  340. firstfield,
  341. maxlines,
  342. maxlinesmpls,
  343. buf))
  344. bt656->pos += 4;
  345. continue;
  346. }
  347. }
  348. if (!cxusb_medion_copy_samples(dvbdev, auxbuf, bt656,
  349. maxlinesmpls, val))
  350. break;
  351. }
  352. if (bt656->line < maxlines) {
  353. cxusb_vprintk(dvbdev, BT656,
  354. "end of buffer pos = %u, line = %u\n",
  355. bt656->pos, bt656->line);
  356. return false;
  357. }
  358. return true;
  359. }
  360. static bool cxusb_medion_v_process_auxbuf(struct cxusb_medion_dev *cxdev,
  361. bool reset)
  362. {
  363. struct dvb_usb_device *dvbdev = cxdev->dvbdev;
  364. struct cxusb_bt656_params *bt656 = &cxdev->bt656;
  365. /*
  366. * if this is a new frame
  367. * fetch a buffer from list
  368. */
  369. if (bt656->mode == NEW_FRAME) {
  370. if (!list_empty(&cxdev->buflist)) {
  371. cxdev->vbuf =
  372. list_first_entry(&cxdev->buflist,
  373. struct cxusb_medion_vbuffer,
  374. list);
  375. list_del(&cxdev->vbuf->list);
  376. } else {
  377. dev_warn(&dvbdev->udev->dev, "no free buffers\n");
  378. }
  379. }
  380. if (bt656->mode == NEW_FRAME || reset) {
  381. cxusb_vprintk(dvbdev, URB, "will copy field 1\n");
  382. bt656->pos = 0;
  383. bt656->mode = FIRST_FIELD;
  384. bt656->fmode = START_SEARCH;
  385. bt656->line = 0;
  386. if (cxdev->vbuf) {
  387. cxdev->vbuf->vb2.vb2_buf.timestamp = ktime_get_ns();
  388. bt656->buf = vb2_plane_vaddr(&cxdev->vbuf->vb2.vb2_buf,
  389. 0);
  390. }
  391. }
  392. if (bt656->mode == FIRST_FIELD) {
  393. if (!cxusb_medion_copy_field(dvbdev, &cxdev->auxbuf, bt656,
  394. true, cxdev->height / 2,
  395. cxdev->width * 2))
  396. return false;
  397. /*
  398. * do not trim buffer there in case
  399. * we need to reset the search later
  400. */
  401. cxusb_vprintk(dvbdev, URB, "will copy field 2\n");
  402. bt656->mode = SECOND_FIELD;
  403. bt656->fmode = START_SEARCH;
  404. bt656->line = 0;
  405. }
  406. if (bt656->mode == SECOND_FIELD) {
  407. if (!cxusb_medion_copy_field(dvbdev, &cxdev->auxbuf, bt656,
  408. false, cxdev->height / 2,
  409. cxdev->width * 2))
  410. return false;
  411. cxusb_auxbuf_head_trim(dvbdev, &cxdev->auxbuf, bt656->pos);
  412. bt656->mode = NEW_FRAME;
  413. if (cxdev->vbuf) {
  414. vb2_set_plane_payload(&cxdev->vbuf->vb2.vb2_buf, 0,
  415. cxdev->width * cxdev->height * 2);
  416. cxdev->vbuf->vb2.field = cxdev->field_order;
  417. cxdev->vbuf->vb2.sequence = cxdev->vbuf_sequence++;
  418. vb2_buffer_done(&cxdev->vbuf->vb2.vb2_buf,
  419. VB2_BUF_STATE_DONE);
  420. cxdev->vbuf = NULL;
  421. cxdev->bt656.buf = NULL;
  422. cxusb_vprintk(dvbdev, URB, "frame done\n");
  423. } else {
  424. cxusb_vprintk(dvbdev, URB, "frame skipped\n");
  425. cxdev->vbuf_sequence++;
  426. }
  427. }
  428. return true;
  429. }
  430. static bool cxusb_medion_v_complete_handle_urb(struct cxusb_medion_dev *cxdev,
  431. bool *auxbuf_reset)
  432. {
  433. struct dvb_usb_device *dvbdev = cxdev->dvbdev;
  434. unsigned int urbn;
  435. struct urb *urb;
  436. int ret;
  437. *auxbuf_reset = false;
  438. urbn = cxdev->nexturb;
  439. if (!test_bit(urbn, &cxdev->urbcomplete))
  440. return false;
  441. clear_bit(urbn, &cxdev->urbcomplete);
  442. do {
  443. cxdev->nexturb++;
  444. cxdev->nexturb %= CXUSB_VIDEO_URBS;
  445. urb = cxdev->streamurbs[cxdev->nexturb];
  446. } while (!urb);
  447. urb = cxdev->streamurbs[urbn];
  448. cxusb_vprintk(dvbdev, URB, "URB %u status = %d\n", urbn, urb->status);
  449. if (urb->status == 0 || urb->status == -EXDEV) {
  450. int i;
  451. unsigned long len;
  452. for (i = 0, len = 0; i < urb->number_of_packets; i++)
  453. len += urb->iso_frame_desc[i].actual_length;
  454. cxusb_vprintk(dvbdev, URB, "URB %u data len = %lu\n", urbn,
  455. len);
  456. if (len > 0) {
  457. cxusb_vprintk(dvbdev, URB, "appending URB\n");
  458. /*
  459. * append new data to auxbuf while
  460. * overwriting old data if necessary
  461. *
  462. * if any overwrite happens then we can no
  463. * longer rely on consistency of the whole
  464. * data so let's start again the current
  465. * auxbuf frame assembling process from
  466. * the beginning
  467. */
  468. *auxbuf_reset =
  469. !cxusb_auxbuf_append_urb(dvbdev,
  470. &cxdev->auxbuf,
  471. urb);
  472. }
  473. }
  474. cxusb_vprintk(dvbdev, URB, "URB %u resubmit\n", urbn);
  475. ret = usb_submit_urb(urb, GFP_KERNEL);
  476. if (ret != 0)
  477. dev_err(&dvbdev->udev->dev,
  478. "unable to resubmit URB %u (%d), you'll have to restart streaming\n",
  479. urbn, ret);
  480. /* next URB is complete already? reschedule us then to handle it */
  481. return test_bit(cxdev->nexturb, &cxdev->urbcomplete);
  482. }
  483. static void cxusb_medion_v_complete_work(struct work_struct *work)
  484. {
  485. struct cxusb_medion_dev *cxdev = container_of(work,
  486. struct cxusb_medion_dev,
  487. urbwork);
  488. struct dvb_usb_device *dvbdev = cxdev->dvbdev;
  489. bool auxbuf_reset;
  490. bool reschedule;
  491. mutex_lock(cxdev->videodev->lock);
  492. cxusb_vprintk(dvbdev, URB, "worker called, stop_streaming = %d\n",
  493. (int)cxdev->stop_streaming);
  494. if (cxdev->stop_streaming)
  495. goto unlock;
  496. reschedule = cxusb_medion_v_complete_handle_urb(cxdev, &auxbuf_reset);
  497. if (cxusb_medion_v_process_auxbuf(cxdev, auxbuf_reset))
  498. /* reschedule us until auxbuf no longer can produce any frame */
  499. reschedule = true;
  500. if (reschedule) {
  501. cxusb_vprintk(dvbdev, URB, "rescheduling worker\n");
  502. schedule_work(&cxdev->urbwork);
  503. }
  504. unlock:
  505. mutex_unlock(cxdev->videodev->lock);
  506. }
  507. static void cxusb_medion_v_complete(struct urb *u)
  508. {
  509. struct dvb_usb_device *dvbdev = u->context;
  510. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  511. unsigned int i;
  512. for (i = 0; i < CXUSB_VIDEO_URBS; i++)
  513. if (cxdev->streamurbs[i] == u)
  514. break;
  515. if (i >= CXUSB_VIDEO_URBS) {
  516. dev_err(&dvbdev->udev->dev,
  517. "complete on unknown URB\n");
  518. return;
  519. }
  520. cxusb_vprintk(dvbdev, URB, "URB %u complete\n", i);
  521. set_bit(i, &cxdev->urbcomplete);
  522. schedule_work(&cxdev->urbwork);
  523. }
  524. static void cxusb_medion_urbs_free(struct cxusb_medion_dev *cxdev)
  525. {
  526. unsigned int i;
  527. for (i = 0; i < CXUSB_VIDEO_URBS; i++)
  528. if (cxdev->streamurbs[i]) {
  529. kfree(cxdev->streamurbs[i]->transfer_buffer);
  530. usb_free_urb(cxdev->streamurbs[i]);
  531. cxdev->streamurbs[i] = NULL;
  532. }
  533. }
  534. static void cxusb_medion_return_buffers(struct cxusb_medion_dev *cxdev,
  535. bool requeue)
  536. {
  537. struct cxusb_medion_vbuffer *vbuf, *vbuf_tmp;
  538. list_for_each_entry_safe(vbuf, vbuf_tmp, &cxdev->buflist,
  539. list) {
  540. list_del(&vbuf->list);
  541. vb2_buffer_done(&vbuf->vb2.vb2_buf,
  542. requeue ? VB2_BUF_STATE_QUEUED :
  543. VB2_BUF_STATE_ERROR);
  544. }
  545. if (cxdev->vbuf) {
  546. vb2_buffer_done(&cxdev->vbuf->vb2.vb2_buf,
  547. requeue ? VB2_BUF_STATE_QUEUED :
  548. VB2_BUF_STATE_ERROR);
  549. cxdev->vbuf = NULL;
  550. cxdev->bt656.buf = NULL;
  551. }
  552. }
  553. static int cxusb_medion_v_ss_auxbuf_alloc(struct cxusb_medion_dev *cxdev,
  554. int *npackets)
  555. {
  556. struct dvb_usb_device *dvbdev = cxdev->dvbdev;
  557. u8 *buf;
  558. unsigned int framelen, urblen, auxbuflen;
  559. framelen = (cxdev->width * 2 + 4 + 4) *
  560. (cxdev->height + 50 /* VBI lines */);
  561. /*
  562. * try to fit a whole frame into each URB, as long as doing so
  563. * does not require very high order memory allocations
  564. */
  565. BUILD_BUG_ON(CXUSB_VIDEO_URB_MAX_SIZE / CXUSB_VIDEO_PKT_SIZE >
  566. CXUSB_VIDEO_MAX_FRAME_PKTS);
  567. *npackets = min_t(int, (framelen + CXUSB_VIDEO_PKT_SIZE - 1) /
  568. CXUSB_VIDEO_PKT_SIZE,
  569. CXUSB_VIDEO_URB_MAX_SIZE / CXUSB_VIDEO_PKT_SIZE);
  570. urblen = *npackets * CXUSB_VIDEO_PKT_SIZE;
  571. cxusb_vprintk(dvbdev, URB,
  572. "each URB will have %d packets for total of %u bytes (%u x %u @ %u)\n",
  573. *npackets, urblen, (unsigned int)cxdev->width,
  574. (unsigned int)cxdev->height, framelen);
  575. auxbuflen = framelen + urblen;
  576. buf = vmalloc(auxbuflen);
  577. if (!buf)
  578. return -ENOMEM;
  579. cxusb_auxbuf_init(dvbdev, &cxdev->auxbuf, buf, auxbuflen);
  580. return 0;
  581. }
  582. static u32 cxusb_medion_norm2field_order(v4l2_std_id norm)
  583. {
  584. bool is625 = norm & V4L2_STD_625_50;
  585. bool is525 = norm & V4L2_STD_525_60;
  586. if (!is625 && !is525)
  587. return V4L2_FIELD_NONE;
  588. if (is625 && is525)
  589. return V4L2_FIELD_NONE;
  590. if (is625)
  591. return V4L2_FIELD_SEQ_TB;
  592. else /* is525 */
  593. return V4L2_FIELD_SEQ_BT;
  594. }
  595. static u32 cxusb_medion_field_order(struct cxusb_medion_dev *cxdev)
  596. {
  597. struct dvb_usb_device *dvbdev = cxdev->dvbdev;
  598. u32 field;
  599. int ret;
  600. v4l2_std_id norm;
  601. /* TV tuner is PAL-only so it is always TB */
  602. if (cxdev->input == 0)
  603. return V4L2_FIELD_SEQ_TB;
  604. field = cxusb_medion_norm2field_order(cxdev->norm);
  605. if (field != V4L2_FIELD_NONE)
  606. return field;
  607. ret = v4l2_subdev_call(cxdev->cx25840, video, g_std, &norm);
  608. if (ret != 0) {
  609. cxusb_vprintk(dvbdev, OPS,
  610. "cannot get current standard for input %u\n",
  611. (unsigned int)cxdev->input);
  612. } else {
  613. field = cxusb_medion_norm2field_order(norm);
  614. if (field != V4L2_FIELD_NONE)
  615. return field;
  616. }
  617. dev_warn(&dvbdev->udev->dev,
  618. "cannot determine field order for the current standard setup and received signal, using TB\n");
  619. return V4L2_FIELD_SEQ_TB;
  620. }
  621. static int cxusb_medion_v_start_streaming(struct vb2_queue *q,
  622. unsigned int count)
  623. {
  624. struct dvb_usb_device *dvbdev = vb2_get_drv_priv(q);
  625. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  626. u8 streamon_params[2] = { 0x03, 0x00 };
  627. int npackets, i;
  628. int ret;
  629. cxusb_vprintk(dvbdev, OPS, "should start streaming\n");
  630. if (cxdev->stop_streaming) {
  631. /* stream is being stopped */
  632. ret = -EBUSY;
  633. goto ret_retbufs;
  634. }
  635. cxdev->field_order = cxusb_medion_field_order(cxdev);
  636. ret = v4l2_subdev_call(cxdev->cx25840, video, s_stream, 1);
  637. if (ret != 0) {
  638. dev_err(&dvbdev->udev->dev,
  639. "unable to start stream (%d)\n", ret);
  640. goto ret_retbufs;
  641. }
  642. ret = cxusb_ctrl_msg(dvbdev, CMD_STREAMING_ON, streamon_params, 2,
  643. NULL, 0);
  644. if (ret != 0) {
  645. dev_err(&dvbdev->udev->dev,
  646. "unable to start streaming (%d)\n", ret);
  647. goto ret_unstream_cx;
  648. }
  649. ret = cxusb_medion_v_ss_auxbuf_alloc(cxdev, &npackets);
  650. if (ret != 0)
  651. goto ret_unstream_md;
  652. for (i = 0; i < CXUSB_VIDEO_URBS; i++) {
  653. int framen;
  654. u8 *streambuf;
  655. struct urb *surb;
  656. /*
  657. * TODO: change this to an array of single pages to avoid
  658. * doing a large continuous allocation when (if)
  659. * s-g isochronous USB transfers are supported
  660. */
  661. streambuf = kmalloc(npackets * CXUSB_VIDEO_PKT_SIZE,
  662. GFP_KERNEL);
  663. if (!streambuf) {
  664. if (i < 2) {
  665. ret = -ENOMEM;
  666. goto ret_freeab;
  667. }
  668. break;
  669. }
  670. surb = usb_alloc_urb(npackets, GFP_KERNEL);
  671. if (!surb) {
  672. kfree(streambuf);
  673. ret = -ENOMEM;
  674. goto ret_freeu;
  675. }
  676. cxdev->streamurbs[i] = surb;
  677. surb->dev = dvbdev->udev;
  678. surb->context = dvbdev;
  679. surb->pipe = usb_rcvisocpipe(dvbdev->udev, 2);
  680. surb->interval = 1;
  681. surb->transfer_flags = URB_ISO_ASAP;
  682. surb->transfer_buffer = streambuf;
  683. surb->complete = cxusb_medion_v_complete;
  684. surb->number_of_packets = npackets;
  685. surb->transfer_buffer_length = npackets * CXUSB_VIDEO_PKT_SIZE;
  686. for (framen = 0; framen < npackets; framen++) {
  687. surb->iso_frame_desc[framen].offset =
  688. CXUSB_VIDEO_PKT_SIZE * framen;
  689. surb->iso_frame_desc[framen].length =
  690. CXUSB_VIDEO_PKT_SIZE;
  691. }
  692. }
  693. cxdev->urbcomplete = 0;
  694. cxdev->nexturb = 0;
  695. cxdev->vbuf_sequence = 0;
  696. cxdev->vbuf = NULL;
  697. cxdev->bt656.mode = NEW_FRAME;
  698. cxdev->bt656.buf = NULL;
  699. for (i = 0; i < CXUSB_VIDEO_URBS; i++)
  700. if (cxdev->streamurbs[i]) {
  701. ret = usb_submit_urb(cxdev->streamurbs[i],
  702. GFP_KERNEL);
  703. if (ret != 0)
  704. dev_err(&dvbdev->udev->dev,
  705. "URB %d submission failed (%d)\n", i,
  706. ret);
  707. }
  708. return 0;
  709. ret_freeu:
  710. cxusb_medion_urbs_free(cxdev);
  711. ret_freeab:
  712. vfree(cxdev->auxbuf.buf);
  713. ret_unstream_md:
  714. cxusb_ctrl_msg(dvbdev, CMD_STREAMING_OFF, NULL, 0, NULL, 0);
  715. ret_unstream_cx:
  716. v4l2_subdev_call(cxdev->cx25840, video, s_stream, 0);
  717. ret_retbufs:
  718. cxusb_medion_return_buffers(cxdev, true);
  719. return ret;
  720. }
  721. static void cxusb_medion_v_stop_streaming(struct vb2_queue *q)
  722. {
  723. struct dvb_usb_device *dvbdev = vb2_get_drv_priv(q);
  724. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  725. int ret;
  726. unsigned int i;
  727. cxusb_vprintk(dvbdev, OPS, "should stop streaming\n");
  728. if (WARN_ON(cxdev->stop_streaming))
  729. return;
  730. cxdev->stop_streaming = true;
  731. cxusb_ctrl_msg(dvbdev, CMD_STREAMING_OFF, NULL, 0, NULL, 0);
  732. ret = v4l2_subdev_call(cxdev->cx25840, video, s_stream, 0);
  733. if (ret != 0)
  734. dev_err(&dvbdev->udev->dev, "unable to stop stream (%d)\n",
  735. ret);
  736. /* let URB completion run */
  737. mutex_unlock(cxdev->videodev->lock);
  738. for (i = 0; i < CXUSB_VIDEO_URBS; i++)
  739. if (cxdev->streamurbs[i])
  740. usb_kill_urb(cxdev->streamurbs[i]);
  741. flush_work(&cxdev->urbwork);
  742. mutex_lock(cxdev->videodev->lock);
  743. /* free transfer buffer and URB */
  744. vfree(cxdev->auxbuf.buf);
  745. cxusb_medion_urbs_free(cxdev);
  746. cxusb_medion_return_buffers(cxdev, false);
  747. cxdev->stop_streaming = false;
  748. }
  749. static void cxusub_medion_v_buf_queue(struct vb2_buffer *vb)
  750. {
  751. struct vb2_v4l2_buffer *v4l2buf = to_vb2_v4l2_buffer(vb);
  752. struct cxusb_medion_vbuffer *vbuf =
  753. container_of(v4l2buf, struct cxusb_medion_vbuffer, vb2);
  754. struct dvb_usb_device *dvbdev = vb2_get_drv_priv(vb->vb2_queue);
  755. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  756. /* cxusb_vprintk(dvbdev, OPS, "mmmm.. a fresh buffer...\n"); */
  757. list_add_tail(&vbuf->list, &cxdev->buflist);
  758. }
  759. static const struct vb2_ops cxdev_video_qops = {
  760. .queue_setup = cxusb_medion_v_queue_setup,
  761. .buf_init = cxusb_medion_v_buf_init,
  762. .start_streaming = cxusb_medion_v_start_streaming,
  763. .stop_streaming = cxusb_medion_v_stop_streaming,
  764. .buf_queue = cxusub_medion_v_buf_queue,
  765. .wait_prepare = vb2_ops_wait_prepare,
  766. .wait_finish = vb2_ops_wait_finish
  767. };
  768. static const __u32 videocaps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
  769. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  770. static const __u32 radiocaps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  771. static int cxusb_medion_v_querycap(struct file *file, void *fh,
  772. struct v4l2_capability *cap)
  773. {
  774. struct dvb_usb_device *dvbdev = video_drvdata(file);
  775. strscpy(cap->driver, dvbdev->udev->dev.driver->name,
  776. sizeof(cap->driver));
  777. strscpy(cap->card, "Medion 95700", sizeof(cap->card));
  778. usb_make_path(dvbdev->udev, cap->bus_info, sizeof(cap->bus_info));
  779. cap->capabilities = videocaps | radiocaps | V4L2_CAP_DEVICE_CAPS;
  780. return 0;
  781. }
  782. static int cxusb_medion_v_enum_fmt_vid_cap(struct file *file, void *fh,
  783. struct v4l2_fmtdesc *f)
  784. {
  785. if (f->index != 0)
  786. return -EINVAL;
  787. f->pixelformat = V4L2_PIX_FMT_UYVY;
  788. return 0;
  789. }
  790. static int cxusb_medion_g_fmt_vid_cap(struct file *file, void *fh,
  791. struct v4l2_format *f)
  792. {
  793. struct dvb_usb_device *dvbdev = video_drvdata(file);
  794. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  795. f->fmt.pix.width = cxdev->width;
  796. f->fmt.pix.height = cxdev->height;
  797. f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  798. f->fmt.pix.field = vb2_start_streaming_called(&cxdev->videoqueue) ?
  799. cxdev->field_order : cxusb_medion_field_order(cxdev);
  800. f->fmt.pix.bytesperline = cxdev->width * 2;
  801. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  802. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
  803. return 0;
  804. }
  805. static int cxusb_medion_try_s_fmt_vid_cap(struct file *file,
  806. struct v4l2_format *f,
  807. bool isset)
  808. {
  809. struct dvb_usb_device *dvbdev = video_drvdata(file);
  810. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  811. struct v4l2_subdev_format subfmt = {
  812. .which = isset ? V4L2_SUBDEV_FORMAT_ACTIVE :
  813. V4L2_SUBDEV_FORMAT_TRY,
  814. };
  815. u32 field;
  816. int ret;
  817. if (isset && vb2_is_busy(&cxdev->videoqueue))
  818. return -EBUSY;
  819. field = vb2_start_streaming_called(&cxdev->videoqueue) ?
  820. cxdev->field_order : cxusb_medion_field_order(cxdev);
  821. subfmt.format.width = f->fmt.pix.width & ~1;
  822. subfmt.format.height = f->fmt.pix.height & ~1;
  823. subfmt.format.code = MEDIA_BUS_FMT_FIXED;
  824. subfmt.format.field = field;
  825. subfmt.format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  826. ret = v4l2_subdev_call(cxdev->cx25840, pad, set_fmt, NULL, &subfmt);
  827. if (ret != 0)
  828. return ret;
  829. f->fmt.pix.width = subfmt.format.width;
  830. f->fmt.pix.height = subfmt.format.height;
  831. f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  832. f->fmt.pix.field = field;
  833. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  834. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
  835. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  836. if (isset) {
  837. cxdev->width = f->fmt.pix.width;
  838. cxdev->height = f->fmt.pix.height;
  839. }
  840. return 0;
  841. }
  842. static int cxusb_medion_try_fmt_vid_cap(struct file *file, void *fh,
  843. struct v4l2_format *f)
  844. {
  845. return cxusb_medion_try_s_fmt_vid_cap(file, f, false);
  846. }
  847. static int cxusb_medion_s_fmt_vid_cap(struct file *file, void *fh,
  848. struct v4l2_format *f)
  849. {
  850. return cxusb_medion_try_s_fmt_vid_cap(file, f, true);
  851. }
  852. static const struct {
  853. struct v4l2_input input;
  854. u32 inputcfg;
  855. } cxusb_medion_inputs[] = {
  856. { .input = { .name = "TV tuner", .type = V4L2_INPUT_TYPE_TUNER,
  857. .tuner = 0, .std = V4L2_STD_PAL },
  858. .inputcfg = CX25840_COMPOSITE2, },
  859. { .input = { .name = "Composite", .type = V4L2_INPUT_TYPE_CAMERA,
  860. .std = V4L2_STD_ALL },
  861. .inputcfg = CX25840_COMPOSITE1, },
  862. { .input = { .name = "S-Video", .type = V4L2_INPUT_TYPE_CAMERA,
  863. .std = V4L2_STD_ALL },
  864. .inputcfg = CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 }
  865. };
  866. #define CXUSB_INPUT_CNT ARRAY_SIZE(cxusb_medion_inputs)
  867. static int cxusb_medion_enum_input(struct file *file, void *fh,
  868. struct v4l2_input *inp)
  869. {
  870. struct dvb_usb_device *dvbdev = video_drvdata(file);
  871. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  872. u32 index = inp->index;
  873. if (index >= CXUSB_INPUT_CNT)
  874. return -EINVAL;
  875. *inp = cxusb_medion_inputs[index].input;
  876. inp->index = index;
  877. inp->capabilities |= V4L2_IN_CAP_STD;
  878. if (index == cxdev->input) {
  879. int ret;
  880. u32 status = 0;
  881. ret = v4l2_subdev_call(cxdev->cx25840, video, g_input_status,
  882. &status);
  883. if (ret != 0)
  884. dev_warn(&dvbdev->udev->dev,
  885. "cx25840 input status query failed (%d)\n",
  886. ret);
  887. else
  888. inp->status = status;
  889. }
  890. return 0;
  891. }
  892. static int cxusb_medion_g_input(struct file *file, void *fh,
  893. unsigned int *i)
  894. {
  895. struct dvb_usb_device *dvbdev = video_drvdata(file);
  896. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  897. *i = cxdev->input;
  898. return 0;
  899. }
  900. static int cxusb_medion_set_norm(struct cxusb_medion_dev *cxdev,
  901. v4l2_std_id norm)
  902. {
  903. struct dvb_usb_device *dvbdev = cxdev->dvbdev;
  904. int ret;
  905. cxusb_vprintk(dvbdev, OPS,
  906. "trying to set standard for input %u to %lx\n",
  907. (unsigned int)cxdev->input,
  908. (unsigned long)norm);
  909. /* no autodetection support */
  910. if (norm == V4L2_STD_UNKNOWN)
  911. return -EINVAL;
  912. /* on composite or S-Video any std is acceptable */
  913. if (cxdev->input != 0) {
  914. ret = v4l2_subdev_call(cxdev->cx25840, video, s_std, norm);
  915. if (ret)
  916. return ret;
  917. goto ret_savenorm;
  918. }
  919. /* TV tuner is only able to demodulate PAL */
  920. if ((norm & ~V4L2_STD_PAL) != 0)
  921. return -EINVAL;
  922. ret = v4l2_subdev_call(cxdev->tda9887, video, s_std, norm);
  923. if (ret != 0) {
  924. dev_err(&dvbdev->udev->dev,
  925. "tda9887 norm setup failed (%d)\n",
  926. ret);
  927. return ret;
  928. }
  929. ret = v4l2_subdev_call(cxdev->tuner, video, s_std, norm);
  930. if (ret != 0) {
  931. dev_err(&dvbdev->udev->dev,
  932. "tuner norm setup failed (%d)\n",
  933. ret);
  934. return ret;
  935. }
  936. ret = v4l2_subdev_call(cxdev->cx25840, video, s_std, norm);
  937. if (ret != 0) {
  938. dev_err(&dvbdev->udev->dev,
  939. "cx25840 norm setup failed (%d)\n",
  940. ret);
  941. return ret;
  942. }
  943. ret_savenorm:
  944. cxdev->norm = norm;
  945. return 0;
  946. }
  947. static int cxusb_medion_s_input(struct file *file, void *fh,
  948. unsigned int i)
  949. {
  950. struct dvb_usb_device *dvbdev = video_drvdata(file);
  951. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  952. int ret;
  953. v4l2_std_id norm;
  954. if (i >= CXUSB_INPUT_CNT)
  955. return -EINVAL;
  956. ret = v4l2_subdev_call(cxdev->cx25840, video, s_routing,
  957. cxusb_medion_inputs[i].inputcfg, 0, 0);
  958. if (ret != 0)
  959. return ret;
  960. cxdev->input = i;
  961. cxdev->videodev->tvnorms = cxusb_medion_inputs[i].input.std;
  962. norm = cxdev->norm & cxusb_medion_inputs[i].input.std;
  963. if (norm == 0)
  964. norm = cxusb_medion_inputs[i].input.std;
  965. cxusb_medion_set_norm(cxdev, norm);
  966. return 0;
  967. }
  968. static int cxusb_medion_g_tuner(struct file *file, void *fh,
  969. struct v4l2_tuner *tuner)
  970. {
  971. struct dvb_usb_device *dvbdev = video_drvdata(file);
  972. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  973. struct video_device *vdev = video_devdata(file);
  974. int ret;
  975. if (tuner->index != 0)
  976. return -EINVAL;
  977. if (vdev->vfl_type == VFL_TYPE_VIDEO)
  978. tuner->type = V4L2_TUNER_ANALOG_TV;
  979. else
  980. tuner->type = V4L2_TUNER_RADIO;
  981. tuner->capability = 0;
  982. tuner->afc = 0;
  983. /*
  984. * fills:
  985. * always: capability (static), rangelow (static), rangehigh (static)
  986. * radio mode: afc (may fail silently), rxsubchans (static), audmode
  987. */
  988. ret = v4l2_subdev_call(cxdev->tda9887, tuner, g_tuner, tuner);
  989. if (ret != 0)
  990. return ret;
  991. /*
  992. * fills:
  993. * always: capability (static), rangelow (static), rangehigh (static)
  994. * radio mode: rxsubchans (always stereo), audmode,
  995. * signal (might be wrong)
  996. */
  997. ret = v4l2_subdev_call(cxdev->tuner, tuner, g_tuner, tuner);
  998. if (ret != 0)
  999. return ret;
  1000. tuner->signal = 0;
  1001. /*
  1002. * fills: TV mode: capability, rxsubchans, audmode, signal
  1003. */
  1004. ret = v4l2_subdev_call(cxdev->cx25840, tuner, g_tuner, tuner);
  1005. if (ret != 0)
  1006. return ret;
  1007. if (vdev->vfl_type == VFL_TYPE_VIDEO)
  1008. strscpy(tuner->name, "TV tuner", sizeof(tuner->name));
  1009. else
  1010. strscpy(tuner->name, "Radio tuner", sizeof(tuner->name));
  1011. memset(tuner->reserved, 0, sizeof(tuner->reserved));
  1012. return 0;
  1013. }
  1014. static int cxusb_medion_s_tuner(struct file *file, void *fh,
  1015. const struct v4l2_tuner *tuner)
  1016. {
  1017. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1018. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1019. struct video_device *vdev = video_devdata(file);
  1020. int ret;
  1021. if (tuner->index != 0)
  1022. return -EINVAL;
  1023. ret = v4l2_subdev_call(cxdev->tda9887, tuner, s_tuner, tuner);
  1024. if (ret != 0)
  1025. return ret;
  1026. ret = v4l2_subdev_call(cxdev->tuner, tuner, s_tuner, tuner);
  1027. if (ret != 0)
  1028. return ret;
  1029. /*
  1030. * make sure that cx25840 is in a correct TV / radio mode,
  1031. * since calls above may have changed it for tuner / IF demod
  1032. */
  1033. if (vdev->vfl_type == VFL_TYPE_VIDEO)
  1034. v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
  1035. else
  1036. v4l2_subdev_call(cxdev->cx25840, tuner, s_radio);
  1037. return v4l2_subdev_call(cxdev->cx25840, tuner, s_tuner, tuner);
  1038. }
  1039. static int cxusb_medion_g_frequency(struct file *file, void *fh,
  1040. struct v4l2_frequency *freq)
  1041. {
  1042. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1043. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1044. if (freq->tuner != 0)
  1045. return -EINVAL;
  1046. return v4l2_subdev_call(cxdev->tuner, tuner, g_frequency, freq);
  1047. }
  1048. static int cxusb_medion_s_frequency(struct file *file, void *fh,
  1049. const struct v4l2_frequency *freq)
  1050. {
  1051. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1052. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1053. struct video_device *vdev = video_devdata(file);
  1054. int ret;
  1055. if (freq->tuner != 0)
  1056. return -EINVAL;
  1057. ret = v4l2_subdev_call(cxdev->tda9887, tuner, s_frequency, freq);
  1058. if (ret != 0)
  1059. return ret;
  1060. ret = v4l2_subdev_call(cxdev->tuner, tuner, s_frequency, freq);
  1061. if (ret != 0)
  1062. return ret;
  1063. /*
  1064. * make sure that cx25840 is in a correct TV / radio mode,
  1065. * since calls above may have changed it for tuner / IF demod
  1066. */
  1067. if (vdev->vfl_type == VFL_TYPE_VIDEO)
  1068. v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
  1069. else
  1070. v4l2_subdev_call(cxdev->cx25840, tuner, s_radio);
  1071. return v4l2_subdev_call(cxdev->cx25840, tuner, s_frequency, freq);
  1072. }
  1073. static int cxusb_medion_g_std(struct file *file, void *fh,
  1074. v4l2_std_id *norm)
  1075. {
  1076. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1077. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1078. *norm = cxdev->norm;
  1079. if (*norm == V4L2_STD_UNKNOWN)
  1080. return -ENODATA;
  1081. return 0;
  1082. }
  1083. static int cxusb_medion_s_std(struct file *file, void *fh,
  1084. v4l2_std_id norm)
  1085. {
  1086. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1087. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1088. return cxusb_medion_set_norm(cxdev, norm);
  1089. }
  1090. static int cxusb_medion_querystd(struct file *file, void *fh,
  1091. v4l2_std_id *norm)
  1092. {
  1093. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1094. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1095. v4l2_std_id norm_mask;
  1096. int ret;
  1097. /*
  1098. * make sure we don't have improper std bits set for the TV tuner
  1099. * (could happen when no signal was present yet after reset)
  1100. */
  1101. if (cxdev->input == 0)
  1102. norm_mask = V4L2_STD_PAL;
  1103. else
  1104. norm_mask = V4L2_STD_ALL;
  1105. ret = v4l2_subdev_call(cxdev->cx25840, video, querystd, norm);
  1106. if (ret != 0) {
  1107. cxusb_vprintk(dvbdev, OPS,
  1108. "cannot get detected standard for input %u\n",
  1109. (unsigned int)cxdev->input);
  1110. return ret;
  1111. }
  1112. cxusb_vprintk(dvbdev, OPS, "input %u detected standard is %lx\n",
  1113. (unsigned int)cxdev->input, (unsigned long)*norm);
  1114. *norm &= norm_mask;
  1115. return 0;
  1116. }
  1117. static int cxusb_medion_log_status(struct file *file, void *fh)
  1118. {
  1119. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1120. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1121. v4l2_device_call_all(&cxdev->v4l2dev, 0, core, log_status);
  1122. return 0;
  1123. }
  1124. static const struct v4l2_ioctl_ops cxusb_video_ioctl = {
  1125. .vidioc_querycap = cxusb_medion_v_querycap,
  1126. .vidioc_enum_fmt_vid_cap = cxusb_medion_v_enum_fmt_vid_cap,
  1127. .vidioc_g_fmt_vid_cap = cxusb_medion_g_fmt_vid_cap,
  1128. .vidioc_s_fmt_vid_cap = cxusb_medion_s_fmt_vid_cap,
  1129. .vidioc_try_fmt_vid_cap = cxusb_medion_try_fmt_vid_cap,
  1130. .vidioc_enum_input = cxusb_medion_enum_input,
  1131. .vidioc_g_input = cxusb_medion_g_input,
  1132. .vidioc_s_input = cxusb_medion_s_input,
  1133. .vidioc_g_tuner = cxusb_medion_g_tuner,
  1134. .vidioc_s_tuner = cxusb_medion_s_tuner,
  1135. .vidioc_g_frequency = cxusb_medion_g_frequency,
  1136. .vidioc_s_frequency = cxusb_medion_s_frequency,
  1137. .vidioc_g_std = cxusb_medion_g_std,
  1138. .vidioc_s_std = cxusb_medion_s_std,
  1139. .vidioc_querystd = cxusb_medion_querystd,
  1140. .vidioc_log_status = cxusb_medion_log_status,
  1141. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1142. .vidioc_querybuf = vb2_ioctl_querybuf,
  1143. .vidioc_qbuf = vb2_ioctl_qbuf,
  1144. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1145. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1146. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1147. .vidioc_streamon = vb2_ioctl_streamon,
  1148. .vidioc_streamoff = vb2_ioctl_streamoff
  1149. };
  1150. static const struct v4l2_ioctl_ops cxusb_radio_ioctl = {
  1151. .vidioc_querycap = cxusb_medion_v_querycap,
  1152. .vidioc_g_tuner = cxusb_medion_g_tuner,
  1153. .vidioc_s_tuner = cxusb_medion_s_tuner,
  1154. .vidioc_g_frequency = cxusb_medion_g_frequency,
  1155. .vidioc_s_frequency = cxusb_medion_s_frequency,
  1156. .vidioc_log_status = cxusb_medion_log_status
  1157. };
  1158. /*
  1159. * in principle, this should be const, but s_io_pin_config is declared
  1160. * to take non-const, and gcc complains
  1161. */
  1162. static struct v4l2_subdev_io_pin_config cxusub_medion_pin_config[] = {
  1163. { .pin = CX25840_PIN_DVALID_PRGM0, .function = CX25840_PAD_DEFAULT,
  1164. .strength = CX25840_PIN_DRIVE_MEDIUM },
  1165. { .pin = CX25840_PIN_PLL_CLK_PRGM7, .function = CX25840_PAD_AUX_PLL },
  1166. { .pin = CX25840_PIN_HRESET_PRGM2, .function = CX25840_PAD_ACTIVE,
  1167. .strength = CX25840_PIN_DRIVE_MEDIUM }
  1168. };
  1169. int cxusb_medion_analog_init(struct dvb_usb_device *dvbdev)
  1170. {
  1171. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1172. u8 tuner_analog_msg_data[] = { 0x9c, 0x60, 0x85, 0x54 };
  1173. struct i2c_msg tuner_analog_msg = { .addr = 0x61, .flags = 0,
  1174. .buf = tuner_analog_msg_data,
  1175. .len =
  1176. sizeof(tuner_analog_msg_data) };
  1177. struct v4l2_subdev_format subfmt = {
  1178. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1179. };
  1180. int ret;
  1181. /* switch tuner to analog mode so IF demod will become accessible */
  1182. ret = i2c_transfer(&dvbdev->i2c_adap, &tuner_analog_msg, 1);
  1183. if (ret != 1)
  1184. dev_warn(&dvbdev->udev->dev,
  1185. "tuner analog switch failed (%d)\n", ret);
  1186. /*
  1187. * cx25840 might have lost power during mode switching so we need
  1188. * to set it again
  1189. */
  1190. ret = v4l2_subdev_call(cxdev->cx25840, core, reset, 0);
  1191. if (ret != 0)
  1192. dev_warn(&dvbdev->udev->dev,
  1193. "cx25840 reset failed (%d)\n", ret);
  1194. ret = v4l2_subdev_call(cxdev->cx25840, video, s_routing,
  1195. CX25840_COMPOSITE1, 0, 0);
  1196. if (ret != 0)
  1197. dev_warn(&dvbdev->udev->dev,
  1198. "cx25840 initial input setting failed (%d)\n", ret);
  1199. /* composite */
  1200. cxdev->input = 1;
  1201. cxdev->videodev->tvnorms = V4L2_STD_ALL;
  1202. cxdev->norm = V4L2_STD_PAL;
  1203. /* TODO: setup audio samples insertion */
  1204. ret = v4l2_subdev_call(cxdev->cx25840, core, s_io_pin_config,
  1205. ARRAY_SIZE(cxusub_medion_pin_config),
  1206. cxusub_medion_pin_config);
  1207. if (ret != 0)
  1208. dev_warn(&dvbdev->udev->dev,
  1209. "cx25840 pin config failed (%d)\n", ret);
  1210. /* make sure that we aren't in radio mode */
  1211. v4l2_subdev_call(cxdev->tda9887, video, s_std, cxdev->norm);
  1212. v4l2_subdev_call(cxdev->tuner, video, s_std, cxdev->norm);
  1213. v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
  1214. subfmt.format.width = cxdev->width;
  1215. subfmt.format.height = cxdev->height;
  1216. subfmt.format.code = MEDIA_BUS_FMT_FIXED;
  1217. subfmt.format.field = V4L2_FIELD_SEQ_TB;
  1218. subfmt.format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1219. ret = v4l2_subdev_call(cxdev->cx25840, pad, set_fmt, NULL, &subfmt);
  1220. if (ret != 0)
  1221. dev_warn(&dvbdev->udev->dev,
  1222. "cx25840 format set failed (%d)\n", ret);
  1223. if (ret == 0) {
  1224. cxdev->width = subfmt.format.width;
  1225. cxdev->height = subfmt.format.height;
  1226. }
  1227. return 0;
  1228. }
  1229. static int cxusb_videoradio_open(struct file *f)
  1230. {
  1231. struct dvb_usb_device *dvbdev = video_drvdata(f);
  1232. int ret;
  1233. /*
  1234. * no locking needed since this call only modifies analog
  1235. * state if there are no other analog handles currenly
  1236. * opened so ops done via them cannot create a conflict
  1237. */
  1238. ret = cxusb_medion_get(dvbdev, CXUSB_OPEN_ANALOG);
  1239. if (ret != 0)
  1240. return ret;
  1241. ret = v4l2_fh_open(f);
  1242. if (ret != 0)
  1243. goto ret_release;
  1244. cxusb_vprintk(dvbdev, OPS, "got open\n");
  1245. return 0;
  1246. ret_release:
  1247. cxusb_medion_put(dvbdev);
  1248. return ret;
  1249. }
  1250. static int cxusb_videoradio_release(struct file *f)
  1251. {
  1252. struct video_device *vdev = video_devdata(f);
  1253. struct dvb_usb_device *dvbdev = video_drvdata(f);
  1254. int ret;
  1255. cxusb_vprintk(dvbdev, OPS, "got release\n");
  1256. if (vdev->vfl_type == VFL_TYPE_VIDEO)
  1257. ret = vb2_fop_release(f);
  1258. else
  1259. ret = v4l2_fh_release(f);
  1260. cxusb_medion_put(dvbdev);
  1261. return ret;
  1262. }
  1263. static const struct v4l2_file_operations cxusb_video_fops = {
  1264. .owner = THIS_MODULE,
  1265. .read = vb2_fop_read,
  1266. .poll = vb2_fop_poll,
  1267. .unlocked_ioctl = video_ioctl2,
  1268. .mmap = vb2_fop_mmap,
  1269. .open = cxusb_videoradio_open,
  1270. .release = cxusb_videoradio_release
  1271. };
  1272. static const struct v4l2_file_operations cxusb_radio_fops = {
  1273. .owner = THIS_MODULE,
  1274. .unlocked_ioctl = video_ioctl2,
  1275. .open = cxusb_videoradio_open,
  1276. .release = cxusb_videoradio_release
  1277. };
  1278. static void cxusb_medion_v4l2_release(struct v4l2_device *v4l2_dev)
  1279. {
  1280. struct cxusb_medion_dev *cxdev =
  1281. container_of(v4l2_dev, struct cxusb_medion_dev, v4l2dev);
  1282. struct dvb_usb_device *dvbdev = cxdev->dvbdev;
  1283. cxusb_vprintk(dvbdev, OPS, "v4l2 device release\n");
  1284. v4l2_device_unregister(&cxdev->v4l2dev);
  1285. mutex_destroy(&cxdev->dev_lock);
  1286. while (completion_done(&cxdev->v4l2_release))
  1287. schedule();
  1288. complete(&cxdev->v4l2_release);
  1289. }
  1290. static void cxusb_medion_videodev_release(struct video_device *vdev)
  1291. {
  1292. struct dvb_usb_device *dvbdev = video_get_drvdata(vdev);
  1293. cxusb_vprintk(dvbdev, OPS, "video device release\n");
  1294. video_device_release(vdev);
  1295. }
  1296. static int cxusb_medion_register_analog_video(struct dvb_usb_device *dvbdev)
  1297. {
  1298. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1299. int ret;
  1300. cxdev->videoqueue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1301. cxdev->videoqueue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ |
  1302. VB2_DMABUF;
  1303. cxdev->videoqueue.ops = &cxdev_video_qops;
  1304. cxdev->videoqueue.mem_ops = &vb2_vmalloc_memops;
  1305. cxdev->videoqueue.drv_priv = dvbdev;
  1306. cxdev->videoqueue.buf_struct_size =
  1307. sizeof(struct cxusb_medion_vbuffer);
  1308. cxdev->videoqueue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1309. cxdev->videoqueue.min_buffers_needed = 6;
  1310. cxdev->videoqueue.lock = &cxdev->dev_lock;
  1311. ret = vb2_queue_init(&cxdev->videoqueue);
  1312. if (ret) {
  1313. dev_err(&dvbdev->udev->dev,
  1314. "video queue init failed, ret = %d\n", ret);
  1315. return ret;
  1316. }
  1317. cxdev->videodev = video_device_alloc();
  1318. if (!cxdev->videodev) {
  1319. dev_err(&dvbdev->udev->dev, "video device alloc failed\n");
  1320. return -ENOMEM;
  1321. }
  1322. cxdev->videodev->device_caps = videocaps;
  1323. cxdev->videodev->fops = &cxusb_video_fops;
  1324. cxdev->videodev->v4l2_dev = &cxdev->v4l2dev;
  1325. cxdev->videodev->queue = &cxdev->videoqueue;
  1326. strscpy(cxdev->videodev->name, "cxusb", sizeof(cxdev->videodev->name));
  1327. cxdev->videodev->vfl_dir = VFL_DIR_RX;
  1328. cxdev->videodev->ioctl_ops = &cxusb_video_ioctl;
  1329. cxdev->videodev->tvnorms = V4L2_STD_ALL;
  1330. cxdev->videodev->release = cxusb_medion_videodev_release;
  1331. cxdev->videodev->lock = &cxdev->dev_lock;
  1332. video_set_drvdata(cxdev->videodev, dvbdev);
  1333. ret = video_register_device(cxdev->videodev, VFL_TYPE_VIDEO, -1);
  1334. if (ret) {
  1335. dev_err(&dvbdev->udev->dev,
  1336. "video device register failed, ret = %d\n", ret);
  1337. goto ret_vrelease;
  1338. }
  1339. return 0;
  1340. ret_vrelease:
  1341. video_device_release(cxdev->videodev);
  1342. return ret;
  1343. }
  1344. static int cxusb_medion_register_analog_radio(struct dvb_usb_device *dvbdev)
  1345. {
  1346. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1347. int ret;
  1348. cxdev->radiodev = video_device_alloc();
  1349. if (!cxdev->radiodev) {
  1350. dev_err(&dvbdev->udev->dev, "radio device alloc failed\n");
  1351. return -ENOMEM;
  1352. }
  1353. cxdev->radiodev->device_caps = radiocaps;
  1354. cxdev->radiodev->fops = &cxusb_radio_fops;
  1355. cxdev->radiodev->v4l2_dev = &cxdev->v4l2dev;
  1356. strscpy(cxdev->radiodev->name, "cxusb", sizeof(cxdev->radiodev->name));
  1357. cxdev->radiodev->vfl_dir = VFL_DIR_RX;
  1358. cxdev->radiodev->ioctl_ops = &cxusb_radio_ioctl;
  1359. cxdev->radiodev->release = video_device_release;
  1360. cxdev->radiodev->lock = &cxdev->dev_lock;
  1361. video_set_drvdata(cxdev->radiodev, dvbdev);
  1362. ret = video_register_device(cxdev->radiodev, VFL_TYPE_RADIO, -1);
  1363. if (ret) {
  1364. dev_err(&dvbdev->udev->dev,
  1365. "radio device register failed, ret = %d\n", ret);
  1366. video_device_release(cxdev->radiodev);
  1367. return ret;
  1368. }
  1369. return 0;
  1370. }
  1371. static int cxusb_medion_register_analog_subdevs(struct dvb_usb_device *dvbdev)
  1372. {
  1373. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1374. int ret;
  1375. struct tuner_setup tun_setup;
  1376. /* attach cx25840 capture chip */
  1377. cxdev->cx25840 = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
  1378. &dvbdev->i2c_adap,
  1379. "cx25840", 0x44, NULL);
  1380. if (!cxdev->cx25840) {
  1381. dev_err(&dvbdev->udev->dev, "cx25840 not found\n");
  1382. return -ENODEV;
  1383. }
  1384. /*
  1385. * Initialize cx25840 chip by calling its subdevice init core op.
  1386. *
  1387. * This switches it into the generic mode that disables some of
  1388. * ivtv-related hacks in the cx25840 driver while allowing setting
  1389. * of the chip video output configuration (passed in the call below
  1390. * as the last argument).
  1391. */
  1392. ret = v4l2_subdev_call(cxdev->cx25840, core, init,
  1393. CX25840_VCONFIG_FMT_BT656 |
  1394. CX25840_VCONFIG_RES_8BIT |
  1395. CX25840_VCONFIG_VBIRAW_DISABLED |
  1396. CX25840_VCONFIG_ANCDATA_DISABLED |
  1397. CX25840_VCONFIG_ACTIVE_COMPOSITE |
  1398. CX25840_VCONFIG_VALID_ANDACTIVE |
  1399. CX25840_VCONFIG_HRESETW_NORMAL |
  1400. CX25840_VCONFIG_CLKGATE_NONE |
  1401. CX25840_VCONFIG_DCMODE_DWORDS);
  1402. if (ret != 0) {
  1403. dev_err(&dvbdev->udev->dev,
  1404. "cx25840 init failed (%d)\n", ret);
  1405. return ret;
  1406. }
  1407. /* attach analog tuner */
  1408. cxdev->tuner = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
  1409. &dvbdev->i2c_adap,
  1410. "tuner", 0x61, NULL);
  1411. if (!cxdev->tuner) {
  1412. dev_err(&dvbdev->udev->dev, "tuner not found\n");
  1413. return -ENODEV;
  1414. }
  1415. /* configure it */
  1416. memset(&tun_setup, 0, sizeof(tun_setup));
  1417. tun_setup.addr = 0x61;
  1418. tun_setup.type = TUNER_PHILIPS_FMD1216ME_MK3;
  1419. tun_setup.mode_mask = T_RADIO | T_ANALOG_TV;
  1420. v4l2_subdev_call(cxdev->tuner, tuner, s_type_addr, &tun_setup);
  1421. /* attach IF demod */
  1422. cxdev->tda9887 = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
  1423. &dvbdev->i2c_adap,
  1424. "tuner", 0x43, NULL);
  1425. if (!cxdev->tda9887) {
  1426. dev_err(&dvbdev->udev->dev, "tda9887 not found\n");
  1427. return -ENODEV;
  1428. }
  1429. return 0;
  1430. }
  1431. int cxusb_medion_register_analog(struct dvb_usb_device *dvbdev)
  1432. {
  1433. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1434. int ret;
  1435. mutex_init(&cxdev->dev_lock);
  1436. init_completion(&cxdev->v4l2_release);
  1437. cxdev->v4l2dev.release = cxusb_medion_v4l2_release;
  1438. ret = v4l2_device_register(&dvbdev->udev->dev, &cxdev->v4l2dev);
  1439. if (ret != 0) {
  1440. dev_err(&dvbdev->udev->dev,
  1441. "V4L2 device registration failed, ret = %d\n", ret);
  1442. mutex_destroy(&cxdev->dev_lock);
  1443. return ret;
  1444. }
  1445. ret = cxusb_medion_register_analog_subdevs(dvbdev);
  1446. if (ret)
  1447. goto ret_unregister;
  1448. INIT_WORK(&cxdev->urbwork, cxusb_medion_v_complete_work);
  1449. INIT_LIST_HEAD(&cxdev->buflist);
  1450. cxdev->width = 320;
  1451. cxdev->height = 240;
  1452. ret = cxusb_medion_register_analog_video(dvbdev);
  1453. if (ret)
  1454. goto ret_unregister;
  1455. ret = cxusb_medion_register_analog_radio(dvbdev);
  1456. if (ret)
  1457. goto ret_vunreg;
  1458. return 0;
  1459. ret_vunreg:
  1460. vb2_video_unregister_device(cxdev->videodev);
  1461. ret_unregister:
  1462. v4l2_device_put(&cxdev->v4l2dev);
  1463. wait_for_completion(&cxdev->v4l2_release);
  1464. return ret;
  1465. }
  1466. void cxusb_medion_unregister_analog(struct dvb_usb_device *dvbdev)
  1467. {
  1468. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1469. cxusb_vprintk(dvbdev, OPS, "unregistering analog\n");
  1470. video_unregister_device(cxdev->radiodev);
  1471. vb2_video_unregister_device(cxdev->videodev);
  1472. v4l2_device_put(&cxdev->v4l2dev);
  1473. wait_for_completion(&cxdev->v4l2_release);
  1474. cxusb_vprintk(dvbdev, OPS, "analog unregistered\n");
  1475. }