em28xx-dvb.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. };
  260. static struct drxk_config hauppauge_930c_drxk = {
  261. .adr = 0x29,
  262. .single_master = 1,
  263. .no_i2c_bridge = 1,
  264. .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
  265. .chunk_size = 56,
  266. };
  267. struct drxk_config terratec_htc_stick_drxk = {
  268. .adr = 0x29,
  269. .single_master = 1,
  270. .no_i2c_bridge = 1,
  271. .microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
  272. .chunk_size = 54,
  273. /* Required for the antenna_gpio to disable LNA. */
  274. .antenna_dvbt = true,
  275. /* The windows driver uses the same. This will disable LNA. */
  276. .antenna_gpio = 0x6,
  277. };
  278. static struct drxk_config maxmedia_ub425_tc_drxk = {
  279. .adr = 0x29,
  280. .single_master = 1,
  281. .no_i2c_bridge = 1,
  282. };
  283. static struct drxk_config pctv_520e_drxk = {
  284. .adr = 0x29,
  285. .single_master = 1,
  286. .microcode_name = "dvb-demod-drxk-pctv.fw",
  287. .chunk_size = 58,
  288. .antenna_dvbt = true, /* disable LNA */
  289. .antenna_gpio = (1 << 2), /* disable LNA */
  290. };
  291. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  292. {
  293. struct em28xx_dvb *dvb = fe->sec_priv;
  294. int status;
  295. if (!dvb)
  296. return -EINVAL;
  297. if (enable) {
  298. down(&dvb->pll_mutex);
  299. status = dvb->gate_ctrl(fe, 1);
  300. } else {
  301. status = dvb->gate_ctrl(fe, 0);
  302. up(&dvb->pll_mutex);
  303. }
  304. return status;
  305. }
  306. static void hauppauge_hvr930c_init(struct em28xx *dev)
  307. {
  308. int i;
  309. struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
  310. {EM2874_R80_GPIO, 0xff, 0xff, 0x65},
  311. {EM2874_R80_GPIO, 0xfb, 0xff, 0x32},
  312. {EM2874_R80_GPIO, 0xff, 0xff, 0xb8},
  313. { -1, -1, -1, -1},
  314. };
  315. struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
  316. {EM2874_R80_GPIO, 0xef, 0xff, 0x01},
  317. {EM2874_R80_GPIO, 0xaf, 0xff, 0x65},
  318. {EM2874_R80_GPIO, 0xef, 0xff, 0x76},
  319. {EM2874_R80_GPIO, 0xef, 0xff, 0x01},
  320. {EM2874_R80_GPIO, 0xcf, 0xff, 0x0b},
  321. {EM2874_R80_GPIO, 0xef, 0xff, 0x40},
  322. {EM2874_R80_GPIO, 0xcf, 0xff, 0x65},
  323. {EM2874_R80_GPIO, 0xef, 0xff, 0x65},
  324. {EM2874_R80_GPIO, 0xcf, 0xff, 0x0b},
  325. {EM2874_R80_GPIO, 0xef, 0xff, 0x65},
  326. { -1, -1, -1, -1},
  327. };
  328. struct {
  329. unsigned char r[4];
  330. int len;
  331. } regs[] = {
  332. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  333. {{ 0x01, 0x02 }, 2},
  334. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  335. {{ 0x01, 0x00 }, 2},
  336. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  337. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  338. {{ 0x01, 0x00 }, 2},
  339. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  340. {{ 0x04, 0x00 }, 2},
  341. {{ 0x00, 0x04 }, 2},
  342. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  343. {{ 0x04, 0x14 }, 2},
  344. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  345. };
  346. em28xx_gpio_set(dev, hauppauge_hvr930c_init);
  347. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  348. msleep(10);
  349. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  350. msleep(10);
  351. dev->i2c_client.addr = 0x82 >> 1;
  352. for (i = 0; i < ARRAY_SIZE(regs); i++)
  353. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  354. em28xx_gpio_set(dev, hauppauge_hvr930c_end);
  355. msleep(100);
  356. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  357. msleep(30);
  358. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  359. msleep(10);
  360. }
  361. static void terratec_h5_init(struct em28xx *dev)
  362. {
  363. int i;
  364. struct em28xx_reg_seq terratec_h5_init[] = {
  365. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  366. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  367. {EM2874_R80_GPIO, 0xf2, 0xff, 50},
  368. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  369. { -1, -1, -1, -1},
  370. };
  371. struct em28xx_reg_seq terratec_h5_end[] = {
  372. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  373. {EM2874_R80_GPIO, 0xa6, 0xff, 50},
  374. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  375. { -1, -1, -1, -1},
  376. };
  377. struct {
  378. unsigned char r[4];
  379. int len;
  380. } regs[] = {
  381. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  382. {{ 0x01, 0x02 }, 2},
  383. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  384. {{ 0x01, 0x00 }, 2},
  385. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  386. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  387. {{ 0x01, 0x00 }, 2},
  388. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  389. {{ 0x04, 0x00 }, 2},
  390. {{ 0x00, 0x04 }, 2},
  391. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  392. {{ 0x04, 0x14 }, 2},
  393. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  394. };
  395. em28xx_gpio_set(dev, terratec_h5_init);
  396. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  397. msleep(10);
  398. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  399. msleep(10);
  400. dev->i2c_client.addr = 0x82 >> 1;
  401. for (i = 0; i < ARRAY_SIZE(regs); i++)
  402. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  403. em28xx_gpio_set(dev, terratec_h5_end);
  404. };
  405. static void terratec_htc_stick_init(struct em28xx *dev)
  406. {
  407. int i;
  408. /*
  409. * GPIO configuration:
  410. * 0xff: unknown (does not affect DVB-T).
  411. * 0xf6: DRX-K (demodulator).
  412. * 0xe6: unknown (does not affect DVB-T).
  413. * 0xb6: unknown (does not affect DVB-T).
  414. */
  415. struct em28xx_reg_seq terratec_htc_stick_init[] = {
  416. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  417. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  418. {EM2874_R80_GPIO, 0xe6, 0xff, 50},
  419. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  420. { -1, -1, -1, -1},
  421. };
  422. struct em28xx_reg_seq terratec_htc_stick_end[] = {
  423. {EM2874_R80_GPIO, 0xb6, 0xff, 100},
  424. {EM2874_R80_GPIO, 0xf6, 0xff, 50},
  425. { -1, -1, -1, -1},
  426. };
  427. /* Init the analog decoder? */
  428. struct {
  429. unsigned char r[4];
  430. int len;
  431. } regs[] = {
  432. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  433. {{ 0x01, 0x02 }, 2},
  434. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  435. {{ 0x01, 0x00 }, 2},
  436. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  437. };
  438. em28xx_gpio_set(dev, terratec_htc_stick_init);
  439. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  440. msleep(10);
  441. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  442. msleep(10);
  443. dev->i2c_client.addr = 0x82 >> 1;
  444. for (i = 0; i < ARRAY_SIZE(regs); i++)
  445. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  446. em28xx_gpio_set(dev, terratec_htc_stick_end);
  447. };
  448. static void pctv_520e_init(struct em28xx *dev)
  449. {
  450. /*
  451. * Init AVF4910B analog decoder. Looks like I2C traffic to
  452. * digital demodulator and tuner are routed via AVF4910B.
  453. */
  454. int i;
  455. struct {
  456. unsigned char r[4];
  457. int len;
  458. } regs[] = {
  459. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  460. {{ 0x01, 0x02 }, 2},
  461. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  462. {{ 0x01, 0x00 }, 2},
  463. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  464. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  465. {{ 0x01, 0x00 }, 2},
  466. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  467. };
  468. dev->i2c_client.addr = 0x82 >> 1; /* 0x41 */
  469. for (i = 0; i < ARRAY_SIZE(regs); i++)
  470. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  471. };
  472. static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
  473. {
  474. /* Values extracted from a USB trace of the Terratec Windows driver */
  475. static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
  476. static u8 reset[] = { RESET, 0x80 };
  477. static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
  478. static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
  479. static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
  480. static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
  481. static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
  482. static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
  483. static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
  484. static u8 tuner_go[] = { TUNER_GO, 0x01};
  485. mt352_write(fe, clock_config, sizeof(clock_config));
  486. udelay(200);
  487. mt352_write(fe, reset, sizeof(reset));
  488. mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
  489. mt352_write(fe, agc_cfg, sizeof(agc_cfg));
  490. mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
  491. mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
  492. mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
  493. mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
  494. mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
  495. mt352_write(fe, tuner_go, sizeof(tuner_go));
  496. return 0;
  497. }
  498. static struct mt352_config terratec_xs_mt352_cfg = {
  499. .demod_address = (0x1e >> 1),
  500. .no_tuner = 1,
  501. .if2 = 45600,
  502. .demod_init = em28xx_mt352_terratec_xs_init,
  503. };
  504. static struct tda10023_config em28xx_tda10023_config = {
  505. .demod_address = 0x0c,
  506. .invert = 1,
  507. };
  508. static struct cxd2820r_config em28xx_cxd2820r_config = {
  509. .i2c_address = (0xd8 >> 1),
  510. .ts_mode = CXD2820R_TS_SERIAL,
  511. /* enable LNA for DVB-T, DVB-T2 and DVB-C */
  512. .gpio_dvbt[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  513. .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  514. .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  515. };
  516. static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
  517. .output_opt = TDA18271_OUTPUT_LT_OFF,
  518. .gate = TDA18271_GATE_DIGITAL,
  519. };
  520. static const struct tda10071_config em28xx_tda10071_config = {
  521. .i2c_address = 0x55, /* (0xaa >> 1) */
  522. .i2c_wr_max = 64,
  523. .ts_mode = TDA10071_TS_SERIAL,
  524. .spec_inv = 0,
  525. .xtal = 40444000, /* 40.444 MHz */
  526. .pll_multiplier = 20,
  527. };
  528. static const struct a8293_config em28xx_a8293_config = {
  529. .i2c_addr = 0x08, /* (0x10 >> 1) */
  530. };
  531. static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
  532. .demod_address = (0x1e >> 1),
  533. .disable_i2c_gate_ctrl = 1,
  534. .no_tuner = 1,
  535. .parallel_ts = 1,
  536. };
  537. static struct qt1010_config em28xx_qt1010_config = {
  538. .i2c_address = 0x62
  539. };
  540. /* ------------------------------------------------------------------ */
  541. static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
  542. {
  543. struct dvb_frontend *fe;
  544. struct xc2028_config cfg;
  545. memset(&cfg, 0, sizeof(cfg));
  546. cfg.i2c_adap = &dev->i2c_adap;
  547. cfg.i2c_addr = addr;
  548. if (!dev->dvb->fe[0]) {
  549. em28xx_errdev("/2: dvb frontend not attached. "
  550. "Can't attach xc3028\n");
  551. return -EINVAL;
  552. }
  553. fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
  554. if (!fe) {
  555. em28xx_errdev("/2: xc3028 attach failed\n");
  556. dvb_frontend_detach(dev->dvb->fe[0]);
  557. dev->dvb->fe[0] = NULL;
  558. return -EINVAL;
  559. }
  560. em28xx_info("%s/2: xc3028 attached\n", dev->name);
  561. return 0;
  562. }
  563. /* ------------------------------------------------------------------ */
  564. static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
  565. struct em28xx *dev, struct device *device)
  566. {
  567. int result;
  568. mutex_init(&dvb->lock);
  569. /* register adapter */
  570. result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
  571. adapter_nr);
  572. if (result < 0) {
  573. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  574. dev->name, result);
  575. goto fail_adapter;
  576. }
  577. /* Ensure all frontends negotiate bus access */
  578. dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  579. if (dvb->fe[1])
  580. dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  581. dvb->adapter.priv = dev;
  582. /* register frontend */
  583. result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
  584. if (result < 0) {
  585. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  586. dev->name, result);
  587. goto fail_frontend0;
  588. }
  589. /* register 2nd frontend */
  590. if (dvb->fe[1]) {
  591. result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
  592. if (result < 0) {
  593. printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
  594. dev->name, result);
  595. goto fail_frontend1;
  596. }
  597. }
  598. /* register demux stuff */
  599. dvb->demux.dmx.capabilities =
  600. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  601. DMX_MEMORY_BASED_FILTERING;
  602. dvb->demux.priv = dvb;
  603. dvb->demux.filternum = 256;
  604. dvb->demux.feednum = 256;
  605. dvb->demux.start_feed = em28xx_start_feed;
  606. dvb->demux.stop_feed = em28xx_stop_feed;
  607. result = dvb_dmx_init(&dvb->demux);
  608. if (result < 0) {
  609. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  610. dev->name, result);
  611. goto fail_dmx;
  612. }
  613. dvb->dmxdev.filternum = 256;
  614. dvb->dmxdev.demux = &dvb->demux.dmx;
  615. dvb->dmxdev.capabilities = 0;
  616. result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
  617. if (result < 0) {
  618. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  619. dev->name, result);
  620. goto fail_dmxdev;
  621. }
  622. dvb->fe_hw.source = DMX_FRONTEND_0;
  623. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  624. if (result < 0) {
  625. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  626. dev->name, result);
  627. goto fail_fe_hw;
  628. }
  629. dvb->fe_mem.source = DMX_MEMORY_FE;
  630. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  631. if (result < 0) {
  632. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  633. dev->name, result);
  634. goto fail_fe_mem;
  635. }
  636. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  637. if (result < 0) {
  638. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  639. dev->name, result);
  640. goto fail_fe_conn;
  641. }
  642. /* register network adapter */
  643. dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
  644. return 0;
  645. fail_fe_conn:
  646. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  647. fail_fe_mem:
  648. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  649. fail_fe_hw:
  650. dvb_dmxdev_release(&dvb->dmxdev);
  651. fail_dmxdev:
  652. dvb_dmx_release(&dvb->demux);
  653. fail_dmx:
  654. if (dvb->fe[1])
  655. dvb_unregister_frontend(dvb->fe[1]);
  656. dvb_unregister_frontend(dvb->fe[0]);
  657. fail_frontend1:
  658. if (dvb->fe[1])
  659. dvb_frontend_detach(dvb->fe[1]);
  660. fail_frontend0:
  661. dvb_frontend_detach(dvb->fe[0]);
  662. dvb_unregister_adapter(&dvb->adapter);
  663. fail_adapter:
  664. return result;
  665. }
  666. static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
  667. {
  668. dvb_net_release(&dvb->net);
  669. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  670. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  671. dvb_dmxdev_release(&dvb->dmxdev);
  672. dvb_dmx_release(&dvb->demux);
  673. if (dvb->fe[1])
  674. dvb_unregister_frontend(dvb->fe[1]);
  675. dvb_unregister_frontend(dvb->fe[0]);
  676. if (dvb->fe[1] && !dvb->dont_attach_fe1)
  677. dvb_frontend_detach(dvb->fe[1]);
  678. dvb_frontend_detach(dvb->fe[0]);
  679. dvb_unregister_adapter(&dvb->adapter);
  680. }
  681. static int em28xx_dvb_init(struct em28xx *dev)
  682. {
  683. int result = 0, mfe_shared = 0;
  684. struct em28xx_dvb *dvb;
  685. if (!dev->board.has_dvb) {
  686. /* This device does not support the extension */
  687. printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
  688. return 0;
  689. }
  690. dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
  691. if (dvb == NULL) {
  692. em28xx_info("em28xx_dvb: memory allocation failed\n");
  693. return -ENOMEM;
  694. }
  695. dev->dvb = dvb;
  696. dvb->fe[0] = dvb->fe[1] = NULL;
  697. mutex_lock(&dev->lock);
  698. em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  699. /* init frontend */
  700. switch (dev->model) {
  701. case EM2874_BOARD_LEADERSHIP_ISDBT:
  702. dvb->fe[0] = dvb_attach(s921_attach,
  703. &sharp_isdbt, &dev->i2c_adap);
  704. if (!dvb->fe[0]) {
  705. result = -EINVAL;
  706. goto out_free;
  707. }
  708. break;
  709. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
  710. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  711. case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
  712. case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
  713. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  714. &em2880_lgdt3303_dev,
  715. &dev->i2c_adap);
  716. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  717. result = -EINVAL;
  718. goto out_free;
  719. }
  720. break;
  721. case EM2880_BOARD_KWORLD_DVB_310U:
  722. dvb->fe[0] = dvb_attach(zl10353_attach,
  723. &em28xx_zl10353_with_xc3028,
  724. &dev->i2c_adap);
  725. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  726. result = -EINVAL;
  727. goto out_free;
  728. }
  729. break;
  730. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
  731. case EM2882_BOARD_TERRATEC_HYBRID_XS:
  732. case EM2880_BOARD_EMPIRE_DUAL_TV:
  733. dvb->fe[0] = dvb_attach(zl10353_attach,
  734. &em28xx_zl10353_xc3028_no_i2c_gate,
  735. &dev->i2c_adap);
  736. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  737. result = -EINVAL;
  738. goto out_free;
  739. }
  740. break;
  741. case EM2880_BOARD_TERRATEC_HYBRID_XS:
  742. case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
  743. case EM2881_BOARD_PINNACLE_HYBRID_PRO:
  744. case EM2882_BOARD_DIKOM_DK300:
  745. case EM2882_BOARD_KWORLD_VS_DVBT:
  746. dvb->fe[0] = dvb_attach(zl10353_attach,
  747. &em28xx_zl10353_xc3028_no_i2c_gate,
  748. &dev->i2c_adap);
  749. if (dvb->fe[0] == NULL) {
  750. /* This board could have either a zl10353 or a mt352.
  751. If the chip id isn't for zl10353, try mt352 */
  752. dvb->fe[0] = dvb_attach(mt352_attach,
  753. &terratec_xs_mt352_cfg,
  754. &dev->i2c_adap);
  755. }
  756. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  757. result = -EINVAL;
  758. goto out_free;
  759. }
  760. break;
  761. case EM2870_BOARD_KWORLD_355U:
  762. dvb->fe[0] = dvb_attach(zl10353_attach,
  763. &em28xx_zl10353_no_i2c_gate_dev,
  764. &dev->i2c_adap);
  765. if (dvb->fe[0] != NULL)
  766. dvb_attach(qt1010_attach, dvb->fe[0],
  767. &dev->i2c_adap, &em28xx_qt1010_config);
  768. break;
  769. case EM2883_BOARD_KWORLD_HYBRID_330U:
  770. case EM2882_BOARD_EVGA_INDTUBE:
  771. dvb->fe[0] = dvb_attach(s5h1409_attach,
  772. &em28xx_s5h1409_with_xc3028,
  773. &dev->i2c_adap);
  774. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  775. result = -EINVAL;
  776. goto out_free;
  777. }
  778. break;
  779. case EM2882_BOARD_KWORLD_ATSC_315U:
  780. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  781. &em2880_lgdt3303_dev,
  782. &dev->i2c_adap);
  783. if (dvb->fe[0] != NULL) {
  784. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  785. &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
  786. result = -EINVAL;
  787. goto out_free;
  788. }
  789. }
  790. break;
  791. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
  792. case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
  793. dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
  794. &dev->i2c_adap, &dev->udev->dev);
  795. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  796. result = -EINVAL;
  797. goto out_free;
  798. }
  799. break;
  800. case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
  801. /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
  802. dvb->fe[0] = dvb_attach(tda10023_attach,
  803. &em28xx_tda10023_config,
  804. &dev->i2c_adap, 0x48);
  805. if (dvb->fe[0]) {
  806. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  807. &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
  808. result = -EINVAL;
  809. goto out_free;
  810. }
  811. }
  812. break;
  813. case EM2870_BOARD_KWORLD_A340:
  814. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  815. &em2870_lgdt3304_dev,
  816. &dev->i2c_adap);
  817. if (dvb->fe[0] != NULL)
  818. dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  819. &dev->i2c_adap, &kworld_a340_config);
  820. break;
  821. case EM28174_BOARD_PCTV_290E:
  822. dvb->fe[0] = dvb_attach(cxd2820r_attach,
  823. &em28xx_cxd2820r_config,
  824. &dev->i2c_adap);
  825. if (dvb->fe[0]) {
  826. /* FE 0 attach tuner */
  827. if (!dvb_attach(tda18271_attach,
  828. dvb->fe[0],
  829. 0x60,
  830. &dev->i2c_adap,
  831. &em28xx_cxd2820r_tda18271_config)) {
  832. dvb_frontend_detach(dvb->fe[0]);
  833. result = -EINVAL;
  834. goto out_free;
  835. }
  836. }
  837. break;
  838. case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
  839. {
  840. struct xc5000_config cfg;
  841. hauppauge_hvr930c_init(dev);
  842. dvb->fe[0] = dvb_attach(drxk_attach,
  843. &hauppauge_930c_drxk, &dev->i2c_adap);
  844. if (!dvb->fe[0]) {
  845. result = -EINVAL;
  846. goto out_free;
  847. }
  848. /* FIXME: do we need a pll semaphore? */
  849. dvb->fe[0]->sec_priv = dvb;
  850. sema_init(&dvb->pll_mutex, 1);
  851. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  852. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  853. /* Attach xc5000 */
  854. memset(&cfg, 0, sizeof(cfg));
  855. cfg.i2c_address = 0x61;
  856. cfg.if_khz = 4000;
  857. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  858. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  859. if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap,
  860. &cfg)) {
  861. result = -EINVAL;
  862. goto out_free;
  863. }
  864. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  865. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  866. break;
  867. }
  868. case EM2884_BOARD_TERRATEC_H5:
  869. terratec_h5_init(dev);
  870. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap);
  871. if (!dvb->fe[0]) {
  872. result = -EINVAL;
  873. goto out_free;
  874. }
  875. /* FIXME: do we need a pll semaphore? */
  876. dvb->fe[0]->sec_priv = dvb;
  877. sema_init(&dvb->pll_mutex, 1);
  878. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  879. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  880. /* Attach tda18271 to DVB-C frontend */
  881. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  882. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  883. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
  884. result = -EINVAL;
  885. goto out_free;
  886. }
  887. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  888. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  889. break;
  890. case EM28174_BOARD_PCTV_460E:
  891. /* attach demod */
  892. dvb->fe[0] = dvb_attach(tda10071_attach,
  893. &em28xx_tda10071_config, &dev->i2c_adap);
  894. /* attach SEC */
  895. if (dvb->fe[0])
  896. dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap,
  897. &em28xx_a8293_config);
  898. break;
  899. case EM2874_BOARD_MAXMEDIA_UB425_TC:
  900. /* attach demodulator */
  901. dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
  902. &dev->i2c_adap);
  903. if (dvb->fe[0]) {
  904. /* disable I2C-gate */
  905. dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
  906. /* attach tuner */
  907. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0],
  908. &dev->i2c_adap, 0x60)) {
  909. dvb_frontend_detach(dvb->fe[0]);
  910. result = -EINVAL;
  911. goto out_free;
  912. }
  913. }
  914. /* TODO: we need drx-3913k firmware in order to support DVB-T */
  915. em28xx_info("MaxMedia UB425-TC: only DVB-C supported by that " \
  916. "driver version\n");
  917. break;
  918. case EM2884_BOARD_PCTV_510E:
  919. case EM2884_BOARD_PCTV_520E:
  920. pctv_520e_init(dev);
  921. /* attach demodulator */
  922. dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
  923. &dev->i2c_adap);
  924. if (dvb->fe[0]) {
  925. /* attach tuner */
  926. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  927. &dev->i2c_adap,
  928. &em28xx_cxd2820r_tda18271_config)) {
  929. dvb_frontend_detach(dvb->fe[0]);
  930. result = -EINVAL;
  931. goto out_free;
  932. }
  933. }
  934. break;
  935. case EM2884_BOARD_CINERGY_HTC_STICK:
  936. terratec_htc_stick_init(dev);
  937. /* attach demodulator */
  938. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
  939. &dev->i2c_adap);
  940. if (!dvb->fe[0]) {
  941. result = -EINVAL;
  942. goto out_free;
  943. }
  944. /* Attach the demodulator. */
  945. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  946. &dev->i2c_adap,
  947. &em28xx_cxd2820r_tda18271_config)) {
  948. result = -EINVAL;
  949. goto out_free;
  950. }
  951. break;
  952. default:
  953. em28xx_errdev("/2: The frontend of your DVB/ATSC card"
  954. " isn't supported yet\n");
  955. break;
  956. }
  957. if (NULL == dvb->fe[0]) {
  958. em28xx_errdev("/2: frontend initialization failed\n");
  959. result = -EINVAL;
  960. goto out_free;
  961. }
  962. /* define general-purpose callback pointer */
  963. dvb->fe[0]->callback = em28xx_tuner_callback;
  964. if (dvb->fe[1])
  965. dvb->fe[1]->callback = em28xx_tuner_callback;
  966. /* register everything */
  967. result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
  968. if (result < 0)
  969. goto out_free;
  970. /* MFE lock */
  971. dvb->adapter.mfe_shared = mfe_shared;
  972. em28xx_info("Successfully loaded em28xx-dvb\n");
  973. ret:
  974. em28xx_set_mode(dev, EM28XX_SUSPEND);
  975. mutex_unlock(&dev->lock);
  976. return result;
  977. out_free:
  978. kfree(dvb);
  979. dev->dvb = NULL;
  980. goto ret;
  981. }
  982. static inline void prevent_sleep(struct dvb_frontend_ops *ops)
  983. {
  984. ops->set_voltage = NULL;
  985. ops->sleep = NULL;
  986. ops->tuner_ops.sleep = NULL;
  987. }
  988. static int em28xx_dvb_fini(struct em28xx *dev)
  989. {
  990. if (!dev->board.has_dvb) {
  991. /* This device does not support the extension */
  992. return 0;
  993. }
  994. if (dev->dvb) {
  995. struct em28xx_dvb *dvb = dev->dvb;
  996. if (dev->state & DEV_DISCONNECTED) {
  997. /* We cannot tell the device to sleep
  998. * once it has been unplugged. */
  999. if (dvb->fe[0])
  1000. prevent_sleep(&dvb->fe[0]->ops);
  1001. if (dvb->fe[1])
  1002. prevent_sleep(&dvb->fe[1]->ops);
  1003. }
  1004. em28xx_unregister_dvb(dvb);
  1005. kfree(dvb);
  1006. dev->dvb = NULL;
  1007. }
  1008. return 0;
  1009. }
  1010. static struct em28xx_ops dvb_ops = {
  1011. .id = EM28XX_DVB,
  1012. .name = "Em28xx dvb Extension",
  1013. .init = em28xx_dvb_init,
  1014. .fini = em28xx_dvb_fini,
  1015. };
  1016. static int __init em28xx_dvb_register(void)
  1017. {
  1018. return em28xx_register_extension(&dvb_ops);
  1019. }
  1020. static void __exit em28xx_dvb_unregister(void)
  1021. {
  1022. em28xx_unregister_extension(&dvb_ops);
  1023. }
  1024. module_init(em28xx_dvb_register);
  1025. module_exit(em28xx_dvb_unregister);