em28xx-dvb.c 34 KB

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