em28xx-dvb.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /*
  2. DVB device driver for em28xx
  3. (c) 2008-2011 Mauro Carvalho Chehab <mchehab@infradead.org>
  4. (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
  5. - Fixes for the driver to properly work with HVR-950
  6. - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
  7. - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
  8. (c) 2008 Aidan Thornton <makosoft@googlemail.com>
  9. Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
  10. (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  11. (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/usb.h>
  19. #include "em28xx.h"
  20. #include <media/v4l2-common.h>
  21. #include <media/videobuf-vmalloc.h>
  22. #include <media/tuner.h>
  23. #include "tuner-simple.h"
  24. #include "lgdt330x.h"
  25. #include "lgdt3305.h"
  26. #include "zl10353.h"
  27. #include "s5h1409.h"
  28. #include "mt352.h"
  29. #include "mt352_priv.h" /* FIXME */
  30. #include "tda1002x.h"
  31. #include "tda18271.h"
  32. #include "s921.h"
  33. #include "drxd.h"
  34. #include "cxd2820r.h"
  35. #include "tda18271c2dd.h"
  36. #include "drxk.h"
  37. #include "tda10071.h"
  38. #include "a8293.h"
  39. #include "qt1010.h"
  40. MODULE_DESCRIPTION("driver for em28xx based DVB cards");
  41. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  42. MODULE_LICENSE("GPL");
  43. static unsigned int debug;
  44. module_param(debug, int, 0644);
  45. MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
  46. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  47. #define dprintk(level, fmt, arg...) do { \
  48. if (debug >= level) \
  49. printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
  50. } while (0)
  51. struct em28xx_dvb {
  52. struct dvb_frontend *fe[2];
  53. /* feed count management */
  54. struct mutex lock;
  55. int nfeeds;
  56. /* general boilerplate stuff */
  57. struct dvb_adapter adapter;
  58. struct dvb_demux demux;
  59. struct dmxdev dmxdev;
  60. struct dmx_frontend fe_hw;
  61. struct dmx_frontend fe_mem;
  62. struct dvb_net net;
  63. /* Due to DRX-K - probably need changes */
  64. int (*gate_ctrl)(struct dvb_frontend *, int);
  65. struct semaphore pll_mutex;
  66. bool dont_attach_fe1;
  67. };
  68. static inline void print_err_status(struct em28xx *dev,
  69. int packet, int status)
  70. {
  71. char *errmsg = "Unknown";
  72. switch (status) {
  73. case -ENOENT:
  74. errmsg = "unlinked synchronuously";
  75. break;
  76. case -ECONNRESET:
  77. errmsg = "unlinked asynchronuously";
  78. break;
  79. case -ENOSR:
  80. errmsg = "Buffer error (overrun)";
  81. break;
  82. case -EPIPE:
  83. errmsg = "Stalled (device not responding)";
  84. break;
  85. case -EOVERFLOW:
  86. errmsg = "Babble (bad cable?)";
  87. break;
  88. case -EPROTO:
  89. errmsg = "Bit-stuff error (bad cable?)";
  90. break;
  91. case -EILSEQ:
  92. errmsg = "CRC/Timeout (could be anything)";
  93. break;
  94. case -ETIME:
  95. errmsg = "Device does not respond";
  96. break;
  97. }
  98. if (packet < 0) {
  99. dprintk(1, "URB status %d [%s].\n", status, errmsg);
  100. } else {
  101. dprintk(1, "URB packet %d, status %d [%s].\n",
  102. packet, status, errmsg);
  103. }
  104. }
  105. static inline int em28xx_dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
  106. {
  107. int i;
  108. if (!dev)
  109. return 0;
  110. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  111. return 0;
  112. if (urb->status < 0) {
  113. print_err_status(dev, -1, urb->status);
  114. if (urb->status == -ENOENT)
  115. return 0;
  116. }
  117. for (i = 0; i < urb->number_of_packets; i++) {
  118. int status = urb->iso_frame_desc[i].status;
  119. if (status < 0) {
  120. print_err_status(dev, i, status);
  121. if (urb->iso_frame_desc[i].status != -EPROTO)
  122. continue;
  123. }
  124. dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
  125. urb->iso_frame_desc[i].offset,
  126. urb->iso_frame_desc[i].actual_length);
  127. }
  128. return 0;
  129. }
  130. static int em28xx_start_streaming(struct em28xx_dvb *dvb)
  131. {
  132. int rc;
  133. struct em28xx *dev = dvb->adapter.priv;
  134. int max_dvb_packet_size;
  135. usb_set_interface(dev->udev, 0, dev->dvb_alt);
  136. rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  137. if (rc < 0)
  138. return rc;
  139. max_dvb_packet_size = dev->dvb_max_pkt_size;
  140. if (max_dvb_packet_size < 0)
  141. return max_dvb_packet_size;
  142. dprintk(1, "Using %d buffers each with %d x %d bytes\n",
  143. EM28XX_DVB_NUM_BUFS,
  144. EM28XX_DVB_MAX_PACKETS,
  145. max_dvb_packet_size);
  146. return em28xx_init_isoc(dev, EM28XX_DIGITAL_MODE,
  147. EM28XX_DVB_MAX_PACKETS, EM28XX_DVB_NUM_BUFS,
  148. max_dvb_packet_size, em28xx_dvb_isoc_copy);
  149. }
  150. static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
  151. {
  152. struct em28xx *dev = dvb->adapter.priv;
  153. em28xx_stop_urbs(dev);
  154. em28xx_set_mode(dev, EM28XX_SUSPEND);
  155. return 0;
  156. }
  157. static int em28xx_start_feed(struct dvb_demux_feed *feed)
  158. {
  159. struct dvb_demux *demux = feed->demux;
  160. struct em28xx_dvb *dvb = demux->priv;
  161. int rc, ret;
  162. if (!demux->dmx.frontend)
  163. return -EINVAL;
  164. mutex_lock(&dvb->lock);
  165. dvb->nfeeds++;
  166. rc = dvb->nfeeds;
  167. if (dvb->nfeeds == 1) {
  168. ret = em28xx_start_streaming(dvb);
  169. if (ret < 0)
  170. rc = ret;
  171. }
  172. mutex_unlock(&dvb->lock);
  173. return rc;
  174. }
  175. static int em28xx_stop_feed(struct dvb_demux_feed *feed)
  176. {
  177. struct dvb_demux *demux = feed->demux;
  178. struct em28xx_dvb *dvb = demux->priv;
  179. int err = 0;
  180. mutex_lock(&dvb->lock);
  181. dvb->nfeeds--;
  182. if (0 == dvb->nfeeds)
  183. err = em28xx_stop_streaming(dvb);
  184. mutex_unlock(&dvb->lock);
  185. return err;
  186. }
  187. /* ------------------------------------------------------------------ */
  188. static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
  189. {
  190. struct em28xx *dev = fe->dvb->priv;
  191. if (acquire)
  192. return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  193. else
  194. return em28xx_set_mode(dev, EM28XX_SUSPEND);
  195. }
  196. /* ------------------------------------------------------------------ */
  197. static struct lgdt330x_config em2880_lgdt3303_dev = {
  198. .demod_address = 0x0e,
  199. .demod_chip = LGDT3303,
  200. };
  201. static struct lgdt3305_config em2870_lgdt3304_dev = {
  202. .i2c_addr = 0x0e,
  203. .demod_chip = LGDT3304,
  204. .spectral_inversion = 1,
  205. .deny_i2c_rptr = 1,
  206. .mpeg_mode = LGDT3305_MPEG_PARALLEL,
  207. .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
  208. .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
  209. .vsb_if_khz = 3250,
  210. .qam_if_khz = 4000,
  211. };
  212. static struct s921_config sharp_isdbt = {
  213. .demod_address = 0x30 >> 1
  214. };
  215. static struct zl10353_config em28xx_zl10353_with_xc3028 = {
  216. .demod_address = (0x1e >> 1),
  217. .no_tuner = 1,
  218. .parallel_ts = 1,
  219. .if2 = 45600,
  220. };
  221. static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
  222. .demod_address = 0x32 >> 1,
  223. .output_mode = S5H1409_PARALLEL_OUTPUT,
  224. .gpio = S5H1409_GPIO_OFF,
  225. .inversion = S5H1409_INVERSION_OFF,
  226. .status_mode = S5H1409_DEMODLOCKING,
  227. .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
  228. };
  229. static struct tda18271_std_map kworld_a340_std_map = {
  230. .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 0,
  231. .if_lvl = 1, .rfagc_top = 0x37, },
  232. .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 1,
  233. .if_lvl = 1, .rfagc_top = 0x37, },
  234. };
  235. static struct tda18271_config kworld_a340_config = {
  236. .std_map = &kworld_a340_std_map,
  237. };
  238. static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
  239. .demod_address = (0x1e >> 1),
  240. .no_tuner = 1,
  241. .disable_i2c_gate_ctrl = 1,
  242. .parallel_ts = 1,
  243. .if2 = 45600,
  244. };
  245. static struct drxd_config em28xx_drxd = {
  246. .demod_address = 0x70,
  247. .demod_revision = 0xa2,
  248. .pll_type = DRXD_PLL_NONE,
  249. .clock = 12000,
  250. .insert_rs_byte = 1,
  251. .IF = 42800000,
  252. .disable_i2c_gate_ctrl = 1,
  253. };
  254. static struct drxk_config terratec_h5_drxk = {
  255. .adr = 0x29,
  256. .single_master = 1,
  257. .no_i2c_bridge = 1,
  258. .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
  259. .qam_demod_parameter_count = 2,
  260. };
  261. static struct drxk_config hauppauge_930c_drxk = {
  262. .adr = 0x29,
  263. .single_master = 1,
  264. .no_i2c_bridge = 1,
  265. .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
  266. .chunk_size = 56,
  267. .qam_demod_parameter_count = 2,
  268. };
  269. struct drxk_config terratec_htc_stick_drxk = {
  270. .adr = 0x29,
  271. .single_master = 1,
  272. .no_i2c_bridge = 1,
  273. .microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
  274. .chunk_size = 54,
  275. .qam_demod_parameter_count = 2,
  276. /* Required for the antenna_gpio to disable LNA. */
  277. .antenna_dvbt = true,
  278. /* The windows driver uses the same. This will disable LNA. */
  279. .antenna_gpio = 0x6,
  280. };
  281. static struct drxk_config maxmedia_ub425_tc_drxk = {
  282. .adr = 0x29,
  283. .single_master = 1,
  284. .no_i2c_bridge = 1,
  285. };
  286. static struct drxk_config pctv_520e_drxk = {
  287. .adr = 0x29,
  288. .single_master = 1,
  289. .microcode_name = "dvb-demod-drxk-pctv.fw",
  290. .qam_demod_parameter_count = 2,
  291. .chunk_size = 58,
  292. .antenna_dvbt = true, /* disable LNA */
  293. .antenna_gpio = (1 << 2), /* disable LNA */
  294. };
  295. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  296. {
  297. struct em28xx_dvb *dvb = fe->sec_priv;
  298. int status;
  299. if (!dvb)
  300. return -EINVAL;
  301. if (enable) {
  302. down(&dvb->pll_mutex);
  303. status = dvb->gate_ctrl(fe, 1);
  304. } else {
  305. status = dvb->gate_ctrl(fe, 0);
  306. up(&dvb->pll_mutex);
  307. }
  308. return status;
  309. }
  310. static void hauppauge_hvr930c_init(struct em28xx *dev)
  311. {
  312. int i;
  313. struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
  314. {EM2874_R80_GPIO, 0xff, 0xff, 0x65},
  315. {EM2874_R80_GPIO, 0xfb, 0xff, 0x32},
  316. {EM2874_R80_GPIO, 0xff, 0xff, 0xb8},
  317. { -1, -1, -1, -1},
  318. };
  319. struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
  320. {EM2874_R80_GPIO, 0xef, 0xff, 0x01},
  321. {EM2874_R80_GPIO, 0xaf, 0xff, 0x65},
  322. {EM2874_R80_GPIO, 0xef, 0xff, 0x76},
  323. {EM2874_R80_GPIO, 0xef, 0xff, 0x01},
  324. {EM2874_R80_GPIO, 0xcf, 0xff, 0x0b},
  325. {EM2874_R80_GPIO, 0xef, 0xff, 0x40},
  326. {EM2874_R80_GPIO, 0xcf, 0xff, 0x65},
  327. {EM2874_R80_GPIO, 0xef, 0xff, 0x65},
  328. {EM2874_R80_GPIO, 0xcf, 0xff, 0x0b},
  329. {EM2874_R80_GPIO, 0xef, 0xff, 0x65},
  330. { -1, -1, -1, -1},
  331. };
  332. struct {
  333. unsigned char r[4];
  334. int len;
  335. } regs[] = {
  336. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  337. {{ 0x01, 0x02 }, 2},
  338. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  339. {{ 0x01, 0x00 }, 2},
  340. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  341. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  342. {{ 0x01, 0x00 }, 2},
  343. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  344. {{ 0x04, 0x00 }, 2},
  345. {{ 0x00, 0x04 }, 2},
  346. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  347. {{ 0x04, 0x14 }, 2},
  348. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  349. };
  350. em28xx_gpio_set(dev, hauppauge_hvr930c_init);
  351. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  352. msleep(10);
  353. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  354. msleep(10);
  355. dev->i2c_client.addr = 0x82 >> 1;
  356. for (i = 0; i < ARRAY_SIZE(regs); i++)
  357. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  358. em28xx_gpio_set(dev, hauppauge_hvr930c_end);
  359. msleep(100);
  360. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  361. msleep(30);
  362. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  363. msleep(10);
  364. }
  365. static void terratec_h5_init(struct em28xx *dev)
  366. {
  367. int i;
  368. struct em28xx_reg_seq terratec_h5_init[] = {
  369. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  370. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  371. {EM2874_R80_GPIO, 0xf2, 0xff, 50},
  372. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  373. { -1, -1, -1, -1},
  374. };
  375. struct em28xx_reg_seq terratec_h5_end[] = {
  376. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  377. {EM2874_R80_GPIO, 0xa6, 0xff, 50},
  378. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  379. { -1, -1, -1, -1},
  380. };
  381. struct {
  382. unsigned char r[4];
  383. int len;
  384. } regs[] = {
  385. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  386. {{ 0x01, 0x02 }, 2},
  387. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  388. {{ 0x01, 0x00 }, 2},
  389. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  390. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  391. {{ 0x01, 0x00 }, 2},
  392. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  393. {{ 0x04, 0x00 }, 2},
  394. {{ 0x00, 0x04 }, 2},
  395. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  396. {{ 0x04, 0x14 }, 2},
  397. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  398. };
  399. em28xx_gpio_set(dev, terratec_h5_init);
  400. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  401. msleep(10);
  402. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  403. msleep(10);
  404. dev->i2c_client.addr = 0x82 >> 1;
  405. for (i = 0; i < ARRAY_SIZE(regs); i++)
  406. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  407. em28xx_gpio_set(dev, terratec_h5_end);
  408. };
  409. static void terratec_htc_stick_init(struct em28xx *dev)
  410. {
  411. int i;
  412. /*
  413. * GPIO configuration:
  414. * 0xff: unknown (does not affect DVB-T).
  415. * 0xf6: DRX-K (demodulator).
  416. * 0xe6: unknown (does not affect DVB-T).
  417. * 0xb6: unknown (does not affect DVB-T).
  418. */
  419. struct em28xx_reg_seq terratec_htc_stick_init[] = {
  420. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  421. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  422. {EM2874_R80_GPIO, 0xe6, 0xff, 50},
  423. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  424. { -1, -1, -1, -1},
  425. };
  426. struct em28xx_reg_seq terratec_htc_stick_end[] = {
  427. {EM2874_R80_GPIO, 0xb6, 0xff, 100},
  428. {EM2874_R80_GPIO, 0xf6, 0xff, 50},
  429. { -1, -1, -1, -1},
  430. };
  431. /* Init the analog decoder? */
  432. struct {
  433. unsigned char r[4];
  434. int len;
  435. } regs[] = {
  436. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  437. {{ 0x01, 0x02 }, 2},
  438. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  439. {{ 0x01, 0x00 }, 2},
  440. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  441. };
  442. em28xx_gpio_set(dev, terratec_htc_stick_init);
  443. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  444. msleep(10);
  445. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  446. msleep(10);
  447. dev->i2c_client.addr = 0x82 >> 1;
  448. for (i = 0; i < ARRAY_SIZE(regs); i++)
  449. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  450. em28xx_gpio_set(dev, terratec_htc_stick_end);
  451. };
  452. static void pctv_520e_init(struct em28xx *dev)
  453. {
  454. /*
  455. * Init AVF4910B analog decoder. Looks like I2C traffic to
  456. * digital demodulator and tuner are routed via AVF4910B.
  457. */
  458. int i;
  459. struct {
  460. unsigned char r[4];
  461. int len;
  462. } regs[] = {
  463. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  464. {{ 0x01, 0x02 }, 2},
  465. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  466. {{ 0x01, 0x00 }, 2},
  467. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  468. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  469. {{ 0x01, 0x00 }, 2},
  470. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  471. };
  472. dev->i2c_client.addr = 0x82 >> 1; /* 0x41 */
  473. for (i = 0; i < ARRAY_SIZE(regs); i++)
  474. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  475. };
  476. static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
  477. {
  478. /* Values extracted from a USB trace of the Terratec Windows driver */
  479. static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
  480. static u8 reset[] = { RESET, 0x80 };
  481. static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
  482. static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
  483. static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
  484. static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
  485. static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
  486. static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
  487. static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
  488. static u8 tuner_go[] = { TUNER_GO, 0x01};
  489. mt352_write(fe, clock_config, sizeof(clock_config));
  490. udelay(200);
  491. mt352_write(fe, reset, sizeof(reset));
  492. mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
  493. mt352_write(fe, agc_cfg, sizeof(agc_cfg));
  494. mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
  495. mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
  496. mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
  497. mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
  498. mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
  499. mt352_write(fe, tuner_go, sizeof(tuner_go));
  500. return 0;
  501. }
  502. static struct mt352_config terratec_xs_mt352_cfg = {
  503. .demod_address = (0x1e >> 1),
  504. .no_tuner = 1,
  505. .if2 = 45600,
  506. .demod_init = em28xx_mt352_terratec_xs_init,
  507. };
  508. static struct tda10023_config em28xx_tda10023_config = {
  509. .demod_address = 0x0c,
  510. .invert = 1,
  511. };
  512. static struct cxd2820r_config em28xx_cxd2820r_config = {
  513. .i2c_address = (0xd8 >> 1),
  514. .ts_mode = CXD2820R_TS_SERIAL,
  515. /* enable LNA for DVB-T, DVB-T2 and DVB-C */
  516. .gpio_dvbt[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  517. .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  518. .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  519. };
  520. static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
  521. .output_opt = TDA18271_OUTPUT_LT_OFF,
  522. .gate = TDA18271_GATE_DIGITAL,
  523. };
  524. static const struct tda10071_config em28xx_tda10071_config = {
  525. .i2c_address = 0x55, /* (0xaa >> 1) */
  526. .i2c_wr_max = 64,
  527. .ts_mode = TDA10071_TS_SERIAL,
  528. .spec_inv = 0,
  529. .xtal = 40444000, /* 40.444 MHz */
  530. .pll_multiplier = 20,
  531. };
  532. static const struct a8293_config em28xx_a8293_config = {
  533. .i2c_addr = 0x08, /* (0x10 >> 1) */
  534. };
  535. static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
  536. .demod_address = (0x1e >> 1),
  537. .disable_i2c_gate_ctrl = 1,
  538. .no_tuner = 1,
  539. .parallel_ts = 1,
  540. };
  541. static struct qt1010_config em28xx_qt1010_config = {
  542. .i2c_address = 0x62
  543. };
  544. /* ------------------------------------------------------------------ */
  545. static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
  546. {
  547. struct dvb_frontend *fe;
  548. struct xc2028_config cfg;
  549. memset(&cfg, 0, sizeof(cfg));
  550. cfg.i2c_adap = &dev->i2c_adap;
  551. cfg.i2c_addr = addr;
  552. if (!dev->dvb->fe[0]) {
  553. em28xx_errdev("/2: dvb frontend not attached. "
  554. "Can't attach xc3028\n");
  555. return -EINVAL;
  556. }
  557. fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
  558. if (!fe) {
  559. em28xx_errdev("/2: xc3028 attach failed\n");
  560. dvb_frontend_detach(dev->dvb->fe[0]);
  561. dev->dvb->fe[0] = NULL;
  562. return -EINVAL;
  563. }
  564. em28xx_info("%s/2: xc3028 attached\n", dev->name);
  565. return 0;
  566. }
  567. /* ------------------------------------------------------------------ */
  568. static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
  569. struct em28xx *dev, struct device *device)
  570. {
  571. int result;
  572. mutex_init(&dvb->lock);
  573. /* register adapter */
  574. result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
  575. adapter_nr);
  576. if (result < 0) {
  577. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  578. dev->name, result);
  579. goto fail_adapter;
  580. }
  581. /* Ensure all frontends negotiate bus access */
  582. dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  583. if (dvb->fe[1])
  584. dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  585. dvb->adapter.priv = dev;
  586. /* register frontend */
  587. result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
  588. if (result < 0) {
  589. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  590. dev->name, result);
  591. goto fail_frontend0;
  592. }
  593. /* register 2nd frontend */
  594. if (dvb->fe[1]) {
  595. result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
  596. if (result < 0) {
  597. printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
  598. dev->name, result);
  599. goto fail_frontend1;
  600. }
  601. }
  602. /* register demux stuff */
  603. dvb->demux.dmx.capabilities =
  604. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  605. DMX_MEMORY_BASED_FILTERING;
  606. dvb->demux.priv = dvb;
  607. dvb->demux.filternum = 256;
  608. dvb->demux.feednum = 256;
  609. dvb->demux.start_feed = em28xx_start_feed;
  610. dvb->demux.stop_feed = em28xx_stop_feed;
  611. result = dvb_dmx_init(&dvb->demux);
  612. if (result < 0) {
  613. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  614. dev->name, result);
  615. goto fail_dmx;
  616. }
  617. dvb->dmxdev.filternum = 256;
  618. dvb->dmxdev.demux = &dvb->demux.dmx;
  619. dvb->dmxdev.capabilities = 0;
  620. result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
  621. if (result < 0) {
  622. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  623. dev->name, result);
  624. goto fail_dmxdev;
  625. }
  626. dvb->fe_hw.source = DMX_FRONTEND_0;
  627. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  628. if (result < 0) {
  629. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  630. dev->name, result);
  631. goto fail_fe_hw;
  632. }
  633. dvb->fe_mem.source = DMX_MEMORY_FE;
  634. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  635. if (result < 0) {
  636. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  637. dev->name, result);
  638. goto fail_fe_mem;
  639. }
  640. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  641. if (result < 0) {
  642. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  643. dev->name, result);
  644. goto fail_fe_conn;
  645. }
  646. /* register network adapter */
  647. dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
  648. return 0;
  649. fail_fe_conn:
  650. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  651. fail_fe_mem:
  652. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  653. fail_fe_hw:
  654. dvb_dmxdev_release(&dvb->dmxdev);
  655. fail_dmxdev:
  656. dvb_dmx_release(&dvb->demux);
  657. fail_dmx:
  658. if (dvb->fe[1])
  659. dvb_unregister_frontend(dvb->fe[1]);
  660. dvb_unregister_frontend(dvb->fe[0]);
  661. fail_frontend1:
  662. if (dvb->fe[1])
  663. dvb_frontend_detach(dvb->fe[1]);
  664. fail_frontend0:
  665. dvb_frontend_detach(dvb->fe[0]);
  666. dvb_unregister_adapter(&dvb->adapter);
  667. fail_adapter:
  668. return result;
  669. }
  670. static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
  671. {
  672. dvb_net_release(&dvb->net);
  673. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  674. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  675. dvb_dmxdev_release(&dvb->dmxdev);
  676. dvb_dmx_release(&dvb->demux);
  677. if (dvb->fe[1])
  678. dvb_unregister_frontend(dvb->fe[1]);
  679. dvb_unregister_frontend(dvb->fe[0]);
  680. if (dvb->fe[1] && !dvb->dont_attach_fe1)
  681. dvb_frontend_detach(dvb->fe[1]);
  682. dvb_frontend_detach(dvb->fe[0]);
  683. dvb_unregister_adapter(&dvb->adapter);
  684. }
  685. static int em28xx_dvb_init(struct em28xx *dev)
  686. {
  687. int result = 0, mfe_shared = 0;
  688. struct em28xx_dvb *dvb;
  689. if (!dev->board.has_dvb) {
  690. /* This device does not support the extension */
  691. printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
  692. return 0;
  693. }
  694. dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
  695. if (dvb == NULL) {
  696. em28xx_info("em28xx_dvb: memory allocation failed\n");
  697. return -ENOMEM;
  698. }
  699. dev->dvb = dvb;
  700. dvb->fe[0] = dvb->fe[1] = NULL;
  701. mutex_lock(&dev->lock);
  702. em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  703. /* init frontend */
  704. switch (dev->model) {
  705. case EM2874_BOARD_LEADERSHIP_ISDBT:
  706. dvb->fe[0] = dvb_attach(s921_attach,
  707. &sharp_isdbt, &dev->i2c_adap);
  708. if (!dvb->fe[0]) {
  709. result = -EINVAL;
  710. goto out_free;
  711. }
  712. break;
  713. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
  714. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  715. case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
  716. case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
  717. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  718. &em2880_lgdt3303_dev,
  719. &dev->i2c_adap);
  720. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  721. result = -EINVAL;
  722. goto out_free;
  723. }
  724. break;
  725. case EM2880_BOARD_KWORLD_DVB_310U:
  726. dvb->fe[0] = dvb_attach(zl10353_attach,
  727. &em28xx_zl10353_with_xc3028,
  728. &dev->i2c_adap);
  729. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  730. result = -EINVAL;
  731. goto out_free;
  732. }
  733. break;
  734. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
  735. case EM2882_BOARD_TERRATEC_HYBRID_XS:
  736. case EM2880_BOARD_EMPIRE_DUAL_TV:
  737. dvb->fe[0] = dvb_attach(zl10353_attach,
  738. &em28xx_zl10353_xc3028_no_i2c_gate,
  739. &dev->i2c_adap);
  740. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  741. result = -EINVAL;
  742. goto out_free;
  743. }
  744. break;
  745. case EM2880_BOARD_TERRATEC_HYBRID_XS:
  746. case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
  747. case EM2881_BOARD_PINNACLE_HYBRID_PRO:
  748. case EM2882_BOARD_DIKOM_DK300:
  749. case EM2882_BOARD_KWORLD_VS_DVBT:
  750. dvb->fe[0] = dvb_attach(zl10353_attach,
  751. &em28xx_zl10353_xc3028_no_i2c_gate,
  752. &dev->i2c_adap);
  753. if (dvb->fe[0] == NULL) {
  754. /* This board could have either a zl10353 or a mt352.
  755. If the chip id isn't for zl10353, try mt352 */
  756. dvb->fe[0] = dvb_attach(mt352_attach,
  757. &terratec_xs_mt352_cfg,
  758. &dev->i2c_adap);
  759. }
  760. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  761. result = -EINVAL;
  762. goto out_free;
  763. }
  764. break;
  765. case EM2870_BOARD_KWORLD_355U:
  766. dvb->fe[0] = dvb_attach(zl10353_attach,
  767. &em28xx_zl10353_no_i2c_gate_dev,
  768. &dev->i2c_adap);
  769. if (dvb->fe[0] != NULL)
  770. dvb_attach(qt1010_attach, dvb->fe[0],
  771. &dev->i2c_adap, &em28xx_qt1010_config);
  772. break;
  773. case EM2883_BOARD_KWORLD_HYBRID_330U:
  774. case EM2882_BOARD_EVGA_INDTUBE:
  775. dvb->fe[0] = dvb_attach(s5h1409_attach,
  776. &em28xx_s5h1409_with_xc3028,
  777. &dev->i2c_adap);
  778. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  779. result = -EINVAL;
  780. goto out_free;
  781. }
  782. break;
  783. case EM2882_BOARD_KWORLD_ATSC_315U:
  784. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  785. &em2880_lgdt3303_dev,
  786. &dev->i2c_adap);
  787. if (dvb->fe[0] != NULL) {
  788. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  789. &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
  790. result = -EINVAL;
  791. goto out_free;
  792. }
  793. }
  794. break;
  795. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
  796. case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
  797. dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
  798. &dev->i2c_adap, &dev->udev->dev);
  799. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  800. result = -EINVAL;
  801. goto out_free;
  802. }
  803. break;
  804. case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
  805. /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
  806. dvb->fe[0] = dvb_attach(tda10023_attach,
  807. &em28xx_tda10023_config,
  808. &dev->i2c_adap, 0x48);
  809. if (dvb->fe[0]) {
  810. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  811. &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
  812. result = -EINVAL;
  813. goto out_free;
  814. }
  815. }
  816. break;
  817. case EM2870_BOARD_KWORLD_A340:
  818. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  819. &em2870_lgdt3304_dev,
  820. &dev->i2c_adap);
  821. if (dvb->fe[0] != NULL)
  822. dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  823. &dev->i2c_adap, &kworld_a340_config);
  824. break;
  825. case EM28174_BOARD_PCTV_290E:
  826. dvb->fe[0] = dvb_attach(cxd2820r_attach,
  827. &em28xx_cxd2820r_config,
  828. &dev->i2c_adap);
  829. if (dvb->fe[0]) {
  830. /* FE 0 attach tuner */
  831. if (!dvb_attach(tda18271_attach,
  832. dvb->fe[0],
  833. 0x60,
  834. &dev->i2c_adap,
  835. &em28xx_cxd2820r_tda18271_config)) {
  836. dvb_frontend_detach(dvb->fe[0]);
  837. result = -EINVAL;
  838. goto out_free;
  839. }
  840. }
  841. break;
  842. case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
  843. {
  844. struct xc5000_config cfg;
  845. hauppauge_hvr930c_init(dev);
  846. dvb->fe[0] = dvb_attach(drxk_attach,
  847. &hauppauge_930c_drxk, &dev->i2c_adap);
  848. if (!dvb->fe[0]) {
  849. result = -EINVAL;
  850. goto out_free;
  851. }
  852. /* FIXME: do we need a pll semaphore? */
  853. dvb->fe[0]->sec_priv = dvb;
  854. sema_init(&dvb->pll_mutex, 1);
  855. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  856. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  857. /* Attach xc5000 */
  858. memset(&cfg, 0, sizeof(cfg));
  859. cfg.i2c_address = 0x61;
  860. cfg.if_khz = 4000;
  861. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  862. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  863. if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap,
  864. &cfg)) {
  865. result = -EINVAL;
  866. goto out_free;
  867. }
  868. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  869. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  870. break;
  871. }
  872. case EM2884_BOARD_TERRATEC_H5:
  873. terratec_h5_init(dev);
  874. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap);
  875. if (!dvb->fe[0]) {
  876. result = -EINVAL;
  877. goto out_free;
  878. }
  879. /* FIXME: do we need a pll semaphore? */
  880. dvb->fe[0]->sec_priv = dvb;
  881. sema_init(&dvb->pll_mutex, 1);
  882. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  883. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  884. /* Attach tda18271 to DVB-C frontend */
  885. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  886. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  887. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
  888. result = -EINVAL;
  889. goto out_free;
  890. }
  891. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  892. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  893. break;
  894. case EM28174_BOARD_PCTV_460E:
  895. /* attach demod */
  896. dvb->fe[0] = dvb_attach(tda10071_attach,
  897. &em28xx_tda10071_config, &dev->i2c_adap);
  898. /* attach SEC */
  899. if (dvb->fe[0])
  900. dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap,
  901. &em28xx_a8293_config);
  902. break;
  903. case EM2874_BOARD_MAXMEDIA_UB425_TC:
  904. /* attach demodulator */
  905. dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
  906. &dev->i2c_adap);
  907. if (dvb->fe[0]) {
  908. /* disable I2C-gate */
  909. dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
  910. /* attach tuner */
  911. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0],
  912. &dev->i2c_adap, 0x60)) {
  913. dvb_frontend_detach(dvb->fe[0]);
  914. result = -EINVAL;
  915. goto out_free;
  916. }
  917. }
  918. /* TODO: we need drx-3913k firmware in order to support DVB-T */
  919. em28xx_info("MaxMedia UB425-TC: only DVB-C supported by that " \
  920. "driver version\n");
  921. break;
  922. case EM2884_BOARD_PCTV_510E:
  923. case EM2884_BOARD_PCTV_520E:
  924. pctv_520e_init(dev);
  925. /* attach demodulator */
  926. dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
  927. &dev->i2c_adap);
  928. if (dvb->fe[0]) {
  929. /* attach tuner */
  930. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  931. &dev->i2c_adap,
  932. &em28xx_cxd2820r_tda18271_config)) {
  933. dvb_frontend_detach(dvb->fe[0]);
  934. result = -EINVAL;
  935. goto out_free;
  936. }
  937. }
  938. break;
  939. case EM2884_BOARD_CINERGY_HTC_STICK:
  940. terratec_htc_stick_init(dev);
  941. /* attach demodulator */
  942. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
  943. &dev->i2c_adap);
  944. if (!dvb->fe[0]) {
  945. result = -EINVAL;
  946. goto out_free;
  947. }
  948. /* Attach the demodulator. */
  949. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  950. &dev->i2c_adap,
  951. &em28xx_cxd2820r_tda18271_config)) {
  952. result = -EINVAL;
  953. goto out_free;
  954. }
  955. break;
  956. default:
  957. em28xx_errdev("/2: The frontend of your DVB/ATSC card"
  958. " isn't supported yet\n");
  959. break;
  960. }
  961. if (NULL == dvb->fe[0]) {
  962. em28xx_errdev("/2: frontend initialization failed\n");
  963. result = -EINVAL;
  964. goto out_free;
  965. }
  966. /* define general-purpose callback pointer */
  967. dvb->fe[0]->callback = em28xx_tuner_callback;
  968. if (dvb->fe[1])
  969. dvb->fe[1]->callback = em28xx_tuner_callback;
  970. /* register everything */
  971. result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
  972. if (result < 0)
  973. goto out_free;
  974. /* MFE lock */
  975. dvb->adapter.mfe_shared = mfe_shared;
  976. em28xx_info("Successfully loaded em28xx-dvb\n");
  977. ret:
  978. em28xx_set_mode(dev, EM28XX_SUSPEND);
  979. mutex_unlock(&dev->lock);
  980. return result;
  981. out_free:
  982. kfree(dvb);
  983. dev->dvb = NULL;
  984. goto ret;
  985. }
  986. static inline void prevent_sleep(struct dvb_frontend_ops *ops)
  987. {
  988. ops->set_voltage = NULL;
  989. ops->sleep = NULL;
  990. ops->tuner_ops.sleep = NULL;
  991. }
  992. static int em28xx_dvb_fini(struct em28xx *dev)
  993. {
  994. if (!dev->board.has_dvb) {
  995. /* This device does not support the extension */
  996. return 0;
  997. }
  998. if (dev->dvb) {
  999. struct em28xx_dvb *dvb = dev->dvb;
  1000. if (dev->state & DEV_DISCONNECTED) {
  1001. /* We cannot tell the device to sleep
  1002. * once it has been unplugged. */
  1003. if (dvb->fe[0])
  1004. prevent_sleep(&dvb->fe[0]->ops);
  1005. if (dvb->fe[1])
  1006. prevent_sleep(&dvb->fe[1]->ops);
  1007. }
  1008. em28xx_unregister_dvb(dvb);
  1009. kfree(dvb);
  1010. dev->dvb = NULL;
  1011. }
  1012. return 0;
  1013. }
  1014. static struct em28xx_ops dvb_ops = {
  1015. .id = EM28XX_DVB,
  1016. .name = "Em28xx dvb Extension",
  1017. .init = em28xx_dvb_init,
  1018. .fini = em28xx_dvb_fini,
  1019. };
  1020. static int __init em28xx_dvb_register(void)
  1021. {
  1022. return em28xx_register_extension(&dvb_ops);
  1023. }
  1024. static void __exit em28xx_dvb_unregister(void)
  1025. {
  1026. em28xx_unregister_extension(&dvb_ops);
  1027. }
  1028. module_init(em28xx_dvb_register);
  1029. module_exit(em28xx_dvb_unregister);