em28xx-dvb.c 31 KB

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