cxusb-analog.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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 (mail@maciej.szmigiero.name)
  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_array(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. };
  766. static const __u32 videocaps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
  767. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  768. static const __u32 radiocaps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  769. static int cxusb_medion_v_querycap(struct file *file, void *fh,
  770. struct v4l2_capability *cap)
  771. {
  772. struct dvb_usb_device *dvbdev = video_drvdata(file);
  773. strscpy(cap->driver, dvbdev->udev->dev.driver->name,
  774. sizeof(cap->driver));
  775. strscpy(cap->card, "Medion 95700", sizeof(cap->card));
  776. usb_make_path(dvbdev->udev, cap->bus_info, sizeof(cap->bus_info));
  777. cap->capabilities = videocaps | radiocaps | V4L2_CAP_DEVICE_CAPS;
  778. return 0;
  779. }
  780. static int cxusb_medion_v_enum_fmt_vid_cap(struct file *file, void *fh,
  781. struct v4l2_fmtdesc *f)
  782. {
  783. if (f->index != 0)
  784. return -EINVAL;
  785. f->pixelformat = V4L2_PIX_FMT_UYVY;
  786. return 0;
  787. }
  788. static int cxusb_medion_g_fmt_vid_cap(struct file *file, void *fh,
  789. struct v4l2_format *f)
  790. {
  791. struct dvb_usb_device *dvbdev = video_drvdata(file);
  792. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  793. f->fmt.pix.width = cxdev->width;
  794. f->fmt.pix.height = cxdev->height;
  795. f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  796. f->fmt.pix.field = vb2_start_streaming_called(&cxdev->videoqueue) ?
  797. cxdev->field_order : cxusb_medion_field_order(cxdev);
  798. f->fmt.pix.bytesperline = cxdev->width * 2;
  799. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  800. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
  801. return 0;
  802. }
  803. static int cxusb_medion_try_s_fmt_vid_cap(struct file *file,
  804. struct v4l2_format *f,
  805. bool isset)
  806. {
  807. struct dvb_usb_device *dvbdev = video_drvdata(file);
  808. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  809. struct v4l2_subdev_format subfmt = {
  810. .which = isset ? V4L2_SUBDEV_FORMAT_ACTIVE :
  811. V4L2_SUBDEV_FORMAT_TRY,
  812. };
  813. u32 field;
  814. int ret;
  815. if (isset && vb2_is_busy(&cxdev->videoqueue))
  816. return -EBUSY;
  817. field = vb2_start_streaming_called(&cxdev->videoqueue) ?
  818. cxdev->field_order : cxusb_medion_field_order(cxdev);
  819. subfmt.format.width = f->fmt.pix.width & ~1;
  820. subfmt.format.height = f->fmt.pix.height & ~1;
  821. subfmt.format.code = MEDIA_BUS_FMT_FIXED;
  822. subfmt.format.field = field;
  823. subfmt.format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  824. ret = v4l2_subdev_call(cxdev->cx25840, pad, set_fmt, NULL, &subfmt);
  825. if (ret != 0)
  826. return ret;
  827. f->fmt.pix.width = subfmt.format.width;
  828. f->fmt.pix.height = subfmt.format.height;
  829. f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  830. f->fmt.pix.field = field;
  831. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  832. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
  833. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  834. if (isset) {
  835. cxdev->width = f->fmt.pix.width;
  836. cxdev->height = f->fmt.pix.height;
  837. }
  838. return 0;
  839. }
  840. static int cxusb_medion_try_fmt_vid_cap(struct file *file, void *fh,
  841. struct v4l2_format *f)
  842. {
  843. return cxusb_medion_try_s_fmt_vid_cap(file, f, false);
  844. }
  845. static int cxusb_medion_s_fmt_vid_cap(struct file *file, void *fh,
  846. struct v4l2_format *f)
  847. {
  848. return cxusb_medion_try_s_fmt_vid_cap(file, f, true);
  849. }
  850. static const struct {
  851. struct v4l2_input input;
  852. u32 inputcfg;
  853. } cxusb_medion_inputs[] = {
  854. { .input = { .name = "TV tuner", .type = V4L2_INPUT_TYPE_TUNER,
  855. .tuner = 0, .std = V4L2_STD_PAL },
  856. .inputcfg = CX25840_COMPOSITE2, },
  857. { .input = { .name = "Composite", .type = V4L2_INPUT_TYPE_CAMERA,
  858. .std = V4L2_STD_ALL },
  859. .inputcfg = CX25840_COMPOSITE1, },
  860. { .input = { .name = "S-Video", .type = V4L2_INPUT_TYPE_CAMERA,
  861. .std = V4L2_STD_ALL },
  862. .inputcfg = CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 }
  863. };
  864. #define CXUSB_INPUT_CNT ARRAY_SIZE(cxusb_medion_inputs)
  865. static int cxusb_medion_enum_input(struct file *file, void *fh,
  866. struct v4l2_input *inp)
  867. {
  868. struct dvb_usb_device *dvbdev = video_drvdata(file);
  869. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  870. u32 index = inp->index;
  871. if (index >= CXUSB_INPUT_CNT)
  872. return -EINVAL;
  873. *inp = cxusb_medion_inputs[index].input;
  874. inp->index = index;
  875. inp->capabilities |= V4L2_IN_CAP_STD;
  876. if (index == cxdev->input) {
  877. int ret;
  878. u32 status = 0;
  879. ret = v4l2_subdev_call(cxdev->cx25840, video, g_input_status,
  880. &status);
  881. if (ret != 0)
  882. dev_warn(&dvbdev->udev->dev,
  883. "cx25840 input status query failed (%d)\n",
  884. ret);
  885. else
  886. inp->status = status;
  887. }
  888. return 0;
  889. }
  890. static int cxusb_medion_g_input(struct file *file, void *fh,
  891. unsigned int *i)
  892. {
  893. struct dvb_usb_device *dvbdev = video_drvdata(file);
  894. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  895. *i = cxdev->input;
  896. return 0;
  897. }
  898. static int cxusb_medion_set_norm(struct cxusb_medion_dev *cxdev,
  899. v4l2_std_id norm)
  900. {
  901. struct dvb_usb_device *dvbdev = cxdev->dvbdev;
  902. int ret;
  903. cxusb_vprintk(dvbdev, OPS,
  904. "trying to set standard for input %u to %lx\n",
  905. (unsigned int)cxdev->input,
  906. (unsigned long)norm);
  907. /* no autodetection support */
  908. if (norm == V4L2_STD_UNKNOWN)
  909. return -EINVAL;
  910. /* on composite or S-Video any std is acceptable */
  911. if (cxdev->input != 0) {
  912. ret = v4l2_subdev_call(cxdev->cx25840, video, s_std, norm);
  913. if (ret)
  914. return ret;
  915. goto ret_savenorm;
  916. }
  917. /* TV tuner is only able to demodulate PAL */
  918. if ((norm & ~V4L2_STD_PAL) != 0)
  919. return -EINVAL;
  920. ret = v4l2_subdev_call(cxdev->tda9887, video, s_std, norm);
  921. if (ret != 0) {
  922. dev_err(&dvbdev->udev->dev,
  923. "tda9887 norm setup failed (%d)\n",
  924. ret);
  925. return ret;
  926. }
  927. ret = v4l2_subdev_call(cxdev->tuner, video, s_std, norm);
  928. if (ret != 0) {
  929. dev_err(&dvbdev->udev->dev,
  930. "tuner norm setup failed (%d)\n",
  931. ret);
  932. return ret;
  933. }
  934. ret = v4l2_subdev_call(cxdev->cx25840, video, s_std, norm);
  935. if (ret != 0) {
  936. dev_err(&dvbdev->udev->dev,
  937. "cx25840 norm setup failed (%d)\n",
  938. ret);
  939. return ret;
  940. }
  941. ret_savenorm:
  942. cxdev->norm = norm;
  943. return 0;
  944. }
  945. static int cxusb_medion_s_input(struct file *file, void *fh,
  946. unsigned int i)
  947. {
  948. struct dvb_usb_device *dvbdev = video_drvdata(file);
  949. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  950. int ret;
  951. v4l2_std_id norm;
  952. if (i >= CXUSB_INPUT_CNT)
  953. return -EINVAL;
  954. ret = v4l2_subdev_call(cxdev->cx25840, video, s_routing,
  955. cxusb_medion_inputs[i].inputcfg, 0, 0);
  956. if (ret != 0)
  957. return ret;
  958. cxdev->input = i;
  959. cxdev->videodev->tvnorms = cxusb_medion_inputs[i].input.std;
  960. norm = cxdev->norm & cxusb_medion_inputs[i].input.std;
  961. if (norm == 0)
  962. norm = cxusb_medion_inputs[i].input.std;
  963. cxusb_medion_set_norm(cxdev, norm);
  964. return 0;
  965. }
  966. static int cxusb_medion_g_tuner(struct file *file, void *fh,
  967. struct v4l2_tuner *tuner)
  968. {
  969. struct dvb_usb_device *dvbdev = video_drvdata(file);
  970. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  971. struct video_device *vdev = video_devdata(file);
  972. int ret;
  973. if (tuner->index != 0)
  974. return -EINVAL;
  975. if (vdev->vfl_type == VFL_TYPE_VIDEO)
  976. tuner->type = V4L2_TUNER_ANALOG_TV;
  977. else
  978. tuner->type = V4L2_TUNER_RADIO;
  979. tuner->capability = 0;
  980. tuner->afc = 0;
  981. /*
  982. * fills:
  983. * always: capability (static), rangelow (static), rangehigh (static)
  984. * radio mode: afc (may fail silently), rxsubchans (static), audmode
  985. */
  986. ret = v4l2_subdev_call(cxdev->tda9887, tuner, g_tuner, tuner);
  987. if (ret != 0)
  988. return ret;
  989. /*
  990. * fills:
  991. * always: capability (static), rangelow (static), rangehigh (static)
  992. * radio mode: rxsubchans (always stereo), audmode,
  993. * signal (might be wrong)
  994. */
  995. ret = v4l2_subdev_call(cxdev->tuner, tuner, g_tuner, tuner);
  996. if (ret != 0)
  997. return ret;
  998. tuner->signal = 0;
  999. /*
  1000. * fills: TV mode: capability, rxsubchans, audmode, signal
  1001. */
  1002. ret = v4l2_subdev_call(cxdev->cx25840, tuner, g_tuner, tuner);
  1003. if (ret != 0)
  1004. return ret;
  1005. if (vdev->vfl_type == VFL_TYPE_VIDEO)
  1006. strscpy(tuner->name, "TV tuner", sizeof(tuner->name));
  1007. else
  1008. strscpy(tuner->name, "Radio tuner", sizeof(tuner->name));
  1009. memset(tuner->reserved, 0, sizeof(tuner->reserved));
  1010. return 0;
  1011. }
  1012. static int cxusb_medion_s_tuner(struct file *file, void *fh,
  1013. const struct v4l2_tuner *tuner)
  1014. {
  1015. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1016. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1017. struct video_device *vdev = video_devdata(file);
  1018. int ret;
  1019. if (tuner->index != 0)
  1020. return -EINVAL;
  1021. ret = v4l2_subdev_call(cxdev->tda9887, tuner, s_tuner, tuner);
  1022. if (ret != 0)
  1023. return ret;
  1024. ret = v4l2_subdev_call(cxdev->tuner, tuner, s_tuner, tuner);
  1025. if (ret != 0)
  1026. return ret;
  1027. /*
  1028. * make sure that cx25840 is in a correct TV / radio mode,
  1029. * since calls above may have changed it for tuner / IF demod
  1030. */
  1031. if (vdev->vfl_type == VFL_TYPE_VIDEO)
  1032. v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
  1033. else
  1034. v4l2_subdev_call(cxdev->cx25840, tuner, s_radio);
  1035. return v4l2_subdev_call(cxdev->cx25840, tuner, s_tuner, tuner);
  1036. }
  1037. static int cxusb_medion_g_frequency(struct file *file, void *fh,
  1038. struct v4l2_frequency *freq)
  1039. {
  1040. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1041. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1042. if (freq->tuner != 0)
  1043. return -EINVAL;
  1044. return v4l2_subdev_call(cxdev->tuner, tuner, g_frequency, freq);
  1045. }
  1046. static int cxusb_medion_s_frequency(struct file *file, void *fh,
  1047. const struct v4l2_frequency *freq)
  1048. {
  1049. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1050. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1051. struct video_device *vdev = video_devdata(file);
  1052. int ret;
  1053. if (freq->tuner != 0)
  1054. return -EINVAL;
  1055. ret = v4l2_subdev_call(cxdev->tda9887, tuner, s_frequency, freq);
  1056. if (ret != 0)
  1057. return ret;
  1058. ret = v4l2_subdev_call(cxdev->tuner, tuner, s_frequency, freq);
  1059. if (ret != 0)
  1060. return ret;
  1061. /*
  1062. * make sure that cx25840 is in a correct TV / radio mode,
  1063. * since calls above may have changed it for tuner / IF demod
  1064. */
  1065. if (vdev->vfl_type == VFL_TYPE_VIDEO)
  1066. v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
  1067. else
  1068. v4l2_subdev_call(cxdev->cx25840, tuner, s_radio);
  1069. return v4l2_subdev_call(cxdev->cx25840, tuner, s_frequency, freq);
  1070. }
  1071. static int cxusb_medion_g_std(struct file *file, void *fh,
  1072. v4l2_std_id *norm)
  1073. {
  1074. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1075. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1076. *norm = cxdev->norm;
  1077. if (*norm == V4L2_STD_UNKNOWN)
  1078. return -ENODATA;
  1079. return 0;
  1080. }
  1081. static int cxusb_medion_s_std(struct file *file, void *fh,
  1082. v4l2_std_id norm)
  1083. {
  1084. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1085. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1086. return cxusb_medion_set_norm(cxdev, norm);
  1087. }
  1088. static int cxusb_medion_querystd(struct file *file, void *fh,
  1089. v4l2_std_id *norm)
  1090. {
  1091. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1092. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1093. v4l2_std_id norm_mask;
  1094. int ret;
  1095. /*
  1096. * make sure we don't have improper std bits set for the TV tuner
  1097. * (could happen when no signal was present yet after reset)
  1098. */
  1099. if (cxdev->input == 0)
  1100. norm_mask = V4L2_STD_PAL;
  1101. else
  1102. norm_mask = V4L2_STD_ALL;
  1103. ret = v4l2_subdev_call(cxdev->cx25840, video, querystd, norm);
  1104. if (ret != 0) {
  1105. cxusb_vprintk(dvbdev, OPS,
  1106. "cannot get detected standard for input %u\n",
  1107. (unsigned int)cxdev->input);
  1108. return ret;
  1109. }
  1110. cxusb_vprintk(dvbdev, OPS, "input %u detected standard is %lx\n",
  1111. (unsigned int)cxdev->input, (unsigned long)*norm);
  1112. *norm &= norm_mask;
  1113. return 0;
  1114. }
  1115. static int cxusb_medion_log_status(struct file *file, void *fh)
  1116. {
  1117. struct dvb_usb_device *dvbdev = video_drvdata(file);
  1118. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1119. v4l2_device_call_all(&cxdev->v4l2dev, 0, core, log_status);
  1120. return 0;
  1121. }
  1122. static const struct v4l2_ioctl_ops cxusb_video_ioctl = {
  1123. .vidioc_querycap = cxusb_medion_v_querycap,
  1124. .vidioc_enum_fmt_vid_cap = cxusb_medion_v_enum_fmt_vid_cap,
  1125. .vidioc_g_fmt_vid_cap = cxusb_medion_g_fmt_vid_cap,
  1126. .vidioc_s_fmt_vid_cap = cxusb_medion_s_fmt_vid_cap,
  1127. .vidioc_try_fmt_vid_cap = cxusb_medion_try_fmt_vid_cap,
  1128. .vidioc_enum_input = cxusb_medion_enum_input,
  1129. .vidioc_g_input = cxusb_medion_g_input,
  1130. .vidioc_s_input = cxusb_medion_s_input,
  1131. .vidioc_g_tuner = cxusb_medion_g_tuner,
  1132. .vidioc_s_tuner = cxusb_medion_s_tuner,
  1133. .vidioc_g_frequency = cxusb_medion_g_frequency,
  1134. .vidioc_s_frequency = cxusb_medion_s_frequency,
  1135. .vidioc_g_std = cxusb_medion_g_std,
  1136. .vidioc_s_std = cxusb_medion_s_std,
  1137. .vidioc_querystd = cxusb_medion_querystd,
  1138. .vidioc_log_status = cxusb_medion_log_status,
  1139. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1140. .vidioc_querybuf = vb2_ioctl_querybuf,
  1141. .vidioc_qbuf = vb2_ioctl_qbuf,
  1142. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1143. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1144. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1145. .vidioc_streamon = vb2_ioctl_streamon,
  1146. .vidioc_streamoff = vb2_ioctl_streamoff
  1147. };
  1148. static const struct v4l2_ioctl_ops cxusb_radio_ioctl = {
  1149. .vidioc_querycap = cxusb_medion_v_querycap,
  1150. .vidioc_g_tuner = cxusb_medion_g_tuner,
  1151. .vidioc_s_tuner = cxusb_medion_s_tuner,
  1152. .vidioc_g_frequency = cxusb_medion_g_frequency,
  1153. .vidioc_s_frequency = cxusb_medion_s_frequency,
  1154. .vidioc_log_status = cxusb_medion_log_status
  1155. };
  1156. /*
  1157. * in principle, this should be const, but s_io_pin_config is declared
  1158. * to take non-const, and gcc complains
  1159. */
  1160. static struct v4l2_subdev_io_pin_config cxusub_medion_pin_config[] = {
  1161. { .pin = CX25840_PIN_DVALID_PRGM0, .function = CX25840_PAD_DEFAULT,
  1162. .strength = CX25840_PIN_DRIVE_MEDIUM },
  1163. { .pin = CX25840_PIN_PLL_CLK_PRGM7, .function = CX25840_PAD_AUX_PLL },
  1164. { .pin = CX25840_PIN_HRESET_PRGM2, .function = CX25840_PAD_ACTIVE,
  1165. .strength = CX25840_PIN_DRIVE_MEDIUM }
  1166. };
  1167. int cxusb_medion_analog_init(struct dvb_usb_device *dvbdev)
  1168. {
  1169. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1170. u8 tuner_analog_msg_data[] = { 0x9c, 0x60, 0x85, 0x54 };
  1171. struct i2c_msg tuner_analog_msg = { .addr = 0x61, .flags = 0,
  1172. .buf = tuner_analog_msg_data,
  1173. .len =
  1174. sizeof(tuner_analog_msg_data) };
  1175. struct v4l2_subdev_format subfmt = {
  1176. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1177. };
  1178. int ret;
  1179. /* switch tuner to analog mode so IF demod will become accessible */
  1180. ret = i2c_transfer(&dvbdev->i2c_adap, &tuner_analog_msg, 1);
  1181. if (ret != 1)
  1182. dev_warn(&dvbdev->udev->dev,
  1183. "tuner analog switch failed (%d)\n", ret);
  1184. /*
  1185. * cx25840 might have lost power during mode switching so we need
  1186. * to set it again
  1187. */
  1188. ret = v4l2_subdev_call(cxdev->cx25840, core, reset, 0);
  1189. if (ret != 0)
  1190. dev_warn(&dvbdev->udev->dev,
  1191. "cx25840 reset failed (%d)\n", ret);
  1192. ret = v4l2_subdev_call(cxdev->cx25840, video, s_routing,
  1193. CX25840_COMPOSITE1, 0, 0);
  1194. if (ret != 0)
  1195. dev_warn(&dvbdev->udev->dev,
  1196. "cx25840 initial input setting failed (%d)\n", ret);
  1197. /* composite */
  1198. cxdev->input = 1;
  1199. cxdev->videodev->tvnorms = V4L2_STD_ALL;
  1200. cxdev->norm = V4L2_STD_PAL;
  1201. /* TODO: setup audio samples insertion */
  1202. ret = v4l2_subdev_call(cxdev->cx25840, core, s_io_pin_config,
  1203. ARRAY_SIZE(cxusub_medion_pin_config),
  1204. cxusub_medion_pin_config);
  1205. if (ret != 0)
  1206. dev_warn(&dvbdev->udev->dev,
  1207. "cx25840 pin config failed (%d)\n", ret);
  1208. /* make sure that we aren't in radio mode */
  1209. v4l2_subdev_call(cxdev->tda9887, video, s_std, cxdev->norm);
  1210. v4l2_subdev_call(cxdev->tuner, video, s_std, cxdev->norm);
  1211. v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
  1212. subfmt.format.width = cxdev->width;
  1213. subfmt.format.height = cxdev->height;
  1214. subfmt.format.code = MEDIA_BUS_FMT_FIXED;
  1215. subfmt.format.field = V4L2_FIELD_SEQ_TB;
  1216. subfmt.format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1217. ret = v4l2_subdev_call(cxdev->cx25840, pad, set_fmt, NULL, &subfmt);
  1218. if (ret != 0)
  1219. dev_warn(&dvbdev->udev->dev,
  1220. "cx25840 format set failed (%d)\n", ret);
  1221. if (ret == 0) {
  1222. cxdev->width = subfmt.format.width;
  1223. cxdev->height = subfmt.format.height;
  1224. }
  1225. return 0;
  1226. }
  1227. static int cxusb_videoradio_open(struct file *f)
  1228. {
  1229. struct dvb_usb_device *dvbdev = video_drvdata(f);
  1230. int ret;
  1231. /*
  1232. * no locking needed since this call only modifies analog
  1233. * state if there are no other analog handles currenly
  1234. * opened so ops done via them cannot create a conflict
  1235. */
  1236. ret = cxusb_medion_get(dvbdev, CXUSB_OPEN_ANALOG);
  1237. if (ret != 0)
  1238. return ret;
  1239. ret = v4l2_fh_open(f);
  1240. if (ret != 0)
  1241. goto ret_release;
  1242. cxusb_vprintk(dvbdev, OPS, "got open\n");
  1243. return 0;
  1244. ret_release:
  1245. cxusb_medion_put(dvbdev);
  1246. return ret;
  1247. }
  1248. static int cxusb_videoradio_release(struct file *f)
  1249. {
  1250. struct video_device *vdev = video_devdata(f);
  1251. struct dvb_usb_device *dvbdev = video_drvdata(f);
  1252. int ret;
  1253. cxusb_vprintk(dvbdev, OPS, "got release\n");
  1254. if (vdev->vfl_type == VFL_TYPE_VIDEO)
  1255. ret = vb2_fop_release(f);
  1256. else
  1257. ret = v4l2_fh_release(f);
  1258. cxusb_medion_put(dvbdev);
  1259. return ret;
  1260. }
  1261. static const struct v4l2_file_operations cxusb_video_fops = {
  1262. .owner = THIS_MODULE,
  1263. .read = vb2_fop_read,
  1264. .poll = vb2_fop_poll,
  1265. .unlocked_ioctl = video_ioctl2,
  1266. .mmap = vb2_fop_mmap,
  1267. .open = cxusb_videoradio_open,
  1268. .release = cxusb_videoradio_release
  1269. };
  1270. static const struct v4l2_file_operations cxusb_radio_fops = {
  1271. .owner = THIS_MODULE,
  1272. .unlocked_ioctl = video_ioctl2,
  1273. .open = cxusb_videoradio_open,
  1274. .release = cxusb_videoradio_release
  1275. };
  1276. static void cxusb_medion_v4l2_release(struct v4l2_device *v4l2_dev)
  1277. {
  1278. struct cxusb_medion_dev *cxdev =
  1279. container_of(v4l2_dev, struct cxusb_medion_dev, v4l2dev);
  1280. struct dvb_usb_device *dvbdev = cxdev->dvbdev;
  1281. cxusb_vprintk(dvbdev, OPS, "v4l2 device release\n");
  1282. v4l2_device_unregister(&cxdev->v4l2dev);
  1283. mutex_destroy(&cxdev->dev_lock);
  1284. while (completion_done(&cxdev->v4l2_release))
  1285. schedule();
  1286. complete(&cxdev->v4l2_release);
  1287. }
  1288. static void cxusb_medion_videodev_release(struct video_device *vdev)
  1289. {
  1290. struct dvb_usb_device *dvbdev = video_get_drvdata(vdev);
  1291. cxusb_vprintk(dvbdev, OPS, "video device release\n");
  1292. video_device_release(vdev);
  1293. }
  1294. static int cxusb_medion_register_analog_video(struct dvb_usb_device *dvbdev)
  1295. {
  1296. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1297. int ret;
  1298. cxdev->videoqueue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1299. cxdev->videoqueue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ |
  1300. VB2_DMABUF;
  1301. cxdev->videoqueue.ops = &cxdev_video_qops;
  1302. cxdev->videoqueue.mem_ops = &vb2_vmalloc_memops;
  1303. cxdev->videoqueue.drv_priv = dvbdev;
  1304. cxdev->videoqueue.buf_struct_size =
  1305. sizeof(struct cxusb_medion_vbuffer);
  1306. cxdev->videoqueue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1307. cxdev->videoqueue.min_queued_buffers = 6;
  1308. cxdev->videoqueue.lock = &cxdev->dev_lock;
  1309. ret = vb2_queue_init(&cxdev->videoqueue);
  1310. if (ret) {
  1311. dev_err(&dvbdev->udev->dev,
  1312. "video queue init failed, ret = %d\n", ret);
  1313. return ret;
  1314. }
  1315. cxdev->videodev = video_device_alloc();
  1316. if (!cxdev->videodev) {
  1317. dev_err(&dvbdev->udev->dev, "video device alloc failed\n");
  1318. return -ENOMEM;
  1319. }
  1320. cxdev->videodev->device_caps = videocaps;
  1321. cxdev->videodev->fops = &cxusb_video_fops;
  1322. cxdev->videodev->v4l2_dev = &cxdev->v4l2dev;
  1323. cxdev->videodev->queue = &cxdev->videoqueue;
  1324. strscpy(cxdev->videodev->name, "cxusb", sizeof(cxdev->videodev->name));
  1325. cxdev->videodev->vfl_dir = VFL_DIR_RX;
  1326. cxdev->videodev->ioctl_ops = &cxusb_video_ioctl;
  1327. cxdev->videodev->tvnorms = V4L2_STD_ALL;
  1328. cxdev->videodev->release = cxusb_medion_videodev_release;
  1329. cxdev->videodev->lock = &cxdev->dev_lock;
  1330. video_set_drvdata(cxdev->videodev, dvbdev);
  1331. ret = video_register_device(cxdev->videodev, VFL_TYPE_VIDEO, -1);
  1332. if (ret) {
  1333. dev_err(&dvbdev->udev->dev,
  1334. "video device register failed, ret = %d\n", ret);
  1335. goto ret_vrelease;
  1336. }
  1337. return 0;
  1338. ret_vrelease:
  1339. video_device_release(cxdev->videodev);
  1340. return ret;
  1341. }
  1342. static int cxusb_medion_register_analog_radio(struct dvb_usb_device *dvbdev)
  1343. {
  1344. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1345. int ret;
  1346. cxdev->radiodev = video_device_alloc();
  1347. if (!cxdev->radiodev) {
  1348. dev_err(&dvbdev->udev->dev, "radio device alloc failed\n");
  1349. return -ENOMEM;
  1350. }
  1351. cxdev->radiodev->device_caps = radiocaps;
  1352. cxdev->radiodev->fops = &cxusb_radio_fops;
  1353. cxdev->radiodev->v4l2_dev = &cxdev->v4l2dev;
  1354. strscpy(cxdev->radiodev->name, "cxusb", sizeof(cxdev->radiodev->name));
  1355. cxdev->radiodev->vfl_dir = VFL_DIR_RX;
  1356. cxdev->radiodev->ioctl_ops = &cxusb_radio_ioctl;
  1357. cxdev->radiodev->release = video_device_release;
  1358. cxdev->radiodev->lock = &cxdev->dev_lock;
  1359. video_set_drvdata(cxdev->radiodev, dvbdev);
  1360. ret = video_register_device(cxdev->radiodev, VFL_TYPE_RADIO, -1);
  1361. if (ret) {
  1362. dev_err(&dvbdev->udev->dev,
  1363. "radio device register failed, ret = %d\n", ret);
  1364. video_device_release(cxdev->radiodev);
  1365. return ret;
  1366. }
  1367. return 0;
  1368. }
  1369. static int cxusb_medion_register_analog_subdevs(struct dvb_usb_device *dvbdev)
  1370. {
  1371. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1372. int ret;
  1373. struct tuner_setup tun_setup;
  1374. /* attach cx25840 capture chip */
  1375. cxdev->cx25840 = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
  1376. &dvbdev->i2c_adap,
  1377. "cx25840", 0x44, NULL);
  1378. if (!cxdev->cx25840) {
  1379. dev_err(&dvbdev->udev->dev, "cx25840 not found\n");
  1380. return -ENODEV;
  1381. }
  1382. /*
  1383. * Initialize cx25840 chip by calling its subdevice init core op.
  1384. *
  1385. * This switches it into the generic mode that disables some of
  1386. * ivtv-related hacks in the cx25840 driver while allowing setting
  1387. * of the chip video output configuration (passed in the call below
  1388. * as the last argument).
  1389. */
  1390. ret = v4l2_subdev_call(cxdev->cx25840, core, init,
  1391. CX25840_VCONFIG_FMT_BT656 |
  1392. CX25840_VCONFIG_RES_8BIT |
  1393. CX25840_VCONFIG_VBIRAW_DISABLED |
  1394. CX25840_VCONFIG_ANCDATA_DISABLED |
  1395. CX25840_VCONFIG_ACTIVE_COMPOSITE |
  1396. CX25840_VCONFIG_VALID_ANDACTIVE |
  1397. CX25840_VCONFIG_HRESETW_NORMAL |
  1398. CX25840_VCONFIG_CLKGATE_NONE |
  1399. CX25840_VCONFIG_DCMODE_DWORDS);
  1400. if (ret != 0) {
  1401. dev_err(&dvbdev->udev->dev,
  1402. "cx25840 init failed (%d)\n", ret);
  1403. return ret;
  1404. }
  1405. /* attach analog tuner */
  1406. cxdev->tuner = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
  1407. &dvbdev->i2c_adap,
  1408. "tuner", 0x61, NULL);
  1409. if (!cxdev->tuner) {
  1410. dev_err(&dvbdev->udev->dev, "tuner not found\n");
  1411. return -ENODEV;
  1412. }
  1413. /* configure it */
  1414. memset(&tun_setup, 0, sizeof(tun_setup));
  1415. tun_setup.addr = 0x61;
  1416. tun_setup.type = TUNER_PHILIPS_FMD1216ME_MK3;
  1417. tun_setup.mode_mask = T_RADIO | T_ANALOG_TV;
  1418. v4l2_subdev_call(cxdev->tuner, tuner, s_type_addr, &tun_setup);
  1419. /* attach IF demod */
  1420. cxdev->tda9887 = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
  1421. &dvbdev->i2c_adap,
  1422. "tuner", 0x43, NULL);
  1423. if (!cxdev->tda9887) {
  1424. dev_err(&dvbdev->udev->dev, "tda9887 not found\n");
  1425. return -ENODEV;
  1426. }
  1427. return 0;
  1428. }
  1429. int cxusb_medion_register_analog(struct dvb_usb_device *dvbdev)
  1430. {
  1431. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1432. int ret;
  1433. mutex_init(&cxdev->dev_lock);
  1434. init_completion(&cxdev->v4l2_release);
  1435. cxdev->v4l2dev.release = cxusb_medion_v4l2_release;
  1436. ret = v4l2_device_register(&dvbdev->udev->dev, &cxdev->v4l2dev);
  1437. if (ret != 0) {
  1438. dev_err(&dvbdev->udev->dev,
  1439. "V4L2 device registration failed, ret = %d\n", ret);
  1440. mutex_destroy(&cxdev->dev_lock);
  1441. return ret;
  1442. }
  1443. ret = cxusb_medion_register_analog_subdevs(dvbdev);
  1444. if (ret)
  1445. goto ret_unregister;
  1446. INIT_WORK(&cxdev->urbwork, cxusb_medion_v_complete_work);
  1447. INIT_LIST_HEAD(&cxdev->buflist);
  1448. cxdev->width = 320;
  1449. cxdev->height = 240;
  1450. ret = cxusb_medion_register_analog_video(dvbdev);
  1451. if (ret)
  1452. goto ret_unregister;
  1453. ret = cxusb_medion_register_analog_radio(dvbdev);
  1454. if (ret)
  1455. goto ret_vunreg;
  1456. return 0;
  1457. ret_vunreg:
  1458. vb2_video_unregister_device(cxdev->videodev);
  1459. ret_unregister:
  1460. v4l2_device_put(&cxdev->v4l2dev);
  1461. wait_for_completion(&cxdev->v4l2_release);
  1462. return ret;
  1463. }
  1464. void cxusb_medion_unregister_analog(struct dvb_usb_device *dvbdev)
  1465. {
  1466. struct cxusb_medion_dev *cxdev = dvbdev->priv;
  1467. cxusb_vprintk(dvbdev, OPS, "unregistering analog\n");
  1468. video_unregister_device(cxdev->radiodev);
  1469. vb2_video_unregister_device(cxdev->videodev);
  1470. v4l2_device_put(&cxdev->v4l2dev);
  1471. wait_for_completion(&cxdev->v4l2_release);
  1472. cxusb_vprintk(dvbdev, OPS, "analog unregistered\n");
  1473. }